ogbase.pas 70 KB

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