ogbase.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724
  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. {code} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep],
  812. {Data} [oso_Data,oso_load,oso_write,oso_keep],
  813. { TODO: Fix sec_rodata be read-only-with-relocs}
  814. {roData} [oso_Data,oso_load,oso_write,oso_keep],
  815. { TODO: Fix sec_rodata_norel be read-only/constant}
  816. {roData_norel} [oso_Data,oso_load,oso_write,oso_keep],
  817. {bss} [oso_load,oso_write,oso_keep],
  818. {threadvar} [oso_load,oso_write],
  819. {pdata} [oso_load,oso_readonly,oso_keep],
  820. {stub} [oso_Data,oso_load,oso_readonly,oso_executable],
  821. {data_nonlazy} [oso_Data,oso_load,oso_write],
  822. {data_lazy} [oso_Data,oso_load,oso_write],
  823. {init_func} [oso_Data,oso_load],
  824. {term_func} [oso_Data,oso_load],
  825. {stab} [oso_Data,oso_noload,oso_debug],
  826. {stabstr} [oso_Data,oso_noload,oso_strings,oso_debug],
  827. {iData2} [oso_Data,oso_load,oso_write],
  828. {iData4} [oso_Data,oso_load,oso_write],
  829. {iData5} [oso_Data,oso_load,oso_write],
  830. {iData6} [oso_Data,oso_load,oso_write],
  831. {iData7} [oso_Data,oso_load,oso_write],
  832. {eData} [oso_Data,oso_load,oso_readonly],
  833. {eh_frame} [oso_Data,oso_load,oso_readonly],
  834. {debug_frame} [oso_Data,oso_noload,oso_debug],
  835. {debug_info} [oso_Data,oso_noload,oso_debug],
  836. {debug_line} [oso_Data,oso_noload,oso_debug],
  837. {debug_abbrev} [oso_Data,oso_noload,oso_debug],
  838. {fpc} [oso_Data,oso_load,oso_write,oso_keep],
  839. {toc} [oso_Data,oso_load,oso_readonly],
  840. {init} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep],
  841. {fini} [oso_Data,oso_load,oso_readonly,oso_executable,oso_keep],
  842. {objc_class} [oso_data,oso_load],
  843. {objc_meta_class} [oso_data,oso_load],
  844. {objc_cat_cls_meth} [oso_data,oso_load],
  845. {objc_cat_inst_meth} [oso_data,oso_load],
  846. {objc_protocol} [oso_data,oso_load],
  847. {objc_string_object} [oso_data,oso_load],
  848. {objc_cls_meth} [oso_data,oso_load],
  849. {objc_inst_meth} [oso_data,oso_load],
  850. {objc_cls_refs} [oso_data,oso_load],
  851. {objc_message_refs} [oso_data,oso_load],
  852. {objc_symbols} [oso_data,oso_load],
  853. {objc_category} [oso_data,oso_load],
  854. {objc_class_vars} [oso_data,oso_load],
  855. {objc_instance_vars} [oso_data,oso_load],
  856. {objc_module_info} [oso_data,oso_load],
  857. {objc_class_names} [oso_data,oso_load],
  858. {objc_meth_var_types} [oso_data,oso_load],
  859. {objc_meth_var_names} [oso_data,oso_load],
  860. {objc_selector_strs} [oso_data,oso_load],
  861. {objc_protocol_ext} [oso_data,oso_load],
  862. {objc_class_ext} [oso_data,oso_load],
  863. {objc_property} [oso_data,oso_load],
  864. {objc_image_info} [oso_data,oso_load],
  865. {objc_cstring_object} [oso_data,oso_load],
  866. {objc_sel_fixup} [oso_data,oso_load],
  867. {sec_objc_data} [oso_data,oso_load],
  868. {sec_objc_const} [oso_data,oso_load],
  869. {sec_objc_sup_refs} [oso_data,oso_load],
  870. {sec_data_coalesced} [oso_data,oso_load],
  871. {sec_objc_classlist} [oso_data,oso_load],
  872. {sec_objc_nlclasslist} [oso_data,oso_load],
  873. {sec_objc_catlist} [oso_data,oso_load],
  874. {sec_objc_nlcatlist} [oso_data,oso_load],
  875. {sec_objc_protolist'} [oso_data,oso_load]
  876. );
  877. begin
  878. result:=secoptions[atype];
  879. end;
  880. function TObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  881. begin
  882. case atype of
  883. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev:
  884. result:=1;
  885. sec_code,
  886. sec_bss,
  887. sec_data:
  888. result:=16;
  889. { For idata (at least idata2) it must be 4 bytes, because
  890. an entry is always (also in win64) 20 bytes and aligning
  891. on 8 bytes will insert 4 bytes between the entries resulting
  892. in a corrupt idata section }
  893. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7:
  894. result:=4;
  895. else
  896. result:=sizeof(pint);
  897. end;
  898. end;
  899. function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
  900. begin
  901. result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
  902. end;
  903. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions;DiscardDuplicate:boolean):TObjSection;
  904. begin
  905. if DiscardDuplicate then
  906. result:=TObjSection(FObjSectionList.Find(aname))
  907. else
  908. result:=nil;
  909. if not assigned(result) then
  910. begin
  911. result:=CObjSection.create(FObjSectionList,aname,aalign,aoptions);
  912. result.ObjData:=self;
  913. end;
  914. FCurrObjSec:=result;
  915. end;
  916. procedure TObjData.CreateDebugSections;
  917. begin
  918. end;
  919. function TObjData.FindSection(const aname:string):TObjSection;
  920. begin
  921. result:=TObjSection(FObjSectionList.Find(aname));
  922. end;
  923. procedure TObjData.setsection(asec:TObjSection);
  924. begin
  925. if asec.ObjData<>self then
  926. internalerror(200403041);
  927. FCurrObjSec:=asec;
  928. end;
  929. function TObjData.createsymbol(const aname:string):TObjSymbol;
  930. begin
  931. result:=TObjSymbol(FObjSymbolList.Find(aname));
  932. if not assigned(result) then
  933. result:=TObjSymbol.Create(FObjSymbolList,aname);
  934. end;
  935. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  936. begin
  937. if assigned(asmsym) then
  938. begin
  939. if not assigned(asmsym.cachedObjSymbol) then
  940. begin
  941. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  942. asmsym.cachedObjSymbol:=result;
  943. FCachedAsmSymbolList.add(asmsym);
  944. end
  945. else
  946. begin
  947. result:=TObjSymbol(asmsym.cachedObjSymbol);
  948. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  949. { Register also in TObjSection }
  950. CurrObjSec.AddSymbolDefine(result);
  951. end;
  952. end
  953. else
  954. result:=nil;
  955. end;
  956. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  957. begin
  958. if not assigned(CurrObjSec) then
  959. internalerror(200603051);
  960. result:=CreateSymbol(aname);
  961. { Register also in TObjSection }
  962. CurrObjSec.AddSymbolDefine(result);
  963. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  964. end;
  965. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  966. begin
  967. if assigned(asmsym) then
  968. begin
  969. if not assigned(asmsym.cachedObjSymbol) then
  970. begin
  971. result:=symbolref(asmsym.name);
  972. asmsym.cachedObjSymbol:=result;
  973. FCachedAsmSymbolList.add(asmsym);
  974. end
  975. else
  976. result:=TObjSymbol(asmsym.cachedObjSymbol);
  977. end
  978. else
  979. result:=nil;
  980. end;
  981. function TObjData.symbolref(const aname:string):TObjSymbol;
  982. begin
  983. if not assigned(CurrObjSec) then
  984. internalerror(200603052);
  985. result:=CreateSymbol(aname);
  986. end;
  987. procedure TObjData.ResetCachedAsmSymbols;
  988. var
  989. i : longint;
  990. begin
  991. for i:=0 to FCachedAsmSymbolList.Count-1 do
  992. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  993. FCachedAsmSymbolList.Clear;
  994. end;
  995. procedure TObjData.writebytes(const Data;len:aword);
  996. begin
  997. if not assigned(CurrObjSec) then
  998. internalerror(200402251);
  999. CurrObjSec.write(Data,len);
  1000. end;
  1001. procedure TObjData.alloc(len:aword);
  1002. begin
  1003. if not assigned(CurrObjSec) then
  1004. internalerror(200402252);
  1005. CurrObjSec.alloc(len);
  1006. end;
  1007. procedure TObjData.allocalign(len:shortint);
  1008. begin
  1009. if not assigned(CurrObjSec) then
  1010. internalerror(200402253);
  1011. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  1012. end;
  1013. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  1014. begin
  1015. with TObjSection(p) do
  1016. alloc(align(size,secalign)-size);
  1017. end;
  1018. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  1019. begin
  1020. with TObjSection(p) do
  1021. begin
  1022. if assigned(Data) then
  1023. writezeros(align(size,secalign)-size);
  1024. end;
  1025. end;
  1026. procedure TObjData.section_reset(p:TObject;arg:pointer);
  1027. begin
  1028. with TObjSection(p) do
  1029. begin
  1030. Size:=0;
  1031. Datapos:=0;
  1032. mempos:=0;
  1033. end;
  1034. end;
  1035. procedure TObjData.beforealloc;
  1036. begin
  1037. { create stabs sections if debugging }
  1038. if assigned(StabsSec) then
  1039. begin
  1040. StabsSec.Alloc(sizeof(TObjStabEntry));
  1041. StabStrSec.Alloc(1);
  1042. end;
  1043. end;
  1044. procedure TObjData.beforewrite;
  1045. var
  1046. s : string[1];
  1047. hstab : TObjStabEntry;
  1048. begin
  1049. { create stabs sections if debugging }
  1050. if assigned(StabsSec) then
  1051. begin
  1052. { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
  1053. fillchar(hstab,sizeof(hstab),0);
  1054. StabsSec.Write(hstab,sizeof(hstab));
  1055. { start of stabstr }
  1056. s:=#0;
  1057. StabStrSec.write(s[1],length(s));
  1058. end;
  1059. end;
  1060. procedure TObjData.afteralloc;
  1061. begin
  1062. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  1063. end;
  1064. procedure TObjData.afterwrite;
  1065. var
  1066. s : string[1];
  1067. hstab : TObjStabEntry;
  1068. begin
  1069. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  1070. { For the stab section we need an HdrSym which can now be
  1071. calculated more easily }
  1072. if assigned(StabsSec) then
  1073. begin
  1074. { end of stabstr }
  1075. s:=#0;
  1076. StabStrSec.write(s[1],length(s));
  1077. { header stab }
  1078. hstab.strpos:=1;
  1079. hstab.ntype:=0;
  1080. hstab.nother:=0;
  1081. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  1082. hstab.nvalue:=StabStrSec.Size;
  1083. StabsSec.Data.seek(0);
  1084. StabsSec.Data.write(hstab,sizeof(hstab));
  1085. end;
  1086. end;
  1087. procedure TObjData.resetsections;
  1088. begin
  1089. FObjSectionList.ForEachCall(@section_reset,nil);
  1090. end;
  1091. {****************************************************************************
  1092. TObjOutput
  1093. ****************************************************************************}
  1094. constructor TObjOutput.create(AWriter:TObjectWriter);
  1095. begin
  1096. FWriter:=AWriter;
  1097. CObjData:=TObjData;
  1098. end;
  1099. destructor TObjOutput.destroy;
  1100. begin
  1101. inherited destroy;
  1102. end;
  1103. function TObjOutput.newObjData(const n:string):TObjData;
  1104. begin
  1105. result:=CObjData.create(n);
  1106. if (cs_use_lineinfo in current_settings.globalswitches) or
  1107. (cs_debuginfo in current_settings.moduleswitches) then
  1108. result.CreateDebugSections;
  1109. end;
  1110. function TObjOutput.startObjectfile(const fn:string):boolean;
  1111. begin
  1112. result:=false;
  1113. { start the writer already, so the .a generation can initialize
  1114. the position of the current objectfile }
  1115. if not FWriter.createfile(fn) then
  1116. Comment(V_Fatal,'Can''t create object '+fn);
  1117. result:=true;
  1118. end;
  1119. function TObjOutput.writeobjectfile(Data:TObjData):boolean;
  1120. begin
  1121. if errorcount=0 then
  1122. result:=writeData(Data)
  1123. else
  1124. result:=true;
  1125. { close the writer }
  1126. FWriter.closefile;
  1127. end;
  1128. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  1129. begin
  1130. { export globals and common symbols, this is needed
  1131. for .a files }
  1132. if p.bind in [AB_GLOBAL,AB_COMMON] then
  1133. FWriter.writesym(p.name);
  1134. end;
  1135. {****************************************************************************
  1136. TExeVTable
  1137. ****************************************************************************}
  1138. constructor TExeVTable.Create(AExeSymbol:TExeSymbol);
  1139. begin
  1140. ExeSymbol:=AExeSymbol;
  1141. if ExeSymbol.State=symstate_undefined then
  1142. internalerror(200604012);
  1143. ChildList:=TFPObjectList.Create(false);
  1144. end;
  1145. destructor TExeVTable.Destroy;
  1146. begin
  1147. ChildList.Free;
  1148. if assigned(EntryArray) then
  1149. Freemem(EntryArray);
  1150. end;
  1151. procedure TExeVTable.CheckIdx(VTableIdx:longint);
  1152. var
  1153. OldEntryCnt : longint;
  1154. begin
  1155. if VTableIdx>=EntryCnt then
  1156. begin
  1157. OldEntryCnt:=EntryCnt;
  1158. EntryCnt:=VTableIdx+1;
  1159. ReAllocMem(EntryArray,EntryCnt*sizeof(TVTableEntry));
  1160. FillChar(EntryArray[OldEntryCnt],(EntryCnt-OldEntryCnt)*sizeof(TVTableEntry),0);
  1161. end;
  1162. end;
  1163. procedure TExeVTable.AddChild(vt:TExeVTable);
  1164. begin
  1165. ChildList.Add(vt);
  1166. end;
  1167. procedure TExeVTable.AddEntry(VTableIdx:Longint);
  1168. var
  1169. i : longint;
  1170. objreloc : TObjRelocation;
  1171. vtblentryoffset : aword;
  1172. begin
  1173. CheckIdx(VTableIdx);
  1174. vtblentryoffset:=ExeSymbol.ObjSymbol.Offset+longword(VTableIdx)*sizeof(pint);
  1175. { Find and disable relocation }
  1176. for i:=0 to ExeSymbol.ObjSymbol.ObjSection.ObjRelocations.Count-1 do
  1177. begin
  1178. objreloc:=TObjRelocation(ExeSymbol.ObjSymbol.ObjSection.ObjRelocations[i]);
  1179. if objreloc.dataoffset=vtblentryoffset then
  1180. begin
  1181. EntryArray[VTableIdx].ObjRelocation:=objreloc;
  1182. EntryArray[VTableIdx].OrgRelocType:=objreloc.typ;
  1183. objreloc.typ:=RELOC_ZERO;
  1184. break;
  1185. end;
  1186. end;
  1187. if not assigned(EntryArray[VTableIdx].ObjRelocation) then
  1188. internalerror(200604011);
  1189. end;
  1190. procedure TExeVTable.SetVTableSize(ASize:longint);
  1191. begin
  1192. if EntryCnt<>0 then
  1193. internalerror(200603313);
  1194. EntryCnt:=ASize div sizeof(pint);
  1195. EntryArray:=AllocMem(EntryCnt*sizeof(TVTableEntry));
  1196. end;
  1197. function TExeVTable.VTableRef(VTableIdx:Longint):TObjRelocation;
  1198. begin
  1199. result:=nil;
  1200. CheckIdx(VTableIdx);
  1201. if EntryArray[VTableIdx].Used then
  1202. exit;
  1203. { Restore relocation if available }
  1204. if assigned(EntryArray[VTableIdx].ObjRelocation) then
  1205. begin
  1206. EntryArray[VTableIdx].ObjRelocation.typ:=EntryArray[VTableIdx].OrgRelocType;
  1207. result:=EntryArray[VTableIdx].ObjRelocation;
  1208. end;
  1209. EntryArray[VTableIdx].Used:=true;
  1210. end;
  1211. {****************************************************************************
  1212. TExeSection
  1213. ****************************************************************************}
  1214. constructor TExeSection.create(AList:TFPHashObjectList;const AName:string);
  1215. begin
  1216. inherited create(AList,AName);
  1217. Size:=0;
  1218. MemPos:=0;
  1219. DataPos:=0;
  1220. FSecSymIdx:=0;
  1221. FObjSectionList:=TFPObjectList.Create(false);
  1222. end;
  1223. destructor TExeSection.destroy;
  1224. begin
  1225. ObjSectionList.Free;
  1226. inherited destroy;
  1227. end;
  1228. procedure TExeSection.AddObjSection(objsec:TObjSection);
  1229. begin
  1230. ObjSectionList.Add(objsec);
  1231. if (SecOptions<>[]) then
  1232. begin
  1233. { Only if the section contains (un)initialized data the
  1234. data flag must match. This check is not needed if the
  1235. section is empty for a symbol allocation }
  1236. if (objsec.size>0) and
  1237. ((oso_Data in SecOptions)<>(oso_Data in objsec.SecOptions)) then
  1238. Comment(V_Error,'Incompatible section options');
  1239. end
  1240. else
  1241. begin
  1242. { inherit section options }
  1243. SecAlign:=objsec.SecAlign;
  1244. SecOptions:=SecOptions+objsec.SecOptions;
  1245. end;
  1246. { relate ObjSection to ExeSection, and mark it Used by default }
  1247. objsec.ExeSection:=self;
  1248. objsec.Used:=true;
  1249. end;
  1250. {****************************************************************************
  1251. TStaticLibrary
  1252. ****************************************************************************}
  1253. constructor TStaticLibrary.create(AList:TFPHashObjectList;const AName:string;AReader:TObjectReader;AObjInputClass:TObjInputClass);
  1254. begin
  1255. inherited create(AList,AName);
  1256. FArReader:=AReader;
  1257. FObjInputClass:=AObjInputClass;
  1258. end;
  1259. destructor TStaticLibrary.destroy;
  1260. begin
  1261. ArReader.Free;
  1262. inherited destroy;
  1263. end;
  1264. {****************************************************************************
  1265. TImportLibrary
  1266. ****************************************************************************}
  1267. constructor TImportLibrary.create(AList:TFPHashObjectList;const AName:string);
  1268. begin
  1269. inherited create(AList,AName);
  1270. FImportSymbolList:=TFPHashObjectList.Create(true);
  1271. end;
  1272. destructor TImportLibrary.destroy;
  1273. begin
  1274. ImportSymbolList.Free;
  1275. inherited destroy;
  1276. end;
  1277. {****************************************************************************
  1278. TImportSymbol
  1279. ****************************************************************************}
  1280. constructor TImportSymbol.create(AList:TFPHashObjectList;const AName:string;AOrdNr:longint;AIsVar:boolean);
  1281. begin
  1282. inherited Create(AList, AName);
  1283. FOrdNr:=AOrdNr;
  1284. FIsVar:=AIsVar;
  1285. FMangledName:=AName;
  1286. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1287. { This allows to import VC++ mangled names from DLLs. }
  1288. if target_info.system in systems_all_windows then
  1289. begin
  1290. Replace(FMangledName,'?','__q$$');
  1291. {$ifdef arm}
  1292. { @ symbol is not allowed in ARM assembler only }
  1293. Replace(FMangledName,'@','__a$$');
  1294. {$endif arm}
  1295. end;
  1296. end;
  1297. {****************************************************************************
  1298. TExeOutput
  1299. ****************************************************************************}
  1300. constructor TExeOutput.create;
  1301. begin
  1302. { init writer }
  1303. FWriter:=TObjectwriter.create;
  1304. FExeWriteMode:=ewm_exefull;
  1305. { object files }
  1306. FObjDataList:=TFPObjectList.Create(true);
  1307. { symbols }
  1308. FExeSymbolList:=TFPHashObjectList.Create(true);
  1309. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  1310. FExternalObjSymbols:=TFPObjectList.Create(false);
  1311. FCommonObjSymbols:=TFPObjectList.Create(false);
  1312. FExeVTableList:=TFPObjectList.Create(false);
  1313. FEntryName:='start';
  1314. { sections }
  1315. FExeSectionList:=TFPHashObjectList.Create(true);
  1316. FImageBase:=0;
  1317. SectionMemAlign:=$1000;
  1318. SectionDataAlign:=$200;
  1319. FCExeSection:=TExeSection;
  1320. FCObjData:=TObjData;
  1321. end;
  1322. destructor TExeOutput.destroy;
  1323. begin
  1324. FExeSymbolList.free;
  1325. UnresolvedExeSymbols.free;
  1326. ExternalObjSymbols.free;
  1327. CommonObjSymbols.free;
  1328. ExeVTableList.free;
  1329. FExeSectionList.free;
  1330. ObjDatalist.free;
  1331. FWriter.free;
  1332. inherited destroy;
  1333. end;
  1334. function TExeOutput.WriteExeFile(const fn:string):boolean;
  1335. begin
  1336. result:=false;
  1337. if FWriter.createfile(fn) then
  1338. begin
  1339. { Only write the .o if there are no errors }
  1340. if errorcount=0 then
  1341. result:=writedata
  1342. else
  1343. result:=true;
  1344. { close the writer }
  1345. FWriter.closefile;
  1346. end
  1347. else
  1348. Comment(V_Fatal,'Can''t create executable '+fn);
  1349. end;
  1350. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1351. begin
  1352. result:=TExeSection(ExeSectionList.Find(aname));
  1353. end;
  1354. procedure TExeOutput.AddObjData(ObjData:TObjData);
  1355. begin
  1356. if ObjData.classtype<>FCObjData then
  1357. Comment(V_Error,'Invalid input object format for '+ObjData.name+' got '+ObjData.classname+' expected '+FCObjData.classname);
  1358. ObjDataList.Add(ObjData);
  1359. end;
  1360. procedure TExeOutput.Load_Start;
  1361. begin
  1362. ObjDataList.Clear;
  1363. { Globals defined in the linker script }
  1364. if not assigned(internalObjData) then
  1365. internalObjData:=CObjData.create('*Internal*');
  1366. AddObjData(internalObjData);
  1367. { Common Data section }
  1368. commonObjSection:=internalObjData.createsection(sec_bss,'');
  1369. end;
  1370. procedure TExeOutput.Load_EntryName(const aname:string);
  1371. begin
  1372. EntryName:=aname;
  1373. end;
  1374. procedure TExeOutput.Load_IsSharedLibrary;
  1375. begin
  1376. IsSharedLibrary:=true;
  1377. end;
  1378. procedure TExeOutput.Load_ImageBase(const avalue:string);
  1379. var
  1380. code : integer;
  1381. objsec : TObjSection;
  1382. objsym : TObjSymbol;
  1383. exesym : TExeSymbol;
  1384. begin
  1385. val(avalue,ImageBase,code);
  1386. if code<>0 then
  1387. Comment(V_Error,'Invalid number '+avalue);
  1388. { Create __image_base__ symbol, create the symbol
  1389. in a section with adress 0 and at offset 0 }
  1390. objsec:=internalObjData.createsection('*__image_base__',0,[]);
  1391. internalObjData.setsection(objsec);
  1392. objsym:=internalObjData.SymbolDefine('__image_base__',AB_GLOBAL,AT_FUNCTION);
  1393. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1394. exesym.ObjSymbol:=objsym;
  1395. end;
  1396. procedure TExeOutput.Load_Symbol(const aname:string);
  1397. begin
  1398. internalObjData.createsection('*'+aname,0,[]);
  1399. internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION);
  1400. end;
  1401. procedure TExeOutput.Order_Start;
  1402. begin
  1403. end;
  1404. procedure TExeOutput.Order_End;
  1405. begin
  1406. internalObjData.afterwrite;
  1407. end;
  1408. procedure TExeOutput.Order_ExeSection(const aname:string);
  1409. var
  1410. sec : TExeSection;
  1411. begin
  1412. sec:=FindExeSection(aname);
  1413. if not assigned(sec) then
  1414. sec:=CExeSection.create(ExeSectionList,aname);
  1415. { Clear ExeSection contents }
  1416. FCurrExeSec:=sec;
  1417. end;
  1418. procedure TExeOutput.Order_EndExeSection;
  1419. begin
  1420. if not assigned(CurrExeSec) then
  1421. internalerror(200602184);
  1422. FCurrExeSec:=nil;
  1423. end;
  1424. procedure TExeOutput.Order_ObjSection(const aname:string);
  1425. var
  1426. i,j : longint;
  1427. ObjData : TObjData;
  1428. objsec : TObjSection;
  1429. TmpObjSectionList : TFPObjectList;
  1430. begin
  1431. if not assigned(CurrExeSec) then
  1432. internalerror(200602181);
  1433. TmpObjSectionList:=TFPObjectList.Create(false);
  1434. for i:=0 to ObjDataList.Count-1 do
  1435. begin
  1436. ObjData:=TObjData(ObjDataList[i]);
  1437. for j:=0 to ObjData.ObjSectionList.Count-1 do
  1438. begin
  1439. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  1440. if (not objsec.Used) and
  1441. MatchPattern(aname,objsec.name) then
  1442. TmpObjSectionList.Add(objsec);
  1443. end;
  1444. end;
  1445. { Order list if needed }
  1446. Order_ObjSectionList(TmpObjSectionList);
  1447. { Add the (ordered) list to the current ExeSection }
  1448. for i:=0 to TmpObjSectionList.Count-1 do
  1449. begin
  1450. objsec:=TObjSection(TmpObjSectionList[i]);
  1451. CurrExeSec.AddObjSection(objsec);
  1452. end;
  1453. TmpObjSectionList.Free;
  1454. end;
  1455. procedure TExeOutput.Order_ObjSectionList(ObjSectionList : TFPObjectList);
  1456. begin
  1457. end;
  1458. procedure TExeOutput.Order_Symbol(const aname:string);
  1459. var
  1460. ObjSection : TObjSection;
  1461. begin
  1462. ObjSection:=internalObjData.findsection('*'+aname);
  1463. if not assigned(ObjSection) then
  1464. internalerror(200603041);
  1465. CurrExeSec.AddObjSection(ObjSection);
  1466. end;
  1467. procedure TExeOutput.Order_Align(const avalue:string);
  1468. var
  1469. code : integer;
  1470. alignval : shortint;
  1471. objsec : TObjSection;
  1472. begin
  1473. val(avalue,alignval,code);
  1474. if code<>0 then
  1475. Comment(V_Error,'Invalid number '+avalue);
  1476. if alignval<=0 then
  1477. exit;
  1478. { Create an empty section with the required aligning }
  1479. inc(Fzeronr);
  1480. objsec:=internalObjData.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1481. CurrExeSec.AddObjSection(objsec);
  1482. end;
  1483. procedure TExeOutput.Order_Zeros(const avalue:string);
  1484. var
  1485. zeros : array[0..1023] of byte;
  1486. code : integer;
  1487. len : longint;
  1488. objsec : TObjSection;
  1489. begin
  1490. val(avalue,len,code);
  1491. if code<>0 then
  1492. Comment(V_Error,'Invalid number '+avalue);
  1493. if len<=0 then
  1494. exit;
  1495. if len>sizeof(zeros) then
  1496. internalerror(200602254);
  1497. fillchar(zeros,len,0);
  1498. inc(Fzeronr);
  1499. objsec:=internalObjData.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_Data,oso_keep]);
  1500. internalObjData.writebytes(zeros,len);
  1501. CurrExeSec.AddObjSection(objsec);
  1502. end;
  1503. procedure TExeOutput.MemPos_Start;
  1504. begin
  1505. CurrMemPos:=0;
  1506. end;
  1507. procedure TExeOutput.MemPos_Header;
  1508. begin
  1509. end;
  1510. procedure TExeOutput.MemPos_ExeSection(const aname:string);
  1511. var
  1512. i : longint;
  1513. objsec : TObjSection;
  1514. begin
  1515. { Section can be removed }
  1516. FCurrExeSec:=FindExeSection(aname);
  1517. if not assigned(CurrExeSec) then
  1518. exit;
  1519. { Alignment of ExeSection }
  1520. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1521. CurrExeSec.MemPos:=CurrMemPos;
  1522. { set position of object ObjSections }
  1523. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1524. begin
  1525. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1526. CurrMemPos:=objsec.setmempos(CurrMemPos);
  1527. end;
  1528. { calculate size of the section }
  1529. CurrExeSec.Size:=CurrMemPos-CurrExeSec.MemPos;
  1530. end;
  1531. procedure TExeOutput.MemPos_EndExeSection;
  1532. begin
  1533. if not assigned(CurrExeSec) then
  1534. exit;
  1535. FCurrExeSec:=nil;
  1536. end;
  1537. procedure TExeOutput.DataPos_Start;
  1538. begin
  1539. end;
  1540. procedure TExeOutput.DataPos_Header;
  1541. begin
  1542. end;
  1543. procedure TExeOutput.DataPos_ExeSection(const aname:string);
  1544. var
  1545. i : longint;
  1546. objsec : TObjSection;
  1547. begin
  1548. { Section can be removed }
  1549. FCurrExeSec:=FindExeSection(aname);
  1550. if not assigned(CurrExeSec) then
  1551. exit;
  1552. { don't write normal section if writing only debug info }
  1553. if (ExeWriteMode=ewm_dbgonly) and
  1554. not(oso_debug in CurrExeSec.SecOptions) then
  1555. exit;
  1556. if (oso_Data in currexesec.SecOptions) then
  1557. begin
  1558. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1559. CurrExeSec.DataPos:=CurrDataPos;
  1560. end;
  1561. { set position of object ObjSections }
  1562. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1563. begin
  1564. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1565. if (oso_Data in objsec.SecOptions) then
  1566. begin
  1567. if not(oso_Data in currexesec.SecOptions) then
  1568. internalerror(200603043);
  1569. if not assigned(objsec.Data) then
  1570. internalerror(200603044);
  1571. objsec.setDatapos(CurrDataPos);
  1572. end;
  1573. end;
  1574. end;
  1575. procedure TExeOutput.DataPos_EndExeSection;
  1576. begin
  1577. if not assigned(CurrExeSec) then
  1578. exit;
  1579. FCurrExeSec:=nil;
  1580. end;
  1581. procedure TExeOutput.DataPos_Symbols;
  1582. var
  1583. i : longint;
  1584. sym : TExeSymbol;
  1585. begin
  1586. { Removing unused symbols }
  1587. for i:=0 to ExeSymbolList.Count-1 do
  1588. begin
  1589. sym:=TExeSymbol(ExeSymbolList[i]);
  1590. if not sym.ObjSymbol.objsection.Used then
  1591. ExeSymbolList[i]:=nil;
  1592. end;
  1593. ExeSymbolList.Pack;
  1594. end;
  1595. procedure TExeOutput.BuildVTableTree(VTInheritList,VTEntryList:TFPObjectList);
  1596. var
  1597. hs : string;
  1598. code : integer;
  1599. i,k,
  1600. vtableidx : longint;
  1601. vtableexesym,
  1602. childexesym,
  1603. parentexesym : TExeSymbol;
  1604. objsym : TObjSymbol;
  1605. begin
  1606. { Build inheritance tree from VTINHERIT }
  1607. for i:=0 to VTInheritList.Count-1 do
  1608. begin
  1609. objsym:=TObjSymbol(VTInheritList[i]);
  1610. hs:=objsym.name;
  1611. { VTINHERIT_<ChildVMTName>$$<ParentVMTName> }
  1612. Delete(hs,1,Pos('_',hs));
  1613. k:=Pos('$$',hs);
  1614. if k=0 then
  1615. internalerror(200603311);
  1616. childexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1617. parentexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,k+2,length(hs)-k-1)));
  1618. if not assigned(childexesym) or
  1619. not assigned(parentexesym)then
  1620. internalerror(200603312);
  1621. if not assigned(childexesym.vtable) then
  1622. begin
  1623. childexesym.vtable:=TExeVTable.Create(childexesym);
  1624. ExeVTableList.Add(childexesym.vtable);
  1625. end;
  1626. if not assigned(parentexesym.vtable) then
  1627. begin
  1628. parentexesym.vtable:=TExeVTable.Create(parentexesym);
  1629. ExeVTableList.Add(parentexesym.vtable);
  1630. end;
  1631. childexesym.vtable.SetVTableSize(childexesym.ObjSymbol.Size);
  1632. if parentexesym<>childexesym then
  1633. parentexesym.vtable.AddChild(childexesym.vtable);
  1634. end;
  1635. { Find VTable entries from VTENTRY }
  1636. for i:=0 to VTEntryList.Count-1 do
  1637. begin
  1638. objsym:=TObjSymbol(VTEntryList[i]);
  1639. hs:=objsym.name;
  1640. { VTENTRY_<VTableName>$$<Index> }
  1641. Delete(hs,1,Pos('_',hs));
  1642. k:=Pos('$$',hs);
  1643. if k=0 then
  1644. internalerror(200603319);
  1645. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  1646. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  1647. if (code<>0) then
  1648. internalerror(200603318);
  1649. if not assigned(vtableexesym) then
  1650. internalerror(2006033110);
  1651. vtableexesym.vtable.AddEntry(vtableidx);
  1652. end;
  1653. end;
  1654. procedure TExeOutput.PackUnresolvedExeSymbols(const s:string);
  1655. var
  1656. i : longint;
  1657. exesym : TExeSymbol;
  1658. begin
  1659. { Generate a list of Unresolved External symbols }
  1660. for i:=0 to UnresolvedExeSymbols.count-1 do
  1661. begin
  1662. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1663. if exesym.State<>symstate_undefined then
  1664. UnresolvedExeSymbols[i]:=nil;
  1665. end;
  1666. UnresolvedExeSymbols.Pack;
  1667. Comment(V_Debug,'Number of unresolved externals '+s+' '+tostr(UnresolvedExeSymbols.Count));
  1668. end;
  1669. procedure TExeOutput.ResolveSymbols(StaticLibraryList:TFPHashObjectList);
  1670. var
  1671. ObjData : TObjData;
  1672. exesym : TExeSymbol;
  1673. objsym,
  1674. commonsym : TObjSymbol;
  1675. objinput : TObjInput;
  1676. StaticLibrary : TStaticLibrary;
  1677. firstarchive,
  1678. firstcommon : boolean;
  1679. i,j : longint;
  1680. VTEntryList,
  1681. VTInheritList : TFPObjectList;
  1682. procedure LoadObjDataSymbols(ObjData:TObjData);
  1683. var
  1684. j : longint;
  1685. hs : string;
  1686. exesym : TExeSymbol;
  1687. objsym : TObjSymbol;
  1688. begin
  1689. for j:=0 to ObjData.ObjSymbolList.Count-1 do
  1690. begin
  1691. objsym:=TObjSymbol(ObjData.ObjSymbolList[j]);
  1692. { From the local symbols we are only interressed in the
  1693. VTENTRY and VTINHERIT symbols }
  1694. if objsym.bind=AB_LOCAL then
  1695. begin
  1696. if cs_link_opt_vtable in current_settings.globalswitches then
  1697. begin
  1698. hs:=objsym.name;
  1699. if (hs[1]='V') then
  1700. begin
  1701. if Copy(hs,1,5)='VTREF' then
  1702. begin
  1703. if not assigned(objsym.ObjSection.VTRefList) then
  1704. objsym.ObjSection.VTRefList:=TFPObjectList.Create(false);
  1705. objsym.ObjSection.VTRefList.Add(objsym);
  1706. end
  1707. else if Copy(hs,1,7)='VTENTRY' then
  1708. VTEntryList.Add(objsym)
  1709. else if Copy(hs,1,9)='VTINHERIT' then
  1710. VTInheritList.Add(objsym);
  1711. end;
  1712. end;
  1713. continue;
  1714. end;
  1715. { Search for existing exesymbol }
  1716. exesym:=texesymbol(FExeSymbolList.Find(objsym.name));
  1717. if not assigned(exesym) then
  1718. begin
  1719. exesym:=texesymbol.Create(FExeSymbolList,objsym.name);
  1720. exesym.ObjSymbol:=objsym;
  1721. end;
  1722. objsym.ExeSymbol:=exesym;
  1723. case objsym.bind of
  1724. AB_GLOBAL :
  1725. begin
  1726. if exesym.State<>symstate_defined then
  1727. begin
  1728. exesym.ObjSymbol:=objsym;
  1729. exesym.State:=symstate_defined;
  1730. end
  1731. else
  1732. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  1733. end;
  1734. AB_EXTERNAL :
  1735. begin
  1736. ExternalObjSymbols.add(objsym);
  1737. { Register unresolved symbols only the first time they
  1738. are registered }
  1739. if exesym.ObjSymbol=objsym then
  1740. UnresolvedExeSymbols.Add(exesym);
  1741. end;
  1742. AB_COMMON :
  1743. begin
  1744. if exesym.State=symstate_undefined then
  1745. begin
  1746. exesym.ObjSymbol:=objsym;
  1747. exesym.State:=symstate_common;
  1748. end;
  1749. CommonObjSymbols.add(objsym);
  1750. end;
  1751. end;
  1752. end;
  1753. end;
  1754. begin
  1755. VTEntryList:=TFPObjectList.Create(false);
  1756. VTInheritList:=TFPObjectList.Create(false);
  1757. {
  1758. The symbol resolving is done in 3 steps:
  1759. 1. Register symbols from objects
  1760. 2. Find symbols in static libraries
  1761. 3. Define stil undefined common symbols
  1762. }
  1763. { Step 1, Register symbols from objects }
  1764. for i:=0 to ObjDataList.Count-1 do
  1765. begin
  1766. ObjData:=TObjData(ObjDataList[i]);
  1767. LoadObjDataSymbols(ObjData);
  1768. end;
  1769. PackUnresolvedExeSymbols('in objects');
  1770. { Step 2, Find unresolved symbols in the libraries }
  1771. firstarchive:=true;
  1772. for i:=0 to StaticLibraryList.Count-1 do
  1773. begin
  1774. StaticLibrary:=TStaticLibrary(StaticLibraryList[i]);
  1775. { Process list of Unresolved External symbols, we need
  1776. to use a while loop because the list can be extended when
  1777. we load members from the library. }
  1778. j:=0;
  1779. while (j<UnresolvedExeSymbols.count) do
  1780. begin
  1781. exesym:=TExeSymbol(UnresolvedExeSymbols[j]);
  1782. { Check first if the symbol is still undefined }
  1783. if exesym.State=symstate_undefined then
  1784. begin
  1785. if StaticLibrary.ArReader.OpenFile(exesym.name) then
  1786. begin
  1787. if assigned(exemap) then
  1788. begin
  1789. if firstarchive then
  1790. begin
  1791. exemap.Add('');
  1792. exemap.Add('Archive member included because of file (symbol)');
  1793. exemap.Add('');
  1794. firstarchive:=false;
  1795. end;
  1796. exemap.Add(StaticLibrary.ArReader.FileName+' - '+
  1797. {exesym.ObjSymbol.ObjSection.FullName+}
  1798. '('+exesym.Name+')');
  1799. end;
  1800. objinput:=StaticLibrary.ObjInputClass.Create;
  1801. objdata:=objinput.newObjData(StaticLibrary.ArReader.FileName);
  1802. objinput.ReadObjData(StaticLibrary.ArReader,objdata);
  1803. objinput.free;
  1804. AddObjData(objdata);
  1805. LoadObjDataSymbols(objdata);
  1806. StaticLibrary.ArReader.CloseFile;
  1807. end;
  1808. end;
  1809. inc(j);
  1810. end;
  1811. end;
  1812. PackUnresolvedExeSymbols('after static libraries');
  1813. { Step 3, Match common symbols or add to the globals }
  1814. firstcommon:=true;
  1815. for i:=0 to CommonObjSymbols.count-1 do
  1816. begin
  1817. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1818. if objsym.exesymbol.State=symstate_defined then
  1819. begin
  1820. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  1821. Comment(V_Debug,'Size of common symbol '+objsym.name+' is different, expected '+tostr(objsym.size)+' got '+tostr(objsym.exesymbol.ObjSymbol.size));
  1822. end
  1823. else
  1824. begin
  1825. { allocate new objsymbol in .bss of *COMMON* and assign
  1826. it to the exesymbol }
  1827. if firstcommon then
  1828. begin
  1829. if assigned(exemap) then
  1830. exemap.AddCommonSymbolsHeader;
  1831. firstcommon:=false;
  1832. end;
  1833. internalObjData.setsection(commonObjSection);
  1834. internalObjData.allocalign(var_align(objsym.size));
  1835. commonsym:=internalObjData.symboldefine(objsym.name,AB_GLOBAL,AT_FUNCTION);
  1836. commonsym.size:=objsym.size;
  1837. internalObjData.alloc(objsym.size);
  1838. if assigned(exemap) then
  1839. exemap.AddCommonSymbol(commonsym);
  1840. { Assign to the exesymbol }
  1841. objsym.exesymbol.objsymbol:=commonsym;
  1842. objsym.exesymbol.state:=symstate_defined;
  1843. end;
  1844. end;
  1845. PackUnresolvedExeSymbols('after defining COMMON symbols');
  1846. { Find entry symbol and print in map }
  1847. exesym:=texesymbol(ExeSymbolList.Find(EntryName));
  1848. if assigned(exesym) then
  1849. begin
  1850. EntrySym:=exesym.ObjSymbol;
  1851. if assigned(exemap) then
  1852. begin
  1853. exemap.Add('');
  1854. exemap.Add('Entry symbol '+EntryName);
  1855. end;
  1856. end
  1857. else
  1858. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  1859. { Generate VTable tree }
  1860. if cs_link_opt_vtable in current_settings.globalswitches then
  1861. BuildVTableTree(VTInheritList,VTEntryList);
  1862. VTInheritList.Free;
  1863. VTEntryList.Free;
  1864. end;
  1865. procedure TExeOutput.GenerateDebugLink(const dbgname:string;dbgcrc:cardinal);
  1866. var
  1867. debuglink : array[0..1023] of byte;
  1868. len : longint;
  1869. objsec : TObjSection;
  1870. exesec : TExeSection;
  1871. begin
  1872. { From the gdb manual chapter 15. GDB Files:
  1873. * A filename, with any leading directory components removed, followed by a zero byte,
  1874. * zero to three bytes of padding, as needed to reach the next four-byte boundary within the section, and
  1875. * a four-byte CRC checksum, stored in the same endianness used for the executable file itself. The checksum is computed
  1876. on the debugging information file's full contents by the function given below, passing zero as the crc argument.
  1877. }
  1878. fillchar(debuglink,sizeof(debuglink),0);
  1879. len:=0;
  1880. move(dbgname[1],debuglink[len],length(dbgname));
  1881. inc(len,length(dbgname)+1);
  1882. len:=align(len,4);
  1883. if source_info.endian<>target_info.endian then
  1884. SwapEndian(dbgcrc);
  1885. move(dbgcrc,debuglink[len],sizeof(cardinal));
  1886. inc(len,4);
  1887. { Add section }
  1888. exesec:=FindExeSection(debuglinkname);
  1889. if not assigned(exesec) then
  1890. exesec:=CExeSection.create(ExeSectionList,debuglinkname);
  1891. exesec.SecOptions:=[oso_data,oso_keep];
  1892. exesec.SecAlign:=4;
  1893. objsec:=internalObjData.createsection(exesec.name,0,exesec.SecOptions);
  1894. internalObjData.writebytes(debuglink,len);
  1895. exesec.AddObjSection(objsec);
  1896. end;
  1897. procedure TExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  1898. begin
  1899. end;
  1900. procedure TExeOutput.PrintMemoryMap;
  1901. var
  1902. exesec : TExeSection;
  1903. objsec : TObjSection;
  1904. objsym : TObjSymbol;
  1905. i,j,k : longint;
  1906. begin
  1907. if not assigned(exemap) then
  1908. exit;
  1909. exemap.AddMemoryMapHeader(ImageBase);
  1910. for i:=0 to ExeSectionList.Count-1 do
  1911. begin
  1912. exesec:=TExeSection(ExeSectionList[i]);
  1913. exemap.AddMemoryMapExeSection(exesec);
  1914. for j:=0 to exesec.ObjSectionList.count-1 do
  1915. begin
  1916. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1917. exemap.AddMemoryMapObjectSection(objsec);
  1918. for k:=0 to objsec.ObjSymbolDefines.Count-1 do
  1919. begin
  1920. objsym:=TObjSymbol(objsec.ObjSymbolDefines[k]);
  1921. exemap.AddMemoryMapSymbol(objsym);
  1922. end;
  1923. end;
  1924. end;
  1925. end;
  1926. procedure TExeOutput.FixupSymbols;
  1927. procedure UpdateSymbol(objsym:TObjSymbol);
  1928. begin
  1929. objsym.bind:=objsym.ExeSymbol.ObjSymbol.bind;
  1930. objsym.offset:=objsym.ExeSymbol.ObjSymbol.offset;
  1931. objsym.size:=objsym.ExeSymbol.ObjSymbol.size;
  1932. objsym.typ:=objsym.ExeSymbol.ObjSymbol.typ;
  1933. objsym.ObjSection:=objsym.ExeSymbol.ObjSymbol.ObjSection;
  1934. end;
  1935. var
  1936. i : longint;
  1937. objsym : TObjSymbol;
  1938. exesym : TExeSymbol;
  1939. begin
  1940. { Print list of Unresolved External symbols }
  1941. for i:=0 to UnresolvedExeSymbols.count-1 do
  1942. begin
  1943. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1944. if exesym.State<>symstate_defined then
  1945. Comment(V_Error,'Undefined symbol: '+exesym.name);
  1946. end;
  1947. { Update ImageBase to ObjData so it can access from ObjSymbols }
  1948. for i:=0 to ObjDataList.Count-1 do
  1949. TObjData(ObjDataList[i]).imagebase:=imagebase;
  1950. {
  1951. Fixing up symbols is done in the following steps:
  1952. 1. Update common references
  1953. 2. Update external references
  1954. }
  1955. { Step 1, Update commons }
  1956. for i:=0 to CommonObjSymbols.count-1 do
  1957. begin
  1958. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1959. if objsym.bind<>AB_COMMON then
  1960. internalerror(200606241);
  1961. UpdateSymbol(objsym);
  1962. end;
  1963. { Step 2, Update externals }
  1964. for i:=0 to ExternalObjSymbols.count-1 do
  1965. begin
  1966. objsym:=TObjSymbol(ExternalObjSymbols[i]);
  1967. if objsym.bind<>AB_EXTERNAL then
  1968. internalerror(200606242);
  1969. UpdateSymbol(objsym);
  1970. end;
  1971. end;
  1972. procedure TExeOutput.MergeStabs;
  1973. var
  1974. stabexesec,
  1975. stabstrexesec : TExeSection;
  1976. relocsec,
  1977. currstabsec,
  1978. currstabstrsec,
  1979. mergedstabsec,
  1980. mergedstabstrsec : TObjSection;
  1981. hstabreloc,
  1982. currstabreloc : TObjRelocation;
  1983. i,j : longint;
  1984. currstabrelocidx,
  1985. mergestabcnt,
  1986. stabcnt : longword;
  1987. skipstab : boolean;
  1988. skipfun : boolean;
  1989. hstab : TObjStabEntry;
  1990. stabrelocofs : longword;
  1991. buf : array[0..1023] of byte;
  1992. bufend,
  1993. bufsize : longint;
  1994. begin
  1995. stabexesec:=FindExeSection('.stab');
  1996. stabstrexesec:=FindExeSection('.stabstr');
  1997. if (stabexesec=nil) or
  1998. (stabstrexesec=nil) or
  1999. (stabexesec.ObjSectionlist.count=0) then
  2000. exit;
  2001. { Create new stabsection }
  2002. stabRelocofs:=pbyte(@hstab.nvalue)-pbyte(@hstab);
  2003. mergedstabsec:=internalObjData.CreateSection(sec_stab,'');
  2004. mergedstabstrsec:=internalObjData.CreateSection(sec_stabstr,'');
  2005. { write stab for hdrsym }
  2006. fillchar(hstab,sizeof(TObjStabEntry),0);
  2007. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  2008. mergestabcnt:=1;
  2009. { .stabstr starts with a #0 }
  2010. buf[0]:=0;
  2011. mergedstabstrsec.write(buf[0],1);
  2012. skipfun:=false;
  2013. { Copy stabs and corresponding Relocations }
  2014. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  2015. begin
  2016. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  2017. currstabstrsec:=currstabsec.ObjData.findsection('.stabstr');
  2018. if assigned(currstabstrsec) then
  2019. begin
  2020. stabcnt:=currstabsec.Data.size div sizeof(TObjStabEntry);
  2021. currstabsec.Data.seek(0);
  2022. currstabrelocidx:=0;
  2023. for j:=0 to stabcnt-1 do
  2024. begin
  2025. hstabreloc:=nil;
  2026. skipstab:=false;
  2027. currstabsec.Data.read(hstab,sizeof(TObjStabEntry));
  2028. { Only include first hdrsym stab }
  2029. if hstab.ntype=0 then
  2030. skipstab:=true;
  2031. if skipfun then
  2032. begin
  2033. { Skip all stabs for function body until N_RBRAC }
  2034. skipfun:=hstab.ntype<>N_RBRAC;
  2035. skipstab:=true;
  2036. end;
  2037. if not skipstab then
  2038. begin
  2039. { Find corresponding Relocation }
  2040. currstabreloc:=nil;
  2041. while (currstabrelocidx<longword(currstabsec.ObjRelocations.Count)) do
  2042. begin
  2043. currstabreloc:=TObjRelocation(currstabsec.ObjRelocations[currstabrelocidx]);
  2044. if assigned(currstabreloc) and
  2045. (currstabreloc.dataoffset>=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2046. break;
  2047. inc(currstabrelocidx);
  2048. end;
  2049. if assigned(currstabreloc) and
  2050. (currstabreloc.dataoffset=longword(j)*sizeof(TObjStabEntry)+stabrelocofs) then
  2051. begin
  2052. hstabReloc:=currstabReloc;
  2053. inc(currstabrelocidx);
  2054. end;
  2055. { Check if the stab is refering to a removed section }
  2056. if assigned(hstabreloc) then
  2057. begin
  2058. if assigned(hstabreloc.Symbol) then
  2059. relocsec:=hstabreloc.Symbol.ObjSection
  2060. else
  2061. relocsec:=hstabreloc.ObjSection;
  2062. if not assigned(relocsec) then
  2063. internalerror(200603302);
  2064. if not relocsec.Used then
  2065. begin
  2066. skipstab:=true;
  2067. if (hstab.ntype=N_Function) and (hstab.strpos<>0) then
  2068. begin
  2069. currstabstrsec.Data.seek(hstab.strpos);
  2070. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2071. bufend:=indexbyte(buf,bufsize,Ord(':'));
  2072. if (bufend<>-1) and (bufend<bufsize-1) and (buf[bufend+1]=Ord('F')) then
  2073. skipfun:=true;
  2074. end;
  2075. end;
  2076. end;
  2077. end;
  2078. if not skipstab then
  2079. begin
  2080. { Copy string in stabstr }
  2081. if hstab.strpos<>0 then
  2082. begin
  2083. currstabstrsec.Data.seek(hstab.strpos);
  2084. hstab.strpos:=mergedstabstrsec.Size;
  2085. repeat
  2086. bufsize:=currstabstrsec.Data.read(buf,sizeof(buf));
  2087. bufend:=indexbyte(buf,bufsize,0);
  2088. if bufend=-1 then
  2089. bufend:=bufsize
  2090. else
  2091. begin
  2092. { include the #0 }
  2093. inc(bufend);
  2094. end;
  2095. mergedstabstrsec.write(buf,bufend);
  2096. until (buf[bufend-1]=0) or (bufsize<sizeof(buf));
  2097. end;
  2098. { Copy and Update the relocation }
  2099. if assigned(hstabreloc) then
  2100. begin
  2101. hstabreloc.Dataoffset:=mergestabcnt*sizeof(TObjStabEntry)+stabRelocofs;
  2102. { Remove from List without freeing the object }
  2103. currstabsec.ObjRelocations.List[currstabrelocidx-1]:=nil;
  2104. mergedstabsec.ObjRelocations.Add(hstabreloc);
  2105. end;
  2106. { Write updated stab }
  2107. mergedstabsec.write(hstab,sizeof(hstab));
  2108. inc(mergestabcnt);
  2109. end;
  2110. end;
  2111. end;
  2112. { Unload stabs }
  2113. if assigned(currstabstrsec) then
  2114. begin
  2115. currstabstrsec.Used:=False;
  2116. currstabstrsec.ReleaseData;
  2117. end;
  2118. currstabsec.Used:=false;
  2119. currstabsec.ReleaseData;
  2120. end;
  2121. { Generate new HdrSym }
  2122. if mergedstabsec.Size>0 then
  2123. begin
  2124. hstab.strpos:=1;
  2125. hstab.ntype:=0;
  2126. hstab.nother:=0;
  2127. hstab.ndesc:=word(mergestabcnt-1);
  2128. hstab.nvalue:=mergedstabstrsec.Size;
  2129. mergedstabsec.Data.seek(0);
  2130. mergedstabsec.Data.write(hstab,sizeof(hstab));
  2131. end;
  2132. { Replace all sections with our combined stabsec }
  2133. stabexesec.ObjSectionList.Clear;
  2134. stabstrexesec.ObjSectionList.Clear;
  2135. stabexesec.AddObjSection(mergedstabsec);
  2136. stabstrexesec.AddObjSection(mergedstabstrsec);
  2137. end;
  2138. procedure TExeOutput.RemoveEmptySections;
  2139. var
  2140. i, j : longint;
  2141. exesec : TExeSection;
  2142. doremove : boolean;
  2143. begin
  2144. for i:=0 to ExeSectionList.Count-1 do
  2145. begin
  2146. exesec:=TExeSection(ExeSectionList[i]);
  2147. doremove:=not(oso_keep in exesec.SecOptions) and
  2148. (
  2149. (exesec.ObjSectionlist.count=0) or
  2150. (
  2151. (cs_link_strip in current_settings.globalswitches) and
  2152. not(cs_link_separate_dbg_file in current_settings.globalswitches) and
  2153. (oso_debug in exesec.SecOptions)
  2154. )
  2155. );
  2156. if not doremove then
  2157. begin
  2158. { Check if section has no actual data }
  2159. doremove:=true;
  2160. for j:=0 to exesec.ObjSectionList.Count-1 do
  2161. if TObjSection(exesec.ObjSectionList[j]).Size<>0 then
  2162. begin
  2163. doremove:=false;
  2164. break;
  2165. end;
  2166. end;
  2167. if doremove and not (RelocSection and (exesec.Name='.reloc')) then
  2168. begin
  2169. Comment(V_Debug,'Deleting empty section '+exesec.name);
  2170. ExeSectionList[i]:=nil;
  2171. end;
  2172. end;
  2173. ExeSectionList.Pack;
  2174. end;
  2175. procedure TExeOutput.RemoveDebugInfo;
  2176. var
  2177. i : longint;
  2178. exesec : TExeSection;
  2179. begin
  2180. for i:=0 to ExeSectionList.Count-1 do
  2181. begin
  2182. exesec:=TExeSection(ExeSectionList[i]);
  2183. if (oso_debug in exesec.SecOptions) then
  2184. ExeSectionList[i]:=nil;
  2185. end;
  2186. ExeSectionList.Pack;
  2187. end;
  2188. procedure TExeOutput.RemoveUnreferencedSections;
  2189. var
  2190. ObjSectionWorkList : TFPObjectList;
  2191. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  2192. begin
  2193. if not aobjsec.Used then
  2194. begin
  2195. aobjsec.Used:=true;
  2196. ObjSectionWorkList.Add(aobjsec);
  2197. end;
  2198. end;
  2199. procedure DoReloc(objreloc:TObjRelocation);
  2200. var
  2201. objsym : TObjSymbol;
  2202. refobjsec : TObjSection;
  2203. begin
  2204. { Disabled Relocation to 0 }
  2205. if objreloc.typ=RELOC_ZERO then
  2206. exit;
  2207. if assigned(objreloc.symbol) then
  2208. begin
  2209. objsym:=objreloc.symbol;
  2210. if objsym.bind<>AB_LOCAL then
  2211. begin
  2212. if not(assigned(objsym.exesymbol) and
  2213. (objsym.exesymbol.State=symstate_defined)) then
  2214. internalerror(200603063);
  2215. objsym:=objsym.exesymbol.objsymbol;
  2216. end;
  2217. if not assigned(objsym.objsection) then
  2218. internalerror(200603062);
  2219. refobjsec:=objsym.objsection;
  2220. end
  2221. else
  2222. if assigned(objreloc.objsection) then
  2223. refobjsec:=objreloc.objsection
  2224. else
  2225. internalerror(200603316);
  2226. if assigned(exemap) then
  2227. begin
  2228. objsym:=objreloc.symbol;
  2229. if assigned(objsym) then
  2230. exemap.Add(' References '+objsym.name+' in '
  2231. +refobjsec.fullname)
  2232. else
  2233. exemap.Add(' References '+refobjsec.fullname);
  2234. end;
  2235. AddToObjSectionWorkList(refobjsec);
  2236. end;
  2237. procedure DoVTableRef(vtable:TExeVTable;VTableIdx:longint);
  2238. var
  2239. i : longint;
  2240. objreloc : TObjRelocation;
  2241. begin
  2242. objreloc:=vtable.VTableRef(VTableIdx);
  2243. if assigned(objreloc) then
  2244. begin
  2245. { Process the relocation now if the ObjSection is
  2246. already processed and marked as used. Otherwise we leave it
  2247. unprocessed. It'll then be resolved when the ObjSection is
  2248. changed to Used }
  2249. if vtable.ExeSymbol.ObjSymbol.ObjSection.Used then
  2250. DoReloc(objreloc);
  2251. end;
  2252. { This recursive walking is done here instead of
  2253. in TExeVTable.VTableRef because we can now process
  2254. all needed relocations }
  2255. for i:=0 to vtable.ChildList.Count-1 do
  2256. DoVTableRef(TExeVTable(vtable.ChildList[i]),VTableIdx);
  2257. end;
  2258. var
  2259. hs : string;
  2260. i,j,k : longint;
  2261. exesec : TExeSection;
  2262. objdata : TObjData;
  2263. objsec : TObjSection;
  2264. objsym : TObjSymbol;
  2265. code : integer;
  2266. vtableidx : longint;
  2267. vtableexesym : TExeSymbol;
  2268. begin
  2269. ObjSectionWorkList:=TFPObjectList.Create(false);
  2270. if assigned(exemap) then
  2271. exemap.AddHeader('Removing unreferenced sections');
  2272. { Initialize by marking all sections unused and
  2273. adding the sections with oso_keep flags to the ObjSectionWorkList }
  2274. for i:=0 to ObjDataList.Count-1 do
  2275. begin
  2276. ObjData:=TObjData(ObjDataList[i]);
  2277. for j:=0 to ObjData.ObjSectionList.Count-1 do
  2278. begin
  2279. objsec:=TObjSection(ObjData.ObjSectionList[j]);
  2280. objsec.Used:=false;
  2281. { TODO: remove debug section always keep}
  2282. if oso_debug in objsec.secoptions then
  2283. objsec.Used:=true;
  2284. if (oso_keep in objsec.secoptions) then
  2285. begin
  2286. AddToObjSectionWorkList(objsec);
  2287. if objsec.name='.fpc.n_links' then
  2288. objsec.Used:=false;
  2289. end;
  2290. end;
  2291. end;
  2292. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  2293. { Process all sections, add new sections to process based
  2294. on the symbol references }
  2295. while ObjSectionWorkList.Count>0 do
  2296. begin
  2297. objsec:=TObjSection(ObjSectionWorkList.Last);
  2298. if assigned(exemap) then
  2299. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjRelocations.Count)+' references');
  2300. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  2301. { Process Relocations }
  2302. for i:=0 to objsec.ObjRelocations.count-1 do
  2303. DoReloc(TObjRelocation(objsec.ObjRelocations[i]));
  2304. { Process Virtual Entry calls }
  2305. if cs_link_opt_vtable in current_settings.globalswitches then
  2306. begin
  2307. for i:=0 to objsec.VTRefList.count-1 do
  2308. begin
  2309. objsym:=TObjSymbol(objsec.VTRefList[i]);
  2310. hs:=objsym.name;
  2311. Delete(hs,1,Pos('_',hs));
  2312. k:=Pos('$$',hs);
  2313. if k=0 then
  2314. internalerror(200603314);
  2315. vtableexesym:=texesymbol(FExeSymbolList.Find(Copy(hs,1,k-1)));
  2316. val(Copy(hs,k+2,length(hs)-k-1),vtableidx,code);
  2317. if (code<>0) then
  2318. internalerror(200603317);
  2319. if not assigned(vtableexesym) then
  2320. internalerror(200603315);
  2321. if not assigned(vtableexesym.vtable) then
  2322. internalerror(200603316);
  2323. DoVTableRef(vtableexesym.vtable,vtableidx);
  2324. end;
  2325. end;
  2326. end;
  2327. ObjSectionWorkList.Free;
  2328. ObjSectionWorkList:=nil;
  2329. { Remove unused objsections from ExeSectionList }
  2330. for i:=0 to ExeSectionList.Count-1 do
  2331. begin
  2332. exesec:=TExeSection(ExeSectionList[i]);
  2333. for j:=0 to exesec.ObjSectionlist.count-1 do
  2334. begin
  2335. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2336. if not objsec.used then
  2337. begin
  2338. if assigned(exemap) then
  2339. exemap.Add('Removing '+objsec.FullName);
  2340. exesec.ObjSectionlist[j]:=nil;
  2341. objsec.ReleaseData;
  2342. end;
  2343. end;
  2344. exesec.ObjSectionlist.Pack;
  2345. end;
  2346. end;
  2347. procedure TExeOutput.FixupRelocations;
  2348. var
  2349. i,j : longint;
  2350. exesec : TExeSection;
  2351. objsec : TObjSection;
  2352. begin
  2353. for i:=0 to ExeSectionList.Count-1 do
  2354. begin
  2355. exesec:=TExeSection(ExeSectionList[i]);
  2356. if not assigned(exesec) then
  2357. continue;
  2358. for j:=0 to exesec.ObjSectionlist.count-1 do
  2359. begin
  2360. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  2361. if not objsec.Used then
  2362. internalerror(200603301);
  2363. objsec.FixupRelocs;
  2364. end;
  2365. end;
  2366. end;
  2367. procedure TExeOutput.SetCurrMemPos(const AValue: qword);
  2368. begin
  2369. if AValue>MaxMemPos then
  2370. Message1(link_f_executable_too_big, target_os_string);
  2371. FCurrMemPos:=AValue;
  2372. end;
  2373. {****************************************************************************
  2374. TObjInput
  2375. ****************************************************************************}
  2376. constructor TObjInput.create;
  2377. begin
  2378. end;
  2379. destructor TObjInput.destroy;
  2380. begin
  2381. inherited destroy;
  2382. end;
  2383. function TObjInput.newObjData(const n:string):TObjData;
  2384. begin
  2385. result:=CObjData.create(n);
  2386. end;
  2387. procedure TObjInput.inputerror(const s : string);
  2388. begin
  2389. Comment(V_Error,s+' while reading '+InputFileName);
  2390. end;
  2391. {$ifdef MEMDEBUG}
  2392. initialization
  2393. memobjsymbols:=TMemDebug.create('ObjSymbols');
  2394. memobjsymbols.stop;
  2395. memobjsections:=TMemDebug.create('ObjSections');
  2396. memobjsections.stop;
  2397. finalization
  2398. memobjsymbols.free;
  2399. memobjsections.free;
  2400. {$endif MEMDEBUG}
  2401. end.