ogbase.pas 84 KB

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