ogbase.pas 79 KB

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