ogbase.pas 70 KB

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