ogbase.pas 84 KB

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