ogbase.pas 70 KB

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