ogbase.pas 71 KB

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