ogbase.pas 89 KB

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