ogbase.pas 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  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. TObjRelocationType = (
  36. { Relocation to absolute address }
  37. RELOC_ABSOLUTE,
  38. {$ifdef x86_64}
  39. { 32bit Relocation to absolute address }
  40. RELOC_ABSOLUTE32,
  41. { 64 bit coff only }
  42. RELOC_RELATIVE_1,
  43. RELOC_RELATIVE_2,
  44. RELOC_RELATIVE_3,
  45. RELOC_RELATIVE_4,
  46. RELOC_RELATIVE_5,
  47. { PIC }
  48. RELOC_GOTPCREL,
  49. RELOC_PLT32,
  50. {$endif x86_64}
  51. {$ifdef arm}
  52. RELOC_RELATIVE_24,
  53. {$endif arm}
  54. { Relative relocation }
  55. RELOC_RELATIVE,
  56. { PECoff (Windows) RVA relocation }
  57. RELOC_RVA,
  58. { PECoff (Windows) section relocation, required by DWARF2 debug info }
  59. RELOC_SECREL32,
  60. { Generate a 0 value at the place of the relocation,
  61. this is used to remove unused vtable entries }
  62. RELOC_ZERO,
  63. { No relocation is needed. It is used in ARM object files.
  64. Also internal linker use this reloc to make virtual (not real)
  65. links to some sections }
  66. RELOC_NONE
  67. );
  68. {$ifndef x86_64}
  69. const
  70. RELOC_ABSOLUTE32 = RELOC_ABSOLUTE;
  71. {$endif x86_64}
  72. const
  73. { stab types }
  74. N_GSYM = $20;
  75. N_STSYM = 38; { initialized const }
  76. N_LCSYM = 40; { non initialized variable}
  77. N_Function = $24; { function or const }
  78. N_TextLine = $44;
  79. N_DataLine = $46;
  80. N_BssLine = $48;
  81. N_RSYM = $40; { register variable }
  82. N_LSYM = $80;
  83. N_tsym = 160;
  84. N_SourceFile = $64;
  85. N_IncludeFile = $84;
  86. N_BINCL = $82;
  87. N_EINCL = $A2;
  88. N_LBRAC = $C0;
  89. N_EXCL = $C2;
  90. N_RBRAC = $E0;
  91. { GNU extensions }
  92. debuglinkname='.gnu_debuglink';
  93. type
  94. TObjSectionOption = (
  95. { Has Data available in the file }
  96. oso_Data,
  97. { Is loaded into memory }
  98. oso_load,
  99. { Not loaded into memory }
  100. oso_noload,
  101. { Read only }
  102. oso_readonly,
  103. { Read/Write }
  104. oso_write,
  105. { Contains executable instructions }
  106. oso_executable,
  107. { Never discard section }
  108. oso_keep,
  109. { Special common symbols }
  110. oso_common,
  111. { Contains debug info and can be stripped }
  112. oso_debug,
  113. { Contains only strings }
  114. oso_strings
  115. );
  116. TObjSectionOptions = set of TObjSectionOption;
  117. TObjSymbol = class(TFPHashObject)
  118. public
  119. bind : TAsmsymbind;
  120. typ : TAsmsymtype;
  121. { Current assemble pass, used to detect duplicate labels }
  122. pass : byte;
  123. objsection : TObjSection;
  124. symidx : longint;
  125. offset,
  126. size : aword;
  127. { Used for external and common solving during linking }
  128. exesymbol : TExeSymbol;
  129. constructor create(AList:TFPHashObjectList;const AName:string);
  130. function address:aword;
  131. procedure SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  132. end;
  133. { Stabs is common for all targets }
  134. TObjStabEntry=packed record
  135. strpos : longint;
  136. ntype : byte;
  137. nother : byte;
  138. ndesc : word;
  139. nvalue : longint;
  140. end;
  141. PObjStabEntry=^TObjStabEntry;
  142. TObjRelocation = class
  143. DataOffset,
  144. orgsize : aword; { original size of the symbol to Relocate, required for COFF }
  145. symbol : TObjSymbol;
  146. objsection : TObjSection; { only used if symbol=nil }
  147. typ : TObjRelocationType;
  148. constructor CreateSymbol(ADataOffset:aword;s:TObjSymbol;Atyp:TObjRelocationType);
  149. constructor CreateSymbolSize(ADataOffset:aword;s:TObjSymbol;Aorgsize:aword;Atyp:TObjRelocationType);
  150. constructor CreateSection(ADataOffset:aword;aobjsec:TObjSection;Atyp:TObjRelocationType);
  151. end;
  152. TObjSection = class(TFPHashObject)
  153. private
  154. FData : TDynamicArray;
  155. FSecOptions : TObjSectionOptions;
  156. FCachedFullName : pshortstring;
  157. procedure SetSecOptions(Aoptions:TObjSectionOptions);
  158. public
  159. ObjData : TObjData;
  160. SecSymIdx : longint; { index for the section in symtab }
  161. SecAlign : shortint; { alignment of the section }
  162. { section Data }
  163. Size,
  164. DataPos,
  165. MemPos : aword;
  166. DataAlignBytes : shortint;
  167. { Relocations (=references) to other sections }
  168. ObjRelocations : TFPObjectList;
  169. { Symbols this defines }
  170. ObjSymbolDefines : TFPObjectList;
  171. { executable linking }
  172. ExeSection : TExeSection;
  173. USed : Boolean;
  174. VTRefList : TFPObjectList;
  175. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);virtual;
  176. destructor destroy;override;
  177. function write(const d;l:aword):aword;
  178. function writestr(const s:string):aword;
  179. function WriteZeros(l:longword):aword;
  180. function setmempos(mpos:qword):qword;
  181. procedure setDatapos(var dpos:aword);
  182. procedure alloc(l:aword);
  183. procedure addsymReloc(ofs:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  184. procedure addsectionReloc(ofs:aword;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  185. procedure AddSymbolDefine(p:TObjSymbol);
  186. procedure FixupRelocs;virtual;
  187. procedure ReleaseData;
  188. function FullName:string;
  189. property Data:TDynamicArray read FData;
  190. property SecOptions:TObjSectionOptions read FSecOptions write SetSecOptions;
  191. end;
  192. TObjSectionClass = class of TObjSection;
  193. TString80 = string[80];
  194. TObjData = class(TLinkedListItem)
  195. private
  196. FName : TString80;
  197. FCurrObjSec : TObjSection;
  198. FObjSectionList : TFPHashObjectList;
  199. FCObjSection : TObjSectionClass;
  200. { Symbols that will be defined in this object file }
  201. FObjSymbolList : TFPHashObjectList;
  202. FCachedAsmSymbolList : TFPObjectList;
  203. { Special info sections that are written to during object generation }
  204. FStabsObjSec,
  205. FStabStrObjSec : TObjSection;
  206. procedure section_reset(p:TObject;arg:pointer);
  207. procedure section_afteralloc(p:TObject;arg:pointer);
  208. procedure section_afterwrite(p:TObject;arg:pointer);
  209. protected
  210. property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
  211. public
  212. CurrPass : byte;
  213. ImageBase : aword;
  214. constructor create(const n:string);virtual;
  215. destructor destroy;override;
  216. { Sections }
  217. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  218. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
  219. function sectiontype2align(atype:TAsmSectiontype):shortint;virtual;
  220. function createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;
  221. function createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean=true):TObjSection;virtual;
  222. procedure CreateDebugSections;virtual;
  223. function findsection(const aname:string):TObjSection;
  224. procedure setsection(asec:TObjSection);
  225. { Symbols }
  226. function createsymbol(const aname:string):TObjSymbol;
  227. function symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  228. function symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  229. function symbolref(asmsym:TAsmSymbol):TObjSymbol;
  230. function symbolref(const aname:string):TObjSymbol;
  231. procedure ResetCachedAsmSymbols;
  232. { Allocation }
  233. procedure alloc(len:aword);
  234. procedure allocalign(len:shortint);
  235. procedure writebytes(const Data;len:aword);
  236. procedure writeReloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);virtual;abstract;
  237. procedure beforealloc;virtual;
  238. procedure beforewrite;virtual;
  239. procedure afteralloc;virtual;
  240. procedure afterwrite;virtual;
  241. procedure resetsections;
  242. property Name:TString80 read FName;
  243. property CurrObjSec:TObjSection read FCurrObjSec;
  244. property ObjSymbolList:TFPHashObjectList read FObjSymbolList;
  245. property ObjSectionList:TFPHashObjectList read FObjSectionList;
  246. property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
  247. property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
  248. end;
  249. TObjDataClass = class of TObjData;
  250. TObjOutput = class
  251. private
  252. FCObjData : TObjDataClass;
  253. protected
  254. { writer }
  255. FWriter : TObjectwriter;
  256. function writeData(Data:TObjData):boolean;virtual;abstract;
  257. property CObjData : TObjDataClass read FCObjData write FCObjData;
  258. public
  259. constructor create(AWriter:TObjectWriter);virtual;
  260. destructor destroy;override;
  261. function newObjData(const n:string):TObjData;
  262. function startObjectfile(const fn:string):boolean;
  263. function writeobjectfile(Data:TObjData):boolean;
  264. procedure exportsymbol(p:TObjSymbol);
  265. property Writer:TObjectWriter read FWriter;
  266. end;
  267. TObjOutputClass=class of TObjOutput;
  268. TObjInput = class
  269. private
  270. FCObjData : TObjDataClass;
  271. protected
  272. { reader }
  273. FReader : TObjectReader;
  274. InputFileName : string;
  275. property CObjData : TObjDataClass read FCObjData write FCObjData;
  276. public
  277. constructor create;virtual;
  278. destructor destroy;override;
  279. function newObjData(const n:string):TObjData;
  280. function ReadObjData(AReader:TObjectreader;Data:TObjData):boolean;virtual;abstract;
  281. procedure inputerror(const s : string);
  282. end;
  283. TObjInputClass=class of TObjInput;
  284. TVTableEntry=record
  285. ObjRelocation : TObjRelocation;
  286. orgreloctype : TObjRelocationType;
  287. Enabled,
  288. Used : Boolean;
  289. end;
  290. PVTableEntry=^TVTableEntry;
  291. TExeVTable = class
  292. private
  293. procedure CheckIdx(VTableIdx:longint);
  294. public
  295. ExeSymbol : TExeSymbol;
  296. EntryCnt : Longint;
  297. EntryArray : PVTableEntry;
  298. Consolidated : Boolean;
  299. ChildList : TFPObjectList;
  300. constructor Create(AExeSymbol:TExeSymbol);
  301. destructor Destroy;override;
  302. procedure AddChild(vt:TExeVTable);
  303. procedure AddEntry(VTableIdx:Longint);
  304. procedure SetVTableSize(ASize:longint);
  305. function VTableRef(VTableIdx:Longint):TObjRelocation;
  306. end;
  307. TSymbolState = (symstate_undefined,symstate_defined,symstate_common);
  308. TExeSymbol = class(TFPHashObject)
  309. ObjSymbol : TObjSymbol;
  310. ExeSection : TExeSection;
  311. State : TSymbolState;
  312. { Used for vmt references optimization }
  313. VTable : TExeVTable;
  314. end;
  315. TExeSection = class(TFPHashObject)
  316. private
  317. FSecSymIdx : longint;
  318. FObjSectionList : TFPObjectList;
  319. public
  320. Size,
  321. DataPos,
  322. MemPos : aword;
  323. SecAlign : shortint;
  324. SecOptions : TObjSectionOptions;
  325. constructor create(AList:TFPHashObjectList;const AName:string);virtual;
  326. destructor destroy;override;
  327. procedure AddObjSection(objsec:TObjSection);
  328. property ObjSectionList:TFPObjectList read FObjSectionList;
  329. property SecSymIdx:longint read FSecSymIdx write FSecSymIdx;
  330. end;
  331. TExeSectionClass=class of TExeSection;
  332. TStaticLibrary = class(TFPHashObject)
  333. private
  334. FArReader : TObjectReader;
  335. FObjInputClass : TObjInputClass;
  336. public
  337. constructor create(AList:TFPHashObjectList;const AName:string;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  338. destructor destroy;override;
  339. property ArReader:TObjectReader read FArReader;
  340. property ObjInputClass:TObjInputClass read FObjInputClass;
  341. end;
  342. TImportLibrary = class(TFPHashObject)
  343. private
  344. FImportSymbolList : TFPHashObjectList;
  345. public
  346. constructor create(AList:TFPHashObjectList;const AName:string);
  347. destructor destroy;override;
  348. property ImportSymbolList:TFPHashObjectList read FImportSymbolList;
  349. end;
  350. TImportSymbol = class(TFPHashObject)
  351. private
  352. FOrdNr : longint;
  353. FIsVar : boolean;
  354. FMangledName : string;
  355. public
  356. constructor create(AList:TFPHashObjectList;const AName:string;AOrdNr:longint;AIsVar:boolean);
  357. property OrdNr: longint read FOrdNr;
  358. property MangledName: string read FMangledName;
  359. property IsVar: boolean read FIsVar;
  360. end;
  361. TExeWriteMode = (ewm_exefull,ewm_dbgonly,ewm_exeonly);
  362. TExeOutput = class
  363. private
  364. { ExeSectionList }
  365. FCObjData : TObjDataClass;
  366. FCExeSection : TExeSectionClass;
  367. FCurrExeSec : TExeSection;
  368. FExeSectionList : TFPHashObjectList;
  369. Fzeronr : longint;
  370. { Symbols }
  371. FExeSymbolList : TFPHashObjectList;
  372. FUnresolvedExeSymbols : TFPObjectList;
  373. FExternalObjSymbols,
  374. FCommonObjSymbols : TFPObjectList;
  375. FEntryName : string;
  376. FExeVTableList : TFPObjectList;
  377. { Objects }
  378. FObjDataList : TFPObjectList;
  379. { Position calculation }
  380. FImageBase : aword;
  381. FCurrMemPos : qword;
  382. procedure SetCurrMemPos(const AValue: qword);
  383. protected
  384. { writer }
  385. FExeWriteMode : TExeWriteMode;
  386. FWriter : TObjectwriter;
  387. commonObjSection : TObjSection;
  388. internalObjData : TObjData;
  389. EntrySym : TObjSymbol;
  390. SectionDataAlign,
  391. SectionMemAlign : aword;
  392. function writeData:boolean;virtual;abstract;
  393. property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
  394. property CObjData:TObjDataClass read FCObjData write FCObjData;
  395. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList);virtual;
  396. public
  397. CurrDataPos : aword;
  398. MaxMemPos : qword;
  399. IsSharedLibrary : boolean;
  400. constructor create;virtual;
  401. destructor destroy;override;
  402. function FindExeSection(const aname:string):TExeSection;
  403. procedure AddObjData(ObjData:TObjData);
  404. procedure Load_Start;virtual;
  405. procedure Load_EntryName(const aname:string);virtual;
  406. procedure Load_Symbol(const aname:string);virtual;
  407. procedure Load_IsSharedLibrary;
  408. procedure Load_ImageBase(const avalue:string);
  409. procedure Order_Start;virtual;
  410. procedure Order_End;virtual;
  411. procedure Order_ExeSection(const aname:string);virtual;
  412. procedure Order_Align(const avalue:string);virtual;
  413. procedure Order_Zeros(const avalue:string);virtual;
  414. procedure Order_Symbol(const aname:string);virtual;
  415. procedure Order_EndExeSection;virtual;
  416. procedure Order_ObjSection(const aname:string);virtual;
  417. procedure MemPos_Start;virtual;
  418. procedure MemPos_Header;virtual;
  419. procedure MemPos_ExeSection(const aname:string);virtual;
  420. procedure MemPos_EndExeSection;virtual;
  421. procedure DataPos_Start;virtual;
  422. procedure DataPos_Header;virtual;
  423. procedure DataPos_ExeSection(const aname:string);virtual;
  424. procedure DataPos_EndExeSection;virtual;
  425. procedure DataPos_Symbols;virtual;
  426. procedure BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  427. procedure PackUnresolvedExeSymbols(const s:string);
  428. procedure ResolveSymbols(StaticLibraryList:TFPHashObjectList);
  429. procedure PrintMemoryMap;
  430. procedure FixupSymbols;
  431. procedure FixupRelocations;
  432. procedure MergeStabs;
  433. procedure RemoveUnreferencedSections;
  434. procedure RemoveEmptySections;
  435. procedure RemoveDebugInfo;
  436. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);virtual;
  437. procedure GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  438. function WriteExeFile(const fn:string):boolean;
  439. property Writer:TObjectWriter read FWriter;
  440. property ExeSectionList:TFPHashObjectList read FExeSectionList;
  441. property ObjDataList:TFPObjectList read FObjDataList;
  442. property ExeSymbolList:TFPHashObjectList read FExeSymbolList;
  443. property UnresolvedExeSymbols:TFPObjectList read FUnresolvedExeSymbols;
  444. property ExternalObjSymbols:TFPObjectList read FExternalObjSymbols;
  445. property CommonObjSymbols:TFPObjectList read FCommonObjSymbols;
  446. property ExeVTableList:TFPObjectList read FExeVTableList;
  447. property EntryName:string read FEntryName write FEntryName;
  448. property ImageBase:aword read FImageBase write FImageBase;
  449. property CurrExeSec:TExeSection read FCurrExeSec;
  450. property ExeWriteMode:TExeWriteMode read FExeWriteMode write FExeWriteMode;
  451. property CurrMemPos:qword read FCurrMemPos write SetCurrMemPos;
  452. end;
  453. TExeOutputClass=class of TExeOutput;
  454. var
  455. exeoutput : TExeOutput;
  456. implementation
  457. uses
  458. SysUtils,
  459. globals,verbose,fmodule,ogmap;
  460. const
  461. SectionDataMaxGrow = 4096;
  462. {$ifdef MEMDEBUG}
  463. var
  464. memobjsymbols,
  465. memobjsections : TMemDebug;
  466. {$endif MEMDEBUG}
  467. {*****************************************************************************
  468. TObjSymbol
  469. *****************************************************************************}
  470. constructor TObjSymbol.create(AList:TFPHashObjectList;const AName:string);
  471. begin;
  472. inherited create(AList,AName);
  473. bind:=AB_EXTERNAL;
  474. typ:=AT_NONE;
  475. symidx:=-1;
  476. size:=0;
  477. offset:=0;
  478. objsection:=nil;
  479. end;
  480. function TObjSymbol.address:aword;
  481. begin
  482. if assigned(objsection) then
  483. result:=offset+objsection.mempos
  484. else
  485. result:=0;
  486. end;
  487. procedure TObjSymbol.SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  488. begin
  489. if not(abind in [AB_GLOBAL,AB_LOCAL,AB_COMMON]) then
  490. internalerror(200603016);
  491. if not assigned(aobjsec) then
  492. internalerror(200603017);
  493. if (bind=AB_EXTERNAL) then
  494. begin
  495. bind:=abind;
  496. typ:=atyp;
  497. end
  498. else
  499. begin
  500. if pass=apass then
  501. begin
  502. Message1(asmw_e_duplicate_label,name);
  503. exit;
  504. end;
  505. end;
  506. pass:=apass;
  507. { Code can never grow after a pass }
  508. if assigned(objsection) and
  509. (objsection=aobjsec) and
  510. (aobjsec.size>offset) then
  511. internalerror(200603014);
  512. objsection:=aobjsec;
  513. offset:=aobjsec.size;
  514. end;
  515. {****************************************************************************
  516. TObjRelocation
  517. ****************************************************************************}
  518. constructor TObjRelocation.CreateSymbol(ADataOffset:aword;s:TObjSymbol;Atyp:TObjRelocationType);
  519. begin
  520. if not assigned(s) then
  521. internalerror(200603034);
  522. DataOffset:=ADataOffset;
  523. Symbol:=s;
  524. OrgSize:=0;
  525. ObjSection:=nil;
  526. Typ:=Atyp;
  527. end;
  528. constructor TObjRelocation.CreateSymbolSize(ADataOffset:aword;s:TObjSymbol;Aorgsize:aword;Atyp:TObjRelocationType);
  529. begin
  530. if not assigned(s) then
  531. internalerror(200603035);
  532. DataOffset:=ADataOffset;
  533. Symbol:=s;
  534. OrgSize:=Aorgsize;
  535. ObjSection:=nil;
  536. Typ:=Atyp;
  537. end;
  538. constructor TObjRelocation.CreateSection(ADataOffset:aword;aobjsec:TObjSection;Atyp:TObjRelocationType);
  539. begin
  540. if not assigned(aobjsec) then
  541. internalerror(200603036);
  542. DataOffset:=ADataOffset;
  543. Symbol:=nil;
  544. OrgSize:=0;
  545. ObjSection:=aobjsec;
  546. Typ:=Atyp;
  547. end;
  548. {****************************************************************************
  549. TObjSection
  550. ****************************************************************************}
  551. constructor TObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  552. begin
  553. inherited Create(AList,Aname);
  554. { Data }
  555. Size:=0;
  556. Datapos:=0;
  557. mempos:=0;
  558. FData:=Nil;
  559. { Setting the secoptions allocates Data if needed }
  560. secoptions:=Aoptions;
  561. secalign:=Aalign;
  562. secsymidx:=0;
  563. { relocation }
  564. ObjRelocations:=TFPObjectList.Create(true);
  565. ObjSymbolDefines:=TFPObjectList.Create(false);
  566. VTRefList:=TFPObjectList.Create(false);
  567. end;
  568. destructor TObjSection.destroy;
  569. begin
  570. if assigned(Data) then
  571. Data.Free;
  572. stringdispose(FCachedFullName);
  573. ObjRelocations.Free;
  574. ObjSymbolDefines.Free;
  575. VTRefList.Free;
  576. inherited destroy;
  577. end;
  578. procedure TObjSection.SetSecOptions(Aoptions:TObjSectionOptions);
  579. begin
  580. FSecOptions:=FSecOptions+AOptions;
  581. if (oso_Data in secoptions) and
  582. not assigned(FData) then
  583. FData:=TDynamicArray.Create(SectionDataMaxGrow);
  584. end;
  585. function TObjSection.write(const d;l:aword):aword;
  586. begin
  587. result:=size;
  588. if assigned(Data) then
  589. begin
  590. if Size<>Data.size then
  591. internalerror(200602281);
  592. Data.write(d,l);
  593. inc(Size,l);
  594. end
  595. else
  596. internalerror(200602289);
  597. end;
  598. function TObjSection.writestr(const s:string):aword;
  599. begin
  600. result:=Write(s[1],length(s));
  601. end;
  602. function TObjSection.WriteZeros(l:longword):aword;
  603. var
  604. empty : array[0..1023] of byte;
  605. begin
  606. if l>sizeof(empty) then
  607. internalerror(200404082);
  608. if l>0 then
  609. begin
  610. fillchar(empty,l,0);
  611. result:=Write(empty,l);
  612. end
  613. else
  614. result:=Size;
  615. end;
  616. procedure TObjSection.setDatapos(var dpos:aword);
  617. begin
  618. if oso_Data in secoptions then
  619. begin
  620. { get aligned Datapos }
  621. Datapos:=align(dpos,secalign);
  622. Dataalignbytes:=Datapos-dpos;
  623. { return updated Datapos }
  624. dpos:=Datapos+size;
  625. end
  626. else
  627. Datapos:=dpos;
  628. end;
  629. function TObjSection.setmempos(mpos:qword):qword;
  630. begin
  631. mempos:=align(mpos,secalign);
  632. { return updated mempos }
  633. result:=mempos+size;
  634. end;
  635. procedure TObjSection.alloc(l:aword);
  636. begin
  637. inc(size,l);
  638. end;
  639. procedure TObjSection.addsymReloc(ofs:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  640. begin
  641. ObjRelocations.Add(TObjRelocation.CreateSymbol(ofs,p,reloctype));
  642. end;
  643. procedure TObjSection.addsectionReloc(ofs:aword;aobjsec:TObjSection;Reloctype:TObjRelocationType);
  644. begin
  645. ObjRelocations.Add(TObjRelocation.CreateSection(ofs,aobjsec,reloctype));
  646. end;
  647. procedure TObjSection.AddSymbolDefine(p:TObjSymbol);
  648. begin
  649. if p.bind<>AB_GLOBAL then
  650. exit;
  651. ObjSymbolDefines.Add(p);
  652. end;
  653. procedure TObjSection.FixupRelocs;
  654. begin
  655. end;
  656. procedure TObjSection.ReleaseData;
  657. begin
  658. if assigned(FData) then
  659. begin
  660. FData.free;
  661. FData:=nil;
  662. end;
  663. ObjRelocations.free;
  664. ObjRelocations:=nil;
  665. ObjSymbolDefines.Free;
  666. ObjSymbolDefines:=nil;
  667. if assigned(FCachedFullName) then
  668. begin
  669. stringdispose(FCachedFullName);
  670. FCachedFullName:=nil;
  671. end;
  672. end;
  673. function TObjSection.FullName:string;
  674. begin
  675. if not assigned(FCachedFullName) then
  676. begin
  677. if assigned(ObjData) then
  678. FCachedFullName:=stringdup(ObjData.Name+'('+Name+')')
  679. else
  680. FCachedFullName:=stringdup(Name);
  681. end;
  682. result:=FCachedFullName^;
  683. end;
  684. {****************************************************************************
  685. TObjData
  686. ****************************************************************************}
  687. constructor TObjData.create(const n:string);
  688. begin
  689. inherited create;
  690. FName:=ExtractFileName(n);
  691. FObjSectionList:=TFPHashObjectList.Create(true);
  692. FStabsObjSec:=nil;
  693. FStabStrObjSec:=nil;
  694. { symbols }
  695. FObjSymbolList:=TFPHashObjectList.Create(true);
  696. FCachedAsmSymbolList:=TFPObjectList.Create(false);
  697. { section class type for creating of new sections }
  698. FCObjSection:=TObjSection;
  699. end;
  700. destructor TObjData.destroy;
  701. begin
  702. { Symbols }
  703. {$ifdef MEMDEBUG}
  704. MemObjSymbols.Start;
  705. {$endif}
  706. ResetCachedAsmSymbols;
  707. FCachedAsmSymbolList.free;
  708. FObjSymbolList.free;
  709. {$ifdef MEMDEBUG}
  710. MemObjSymbols.Stop;
  711. {$endif}
  712. { Sections }
  713. {$ifdef MEMDEBUG}
  714. MemObjSections.Start;
  715. {$endif}
  716. FObjSectionList.free;
  717. {$ifdef MEMDEBUG}
  718. MemObjSections.Stop;
  719. {$endif}
  720. inherited destroy;
  721. end;
  722. function TObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  723. const
  724. secnames : array[TAsmSectiontype] of string[16] = ('',
  725. 'code',
  726. 'Data',
  727. 'Data',
  728. 'roData',
  729. 'bss',
  730. 'threadvar',
  731. 'pdata',
  732. 'stub',
  733. 'stab','stabstr',
  734. 'iData2','iData4','iData5','iData6','iData7','eData',
  735. 'eh_frame',
  736. 'debug_frame','debug_info','debug_line','debug_abbrev',
  737. 'fpc',
  738. 'toc',
  739. 'init',
  740. 'fini'
  741. );
  742. var
  743. sep : string[3];
  744. begin
  745. if aname<>'' then
  746. begin
  747. case aorder of
  748. secorder_begin :
  749. sep:='.b_';
  750. secorder_end :
  751. sep:='.z_';
  752. else
  753. sep:='.n_';
  754. end;
  755. result:=secnames[atype]+sep+aname
  756. end
  757. else
  758. result:=secnames[atype];
  759. end;
  760. function TObjData.sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;
  761. const
  762. secoptions : array[TAsmSectiontype] of TObjSectionOptions = ([],
  763. {code} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep],
  764. {Data} [oso_Data,oso_load,oso_write,oso_keep],
  765. { TODO: Fix sec_rodata be read-only-with-relocs}
  766. {roData} [oso_Data,oso_load,oso_write,oso_keep],
  767. { TODO: Fix sec_rodata_norel be read-only/constant}
  768. {roData_norel} [oso_Data,oso_load,oso_write,oso_keep],
  769. {bss} [oso_load,oso_write,oso_keep],
  770. {threadvar} [oso_load,oso_write],
  771. {pdata} [oso_load,oso_readonly,oso_keep],
  772. {stub} [oso_Data,oso_load,oso_readonly,oso_executable],
  773. {stab} [oso_Data,oso_noload,oso_debug],
  774. {stabstr} [oso_Data,oso_noload,oso_strings,oso_debug],
  775. {iData2} [oso_Data,oso_load,oso_write],
  776. {iData4} [oso_Data,oso_load,oso_write],
  777. {iData5} [oso_Data,oso_load,oso_write],
  778. {iData6} [oso_Data,oso_load,oso_write],
  779. {iData7} [oso_Data,oso_load,oso_write],
  780. {eData} [oso_Data,oso_load,oso_readonly],
  781. {eh_frame} [oso_Data,oso_load,oso_readonly],
  782. {debug_frame} [oso_Data,oso_noload,oso_debug],
  783. {debug_info} [oso_Data,oso_noload,oso_debug],
  784. {debug_line} [oso_Data,oso_noload,oso_debug],
  785. {debug_abbrev} [oso_Data,oso_noload,oso_debug],
  786. {fpc} [oso_Data,oso_load,oso_write,oso_keep],
  787. {toc} [oso_Data,oso_load,oso_readonly],
  788. {init} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep],
  789. {fini} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep]
  790. );
  791. begin
  792. result:=secoptions[atype];
  793. end;
  794. function TObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  795. begin
  796. case atype of
  797. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev:
  798. result:=1;
  799. sec_code,
  800. sec_bss,
  801. sec_data:
  802. result:=16;
  803. { For idata (at least idata2) it must be 4 bytes, because
  804. an entry is always (also in win64) 20 bytes and aligning
  805. on 8 bytes will insert 4 bytes between the entries resulting
  806. in a corrupt idata section }
  807. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7:
  808. result:=4;
  809. else
  810. result:=sizeof(pint);
  811. end;
  812. end;
  813. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  814. begin
  815. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  816. end;
  817. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  818. begin
  819. if DiscardDuplicate then
  820. result:=TObjSection(FObjSectionList.Find(aname))
  821. else
  822. result:=nil;
  823. if not assigned(result) then
  824. begin
  825. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  826. result.ObjData:=self;
  827. end;
  828. FCurrObjSec:=result;
  829. end;
  830. procedure TObjData.CreateDebugSections;
  831. begin
  832. end;
  833. function TObjData.FindSection(const aname:string):TObjSection;
  834. begin
  835. result:=TObjSection(FObjSectionList.Find(aname));
  836. end;
  837. procedure TObjData.setsection(asec:TObjSection);
  838. begin
  839. if asec.ObjData<>self then
  840. internalerror(200403041);
  841. FCurrObjSec:=asec;
  842. end;
  843. function TObjData.createsymbol(const aname:string):TObjSymbol;
  844. begin
  845. result:=TObjSymbol(FObjSymbolList.Find(aname));
  846. if not assigned(result) then
  847. result:=TObjSymbol.Create(FObjSymbolList,aname);
  848. end;
  849. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  850. begin
  851. if assigned(asmsym) then
  852. begin
  853. if not assigned(asmsym.cachedObjSymbol) then
  854. begin
  855. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  856. asmsym.cachedObjSymbol:=result;
  857. FCachedAsmSymbolList.add(asmsym);
  858. end
  859. else
  860. begin
  861. result:=TObjSymbol(asmsym.cachedObjSymbol);
  862. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  863. { Register also in TObjSection }
  864. CurrObjSec.AddSymbolDefine(result);
  865. end;
  866. end
  867. else
  868. result:=nil;
  869. end;
  870. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  871. begin
  872. if not assigned(CurrObjSec) then
  873. internalerror(200603051);
  874. result:=CreateSymbol(aname);
  875. { Register also in TObjSection }
  876. CurrObjSec.AddSymbolDefine(result);
  877. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  878. end;
  879. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  880. begin
  881. if assigned(asmsym) then
  882. begin
  883. if not assigned(asmsym.cachedObjSymbol) then
  884. begin
  885. result:=symbolref(asmsym.name);
  886. asmsym.cachedObjSymbol:=result;
  887. FCachedAsmSymbolList.add(asmsym);
  888. end
  889. else
  890. result:=TObjSymbol(asmsym.cachedObjSymbol);
  891. end
  892. else
  893. result:=nil;
  894. end;
  895. function TObjData.symbolref(const aname:string):TObjSymbol;
  896. begin
  897. if not assigned(CurrObjSec) then
  898. internalerror(200603052);
  899. result:=CreateSymbol(aname);
  900. end;
  901. procedure TObjData.ResetCachedAsmSymbols;
  902. var
  903. i : longint;
  904. begin
  905. for i:=0 to FCachedAsmSymbolList.Count-1 do
  906. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  907. FCachedAsmSymbolList.Clear;
  908. end;
  909. procedure TObjData.writebytes(const Data;len:aword);
  910. begin
  911. if not assigned(CurrObjSec) then
  912. internalerror(200402251);
  913. CurrObjSec.write(Data,len);
  914. end;
  915. procedure TObjData.alloc(len:aword);
  916. begin
  917. if not assigned(CurrObjSec) then
  918. internalerror(200402252);
  919. CurrObjSec.alloc(len);
  920. end;
  921. procedure TObjData.allocalign(len:shortint);
  922. begin
  923. if not assigned(CurrObjSec) then
  924. internalerror(200402253);
  925. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  926. end;
  927. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  928. begin
  929. with TObjSection(p) do
  930. alloc(align(size,secalign)-size);
  931. end;
  932. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  933. begin
  934. with TObjSection(p) do
  935. begin
  936. if assigned(Data) then
  937. writezeros(align(size,secalign)-size);
  938. end;
  939. end;
  940. procedure TObjData.section_reset(p:TObject;arg:pointer);
  941. begin
  942. with TObjSection(p) do
  943. begin
  944. Size:=0;
  945. Datapos:=0;
  946. mempos:=0;
  947. end;
  948. end;
  949. procedure TObjData.beforealloc;
  950. begin
  951. { create stabs sections if debugging }
  952. if assigned(StabsSec) then
  953. begin
  954. StabsSec.Alloc(sizeof(TObjStabEntry));
  955. StabStrSec.Alloc(1);
  956. end;
  957. end;
  958. procedure TObjData.beforewrite;
  959. var
  960. s : string[1];
  961. hstab : TObjStabEntry;
  962. begin
  963. { create stabs sections if debugging }
  964. if assigned(StabsSec) then
  965. begin
  966. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  967. fillchar(hstab,sizeof(hstab),0);
  968. StabsSec.Write(hstab,sizeof(hstab));
  969. { start of stabstr }
  970. s:=#0;
  971. StabStrSec.write(s[1],length(s));
  972. end;
  973. end;
  974. procedure TObjData.afteralloc;
  975. begin
  976. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  977. end;
  978. procedure TObjData.afterwrite;
  979. var
  980. s : string[1];
  981. hstab : TObjStabEntry;
  982. begin
  983. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  984. { For the stab section we need an HdrSym which can now be
  985. calculated more easily }
  986. if assigned(StabsSec) then
  987. begin
  988. { end of stabstr }
  989. s:=#0;
  990. StabStrSec.write(s[1],length(s));
  991. { header stab }
  992. hstab.strpos:=1;
  993. hstab.ntype:=0;
  994. hstab.nother:=0;
  995. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  996. hstab.nvalue:=StabStrSec.Size;
  997. StabsSec.Data.seek(0);
  998. StabsSec.Data.write(hstab,sizeof(hstab));
  999. end;
  1000. end;
  1001. procedure TObjData.resetsections;
  1002. begin
  1003. FObjSectionList.ForEachCall(@section_reset,nil);
  1004. end;
  1005. {****************************************************************************
  1006. TObjOutput
  1007. ****************************************************************************}
  1008. constructor TObjOutput.create(AWriter:TObjectWriter);
  1009. begin
  1010. FWriter:=AWriter;
  1011. CObjData:=TObjData;
  1012. end;
  1013. destructor TObjOutput.destroy;
  1014. begin
  1015. inherited destroy;
  1016. end;
  1017. function TObjOutput.newObjData(const n:string):TObjData;
  1018. begin
  1019. result:=CObjData.create(n);
  1020. if (cs_use_lineinfo in current_settings.globalswitches) or
  1021. (cs_debuginfo in current_settings.moduleswitches) then
  1022. result.CreateDebugSections;
  1023. end;
  1024. function TObjOutput.startObjectfile(const fn:string):boolean;
  1025. begin
  1026. result:=false;
  1027. { start the writer already, so the .a generation can initialize
  1028. the position of the current objectfile }
  1029. if not FWriter.createfile(fn) then
  1030. Comment(V_Fatal,'Can''t create object '+fn);
  1031. result:=true;
  1032. end;
  1033. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1034. begin
  1035. if errorcount=0 then
  1036. result:=writeData(Data)
  1037. else
  1038. result:=true;
  1039. { close the writer }
  1040. FWriter.closefile;
  1041. end;
  1042. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1043. begin
  1044. { export globals and common symbols, this is needed
  1045. for .a files }
  1046. if p.bind in [AB_GLOBAL,AB_COMMON] then
  1047. FWriter.writesym(p.name);
  1048. end;
  1049. {****************************************************************************
  1050. TExeVTable
  1051. ****************************************************************************}
  1052. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1053. begin
  1054. ExeSymbol:=AExeSymbol;
  1055. if ExeSymbol.State=symstate_undefined then
  1056. internalerror(200604012);
  1057. ChildList:=TFPObjectList.Create(false);
  1058. end;
  1059. destructor TExeVTable.Destroy;
  1060. begin
  1061. ChildList.Free;
  1062. if assigned(EntryArray) then
  1063. Freemem(EntryArray);
  1064. end;
  1065. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1066. var
  1067. OldEntryCnt : longint;
  1068. begin
  1069. if VTableIdx>=EntryCnt then
  1070. begin
  1071. OldEntryCnt:=EntryCnt;
  1072. EntryCnt:=VTableIdx+1;
  1073. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1074. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1075. end;
  1076. end;
  1077. procedure TExeVTable.AddChild(vt:TExeVTable);
  1078. begin
  1079. ChildList.Add(vt);
  1080. end;
  1081. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1082. var
  1083. i : longint;
  1084. objreloc : TObjRelocation;
  1085. vtblentryoffset : aword;
  1086. begin
  1087. CheckIdx(VTableIdx);
  1088. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
  1089. { Find and disable relocation }
  1090. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1091. begin
  1092. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1093. if objreloc.dataoffset=vtblentryoffset then
  1094. begin
  1095. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1096. EntryArray[VTableIdx].OrgRelocType:=objreloc.typ;
  1097. objreloc.typ:=RELOC_ZERO;
  1098. break;
  1099. end;
  1100. end;
  1101. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1102. internalerror(200604011);
  1103. end;
  1104. procedure TExeVTable.SetVTableSize(ASize:longint);
  1105. begin
  1106. if EntryCnt<>0 then
  1107. internalerror(200603313);
  1108. EntryCnt:=ASize div sizeof(pint);
  1109. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1110. end;
  1111. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1112. begin
  1113. result:=nil;
  1114. CheckIdx(VTableIdx);
  1115. if EntryArray[VTableIdx].Used then
  1116. exit;
  1117. { Restore relocation if available }
  1118. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1119. begin
  1120. EntryArray[VTableIdx].ObjRelocation.typ:=EntryArray[VTableIdx].OrgRelocType;
  1121. result:=EntryArray[VTableIdx].ObjRelocation;
  1122. end;
  1123. EntryArray[VTableIdx].Used:=true;
  1124. end;
  1125. {****************************************************************************
  1126. TExeSection
  1127. ****************************************************************************}
  1128. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1129. begin
  1130. inherited create(AList,AName);
  1131. Size:=0;
  1132. MemPos:=0;
  1133. DataPos:=0;
  1134. FSecSymIdx:=0;
  1135. FObjSectionList:=TFPObjectList.Create(false);
  1136. end;
  1137. destructor TExeSection.destroy;
  1138. begin
  1139. ObjSectionList.Free;
  1140. inherited destroy;
  1141. end;
  1142. procedure TExeSection.AddObjSection(objsec:TObjSection);
  1143. begin
  1144. ObjSectionList.Add(objsec);
  1145. if (SecOptions<>[]) then
  1146. begin
  1147. { Only if the section contains (un)initialized data the
  1148. data flag must match. This check is not needed if the
  1149. section is empty for a symbol allocation }
  1150. if (objsec.size>0) and
  1151. ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1152. Comment(V_Error,'Incompatible section options');
  1153. end
  1154. else
  1155. begin
  1156. { inherit section options }
  1157. SecAlign:=objsec.SecAlign;
  1158. SecOptions:=SecOptions+objsec.SecOptions;
  1159. end;
  1160. { relate ObjSection to ExeSection, and mark it Used by default }
  1161. objsec.ExeSection:=self;
  1162. objsec.Used:=true;
  1163. end;
  1164. {****************************************************************************
  1165. TStaticLibrary
  1166. ****************************************************************************}
  1167. constructor TStaticLibrary.create(AList:TFPHashObjectList;const AName:string;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1168. begin
  1169. inherited create(AList,AName);
  1170. FArReader:=AReader;
  1171. FObjInputClass:=AObjInputClass;
  1172. end;
  1173. destructor TStaticLibrary.destroy;
  1174. begin
  1175. ArReader.Free;
  1176. inherited destroy;
  1177. end;
  1178. {****************************************************************************
  1179. TImportLibrary
  1180. ****************************************************************************}
  1181. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1182. begin
  1183. inherited create(AList,AName);
  1184. FImportSymbolList:=TFPHashObjectList.Create(true);
  1185. end;
  1186. destructor TImportLibrary.destroy;
  1187. begin
  1188. ImportSymbolList.Free;
  1189. inherited destroy;
  1190. end;
  1191. {****************************************************************************
  1192. TImportSymbol
  1193. ****************************************************************************}
  1194. constructor TImportSymbol.create(AList:TFPHashObjectList;const AName:string;AOrdNr:longint;AIsVar:boolean);
  1195. begin
  1196. inherited Create(AList, AName);
  1197. FOrdNr:=AOrdNr;
  1198. FIsVar:=AIsVar;
  1199. FMangledName:=AName;
  1200. { Replace ? and @ in import name }
  1201. { these replaces broke existing code on i386-win32 at least, while fixed
  1202. bug 8391 on arm-wince so limit this to arm-wince (KB) }
  1203. if (target_info.system in [system_arm_wince]) then
  1204. begin
  1205. Replace(FMangledName,'?','__q$$');
  1206. Replace(FMangledName,'@','__a$$');
  1207. end;
  1208. end;
  1209. {****************************************************************************
  1210. TExeOutput
  1211. ****************************************************************************}
  1212. constructor TExeOutput.create;
  1213. begin
  1214. { init writer }
  1215. FWriter:=TObjectwriter.create;
  1216. FExeWriteMode:=ewm_exefull;
  1217. { object files }
  1218. FObjDataList:=TFPObjectList.Create(true);
  1219. { symbols }
  1220. FExeSymbolList:=TFPHashObjectList.Create(true);
  1221. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1222. FExternalObjSymbols:=TFPObjectList.Create(false);
  1223. FCommonObjSymbols:=TFPObjectList.Create(false);
  1224. FExeVTableList:=TFPObjectList.Create(false);
  1225. FEntryName:='start';
  1226. { sections }
  1227. FExeSectionList:=TFPHashObjectList.Create(true);
  1228. FImageBase:=0;
  1229. SectionMemAlign:=$1000;
  1230. SectionDataAlign:=$200;
  1231. FCExeSection:=TExeSection;
  1232. FCObjData:=TObjData;
  1233. end;
  1234. destructor TExeOutput.destroy;
  1235. begin
  1236. FExeSymbolList.free;
  1237. UnresolvedExeSymbols.free;
  1238. ExternalObjSymbols.free;
  1239. CommonObjSymbols.free;
  1240. ExeVTableList.free;
  1241. FExeSectionList.free;
  1242. ObjDatalist.free;
  1243. FWriter.free;
  1244. inherited destroy;
  1245. end;
  1246. function TExeOutput.WriteExeFile(const fn:string):boolean;
  1247. begin
  1248. result:=false;
  1249. if FWriter.createfile(fn) then
  1250. begin
  1251. { Only write the .o if there are no errors }
  1252. if errorcount=0 then
  1253. result:=writedata
  1254. else
  1255. result:=true;
  1256. { close the writer }
  1257. FWriter.closefile;
  1258. end
  1259. else
  1260. Comment(V_Fatal,'Can''t create executable '+fn);
  1261. end;
  1262. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1263. begin
  1264. result:=TExeSection(ExeSectionList.Find(aname));
  1265. end;
  1266. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1267. begin
  1268. if ObjData.classtype<>FCObjData then
  1269. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1270. ObjDataList.Add(ObjData);
  1271. end;
  1272. procedure TExeOutput.Load_Start;
  1273. begin
  1274. ObjDataList.Clear;
  1275. { Globals defined in the linker script }
  1276. if not assigned(internalObjData) then
  1277. internalObjData:=CObjData.create('*Internal*');
  1278. AddObjData(internalObjData);
  1279. { Common Data section }
  1280. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1281. end;
  1282. procedure TExeOutput.Load_EntryName(const aname:string);
  1283. begin
  1284. EntryName:=aname;
  1285. end;
  1286. procedure TExeOutput.Load_IsSharedLibrary;
  1287. begin
  1288. IsSharedLibrary:=true;
  1289. end;
  1290. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1291. var
  1292. code : integer;
  1293. objsec : TObjSection;
  1294. objsym : TObjSymbol;
  1295. exesym : TExeSymbol;
  1296. begin
  1297. val(avalue,ImageBase,code);
  1298. if code<>0 then
  1299. Comment(V_Error,'Invalid number '+avalue);
  1300. { Create __image_base__ symbol, create the symbol
  1301. in a section with adress 0 and at offset 0 }
  1302. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1303. internalObjData.setsection(objsec);
  1304. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_FUNCTION);
  1305. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1306. exesym.ObjSymbol:=objsym;
  1307. end;
  1308. procedure TExeOutput.Load_Symbol(const aname:string);
  1309. begin
  1310. internalObjData.createsection('*'+aname,0,[]);
  1311. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION);
  1312. end;
  1313. procedure TExeOutput.Order_Start;
  1314. begin
  1315. end;
  1316. procedure TExeOutput.Order_End;
  1317. begin
  1318. internalObjData.afterwrite;
  1319. end;
  1320. procedure TExeOutput.Order_ExeSection(const aname:string);
  1321. var
  1322. sec : TExeSection;
  1323. begin
  1324. sec:=FindExeSection(aname);
  1325. if not assigned(sec) then
  1326. sec:=CExeSection.create(ExeSectionList,aname);
  1327. { Clear ExeSection contents }
  1328. FCurrExeSec:=sec;
  1329. end;
  1330. procedure TExeOutput.Order_EndExeSection;
  1331. begin
  1332. if not assigned(CurrExeSec) then
  1333. internalerror(200602184);
  1334. FCurrExeSec:=nil;
  1335. end;
  1336. procedure TExeOutput.Order_ObjSection(const aname:string);
  1337. var
  1338. i,j : longint;
  1339. ObjData : TObjData;
  1340. objsec : TObjSection;
  1341. TmpObjSectionList : TFPObjectList;
  1342. begin
  1343. if not assigned(CurrExeSec) then
  1344. internalerror(200602181);
  1345. TmpObjSectionList:=TFPObjectList.Create(false);
  1346. for i:=0 to ObjDataList.Count-1 do
  1347. begin
  1348. ObjData:=TObjData(ObjDataList[i]);
  1349. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1350. begin
  1351. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1352. if (not objsec.Used) and
  1353. MatchPattern(aname,objsec.name) then
  1354. TmpObjSectionList.Add(objsec);
  1355. end;
  1356. end;
  1357. { Order list if needed }
  1358. Order_ObjSectionList(TmpObjSectionList);
  1359. { Add the (ordered) list to the current ExeSection }
  1360. for i:=0 to TmpObjSectionList.Count-1 do
  1361. begin
  1362. objsec:=TObjSection(TmpObjSectionList[i]);
  1363. CurrExeSec.AddObjSection(objsec);
  1364. end;
  1365. TmpObjSectionList.Free;
  1366. end;
  1367. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList);
  1368. begin
  1369. end;
  1370. procedure TExeOutput.Order_Symbol(const aname:string);
  1371. var
  1372. ObjSection : TObjSection;
  1373. begin
  1374. ObjSection:=internalObjData.findsection('*'+aname);
  1375. if not assigned(ObjSection) then
  1376. internalerror(200603041);
  1377. CurrExeSec.AddObjSection(ObjSection);
  1378. end;
  1379. procedure TExeOutput.Order_Align(const avalue:string);
  1380. var
  1381. code : integer;
  1382. alignval : shortint;
  1383. objsec : TObjSection;
  1384. begin
  1385. val(avalue,alignval,code);
  1386. if code<>0 then
  1387. Comment(V_Error,'Invalid number '+avalue);
  1388. if alignval<=0 then
  1389. exit;
  1390. { Create an empty section with the required aligning }
  1391. inc(Fzeronr);
  1392. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1393. CurrExeSec.AddObjSection(objsec);
  1394. end;
  1395. procedure TExeOutput.Order_Zeros(const avalue:string);
  1396. var
  1397. zeros : array[0..1023] of byte;
  1398. code : integer;
  1399. len : longint;
  1400. objsec : TObjSection;
  1401. begin
  1402. val(avalue,len,code);
  1403. if code<>0 then
  1404. Comment(V_Error,'Invalid number '+avalue);
  1405. if len<=0 then
  1406. exit;
  1407. if len>sizeof(zeros) then
  1408. internalerror(200602254);
  1409. fillchar(zeros,len,0);
  1410. inc(Fzeronr);
  1411. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1412. internalObjData.writebytes(zeros,len);
  1413. CurrExeSec.AddObjSection(objsec);
  1414. end;
  1415. procedure TExeOutput.MemPos_Start;
  1416. begin
  1417. CurrMemPos:=0;
  1418. end;
  1419. procedure TExeOutput.MemPos_Header;
  1420. begin
  1421. end;
  1422. procedure TExeOutput.MemPos_ExeSection(const aname:string);
  1423. var
  1424. i : longint;
  1425. objsec : TObjSection;
  1426. begin
  1427. { Section can be removed }
  1428. FCurrExeSec:=FindExeSection(aname);
  1429. if not assigned(CurrExeSec) then
  1430. exit;
  1431. { Alignment of ExeSection }
  1432. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1433. CurrExeSec.MemPos:=CurrMemPos;
  1434. { set position of object ObjSections }
  1435. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1436. begin
  1437. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1438. CurrMemPos:=objsec.setmempos(CurrMemPos);
  1439. end;
  1440. { calculate size of the section }
  1441. CurrExeSec.Size:=CurrMemPos-CurrExeSec.MemPos;
  1442. end;
  1443. procedure TExeOutput.MemPos_EndExeSection;
  1444. begin
  1445. if not assigned(CurrExeSec) then
  1446. exit;
  1447. FCurrExeSec:=nil;
  1448. end;
  1449. procedure TExeOutput.DataPos_Start;
  1450. begin
  1451. end;
  1452. procedure TExeOutput.DataPos_Header;
  1453. begin
  1454. end;
  1455. procedure TExeOutput.DataPos_ExeSection(const aname:string);
  1456. var
  1457. i : longint;
  1458. objsec : TObjSection;
  1459. begin
  1460. { Section can be removed }
  1461. FCurrExeSec:=FindExeSection(aname);
  1462. if not assigned(CurrExeSec) then
  1463. exit;
  1464. { don't write normal section if writing only debug info }
  1465. if (ExeWriteMode=ewm_dbgonly) and
  1466. not(oso_debug in CurrExeSec.SecOptions) then
  1467. exit;
  1468. if (oso_Data in currexesec.SecOptions) then
  1469. begin
  1470. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1471. CurrExeSec.DataPos:=CurrDataPos;
  1472. end;
  1473. { set position of object ObjSections }
  1474. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1475. begin
  1476. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1477. if (oso_Data in objsec.SecOptions) then
  1478. begin
  1479. if not(oso_Data in currexesec.SecOptions) then
  1480. internalerror(200603043);
  1481. if not assigned(objsec.Data) then
  1482. internalerror(200603044);
  1483. objsec.setDatapos(CurrDataPos);
  1484. end;
  1485. end;
  1486. end;
  1487. procedure TExeOutput.DataPos_EndExeSection;
  1488. begin
  1489. if not assigned(CurrExeSec) then
  1490. exit;
  1491. FCurrExeSec:=nil;
  1492. end;
  1493. procedure TExeOutput.DataPos_Symbols;
  1494. var
  1495. i : longint;
  1496. sym : TExeSymbol;
  1497. begin
  1498. { Removing unused symbols }
  1499. for i:=0 to ExeSymbolList.Count-1 do
  1500. begin
  1501. sym:=TExeSymbol(ExeSymbolList[i]);
  1502. if not sym.ObjSymbol.objsection.Used then
  1503. ExeSymbolList[i]:=nil;
  1504. end;
  1505. ExeSymbolList.Pack;
  1506. end;
  1507. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  1508. var
  1509. hs : string;
  1510. code : integer;
  1511. i,k,
  1512. vtableidx : longint;
  1513. vtableexesym,
  1514. childexesym,
  1515. parentexesym : TExeSymbol;
  1516. objsym : TObjSymbol;
  1517. begin
  1518. { Build inheritance tree from VTINHERIT }
  1519. for i:=0 to VTInheritList.Count-1 do
  1520. begin
  1521. objsym:=TObjSymbol(VTInheritList[i]);
  1522. hs:=objsym.name;
  1523. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  1524. Delete(hs,1,Pos('_',hs));
  1525. k:=Pos('$$',hs);
  1526. if k=0 then
  1527. internalerror(200603311);
  1528. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1529. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  1530. if not assigned(childexesym) or
  1531. not assigned(parentexesym)then
  1532. internalerror(200603312);
  1533. if not assigned(childexesym.vtable) then
  1534. begin
  1535. childexesym.vtable:=TExeVTable.Create(childexesym);
  1536. ExeVTableList.Add(childexesym.vtable);
  1537. end;
  1538. if not assigned(parentexesym.vtable) then
  1539. begin
  1540. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  1541. ExeVTableList.Add(parentexesym.vtable);
  1542. end;
  1543. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  1544. if parentexesym<>childexesym then
  1545. parentexesym.vtable.AddChild(childexesym.vtable);
  1546. end;
  1547. { Find VTable entries from VTENTRY }
  1548. for i:=0 to VTEntryList.Count-1 do
  1549. begin
  1550. objsym:=TObjSymbol(VTEntryList[i]);
  1551. hs:=objsym.name;
  1552. { VTENTRY_<VTableName>$$<Index> }
  1553. Delete(hs,1,Pos('_',hs));
  1554. k:=Pos('$$',hs);
  1555. if k=0 then
  1556. internalerror(200603319);
  1557. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1558. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  1559. if (code<>0) then
  1560. internalerror(200603318);
  1561. if not assigned(vtableexesym) then
  1562. internalerror(2006033110);
  1563. vtableexesym.vtable.AddEntry(vtableidx);
  1564. end;
  1565. end;
  1566. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  1567. var
  1568. i : longint;
  1569. exesym : TExeSymbol;
  1570. begin
  1571. { Generate a list of Unresolved External symbols }
  1572. for i:=0 to UnresolvedExeSymbols.count-1 do
  1573. begin
  1574. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1575. if exesym.State<>symstate_undefined then
  1576. UnresolvedExeSymbols[i]:=nil;
  1577. end;
  1578. UnresolvedExeSymbols.Pack;
  1579. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  1580. end;
  1581. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPHashObjectList);
  1582. var
  1583. ObjData : TObjData;
  1584. exesym : TExeSymbol;
  1585. objsym,
  1586. commonsym : TObjSymbol;
  1587. objinput : TObjInput;
  1588. StaticLibrary : TStaticLibrary;
  1589. firstarchive,
  1590. firstcommon : boolean;
  1591. i,j : longint;
  1592. VTEntryList,
  1593. VTInheritList : TFPObjectList;
  1594. procedure LoadObjDataSymbols(ObjData:TObjData);
  1595. var
  1596. j : longint;
  1597. hs : string;
  1598. exesym : TExeSymbol;
  1599. objsym : TObjSymbol;
  1600. begin
  1601. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  1602. begin
  1603. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  1604. { From the local symbols we are only interressed in the
  1605. VTENTRY and VTINHERIT symbols }
  1606. if objsym.bind=AB_LOCAL then
  1607. begin
  1608. if cs_link_opt_vtable in current_settings.globalswitches then
  1609. begin
  1610. hs:=objsym.name;
  1611. if (hs[1]='V') then
  1612. begin
  1613. if Copy(hs,1,5)='VTREF' then
  1614. begin
  1615. if not assigned(objsym.ObjSection.VTRefList) then
  1616. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  1617. objsym.ObjSection.VTRefList.Add(objsym);
  1618. end
  1619. else if Copy(hs,1,7)='VTENTRY' then
  1620. VTEntryList.Add(objsym)
  1621. else if Copy(hs,1,9)='VTINHERIT' then
  1622. VTInheritList.Add(objsym);
  1623. end;
  1624. end;
  1625. continue;
  1626. end;
  1627. { Search for existing exesymbol }
  1628. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  1629. if not assigned(exesym) then
  1630. begin
  1631. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1632. exesym.ObjSymbol:=objsym;
  1633. end;
  1634. objsym.ExeSymbol:=exesym;
  1635. case objsym.bind of
  1636. AB_GLOBAL :
  1637. begin
  1638. if exesym.State<>symstate_defined then
  1639. begin
  1640. exesym.ObjSymbol:=objsym;
  1641. exesym.State:=symstate_defined;
  1642. end
  1643. else
  1644. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  1645. end;
  1646. AB_EXTERNAL :
  1647. begin
  1648. ExternalObjSymbols.add(objsym);
  1649. { Register unresolved symbols only the first time they
  1650. are registered }
  1651. if exesym.ObjSymbol=objsym then
  1652. UnresolvedExeSymbols.Add(exesym);
  1653. end;
  1654. AB_COMMON :
  1655. begin
  1656. if exesym.State=symstate_undefined then
  1657. begin
  1658. exesym.ObjSymbol:=objsym;
  1659. exesym.State:=symstate_common;
  1660. end;
  1661. CommonObjSymbols.add(objsym);
  1662. end;
  1663. end;
  1664. end;
  1665. end;
  1666. begin
  1667. VTEntryList:=TFPObjectList.Create(false);
  1668. VTInheritList:=TFPObjectList.Create(false);
  1669. {
  1670. The symbol resolving is done in 3 steps:
  1671. 1. Register symbols from objects
  1672. 2. Find symbols in static libraries
  1673. 3. Define stil undefined common symbols
  1674. }
  1675. { Step 1, Register symbols from objects }
  1676. for i:=0 to ObjDataList.Count-1 do
  1677. begin
  1678. ObjData:=TObjData(ObjDataList[i]);
  1679. LoadObjDataSymbols(ObjData);
  1680. end;
  1681. PackUnresolvedExeSymbols('in objects');
  1682. { Step 2, Find unresolved symbols in the libraries }
  1683. firstarchive:=true;
  1684. for i:=0 to StaticLibraryList.Count-1 do
  1685. begin
  1686. StaticLibrary:=TStaticLibrary(StaticLibraryList[i]);
  1687. { Process list of Unresolved External symbols, we need
  1688. to use a while loop because the list can be extended when
  1689. we load members from the library. }
  1690. j:=0;
  1691. while (j<UnresolvedExeSymbols.count) do
  1692. begin
  1693. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  1694. { Check first if the symbol is still undefined }
  1695. if exesym.State=symstate_undefined then
  1696. begin
  1697. if StaticLibrary.ArReader.OpenFile(exesym.name) then
  1698. begin
  1699. if assigned(exemap) then
  1700. begin
  1701. if firstarchive then
  1702. begin
  1703. exemap.Add('');
  1704. exemap.Add('Archive member included because of file (symbol)');
  1705. exemap.Add('');
  1706. firstarchive:=false;
  1707. end;
  1708. exemap.Add(StaticLibrary.ArReader.FileName+' - '+
  1709. {exesym.ObjSymbol.ObjSection.FullName+}
  1710. '('+exesym.Name+')');
  1711. end;
  1712. objinput:=StaticLibrary.ObjInputClass.Create;
  1713. objdata:=objinput.newObjData(StaticLibrary.ArReader.FileName);
  1714. objinput.ReadObjData(StaticLibrary.ArReader,objdata);
  1715. objinput.free;
  1716. AddObjData(objdata);
  1717. LoadObjDataSymbols(objdata);
  1718. StaticLibrary.ArReader.CloseFile;
  1719. end;
  1720. end;
  1721. inc(j);
  1722. end;
  1723. end;
  1724. PackUnresolvedExeSymbols('after static libraries');
  1725. { Step 3, Match common symbols or add to the globals }
  1726. firstcommon:=true;
  1727. for i:=0 to CommonObjSymbols.count-1 do
  1728. begin
  1729. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1730. if objsym.exesymbol.State=symstate_defined then
  1731. begin
  1732. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  1733. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  1734. end
  1735. else
  1736. begin
  1737. { allocate new objsymbol in .bss of *COMMON* and assign
  1738. it to the exesymbol }
  1739. if firstcommon then
  1740. begin
  1741. if assigned(exemap) then
  1742. exemap.AddCommonSymbolsHeader;
  1743. firstcommon:=false;
  1744. end;
  1745. internalObjData.setsection(commonObjSection);
  1746. internalObjData.allocalign(var_align(objsym.size));
  1747. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_FUNCTION);
  1748. commonsym.size:=objsym.size;
  1749. internalObjData.alloc(objsym.size);
  1750. if assigned(exemap) then
  1751. exemap.AddCommonSymbol(commonsym);
  1752. { Assign to the exesymbol }
  1753. objsym.exesymbol.objsymbol:=commonsym;
  1754. objsym.exesymbol.state:=symstate_defined;
  1755. end;
  1756. end;
  1757. PackUnresolvedExeSymbols('after defining COMMON symbols');
  1758. { Find entry symbol and print in map }
  1759. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  1760. if assigned(exesym) then
  1761. begin
  1762. EntrySym:=exesym.ObjSymbol;
  1763. if assigned(exemap) then
  1764. begin
  1765. exemap.Add('');
  1766. exemap.Add('Entry symbol '+EntryName);
  1767. end;
  1768. end
  1769. else
  1770. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  1771. { Generate VTable tree }
  1772. if cs_link_opt_vtable in current_settings.globalswitches then
  1773. BuildVTableTree(VTInheritList,VTEntryList);
  1774. VTInheritList.Free;
  1775. VTEntryList.Free;
  1776. end;
  1777. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  1778. var
  1779. debuglink : array[0..1023] of byte;
  1780. len : longint;
  1781. objsec : TObjSection;
  1782. exesec : TExeSection;
  1783. begin
  1784. { From the gdb manual chapter 15. GDB Files:
  1785. * A filename, with any leading directory components removed, followed by a zero byte,
  1786. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  1787. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  1788. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  1789. }
  1790. fillchar(debuglink,sizeof(debuglink),0);
  1791. len:=0;
  1792. move(dbgname[1],debuglink[len],length(dbgname));
  1793. inc(len,length(dbgname)+1);
  1794. len:=align(len,4);
  1795. if source_info.endian<>target_info.endian then
  1796. SwapEndian(dbgcrc);
  1797. move(dbgcrc,debuglink[len],sizeof(cardinal));
  1798. inc(len,4);
  1799. { Add section }
  1800. exesec:=FindExeSection(debuglinkname);
  1801. if not assigned(exesec) then
  1802. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  1803. exesec.SecOptions:=[oso_data,oso_keep];
  1804. exesec.SecAlign:=4;
  1805. objsec:=internalObjData.createsection(exesec.name,0,exesec.SecOptions);
  1806. internalObjData.writebytes(debuglink,len);
  1807. exesec.AddObjSection(objsec);
  1808. end;
  1809. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  1810. begin
  1811. end;
  1812. procedure TExeOutput.PrintMemoryMap;
  1813. var
  1814. exesec : TExeSection;
  1815. objsec : TObjSection;
  1816. objsym : TObjSymbol;
  1817. i,j,k : longint;
  1818. begin
  1819. if not assigned(exemap) then
  1820. exit;
  1821. exemap.AddMemoryMapHeader(ImageBase);
  1822. for i:=0 to ExeSectionList.Count-1 do
  1823. begin
  1824. exesec:=TExeSection(ExeSectionList[i]);
  1825. exemap.AddMemoryMapExeSection(exesec);
  1826. for j:=0 to exesec.ObjSectionList.count-1 do
  1827. begin
  1828. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1829. exemap.AddMemoryMapObjectSection(objsec);
  1830. for k:=0 to objsec.ObjSymbolDefines.Count-1 do
  1831. begin
  1832. objsym:=TObjSymbol(objsec.ObjSymbolDefines[k]);
  1833. exemap.AddMemoryMapSymbol(objsym);
  1834. end;
  1835. end;
  1836. end;
  1837. end;
  1838. procedure TExeOutput.FixupSymbols;
  1839. procedure UpdateSymbol(objsym:TObjSymbol);
  1840. begin
  1841. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  1842. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  1843. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  1844. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  1845. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  1846. end;
  1847. var
  1848. i : longint;
  1849. objsym : TObjSymbol;
  1850. exesym : TExeSymbol;
  1851. begin
  1852. { Print list of Unresolved External symbols }
  1853. for i:=0 to UnresolvedExeSymbols.count-1 do
  1854. begin
  1855. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1856. if exesym.State<>symstate_defined then
  1857. Comment(V_Error,'Undefined symbol: '+exesym.name);
  1858. end;
  1859. { Update ImageBase to ObjData so it can access from ObjSymbols }
  1860. for i:=0 to ObjDataList.Count-1 do
  1861. TObjData(ObjDataList[i]).imagebase:=imagebase;
  1862. {
  1863. Fixing up symbols is done in the following steps:
  1864. 1. Update common references
  1865. 2. Update external references
  1866. }
  1867. { Step 1, Update commons }
  1868. for i:=0 to CommonObjSymbols.count-1 do
  1869. begin
  1870. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1871. if objsym.bind<>AB_COMMON then
  1872. internalerror(200606241);
  1873. UpdateSymbol(objsym);
  1874. end;
  1875. { Step 2, Update externals }
  1876. for i:=0 to ExternalObjSymbols.count-1 do
  1877. begin
  1878. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  1879. if objsym.bind<>AB_EXTERNAL then
  1880. internalerror(200606242);
  1881. UpdateSymbol(objsym);
  1882. end;
  1883. end;
  1884. procedure TExeOutput.MergeStabs;
  1885. var
  1886. stabexesec,
  1887. stabstrexesec : TExeSection;
  1888. relocsec,
  1889. currstabsec,
  1890. currstabstrsec,
  1891. mergedstabsec,
  1892. mergedstabstrsec : TObjSection;
  1893. hstabreloc,
  1894. currstabreloc : TObjRelocation;
  1895. i,j : longint;
  1896. currstabrelocidx,
  1897. mergestabcnt,
  1898. stabcnt : longword;
  1899. skipstab : boolean;
  1900. skipfun : boolean;
  1901. hstab : TObjStabEntry;
  1902. stabrelocofs : longword;
  1903. buf : array[0..1023] of byte;
  1904. bufend,
  1905. bufsize : longint;
  1906. begin
  1907. stabexesec:=FindExeSection('.stab');
  1908. stabstrexesec:=FindExeSection('.stabstr');
  1909. if (stabexesec=nil) or
  1910. (stabstrexesec=nil) or
  1911. (stabexesec.ObjSectionlist.count=0) then
  1912. exit;
  1913. { Create new stabsection }
  1914. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  1915. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  1916. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  1917. { write stab for hdrsym }
  1918. fillchar(hstab,sizeof(TObjStabEntry),0);
  1919. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  1920. mergestabcnt:=1;
  1921. { .stabstr starts with a #0 }
  1922. buf[0]:=0;
  1923. mergedstabstrsec.write(buf[0],1);
  1924. skipfun:=false;
  1925. { Copy stabs and corresponding Relocations }
  1926. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  1927. begin
  1928. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  1929. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  1930. if assigned(currstabstrsec) then
  1931. begin
  1932. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  1933. currstabsec.Data.seek(0);
  1934. currstabrelocidx:=0;
  1935. for j:=0 to stabcnt-1 do
  1936. begin
  1937. hstabreloc:=nil;
  1938. skipstab:=false;
  1939. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  1940. { Only include first hdrsym stab }
  1941. if hstab.ntype=0 then
  1942. skipstab:=true;
  1943. if skipfun then
  1944. begin
  1945. { Skip all stabs for function body until N_RBRAC }
  1946. skipfun:=hstab.ntype<>N_RBRAC;
  1947. skipstab:=true;
  1948. end;
  1949. if not skipstab then
  1950. begin
  1951. { Find corresponding Relocation }
  1952. currstabreloc:=nil;
  1953. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  1954. begin
  1955. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  1956. if assigned(currstabreloc) and
  1957. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  1958. break;
  1959. inc(currstabrelocidx);
  1960. end;
  1961. if assigned(currstabreloc) and
  1962. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  1963. begin
  1964. hstabReloc:=currstabReloc;
  1965. inc(currstabrelocidx);
  1966. end;
  1967. { Check if the stab is refering to a removed section }
  1968. if assigned(hstabreloc) then
  1969. begin
  1970. if assigned(hstabreloc.Symbol) then
  1971. relocsec:=hstabreloc.Symbol.ObjSection
  1972. else
  1973. relocsec:=hstabreloc.ObjSection;
  1974. if not assigned(relocsec) then
  1975. internalerror(200603302);
  1976. if not relocsec.Used then
  1977. begin
  1978. skipstab:=true;
  1979. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  1980. begin
  1981. currstabstrsec.Data.seek(hstab.strpos);
  1982. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  1983. bufend:=indexbyte(buf,bufsize,Ord(':'));
  1984. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  1985. skipfun:=true;
  1986. end;
  1987. end;
  1988. end;
  1989. end;
  1990. if not skipstab then
  1991. begin
  1992. { Copy string in stabstr }
  1993. if hstab.strpos<>0 then
  1994. begin
  1995. currstabstrsec.Data.seek(hstab.strpos);
  1996. hstab.strpos:=mergedstabstrsec.Size;
  1997. repeat
  1998. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  1999. bufend:=indexbyte(buf,bufsize,0);
  2000. if bufend=-1 then
  2001. bufend:=bufsize
  2002. else
  2003. begin
  2004. { include the #0 }
  2005. inc(bufend);
  2006. end;
  2007. mergedstabstrsec.write(buf,bufend);
  2008. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2009. end;
  2010. { Copy and Update the relocation }
  2011. if assigned(hstabreloc) then
  2012. begin
  2013. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2014. { Remove from List without freeing the object }
  2015. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2016. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2017. end;
  2018. { Write updated stab }
  2019. mergedstabsec.write(hstab,sizeof(hstab));
  2020. inc(mergestabcnt);
  2021. end;
  2022. end;
  2023. end;
  2024. { Unload stabs }
  2025. if assigned(currstabstrsec) then
  2026. begin
  2027. currstabstrsec.Used:=False;
  2028. currstabstrsec.ReleaseData;
  2029. end;
  2030. currstabsec.Used:=false;
  2031. currstabsec.ReleaseData;
  2032. end;
  2033. { Generate new HdrSym }
  2034. if mergedstabsec.Size>0 then
  2035. begin
  2036. hstab.strpos:=1;
  2037. hstab.ntype:=0;
  2038. hstab.nother:=0;
  2039. hstab.ndesc:=word(mergestabcnt-1);
  2040. hstab.nvalue:=mergedstabstrsec.Size;
  2041. mergedstabsec.Data.seek(0);
  2042. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2043. end;
  2044. { Replace all sections with our combined stabsec }
  2045. stabexesec.ObjSectionList.Clear;
  2046. stabstrexesec.ObjSectionList.Clear;
  2047. stabexesec.AddObjSection(mergedstabsec);
  2048. stabstrexesec.AddObjSection(mergedstabstrsec);
  2049. end;
  2050. procedure TExeOutput.RemoveEmptySections;
  2051. var
  2052. i, j : longint;
  2053. exesec : TExeSection;
  2054. doremove : boolean;
  2055. begin
  2056. for i:=0 to ExeSectionList.Count-1 do
  2057. begin
  2058. exesec:=TExeSection(ExeSectionList[i]);
  2059. doremove:=not(oso_keep in exesec.SecOptions) and
  2060. (
  2061. (exesec.ObjSectionlist.count=0) or
  2062. (
  2063. (cs_link_strip in current_settings.globalswitches) and
  2064. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2065. (oso_debug in exesec.SecOptions)
  2066. )
  2067. );
  2068. if not doremove then
  2069. begin
  2070. { Check if section has no actual data }
  2071. doremove:=true;
  2072. for j:=0 to exesec.ObjSectionList.Count-1 do
  2073. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2074. begin
  2075. doremove:=false;
  2076. break;
  2077. end;
  2078. end;
  2079. if doremove and not (RelocSection and (exesec.Name='.reloc')) then
  2080. begin
  2081. Comment(V_Debug,'Deleting empty section '+exesec.name);
  2082. ExeSectionList[i]:=nil;
  2083. end;
  2084. end;
  2085. ExeSectionList.Pack;
  2086. end;
  2087. procedure TExeOutput.RemoveDebugInfo;
  2088. var
  2089. i : longint;
  2090. exesec : TExeSection;
  2091. begin
  2092. for i:=0 to ExeSectionList.Count-1 do
  2093. begin
  2094. exesec:=TExeSection(ExeSectionList[i]);
  2095. if (oso_debug in exesec.SecOptions) then
  2096. ExeSectionList[i]:=nil;
  2097. end;
  2098. ExeSectionList.Pack;
  2099. end;
  2100. procedure TExeOutput.RemoveUnreferencedSections;
  2101. var
  2102. ObjSectionWorkList : TFPObjectList;
  2103. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2104. begin
  2105. if not aobjsec.Used then
  2106. begin
  2107. aobjsec.Used:=true;
  2108. ObjSectionWorkList.Add(aobjsec);
  2109. end;
  2110. end;
  2111. procedure DoReloc(objreloc:TObjRelocation);
  2112. var
  2113. objsym : TObjSymbol;
  2114. refobjsec : TObjSection;
  2115. begin
  2116. { Disabled Relocation to 0 }
  2117. if objreloc.typ=RELOC_ZERO then
  2118. exit;
  2119. if assigned(objreloc.symbol) then
  2120. begin
  2121. objsym:=objreloc.symbol;
  2122. if objsym.bind<>AB_LOCAL then
  2123. begin
  2124. if not(assigned(objsym.exesymbol) and
  2125. (objsym.exesymbol.State=symstate_defined)) then
  2126. internalerror(200603063);
  2127. objsym:=objsym.exesymbol.objsymbol;
  2128. end;
  2129. if not assigned(objsym.objsection) then
  2130. internalerror(200603062);
  2131. refobjsec:=objsym.objsection;
  2132. end
  2133. else
  2134. if assigned(objreloc.objsection) then
  2135. refobjsec:=objreloc.objsection
  2136. else
  2137. internalerror(200603316);
  2138. if assigned(exemap) then
  2139. begin
  2140. objsym:=objreloc.symbol;
  2141. if assigned(objsym) then
  2142. exemap.Add(' References '+objsym.name+' in '
  2143. +refobjsec.fullname)
  2144. else
  2145. exemap.Add(' References '+refobjsec.fullname);
  2146. end;
  2147. AddToObjSectionWorkList(refobjsec);
  2148. end;
  2149. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2150. var
  2151. i : longint;
  2152. objreloc : TObjRelocation;
  2153. begin
  2154. objreloc:=vtable.VTableRef(VTableIdx);
  2155. if assigned(objreloc) then
  2156. begin
  2157. { Process the relocation now if the ObjSection is
  2158. already processed and marked as used. Otherwise we leave it
  2159. unprocessed. It'll then be resolved when the ObjSection is
  2160. changed to Used }
  2161. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2162. DoReloc(objreloc);
  2163. end;
  2164. { This recursive walking is done here instead of
  2165. in TExeVTable.VTableRef because we can now process
  2166. all needed relocations }
  2167. for i:=0 to vtable.ChildList.Count-1 do
  2168. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2169. end;
  2170. var
  2171. hs : string;
  2172. i,j,k : longint;
  2173. exesec : TExeSection;
  2174. objdata : TObjData;
  2175. objsec : TObjSection;
  2176. objsym : TObjSymbol;
  2177. code : integer;
  2178. vtableidx : longint;
  2179. vtableexesym : TExeSymbol;
  2180. begin
  2181. ObjSectionWorkList:=TFPObjectList.Create(false);
  2182. if assigned(exemap) then
  2183. exemap.AddHeader('Removing unreferenced sections');
  2184. { Initialize by marking all sections unused and
  2185. adding the sections with oso_keep flags to the ObjSectionWorkList }
  2186. for i:=0 to ObjDataList.Count-1 do
  2187. begin
  2188. ObjData:=TObjData(ObjDataList[i]);
  2189. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2190. begin
  2191. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2192. objsec.Used:=false;
  2193. { TODO: remove debug section always keep}
  2194. if oso_debug in objsec.secoptions then
  2195. objsec.Used:=true;
  2196. if (oso_keep in objsec.secoptions) then
  2197. begin
  2198. AddToObjSectionWorkList(objsec);
  2199. if objsec.name='.fpc.n_links' then
  2200. objsec.Used:=false;
  2201. end;
  2202. end;
  2203. end;
  2204. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  2205. { Process all sections, add new sections to process based
  2206. on the symbol references }
  2207. while ObjSectionWorkList.Count>0 do
  2208. begin
  2209. objsec:=TObjSection(ObjSectionWorkList.Last);
  2210. if assigned(exemap) then
  2211. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2212. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2213. { Process Relocations }
  2214. for i:=0 to objsec.ObjRelocations.count-1 do
  2215. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2216. { Process Virtual Entry calls }
  2217. if cs_link_opt_vtable in current_settings.globalswitches then
  2218. begin
  2219. for i:=0 to objsec.VTRefList.count-1 do
  2220. begin
  2221. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2222. hs:=objsym.name;
  2223. Delete(hs,1,Pos('_',hs));
  2224. k:=Pos('$$',hs);
  2225. if k=0 then
  2226. internalerror(200603314);
  2227. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2228. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2229. if (code<>0) then
  2230. internalerror(200603317);
  2231. if not assigned(vtableexesym) then
  2232. internalerror(200603315);
  2233. if not assigned(vtableexesym.vtable) then
  2234. internalerror(200603316);
  2235. DoVTableRef(vtableexesym.vtable,vtableidx);
  2236. end;
  2237. end;
  2238. end;
  2239. ObjSectionWorkList.Free;
  2240. ObjSectionWorkList:=nil;
  2241. { Remove unused objsections from ExeSectionList }
  2242. for i:=0 to ExeSectionList.Count-1 do
  2243. begin
  2244. exesec:=TExeSection(ExeSectionList[i]);
  2245. for j:=0 to exesec.ObjSectionlist.count-1 do
  2246. begin
  2247. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2248. if not objsec.used then
  2249. begin
  2250. if assigned(exemap) then
  2251. exemap.Add('Removing '+objsec.FullName);
  2252. exesec.ObjSectionlist[j]:=nil;
  2253. objsec.ReleaseData;
  2254. end;
  2255. end;
  2256. exesec.ObjSectionlist.Pack;
  2257. end;
  2258. end;
  2259. procedure TExeOutput.FixupRelocations;
  2260. var
  2261. i,j : longint;
  2262. exesec : TExeSection;
  2263. objsec : TObjSection;
  2264. begin
  2265. for i:=0 to ExeSectionList.Count-1 do
  2266. begin
  2267. exesec:=TExeSection(ExeSectionList[i]);
  2268. if not assigned(exesec) then
  2269. continue;
  2270. for j:=0 to exesec.ObjSectionlist.count-1 do
  2271. begin
  2272. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2273. if not objsec.Used then
  2274. internalerror(200603301);
  2275. objsec.FixupRelocs;
  2276. end;
  2277. end;
  2278. end;
  2279. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  2280. var
  2281. m: qword;
  2282. begin
  2283. if not IsSharedLibrary then
  2284. m:=AValue+FImageBase
  2285. else
  2286. m:=AValue;
  2287. if m>MaxMemPos then
  2288. Message1(link_f_executable_too_big, target_os_string);
  2289. FCurrMemPos:=AValue;
  2290. end;
  2291. {****************************************************************************
  2292. TObjInput
  2293. ****************************************************************************}
  2294. constructor TObjInput.create;
  2295. begin
  2296. end;
  2297. destructor TObjInput.destroy;
  2298. begin
  2299. inherited destroy;
  2300. end;
  2301. function TObjInput.newObjData(const n:string):TObjData;
  2302. begin
  2303. result:=CObjData.create(n);
  2304. end;
  2305. procedure TObjInput.inputerror(const s : string);
  2306. begin
  2307. Comment(V_Error,s+' while reading '+InputFileName);
  2308. end;
  2309. {$ifdef MEMDEBUG}
  2310. initialization
  2311. memobjsymbols:=TMemDebug.create('ObjSymbols');
  2312. memobjsymbols.stop;
  2313. memobjsections:=TMemDebug.create('ObjSections');
  2314. memobjsections.stop;
  2315. finalization
  2316. memobjsymbols.free;
  2317. memobjsections.free;
  2318. {$endif MEMDEBUG}
  2319. end.