ogbase.pas 89 KB

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