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