ogbase.pas 111 KB

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