ogbase.pas 85 KB

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