ogbase.pas 86 KB

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