ogbase.pas 85 KB

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