ogbase.pas 79 KB

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