ogbase.pas 85 KB

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