ogbase.pas 93 KB

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