ogbase.pas 79 KB

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