ogbase.pas 89 KB

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