ogbase.pas 84 KB

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