ogbase.pas 71 KB

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