ogbase.pas 88 KB

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