ogbase.pas 69 KB

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