ogbase.pas 85 KB

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