ogbase.pas 123 KB

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