ogbase.pas 87 KB

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