ogbase.pas 108 KB

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