ogbase.pas 83 KB

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