ogbase.pas 79 KB

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