ogbase.pas 123 KB

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