ogbase.pas 70 KB

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