ogbase.pas 89 KB

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