2
0

ogbase.pas 124 KB

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