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