2
0

ogbase.pas 71 KB

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