ogbase.pas 85 KB

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