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. 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. { For idata (at least idata2) it must be 4 bytes, because
  794. an entry is always (also in win64) 20 bytes and aligning
  795. on 8 bytes will insert 4 bytes between the entries resulting
  796. in a corrupt idata section }
  797. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7:
  798. result:=4;
  799. else
  800. result:=sizeof(aint);
  801. end;
  802. end;
  803. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  804. begin
  805. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  806. end;
  807. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  808. begin
  809. if DiscardDuplicate then
  810. result:=TObjSection(FObjSectionList.Find(aname))
  811. else
  812. result:=nil;
  813. if not assigned(result) then
  814. begin
  815. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  816. result.ObjData:=self;
  817. end;
  818. FCurrObjSec:=result;
  819. end;
  820. procedure TObjData.CreateDebugSections;
  821. begin
  822. end;
  823. function TObjData.FindSection(const aname:string):TObjSection;
  824. begin
  825. result:=TObjSection(FObjSectionList.Find(aname));
  826. end;
  827. procedure TObjData.setsection(asec:TObjSection);
  828. begin
  829. if asec.ObjData<>self then
  830. internalerror(200403041);
  831. FCurrObjSec:=asec;
  832. end;
  833. function TObjData.createsymbol(const aname:string):TObjSymbol;
  834. begin
  835. result:=TObjSymbol(FObjSymbolList.Find(aname));
  836. if not assigned(result) then
  837. result:=TObjSymbol.Create(FObjSymbolList,aname);
  838. end;
  839. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  840. begin
  841. if assigned(asmsym) then
  842. begin
  843. if not assigned(asmsym.cachedObjSymbol) then
  844. begin
  845. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  846. asmsym.cachedObjSymbol:=result;
  847. FCachedAsmSymbolList.add(asmsym);
  848. end
  849. else
  850. begin
  851. result:=TObjSymbol(asmsym.cachedObjSymbol);
  852. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  853. { Register also in TObjSection }
  854. CurrObjSec.AddSymbolDefine(result);
  855. end;
  856. end
  857. else
  858. result:=nil;
  859. end;
  860. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  861. begin
  862. if not assigned(CurrObjSec) then
  863. internalerror(200603051);
  864. result:=CreateSymbol(aname);
  865. { Register also in TObjSection }
  866. CurrObjSec.AddSymbolDefine(result);
  867. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  868. end;
  869. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  870. begin
  871. if assigned(asmsym) then
  872. begin
  873. if not assigned(asmsym.cachedObjSymbol) then
  874. begin
  875. result:=symbolref(asmsym.name);
  876. asmsym.cachedObjSymbol:=result;
  877. FCachedAsmSymbolList.add(asmsym);
  878. end
  879. else
  880. result:=TObjSymbol(asmsym.cachedObjSymbol);
  881. end
  882. else
  883. result:=nil;
  884. end;
  885. function TObjData.symbolref(const aname:string):TObjSymbol;
  886. begin
  887. if not assigned(CurrObjSec) then
  888. internalerror(200603052);
  889. result:=CreateSymbol(aname);
  890. end;
  891. procedure TObjData.ResetCachedAsmSymbols;
  892. var
  893. i : longint;
  894. begin
  895. for i:=0 to FCachedAsmSymbolList.Count-1 do
  896. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  897. FCachedAsmSymbolList.Clear;
  898. end;
  899. procedure TObjData.writebytes(const Data;len:aint);
  900. begin
  901. if not assigned(CurrObjSec) then
  902. internalerror(200402251);
  903. CurrObjSec.write(Data,len);
  904. end;
  905. procedure TObjData.alloc(len:aint);
  906. begin
  907. if not assigned(CurrObjSec) then
  908. internalerror(200402252);
  909. CurrObjSec.alloc(len);
  910. end;
  911. procedure TObjData.allocalign(len:shortint);
  912. begin
  913. if not assigned(CurrObjSec) then
  914. internalerror(200402253);
  915. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  916. end;
  917. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  918. begin
  919. with TObjSection(p) do
  920. alloc(align(size,secalign)-size);
  921. end;
  922. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  923. begin
  924. with TObjSection(p) do
  925. begin
  926. if assigned(Data) then
  927. writezeros(align(size,secalign)-size);
  928. end;
  929. end;
  930. procedure TObjData.section_reset(p:TObject;arg:pointer);
  931. begin
  932. with TObjSection(p) do
  933. begin
  934. Size:=0;
  935. Datapos:=0;
  936. mempos:=0;
  937. end;
  938. end;
  939. procedure TObjData.beforealloc;
  940. begin
  941. { create stabs sections if debugging }
  942. if assigned(StabsSec) then
  943. begin
  944. StabsSec.Alloc(sizeof(TObjStabEntry));
  945. StabStrSec.Alloc(1);
  946. end;
  947. end;
  948. procedure TObjData.beforewrite;
  949. var
  950. s : string[1];
  951. hstab : TObjStabEntry;
  952. begin
  953. { create stabs sections if debugging }
  954. if assigned(StabsSec) then
  955. begin
  956. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  957. fillchar(hstab,sizeof(hstab),0);
  958. StabsSec.Write(hstab,sizeof(hstab));
  959. { start of stabstr }
  960. s:=#0;
  961. StabStrSec.write(s[1],length(s));
  962. end;
  963. end;
  964. procedure TObjData.afteralloc;
  965. begin
  966. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  967. end;
  968. procedure TObjData.afterwrite;
  969. var
  970. s : string[1];
  971. hstab : TObjStabEntry;
  972. begin
  973. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  974. { For the stab section we need an HdrSym which can now be
  975. calculated more easily }
  976. if assigned(StabsSec) then
  977. begin
  978. { end of stabstr }
  979. s:=#0;
  980. StabStrSec.write(s[1],length(s));
  981. { header stab }
  982. hstab.strpos:=1;
  983. hstab.ntype:=0;
  984. hstab.nother:=0;
  985. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  986. hstab.nvalue:=StabStrSec.Size;
  987. StabsSec.Data.seek(0);
  988. StabsSec.Data.write(hstab,sizeof(hstab));
  989. end;
  990. end;
  991. procedure TObjData.resetsections;
  992. begin
  993. FObjSectionList.ForEachCall(@section_reset,nil);
  994. end;
  995. {****************************************************************************
  996. TObjOutput
  997. ****************************************************************************}
  998. constructor TObjOutput.create(AWriter:TObjectWriter);
  999. begin
  1000. FWriter:=AWriter;
  1001. CObjData:=TObjData;
  1002. end;
  1003. destructor TObjOutput.destroy;
  1004. begin
  1005. inherited destroy;
  1006. end;
  1007. function TObjOutput.newObjData(const n:string):TObjData;
  1008. begin
  1009. result:=CObjData.create(n);
  1010. if (cs_use_lineinfo in current_settings.globalswitches) or
  1011. (cs_debuginfo in current_settings.moduleswitches) then
  1012. result.CreateDebugSections;
  1013. end;
  1014. function TObjOutput.startObjectfile(const fn:string):boolean;
  1015. begin
  1016. result:=false;
  1017. { start the writer already, so the .a generation can initialize
  1018. the position of the current objectfile }
  1019. if not FWriter.createfile(fn) then
  1020. Comment(V_Fatal,'Can''t create object '+fn);
  1021. result:=true;
  1022. end;
  1023. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1024. begin
  1025. if errorcount=0 then
  1026. result:=writeData(Data)
  1027. else
  1028. result:=true;
  1029. { close the writer }
  1030. FWriter.closefile;
  1031. end;
  1032. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1033. begin
  1034. { export globals and common symbols, this is needed
  1035. for .a files }
  1036. if p.bind in [AB_GLOBAL,AB_COMMON] then
  1037. FWriter.writesym(p.name);
  1038. end;
  1039. {****************************************************************************
  1040. TExeVTable
  1041. ****************************************************************************}
  1042. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1043. begin
  1044. ExeSymbol:=AExeSymbol;
  1045. if ExeSymbol.State=symstate_undefined then
  1046. internalerror(200604012);
  1047. ChildList:=TFPObjectList.Create(false);
  1048. end;
  1049. destructor TExeVTable.Destroy;
  1050. begin
  1051. ChildList.Free;
  1052. if assigned(EntryArray) then
  1053. Freemem(EntryArray);
  1054. end;
  1055. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1056. var
  1057. OldEntryCnt : longint;
  1058. begin
  1059. if VTableIdx>=EntryCnt then
  1060. begin
  1061. OldEntryCnt:=EntryCnt;
  1062. EntryCnt:=VTableIdx+1;
  1063. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1064. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1065. end;
  1066. end;
  1067. procedure TExeVTable.AddChild(vt:TExeVTable);
  1068. begin
  1069. ChildList.Add(vt);
  1070. end;
  1071. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1072. var
  1073. i : longint;
  1074. objreloc : TObjRelocation;
  1075. vtblentryoffset : aint;
  1076. begin
  1077. CheckIdx(VTableIdx);
  1078. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+VTableIdx*sizeof(aint);
  1079. { Find and disable relocation }
  1080. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1081. begin
  1082. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1083. if objreloc.dataoffset=vtblentryoffset then
  1084. begin
  1085. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1086. EntryArray[VTableIdx].OrgRelocType:=objreloc.typ;
  1087. objreloc.typ:=RELOC_ZERO;
  1088. break;
  1089. end;
  1090. end;
  1091. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1092. internalerror(200604011);
  1093. end;
  1094. procedure TExeVTable.SetVTableSize(ASize:longint);
  1095. begin
  1096. if EntryCnt<>0 then
  1097. internalerror(200603313);
  1098. EntryCnt:=ASize div sizeof(aint);
  1099. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1100. end;
  1101. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1102. begin
  1103. result:=nil;
  1104. CheckIdx(VTableIdx);
  1105. if EntryArray[VTableIdx].Used then
  1106. exit;
  1107. { Restore relocation if available }
  1108. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1109. begin
  1110. EntryArray[VTableIdx].ObjRelocation.typ:=EntryArray[VTableIdx].OrgRelocType;
  1111. result:=EntryArray[VTableIdx].ObjRelocation;
  1112. end;
  1113. EntryArray[VTableIdx].Used:=true;
  1114. end;
  1115. {****************************************************************************
  1116. TExeSection
  1117. ****************************************************************************}
  1118. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1119. begin
  1120. inherited create(AList,AName);
  1121. Size:=0;
  1122. MemPos:=0;
  1123. DataPos:=0;
  1124. FSecSymIdx:=0;
  1125. FObjSectionList:=TFPObjectList.Create(false);
  1126. end;
  1127. destructor TExeSection.destroy;
  1128. begin
  1129. ObjSectionList.Free;
  1130. inherited destroy;
  1131. end;
  1132. procedure TExeSection.AddObjSection(objsec:TObjSection);
  1133. begin
  1134. ObjSectionList.Add(objsec);
  1135. if (SecOptions<>[]) then
  1136. begin
  1137. { Only if the section contains (un)initialized data the
  1138. data flag must match. This check is not needed if the
  1139. section is empty for a symbol allocation }
  1140. if (objsec.size>0) and
  1141. ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1142. Comment(V_Error,'Incompatible section options');
  1143. end
  1144. else
  1145. begin
  1146. { inherit section options }
  1147. SecAlign:=objsec.SecAlign;
  1148. SecOptions:=SecOptions+objsec.SecOptions;
  1149. end;
  1150. { relate ObjSection to ExeSection, and mark it Used by default }
  1151. objsec.ExeSection:=self;
  1152. objsec.Used:=true;
  1153. end;
  1154. {****************************************************************************
  1155. TStaticLibrary
  1156. ****************************************************************************}
  1157. constructor TStaticLibrary.create(AList:TFPHashObjectList;const AName:string;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1158. begin
  1159. inherited create(AList,AName);
  1160. FArReader:=AReader;
  1161. FObjInputClass:=AObjInputClass;
  1162. end;
  1163. destructor TStaticLibrary.destroy;
  1164. begin
  1165. ArReader.Free;
  1166. inherited destroy;
  1167. end;
  1168. {****************************************************************************
  1169. TImportLibrary
  1170. ****************************************************************************}
  1171. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1172. begin
  1173. inherited create(AList,AName);
  1174. FImportSymbolList:=TFPHashObjectList.Create(true);
  1175. end;
  1176. destructor TImportLibrary.destroy;
  1177. begin
  1178. ImportSymbolList.Free;
  1179. inherited destroy;
  1180. end;
  1181. {****************************************************************************
  1182. TImportSymbol
  1183. ****************************************************************************}
  1184. constructor TImportSymbol.create(AList:TFPHashObjectList;const AName:string;AOrdNr:longint;AIsVar:boolean);
  1185. begin
  1186. inherited Create(AList, AName);
  1187. FOrdNr:=AOrdNr;
  1188. FIsVar:=AIsVar;
  1189. FMangledName:=AName;
  1190. { Replace ? and @ in import name }
  1191. Replace(FMangledName,'?','__q$$');
  1192. Replace(FMangledName,'@','__a$$');
  1193. end;
  1194. {****************************************************************************
  1195. TExeOutput
  1196. ****************************************************************************}
  1197. constructor TExeOutput.create;
  1198. begin
  1199. { init writer }
  1200. FWriter:=TObjectwriter.create;
  1201. { object files }
  1202. FObjDataList:=TFPObjectList.Create(true);
  1203. { symbols }
  1204. FExeSymbolList:=TFPHashObjectList.Create(true);
  1205. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1206. FExternalObjSymbols:=TFPObjectList.Create(false);
  1207. FCommonObjSymbols:=TFPObjectList.Create(false);
  1208. FExeVTableList:=TFPObjectList.Create(false);
  1209. FEntryName:='start';
  1210. { sections }
  1211. FExeSectionList:=TFPHashObjectList.Create(true);
  1212. FDebugSectionList:=TFPHashObjectList.Create(true);
  1213. FImageBase:=0;
  1214. SectionMemAlign:=$1000;
  1215. SectionDataAlign:=$200;
  1216. FCExeSection:=TExeSection;
  1217. FCObjData:=TObjData;
  1218. end;
  1219. destructor TExeOutput.destroy;
  1220. begin
  1221. FExeSymbolList.free;
  1222. UnresolvedExeSymbols.free;
  1223. ExternalObjSymbols.free;
  1224. CommonObjSymbols.free;
  1225. ExeVTableList.free;
  1226. FExeSectionList.free;
  1227. FDebugSectionList.free;
  1228. ObjDatalist.free;
  1229. FWriter.free;
  1230. inherited destroy;
  1231. end;
  1232. function TExeOutput.writedbgfile(const fn:string):boolean;
  1233. var
  1234. ObjWriter : TObjectwriter;
  1235. exesec : TExeSection;
  1236. objsec : TObjSection;
  1237. header : string[6];
  1238. s : string;
  1239. i,j,
  1240. seccnt,
  1241. secstart : longint;
  1242. begin
  1243. result:=false;
  1244. {
  1245. File layout:
  1246. <header> 6 bytes
  1247. <section count> 4 bytes
  1248. (
  1249. <section offset> 4 bytes
  1250. <section name> asciiz
  1251. ...
  1252. )
  1253. (
  1254. <section data>
  1255. ...
  1256. )
  1257. }
  1258. header:='FPCDBG';
  1259. { Calculate file offsets }
  1260. secstart:=length(header)+sizeof(seccnt);
  1261. for i:=0 to DebugSectionList.Count-1 do
  1262. begin
  1263. exesec:=TExeSection(DebugSectionList[i]);
  1264. inc(secstart,sizeof(secstart)+length(exesec.Name)+1);
  1265. end;
  1266. for i:=0 to DebugSectionList.Count-1 do
  1267. begin
  1268. exesec:=TExeSection(DebugSectionList[i]);
  1269. exesec.DataPos:=secstart;
  1270. for j:=0 to exesec.ObjSectionList.Count-1 do
  1271. begin
  1272. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1273. inc(secstart,objsec.size);
  1274. end;
  1275. end;
  1276. { Create file }
  1277. ObjWriter:=TObjectwriter.Create;
  1278. if not ObjWriter.createfile(fn) then
  1279. begin
  1280. ObjWriter.Free;
  1281. exit;
  1282. end;
  1283. { Header }
  1284. ObjWriter.Write(header[1], Length(header));
  1285. seccnt:=DebugSectionList.Count;
  1286. ObjWriter.Write(seccnt,sizeof(seccnt));
  1287. for i:=0 to DebugSectionList.Count-1 do
  1288. begin
  1289. exesec:=TExeSection(DebugSectionList[i]);
  1290. ObjWriter.Write(exesec.DataPos,sizeof(secstart));
  1291. s:=exesec.Name;
  1292. ObjWriter.Write(s[1],length(s));
  1293. ObjWriter.writezeros(1);
  1294. end;
  1295. { Write section data }
  1296. for i:=0 to DebugSectionList.Count-1 do
  1297. begin
  1298. exesec:=TExeSection(DebugSectionList[i]);
  1299. if exesec.DataPos<>ObjWriter.ObjSize then
  1300. internalerror(200702241);
  1301. for j:=0 to exesec.ObjSectionList.Count-1 do
  1302. begin
  1303. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1304. ObjWriter.writearray(objsec.data);
  1305. end;
  1306. end;
  1307. ObjWriter.Free;
  1308. result:=true;
  1309. end;
  1310. function TExeOutput.writeexefile(const fn:string):boolean;
  1311. begin
  1312. result:=false;
  1313. if FWriter.createfile(fn) then
  1314. begin
  1315. { Only write the .o if there are no errors }
  1316. if errorcount=0 then
  1317. result:=writeData
  1318. else
  1319. result:=true;
  1320. { close the writer }
  1321. FWriter.closefile;
  1322. end
  1323. else
  1324. Comment(V_Fatal,'Can''t create executable '+fn);
  1325. end;
  1326. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1327. begin
  1328. result:=TExeSection(ExeSectionList.Find(aname));
  1329. end;
  1330. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1331. begin
  1332. if ObjData.classtype<>FCObjData then
  1333. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1334. ObjDataList.Add(ObjData);
  1335. end;
  1336. procedure TExeOutput.Load_Start;
  1337. begin
  1338. ObjDataList.Clear;
  1339. { Globals defined in the linker script }
  1340. if not assigned(internalObjData) then
  1341. internalObjData:=CObjData.create('*Internal*');
  1342. AddObjData(internalObjData);
  1343. { Common Data section }
  1344. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1345. end;
  1346. procedure TExeOutput.Load_EntryName(const aname:string);
  1347. begin
  1348. EntryName:=aname;
  1349. end;
  1350. procedure TExeOutput.Load_IsSharedLibrary;
  1351. begin
  1352. IsSharedLibrary:=true;
  1353. end;
  1354. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1355. var
  1356. code : integer;
  1357. objsec : TObjSection;
  1358. objsym : TObjSymbol;
  1359. exesym : TExeSymbol;
  1360. begin
  1361. val(avalue,ImageBase,code);
  1362. if code<>0 then
  1363. Comment(V_Error,'Invalid number '+avalue);
  1364. { Create __image_base__ symbol, create the symbol
  1365. in a section with adress 0 and at offset 0 }
  1366. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1367. internalObjData.setsection(objsec);
  1368. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_FUNCTION);
  1369. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1370. exesym.ObjSymbol:=objsym;
  1371. end;
  1372. procedure TExeOutput.Load_Symbol(const aname:string);
  1373. begin
  1374. internalObjData.createsection('*'+aname,0,[]);
  1375. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION);
  1376. end;
  1377. procedure TExeOutput.Order_Start;
  1378. begin
  1379. end;
  1380. procedure TExeOutput.Order_End;
  1381. begin
  1382. internalObjData.afterwrite;
  1383. end;
  1384. procedure TExeOutput.Order_ExeSection(const aname:string);
  1385. var
  1386. sec : TExeSection;
  1387. begin
  1388. sec:=FindExeSection(aname);
  1389. if not assigned(sec) then
  1390. sec:=CExeSection.create(ExeSectionList,aname);
  1391. { Clear ExeSection contents }
  1392. FCurrExeSec:=sec;
  1393. end;
  1394. procedure TExeOutput.Order_EndExeSection;
  1395. begin
  1396. if not assigned(CurrExeSec) then
  1397. internalerror(200602184);
  1398. FCurrExeSec:=nil;
  1399. end;
  1400. procedure TExeOutput.Order_ObjSection(const aname:string);
  1401. var
  1402. i,j : longint;
  1403. ObjData : TObjData;
  1404. objsec : TObjSection;
  1405. TmpObjSectionList : TFPObjectList;
  1406. begin
  1407. if not assigned(CurrExeSec) then
  1408. internalerror(200602181);
  1409. TmpObjSectionList:=TFPObjectList.Create(false);
  1410. for i:=0 to ObjDataList.Count-1 do
  1411. begin
  1412. ObjData:=TObjData(ObjDataList[i]);
  1413. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1414. begin
  1415. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1416. if (not objsec.Used) and
  1417. MatchPattern(aname,objsec.name) then
  1418. TmpObjSectionList.Add(objsec);
  1419. end;
  1420. end;
  1421. { Order list if needed }
  1422. Order_ObjSectionList(TmpObjSectionList);
  1423. { Add the (ordered) list to the current ExeSection }
  1424. for i:=0 to TmpObjSectionList.Count-1 do
  1425. begin
  1426. objsec:=TObjSection(TmpObjSectionList[i]);
  1427. CurrExeSec.AddObjSection(objsec);
  1428. end;
  1429. TmpObjSectionList.Free;
  1430. end;
  1431. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList);
  1432. begin
  1433. end;
  1434. procedure TExeOutput.Order_Symbol(const aname:string);
  1435. var
  1436. ObjSection : TObjSection;
  1437. begin
  1438. ObjSection:=internalObjData.findsection('*'+aname);
  1439. if not assigned(ObjSection) then
  1440. internalerror(200603041);
  1441. CurrExeSec.AddObjSection(ObjSection);
  1442. end;
  1443. procedure TExeOutput.Order_Align(const avalue:string);
  1444. var
  1445. code : integer;
  1446. alignval : shortint;
  1447. objsec : TObjSection;
  1448. begin
  1449. val(avalue,alignval,code);
  1450. if code<>0 then
  1451. Comment(V_Error,'Invalid number '+avalue);
  1452. if alignval<=0 then
  1453. exit;
  1454. { Create an empty section with the required aligning }
  1455. inc(Fzeronr);
  1456. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1457. CurrExeSec.AddObjSection(objsec);
  1458. end;
  1459. procedure TExeOutput.Order_Zeros(const avalue:string);
  1460. var
  1461. zeros : array[0..1023] of byte;
  1462. code : integer;
  1463. len : longint;
  1464. objsec : TObjSection;
  1465. begin
  1466. val(avalue,len,code);
  1467. if code<>0 then
  1468. Comment(V_Error,'Invalid number '+avalue);
  1469. if len<=0 then
  1470. exit;
  1471. if len>sizeof(zeros) then
  1472. internalerror(200602254);
  1473. fillchar(zeros,len,0);
  1474. inc(Fzeronr);
  1475. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1476. internalObjData.writebytes(zeros,len);
  1477. CurrExeSec.AddObjSection(objsec);
  1478. end;
  1479. procedure TExeOutput.CalcPos_ExeSection(const aname:string);
  1480. var
  1481. i : longint;
  1482. objsec : TObjSection;
  1483. begin
  1484. { Section can be removed }
  1485. FCurrExeSec:=FindExeSection(aname);
  1486. if not assigned(CurrExeSec) then
  1487. exit;
  1488. { Alignment of ExeSection }
  1489. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1490. CurrExeSec.MemPos:=CurrMemPos;
  1491. if (oso_Data in currexesec.SecOptions) then
  1492. begin
  1493. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1494. CurrExeSec.DataPos:=CurrDataPos;
  1495. end;
  1496. { set position of object ObjSections }
  1497. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1498. begin
  1499. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1500. { Position in memory }
  1501. objsec.setmempos(CurrMemPos);
  1502. { Position in File }
  1503. if (oso_Data in objsec.SecOptions) then
  1504. begin
  1505. if not (oso_Data in currexesec.SecOptions) then
  1506. internalerror(200603043);
  1507. if not assigned(objsec.Data) then
  1508. internalerror(200603044);
  1509. objsec.setDatapos(CurrDataPos);
  1510. end;
  1511. end;
  1512. { calculate size of the section }
  1513. CurrExeSec.Size:=CurrMemPos-CurrExeSec.MemPos;
  1514. end;
  1515. procedure TExeOutput.CalcPos_EndExeSection;
  1516. begin
  1517. if not assigned(CurrExeSec) then
  1518. exit;
  1519. FCurrExeSec:=nil;
  1520. end;
  1521. procedure TExeOutput.CalcPos_Start;
  1522. begin
  1523. CurrMemPos:=0;
  1524. CurrDataPos:=0;
  1525. end;
  1526. procedure TExeOutput.CalcPos_Header;
  1527. begin
  1528. end;
  1529. procedure TExeOutput.CalcPos_Symbols;
  1530. var
  1531. i : longint;
  1532. sym : TExeSymbol;
  1533. begin
  1534. { Removing unused symbols }
  1535. for i:=0 to ExeSymbolList.Count-1 do
  1536. begin
  1537. sym:=TExeSymbol(ExeSymbolList[i]);
  1538. if not sym.ObjSymbol.objsection.Used then
  1539. ExeSymbolList[i]:=nil;
  1540. end;
  1541. ExeSymbolList.Pack;
  1542. end;
  1543. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  1544. var
  1545. hs : string;
  1546. code : integer;
  1547. i,k,
  1548. vtableidx : longint;
  1549. vtableexesym,
  1550. childexesym,
  1551. parentexesym : TExeSymbol;
  1552. objsym : TObjSymbol;
  1553. begin
  1554. { Build inheritance tree from VTINHERIT }
  1555. for i:=0 to VTInheritList.Count-1 do
  1556. begin
  1557. objsym:=TObjSymbol(VTInheritList[i]);
  1558. hs:=objsym.name;
  1559. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  1560. Delete(hs,1,Pos('_',hs));
  1561. k:=Pos('$$',hs);
  1562. if k=0 then
  1563. internalerror(200603311);
  1564. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1565. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  1566. if not assigned(childexesym) or
  1567. not assigned(parentexesym)then
  1568. internalerror(200603312);
  1569. if not assigned(childexesym.vtable) then
  1570. begin
  1571. childexesym.vtable:=TExeVTable.Create(childexesym);
  1572. ExeVTableList.Add(childexesym.vtable);
  1573. end;
  1574. if not assigned(parentexesym.vtable) then
  1575. begin
  1576. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  1577. ExeVTableList.Add(parentexesym.vtable);
  1578. end;
  1579. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  1580. if parentexesym<>childexesym then
  1581. parentexesym.vtable.AddChild(childexesym.vtable);
  1582. end;
  1583. { Find VTable entries from VTENTRY }
  1584. for i:=0 to VTEntryList.Count-1 do
  1585. begin
  1586. objsym:=TObjSymbol(VTEntryList[i]);
  1587. hs:=objsym.name;
  1588. { VTENTRY_<VTableName>$$<Index> }
  1589. Delete(hs,1,Pos('_',hs));
  1590. k:=Pos('$$',hs);
  1591. if k=0 then
  1592. internalerror(200603319);
  1593. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1594. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  1595. if (code<>0) then
  1596. internalerror(200603318);
  1597. if not assigned(vtableexesym) then
  1598. internalerror(2006033110);
  1599. vtableexesym.vtable.AddEntry(vtableidx);
  1600. end;
  1601. end;
  1602. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  1603. var
  1604. i : longint;
  1605. exesym : TExeSymbol;
  1606. begin
  1607. { Generate a list of Unresolved External symbols }
  1608. for i:=0 to UnresolvedExeSymbols.count-1 do
  1609. begin
  1610. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1611. if exesym.State<>symstate_undefined then
  1612. UnresolvedExeSymbols[i]:=nil;
  1613. end;
  1614. UnresolvedExeSymbols.Pack;
  1615. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  1616. end;
  1617. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPHashObjectList);
  1618. var
  1619. ObjData : TObjData;
  1620. exesym : TExeSymbol;
  1621. objsym,
  1622. commonsym : TObjSymbol;
  1623. objinput : TObjInput;
  1624. StaticLibrary : TStaticLibrary;
  1625. firstarchive,
  1626. firstcommon : boolean;
  1627. i,j : longint;
  1628. VTEntryList,
  1629. VTInheritList : TFPObjectList;
  1630. procedure LoadObjDataSymbols(ObjData:TObjData);
  1631. var
  1632. j : longint;
  1633. hs : string;
  1634. exesym : TExeSymbol;
  1635. objsym : TObjSymbol;
  1636. begin
  1637. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  1638. begin
  1639. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  1640. { From the local symbols we are only interressed in the
  1641. VTENTRY and VTINHERIT symbols }
  1642. if objsym.bind=AB_LOCAL then
  1643. begin
  1644. if cs_link_opt_vtable in current_settings.globalswitches then
  1645. begin
  1646. hs:=objsym.name;
  1647. if (hs[1]='V') then
  1648. begin
  1649. if Copy(hs,1,5)='VTREF' then
  1650. begin
  1651. if not assigned(objsym.ObjSection.VTRefList) then
  1652. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  1653. objsym.ObjSection.VTRefList.Add(objsym);
  1654. end
  1655. else if Copy(hs,1,7)='VTENTRY' then
  1656. VTEntryList.Add(objsym)
  1657. else if Copy(hs,1,9)='VTINHERIT' then
  1658. VTInheritList.Add(objsym);
  1659. end;
  1660. end;
  1661. continue;
  1662. end;
  1663. { Search for existing exesymbol }
  1664. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  1665. if not assigned(exesym) then
  1666. begin
  1667. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1668. exesym.ObjSymbol:=objsym;
  1669. end;
  1670. objsym.ExeSymbol:=exesym;
  1671. case objsym.bind of
  1672. AB_GLOBAL :
  1673. begin
  1674. if exesym.State<>symstate_defined then
  1675. begin
  1676. exesym.ObjSymbol:=objsym;
  1677. exesym.State:=symstate_defined;
  1678. end
  1679. else
  1680. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  1681. end;
  1682. AB_EXTERNAL :
  1683. begin
  1684. ExternalObjSymbols.add(objsym);
  1685. { Register unresolved symbols only the first time they
  1686. are registered }
  1687. if exesym.ObjSymbol=objsym then
  1688. UnresolvedExeSymbols.Add(exesym);
  1689. end;
  1690. AB_COMMON :
  1691. begin
  1692. if exesym.State=symstate_undefined then
  1693. begin
  1694. exesym.ObjSymbol:=objsym;
  1695. exesym.State:=symstate_common;
  1696. end;
  1697. CommonObjSymbols.add(objsym);
  1698. end;
  1699. end;
  1700. end;
  1701. end;
  1702. begin
  1703. VTEntryList:=TFPObjectList.Create(false);
  1704. VTInheritList:=TFPObjectList.Create(false);
  1705. {
  1706. The symbol resolving is done in 3 steps:
  1707. 1. Register symbols from objects
  1708. 2. Find symbols in static libraries
  1709. 3. Define stil undefined common symbols
  1710. }
  1711. { Step 1, Register symbols from objects }
  1712. for i:=0 to ObjDataList.Count-1 do
  1713. begin
  1714. ObjData:=TObjData(ObjDataList[i]);
  1715. LoadObjDataSymbols(ObjData);
  1716. end;
  1717. PackUnresolvedExeSymbols('in objects');
  1718. { Step 2, Find unresolved symbols in the libraries }
  1719. firstarchive:=true;
  1720. for i:=0 to StaticLibraryList.Count-1 do
  1721. begin
  1722. StaticLibrary:=TStaticLibrary(StaticLibraryList[i]);
  1723. { Process list of Unresolved External symbols, we need
  1724. to use a while loop because the list can be extended when
  1725. we load members from the library. }
  1726. j:=0;
  1727. while (j<UnresolvedExeSymbols.count) do
  1728. begin
  1729. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  1730. { Check first if the symbol is still undefined }
  1731. if exesym.State=symstate_undefined then
  1732. begin
  1733. if StaticLibrary.ArReader.OpenFile(exesym.name) then
  1734. begin
  1735. if assigned(exemap) then
  1736. begin
  1737. if firstarchive then
  1738. begin
  1739. exemap.Add('');
  1740. exemap.Add('Archive member included because of file (symbol)');
  1741. exemap.Add('');
  1742. firstarchive:=false;
  1743. end;
  1744. exemap.Add(StaticLibrary.ArReader.FileName+' - '+
  1745. {exesym.ObjSymbol.ObjSection.FullName+}
  1746. '('+exesym.Name+')');
  1747. end;
  1748. objinput:=StaticLibrary.ObjInputClass.Create;
  1749. objdata:=objinput.newObjData(StaticLibrary.ArReader.FileName);
  1750. objinput.ReadObjData(StaticLibrary.ArReader,objdata);
  1751. objinput.free;
  1752. AddObjData(objdata);
  1753. LoadObjDataSymbols(objdata);
  1754. StaticLibrary.ArReader.CloseFile;
  1755. end;
  1756. end;
  1757. inc(j);
  1758. end;
  1759. end;
  1760. PackUnresolvedExeSymbols('after static libraries');
  1761. { Step 3, Match common symbols or add to the globals }
  1762. firstcommon:=true;
  1763. for i:=0 to CommonObjSymbols.count-1 do
  1764. begin
  1765. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1766. if objsym.exesymbol.State=symstate_defined then
  1767. begin
  1768. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  1769. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  1770. end
  1771. else
  1772. begin
  1773. { allocate new objsymbol in .bss of *COMMON* and assign
  1774. it to the exesymbol }
  1775. if firstcommon then
  1776. begin
  1777. if assigned(exemap) then
  1778. exemap.AddCommonSymbolsHeader;
  1779. firstcommon:=false;
  1780. end;
  1781. internalObjData.setsection(commonObjSection);
  1782. internalObjData.allocalign(var_align(objsym.size));
  1783. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_FUNCTION);
  1784. commonsym.size:=objsym.size;
  1785. internalObjData.alloc(objsym.size);
  1786. if assigned(exemap) then
  1787. exemap.AddCommonSymbol(commonsym);
  1788. { Assign to the exesymbol }
  1789. objsym.exesymbol.objsymbol:=commonsym;
  1790. objsym.exesymbol.state:=symstate_defined;
  1791. end;
  1792. end;
  1793. PackUnresolvedExeSymbols('after defining COMMON symbols');
  1794. { Find entry symbol and print in map }
  1795. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  1796. if assigned(exesym) then
  1797. begin
  1798. EntrySym:=exesym.ObjSymbol;
  1799. if assigned(exemap) then
  1800. begin
  1801. exemap.Add('');
  1802. exemap.Add('Entry symbol '+EntryName);
  1803. end;
  1804. end
  1805. else
  1806. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  1807. { Generate VTable tree }
  1808. if cs_link_opt_vtable in current_settings.globalswitches then
  1809. BuildVTableTree(VTInheritList,VTEntryList);
  1810. VTInheritList.Free;
  1811. VTEntryList.Free;
  1812. end;
  1813. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  1814. begin
  1815. end;
  1816. procedure TExeOutput.PrintMemoryMap;
  1817. var
  1818. exesec : TExeSection;
  1819. objsec : TObjSection;
  1820. objsym : TObjSymbol;
  1821. i,j,k : longint;
  1822. begin
  1823. if not assigned(exemap) then
  1824. exit;
  1825. exemap.AddMemoryMapHeader(ImageBase);
  1826. for i:=0 to ExeSectionList.Count-1 do
  1827. begin
  1828. exesec:=TExeSection(ExeSectionList[i]);
  1829. exemap.AddMemoryMapExeSection(exesec);
  1830. for j:=0 to exesec.ObjSectionList.count-1 do
  1831. begin
  1832. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1833. exemap.AddMemoryMapObjectSection(objsec);
  1834. for k:=0 to objsec.ObjSymbolDefines.Count-1 do
  1835. begin
  1836. objsym:=TObjSymbol(objsec.ObjSymbolDefines[k]);
  1837. exemap.AddMemoryMapSymbol(objsym);
  1838. end;
  1839. end;
  1840. end;
  1841. end;
  1842. procedure TExeOutput.FixupSymbols;
  1843. procedure UpdateSymbol(objsym:TObjSymbol);
  1844. begin
  1845. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  1846. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  1847. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  1848. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  1849. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  1850. end;
  1851. var
  1852. i : longint;
  1853. objsym : TObjSymbol;
  1854. exesym : TExeSymbol;
  1855. begin
  1856. { Print list of Unresolved External symbols }
  1857. for i:=0 to UnresolvedExeSymbols.count-1 do
  1858. begin
  1859. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1860. if exesym.State<>symstate_defined then
  1861. Comment(V_Error,'Undefined symbol: '+exesym.name);
  1862. end;
  1863. { Update ImageBase to ObjData so it can access from ObjSymbols }
  1864. for i:=0 to ObjDataList.Count-1 do
  1865. TObjData(ObjDataList[i]).imagebase:=imagebase;
  1866. {
  1867. Fixing up symbols is done in the following steps:
  1868. 1. Update common references
  1869. 2. Update external references
  1870. }
  1871. { Step 1, Update commons }
  1872. for i:=0 to CommonObjSymbols.count-1 do
  1873. begin
  1874. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1875. if objsym.bind<>AB_COMMON then
  1876. internalerror(200606241);
  1877. UpdateSymbol(objsym);
  1878. end;
  1879. { Step 2, Update externals }
  1880. for i:=0 to ExternalObjSymbols.count-1 do
  1881. begin
  1882. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  1883. if objsym.bind<>AB_EXTERNAL then
  1884. internalerror(200606242);
  1885. UpdateSymbol(objsym);
  1886. end;
  1887. end;
  1888. procedure TExeOutput.MergeStabs;
  1889. var
  1890. stabexesec,
  1891. stabstrexesec : TExeSection;
  1892. relocsec,
  1893. currstabsec,
  1894. currstabstrsec,
  1895. mergedstabsec,
  1896. mergedstabstrsec : TObjSection;
  1897. hstabreloc,
  1898. currstabreloc : TObjRelocation;
  1899. currstabrelocidx,
  1900. i,j,
  1901. mergestabcnt,
  1902. stabcnt : longint;
  1903. skipstab : boolean;
  1904. skipfun : boolean;
  1905. hstab : TObjStabEntry;
  1906. stabrelocofs : longint;
  1907. buf : array[0..1023] of byte;
  1908. bufend,
  1909. bufsize : longint;
  1910. begin
  1911. stabexesec:=FindExeSection('.stab');
  1912. stabstrexesec:=FindExeSection('.stabstr');
  1913. if (stabexesec=nil) or
  1914. (stabstrexesec=nil) or
  1915. (stabexesec.ObjSectionlist.count=0) then
  1916. exit;
  1917. { Create new stabsection }
  1918. stabRelocofs:=pointer(@hstab.nvalue)-@hstab;
  1919. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  1920. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  1921. { write stab for hdrsym }
  1922. fillchar(hstab,sizeof(TObjStabEntry),0);
  1923. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  1924. mergestabcnt:=1;
  1925. { .stabstr starts with a #0 }
  1926. buf[0]:=0;
  1927. mergedstabstrsec.write(buf[0],1);
  1928. skipfun:=false;
  1929. { Copy stabs and corresponding Relocations }
  1930. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  1931. begin
  1932. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  1933. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  1934. if assigned(currstabstrsec) then
  1935. begin
  1936. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  1937. currstabsec.Data.seek(0);
  1938. currstabrelocidx:=0;
  1939. for j:=0 to stabcnt-1 do
  1940. begin
  1941. hstabreloc:=nil;
  1942. skipstab:=false;
  1943. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  1944. { Only include first hdrsym stab }
  1945. if hstab.ntype=0 then
  1946. skipstab:=true;
  1947. if skipfun then
  1948. begin
  1949. skipstab:=hstab.ntype in [N_TextLine,N_RSYM,N_LSYM,N_tsym,N_LBRAC,N_RBRAC,N_IncludeFile];
  1950. skipfun:=skipstab;
  1951. end;
  1952. if not skipstab then
  1953. begin
  1954. { Find corresponding Relocation }
  1955. currstabreloc:=nil;
  1956. while (currstabrelocidx<currstabsec.ObjRelocations.Count) do
  1957. begin
  1958. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  1959. if assigned(currstabreloc) and
  1960. (currstabreloc.dataoffset>=j*sizeof(TObjStabEntry)+stabrelocofs) then
  1961. break;
  1962. inc(currstabrelocidx);
  1963. end;
  1964. if assigned(currstabreloc) and
  1965. (currstabreloc.dataoffset=j*sizeof(TObjStabEntry)+stabrelocofs) then
  1966. begin
  1967. hstabReloc:=currstabReloc;
  1968. inc(currstabrelocidx);
  1969. end;
  1970. { Check if the stab is refering to a removed section }
  1971. if assigned(hstabreloc) then
  1972. begin
  1973. if assigned(hstabreloc.Symbol) then
  1974. relocsec:=hstabreloc.Symbol.ObjSection
  1975. else
  1976. relocsec:=hstabreloc.ObjSection;
  1977. if not assigned(relocsec) then
  1978. internalerror(200603302);
  1979. if not relocsec.Used then
  1980. begin
  1981. skipstab:=true;
  1982. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  1983. begin
  1984. currstabstrsec.Data.seek(hstab.strpos);
  1985. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  1986. bufend:=indexbyte(buf,bufsize,Ord(':'));
  1987. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  1988. skipfun:=true;
  1989. end;
  1990. end;
  1991. end;
  1992. end;
  1993. if not skipstab then
  1994. begin
  1995. { Copy string in stabstr }
  1996. if hstab.strpos<>0 then
  1997. begin
  1998. currstabstrsec.Data.seek(hstab.strpos);
  1999. hstab.strpos:=mergedstabstrsec.Size;
  2000. repeat
  2001. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2002. bufend:=indexbyte(buf,bufsize,0);
  2003. if bufend=-1 then
  2004. bufend:=bufsize
  2005. else
  2006. begin
  2007. { include the #0 }
  2008. inc(bufend);
  2009. end;
  2010. mergedstabstrsec.write(buf,bufend);
  2011. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2012. end;
  2013. { Copy and Update the relocation }
  2014. if assigned(hstabreloc) then
  2015. begin
  2016. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2017. { Remove from List without freeing the object }
  2018. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2019. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2020. end;
  2021. { Write updated stab }
  2022. mergedstabsec.write(hstab,sizeof(hstab));
  2023. inc(mergestabcnt);
  2024. end;
  2025. end;
  2026. end;
  2027. { Unload stabs }
  2028. if assigned(currstabstrsec) then
  2029. begin
  2030. currstabstrsec.Used:=False;
  2031. currstabstrsec.ReleaseData;
  2032. end;
  2033. currstabsec.Used:=false;
  2034. currstabsec.ReleaseData;
  2035. end;
  2036. { Generate new HdrSym }
  2037. if mergedstabsec.Size>0 then
  2038. begin
  2039. hstab.strpos:=1;
  2040. hstab.ntype:=0;
  2041. hstab.nother:=0;
  2042. hstab.ndesc:=word(mergestabcnt-1);
  2043. hstab.nvalue:=mergedstabstrsec.Size;
  2044. mergedstabsec.Data.seek(0);
  2045. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2046. end;
  2047. { Replace all sections with our combined stabsec }
  2048. stabexesec.ObjSectionList.Clear;
  2049. stabstrexesec.ObjSectionList.Clear;
  2050. stabexesec.AddObjSection(mergedstabsec);
  2051. stabstrexesec.AddObjSection(mergedstabstrsec);
  2052. end;
  2053. procedure TExeOutput.RemoveEmptySections;
  2054. var
  2055. i, j : longint;
  2056. exesec : TExeSection;
  2057. doremove : boolean;
  2058. begin
  2059. for i:=0 to ExeSectionList.Count-1 do
  2060. begin
  2061. exesec:=TExeSection(ExeSectionList[i]);
  2062. if (cs_link_separate_dbg_file in current_settings.globalswitches) and
  2063. (oso_debug in exesec.SecOptions) then
  2064. begin
  2065. Comment(V_Debug,'Adding debug section '+exesec.name+' to debugfile');
  2066. ExeSectionList.OwnsObjects:=false;
  2067. ExeSectionList[i]:=nil;
  2068. ExeSectionList.OwnsObjects:=true;
  2069. DebugSectionList.Add(exesec.Name,exesec);
  2070. end
  2071. else
  2072. begin
  2073. doremove:=not(oso_keep in exesec.SecOptions) and
  2074. (
  2075. (exesec.ObjSectionlist.count=0) or
  2076. (
  2077. (cs_link_strip in current_settings.globalswitches) and
  2078. (oso_debug in exesec.SecOptions)
  2079. )
  2080. );
  2081. if not doremove then
  2082. begin
  2083. { Check if section has no actual data }
  2084. doremove:=true;
  2085. for j:=0 to exesec.ObjSectionList.Count-1 do
  2086. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2087. begin
  2088. doremove:=false;
  2089. break;
  2090. end;
  2091. end;
  2092. if doremove and not (RelocSection and (exesec.Name='.reloc')) then
  2093. begin
  2094. Comment(V_Debug,'Deleting empty section '+exesec.name);
  2095. ExeSectionList[i]:=nil;
  2096. end;
  2097. end;
  2098. end;
  2099. ExeSectionList.Pack;
  2100. end;
  2101. procedure TExeOutput.RemoveUnreferencedSections;
  2102. var
  2103. ObjSectionWorkList : TFPObjectList;
  2104. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2105. begin
  2106. if not aobjsec.Used then
  2107. begin
  2108. aobjsec.Used:=true;
  2109. ObjSectionWorkList.Add(aobjsec);
  2110. end;
  2111. end;
  2112. procedure DoReloc(objreloc:TObjRelocation);
  2113. var
  2114. objsym : TObjSymbol;
  2115. refobjsec : TObjSection;
  2116. begin
  2117. { Disabled Relocation to 0 }
  2118. if objreloc.typ=RELOC_ZERO then
  2119. exit;
  2120. if assigned(objreloc.symbol) then
  2121. begin
  2122. objsym:=objreloc.symbol;
  2123. if objsym.bind<>AB_LOCAL then
  2124. begin
  2125. if not(assigned(objsym.exesymbol) and
  2126. (objsym.exesymbol.State=symstate_defined)) then
  2127. internalerror(200603063);
  2128. objsym:=objsym.exesymbol.objsymbol;
  2129. end;
  2130. if not assigned(objsym.objsection) then
  2131. internalerror(200603062);
  2132. refobjsec:=objsym.objsection;
  2133. end
  2134. else
  2135. if assigned(objreloc.objsection) then
  2136. refobjsec:=objreloc.objsection
  2137. else
  2138. internalerror(200603316);
  2139. if assigned(exemap) then
  2140. begin
  2141. objsym:=objreloc.symbol;
  2142. if assigned(objsym) then
  2143. exemap.Add(' References '+objsym.name+' in '
  2144. +refobjsec.fullname)
  2145. else
  2146. exemap.Add(' References '+refobjsec.fullname);
  2147. end;
  2148. AddToObjSectionWorkList(refobjsec);
  2149. end;
  2150. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2151. var
  2152. i : longint;
  2153. objreloc : TObjRelocation;
  2154. begin
  2155. objreloc:=vtable.VTableRef(VTableIdx);
  2156. if assigned(objreloc) then
  2157. begin
  2158. { Process the relocation now if the ObjSection is
  2159. already processed and marked as used. Otherwise we leave it
  2160. unprocessed. It'll then be resolved when the ObjSection is
  2161. changed to Used }
  2162. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2163. DoReloc(objreloc);
  2164. end;
  2165. { This recursive walking is done here instead of
  2166. in TExeVTable.VTableRef because we can now process
  2167. all needed relocations }
  2168. for i:=0 to vtable.ChildList.Count-1 do
  2169. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2170. end;
  2171. var
  2172. hs : string;
  2173. i,j,k : longint;
  2174. exesec : TExeSection;
  2175. objdata : TObjData;
  2176. objsec : TObjSection;
  2177. objsym : TObjSymbol;
  2178. code : integer;
  2179. vtableidx : longint;
  2180. vtableexesym : TExeSymbol;
  2181. begin
  2182. ObjSectionWorkList:=TFPObjectList.Create(false);
  2183. if assigned(exemap) then
  2184. exemap.AddHeader('Removing unreferenced sections');
  2185. { Initialize by marking all sections unused and
  2186. adding the sections with oso_keep flags to the ObjSectionWorkList }
  2187. for i:=0 to ObjDataList.Count-1 do
  2188. begin
  2189. ObjData:=TObjData(ObjDataList[i]);
  2190. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2191. begin
  2192. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2193. objsec.Used:=false;
  2194. {$warning TODO remove debug section always keep}
  2195. if oso_debug in objsec.secoptions then
  2196. objsec.Used:=true;
  2197. if (oso_keep in objsec.secoptions) then
  2198. begin
  2199. AddToObjSectionWorkList(objsec);
  2200. if objsec.name='.fpc.n_links' then
  2201. objsec.Used:=false;
  2202. end;
  2203. end;
  2204. end;
  2205. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  2206. { Process all sections, add new sections to process based
  2207. on the symbol references }
  2208. while ObjSectionWorkList.Count>0 do
  2209. begin
  2210. objsec:=TObjSection(ObjSectionWorkList.Last);
  2211. if assigned(exemap) then
  2212. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2213. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2214. { Process Relocations }
  2215. for i:=0 to objsec.ObjRelocations.count-1 do
  2216. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2217. { Process Virtual Entry calls }
  2218. if cs_link_opt_vtable in current_settings.globalswitches then
  2219. begin
  2220. for i:=0 to objsec.VTRefList.count-1 do
  2221. begin
  2222. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2223. hs:=objsym.name;
  2224. Delete(hs,1,Pos('_',hs));
  2225. k:=Pos('$$',hs);
  2226. if k=0 then
  2227. internalerror(200603314);
  2228. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2229. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2230. if (code<>0) then
  2231. internalerror(200603317);
  2232. if not assigned(vtableexesym) then
  2233. internalerror(200603315);
  2234. if not assigned(vtableexesym.vtable) then
  2235. internalerror(200603316);
  2236. DoVTableRef(vtableexesym.vtable,vtableidx);
  2237. end;
  2238. end;
  2239. end;
  2240. ObjSectionWorkList.Free;
  2241. ObjSectionWorkList:=nil;
  2242. { Remove unused objsections from ExeSectionList }
  2243. for i:=0 to ExeSectionList.Count-1 do
  2244. begin
  2245. exesec:=TExeSection(ExeSectionList[i]);
  2246. for j:=0 to exesec.ObjSectionlist.count-1 do
  2247. begin
  2248. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2249. if not objsec.used then
  2250. begin
  2251. if assigned(exemap) then
  2252. exemap.Add('Removing '+objsec.FullName);
  2253. exesec.ObjSectionlist[j]:=nil;
  2254. objsec.ReleaseData;
  2255. end;
  2256. end;
  2257. exesec.ObjSectionlist.Pack;
  2258. end;
  2259. end;
  2260. procedure TExeOutput.FixupRelocations;
  2261. procedure FixupSectionList(List:TFPHashObjectList);
  2262. var
  2263. i,j : longint;
  2264. exesec : TExeSection;
  2265. objsec : TObjSection;
  2266. begin
  2267. for i:=0 to List.Count-1 do
  2268. begin
  2269. exesec:=TExeSection(List[i]);
  2270. if not assigned(exesec) then
  2271. continue;
  2272. for j:=0 to exesec.ObjSectionlist.count-1 do
  2273. begin
  2274. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2275. if not objsec.Used then
  2276. internalerror(200603301);
  2277. objsec.FixupRelocs;
  2278. end;
  2279. end;
  2280. end;
  2281. begin
  2282. FixupSectionList(ExeSectionList);
  2283. FixupSectionList(DebugSectionList);
  2284. end;
  2285. {****************************************************************************
  2286. TObjInput
  2287. ****************************************************************************}
  2288. constructor TObjInput.create;
  2289. begin
  2290. end;
  2291. destructor TObjInput.destroy;
  2292. begin
  2293. inherited destroy;
  2294. end;
  2295. function TObjInput.newObjData(const n:string):TObjData;
  2296. begin
  2297. result:=CObjData.create(n);
  2298. end;
  2299. procedure TObjInput.inputerror(const s : string);
  2300. begin
  2301. Comment(V_Error,s+' while reading '+InputFileName);
  2302. end;
  2303. {$ifdef MEMDEBUG}
  2304. initialization
  2305. memobjsymbols:=TMemDebug.create('ObjSymbols');
  2306. memobjsymbols.stop;
  2307. memobjsections:=TMemDebug.create('ObjSections');
  2308. memobjsections.stop;
  2309. finalization
  2310. memobjsymbols.free;
  2311. memobjsections.free;
  2312. {$endif MEMDEBUG}
  2313. end.