ogbase.pas 123 KB

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