ogbase.pas 85 KB

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