ogbase.pas 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the base stuff for binary object file writers
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. cclasses,
  24. { targets }
  25. systems,globtype,
  26. { outputwriters }
  27. owbase,
  28. { assembler }
  29. aasmbase,
  30. cpuinfo;
  31. type
  32. TObjSection = class;
  33. TObjData = class;
  34. TExeSection = class;
  35. TExeSymbol = class;
  36. TExeOutput = class;
  37. TObjRelocationType = (
  38. { Relocation to absolute address }
  39. RELOC_ABSOLUTE,
  40. {$ifdef x86_64}
  41. { 32bit Relocation to absolute address }
  42. RELOC_ABSOLUTE32,
  43. { 64 bit coff only }
  44. RELOC_RELATIVE_1,
  45. RELOC_RELATIVE_2,
  46. RELOC_RELATIVE_3,
  47. RELOC_RELATIVE_4,
  48. RELOC_RELATIVE_5,
  49. { PIC }
  50. RELOC_GOTPCREL,
  51. RELOC_PLT32,
  52. {$endif x86_64}
  53. {$ifdef i386}
  54. { PIC }
  55. RELOC_GOTPC,
  56. RELOC_GOT32,
  57. RELOC_PLT32,
  58. {$endif i386}
  59. {$ifdef i8086}
  60. RELOC_ABSOLUTE32,
  61. RELOC_RELATIVE32,
  62. RELOC_FARPTR,
  63. RELOC_FARPTR_RELATIVEOFFSET,
  64. RELOC_FARPTR48,
  65. RELOC_FARPTR48_RELATIVEOFFSET,
  66. RELOC_SEG,
  67. RELOC_SEGREL,
  68. RELOC_DGROUP,
  69. RELOC_DGROUPREL,
  70. RELOC_FARDATASEG,
  71. RELOC_FARDATASEGREL,
  72. {$endif i8086}
  73. {$ifdef arm}
  74. RELOC_RELATIVE_24,
  75. RELOC_RELATIVE_CALL,
  76. RELOC_RELATIVE_24_THUMB,
  77. RELOC_RELATIVE_CALL_THUMB,
  78. RELOC_GOT32,
  79. {$endif arm}
  80. { Relative relocation }
  81. RELOC_RELATIVE,
  82. { PECoff (Windows) RVA relocation }
  83. RELOC_RVA,
  84. { PECoff (Windows) section relocation, required by DWARF2 debug info }
  85. RELOC_SECREL32,
  86. { Generate a 0 value at the place of the relocation,
  87. this is used to remove unused vtable entries }
  88. RELOC_ZERO,
  89. { No relocation is needed. It is used in ARM object files.
  90. Also internal linker use this reloc to make virtual (not real)
  91. links to some sections }
  92. RELOC_NONE,
  93. { Darwin relocation, using PAIR }
  94. RELOC_PIC_PAIR,
  95. { Relative to GOT/gp }
  96. RELOC_GOTOFF,
  97. { Untranslated target-specific value }
  98. RELOC_RAW
  99. );
  100. {$if defined(x86_64)}
  101. { no special aliases for x86_64 }
  102. {$elseif defined(i8086)}
  103. const
  104. RELOC_ABSOLUTE16 = RELOC_ABSOLUTE;
  105. RELOC_RELATIVE16 = RELOC_RELATIVE;
  106. {$else}
  107. const
  108. RELOC_ABSOLUTE32 = RELOC_ABSOLUTE;
  109. {$endif}
  110. const
  111. { stab types }
  112. N_GSYM = $20;
  113. N_STSYM = 38; { initialized const }
  114. N_LCSYM = 40; { non initialized variable}
  115. N_Function = $24; { function or const }
  116. N_TextLine = $44;
  117. N_DataLine = $46;
  118. N_BssLine = $48;
  119. N_RSYM = $40; { register variable }
  120. N_LSYM = $80;
  121. N_tsym = 160;
  122. N_SourceFile = $64;
  123. N_IncludeFile = $84;
  124. N_BINCL = $82;
  125. N_EINCL = $A2;
  126. N_LBRAC = $C0;
  127. N_EXCL = $C2;
  128. N_RBRAC = $E0;
  129. { GNU extensions }
  130. debuglinkname='.gnu_debuglink';
  131. { TObjRelocation.flags }
  132. { 'ftype' field contains platform-specific value }
  133. rf_raw = 1;
  134. { relocation must be added to dynamic list }
  135. rf_dynamic = 2;
  136. { relocation target is absent/irrelevant (e.g. R_ARM_V4BX) }
  137. rf_nosymbol = 4;
  138. type
  139. TObjSectionOption = (
  140. { Has Data available in the file }
  141. oso_Data,
  142. { Is loaded into memory }
  143. oso_load,
  144. { Writable }
  145. oso_write,
  146. { Contains executable instructions }
  147. oso_executable,
  148. { Never discard section }
  149. oso_keep,
  150. { Procedure Linkage Table specific treatment }
  151. oso_plt,
  152. { Contains debug info and can be stripped }
  153. oso_debug,
  154. { Contains only strings }
  155. oso_strings,
  156. { Must be cloned when writing separate debug file }
  157. oso_debug_copy,
  158. { Has relocations with explicit addends (ELF-specific) }
  159. oso_rela_relocs,
  160. { Supports bss-like allocation of data, even though it is written in file (i.e. also has oso_Data) }
  161. oso_sparse_data,
  162. { Section to support the resolution of multiple symbols with the same name }
  163. oso_comdat
  164. );
  165. TObjSectionOptions = set of TObjSectionOption;
  166. TObjSectionComdatSelection = (
  167. { Section is not a COMDAT section }
  168. oscs_none,
  169. { Select any of the symbols }
  170. oscs_any,
  171. { Select any symbol, but abort if they differ in size }
  172. oscs_same_size,
  173. { Select any symbol, but abort if they differ in size or content }
  174. oscs_exact_match,
  175. { Select the symbol only if the associated symbol is linked as well }
  176. oscs_associative,
  177. { Select the largest symbol }
  178. oscs_largest
  179. );
  180. {$ifdef i8086}
  181. { allow 32-bit sections on i8086. Useful for the dwarf debug info, as well
  182. as to allow linking 32-bit obj modules. }
  183. TObjSectionOfs = LongWord;
  184. {$else i8086}
  185. TObjSectionOfs = PUInt;
  186. {$endif i8086}
  187. TObjSectionGroup = class;
  188. TObjSymbol = class(TFPHashObject)
  189. public
  190. bind : TAsmsymbind;
  191. typ : TAsmsymtype;
  192. { Current assemble pass, used to detect duplicate labels }
  193. pass : byte;
  194. { how the symbol is referenced (target-specific bitfield) }
  195. refs : byte;
  196. symidx : longint;
  197. objsection : TObjSection;
  198. offset,
  199. size : TObjSectionOfs;
  200. { Used for external and common solving during linking }
  201. exesymbol : TExeSymbol;
  202. { Darwin asm is using indirect symbols resolving }
  203. indsymbol : TObjSymbol;
  204. { Used by the OMF object format and its complicated relocation records }
  205. group: TObjSectionGroup;
  206. {$ifdef ARM}
  207. ThumbFunc : boolean;
  208. {$endif ARM}
  209. constructor create(AList:TFPHashObjectList;const AName:string);
  210. function address:qword;
  211. procedure SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  212. function ObjData: TObjData;
  213. { string representation for the linker map file }
  214. function AddressStr(AImageBase: qword): string;virtual;
  215. end;
  216. TObjSymbolClass = class of TObjSymbol;
  217. { Stabs is common for all targets }
  218. TObjStabEntry=packed record
  219. strpos : longint;
  220. ntype : byte;
  221. nother : byte;
  222. ndesc : word;
  223. nvalue : longint;
  224. end;
  225. PObjStabEntry=^TObjStabEntry;
  226. TObjRelocation = class
  227. private
  228. function GetType:TObjRelocationType;
  229. procedure SetType(v:TObjRelocationType);
  230. public
  231. DataOffset,
  232. orgsize : TObjSectionOfs; { COFF: original size of the symbol to relocate }
  233. { ELF: explicit addend }
  234. symbol : TObjSymbol;
  235. objsection : TObjSection; { only used if symbol=nil }
  236. group : TObjSectionGroup; { only used if symbol=nil and objsection=nil }
  237. ftype : byte;
  238. size : byte;
  239. flags : byte;
  240. constructor CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
  241. constructor CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
  242. constructor CreateGroup(ADataOffset:TObjSectionOfs;grp:TObjSectionGroup;Atyp:TObjRelocationType);
  243. constructor CreateRaw(ADataOffset:TObjSectionOfs;s:TObjSymbol;ARawType:byte);
  244. function TargetName:TSymStr;
  245. property typ: TObjRelocationType read GetType write SetType;
  246. end;
  247. TObjSection = class(TFPHashObject)
  248. private
  249. FData : TDynamicArray;
  250. FSecOptions : TObjSectionOptions;
  251. FCachedFullName : pshortstring;
  252. FSizeLimit : TObjSectionOfs;
  253. procedure SetSecOptions(Aoptions:TObjSectionOptions);
  254. procedure SectionTooLargeError;
  255. public
  256. ObjData : TObjData;
  257. index : longword; { index of section in section headers }
  258. SecSymIdx : longint; { index for the section in symtab }
  259. SecAlign : longint; { alignment of the section }
  260. { section Data }
  261. Size,
  262. DataPos : TObjSectionOfs;
  263. MemPos : qword;
  264. Group : TObjSectionGroup;
  265. AssociativeSection : TObjSection;
  266. ComdatSelection : TObjSectionComdatSelection;
  267. DataAlignBytes : shortint;
  268. { Relocations (=references) to other sections }
  269. ObjRelocations : TFPObjectList;
  270. { executable linking }
  271. ExeSection : TExeSection;
  272. USed : Boolean;
  273. VTRefList : TFPObjectList;
  274. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);virtual;
  275. destructor destroy;override;
  276. function write(const d;l:TObjSectionOfs):TObjSectionOfs;
  277. { writes string plus zero byte }
  278. function writestr(const s:string):TObjSectionOfs;
  279. function WriteZeros(l:longword):TObjSectionOfs;
  280. { writes content of s without null termination }
  281. function WriteBytes(const s:string):TObjSectionOfs;
  282. procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);virtual;
  283. function setmempos(mpos:qword):qword;
  284. procedure setDatapos(var dpos:TObjSectionOfs);
  285. procedure alloc(l:TObjSectionOfs);
  286. procedure addsymReloc(ofs:TObjSectionOfs;p:TObjSymbol;Reloctype:TObjRelocationType);
  287. procedure addsectionReloc(ofs:TObjSectionOfs;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  288. procedure addrawReloc(ofs:TObjSectionOfs;p:TObjSymbol;RawReloctype:byte);
  289. procedure ReleaseData;
  290. function FullName:string;
  291. { string representation for the linker map file }
  292. function MemPosStr(AImageBase: qword): string;virtual;
  293. property Data:TDynamicArray read FData;
  294. property SecOptions:TObjSectionOptions read FSecOptions write SetSecOptions;
  295. property SizeLimit:TObjSectionOfs read FSizeLimit write FSizeLimit;
  296. end;
  297. TObjSectionClass = class of TObjSection;
  298. TObjSectionGroup = class(TFPHashObject)
  299. public
  300. index: longword; { index of group in group headers }
  301. members: array of TObjSection;
  302. iscomdat: boolean;
  303. end;
  304. TObjSectionGroupClass = class of TObjSectionGroup;
  305. TString80 = string[80];
  306. TObjSymbolList = class(TFPHashObjectList)
  307. public
  308. Owner: TObjData;
  309. end;
  310. {$ifdef i8086}
  311. { on i8086 we use a longint, to support 32-bit relocations as well (e.g.
  312. for allowing 386+ instructions with 32-bit addresses in inline asm code) }
  313. TRelocDataInt = longint;
  314. {$else i8086}
  315. TRelocDataInt = aint;
  316. {$endif i8086}
  317. TObjData = class(TLinkedListItem)
  318. private
  319. FCurrObjSec : TObjSection;
  320. FObjSectionList : TFPHashObjectList;
  321. FCObjSymbol : TObjSymbolClass;
  322. FCObjSection : TObjSectionClass;
  323. FCObjSectionGroup: TObjSectionGroupClass;
  324. { Symbols that will be defined in this object file }
  325. FObjSymbolList : TObjSymbolList;
  326. FCachedAsmSymbolList : TFPObjectList;
  327. { Special info sections that are written to during object generation }
  328. FStabsObjSec,
  329. FStabStrObjSec : TObjSection;
  330. FGroupsList : TFPHashObjectList;
  331. FCPUType : tcputype;
  332. procedure section_reset(p:TObject;arg:pointer);
  333. procedure section_afteralloc(p:TObject;arg:pointer);
  334. procedure section_afterwrite(p:TObject;arg:pointer);
  335. protected
  336. FName : TString80;
  337. property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
  338. property CObjSectionGroup: TObjSectionGroupClass read FCObjSectionGroup write FCObjSectionGroup;
  339. public
  340. CurrPass : byte;
  341. ExecStack : boolean;
  342. {$ifdef ARM}
  343. ThumbFunc : boolean;
  344. {$endif ARM}
  345. constructor create(const n:string);virtual;
  346. destructor destroy;override;
  347. { Sections }
  348. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;abstract;
  349. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
  350. function sectiontype2align(atype:TAsmSectiontype):longint;virtual;
  351. function createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;virtual;
  352. function createsection(const aname:string;aalign:longint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean=true):TObjSection;virtual;
  353. function createsectiongroup(const aname:string):TObjSectionGroup;
  354. procedure CreateDebugSections;virtual;
  355. function findsection(const aname:string):TObjSection;
  356. procedure setsection(asec:TObjSection);
  357. { Symbols }
  358. function createsymbol(const aname:string):TObjSymbol;
  359. function symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  360. function symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  361. function symbolref(asmsym:TAsmSymbol):TObjSymbol;
  362. function symbolref(const aname:string):TObjSymbol;
  363. procedure ResetCachedAsmSymbols;
  364. { Allocation }
  365. procedure alloc(len:aword);
  366. procedure allocalign(len:longint);
  367. procedure writebytes(const Data;len:aword);
  368. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);virtual;abstract;
  369. procedure beforealloc;virtual;
  370. procedure beforewrite;virtual;
  371. procedure afteralloc;virtual;
  372. procedure afterwrite;virtual;
  373. procedure resetsections;
  374. procedure layoutsections(var datapos:TObjSectionOfs);
  375. property Name:TString80 read FName;
  376. property CurrObjSec:TObjSection read FCurrObjSec;
  377. property ObjSymbolList:TObjSymbolList read FObjSymbolList;
  378. property ObjSectionList:TFPHashObjectList read FObjSectionList;
  379. property GroupsList:TFPHashObjectList read FGroupsList;
  380. property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
  381. property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
  382. property CObjSymbol: TObjSymbolClass read FCObjSymbol write FCObjSymbol;
  383. { Current CPU type for the internal asm writer.
  384. Instructions, not supported by the given CPU should produce an error.
  385. A value of 'cpu_none' means no restrictions (all instructions should be accepted) }
  386. property CPUType : tcputype read FCPUType write FCPUType;
  387. end;
  388. TObjDataClass = class of TObjData;
  389. TObjOutput = class
  390. private
  391. FCObjData : TObjDataClass;
  392. protected
  393. { writer }
  394. FWriter : TObjectwriter;
  395. function writeData(Data:TObjData):boolean;virtual;abstract;
  396. property CObjData : TObjDataClass read FCObjData write FCObjData;
  397. procedure WriteSectionContent(Data:TObjData);
  398. public
  399. constructor create(AWriter:TObjectWriter);virtual;
  400. destructor destroy;override;
  401. function newObjData(const n:string):TObjData;
  402. function startObjectfile(const fn:string):boolean;
  403. function writeobjectfile(Data:TObjData):boolean;
  404. procedure exportsymbol(p:TObjSymbol);
  405. property Writer:TObjectWriter read FWriter;
  406. end;
  407. TObjOutputClass=class of TObjOutput;
  408. TObjInput = class
  409. private
  410. FCObjData : TObjDataClass;
  411. protected
  412. { reader }
  413. FReader : TObjectReader;
  414. InputFileName : string;
  415. property CObjData : TObjDataClass read FCObjData write FCObjData;
  416. procedure ReadSectionContent(Data:TObjData);
  417. public
  418. constructor create;virtual;
  419. function ReadObjData(AReader:TObjectreader;out Data:TObjData):boolean;virtual;abstract;
  420. class function CanReadObjData(AReader:TObjectreader):boolean;virtual;
  421. procedure inputerror(const s : string);
  422. end;
  423. TObjInputClass=class of TObjInput;
  424. TVTableEntry=record
  425. ObjRelocation : TObjRelocation;
  426. orgreloctype,
  427. orgrelocflags : byte;
  428. Enabled,
  429. Used : Boolean;
  430. end;
  431. PVTableEntry=^TVTableEntry;
  432. TExeVTable = class
  433. private
  434. procedure CheckIdx(VTableIdx:longint);
  435. public
  436. ExeSymbol : TExeSymbol;
  437. EntryCnt : Longint;
  438. EntryArray : PVTableEntry;
  439. Consolidated : Boolean;
  440. ChildList : TFPObjectList;
  441. constructor Create(AExeSymbol:TExeSymbol);
  442. destructor Destroy;override;
  443. procedure AddChild(vt:TExeVTable);
  444. procedure AddEntry(VTableIdx:Longint);
  445. procedure SetVTableSize(ASize:longint);
  446. function VTableRef(VTableIdx:Longint):TObjRelocation;
  447. end;
  448. TSymbolState = (
  449. symstate_undefined,
  450. symstate_undefweak, // undefined but has only weak refs - don't complain
  451. symstate_defined,
  452. symstate_defweak,
  453. symstate_common,
  454. symstate_dynamic // a matching symbol has been seen in .so
  455. );
  456. TExeSymbol = class(TFPHashObject)
  457. ObjSymbol : TObjSymbol;
  458. State : TSymbolState;
  459. used : boolean;
  460. { Used for vmt references optimization }
  461. VTable : TExeVTable;
  462. { fields for ELF linking }
  463. gotoffset : aword;
  464. dynindex : aword;
  465. { A thunk used to redirect some references to symbol (like absolute
  466. jumps/calls to PIC code).
  467. This probably is also needed for ARM/Thumb interworking and alike.
  468. TODO: consider reusing objsymbol.indsymbol for this purpose }
  469. {$ifdef mips}
  470. stubsymbol : TObjSymbol;
  471. {$endif mips}
  472. end;
  473. TExeSection = class(TFPHashObject)
  474. private
  475. FSecSymIdx : longint;
  476. FObjSectionList : TFPObjectList;
  477. public
  478. Size,
  479. DataPos,
  480. MemPos : qword;
  481. SecAlign : longint;
  482. Disabled : boolean;
  483. SecOptions : TObjSectionOptions;
  484. constructor create(AList:TFPHashObjectList;const AName:string);virtual;
  485. destructor destroy;override;
  486. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);virtual;
  487. { string representation for the linker map file }
  488. function MemPosStr(AImageBase: qword): string;virtual;
  489. property ObjSectionList:TFPObjectList read FObjSectionList;
  490. property SecSymIdx:longint read FSecSymIdx write FSecSymIdx;
  491. end;
  492. TExeSectionClass=class of TExeSection;
  493. TlibKind = (lkArchive,lkObject,lkGroup);
  494. TStaticLibrary = class(TObject)
  495. private
  496. FName : TCmdStr;
  497. FPayload : TObject; { lkArchive: TObjectReader }
  498. { lkObject: TObjData }
  499. { lkGroup: TFPObjectList }
  500. FObjInputClass : TObjInputClass;
  501. FKind: TlibKind;
  502. FAsNeeded : Boolean;
  503. function GetArReader:TObjectReader;
  504. function GetGroupMembers:TFPObjectList;
  505. function GetObjData:TObjData;
  506. public
  507. constructor create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  508. constructor create_object(AObjData:TObjData);
  509. constructor create_group;
  510. destructor destroy;override;
  511. property ArReader:TObjectReader read GetArReader;
  512. property ObjInputClass:TObjInputClass read FObjInputClass;
  513. property GroupMembers:TFPObjectList read GetGroupMembers;
  514. property ObjData:TObjData read GetObjData;
  515. property AsNeeded:Boolean read FAsNeeded write FAsNeeded;
  516. property Kind:TLibKind read FKind;
  517. end;
  518. TImportLibrary = class(TFPHashObject)
  519. private
  520. FImportSymbolList : TFPHashObjectList;
  521. public
  522. constructor create(AList:TFPHashObjectList;const AName:string);
  523. destructor destroy;override;
  524. property ImportSymbolList:TFPHashObjectList read FImportSymbolList;
  525. end;
  526. TImportSymbol = class(TFPHashObject)
  527. private
  528. FOrdNr : longint;
  529. FIsVar : boolean;
  530. FMangledName : string;
  531. FCachedExeSymbol: TExeSymbol;
  532. public
  533. constructor create(AList:TFPHashObjectList;const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
  534. property OrdNr: longint read FOrdNr;
  535. property MangledName: string read FMangledName;
  536. property IsVar: boolean read FIsVar;
  537. property CachedExeSymbol: TExeSymbol read FCachedExeSymbol write FCachedExeSymbol;
  538. end;
  539. TExeWriteMode = (ewm_exefull,ewm_dbgonly,ewm_exeonly);
  540. TExeOutput = class
  541. private
  542. { ExeSectionList }
  543. FCObjSymbol : TObjSymbolClass;
  544. FCObjData : TObjDataClass;
  545. FCExeSection : TExeSectionClass;
  546. FCurrExeSec : TExeSection;
  547. FExeSectionList : TFPHashObjectList;
  548. Fzeronr : longint;
  549. Fvaluesnr : longint;
  550. { Symbols }
  551. FExeSymbolList : TFPHashObjectList;
  552. FUnresolvedExeSymbols : TFPObjectList;
  553. FExternalObjSymbols,
  554. FCommonObjSymbols : TFPObjectList;
  555. FProvidedObjSymbols : TFPObjectList;
  556. FIndirectObjSymbols : TFPObjectList;
  557. FEntryName : string;
  558. FExeVTableList : TFPObjectList;
  559. { Objects }
  560. FObjDataList : TFPObjectList;
  561. { Position calculation }
  562. FImageBase : aword;
  563. FCurrMemPos : qword;
  564. procedure SetCurrMemPos(const AValue: qword);
  565. protected
  566. { writer }
  567. FExeWriteMode : TExeWriteMode;
  568. FWriter : TObjectwriter;
  569. commonObjSection : TObjSection;
  570. internalObjData : TObjData;
  571. EntrySym : TObjSymbol;
  572. SectionDataAlign,
  573. SectionMemAlign : aword;
  574. ComdatGroups : TFPHashList;
  575. FixedSectionAlign : boolean;
  576. AllowUndefinedSymbols : boolean;
  577. function writeData:boolean;virtual;abstract;
  578. property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
  579. property CObjData:TObjDataClass read FCObjData write FCObjData;
  580. property CObjSymbol:TObjSymbolClass read FCObjSymbol write FCObjSymbol;
  581. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);virtual;
  582. procedure WriteExeSectionContent;
  583. procedure DoRelocationFixup(objsec:TObjSection);virtual;abstract;
  584. function MemAlign(exesec: TExeSection): longword;
  585. function DataAlign(exesec: TExeSection): longword;
  586. procedure ReplaceExeSectionList(newlist: TFPList);
  587. public
  588. CurrDataPos : aword;
  589. MaxMemPos : qword;
  590. IsSharedLibrary : boolean;
  591. ExecStack : boolean;
  592. constructor create;virtual;
  593. destructor destroy;override;
  594. function FindExeSection(const aname:string):TExeSection;
  595. procedure AddObjData(ObjData:TObjData);
  596. procedure Load_Start;virtual;
  597. procedure Load_EntryName(const aname:string);virtual;
  598. procedure Load_Symbol(const aname:string);virtual;
  599. procedure Load_ProvideSymbol(const aname:string);virtual;
  600. procedure Load_IsSharedLibrary;
  601. procedure Load_ImageBase(const avalue:string);
  602. procedure Load_DynamicObject(ObjData:TObjData;asneeded:boolean);virtual;
  603. procedure Order_Start;virtual;
  604. procedure Order_End;virtual;
  605. procedure Order_ExeSection(const aname:string);virtual;
  606. procedure Order_Align(const avalue:string);virtual;
  607. procedure Order_Zeros(const avalue:string);virtual;
  608. procedure Order_Values(bytesize : aword; const avalue:string);virtual;
  609. procedure Order_Symbol(const aname:string);virtual;
  610. procedure Order_ProvideSymbol(const aname:string);virtual;
  611. procedure Order_EndExeSection;virtual;
  612. procedure Order_ObjSection(const aname:string);virtual;
  613. procedure MemPos_Start;virtual;
  614. procedure MemPos_Header;virtual;
  615. procedure MemPos_ExeSection(exesec:TExeSection);
  616. procedure MemPos_ExeSection(const aname:string);virtual;
  617. procedure MemPos_EndExeSection;virtual;
  618. procedure DataPos_Start;virtual;
  619. procedure DataPos_Header;virtual;
  620. procedure DataPos_ExeSection(exesec:TExeSection);
  621. procedure DataPos_ExeSection(const aname:string);virtual;
  622. procedure DataPos_EndExeSection;virtual;
  623. procedure DataPos_Symbols;virtual;
  624. procedure BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  625. procedure PackUnresolvedExeSymbols(const s:string);
  626. procedure ResolveSymbols(StaticLibraryList:TFPObjectList);
  627. procedure PrintMemoryMap;
  628. procedure FixupSymbols;
  629. procedure FixupRelocations;virtual;
  630. procedure RemoveUnusedExeSymbols;
  631. procedure MergeStabs;
  632. procedure MarkEmptySections;
  633. procedure RemoveUnreferencedSections;
  634. procedure RemoveDisabledSections;
  635. procedure RemoveDebugInfo;
  636. procedure MarkTargetSpecificSections(WorkList:TFPObjectList);virtual;
  637. procedure AfterUnusedSectionRemoval;virtual;
  638. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);virtual;
  639. procedure GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  640. function WriteExeFile(const fn:string):boolean;
  641. procedure ParseScript (linkscript:TCmdStrList); virtual;
  642. property Writer:TObjectWriter read FWriter;
  643. property ExeSectionList:TFPHashObjectList read FExeSectionList;
  644. property ObjDataList:TFPObjectList read FObjDataList;
  645. property ExeSymbolList:TFPHashObjectList read FExeSymbolList;
  646. property UnresolvedExeSymbols:TFPObjectList read FUnresolvedExeSymbols;
  647. property ExternalObjSymbols:TFPObjectList read FExternalObjSymbols;
  648. property CommonObjSymbols:TFPObjectList read FCommonObjSymbols;
  649. property IndirectObjSymbols:TFPObjectList read FIndirectObjSymbols;
  650. property ExeVTableList:TFPObjectList read FExeVTableList;
  651. property EntryName:string read FEntryName write FEntryName;
  652. property ImageBase:aword read FImageBase write FImageBase;
  653. property CurrExeSec:TExeSection read FCurrExeSec;
  654. property ExeWriteMode:TExeWriteMode read FExeWriteMode write FExeWriteMode;
  655. property CurrMemPos:qword read FCurrMemPos write SetCurrMemPos;
  656. end;
  657. TExeOutputClass=class of TExeOutput;
  658. const
  659. SectionDataMaxGrow = 4096;
  660. var
  661. exeoutput : TExeOutput;
  662. function align_aword(v:aword;a:longword):aword;
  663. function align_qword(v:qword;a:longword):qword;
  664. function align_objsecofs(v:TObjSectionOfs;a:longword):TObjSectionOfs;
  665. implementation
  666. uses
  667. SysUtils,
  668. globals,verbose,ogmap;
  669. {$ifdef MEMDEBUG}
  670. var
  671. memobjsymbols,
  672. memobjsections : TMemDebug;
  673. {$endif MEMDEBUG}
  674. {*****************************************************************************
  675. Helpers
  676. *****************************************************************************}
  677. function align_aword(v:aword;a:longword):aword;
  678. begin
  679. if a<=1 then
  680. result:=v
  681. else
  682. result:=((v+a-1) div a) * a;
  683. end;
  684. function align_qword(v:qword;a:longword):qword;
  685. begin
  686. if a<=1 then
  687. result:=v
  688. else
  689. result:=((v+a-1) div a) * a;
  690. end;
  691. function align_objsecofs(v:TObjSectionOfs;a:longword):TObjSectionOfs;
  692. begin
  693. if a<=1 then
  694. result:=v
  695. else
  696. result:=((v+a-1) div a) * a;
  697. end;
  698. procedure MaybeSwapStab(var v:TObjStabEntry);
  699. begin
  700. if source_info.endian<>target_info.endian then
  701. begin
  702. v.strpos:=SwapEndian(v.strpos);
  703. v.nvalue:=SwapEndian(v.nvalue);
  704. v.ndesc:=SwapEndian(v.ndesc);
  705. end;
  706. end;
  707. {*****************************************************************************
  708. TObjSymbol
  709. *****************************************************************************}
  710. constructor TObjSymbol.create(AList:TFPHashObjectList;const AName:string);
  711. begin
  712. inherited create(AList,AName);
  713. bind:=AB_EXTERNAL;
  714. typ:=AT_NONE;
  715. symidx:=-1;
  716. size:=0;
  717. offset:=0;
  718. objsection:=nil;
  719. end;
  720. function TObjSymbol.address:qword;
  721. begin
  722. if assigned(objsection) then
  723. result:=offset+objsection.mempos
  724. else
  725. result:=0;
  726. end;
  727. procedure TObjSymbol.SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  728. begin
  729. if not(abind in [AB_GLOBAL,AB_PRIVATE_EXTERN,AB_LOCAL,AB_COMMON,AB_IMPORT]) then
  730. internalerror(200603016);
  731. if not assigned(aobjsec) then
  732. internalerror(200603017);
  733. if (bind in [AB_EXTERNAL,AB_LAZY]) or
  734. { Put all COMMON to GLOBAL in step 3 of
  735. TExeOutput.ResolveSymbols }
  736. ((abind=AB_GLOBAL) and (bind=AB_COMMON)) then
  737. begin
  738. { Do not change the AB_TYPE of common symbols yet }
  739. { This will be done in FixupSymbols }
  740. if (pass<>0) or (bind<>AB_COMMON) then
  741. bind:=abind;
  742. typ:=atyp;
  743. end
  744. else
  745. begin
  746. if pass=apass then
  747. begin
  748. Message1(asmw_e_duplicate_label,name);
  749. exit;
  750. end;
  751. end;
  752. pass:=apass;
  753. { Code can never grow after a pass }
  754. if assigned(objsection) and
  755. (objsection=aobjsec) and
  756. (aobjsec.size>offset) then
  757. internalerror(200603014);
  758. objsection:=aobjsec;
  759. offset:=aobjsec.size;
  760. end;
  761. function TObjSymbol.ObjData: TObjData;
  762. begin
  763. result:=(OwnerList as TObjSymbolList).Owner;
  764. end;
  765. function TObjSymbol.AddressStr(AImageBase: qword): string;
  766. begin
  767. Result:='0x'+HexStr(address+Aimagebase,sizeof(pint)*2);
  768. end;
  769. {****************************************************************************
  770. TObjRelocation
  771. ****************************************************************************}
  772. constructor TObjRelocation.CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
  773. begin
  774. if not assigned(s) then
  775. internalerror(200603034);
  776. DataOffset:=ADataOffset;
  777. Symbol:=s;
  778. OrgSize:=0;
  779. Group:=nil;
  780. ObjSection:=nil;
  781. ftype:=ord(Atyp);
  782. end;
  783. constructor TObjRelocation.CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
  784. begin
  785. if not assigned(aobjsec) then
  786. internalerror(200603036);
  787. DataOffset:=ADataOffset;
  788. Symbol:=nil;
  789. OrgSize:=0;
  790. Group:=nil;
  791. ObjSection:=aobjsec;
  792. ftype:=ord(Atyp);
  793. end;
  794. constructor TObjRelocation.CreateGroup(ADataOffset:TObjSectionOfs;grp:TObjSectionGroup;Atyp:TObjRelocationType);
  795. begin
  796. if not assigned(grp) then
  797. internalerror(2015111201);
  798. DataOffset:=ADataOffset;
  799. Symbol:=nil;
  800. ObjSection:=nil;
  801. OrgSize:=0;
  802. Group:=grp;
  803. ftype:=ord(Atyp);
  804. end;
  805. constructor TObjRelocation.CreateRaw(ADataOffset:TObjSectionOfs;s:TObjSymbol;ARawType:byte);
  806. begin
  807. { nil symbol is allowed here }
  808. DataOffset:=ADataOffset;
  809. Symbol:=s;
  810. ObjSection:=nil;
  811. Group:=nil;
  812. orgsize:=0;
  813. ftype:=ARawType;
  814. flags:=rf_raw;
  815. end;
  816. function TObjRelocation.GetType:TObjRelocationType;
  817. begin
  818. if (flags and rf_raw)=0 then
  819. result:=TObjRelocationType(ftype)
  820. else
  821. result:=RELOC_RAW;
  822. end;
  823. procedure TObjRelocation.SetType(v:TObjRelocationType);
  824. begin
  825. ftype:=ord(v);
  826. flags:=flags and (not rf_raw);
  827. end;
  828. function TObjRelocation.TargetName:TSymStr;
  829. begin
  830. if assigned(symbol) then
  831. if symbol.typ=AT_SECTION then
  832. result:=symbol.objsection.name
  833. else
  834. result:=symbol.Name
  835. else
  836. result:=objsection.Name;
  837. end;
  838. {****************************************************************************
  839. TObjSection
  840. ****************************************************************************}
  841. constructor TObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);
  842. begin
  843. inherited Create(AList,Aname);
  844. { Data }
  845. Size:=0;
  846. Datapos:=0;
  847. mempos:=0;
  848. FData:=Nil;
  849. {$ifdef i8086}
  850. FSizeLimit:=high(word);
  851. {$else i8086}
  852. FSizeLimit:=high(TObjSectionOfs);
  853. {$endif i8086}
  854. { Setting the secoptions allocates Data if needed }
  855. secoptions:=Aoptions;
  856. secalign:=Aalign;
  857. secsymidx:=0;
  858. { relocation }
  859. ObjRelocations:=TFPObjectList.Create(true);
  860. VTRefList:=TFPObjectList.Create(false);
  861. end;
  862. destructor TObjSection.destroy;
  863. begin
  864. if assigned(Data) then
  865. Data.Free;
  866. stringdispose(FCachedFullName);
  867. ObjRelocations.Free;
  868. VTRefList.Free;
  869. inherited destroy;
  870. end;
  871. procedure TObjSection.SetSecOptions(Aoptions:TObjSectionOptions);
  872. begin
  873. FSecOptions:=FSecOptions+AOptions;
  874. if (oso_Data in secoptions) and
  875. not assigned(FData) then
  876. FData:=TDynamicArray.Create(SectionDataMaxGrow);
  877. end;
  878. procedure TObjSection.SectionTooLargeError;
  879. begin
  880. if oso_executable in SecOptions then
  881. Message(asmw_f_code_segment_too_large)
  882. else
  883. Message(asmw_f_data_segment_too_large);
  884. end;
  885. function TObjSection.write(const d;l:TObjSectionOfs):TObjSectionOfs;
  886. begin
  887. result:=size;
  888. if assigned(Data) then
  889. begin
  890. if Size<>Data.size then
  891. internalerror(200602281);
  892. {$ifndef cpu64bitalu}
  893. if (qword(size)+l)>SizeLimit then
  894. SectionTooLargeError;
  895. {$endif}
  896. Data.write(d,l);
  897. inc(Size,l);
  898. end
  899. else
  900. internalerror(200602289);
  901. end;
  902. function TObjSection.writestr(const s:string):TObjSectionOfs;
  903. var
  904. b: byte;
  905. begin
  906. result:=Write(s[1],length(s));
  907. b:=0;
  908. Write(b,1);
  909. end;
  910. function TObjSection.WriteBytes(const s:string):TObjSectionOfs;
  911. begin
  912. result:=Write(s[1],length(s));
  913. end;
  914. function TObjSection.WriteZeros(l:longword):TObjSectionOfs;
  915. var
  916. empty : array[0..1023] of byte;
  917. begin
  918. result:=Size;
  919. if l>sizeof(empty) then
  920. begin
  921. fillchar(empty,sizeof(empty),0);
  922. while l>sizeof(empty) do
  923. begin
  924. Write(empty,sizeof(empty));
  925. Dec(l,sizeof(empty));
  926. end;
  927. if l>0 then
  928. Write(empty,l);
  929. end
  930. else if l>0 then
  931. begin
  932. fillchar(empty,l,0);
  933. Write(empty,l);
  934. end;
  935. end;
  936. { Writes relocation to (section+offset) without need to have a symbol at that location.
  937. Not an abstract method because not every backend needs this functionality. }
  938. procedure TObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  939. begin
  940. InternalError(2012081501);
  941. end;
  942. procedure TObjSection.setDatapos(var dpos:TObjSectionOfs);
  943. begin
  944. if oso_Data in secoptions then
  945. begin
  946. { get aligned Datapos }
  947. Datapos:=align_aword(dpos,secalign);
  948. Dataalignbytes:=Datapos-dpos;
  949. { return updated Datapos }
  950. dpos:=Datapos+size;
  951. end
  952. else
  953. Datapos:=dpos;
  954. end;
  955. function TObjSection.setmempos(mpos:qword):qword;
  956. begin
  957. mempos:=align_qword(mpos,secalign);
  958. { return updated mempos }
  959. result:=mempos+size;
  960. end;
  961. procedure TObjSection.alloc(l:TObjSectionOfs);
  962. begin
  963. {$ifndef cpu64bitalu}
  964. if (qword(size)+l)>SizeLimit then
  965. SectionTooLargeError;
  966. {$endif}
  967. if oso_sparse_data in SecOptions then
  968. WriteZeros(l)
  969. else
  970. inc(size,l);
  971. end;
  972. procedure TObjSection.addsymReloc(ofs:TObjSectionOfs;p:TObjSymbol;Reloctype:TObjRelocationType);
  973. begin
  974. ObjRelocations.Add(TObjRelocation.CreateSymbol(ofs,p,reloctype));
  975. end;
  976. procedure TObjSection.addsectionReloc(ofs:TObjSectionOfs;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  977. begin
  978. ObjRelocations.Add(TObjRelocation.CreateSection(ofs,aobjsec,reloctype));
  979. end;
  980. procedure TObjSection.addrawReloc(ofs:TObjSectionOfs;p:TObjSymbol;RawReloctype:byte);
  981. begin
  982. ObjRelocations.Add(TObjRelocation.CreateRaw(ofs,p,RawReloctype));
  983. end;
  984. procedure TObjSection.ReleaseData;
  985. begin
  986. if assigned(FData) then
  987. begin
  988. FData.free;
  989. FData:=nil;
  990. end;
  991. ObjRelocations.free;
  992. ObjRelocations:=nil;
  993. if assigned(FCachedFullName) then
  994. begin
  995. stringdispose(FCachedFullName);
  996. FCachedFullName:=nil;
  997. end;
  998. end;
  999. function TObjSection.FullName:string;
  1000. begin
  1001. if not assigned(FCachedFullName) then
  1002. begin
  1003. if assigned(ObjData) then
  1004. FCachedFullName:=stringdup(ObjData.Name+'('+Name+')')
  1005. else
  1006. FCachedFullName:=stringdup(Name);
  1007. end;
  1008. result:=FCachedFullName^;
  1009. end;
  1010. function TObjSection.MemPosStr(AImageBase: qword): string;
  1011. begin
  1012. result:='0x'+HexStr(mempos+AImageBase,sizeof(pint)*2);
  1013. end;
  1014. {****************************************************************************
  1015. TObjData
  1016. ****************************************************************************}
  1017. constructor TObjData.create(const n:string);
  1018. begin
  1019. inherited create;
  1020. FName:=ExtractFileName(n);
  1021. FObjSectionList:=TFPHashObjectList.Create(true);
  1022. FStabsObjSec:=nil;
  1023. FStabStrObjSec:=nil;
  1024. { symbols }
  1025. FCObjSymbol:=TObjSymbol;
  1026. FObjSymbolList:=TObjSymbolList.Create(true);
  1027. FObjSymbolList.Owner:=Self;
  1028. FCachedAsmSymbolList:=TFPObjectList.Create(false);
  1029. { section class type for creating of new sections }
  1030. FCObjSection:=TObjSection;
  1031. FCObjSectionGroup:=TObjSectionGroup;
  1032. {$ifdef ARM}
  1033. ThumbFunc:=false;
  1034. {$endif ARM}
  1035. end;
  1036. destructor TObjData.destroy;
  1037. begin
  1038. { Symbols }
  1039. {$ifdef MEMDEBUG}
  1040. MemObjSymbols.Start;
  1041. {$endif}
  1042. ResetCachedAsmSymbols;
  1043. FCachedAsmSymbolList.free;
  1044. FObjSymbolList.free;
  1045. {$ifdef MEMDEBUG}
  1046. MemObjSymbols.Stop;
  1047. {$endif}
  1048. GroupsList.free;
  1049. { Sections }
  1050. {$ifdef MEMDEBUG}
  1051. MemObjSections.Start;
  1052. {$endif}
  1053. FObjSectionList.free;
  1054. {$ifdef MEMDEBUG}
  1055. MemObjSections.Stop;
  1056. {$endif}
  1057. inherited destroy;
  1058. end;
  1059. function TObjData.sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;
  1060. const
  1061. secoptions : array[TAsmSectiontype] of TObjSectionOptions = ([],
  1062. {user} [oso_Data,oso_load,oso_write],
  1063. {code} [oso_Data,oso_load,oso_executable],
  1064. {Data} [oso_Data,oso_load,oso_write],
  1065. { Readonly data with relocations must be initially writable for some targets.
  1066. Moreover, e.g. for ELF it depends on whether the executable is linked statically or
  1067. dynamically. Here we declare it writable, target-specific descendants must provide
  1068. further handling. }
  1069. {roData} [oso_Data,oso_load,oso_write],
  1070. {roData_norel} [oso_Data,oso_load],
  1071. {bss} [oso_load,oso_write],
  1072. {threadvar} [oso_load,oso_write],
  1073. {pdata} [oso_data,oso_load],
  1074. {stub} [oso_Data,oso_load,oso_executable],
  1075. {data_nonlazy} [oso_Data,oso_load,oso_write],
  1076. {data_lazy} [oso_Data,oso_load,oso_write],
  1077. {init_func} [oso_Data,oso_load],
  1078. {term_func} [oso_Data,oso_load],
  1079. {stab} [oso_Data,oso_debug],
  1080. {stabstr} [oso_Data,oso_strings,oso_debug],
  1081. {iData2} [oso_Data,oso_load,oso_write],
  1082. {iData4} [oso_Data,oso_load,oso_write],
  1083. {iData5} [oso_Data,oso_load,oso_write],
  1084. {iData6} [oso_Data,oso_load,oso_write],
  1085. {iData7} [oso_Data,oso_load,oso_write],
  1086. {eData} [oso_Data,oso_load],
  1087. {eh_frame} [oso_Data,oso_load],
  1088. {debug_frame} [oso_Data,oso_debug],
  1089. {debug_info} [oso_Data,oso_debug],
  1090. {debug_line} [oso_Data,oso_debug],
  1091. {debug_abbrev} [oso_Data,oso_debug],
  1092. {debug_aranges} [oso_Data,oso_debug],
  1093. {debug_ranges} [oso_Data,oso_debug],
  1094. {fpc} [oso_Data,oso_load,oso_write],
  1095. {toc} [oso_Data,oso_load],
  1096. {init} [oso_Data,oso_load,oso_executable],
  1097. {fini} [oso_Data,oso_load,oso_executable],
  1098. {objc_class} [oso_data,oso_load],
  1099. {objc_meta_class} [oso_data,oso_load],
  1100. {objc_cat_cls_meth} [oso_data,oso_load],
  1101. {objc_cat_inst_meth} [oso_data,oso_load],
  1102. {objc_protocol} [oso_data,oso_load],
  1103. {objc_string_object} [oso_data,oso_load],
  1104. {objc_cls_meth} [oso_data,oso_load],
  1105. {objc_inst_meth} [oso_data,oso_load],
  1106. {objc_cls_refs} [oso_data,oso_load],
  1107. {objc_message_refs} [oso_data,oso_load],
  1108. {objc_symbols} [oso_data,oso_load],
  1109. {objc_category} [oso_data,oso_load],
  1110. {objc_class_vars} [oso_data,oso_load],
  1111. {objc_instance_vars} [oso_data,oso_load],
  1112. {objc_module_info} [oso_data,oso_load],
  1113. {objc_class_names} [oso_data,oso_load],
  1114. {objc_meth_var_types} [oso_data,oso_load],
  1115. {objc_meth_var_names} [oso_data,oso_load],
  1116. {objc_selector_strs} [oso_data,oso_load],
  1117. {objc_protocol_ext} [oso_data,oso_load],
  1118. {objc_class_ext} [oso_data,oso_load],
  1119. {objc_property} [oso_data,oso_load],
  1120. {objc_image_info} [oso_data,oso_load],
  1121. {objc_cstring_object} [oso_data,oso_load],
  1122. {objc_sel_fixup} [oso_data,oso_load],
  1123. {sec_objc_data} [oso_data,oso_load],
  1124. {sec_objc_const} [oso_data,oso_load],
  1125. {sec_objc_sup_refs} [oso_data,oso_load],
  1126. {sec_data_coalesced} [oso_data,oso_load],
  1127. {sec_objc_classlist} [oso_data,oso_load],
  1128. {sec_objc_nlclasslist} [oso_data,oso_load],
  1129. {sec_objc_catlist} [oso_data,oso_load],
  1130. {sec_objc_nlcatlist} [oso_data,oso_load],
  1131. {sec_objc_protolist'} [oso_data,oso_load],
  1132. {stack} [oso_load,oso_write],
  1133. {heap} [oso_load,oso_write],
  1134. {gcc_except_table} [oso_data,oso_load]
  1135. );
  1136. begin
  1137. result:=secoptions[atype];
  1138. end;
  1139. function TObjData.sectiontype2align(atype:TAsmSectiontype):longint;
  1140. begin
  1141. case atype of
  1142. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges:
  1143. result:=1;
  1144. sec_code,
  1145. sec_bss,
  1146. sec_data:
  1147. result:=16;
  1148. { For idata (at least idata2) it must be 4 bytes, because
  1149. an entry is always (also in win64) 20 bytes and aligning
  1150. on 8 bytes will insert 4 bytes between the entries resulting
  1151. in a corrupt idata section.
  1152. Same story with .pdata, it has 4-byte elements which should
  1153. be packed without gaps. }
  1154. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_pdata:
  1155. result:=4;
  1156. else
  1157. result:=sizeof(pint);
  1158. end;
  1159. end;
  1160. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  1161. begin
  1162. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  1163. end;
  1164. function TObjData.createsection(const aname:string;aalign:longint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  1165. begin
  1166. if DiscardDuplicate then
  1167. result:=TObjSection(FObjSectionList.Find(aname))
  1168. else
  1169. result:=nil;
  1170. if not assigned(result) then
  1171. begin
  1172. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  1173. result.ObjData:=self;
  1174. end;
  1175. FCurrObjSec:=result;
  1176. end;
  1177. function TObjData.CreateSectionGroup(const aname:string):TObjSectionGroup;
  1178. begin
  1179. if FGroupsList=nil then
  1180. FGroupsList:=TFPHashObjectList.Create(true);
  1181. result:=CObjSectionGroup.Create(FGroupsList,aname);
  1182. end;
  1183. procedure TObjData.CreateDebugSections;
  1184. begin
  1185. end;
  1186. function TObjData.FindSection(const aname:string):TObjSection;
  1187. begin
  1188. result:=TObjSection(FObjSectionList.Find(aname));
  1189. end;
  1190. procedure TObjData.setsection(asec:TObjSection);
  1191. begin
  1192. if asec.ObjData<>self then
  1193. internalerror(200403041);
  1194. FCurrObjSec:=asec;
  1195. end;
  1196. function TObjData.createsymbol(const aname:string):TObjSymbol;
  1197. begin
  1198. result:=TObjSymbol(FObjSymbolList.Find(aname));
  1199. if not assigned(result) then
  1200. result:=CObjSymbol.Create(FObjSymbolList,aname);
  1201. {$ifdef ARM}
  1202. result.ThumbFunc:=ThumbFunc;
  1203. ThumbFunc:=false;
  1204. {$endif ARM}
  1205. end;
  1206. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  1207. begin
  1208. if assigned(asmsym) then
  1209. begin
  1210. if asmsym.typ = AT_NONE then
  1211. InternalError(2018062800);
  1212. if not assigned(asmsym.cachedObjSymbol) then
  1213. begin
  1214. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  1215. asmsym.cachedObjSymbol:=result;
  1216. FCachedAsmSymbolList.add(asmsym);
  1217. end
  1218. else
  1219. begin
  1220. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1221. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  1222. end;
  1223. end
  1224. else
  1225. result:=nil;
  1226. end;
  1227. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  1228. begin
  1229. if not assigned(CurrObjSec) then
  1230. internalerror(200603051);
  1231. result:=CreateSymbol(aname);
  1232. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  1233. end;
  1234. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  1235. var
  1236. s:string;
  1237. begin
  1238. if assigned(asmsym) then
  1239. begin
  1240. if not assigned(asmsym.cachedObjSymbol) then
  1241. begin
  1242. s:=asmsym.name;
  1243. result:=TObjSymbol(FObjSymbolList.Find(s));
  1244. if result=nil then
  1245. begin
  1246. result:=CObjSymbol.Create(FObjSymbolList,s);
  1247. if asmsym.bind=AB_WEAK_EXTERNAL then
  1248. result.bind:=AB_WEAK_EXTERNAL;
  1249. end;
  1250. asmsym.cachedObjSymbol:=result;
  1251. FCachedAsmSymbolList.add(asmsym);
  1252. end
  1253. else
  1254. result:=TObjSymbol(asmsym.cachedObjSymbol);
  1255. { The weak bit could have been removed from asmsym. }
  1256. if (asmsym.bind=AB_EXTERNAL) and (result.bind=AB_WEAK_EXTERNAL) then
  1257. result.bind:=AB_EXTERNAL;
  1258. end
  1259. else
  1260. result:=nil;
  1261. end;
  1262. function TObjData.symbolref(const aname:string):TObjSymbol;
  1263. begin
  1264. if not assigned(CurrObjSec) then
  1265. internalerror(200603052);
  1266. result:=CreateSymbol(aname);
  1267. end;
  1268. procedure TObjData.ResetCachedAsmSymbols;
  1269. var
  1270. i : longint;
  1271. begin
  1272. for i:=0 to FCachedAsmSymbolList.Count-1 do
  1273. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  1274. FCachedAsmSymbolList.Clear;
  1275. end;
  1276. procedure TObjData.writebytes(const Data;len:aword);
  1277. begin
  1278. if not assigned(CurrObjSec) then
  1279. internalerror(200402251);
  1280. CurrObjSec.write(Data,len);
  1281. end;
  1282. procedure TObjData.alloc(len:aword);
  1283. begin
  1284. if not assigned(CurrObjSec) then
  1285. internalerror(200402252);
  1286. CurrObjSec.alloc(len);
  1287. end;
  1288. procedure TObjData.allocalign(len:longint);
  1289. begin
  1290. if not assigned(CurrObjSec) then
  1291. internalerror(200402253);
  1292. CurrObjSec.alloc(align_objsecofs(CurrObjSec.size,len)-CurrObjSec.size);
  1293. end;
  1294. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  1295. begin
  1296. with TObjSection(p) do
  1297. alloc(align_objsecofs(size,secalign)-size);
  1298. end;
  1299. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  1300. begin
  1301. with TObjSection(p) do
  1302. begin
  1303. if assigned(Data) then
  1304. writezeros(align_objsecofs(size,secalign)-size);
  1305. end;
  1306. end;
  1307. procedure TObjData.section_reset(p:TObject;arg:pointer);
  1308. begin
  1309. with TObjSection(p) do
  1310. begin
  1311. Size:=0;
  1312. Datapos:=0;
  1313. mempos:=0;
  1314. if assigned(Data) then
  1315. Data.reset;
  1316. end;
  1317. end;
  1318. procedure TObjData.beforealloc;
  1319. begin
  1320. FCPUType:=current_settings.cputype;
  1321. { create stabs sections if debugging }
  1322. if assigned(StabsSec) then
  1323. begin
  1324. StabsSec.Alloc(sizeof(TObjStabEntry));
  1325. StabStrSec.Alloc(1);
  1326. end;
  1327. end;
  1328. procedure TObjData.beforewrite;
  1329. begin
  1330. FCPUType:=current_settings.cputype;
  1331. { create stabs sections if debugging }
  1332. if assigned(StabsSec) then
  1333. begin
  1334. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  1335. StabsSec.WriteZeros(sizeof(TObjStabEntry));
  1336. { start of stabstr }
  1337. StabStrSec.writeZeros(1);
  1338. end;
  1339. end;
  1340. procedure TObjData.afteralloc;
  1341. begin
  1342. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  1343. end;
  1344. procedure TObjData.afterwrite;
  1345. var
  1346. hstab : TObjStabEntry;
  1347. begin
  1348. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  1349. { For the stab section we need an HdrSym which can now be
  1350. calculated more easily }
  1351. if assigned(StabsSec) then
  1352. begin
  1353. { end of stabstr }
  1354. StabStrSec.writeZeros(1);
  1355. { header stab }
  1356. hstab.strpos:=1;
  1357. hstab.ntype:=0;
  1358. hstab.nother:=0;
  1359. {$push}{$R-}
  1360. { for jwawindows.pas, this causes an range check error, it contains too much stab symbols }
  1361. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  1362. {$pop}
  1363. hstab.nvalue:=StabStrSec.Size;
  1364. MaybeSwapStab(hstab);
  1365. StabsSec.Data.seek(0);
  1366. StabsSec.Data.write(hstab,sizeof(hstab));
  1367. end;
  1368. end;
  1369. procedure TObjData.resetsections;
  1370. begin
  1371. FObjSectionList.ForEachCall(@section_reset,nil);
  1372. end;
  1373. procedure TObjData.layoutsections(var DataPos:TObjSectionOfs);
  1374. var
  1375. i: longint;
  1376. begin
  1377. for i:=0 to FObjSectionList.Count-1 do
  1378. TObjSection(FObjSectionList[i]).setDatapos(DataPos);
  1379. end;
  1380. {****************************************************************************
  1381. TObjOutput
  1382. ****************************************************************************}
  1383. constructor TObjOutput.create(AWriter:TObjectWriter);
  1384. begin
  1385. FWriter:=AWriter;
  1386. CObjData:=TObjData;
  1387. end;
  1388. destructor TObjOutput.destroy;
  1389. begin
  1390. inherited destroy;
  1391. end;
  1392. function TObjOutput.newObjData(const n:string):TObjData;
  1393. begin
  1394. result:=CObjData.create(n);
  1395. if (cs_use_lineinfo in current_settings.globalswitches) or
  1396. (cs_debuginfo in current_settings.moduleswitches) then
  1397. result.CreateDebugSections;
  1398. end;
  1399. function TObjOutput.startObjectfile(const fn:string):boolean;
  1400. begin
  1401. result:=false;
  1402. { start the writer already, so the .a generation can initialize
  1403. the position of the current objectfile }
  1404. if not FWriter.createfile(fn) then
  1405. Comment(V_Fatal,'Can''t create object '+fn);
  1406. result:=true;
  1407. end;
  1408. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1409. begin
  1410. if errorcount=0 then
  1411. result:=writeData(Data)
  1412. else
  1413. result:=true;
  1414. { close the writer }
  1415. FWriter.closefile;
  1416. end;
  1417. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1418. begin
  1419. { export globals and common symbols, this is needed
  1420. for .a files }
  1421. if p.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN,AB_COMMON] then
  1422. FWriter.writesym(p.name);
  1423. end;
  1424. procedure TObjOutput.WriteSectionContent(Data:TObjData);
  1425. var
  1426. i:longint;
  1427. sec:TObjSection;
  1428. begin
  1429. for i:=0 to Data.ObjSectionList.Count-1 do
  1430. begin
  1431. sec:=TObjSection(Data.ObjSectionList[i]);
  1432. if (oso_data in sec.SecOptions) then
  1433. begin
  1434. if sec.Data=nil then
  1435. internalerror(200403073);
  1436. FWriter.writezeros(sec.dataalignbytes);
  1437. if sec.Datapos<>FWriter.ObjSize then
  1438. internalerror(200604031);
  1439. FWriter.writearray(sec.data);
  1440. end;
  1441. end;
  1442. end;
  1443. {****************************************************************************
  1444. TExeVTable
  1445. ****************************************************************************}
  1446. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1447. begin
  1448. ExeSymbol:=AExeSymbol;
  1449. if ExeSymbol.State=symstate_undefined then
  1450. internalerror(200604012);
  1451. ChildList:=TFPObjectList.Create(false);
  1452. end;
  1453. destructor TExeVTable.Destroy;
  1454. begin
  1455. ChildList.Free;
  1456. if assigned(EntryArray) then
  1457. Freemem(EntryArray);
  1458. end;
  1459. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1460. var
  1461. OldEntryCnt : longint;
  1462. begin
  1463. if VTableIdx>=EntryCnt then
  1464. begin
  1465. OldEntryCnt:=EntryCnt;
  1466. EntryCnt:=VTableIdx+1;
  1467. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1468. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1469. end;
  1470. end;
  1471. procedure TExeVTable.AddChild(vt:TExeVTable);
  1472. begin
  1473. ChildList.Add(vt);
  1474. end;
  1475. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1476. var
  1477. i : longint;
  1478. objreloc : TObjRelocation;
  1479. vtblentryoffset : aword;
  1480. begin
  1481. CheckIdx(VTableIdx);
  1482. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
  1483. { Find and disable relocation }
  1484. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1485. begin
  1486. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1487. if objreloc.dataoffset=vtblentryoffset then
  1488. begin
  1489. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1490. EntryArray[VTableIdx].OrgRelocType:=objreloc.ftype;
  1491. EntryArray[VTableIdx].OrgRelocFlags:=objreloc.flags;
  1492. objreloc.typ:=RELOC_ZERO;
  1493. objreloc.flags:=objreloc.flags or rf_nosymbol;
  1494. break;
  1495. end;
  1496. end;
  1497. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1498. internalerror(200604011);
  1499. end;
  1500. procedure TExeVTable.SetVTableSize(ASize:longint);
  1501. begin
  1502. if EntryCnt<>0 then
  1503. internalerror(200603313);
  1504. EntryCnt:=ASize div sizeof(pint);
  1505. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1506. end;
  1507. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1508. begin
  1509. result:=nil;
  1510. CheckIdx(VTableIdx);
  1511. if EntryArray[VTableIdx].Used then
  1512. exit;
  1513. { Restore relocation if available }
  1514. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1515. begin
  1516. EntryArray[VTableIdx].ObjRelocation.ftype:=EntryArray[VTableIdx].OrgRelocType;
  1517. EntryArray[VTableIdx].ObjRelocation.flags:=EntryArray[VTableIdx].OrgRelocFlags;
  1518. result:=EntryArray[VTableIdx].ObjRelocation;
  1519. end;
  1520. EntryArray[VTableIdx].Used:=true;
  1521. end;
  1522. {****************************************************************************
  1523. TExeSection
  1524. ****************************************************************************}
  1525. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1526. begin
  1527. inherited create(AList,AName);
  1528. Size:=0;
  1529. MemPos:=0;
  1530. DataPos:=0;
  1531. FSecSymIdx:=0;
  1532. FObjSectionList:=TFPObjectList.Create(false);
  1533. end;
  1534. destructor TExeSection.destroy;
  1535. begin
  1536. ObjSectionList.Free;
  1537. inherited destroy;
  1538. end;
  1539. procedure TExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  1540. begin
  1541. ObjSectionList.Add(objsec);
  1542. { relate ObjSection to ExeSection, and mark it Used by default }
  1543. objsec.ExeSection:=self;
  1544. objsec.Used:=true;
  1545. if ignoreprops then
  1546. exit;
  1547. if (SecOptions<>[]) then
  1548. begin
  1549. { Only if the section contains (un)initialized data the
  1550. data flag must match. }
  1551. if ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1552. Comment(V_Error,'Incompatible section options');
  1553. end
  1554. else
  1555. begin
  1556. { inherit section options }
  1557. SecOptions:=SecOptions+objsec.SecOptions;
  1558. end;
  1559. SecAlign:=max(objsec.SecAlign,SecAlign);
  1560. end;
  1561. function TExeSection.MemPosStr(AImageBase: qword): string;
  1562. begin
  1563. result:='0x'+HexStr(mempos+AImageBase,sizeof(pint)*2);
  1564. end;
  1565. {****************************************************************************
  1566. TStaticLibrary
  1567. ****************************************************************************}
  1568. constructor TStaticLibrary.create(const AName:TCmdStr;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1569. begin
  1570. FName:=AName;
  1571. FPayload:=AReader;
  1572. FObjInputClass:=AObjInputClass;
  1573. FKind:=lkArchive;
  1574. end;
  1575. constructor TStaticLibrary.create_object(AObjData:TObjData);
  1576. begin
  1577. FPayload:=AObjData;
  1578. FKind:=lkObject;
  1579. end;
  1580. constructor TStaticLibrary.create_group;
  1581. begin
  1582. FPayload:=TFPObjectList.Create(true);
  1583. FKind:=lkGroup;
  1584. end;
  1585. destructor TStaticLibrary.destroy;
  1586. begin
  1587. FPayload.Free;
  1588. inherited destroy;
  1589. end;
  1590. function TStaticLibrary.GetArReader: TObjectReader;
  1591. begin
  1592. if (FKind<>lkArchive) then
  1593. InternalError(2012071501);
  1594. result:=TObjectReader(FPayload);
  1595. end;
  1596. function TStaticLibrary.GetGroupMembers: TFPObjectList;
  1597. begin
  1598. if (FKind<>lkGroup) then
  1599. InternalError(2012071502);
  1600. result:=TFPObjectList(FPayload);
  1601. end;
  1602. function TStaticLibrary.GetObjData: TObjData;
  1603. begin
  1604. if (FKind<>lkObject) then
  1605. InternalError(2012071503);
  1606. result:=TObjData(FPayload);
  1607. end;
  1608. {****************************************************************************
  1609. TImportLibrary
  1610. ****************************************************************************}
  1611. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1612. begin
  1613. inherited create(AList,AName);
  1614. FImportSymbolList:=TFPHashObjectList.Create(true);
  1615. end;
  1616. destructor TImportLibrary.destroy;
  1617. begin
  1618. ImportSymbolList.Free;
  1619. inherited destroy;
  1620. end;
  1621. {****************************************************************************
  1622. TImportSymbol
  1623. ****************************************************************************}
  1624. constructor TImportSymbol.create(AList:TFPHashObjectList;
  1625. const AName,AMangledName:string;AOrdNr:longint;AIsVar:boolean);
  1626. begin
  1627. inherited Create(AList, AName);
  1628. FOrdNr:=AOrdNr;
  1629. FIsVar:=AIsVar;
  1630. FMangledName:=AMangledName;
  1631. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1632. { This allows to import VC++ mangled names from DLLs. }
  1633. if target_info.system in systems_all_windows then
  1634. begin
  1635. Replace(FMangledName,'?','__q$$');
  1636. {$ifdef arm}
  1637. { @ symbol is not allowed in ARM assembler only }
  1638. Replace(FMangledName,'@','__a$$');
  1639. {$endif arm}
  1640. end;
  1641. end;
  1642. {****************************************************************************
  1643. TExeOutput
  1644. ****************************************************************************}
  1645. constructor TExeOutput.create;
  1646. begin
  1647. { init writer }
  1648. FWriter:=TObjectwriter.create;
  1649. FExeWriteMode:=ewm_exefull;
  1650. { object files }
  1651. FObjDataList:=TFPObjectList.Create(true);
  1652. { symbols }
  1653. FExeSymbolList:=TFPHashObjectList.Create(true);
  1654. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1655. FExternalObjSymbols:=TFPObjectList.Create(false);
  1656. FCommonObjSymbols:=TFPObjectList.Create(false);
  1657. FProvidedObjSymbols:=TFPObjectList.Create(false);
  1658. FIndirectObjSymbols:=TFPObjectList.Create(false);
  1659. FExeVTableList:=TFPObjectList.Create(false);
  1660. ComdatGroups:=TFPHashList.Create;
  1661. { sections }
  1662. FExeSectionList:=TFPHashObjectList.Create(true);
  1663. FImageBase:=0;
  1664. {$ifdef cpu16bitaddr}
  1665. SectionMemAlign:=$10;
  1666. SectionDataAlign:=$10;
  1667. {$else cpu16bitaddr}
  1668. SectionMemAlign:=$1000;
  1669. SectionDataAlign:=$200;
  1670. {$endif cpu16bitaddr}
  1671. FixedSectionAlign:=True;
  1672. FCExeSection:=TExeSection;
  1673. FCObjData:=TObjData;
  1674. FCObjSymbol:=TObjSymbol;
  1675. end;
  1676. destructor TExeOutput.destroy;
  1677. begin
  1678. FExeSymbolList.free;
  1679. UnresolvedExeSymbols.free;
  1680. ExternalObjSymbols.free;
  1681. FProvidedObjSymbols.free;
  1682. FIndirectObjSymbols.free;
  1683. CommonObjSymbols.free;
  1684. ExeVTableList.free;
  1685. FExeSectionList.free;
  1686. ComdatGroups.free;
  1687. ObjDatalist.free;
  1688. FWriter.free;
  1689. inherited destroy;
  1690. end;
  1691. function TExeOutput.MemAlign(exesec:TExeSection):longword;
  1692. begin
  1693. if FixedSectionAlign then
  1694. result:=SectionMemAlign
  1695. else
  1696. result:=exesec.SecAlign;
  1697. end;
  1698. function TExeOutput.DataAlign(exesec:TExeSection):longword;
  1699. begin
  1700. if FixedSectionAlign then
  1701. result:=SectionDataAlign
  1702. else
  1703. result:=exesec.SecAlign;
  1704. end;
  1705. function TExeOutput.WriteExeFile(const fn:string):boolean;
  1706. begin
  1707. result:=false;
  1708. if FWriter.createfile(fn) then
  1709. begin
  1710. { Only write the .o if there are no errors }
  1711. if errorcount=0 then
  1712. result:=writedata
  1713. else
  1714. result:=true;
  1715. { close the writer }
  1716. FWriter.closefile;
  1717. end
  1718. else
  1719. Comment(V_Fatal,'Can''t create executable '+fn);
  1720. end;
  1721. procedure TExeOutput.ParseScript (linkscript:TCmdStrList);
  1722. begin
  1723. end;
  1724. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1725. begin
  1726. result:=TExeSection(ExeSectionList.Find(aname));
  1727. end;
  1728. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1729. begin
  1730. if ObjData.classtype<>FCObjData then
  1731. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1732. ObjDataList.Add(ObjData);
  1733. ExecStack:=ExecStack or ObjData.ExecStack;
  1734. end;
  1735. procedure TExeOutput.Load_Start;
  1736. begin
  1737. ObjDataList.Clear;
  1738. { Globals defined in the linker script }
  1739. if not assigned(internalObjData) then
  1740. internalObjData:=CObjData.create('*Internal*');
  1741. AddObjData(internalObjData);
  1742. { Common Data section }
  1743. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1744. end;
  1745. procedure TExeOutput.Load_EntryName(const aname:string);
  1746. begin
  1747. FEntryName:=aname;
  1748. end;
  1749. procedure TExeOutput.Load_IsSharedLibrary;
  1750. begin
  1751. IsSharedLibrary:=true;
  1752. end;
  1753. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1754. var
  1755. code : integer;
  1756. objsec : TObjSection;
  1757. objsym : TObjSymbol;
  1758. exesym : TExeSymbol;
  1759. begin
  1760. val(avalue,FImageBase,code);
  1761. if code<>0 then
  1762. Comment(V_Error,'Invalid number '+avalue);
  1763. { Create __image_base__ symbol, create the symbol
  1764. in a section with adress 0 and at offset 0 }
  1765. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1766. internalObjData.setsection(objsec);
  1767. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_DATA);
  1768. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1769. exesym.ObjSymbol:=objsym;
  1770. end;
  1771. procedure TExeOutput.Load_Symbol(const aname:string);
  1772. begin
  1773. internalObjData.createsection('*'+aname,0,[]);
  1774. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_DATA);
  1775. end;
  1776. procedure TExeOutput.Load_ProvideSymbol(const aname:string);
  1777. begin
  1778. if assigned(ExeSymbolList.Find(aname)) then
  1779. exit;
  1780. internalObjData.createsection('*'+aname,0,[]);
  1781. // Use AB_COMMON to avoid muliple defined complaints
  1782. internalObjData.SymbolDefine(aname,AB_COMMON,AT_DATA);
  1783. end;
  1784. procedure TExeOutput.Load_DynamicObject(ObjData:TObjData;asneeded:boolean);
  1785. begin
  1786. end;
  1787. procedure TExeOutput.Order_Start;
  1788. begin
  1789. end;
  1790. procedure TExeOutput.Order_End;
  1791. begin
  1792. internalObjData.afterwrite;
  1793. end;
  1794. procedure TExeOutput.Order_ExeSection(const aname:string);
  1795. var
  1796. sec : TExeSection;
  1797. begin
  1798. sec:=FindExeSection(aname);
  1799. if not assigned(sec) then
  1800. sec:=CExeSection.create(ExeSectionList,aname);
  1801. { Clear ExeSection contents }
  1802. FCurrExeSec:=sec;
  1803. end;
  1804. procedure TExeOutput.Order_EndExeSection;
  1805. begin
  1806. if not assigned(CurrExeSec) then
  1807. internalerror(200602184);
  1808. FCurrExeSec:=nil;
  1809. end;
  1810. procedure TExeOutput.Order_ObjSection(const aname:string);
  1811. var
  1812. i,j : longint;
  1813. ObjData : TObjData;
  1814. objsec : TObjSection;
  1815. TmpObjSectionList : TFPObjectList;
  1816. begin
  1817. if not assigned(CurrExeSec) then
  1818. internalerror(200602181);
  1819. TmpObjSectionList:=TFPObjectList.Create(false);
  1820. for i:=0 to ObjDataList.Count-1 do
  1821. begin
  1822. ObjData:=TObjData(ObjDataList[i]);
  1823. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1824. begin
  1825. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1826. if (not objsec.Used) and
  1827. MatchPattern(aname,objsec.name) then
  1828. TmpObjSectionList.Add(objsec);
  1829. end;
  1830. end;
  1831. { Order list if needed }
  1832. Order_ObjSectionList(TmpObjSectionList,aname);
  1833. { Add the (ordered) list to the current ExeSection }
  1834. for i:=0 to TmpObjSectionList.Count-1 do
  1835. begin
  1836. objsec:=TObjSection(TmpObjSectionList[i]);
  1837. CurrExeSec.AddObjSection(objsec);
  1838. end;
  1839. TmpObjSectionList.Free;
  1840. end;
  1841. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);
  1842. begin
  1843. end;
  1844. procedure TExeOutput.Order_Symbol(const aname:string);
  1845. var
  1846. objsym: TObjSymbol;
  1847. begin
  1848. objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
  1849. if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
  1850. internalerror(200603041);
  1851. CurrExeSec.AddObjSection(objsym.ObjSection,True);
  1852. end;
  1853. procedure TExeOutput.Order_ProvideSymbol(const aname:string);
  1854. var
  1855. objsym : TObjSymbol;
  1856. exesym : TExeSymbol;
  1857. begin
  1858. objsym:=TObjSymbol(internalObjData.ObjSymbolList.Find(aname));
  1859. if (objsym=nil) or (objsym.ObjSection.ObjData<>internalObjData) then
  1860. internalerror(200603041);
  1861. exesym:=TExeSymbol(ExeSymbolList.Find(aname));
  1862. if not assigned(exesym) then
  1863. internalerror(201206301);
  1864. { Only include this section if it actually resolves
  1865. the symbol }
  1866. if exesym.objsymbol=objsym then
  1867. CurrExeSec.AddObjSection(objsym.ObjSection,True);
  1868. end;
  1869. procedure TExeOutput.Order_Align(const avalue:string);
  1870. var
  1871. code : integer;
  1872. alignval : shortint;
  1873. objsec : TObjSection;
  1874. begin
  1875. val(avalue,alignval,code);
  1876. if code<>0 then
  1877. Comment(V_Error,'Invalid number '+avalue);
  1878. if alignval<=0 then
  1879. exit;
  1880. { Create an empty section with the required aligning }
  1881. inc(Fzeronr);
  1882. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1883. CurrExeSec.AddObjSection(objsec);
  1884. end;
  1885. procedure TExeOutput.Order_Zeros(const avalue:string);
  1886. var
  1887. zeros : array[0..1023] of byte;
  1888. code : integer;
  1889. len : longint;
  1890. objsec : TObjSection;
  1891. begin
  1892. val(avalue,len,code);
  1893. if code<>0 then
  1894. Comment(V_Error,'Invalid number '+avalue);
  1895. if len<=0 then
  1896. exit;
  1897. if len>sizeof(zeros) then
  1898. internalerror(200602254);
  1899. fillchar(zeros,len,0);
  1900. inc(Fzeronr);
  1901. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1902. internalObjData.writebytes(zeros,len);
  1903. CurrExeSec.AddObjSection(objsec);
  1904. end;
  1905. procedure TExeOutput.Order_Values(bytesize : aword; const avalue:string);
  1906. const
  1907. MAXVAL = 128;
  1908. var
  1909. bytevalues : array[0..MAXVAL-1] of byte;
  1910. twobytevalues : array[0..MAXVAL-1] of word;
  1911. fourbytevalues : array[0..MAXVAL-1] of dword;
  1912. eightbytevalues : array[0..MAXVAL-1] of qword;
  1913. allvals, oneval : string;
  1914. len, commapos : longint;
  1915. indexpos, code : integer;
  1916. anumval : qword;
  1917. signedval : int64;
  1918. objsec : TObjSection;
  1919. begin
  1920. indexpos:=0;
  1921. allvals:=avalue;
  1922. { avoid warnings }
  1923. bytevalues[0]:=0;
  1924. twobytevalues[0]:=0;
  1925. fourbytevalues[0]:=0;
  1926. eightbytevalues[0]:=0;
  1927. repeat
  1928. commapos:=pos(',',allvals);
  1929. if commapos>0 then
  1930. begin
  1931. oneval:=trim(copy(allvals,1,commapos-1));
  1932. allvals:=copy(allvals,commapos+1,length(allvals));
  1933. end
  1934. else
  1935. begin
  1936. oneval:=trim(allvals);
  1937. allvals:='';
  1938. end;
  1939. if oneval<>'' then
  1940. begin
  1941. if oneval[1]='-' then
  1942. begin
  1943. val(oneval,signedval,code);
  1944. anumval:=qword(signedval);
  1945. end
  1946. else
  1947. val(oneval,anumval,code);
  1948. if code<>0 then
  1949. Comment(V_Error,'Invalid number '+avalue)
  1950. else
  1951. begin
  1952. if (indexpos<MAXVAL) then
  1953. begin
  1954. if source_info.endian<>target_info.endian then
  1955. swapendian(anumval);
  1956. { No range checking here }
  1957. if bytesize=1 then
  1958. bytevalues[indexpos]:=byte(anumval)
  1959. else if bytesize=2 then
  1960. twobytevalues[indexpos]:=word(anumval)
  1961. else if bytesize=4 then
  1962. fourbytevalues[indexpos]:=dword(anumval)
  1963. else if bytesize=8 then
  1964. eightbytevalues[indexpos]:=anumval;
  1965. inc(indexpos);
  1966. end
  1967. else
  1968. Comment(V_Error,'Buffer overrun in Order_values');
  1969. end;
  1970. end;
  1971. until allvals='';
  1972. if indexpos=0 then
  1973. begin
  1974. Comment(V_Error,'Invalid number '+avalue);
  1975. exit;
  1976. end;
  1977. if indexpos=MAXVAL then
  1978. begin
  1979. Comment(V_Error,'Too many values '+avalue);
  1980. internalerror(200602254);
  1981. end;
  1982. len:=bytesize*indexpos;
  1983. inc(Fvaluesnr);
  1984. objsec:=internalObjData.createsection('*values'+tostr(Fvaluesnr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1985. if bytesize=1 then
  1986. internalObjData.writebytes(bytevalues,len)
  1987. else if bytesize=2 then
  1988. internalObjData.writebytes(twobytevalues,len)
  1989. else if bytesize=4 then
  1990. internalObjData.writebytes(fourbytevalues,len)
  1991. else if bytesize=8 then
  1992. internalObjData.writebytes(eightbytevalues,len);
  1993. CurrExeSec.AddObjSection(objsec);
  1994. end;
  1995. procedure TExeOutput.MemPos_Start;
  1996. begin
  1997. CurrMemPos:=0;
  1998. RemoveDisabledSections;
  1999. end;
  2000. procedure TExeOutput.MemPos_Header;
  2001. begin
  2002. end;
  2003. procedure TExeOutput.MemPos_ExeSection(exesec:TExeSection);
  2004. var
  2005. i : longint;
  2006. objsec : TObjSection;
  2007. begin
  2008. { Alignment of ExeSection }
  2009. CurrMemPos:=align_qword(CurrMemPos,MemAlign(exesec));
  2010. exesec.MemPos:=CurrMemPos;
  2011. { set position of object ObjSections }
  2012. for i:=0 to exesec.ObjSectionList.Count-1 do
  2013. begin
  2014. objsec:=TObjSection(exesec.ObjSectionList[i]);
  2015. CurrMemPos:=objsec.setmempos(CurrMemPos);
  2016. end;
  2017. { calculate size of the section }
  2018. exesec.Size:=CurrMemPos-exesec.MemPos;
  2019. end;
  2020. procedure TExeOutput.MemPos_ExeSection(const aname:string);
  2021. begin
  2022. { Section can be removed }
  2023. FCurrExeSec:=FindExeSection(aname);
  2024. if not assigned(CurrExeSec) then
  2025. exit;
  2026. MemPos_ExeSection(CurrExeSec);
  2027. end;
  2028. procedure TExeOutput.MemPos_EndExeSection;
  2029. begin
  2030. if not assigned(CurrExeSec) then
  2031. exit;
  2032. FCurrExeSec:=nil;
  2033. end;
  2034. procedure TExeOutput.DataPos_Start;
  2035. begin
  2036. end;
  2037. procedure TExeOutput.DataPos_Header;
  2038. begin
  2039. end;
  2040. procedure TExeOutput.DataPos_ExeSection(exesec:TExeSection);
  2041. begin
  2042. { don't write normal section if writing only debug info }
  2043. if (ExeWriteMode=ewm_dbgonly) and
  2044. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  2045. exit;
  2046. if (oso_Data in exesec.SecOptions) then
  2047. begin
  2048. CurrDataPos:=align_aword(CurrDataPos,DataAlign(exesec));
  2049. exesec.DataPos:=CurrDataPos;
  2050. CurrDataPos:=CurrDataPos+exesec.Size;
  2051. end;
  2052. end;
  2053. procedure TExeOutput.DataPos_ExeSection(const aname:string);
  2054. begin
  2055. { Section can be removed }
  2056. FCurrExeSec:=FindExeSection(aname);
  2057. if not assigned(CurrExeSec) then
  2058. exit;
  2059. DataPos_ExeSection(CurrExeSec);
  2060. end;
  2061. procedure TExeOutput.DataPos_EndExeSection;
  2062. begin
  2063. if not assigned(CurrExeSec) then
  2064. exit;
  2065. FCurrExeSec:=nil;
  2066. end;
  2067. procedure TExeOutput.DataPos_Symbols;
  2068. begin
  2069. end;
  2070. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  2071. var
  2072. hs : string;
  2073. code : integer;
  2074. i,k,
  2075. vtableidx : longint;
  2076. vtableexesym,
  2077. childexesym,
  2078. parentexesym : TExeSymbol;
  2079. objsym : TObjSymbol;
  2080. begin
  2081. { Build inheritance tree from VTINHERIT }
  2082. for i:=0 to VTInheritList.Count-1 do
  2083. begin
  2084. objsym:=TObjSymbol(VTInheritList[i]);
  2085. hs:=objsym.name;
  2086. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  2087. Delete(hs,1,Pos('_',hs));
  2088. k:=Pos('$$',hs);
  2089. if k=0 then
  2090. internalerror(200603311);
  2091. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2092. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  2093. if not assigned(childexesym) or
  2094. not assigned(parentexesym)then
  2095. internalerror(200603312);
  2096. if not assigned(childexesym.vtable) then
  2097. begin
  2098. childexesym.vtable:=TExeVTable.Create(childexesym);
  2099. ExeVTableList.Add(childexesym.vtable);
  2100. end;
  2101. if not assigned(parentexesym.vtable) then
  2102. begin
  2103. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  2104. ExeVTableList.Add(parentexesym.vtable);
  2105. end;
  2106. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  2107. if parentexesym<>childexesym then
  2108. parentexesym.vtable.AddChild(childexesym.vtable);
  2109. end;
  2110. { Find VTable entries from VTENTRY }
  2111. for i:=0 to VTEntryList.Count-1 do
  2112. begin
  2113. objsym:=TObjSymbol(VTEntryList[i]);
  2114. hs:=objsym.name;
  2115. { VTENTRY_<VTableName>$$<Index> }
  2116. Delete(hs,1,Pos('_',hs));
  2117. k:=Pos('$$',hs);
  2118. if k=0 then
  2119. internalerror(200603319);
  2120. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2121. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2122. if (code<>0) then
  2123. internalerror(200603318);
  2124. if not assigned(vtableexesym) then
  2125. internalerror(2006033110);
  2126. vtableexesym.vtable.AddEntry(vtableidx);
  2127. end;
  2128. end;
  2129. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  2130. var
  2131. i : longint;
  2132. exesym : TExeSymbol;
  2133. begin
  2134. { Generate a list of Unresolved External symbols }
  2135. for i:=0 to UnresolvedExeSymbols.count-1 do
  2136. begin
  2137. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2138. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  2139. UnresolvedExeSymbols[i]:=nil;
  2140. end;
  2141. UnresolvedExeSymbols.Pack;
  2142. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  2143. end;
  2144. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPObjectList);
  2145. var
  2146. ObjData : TObjData;
  2147. exesym : TExeSymbol;
  2148. objsym,
  2149. commonsym : TObjSymbol;
  2150. firstarchive,
  2151. firstcommon : boolean;
  2152. i : longint;
  2153. VTEntryList,
  2154. VTInheritList : TFPObjectList;
  2155. procedure LoadObjDataSymbols(ObjData:TObjData);
  2156. var
  2157. j : longint;
  2158. hs : string;
  2159. exesym : TExeSymbol;
  2160. objsym : TObjSymbol;
  2161. grp : TObjSectionGroup;
  2162. begin
  2163. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  2164. begin
  2165. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  2166. { From the local symbols we are only interessed in the
  2167. VTENTRY and VTINHERIT symbols }
  2168. if objsym.bind=AB_LOCAL then
  2169. begin
  2170. if cs_link_opt_vtable in current_settings.globalswitches then
  2171. begin
  2172. hs:=objsym.name;
  2173. if (hs[1]='V') then
  2174. begin
  2175. if Copy(hs,1,5)='VTREF' then
  2176. begin
  2177. if not assigned(objsym.ObjSection.VTRefList) then
  2178. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  2179. objsym.ObjSection.VTRefList.Add(objsym);
  2180. end
  2181. else if Copy(hs,1,7)='VTENTRY' then
  2182. VTEntryList.Add(objsym)
  2183. else if Copy(hs,1,9)='VTINHERIT' then
  2184. VTInheritList.Add(objsym);
  2185. end;
  2186. end;
  2187. continue;
  2188. end;
  2189. { If this symbol comes from COMDAT group, see if a group with
  2190. matching signature is already included. }
  2191. if assigned(objsym.objsection) and
  2192. assigned(objsym.objsection.group) then
  2193. begin
  2194. grp:=objsym.objsection.group;
  2195. if grp.IsComdat then
  2196. begin
  2197. if ComdatGroups.Find(grp.name)=nil then
  2198. ComdatGroups.Add(grp.name,grp)
  2199. else
  2200. begin
  2201. { Undefine the symbol, causing relocations to it from same
  2202. objdata to be redirected to the symbol in the actually
  2203. linked group. }
  2204. if objsym.bind=AB_GLOBAL then
  2205. objsym.bind:=AB_EXTERNAL;
  2206. { AB_WEAK_EXTERNAL remains unchanged }
  2207. objsym.objsection:=nil;
  2208. end;
  2209. end;
  2210. end;
  2211. { Search for existing exesymbol }
  2212. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  2213. if not assigned(exesym) then
  2214. begin
  2215. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  2216. exesym.ObjSymbol:=objsym;
  2217. end;
  2218. objsym.ExeSymbol:=exesym;
  2219. case objsym.bind of
  2220. AB_GLOBAL,
  2221. AB_PRIVATE_EXTERN:
  2222. begin
  2223. if exesym.State<>symstate_defined then
  2224. begin
  2225. exesym.ObjSymbol:=objsym;
  2226. exesym.State:=symstate_defined;
  2227. end
  2228. else
  2229. if (oso_comdat in exesym.ObjSymbol.objsection.SecOptions) and
  2230. (oso_comdat in objsym.objsection.SecOptions) then
  2231. begin
  2232. if exesym.ObjSymbol.objsection.ComdatSelection=objsym.objsection.ComdatSelection then
  2233. begin
  2234. case objsym.objsection.ComdatSelection of
  2235. oscs_none:
  2236. Message1(link_e_duplicate_symbol,objsym.name);
  2237. oscs_any:
  2238. Message1(link_d_comdat_discard_any,objsym.name);
  2239. oscs_same_size:
  2240. if exesym.ObjSymbol.size<>objsym.size then
  2241. Message1(link_e_comdat_size_differs,objsym.name)
  2242. else
  2243. Message1(link_d_comdat_discard_size,objsym.name);
  2244. oscs_exact_match:
  2245. if (exesym.ObjSymbol.size<>objsym.size) and not exesym.ObjSymbol.objsection.Data.equal(objsym.objsection.Data) then
  2246. Message1(link_e_comdat_content_differs,objsym.name)
  2247. else
  2248. Message1(link_d_comdat_discard_content,objsym.name);
  2249. oscs_associative:
  2250. { this is handled in a different way }
  2251. Message1(link_e_duplicate_symbol,objsym.name);
  2252. oscs_largest:
  2253. if objsym.size>exesym.ObjSymbol.size then
  2254. begin
  2255. Message1(link_d_comdat_replace_size,objsym.name);
  2256. exesym.ObjSymbol.exesymbol:=nil;
  2257. exesym.ObjSymbol:=objsym;
  2258. end;
  2259. end;
  2260. end
  2261. else
  2262. Message1(link_e_comdat_selection_differs,objsym.name);
  2263. end
  2264. else
  2265. { specific error if ComDat flags are different? }
  2266. Message1(link_e_duplicate_symbol,objsym.name);
  2267. { hidden symbols must become local symbols in the executable }
  2268. if objsym.bind=AB_PRIVATE_EXTERN then
  2269. objsym.bind:=AB_LOCAL;
  2270. end;
  2271. AB_EXTERNAL :
  2272. begin
  2273. ExternalObjSymbols.add(objsym);
  2274. { Register unresolved symbols only the first time they
  2275. are registered }
  2276. if exesym.ObjSymbol=objsym then
  2277. UnresolvedExeSymbols.Add(exesym)
  2278. { Normal reference removes any existing "weakness" }
  2279. else if exesym.state=symstate_undefweak then
  2280. begin
  2281. exesym.state:=symstate_undefined;
  2282. exesym.ObjSymbol:=objsym;
  2283. end;
  2284. end;
  2285. AB_COMMON :
  2286. begin
  2287. { A COMMON definition overrides weak one.
  2288. Also select the symbol with largest size. }
  2289. if (exesym.State in [symstate_undefined,symstate_undefweak,symstate_defweak]) or
  2290. ((exesym.State=symstate_common) and (objsym.size>exesym.ObjSymbol.size)) then
  2291. begin
  2292. exesym.ObjSymbol:=objsym;
  2293. exesym.State:=symstate_common;
  2294. end;
  2295. if assigned(objsym.objsection) and
  2296. (objsym.objsection.objdata=internalObjData) then
  2297. FProvidedObjSymbols.add(objsym)
  2298. else
  2299. CommonObjSymbols.add(objsym);
  2300. end;
  2301. AB_WEAK_EXTERNAL :
  2302. begin
  2303. if objsym.objsection=nil then { a weak reference }
  2304. begin
  2305. ExternalObjSymbols.add(objsym);
  2306. if exesym.ObjSymbol=objsym then
  2307. begin
  2308. UnresolvedExeSymbols.Add(exesym);
  2309. exesym.state:=symstate_undefweak;
  2310. end;
  2311. end
  2312. else { a weak definition }
  2313. begin
  2314. if exesym.State in [symstate_undefined,symstate_undefweak] then
  2315. begin
  2316. exesym.ObjSymbol:=objsym;
  2317. exesym.state:=symstate_defweak;
  2318. end;
  2319. end;
  2320. end;
  2321. else
  2322. internalerror(2019050510);
  2323. end;
  2324. end;
  2325. end;
  2326. procedure LoadLibrary(lib:TStaticLibrary);
  2327. var
  2328. j,k,oldcount: longint;
  2329. members: TFPObjectList;
  2330. exesym: TExeSymbol;
  2331. objinput: TObjInput;
  2332. begin
  2333. case lib.Kind of
  2334. lkArchive:
  2335. begin
  2336. { Process list of Unresolved External symbols, we need
  2337. to use a while loop because the list can be extended when
  2338. we load members from the library. }
  2339. j:=0;
  2340. while (j<UnresolvedExeSymbols.count) do
  2341. begin
  2342. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  2343. { Check first if the symbol is still undefined }
  2344. if (exesym.State=symstate_undefined) then
  2345. begin
  2346. if lib.ArReader.OpenFile(exesym.name) then
  2347. begin
  2348. if assigned(exemap) then
  2349. begin
  2350. if firstarchive then
  2351. begin
  2352. exemap.Add('');
  2353. exemap.Add('Archive member included because of file (symbol)');
  2354. exemap.Add('');
  2355. firstarchive:=false;
  2356. end;
  2357. exemap.Add(lib.ArReader.FileName+' - '+
  2358. {exesym.ObjSymbol.ObjSection.FullName+}
  2359. '('+exesym.Name+')');
  2360. end;
  2361. objinput:=lib.ObjInputClass.Create;
  2362. objinput.ReadObjData(lib.ArReader,objdata);
  2363. objinput.free;
  2364. AddObjData(objdata);
  2365. LoadObjDataSymbols(objdata);
  2366. lib.ArReader.CloseFile;
  2367. end;
  2368. end;
  2369. inc(j);
  2370. end;
  2371. end;
  2372. lkGroup:
  2373. begin
  2374. { repeatedly process members of the group until no new
  2375. unresolved symbols appear }
  2376. members:=lib.GroupMembers;
  2377. repeat
  2378. oldcount:=UnresolvedExeSymbols.count;
  2379. for k:=0 to members.Count-1 do
  2380. LoadLibrary(TStaticLibrary(members[k]));
  2381. until UnresolvedExeSymbols.count=oldcount;
  2382. end;
  2383. lkObject:
  2384. { TODO: ownership of objdata }
  2385. //if lib.objdata.is_dynamic then
  2386. Load_DynamicObject(lib.objdata,lib.AsNeeded);
  2387. {else
  2388. begin
  2389. AddObjData(lib.objdata);
  2390. LoadObjDataSymbols(lib.objdata);
  2391. end;}
  2392. end;
  2393. end;
  2394. begin
  2395. VTEntryList:=TFPObjectList.Create(false);
  2396. VTInheritList:=TFPObjectList.Create(false);
  2397. {
  2398. The symbol resolving is done in 4 steps:
  2399. 1. Register symbols from objects
  2400. 2. Find symbols in static libraries
  2401. 3. Define symbols PROVIDEd by the link script
  2402. 4. Define still undefined common symbols
  2403. }
  2404. { Step 1, Register symbols from objects }
  2405. for i:=0 to ObjDataList.Count-1 do
  2406. begin
  2407. ObjData:=TObjData(ObjDataList[i]);
  2408. LoadObjDataSymbols(ObjData);
  2409. end;
  2410. PackUnresolvedExeSymbols('in objects');
  2411. { Step 2, Find unresolved symbols in the libraries }
  2412. firstarchive:=true;
  2413. for i:=0 to StaticLibraryList.Count-1 do
  2414. LoadLibrary(TStaticLibrary(StaticLibraryList[i]));
  2415. PackUnresolvedExeSymbols('after static libraries');
  2416. { Step 3, handle symbols provided in script }
  2417. for i:=0 to FProvidedObjSymbols.count-1 do
  2418. begin
  2419. objsym:=TObjSymbol(FProvidedObjSymbols[i]);
  2420. if objsym.exesymbol.State=symstate_defined then
  2421. continue;
  2422. objsym.exesymbol.objsymbol:=objsym;
  2423. objsym.bind:=AB_GLOBAL;
  2424. objsym.exesymbol.State:=symstate_defined;
  2425. end;
  2426. PackUnresolvedExeSymbols('after defining symbols provided by link script');
  2427. { Step 4, Match common symbols or add to the globals }
  2428. firstcommon:=true;
  2429. for i:=0 to CommonObjSymbols.count-1 do
  2430. begin
  2431. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2432. if objsym.exesymbol.State=symstate_defined then
  2433. begin
  2434. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  2435. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  2436. end
  2437. else
  2438. begin
  2439. { allocate new objsymbol in .bss of *COMMON* and assign
  2440. it to the exesymbol }
  2441. if firstcommon then
  2442. begin
  2443. if assigned(exemap) then
  2444. exemap.AddCommonSymbolsHeader;
  2445. firstcommon:=false;
  2446. end;
  2447. internalObjData.setsection(commonObjSection);
  2448. { TODO: support alignment of common symbols (ELF targets at least),
  2449. increase commonObjSection.SecAlign if necessary here. }
  2450. internalObjData.allocalign(used_align(size_2_align(objsym.size),0,commonObjSection.SecAlign));
  2451. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_DATA);
  2452. commonsym.size:=objsym.size;
  2453. internalObjData.alloc(objsym.size);
  2454. if assigned(exemap) then
  2455. exemap.AddCommonSymbol(objsym);
  2456. { Assign to the exesymbol }
  2457. objsym.exesymbol.objsymbol:=commonsym;
  2458. objsym.exesymbol.state:=symstate_defined;
  2459. end;
  2460. end;
  2461. PackUnresolvedExeSymbols('after defining COMMON symbols');
  2462. { Find entry symbol and print in map }
  2463. if (EntryName<>'') then
  2464. begin
  2465. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  2466. if assigned(exesym) then
  2467. begin
  2468. EntrySym:=exesym.ObjSymbol;
  2469. if assigned(exemap) then
  2470. begin
  2471. exemap.Add('');
  2472. exemap.Add('Entry symbol '+EntryName);
  2473. end;
  2474. end
  2475. else
  2476. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  2477. end;
  2478. { Generate VTable tree }
  2479. if cs_link_opt_vtable in current_settings.globalswitches then
  2480. BuildVTableTree(VTInheritList,VTEntryList);
  2481. VTInheritList.Free;
  2482. VTEntryList.Free;
  2483. end;
  2484. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  2485. var
  2486. debuglink : array[0..1023] of byte;
  2487. len : longint;
  2488. objsec : TObjSection;
  2489. exesec : TExeSection;
  2490. begin
  2491. { From the gdb manual chapter 15. GDB Files:
  2492. * A filename, with any leading directory components removed, followed by a zero byte,
  2493. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  2494. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  2495. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  2496. }
  2497. fillchar(debuglink,sizeof(debuglink),0);
  2498. len:=0;
  2499. move(dbgname[1],debuglink[len],length(dbgname));
  2500. inc(len,length(dbgname)+1);
  2501. len:=align(len,4);
  2502. if source_info.endian<>target_info.endian then
  2503. SwapEndian(dbgcrc);
  2504. move(dbgcrc,debuglink[len],sizeof(cardinal));
  2505. inc(len,4);
  2506. { Add section }
  2507. exesec:=FindExeSection(debuglinkname);
  2508. if not assigned(exesec) then
  2509. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  2510. exesec.SecOptions:=[oso_data,oso_keep];
  2511. exesec.SecAlign:=4;
  2512. objsec:=internalObjData.createsection(exesec.name,1,exesec.SecOptions);
  2513. internalObjData.writebytes(debuglink,len);
  2514. exesec.AddObjSection(objsec);
  2515. end;
  2516. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2517. begin
  2518. end;
  2519. procedure TExeOutput.MarkTargetSpecificSections(WorkList:TFPObjectList);
  2520. begin
  2521. end;
  2522. procedure TExeOutput.AfterUnusedSectionRemoval;
  2523. begin
  2524. end;
  2525. function ByAddress(item1,item2:pointer):longint;
  2526. var
  2527. sym1:TObjSymbol absolute item1;
  2528. sym2:TObjSymbol absolute item2;
  2529. begin
  2530. if sym1.address>sym2.address then
  2531. result:=1
  2532. else if sym1.address<sym2.address then
  2533. result:=-1
  2534. else
  2535. result:=0;
  2536. end;
  2537. procedure TExeOutput.PrintMemoryMap;
  2538. var
  2539. exesec : TExeSection;
  2540. objsec : TObjSection;
  2541. objsym : TObjSymbol;
  2542. i,j,k,m: longint;
  2543. list : TFPList;
  2544. flag : boolean;
  2545. begin
  2546. if not assigned(exemap) then
  2547. exit;
  2548. { create a list of symbols sorted by address }
  2549. list:=TFPList.Create;
  2550. for i:=0 to ExeSymbolList.Count-1 do
  2551. list.Add(TExeSymbol(ExeSymbolList[i]).ObjSymbol);
  2552. list.Sort(@ByAddress);
  2553. exemap.AddMemoryMapHeader(ImageBase);
  2554. k:=0;
  2555. for i:=0 to ExeSectionList.Count-1 do
  2556. begin
  2557. exesec:=TExeSection(ExeSectionList[i]);
  2558. exemap.AddMemoryMapExeSection(exesec);
  2559. for j:=0 to exesec.ObjSectionList.count-1 do
  2560. begin
  2561. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2562. exemap.AddMemoryMapObjectSection(objsec);
  2563. while (k<list.Count) and (TObjSymbol(list[k]).Address<objsec.MemPos) do
  2564. inc(k);
  2565. while (k<list.Count) do
  2566. begin
  2567. objsym:=TObjSymbol(list[k]);
  2568. if objsym.address>objsec.MemPos+objsec.Size then
  2569. break;
  2570. if objsym.objsection=objsec then
  2571. exemap.AddMemoryMapSymbol(objsym)
  2572. else
  2573. begin
  2574. { Got a symbol with address falling into current section, but
  2575. belonging to a different section. This may happen for zero-length
  2576. sections because symbol list is sorted by address but not by section.
  2577. Do some look-ahead in this case. }
  2578. m:=k+1;
  2579. flag:=false;
  2580. while (m<list.Count) and (TObjSymbol(list[m]).Address=objsym.address) do
  2581. begin
  2582. if TObjSymbol(list[m]).objsection=objsec then
  2583. begin
  2584. flag:=true;
  2585. list.Exchange(k,m);
  2586. exemap.AddMemoryMapSymbol(TObjSymbol(list[k]));
  2587. break;
  2588. end;
  2589. inc(m);
  2590. end;
  2591. if not flag then
  2592. break;
  2593. end;
  2594. inc(k);
  2595. end;
  2596. end;
  2597. end;
  2598. list.Free;
  2599. end;
  2600. procedure TExeOutput.FixupSymbols;
  2601. procedure UpdateSymbol(objsym:TObjSymbol);
  2602. begin
  2603. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  2604. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2605. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  2606. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2607. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2608. objsym.group:=objsym.ExeSymbol.ObjSymbol.group;
  2609. end;
  2610. var
  2611. i : longint;
  2612. objsym : TObjSymbol;
  2613. exesym : TExeSymbol;
  2614. begin
  2615. { Print list of Unresolved External symbols }
  2616. if not AllowUndefinedSymbols then
  2617. for i:=0 to UnresolvedExeSymbols.count-1 do
  2618. begin
  2619. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2620. if (exesym.State=symstate_undefined) then
  2621. Comment(V_Error,'Undefined symbol: '+exesym.name);
  2622. end;
  2623. {
  2624. Fixing up symbols is done in the following steps:
  2625. 1. Update common references
  2626. 2. Update external references
  2627. Symbols with objsection<>nil are removed from the lists,
  2628. remaining ones can be processed later by calling this method again.
  2629. }
  2630. { Step 1, Update commons. Preserve the original symbol size and bind,
  2631. this is needed for correct relocation of DJCOFF files. }
  2632. for i:=0 to CommonObjSymbols.count-1 do
  2633. begin
  2634. objsym:=TObjSymbol(CommonObjSymbols[i]);
  2635. if objsym.bind<>AB_COMMON then
  2636. internalerror(200606241);
  2637. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  2638. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  2639. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  2640. end;
  2641. { Step 2, Update externals }
  2642. for i:=0 to ExternalObjSymbols.count-1 do
  2643. begin
  2644. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  2645. if not (objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) then
  2646. internalerror(200606242);
  2647. UpdateSymbol(objsym);
  2648. { Collect symbols that resolve to indirect functions,
  2649. they will need additional target-specific processing. }
  2650. if objsym.typ=AT_GNU_IFUNC then
  2651. IndirectObjSymbols.Add(objsym)
  2652. else if assigned(objsym.objsection) then
  2653. ExternalObjSymbols[i]:=nil;
  2654. end;
  2655. CommonObjSymbols.Clear;
  2656. ExternalObjSymbols.Pack;
  2657. end;
  2658. procedure TExeOutput.MergeStabs;
  2659. var
  2660. stabexesec,
  2661. stabstrexesec : TExeSection;
  2662. relocsec,
  2663. currstabsec,
  2664. currstabstrsec,
  2665. mergedstabsec,
  2666. mergedstabstrsec : TObjSection;
  2667. hstabreloc,
  2668. currstabreloc : TObjRelocation;
  2669. i,j : longint;
  2670. currstabrelocidx,
  2671. mergestabcnt,
  2672. stabcnt : longword;
  2673. skipstab : boolean;
  2674. skipfun : boolean;
  2675. hstab : TObjStabEntry;
  2676. stabrelocofs : longword;
  2677. buf : array[0..1023] of byte;
  2678. bufend,
  2679. bufsize : longint;
  2680. begin
  2681. stabexesec:=FindExeSection('.stab');
  2682. stabstrexesec:=FindExeSection('.stabstr');
  2683. if (stabexesec=nil) or
  2684. (stabstrexesec=nil) or
  2685. (stabexesec.ObjSectionlist.count=0) then
  2686. exit;
  2687. { Create new stabsection }
  2688. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2689. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2690. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2691. { write stab for hdrsym }
  2692. mergedstabsec.writeZeros(sizeof(TObjStabEntry));
  2693. mergestabcnt:=1;
  2694. { .stabstr starts with a #0 }
  2695. buf[0]:=0;
  2696. mergedstabstrsec.write(buf[0],1);
  2697. skipfun:=false;
  2698. { Copy stabs and corresponding Relocations }
  2699. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2700. begin
  2701. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2702. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2703. if assigned(currstabstrsec) then
  2704. begin
  2705. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2706. currstabsec.Data.seek(0);
  2707. currstabrelocidx:=0;
  2708. for j:=0 to stabcnt-1 do
  2709. begin
  2710. hstabreloc:=nil;
  2711. skipstab:=false;
  2712. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2713. MaybeSwapStab(hstab);
  2714. { Only include first hdrsym stab }
  2715. if hstab.ntype=0 then
  2716. skipstab:=true;
  2717. if skipfun then
  2718. begin
  2719. { Skip all stabs for function body until N_RBRAC }
  2720. skipfun:=hstab.ntype<>N_RBRAC;
  2721. skipstab:=true;
  2722. end;
  2723. if not skipstab then
  2724. begin
  2725. { Find corresponding Relocation }
  2726. currstabreloc:=nil;
  2727. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2728. begin
  2729. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2730. if assigned(currstabreloc) and
  2731. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2732. break;
  2733. inc(currstabrelocidx);
  2734. end;
  2735. if assigned(currstabreloc) and
  2736. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2737. begin
  2738. hstabReloc:=currstabReloc;
  2739. inc(currstabrelocidx);
  2740. end;
  2741. { Check if the stab is refering to a removed section }
  2742. if assigned(hstabreloc) then
  2743. begin
  2744. if assigned(hstabreloc.Symbol) then
  2745. relocsec:=hstabreloc.Symbol.ObjSection
  2746. else
  2747. relocsec:=hstabreloc.ObjSection;
  2748. if not assigned(relocsec) then
  2749. internalerror(200603302);
  2750. if not relocsec.Used then
  2751. begin
  2752. skipstab:=true;
  2753. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2754. begin
  2755. currstabstrsec.Data.seek(hstab.strpos);
  2756. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2757. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2758. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2759. skipfun:=true;
  2760. end;
  2761. end;
  2762. end;
  2763. end;
  2764. if not skipstab then
  2765. begin
  2766. { Copy string in stabstr }
  2767. if hstab.strpos<>0 then
  2768. begin
  2769. currstabstrsec.Data.seek(hstab.strpos);
  2770. hstab.strpos:=mergedstabstrsec.Size;
  2771. repeat
  2772. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2773. bufend:=indexbyte(buf,bufsize,0);
  2774. if bufend=-1 then
  2775. bufend:=bufsize
  2776. else
  2777. begin
  2778. { include the #0 }
  2779. inc(bufend);
  2780. end;
  2781. mergedstabstrsec.write(buf,bufend);
  2782. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2783. end;
  2784. { Copy and Update the relocation }
  2785. if assigned(hstabreloc) then
  2786. begin
  2787. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2788. { Remove from List without freeing the object }
  2789. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2790. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2791. end;
  2792. { Write updated stab }
  2793. MaybeSwapStab(hstab);
  2794. mergedstabsec.write(hstab,sizeof(hstab));
  2795. inc(mergestabcnt);
  2796. end;
  2797. end;
  2798. end;
  2799. { Unload stabs }
  2800. if assigned(currstabstrsec) then
  2801. begin
  2802. currstabstrsec.Used:=False;
  2803. currstabstrsec.ReleaseData;
  2804. end;
  2805. currstabsec.Used:=false;
  2806. currstabsec.ReleaseData;
  2807. end;
  2808. { Generate new HdrSym }
  2809. if mergedstabsec.Size>0 then
  2810. begin
  2811. hstab.strpos:=1;
  2812. hstab.ntype:=0;
  2813. hstab.nother:=0;
  2814. hstab.ndesc:=word(mergestabcnt-1);
  2815. hstab.nvalue:=mergedstabstrsec.Size;
  2816. MaybeSwapStab(hstab);
  2817. mergedstabsec.Data.seek(0);
  2818. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2819. end;
  2820. { Replace all sections with our combined stabsec }
  2821. stabexesec.ObjSectionList.Clear;
  2822. stabstrexesec.ObjSectionList.Clear;
  2823. stabexesec.AddObjSection(mergedstabsec);
  2824. stabstrexesec.AddObjSection(mergedstabstrsec);
  2825. end;
  2826. procedure TExeOutput.MarkEmptySections;
  2827. var
  2828. i, j : longint;
  2829. exesec : TExeSection;
  2830. doremove : boolean;
  2831. begin
  2832. for i:=0 to ExeSectionList.Count-1 do
  2833. begin
  2834. exesec:=TExeSection(ExeSectionList[i]);
  2835. doremove:=not(oso_keep in exesec.SecOptions) and
  2836. (
  2837. (exesec.ObjSectionlist.count=0) or
  2838. (
  2839. (cs_link_strip in current_settings.globalswitches) and
  2840. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2841. (oso_debug in exesec.SecOptions)
  2842. )
  2843. );
  2844. if not doremove then
  2845. begin
  2846. { Check if section has no actual data }
  2847. doremove:=true;
  2848. for j:=0 to exesec.ObjSectionList.Count-1 do
  2849. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2850. begin
  2851. doremove:=false;
  2852. break;
  2853. end;
  2854. end;
  2855. if doremove then
  2856. begin
  2857. Comment(V_Debug,'Disabling empty section '+exesec.name);
  2858. exesec.Disabled:=true;
  2859. end;
  2860. end;
  2861. end;
  2862. procedure TExeOutput.RemoveDisabledSections;
  2863. var
  2864. i: longint;
  2865. exesec: TExeSection;
  2866. begin
  2867. for i:=0 to ExeSectionList.Count-1 do
  2868. begin
  2869. exesec:=TExeSection(ExeSectionList[i]);
  2870. if exesec.Disabled then
  2871. ExeSectionList[i]:=nil;
  2872. end;
  2873. ExeSectionList.Pack;
  2874. end;
  2875. procedure TExeOutput.RemoveDebugInfo;
  2876. var
  2877. i : longint;
  2878. exesec : TExeSection;
  2879. begin
  2880. for i:=0 to ExeSectionList.Count-1 do
  2881. begin
  2882. exesec:=TExeSection(ExeSectionList[i]);
  2883. if (oso_debug in exesec.SecOptions) then
  2884. ExeSectionList[i]:=nil;
  2885. end;
  2886. ExeSectionList.Pack;
  2887. end;
  2888. procedure TExeOutput.RemoveUnreferencedSections;
  2889. var
  2890. ObjSectionWorkList : TFPObjectList;
  2891. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2892. begin
  2893. if not aobjsec.Used then
  2894. begin
  2895. aobjsec.Used:=true;
  2896. ObjSectionWorkList.Add(aobjsec);
  2897. end;
  2898. end;
  2899. procedure DoReloc(objreloc:TObjRelocation);
  2900. var
  2901. objsym : TObjSymbol;
  2902. refobjsec : TObjSection;
  2903. refgrp : TObjSectionGroup;
  2904. begin
  2905. { Disabled Relocation to 0 }
  2906. if (objreloc.flags and rf_nosymbol)<>0 then
  2907. exit;
  2908. refobjsec:=nil;
  2909. refgrp:=nil;
  2910. if assigned(objreloc.symbol) then
  2911. begin
  2912. objsym:=objreloc.symbol;
  2913. if objsym.bind<>AB_LOCAL then
  2914. begin
  2915. if not assigned(objsym.exesymbol) then
  2916. internalerror(200603063);
  2917. objsym.exesymbol.used:=true;
  2918. objsym:=objsym.exesymbol.objsymbol;
  2919. end;
  2920. if not assigned(objsym.objsection) then
  2921. exit
  2922. else
  2923. refobjsec:=objsym.objsection;
  2924. end
  2925. else if assigned(objreloc.objsection) then
  2926. refobjsec:=objreloc.objsection
  2927. else if assigned(objreloc.group) then
  2928. refgrp:=objreloc.group
  2929. else
  2930. internalerror(200603316);
  2931. if assigned(exemap) then
  2932. begin
  2933. objsym:=objreloc.symbol;
  2934. if assigned(objsym) and (objsym.typ<>AT_SECTION) then
  2935. exemap.Add(' References '+objsym.name+' in '
  2936. +refobjsec.fullname)
  2937. else if assigned(refobjsec) then
  2938. exemap.Add(' References '+refobjsec.fullname)
  2939. else if assigned(refgrp) then
  2940. exemap.Add(' References '+refgrp.Name)
  2941. else
  2942. internalerror(200603316);
  2943. end;
  2944. if assigned(refobjsec) then
  2945. AddToObjSectionWorkList(refobjsec);
  2946. end;
  2947. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2948. var
  2949. i : longint;
  2950. objreloc : TObjRelocation;
  2951. begin
  2952. objreloc:=vtable.VTableRef(VTableIdx);
  2953. if assigned(objreloc) then
  2954. begin
  2955. { Process the relocation now if the ObjSection is
  2956. already processed and marked as used. Otherwise we leave it
  2957. unprocessed. It'll then be resolved when the ObjSection is
  2958. changed to Used }
  2959. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2960. DoReloc(objreloc);
  2961. end;
  2962. { This recursive walking is done here instead of
  2963. in TExeVTable.VTableRef because we can now process
  2964. all needed relocations }
  2965. for i:=0 to vtable.ChildList.Count-1 do
  2966. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2967. end;
  2968. procedure ProcessWorkList;
  2969. var
  2970. hs : string;
  2971. i,k : longint;
  2972. objsec : TObjSection;
  2973. objsym : TObjSymbol;
  2974. code : integer;
  2975. vtableidx : longint;
  2976. vtableexesym : TExeSymbol;
  2977. begin
  2978. while ObjSectionWorkList.Count>0 do
  2979. begin
  2980. objsec:=TObjSection(ObjSectionWorkList.Last);
  2981. if assigned(exemap) then
  2982. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2983. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2984. { Process Relocations }
  2985. for i:=0 to objsec.ObjRelocations.count-1 do
  2986. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2987. { Process Virtual Entry calls }
  2988. if cs_link_opt_vtable in current_settings.globalswitches then
  2989. begin
  2990. for i:=0 to objsec.VTRefList.count-1 do
  2991. begin
  2992. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2993. hs:=objsym.name;
  2994. Delete(hs,1,Pos('_',hs));
  2995. k:=Pos('$$',hs);
  2996. if k=0 then
  2997. internalerror(200603314);
  2998. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2999. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  3000. if (code<>0) then
  3001. internalerror(200603317);
  3002. if not assigned(vtableexesym) then
  3003. internalerror(200603315);
  3004. if not assigned(vtableexesym.vtable) then
  3005. internalerror(200603316);
  3006. DoVTableRef(vtableexesym.vtable,vtableidx);
  3007. end;
  3008. end;
  3009. end;
  3010. end;
  3011. var
  3012. i,j : longint;
  3013. exesec : TExeSection;
  3014. objdata : TObjData;
  3015. objsec : TObjSection;
  3016. begin
  3017. ObjSectionWorkList:=TFPObjectList.Create(false);
  3018. if assigned(exemap) then
  3019. exemap.AddHeader('Removing unreferenced sections');
  3020. { Initialize by marking all sections unused and
  3021. adding the sections with oso_keep flags to the ObjSectionWorkList }
  3022. for i:=0 to ObjDataList.Count-1 do
  3023. begin
  3024. ObjData:=TObjData(ObjDataList[i]);
  3025. for j:=0 to ObjData.ObjSectionList.Count-1 do
  3026. begin
  3027. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  3028. objsec.Used:=false;
  3029. { TODO: remove debug section always keep}
  3030. if oso_debug in objsec.secoptions then
  3031. objsec.Used:=true;
  3032. if (oso_keep in objsec.secoptions) then
  3033. begin
  3034. AddToObjSectionWorkList(objsec);
  3035. if objsec.name='.fpc.n_links' then
  3036. objsec.Used:=false;
  3037. end;
  3038. end;
  3039. end;
  3040. if assigned(entrysym) then
  3041. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  3042. { Process all sections, add new sections to process based
  3043. on the symbol references }
  3044. ProcessWorkList;
  3045. { Handle stuff like .pdata, i.e. sections that are not referenced
  3046. but must be included if sections they reference are included.
  3047. Loop is necessary because .pdata can reference (via .xdata)
  3048. more text sections, VMTs of exception classes, etc. }
  3049. repeat
  3050. MarkTargetSpecificSections(ObjSectionWorkList);
  3051. if (ObjSectionWorkList.Count=0) then
  3052. break;
  3053. ProcessWorkList;
  3054. until False;
  3055. ObjSectionWorkList.Free;
  3056. ObjSectionWorkList:=nil;
  3057. { Remove unused objsections from ExeSectionList }
  3058. for i:=0 to ExeSectionList.Count-1 do
  3059. begin
  3060. exesec:=TExeSection(ExeSectionList[i]);
  3061. for j:=0 to exesec.ObjSectionlist.count-1 do
  3062. begin
  3063. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3064. if not objsec.used then
  3065. begin
  3066. if assigned(exemap) then
  3067. exemap.Add('Removing '+objsec.FullName);
  3068. exesec.ObjSectionlist[j]:=nil;
  3069. objsec.ReleaseData;
  3070. end;
  3071. end;
  3072. exesec.ObjSectionlist.Pack;
  3073. end;
  3074. end;
  3075. procedure TExeOutput.FixupRelocations;
  3076. var
  3077. i,j : longint;
  3078. exesec : TExeSection;
  3079. objsec : TObjSection;
  3080. begin
  3081. for i:=0 to ExeSectionList.Count-1 do
  3082. begin
  3083. exesec:=TExeSection(ExeSectionList[i]);
  3084. if not assigned(exesec) then
  3085. continue;
  3086. for j:=0 to exesec.ObjSectionlist.count-1 do
  3087. begin
  3088. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  3089. if not objsec.Used then
  3090. internalerror(200603301);
  3091. if (objsec.ObjRelocations.Count>0) and
  3092. not assigned(objsec.data) then
  3093. internalerror(200205183);
  3094. DoRelocationFixup(objsec);
  3095. {for size = 0 data is not valid PM }
  3096. if assigned(objsec.data) and (objsec.data.size<>objsec.size) then
  3097. internalerror(2010092801);
  3098. end;
  3099. end;
  3100. end;
  3101. procedure TExeOutput.RemoveUnusedExeSymbols;
  3102. var
  3103. i : longint;
  3104. sym : TExeSymbol;
  3105. begin
  3106. { Removing unused symbols }
  3107. for i:=0 to ExeSymbolList.Count-1 do
  3108. begin
  3109. sym:=TExeSymbol(ExeSymbolList[i]);
  3110. { an unresolved weak symbol has objsection=nil }
  3111. if assigned(sym.ObjSymbol.objsection) and
  3112. (not sym.ObjSymbol.objsection.Used) then
  3113. ExeSymbolList[i]:=nil;
  3114. end;
  3115. ExeSymbolList.Pack;
  3116. end;
  3117. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  3118. begin
  3119. if AValue>MaxMemPos then
  3120. Message1(link_f_executable_too_big, target_os_string);
  3121. FCurrMemPos:=AValue;
  3122. end;
  3123. procedure TExeOutput.WriteExeSectionContent;
  3124. var
  3125. exesec : TExeSection;
  3126. objsec : TObjSection;
  3127. i,j : longint;
  3128. dpos,pad: aword;
  3129. begin
  3130. for j:=0 to ExeSectionList.Count-1 do
  3131. begin
  3132. exesec:=TExeSection(ExeSectionList[j]);
  3133. { don't write normal section if writing only debug info }
  3134. if (ExeWriteMode=ewm_dbgonly) and
  3135. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  3136. continue;
  3137. if oso_data in exesec.SecOptions then
  3138. begin
  3139. if exesec.DataPos<FWriter.Size then
  3140. InternalError(2012103001);
  3141. FWriter.Writezeros(exesec.DataPos-FWriter.Size);
  3142. for i:=0 to exesec.ObjSectionList.Count-1 do
  3143. begin
  3144. objsec:=TObjSection(exesec.ObjSectionList[i]);
  3145. if oso_data in objsec.secoptions then
  3146. begin
  3147. if not assigned(objsec.data) then
  3148. internalerror(200603042);
  3149. dpos:=objsec.MemPos-exesec.MemPos+exesec.DataPos;
  3150. pad:=dpos-FWriter.Size;
  3151. { objsection must be within SecAlign bytes from the previous one }
  3152. if (dpos<FWriter.Size) or
  3153. (pad>=max(objsec.SecAlign,1)) then
  3154. internalerror(200602251);
  3155. FWriter.writeZeros(pad);
  3156. FWriter.writearray(objsec.data);
  3157. end;
  3158. end;
  3159. end;
  3160. end;
  3161. end;
  3162. procedure TExeOutput.ReplaceExeSectionList(newlist: TFPList);
  3163. var
  3164. tmp: TFPHashObjectList;
  3165. i: longint;
  3166. begin
  3167. tmp:=TFPHashObjectList.Create(true);
  3168. for i:=0 to newlist.count-1 do
  3169. TFPHashObject(newlist[i]).ChangeOwner(tmp);
  3170. { prevent destruction of existing sections }
  3171. for i:=0 to ExeSectionList.count-1 do
  3172. ExeSectionList.List[i]:=nil;
  3173. FExeSectionList.Free;
  3174. FExeSectionList:=tmp;
  3175. end;
  3176. {****************************************************************************
  3177. TObjInput
  3178. ****************************************************************************}
  3179. constructor TObjInput.create;
  3180. begin
  3181. end;
  3182. procedure TObjInput.inputerror(const s : string);
  3183. begin
  3184. Comment(V_Error,s+' while reading '+InputFileName);
  3185. end;
  3186. class function TObjInput.CanReadObjData(AReader:TObjectReader):boolean;
  3187. begin
  3188. result:=false;
  3189. end;
  3190. procedure TObjInput.ReadSectionContent(Data:TObjData);
  3191. var
  3192. i: longint;
  3193. sec: TObjSection;
  3194. begin
  3195. for i:=0 to Data.ObjSectionList.Count-1 do
  3196. begin
  3197. sec:=TObjSection(Data.ObjSectionList[i]);
  3198. { Skip debug sections }
  3199. if (oso_debug in sec.SecOptions) and
  3200. (cs_link_strip in current_settings.globalswitches) and
  3201. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  3202. continue;
  3203. if assigned(sec.Data) then
  3204. begin
  3205. FReader.Seek(sec.datapos);
  3206. if not FReader.ReadArray(sec.data,sec.Size) then
  3207. begin
  3208. InputError('Can''t read object data');
  3209. exit;
  3210. end;
  3211. end;
  3212. end;
  3213. end;
  3214. {$ifdef MEMDEBUG}
  3215. initialization
  3216. memobjsymbols:=TMemDebug.create('ObjSymbols');
  3217. memobjsymbols.stop;
  3218. memobjsections:=TMemDebug.create('ObjSections');
  3219. memobjsections.stop;
  3220. finalization
  3221. memobjsymbols.free;
  3222. memobjsections.free;
  3223. {$endif MEMDEBUG}
  3224. end.