ogbase.pas 88 KB

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