ogbase.pas 70 KB

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