ogbase.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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 = (RELOC_ABSOLUTE,RELOC_RELATIVE,RELOC_RVA
  35. {$ifdef x86_64}
  36. ,RELOC_ABSOLUTE32
  37. {$endif x86_64}
  38. );
  39. TObjSectionOption = (
  40. { Has data available in the file }
  41. oso_data,
  42. { Is loaded into memory }
  43. oso_load,
  44. { Not loaded into memory }
  45. oso_noload,
  46. { Read only }
  47. oso_readonly,
  48. { Read/Write }
  49. oso_write,
  50. { Contains executable instructions }
  51. oso_executable,
  52. { Never discard section }
  53. oso_keep,
  54. { Special common symbols }
  55. oso_common,
  56. { Contains debug info and can be stripped }
  57. oso_debug,
  58. { Contains only strings }
  59. oso_strings
  60. );
  61. TObjSectionOptions = set of TObjSectionOption;
  62. TObjSymbol = class(TNamedIndexItem)
  63. public
  64. bind : TAsmsymbind;
  65. typ : TAsmsymtype;
  66. { Current assemble pass, used to detect duplicate labels }
  67. pass : byte;
  68. objsection : TObjSection;
  69. symidx : longint;
  70. offset,
  71. size : aint;
  72. { Used for external and common solving during linking }
  73. exesymbol : TExeSymbol;
  74. constructor create(const s:string);
  75. function address:aint;
  76. procedure SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  77. end;
  78. { Stabs is common for all targets }
  79. TObjStabEntry=packed record
  80. strpos : longint;
  81. ntype : byte;
  82. nother : byte;
  83. ndesc : word;
  84. nvalue : longint;
  85. end;
  86. PObjStabEntry=^TObjStabEntry;
  87. TObjRelocation = class(TLinkedListItem)
  88. DataOffset,
  89. orgsize : aint; { original size of the symbol to relocate, required for COFF }
  90. symbol : TObjSymbol;
  91. objsection : TObjSection; { only used if symbol=nil }
  92. typ : TObjRelocationType;
  93. constructor CreateSymbol(ADataOffset:aint;s:TObjSymbol;Atyp:TObjRelocationType);
  94. constructor CreateSymbolSize(ADataOffset:aint;s:TObjSymbol;Aorgsize:aint;Atyp:TObjRelocationType);
  95. constructor CreateSection(ADataOffset:aint;aobjsec:TObjSection;Atyp:TObjRelocationType);
  96. end;
  97. TObjSection = class(TNamedIndexItem)
  98. private
  99. FData : TDynamicArray;
  100. FSecOptions : TObjSectionOptions;
  101. procedure SetSecOptions(Aoptions:TObjSectionOptions);
  102. public
  103. ObjData : TObjData;
  104. SecSymIdx : longint; { index for the section in symtab }
  105. SecAlign : shortint; { alignment of the section }
  106. { section data }
  107. Size,
  108. DataPos,
  109. MemPos : aint;
  110. DataAlignBytes : shortint;
  111. { relocation }
  112. relocations : TLinkedList;
  113. { Symbols this section references and defines }
  114. ObjSymbolRefs : TFPObjectList;
  115. ObjSymbolDefines : TFPObjectList;
  116. { executable linking }
  117. ExeSection : TExeSection;
  118. Used : boolean;
  119. constructor create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);virtual;
  120. destructor destroy;override;
  121. function write(const d;l:aint):aint;
  122. function writestr(const s:string):aint;
  123. function WriteZeros(l:longint):aint;
  124. procedure setmempos(var mpos:aint);
  125. procedure setdatapos(var dpos:aint);
  126. procedure alloc(l:aint);
  127. procedure addsymreloc(ofs:aint;p:TObjSymbol;reloctype:TObjRelocationType);
  128. procedure addsectionreloc(ofs:aint;aobjsec:TObjSection;reloctype:TObjRelocationType);
  129. procedure AddSymbolDefine(p:TObjSymbol);
  130. procedure AddSymbolRef(p:TObjSymbol);
  131. procedure fixuprelocs;virtual;
  132. function FullName:string;
  133. property Data:TDynamicArray read FData;
  134. property SecOptions:TObjSectionOptions read FSecOptions write SetSecOptions;
  135. end;
  136. TObjSectionClass = class of TObjSection;
  137. TObjData = class(TLinkedListItem)
  138. private
  139. FName : string[80];
  140. FCurrObjSec : TObjSection;
  141. { ObjSections will be stored in order in SectsIndex, this is at least
  142. required for stabs debuginfo. The SectsDict is only used for lookups (PFV) }
  143. FObjSectionDict : TDictionary;
  144. FObjSectionList : TFPObjectList;
  145. FCObjSection : TObjSectionClass;
  146. { Symbols that will be defined in this object file }
  147. FObjSymbolList : TFPObjectList;
  148. FObjSymbolDict : TDictionary;
  149. FCachedAsmSymbolList : TFPObjectList;
  150. { Special info sections that are written to during object generation }
  151. FStabsObjSec,
  152. FStabStrObjSec : TObjSection;
  153. procedure section_reset(p:TObject;arg:pointer);
  154. procedure section_afteralloc(p:TObject;arg:pointer);
  155. procedure section_afterwrite(p:TObject;arg:pointer);
  156. procedure section_fixuprelocs(p:TObject;arg:pointer);
  157. protected
  158. property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
  159. property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
  160. property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
  161. public
  162. CurrPass : byte;
  163. ImageBase : aint;
  164. constructor create(const n:string);virtual;
  165. destructor destroy;override;
  166. { Sections }
  167. function sectionname(atype:TAsmSectiontype;const aname:string):string;virtual;
  168. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
  169. function sectiontype2align(atype:TAsmSectiontype):shortint;virtual;
  170. function createsection(atype:TAsmSectionType;const aname:string):TObjSection;
  171. function createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions):TObjSection;virtual;
  172. procedure CreateDebugSections;virtual;
  173. function findsection(const aname:string):TObjSection;
  174. procedure removesection(asec:TObjSection);
  175. procedure setsection(asec:TObjSection);
  176. { Symbols }
  177. function createsymbol(const aname:string):TObjSymbol;
  178. function symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  179. function symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  180. function symbolref(asmsym:TAsmSymbol):TObjSymbol;
  181. function symbolref(const aname:string):TObjSymbol;
  182. procedure ResetCachedAsmSymbols;
  183. { Allocation }
  184. procedure alloc(len:aint);
  185. procedure allocalign(len:shortint);
  186. procedure allocstab(p:pchar);
  187. procedure writebytes(const data;len:aint);
  188. procedure writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);virtual;abstract;
  189. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);virtual;abstract;
  190. procedure beforealloc;virtual;
  191. procedure beforewrite;virtual;
  192. procedure afteralloc;virtual;
  193. procedure afterwrite;virtual;
  194. procedure resetsections;
  195. procedure fixuprelocs;
  196. property Name:string[80] read FName;
  197. property CurrObjSec:TObjSection read FCurrObjSec;
  198. property ObjSymbolList:TFPObjectList read FObjSymbolList;
  199. property ObjSectionList:TFPObjectList read FObjSectionList;
  200. end;
  201. TObjDataClass = class of TObjData;
  202. TObjOutput = class
  203. private
  204. FCObjData : TObjDataClass;
  205. protected
  206. { writer }
  207. FWriter : TObjectwriter;
  208. function writedata(data:TObjData):boolean;virtual;abstract;
  209. property CObjData : TObjDataClass read FCObjData write FCObjData;
  210. public
  211. constructor create(AWriter:TObjectWriter);virtual;
  212. destructor destroy;override;
  213. function newObjData(const n:string):TObjData;
  214. function startObjectfile(const fn:string):boolean;
  215. function writeobjectfile(data:TObjData):boolean;
  216. procedure exportsymbol(p:TObjSymbol);
  217. property Writer:TObjectWriter read FWriter;
  218. end;
  219. TObjOutputClass=class of TObjOutput;
  220. TObjInput = class
  221. private
  222. FCObjData : TObjDataClass;
  223. protected
  224. { reader }
  225. FReader : TObjectreader;
  226. function readObjData(data:TObjData):boolean;virtual;abstract;
  227. property CObjData : TObjDataClass read FCObjData write FCObjData;
  228. public
  229. constructor create;virtual;
  230. destructor destroy;override;
  231. function newObjData(const n:string):TObjData;
  232. function readobjectfile(const fn:string;data:TObjData):boolean;virtual;
  233. property Reader:TObjectReader read FReader;
  234. end;
  235. TObjInputClass=class of TObjInput;
  236. TExeSymbol = class(TNamedIndexItem)
  237. ObjSymbol : TObjSymbol;
  238. ExeSection : TExeSection;
  239. constructor create(sym:TObjSymbol);
  240. end;
  241. TExeSection = class(tnamedindexitem)
  242. private
  243. FSecSymIdx : longint;
  244. FObjSectionList : TFPObjectList;
  245. public
  246. Size,
  247. DataPos,
  248. MemPos : aint;
  249. SecAlign : shortint;
  250. SecOptions : TObjSectionOptions;
  251. constructor create(const n:string);virtual;
  252. destructor destroy;override;
  253. procedure AddObjSection(objsec:TObjSection);
  254. property ObjSectionList:TFPObjectList read FObjSectionList;
  255. property SecSymIdx:longint read FSecSymIdx write FSecSymIdx;
  256. end;
  257. TExeSectionClass=class of TExeSection;
  258. TExeOutput = class
  259. private
  260. { ExeSections }
  261. FCObjData : TObjDataClass;
  262. FCExeSection : TExeSectionClass;
  263. FCurrExeSec : TExeSection;
  264. FExeSectionList : TFPObjectList;
  265. FExeSectionDict : TDictionary;
  266. Fzeronr : longint;
  267. { Symbols }
  268. FExeSymbolDict : TDictionary;
  269. FExeSymbolList,
  270. FUnresolvedExeSymbols : TFPObjectList;
  271. FExternalObjSymbols,
  272. FCommonObjSymbols : TFPObjectList;
  273. FEntryName : string;
  274. { Objects }
  275. FObjDataList : TFPObjectList;
  276. { Position calculation }
  277. FImageBase : aint;
  278. FCurrDataPos,
  279. FCurrMemPos : aint;
  280. protected
  281. { writer }
  282. FWriter : TObjectwriter;
  283. commonObjSection : TObjSection;
  284. internalobjdata : TObjData;
  285. EntrySym : TObjSymbol;
  286. SectionDataAlign,
  287. SectionMemAlign : aint;
  288. function writedata:boolean;virtual;abstract;
  289. property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
  290. property CObjData:TObjDataClass read FCObjData write FCObjData;
  291. public
  292. constructor create;virtual;
  293. destructor destroy;override;
  294. procedure AddObjData(objdata:TObjData);
  295. function FindExeSection(const aname:string):TExeSection;
  296. procedure Load_Start;virtual;
  297. procedure Load_EntryName(const aname:string);virtual;
  298. procedure Load_Symbol(const aname:string);virtual;
  299. procedure Order_Start;virtual;
  300. procedure Order_End;virtual;
  301. procedure Order_ExeSection(const aname:string);virtual;
  302. procedure Order_Align(const aname:string);virtual;
  303. procedure Order_Zeros(const aname:string);virtual;
  304. procedure Order_Symbol(const aname:string);virtual;
  305. procedure Order_EndExeSection;virtual;
  306. procedure Order_Stabs;
  307. procedure Order_ObjSection(const aname:string);virtual;
  308. procedure CalcPos_ExeSection(const aname:string);virtual;
  309. procedure CalcPos_EndExeSection;virtual;
  310. procedure CalcPos_Header;virtual;
  311. procedure CalcPos_Start;virtual;
  312. procedure CalcPos_Symbols;virtual;
  313. procedure ResolveSymbols;
  314. procedure PrintMemoryMap;
  315. procedure FixUpSymbols;
  316. procedure FixUpRelocations;
  317. procedure RemoveUnreferencedSections;
  318. procedure RemoveEmptySections;
  319. procedure ResolveExternals(const libname:string);virtual;
  320. function writeexefile(const fn:string):boolean;
  321. property Writer:TObjectWriter read FWriter;
  322. property ExeSections:TFPObjectList read FExeSectionList;
  323. property ObjDataList:TFPObjectList read FObjDataList;
  324. property ExeSymbolDict:TDictionary read FExeSymbolDict;
  325. property ExeSymbolList:TFPObjectList read FExeSymbolList;
  326. property UnresolvedExeSymbols:TFPObjectList read FUnresolvedExeSymbols;
  327. property ExternalObjSymbols:TFPObjectList read FExternalObjSymbols;
  328. property CommonObjSymbols:TFPObjectList read FCommonObjSymbols;
  329. property EntryName:string read FEntryName write FEntryName;
  330. property ImageBase:aint read FImageBase write FImageBase;
  331. property CurrExeSec:TExeSection read FCurrExeSec;
  332. property CurrDataPos:aint read FCurrDataPos write FCurrDataPos;
  333. property CurrMemPos:aint read FCurrMemPos write FCurrMemPos;
  334. end;
  335. TExeOutputClass=class of TExeOutput;
  336. {$ifdef i386}
  337. const
  338. RELOC_ABSOLUTE32 = RELOC_ABSOLUTE;
  339. {$endif i386}
  340. var
  341. exeoutput : TExeOutput;
  342. implementation
  343. uses
  344. cutils,globals,verbose,fmodule,ogmap;
  345. const
  346. sectiondatagrowsize = 1024;
  347. {*****************************************************************************
  348. TObjSymbol
  349. *****************************************************************************}
  350. constructor TObjSymbol.create(const s:string);
  351. begin;
  352. inherited createname(s);
  353. bind:=AB_EXTERNAL;
  354. typ:=AT_NONE;
  355. symidx:=-1;
  356. size:=0;
  357. offset:=0;
  358. objsection:=nil;
  359. end;
  360. function TObjSymbol.address:aint;
  361. begin
  362. if assigned(objsection) then
  363. result:=offset+objsection.mempos
  364. else
  365. result:=0;
  366. end;
  367. procedure TObjSymbol.SetAddress(apass:byte;aobjsec:TObjSection;abind:TAsmsymbind;atyp:Tasmsymtype);
  368. begin
  369. if not(abind in [AB_GLOBAL,AB_LOCAL,AB_COMMON]) then
  370. internalerror(200603016);
  371. if not assigned(aobjsec) then
  372. internalerror(200603017);
  373. if (bind=AB_EXTERNAL) then
  374. begin
  375. bind:=abind;
  376. typ:=atyp;
  377. end
  378. else
  379. begin
  380. if pass=apass then
  381. Message1(asmw_e_duplicate_label,name);
  382. end;
  383. pass:=apass;
  384. { Code can never grow after a pass }
  385. if assigned(objsection) and
  386. (aobjsec.size>offset) then
  387. internalerror(200603014);
  388. objsection:=aobjsec;
  389. offset:=aobjsec.size;
  390. end;
  391. {****************************************************************************
  392. TObjRelocation
  393. ****************************************************************************}
  394. constructor TObjRelocation.CreateSymbol(ADataOffset:aint;s:TObjSymbol;Atyp:TObjRelocationType);
  395. begin
  396. if not assigned(s) then
  397. internalerror(200603034);
  398. DataOffset:=ADataOffset;
  399. Symbol:=s;
  400. OrgSize:=0;
  401. ObjSection:=nil;
  402. Typ:=Atyp;
  403. end;
  404. constructor TObjRelocation.CreateSymbolSize(ADataOffset:aint;s:TObjSymbol;Aorgsize:aint;Atyp:TObjRelocationType);
  405. begin
  406. if not assigned(s) then
  407. internalerror(200603035);
  408. DataOffset:=ADataOffset;
  409. Symbol:=s;
  410. OrgSize:=Aorgsize;
  411. ObjSection:=nil;
  412. Typ:=Atyp;
  413. end;
  414. constructor TObjRelocation.CreateSection(ADataOffset:aint;aobjsec:TObjSection;Atyp:TObjRelocationType);
  415. begin
  416. if not assigned(aobjsec) then
  417. internalerror(200603036);
  418. DataOffset:=ADataOffset;
  419. Symbol:=nil;
  420. OrgSize:=0;
  421. ObjSection:=aobjsec;
  422. Typ:=Atyp;
  423. end;
  424. {****************************************************************************
  425. TObjSection
  426. ****************************************************************************}
  427. constructor TObjSection.create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  428. begin
  429. inherited createname(Aname);
  430. name:=Aname;
  431. { data }
  432. Size:=0;
  433. datapos:=0;
  434. mempos:=0;
  435. FData:=Nil;
  436. { Setting the secoptions allocates Data if needed }
  437. secoptions:=Aoptions;
  438. secalign:=Aalign;
  439. secsymidx:=0;
  440. { relocation }
  441. relocations:=TLinkedList.Create;
  442. ObjSymbolRefs:=TFPObjectList.Create(false);
  443. ObjSymbolDefines:=TFPObjectList.Create(false);
  444. end;
  445. destructor TObjSection.destroy;
  446. begin
  447. if assigned(Data) then
  448. Data.Free;
  449. relocations.free;
  450. ObjSymbolRefs.Free;
  451. ObjSymbolDefines.Free;
  452. inherited destroy;
  453. end;
  454. procedure TObjSection.SetSecOptions(Aoptions:TObjSectionOptions);
  455. begin
  456. FSecOptions:=FSecOptions+AOptions;
  457. if (oso_data in secoptions) and
  458. not assigned(FData) then
  459. FData:=TDynamicArray.Create(sectiondatagrowsize);
  460. end;
  461. function TObjSection.write(const d;l:aint):aint;
  462. begin
  463. result:=size;
  464. if assigned(Data) then
  465. begin
  466. if Size<>data.size then
  467. internalerror(200602281);
  468. Data.write(d,l);
  469. inc(Size,l);
  470. end
  471. else
  472. internalerror(200602289);
  473. end;
  474. function TObjSection.writestr(const s:string):aint;
  475. begin
  476. result:=Write(s[1],length(s));
  477. end;
  478. function TObjSection.WriteZeros(l:longint):aint;
  479. var
  480. empty : array[0..1023] of byte;
  481. begin
  482. if l>sizeof(empty) then
  483. internalerror(200404082);
  484. if l>0 then
  485. begin
  486. fillchar(empty,l,0);
  487. result:=Write(empty,l);
  488. end
  489. else
  490. result:=Size;
  491. end;
  492. procedure TObjSection.setdatapos(var dpos:aint);
  493. begin
  494. if oso_data in secoptions then
  495. begin
  496. { get aligned datapos }
  497. datapos:=align(dpos,secalign);
  498. dataalignbytes:=datapos-dpos;
  499. { return updated datapos }
  500. dpos:=datapos+size;
  501. end
  502. else
  503. datapos:=dpos;
  504. end;
  505. procedure TObjSection.setmempos(var mpos:aint);
  506. begin
  507. mempos:=align(mpos,secalign);
  508. { return updated mempos }
  509. mpos:=mempos+size;
  510. end;
  511. procedure TObjSection.alloc(l:aint);
  512. begin
  513. inc(size,l);
  514. end;
  515. procedure TObjSection.addsymreloc(ofs:aint;p:TObjSymbol;reloctype:TObjRelocationType);
  516. begin
  517. relocations.concat(TObjRelocation.CreateSymbol(ofs,p,reloctype));
  518. end;
  519. procedure TObjSection.addsectionreloc(ofs:aint;aobjsec:TObjSection;reloctype:TObjRelocationType);
  520. begin
  521. relocations.concat(TObjRelocation.CreateSection(ofs,aobjsec,reloctype));
  522. end;
  523. procedure TObjSection.AddSymbolDefine(p:TObjSymbol);
  524. begin
  525. if p.bind<>AB_GLOBAL then
  526. exit;
  527. ObjSymbolDefines.Add(p);
  528. end;
  529. procedure TObjSection.AddSymbolRef(p:TObjSymbol);
  530. begin
  531. { Register all references, also the local references between the
  532. ObjSections in an ObjData }
  533. ObjSymbolRefs.Add(p);
  534. end;
  535. procedure TObjSection.fixuprelocs;
  536. begin
  537. end;
  538. function TObjSection.FullName:string;
  539. begin
  540. if assigned(objdata) then
  541. result:=objdata.Name+'('+Name+')'
  542. else
  543. result:=Name;
  544. end;
  545. {****************************************************************************
  546. TObjData
  547. ****************************************************************************}
  548. constructor TObjData.create(const n:string);
  549. begin
  550. inherited create;
  551. FName:=SplitFileName(n);
  552. { sections, the SectsIndex owns the items, the FObjSectionDict
  553. is only used for lookups }
  554. FObjSectionDict:=tdictionary.create;
  555. FObjSectionDict.noclear:=true;
  556. FObjSectionList:=TFPObjectList.Create(true);
  557. FStabsObjSec:=nil;
  558. FStabStrObjSec:=nil;
  559. { symbols }
  560. FObjSymbolDict:=tdictionary.create;
  561. FObjSymbolDict.noclear:=true;
  562. FObjSymbolList:=TFPObjectList.Create(true);
  563. FCachedAsmSymbolList:=TFPObjectList.Create(false);
  564. { section class type for creating of new sections }
  565. FCObjSection:=TObjSection;
  566. end;
  567. destructor TObjData.destroy;
  568. {$ifdef MEMDEBUG}
  569. var
  570. d : tmemdebug;
  571. {$endif}
  572. begin
  573. {$ifdef MEMDEBUG}
  574. d:=tmemdebug.create(name+' - objdata symbols');
  575. {$endif}
  576. ResetCachedAsmSymbols;
  577. FCachedAsmSymbolList.free;
  578. FObjSymbolDict.free;
  579. FObjSymbolList.free;
  580. {$ifdef MEMDEBUG}
  581. d.free;
  582. {$endif}
  583. {$ifdef MEMDEBUG}
  584. d:=tmemdebug.create(name+' - objdata sections');
  585. {$endif}
  586. FObjSectionDict.free;
  587. FObjSectionList.free;
  588. {$ifdef MEMDEBUG}
  589. d.free;
  590. {$endif}
  591. inherited destroy;
  592. end;
  593. function TObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
  594. const
  595. secnames : array[TAsmSectiontype] of string[16] = ('',
  596. 'code',
  597. 'data',
  598. 'rodata',
  599. 'bss',
  600. 'threadvar',
  601. 'stub',
  602. 'stab','stabstr',
  603. 'idata2','idata4','idata5','idata6','idata7','edata',
  604. 'eh_frame',
  605. 'debug_frame','debug_info','debug_line','debug_abbrev',
  606. 'fpc',
  607. 'toc'
  608. );
  609. begin
  610. if aname<>'' then
  611. result:=secnames[atype]+'.'+aname
  612. else
  613. result:=secnames[atype];
  614. end;
  615. function TObjData.sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;
  616. const
  617. secoptions : array[TAsmSectiontype] of TObjSectionOptions = ([],
  618. {code} [oso_data,oso_load,oso_readonly,oso_executable,oso_keep],
  619. {data} [oso_data,oso_load,oso_write,oso_keep],
  620. {$warning TODO Fix rodata be read-only}
  621. {rodata} [oso_data,oso_load,oso_write,oso_keep],
  622. {bss} [oso_load,oso_write,oso_keep],
  623. {threadvar} [oso_load,oso_write],
  624. {stub} [oso_data,oso_load,oso_readonly,oso_executable],
  625. {stab} [oso_data,oso_noload,oso_debug],
  626. {stabstr} [oso_data,oso_noload,oso_strings,oso_debug],
  627. {$warning TODO idata keep can maybe replaced with grouping of text and idata}
  628. {idata2} [oso_data,oso_load,oso_write,oso_keep],
  629. {idata4} [oso_data,oso_load,oso_write,oso_keep],
  630. {idata5} [oso_data,oso_load,oso_write,oso_keep],
  631. {idata6} [oso_data,oso_load,oso_write,oso_keep],
  632. {idata7} [oso_data,oso_load,oso_write,oso_keep],
  633. {edata} [oso_data,oso_load,oso_readonly],
  634. {eh_frame} [oso_data,oso_load,oso_readonly],
  635. {debug_frame} [oso_data,oso_noload,oso_debug],
  636. {debug_info} [oso_data,oso_noload,oso_debug],
  637. {debug_line} [oso_data,oso_noload,oso_debug],
  638. {debug_abbrev} [oso_data,oso_noload,oso_debug],
  639. {fpc} [oso_data,oso_load,oso_write,oso_keep],
  640. {toc} [oso_data,oso_load,oso_readonly]
  641. );
  642. begin
  643. result:=secoptions[atype];
  644. end;
  645. function TObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  646. begin
  647. if atype in [sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev] then
  648. result:=1
  649. else
  650. result:=sizeof(aint);
  651. end;
  652. function TObjData.createsection(atype:TAsmSectionType;const aname:string):TObjSection;
  653. begin
  654. result:=createsection(sectionname(atype,aname),sectiontype2align(atype),sectiontype2options(atype));
  655. end;
  656. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions):TObjSection;
  657. begin
  658. result:=TObjSection(FObjSectionDict.search(aname));
  659. if not assigned(result) then
  660. begin
  661. result:=CObjSection.create(aname,aalign,aoptions);
  662. FObjSectionDict.Insert(result);
  663. FObjSectionList.Add(result);
  664. result.ObjData:=self;
  665. end;
  666. FCurrObjSec:=result;
  667. end;
  668. procedure TObjData.CreateDebugSections;
  669. begin
  670. end;
  671. function TObjData.FindSection(const aname:string):TObjSection;
  672. begin
  673. result:=TObjSection(FObjSectionDict.Search(aname));
  674. end;
  675. procedure TObjData.removesection(asec:TObjSection);
  676. begin
  677. FObjSectionDict.Delete(asec.name);
  678. FObjSectionList.Remove(asec);
  679. end;
  680. procedure TObjData.setsection(asec:TObjSection);
  681. begin
  682. if asec.ObjData<>self then
  683. internalerror(200403041);
  684. FCurrObjSec:=asec;
  685. end;
  686. function TObjData.createsymbol(const aname:string):TObjSymbol;
  687. begin
  688. result:=TObjSymbol(FObjSymbolDict.search(aname));
  689. if not assigned(result) then
  690. begin
  691. result:=TObjSymbol.Create(aname);
  692. FObjSymbolDict.Insert(result);
  693. FObjSymbolList.Add(result);
  694. end;
  695. end;
  696. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  697. begin
  698. if assigned(asmsym) then
  699. begin
  700. if not assigned(asmsym.cachedObjSymbol) then
  701. begin
  702. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  703. asmsym.cachedObjSymbol:=result;
  704. FCachedAsmSymbolList.add(asmsym);
  705. end
  706. else
  707. begin
  708. result:=TObjSymbol(asmsym.cachedObjSymbol);
  709. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  710. { Register also in TObjSection }
  711. CurrObjSec.AddSymbolDefine(result);
  712. end;
  713. end
  714. else
  715. result:=nil;
  716. end;
  717. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  718. begin
  719. if not assigned(CurrObjSec) then
  720. internalerror(200603051);
  721. result:=CreateSymbol(aname);
  722. { Register also in TObjSection }
  723. CurrObjSec.AddSymbolDefine(result);
  724. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  725. end;
  726. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  727. begin
  728. if assigned(asmsym) then
  729. begin
  730. if not assigned(asmsym.cachedObjSymbol) then
  731. begin
  732. result:=symbolref(asmsym.name);
  733. asmsym.cachedObjSymbol:=result;
  734. FCachedAsmSymbolList.add(asmsym);
  735. end
  736. else
  737. begin
  738. result:=TObjSymbol(asmsym.cachedObjSymbol);
  739. { Register also in TObjSection }
  740. CurrObjSec.AddSymbolRef(result);
  741. end;
  742. end
  743. else
  744. result:=nil;
  745. end;
  746. function TObjData.symbolref(const aname:string):TObjSymbol;
  747. begin
  748. if not assigned(CurrObjSec) then
  749. internalerror(200603052);
  750. result:=CreateSymbol(aname);
  751. { Register also in TObjSection }
  752. CurrObjSec.AddSymbolRef(result);
  753. end;
  754. procedure TObjData.ResetCachedAsmSymbols;
  755. var
  756. i : longint;
  757. begin
  758. for i:=0 to FCachedAsmSymbolList.Count-1 do
  759. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  760. FCachedAsmSymbolList.Clear;
  761. end;
  762. procedure TObjData.writebytes(const data;len:aint);
  763. begin
  764. if not assigned(CurrObjSec) then
  765. internalerror(200402251);
  766. CurrObjSec.write(data,len);
  767. end;
  768. procedure TObjData.alloc(len:aint);
  769. begin
  770. if not assigned(CurrObjSec) then
  771. internalerror(200402252);
  772. CurrObjSec.alloc(len);
  773. end;
  774. procedure TObjData.allocalign(len:shortint);
  775. begin
  776. if not assigned(CurrObjSec) then
  777. internalerror(200402253);
  778. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  779. end;
  780. procedure TObjData.allocstab(p:pchar);
  781. begin
  782. if not(assigned(FStabsObjSec) and assigned(FStabStrObjSec)) then
  783. internalerror(200402254);
  784. FStabsObjSec.alloc(sizeof(TObjStabEntry));
  785. if assigned(p) and (p[0]<>#0) then
  786. FStabStrObjSec.alloc(strlen(p)+1);
  787. end;
  788. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  789. begin
  790. with TObjSection(p) do
  791. alloc(align(size,secalign)-size);
  792. end;
  793. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  794. begin
  795. with TObjSection(p) do
  796. begin
  797. if assigned(data) then
  798. writezeros(align(size,secalign)-size);
  799. end;
  800. end;
  801. procedure TObjData.section_reset(p:TObject;arg:pointer);
  802. begin
  803. with TObjSection(p) do
  804. begin
  805. Size:=0;
  806. datapos:=0;
  807. mempos:=0;
  808. end;
  809. end;
  810. procedure TObjData.section_fixuprelocs(p:TObject;arg:pointer);
  811. begin
  812. if TObjSection(p).Used then
  813. TObjSection(p).fixuprelocs;
  814. end;
  815. procedure TObjData.beforealloc;
  816. begin
  817. { create stabs sections if debugging }
  818. if assigned(StabsSec) then
  819. begin
  820. StabsSec.Alloc(sizeof(TObjStabEntry));
  821. StabStrSec.Alloc(1);
  822. end;
  823. end;
  824. procedure TObjData.beforewrite;
  825. var
  826. s : string[1];
  827. begin
  828. { create stabs sections if debugging }
  829. if assigned(StabsSec) then
  830. begin
  831. writestab(0,nil,0,0,0,nil);
  832. s:=#0;
  833. stabstrsec.write(s[1],length(s));
  834. end;
  835. end;
  836. procedure TObjData.afteralloc;
  837. begin
  838. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  839. end;
  840. procedure TObjData.afterwrite;
  841. var
  842. s : string[1];
  843. hstab : TObjStabEntry;
  844. begin
  845. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  846. { For the stab section we need an HdrSym which can now be
  847. calculated more easily }
  848. if assigned(StabsSec) then
  849. begin
  850. { header stab }
  851. s:=#0;
  852. stabstrsec.write(s[1],length(s));
  853. hstab.strpos:=1;
  854. hstab.ntype:=0;
  855. hstab.nother:=0;
  856. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  857. hstab.nvalue:=StabStrSec.Size;
  858. StabsSec.data.seek(0);
  859. StabsSec.data.write(hstab,sizeof(hstab));
  860. end;
  861. end;
  862. procedure TObjData.resetsections;
  863. begin
  864. FObjSectionList.ForEachCall(@section_reset,nil);
  865. end;
  866. procedure TObjData.fixuprelocs;
  867. begin
  868. FObjSectionList.ForEachCall(@section_fixuprelocs,nil);
  869. end;
  870. {****************************************************************************
  871. TObjOutput
  872. ****************************************************************************}
  873. constructor TObjOutput.create(AWriter:TObjectWriter);
  874. begin
  875. FWriter:=AWriter;
  876. CObjData:=TObjData;
  877. end;
  878. destructor TObjOutput.destroy;
  879. begin
  880. inherited destroy;
  881. end;
  882. function TObjOutput.newObjData(const n:string):TObjData;
  883. begin
  884. result:=CObjData.create(n);
  885. if (cs_use_lineinfo in aktglobalswitches) or
  886. (cs_debuginfo in aktmoduleswitches) then
  887. result.CreateDebugSections;
  888. end;
  889. function TObjOutput.startObjectfile(const fn:string):boolean;
  890. begin
  891. result:=false;
  892. { start the writer already, so the .a generation can initialize
  893. the position of the current objectfile }
  894. if not FWriter.createfile(fn) then
  895. Comment(V_Fatal,'Can''t create object '+fn);
  896. result:=true;
  897. end;
  898. function TObjOutput.writeobjectfile(data:TObjData):boolean;
  899. begin
  900. if errorcount=0 then
  901. result:=writedata(data)
  902. else
  903. result:=true;
  904. { close the writer }
  905. FWriter.closefile;
  906. end;
  907. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  908. begin
  909. { export globals and common symbols, this is needed
  910. for .a files }
  911. if p.bind in [AB_GLOBAL,AB_COMMON] then
  912. FWriter.writesym(p.name);
  913. end;
  914. {****************************************************************************
  915. TExeSymbol
  916. ****************************************************************************}
  917. constructor TExeSymbol.create(sym:TObjSymbol);
  918. begin
  919. inherited createname(sym.name);
  920. ObjSymbol:=sym;
  921. end;
  922. {****************************************************************************
  923. tExeSection
  924. ****************************************************************************}
  925. constructor tExeSection.create(const n:string);
  926. begin
  927. inherited createname(n);
  928. Size:=0;
  929. MemPos:=0;
  930. DataPos:=0;
  931. FSecSymIdx:=0;
  932. FObjSectionList:=TFPObjectList.Create(false);
  933. end;
  934. destructor tExeSection.destroy;
  935. begin
  936. ObjSectionList.Free;
  937. inherited destroy;
  938. end;
  939. procedure tExeSection.AddObjSection(objsec:TObjSection);
  940. begin
  941. ObjSectionList.Add(objsec);
  942. if (SecOptions<>[]) then
  943. begin
  944. if (oso_data in SecOptions)<>(oso_data in objsec.SecOptions) then
  945. Comment(V_Error,'Incompatible section options');
  946. end
  947. else
  948. begin
  949. { inherit section options }
  950. SecAlign:=objsec.SecAlign;
  951. SecOptions:=SecOptions+objsec.SecOptions;
  952. end;
  953. { relate ObjSection to ExeSection }
  954. objsec.ExeSection:=self;
  955. end;
  956. {****************************************************************************
  957. TExeOutput
  958. ****************************************************************************}
  959. constructor TExeOutput.create;
  960. begin
  961. { init writer }
  962. FWriter:=TObjectwriter.create;
  963. { object files }
  964. FObjDataList:=TFPObjectList.Create(true);
  965. { symbols }
  966. FExeSymbolDict:=tdictionary.create;
  967. FExeSymbolDict.noclear:=true;
  968. FExeSymbolDict.usehash;
  969. FExeSymbolList:=TFPObjectList.Create(true);
  970. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  971. FExternalObjSymbols:=TFPObjectList.Create(false);
  972. FCommonObjSymbols:=TFPObjectList.Create(false);
  973. FEntryName:='start';
  974. { sections }
  975. FExeSectionDict:=TDictionary.create;
  976. FExeSectionDict.noclear:=true;
  977. FExeSectionList:=TFPObjectList.Create(true);
  978. FImageBase:=0;
  979. SectionMemAlign:=$1000;
  980. SectionDataAlign:=$200;
  981. FCExeSection:=TExeSection;
  982. FCObjData:=TObjData;
  983. end;
  984. destructor TExeOutput.destroy;
  985. begin
  986. FExeSymbolDict.free;
  987. FExeSymbolList.free;
  988. UnresolvedExeSymbols.free;
  989. ExternalObjSymbols.free;
  990. CommonObjSymbols.free;
  991. FExeSectionDict.free;
  992. FExeSectionList.free;
  993. objdatalist.free;
  994. FWriter.free;
  995. inherited destroy;
  996. end;
  997. function TExeOutput.writeexefile(const fn:string):boolean;
  998. begin
  999. result:=false;
  1000. if FWriter.createfile(fn) then
  1001. begin
  1002. { Only write the .o if there are no errors }
  1003. if errorcount=0 then
  1004. result:=writedata
  1005. else
  1006. result:=true;
  1007. { close the writer }
  1008. FWriter.closefile;
  1009. end
  1010. else
  1011. Comment(V_Fatal,'Can''t create executable '+fn);
  1012. end;
  1013. procedure TExeOutput.AddObjData(objdata:TObjData);
  1014. begin
  1015. if objdata.classtype<>FCObjData then
  1016. Comment(V_Error,'Invalid input object format for '+objdata.name+' got '+objdata.classname+' expected '+FCObjData.classname);
  1017. ObjDataList.Add(objdata);
  1018. end;
  1019. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1020. begin
  1021. result:=TExeSection(FExeSectionDict.Search(aname));
  1022. end;
  1023. procedure TExeOutput.Load_Start;
  1024. begin
  1025. ObjDataList.Clear;
  1026. { Globals defined in the linker script }
  1027. if not assigned(internalobjdata) then
  1028. internalobjdata:=CObjData.create('*Internal*');
  1029. AddObjData(internalobjdata);
  1030. { Common data section }
  1031. commonObjSection:=internalobjdata.createsection(sec_bss,'');
  1032. end;
  1033. procedure TExeOutput.Load_EntryName(const aname:string);
  1034. begin
  1035. EntryName:=aname;
  1036. end;
  1037. procedure TExeOutput.Load_Symbol(const aname:string);
  1038. begin
  1039. internalobjdata.createsection('*'+aname,0,[]);
  1040. internalobjdata.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION);
  1041. end;
  1042. procedure TExeOutput.Order_Start;
  1043. begin
  1044. end;
  1045. procedure TExeOutput.Order_End;
  1046. begin
  1047. internalobjdata.afterwrite;
  1048. end;
  1049. procedure TExeOutput.Order_ExeSection(const aname:string);
  1050. var
  1051. sec : TExeSection;
  1052. begin
  1053. sec:=FindExeSection(aname);
  1054. if not assigned(sec) then
  1055. begin
  1056. sec:=CExeSection.create(aname);
  1057. FExeSectionDict.Insert(sec);
  1058. FExeSectionList.Add(sec);
  1059. end;
  1060. { Clear ExeSection contents }
  1061. FCurrExeSec:=sec;
  1062. end;
  1063. procedure TExeOutput.Order_EndExeSection;
  1064. begin
  1065. if not assigned(CurrExeSec) then
  1066. internalerror(200602184);
  1067. FCurrExeSec:=nil;
  1068. end;
  1069. procedure TExeOutput.Order_ObjSection(const aname:string);
  1070. var
  1071. i,j : longint;
  1072. objdata : TObjData;
  1073. objsec : TObjSection;
  1074. begin
  1075. if not assigned(CurrExeSec) then
  1076. internalerror(200602181);
  1077. for i:=0 to ObjDataList.Count-1 do
  1078. begin
  1079. objdata:=TObjData(ObjDataList[i]);
  1080. for j:=0 to objdata.ObjSectionList.Count-1 do
  1081. begin
  1082. objsec:=TObjSection(objdata.ObjSectionList[j]);
  1083. if MatchPattern(aname,objsec.name) then
  1084. CurrExeSec.AddObjSection(objsec);
  1085. end;
  1086. end;
  1087. end;
  1088. procedure TExeOutput.Order_Symbol(const aname:string);
  1089. var
  1090. ObjSection : TObjSection;
  1091. begin
  1092. ObjSection:=internalobjdata.findsection('*'+aname);
  1093. if not assigned(ObjSection) then
  1094. internalerror(200603041);
  1095. ObjSection.SecOptions:=CurrExeSec.SecOptions;
  1096. CurrExeSec.AddObjSection(ObjSection);
  1097. end;
  1098. procedure TExeOutput.Order_Align(const aname:string);
  1099. var
  1100. code : integer;
  1101. alignval : longint;
  1102. objsec : TObjSection;
  1103. begin
  1104. val(aname,alignval,code);
  1105. if alignval<=0 then
  1106. exit;
  1107. { Create an empty section with the required aligning }
  1108. inc(Fzeronr);
  1109. objsec:=internalobjdata.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_data,oso_keep]);
  1110. CurrExeSec.AddObjSection(objsec);
  1111. end;
  1112. procedure TExeOutput.Order_Zeros(const aname:string);
  1113. var
  1114. zeros : array[0..1023] of byte;
  1115. code : integer;
  1116. len : longint;
  1117. objsec : TObjSection;
  1118. begin
  1119. val(aname,len,code);
  1120. if len<=0 then
  1121. exit;
  1122. if len>sizeof(zeros) then
  1123. internalerror(200602254);
  1124. fillchar(zeros,len,0);
  1125. inc(Fzeronr);
  1126. objsec:=internalobjdata.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_data,oso_keep]);
  1127. internalobjdata.writebytes(zeros,len);
  1128. CurrExeSec.AddObjSection(objsec);
  1129. end;
  1130. procedure TExeOutput.Order_Stabs;
  1131. var
  1132. stabexesec,
  1133. stabstrexesec : TExeSection;
  1134. currstabsec,
  1135. currstabstrsec,
  1136. mergedstabsec,
  1137. mergedstabstrsec : TObjSection;
  1138. nextstabreloc,
  1139. currstabreloc : TObjRelocation;
  1140. i,j,
  1141. stabcnt : longint;
  1142. skipstab : boolean;
  1143. hstab : TObjStabEntry;
  1144. stabrelocofs : longint;
  1145. buf : array[0..1023] of byte;
  1146. bufend,
  1147. bufsize : longint;
  1148. begin
  1149. stabexesec:=FindExeSection('.stab');
  1150. stabstrexesec:=FindExeSection('.stabstr');
  1151. if (stabexesec=nil) or
  1152. (stabstrexesec=nil) or
  1153. (stabexesec.ObjSectionlist.count=0) then
  1154. exit;
  1155. { Create new stabsection }
  1156. stabrelocofs:[email protected]@hstab;
  1157. mergedstabsec:=internalobjdata.CreateSection(sec_stab,'');
  1158. mergedstabstrsec:=internalobjdata.CreateSection(sec_stabstr,'');
  1159. { write stab for hdrsym }
  1160. fillchar(hstab,sizeof(TObjStabEntry),0);
  1161. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  1162. { .stabstr starts with a #0 }
  1163. buf[0]:=0;
  1164. mergedstabstrsec.write(buf[0],1);
  1165. { Copy stabs and corresponding relocations }
  1166. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  1167. begin
  1168. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  1169. currstabstrsec:=currstabsec.objdata.findsection('.stabstr');
  1170. if assigned(currstabstrsec) then
  1171. begin
  1172. stabcnt:=currstabsec.data.size div sizeof(TObjStabEntry);
  1173. currstabsec.data.seek(0);
  1174. currstabreloc:=TObjRelocation(currstabsec.relocations.first);
  1175. for j:=0 to stabcnt-1 do
  1176. begin
  1177. skipstab:=false;
  1178. currstabsec.data.read(hstab,sizeof(TObjStabEntry));
  1179. { Only include first hdrsym stab }
  1180. if hstab.ntype=0 then
  1181. skipstab:=true;
  1182. if not skipstab then
  1183. begin
  1184. { Copy string in stabstr }
  1185. if hstab.strpos<>0 then
  1186. begin
  1187. currstabstrsec.data.seek(hstab.strpos);
  1188. hstab.strpos:=mergedstabstrsec.Size;
  1189. repeat
  1190. bufsize:=currstabstrsec.data.read(buf,sizeof(buf));
  1191. bufend:=indexbyte(buf,bufsize,0);
  1192. if bufend=-1 then
  1193. bufend:=bufsize
  1194. else
  1195. begin
  1196. { include the #0 }
  1197. inc(bufend);
  1198. end;
  1199. mergedstabstrsec.write(buf,bufend);
  1200. until (bufend<>-1) or (bufsize<sizeof(buf));
  1201. end;
  1202. { Copy relocation }
  1203. while assigned(currstabreloc) and
  1204. (currstabreloc.dataoffset<j*sizeof(TObjStabEntry)+stabrelocofs) do
  1205. currstabreloc:=TObjRelocation(currstabreloc.next);
  1206. if assigned(currstabreloc) then
  1207. begin
  1208. if (currstabreloc.dataoffset=j*sizeof(TObjStabEntry)+stabrelocofs) then
  1209. begin
  1210. currstabreloc.dataoffset:=mergedstabsec.Size+stabrelocofs;
  1211. nextstabreloc:=TObjRelocation(currstabreloc.next);
  1212. currstabsec.relocations.remove(currstabreloc);
  1213. mergedstabsec.relocations.concat(currstabreloc);
  1214. currstabreloc:=nextstabreloc;
  1215. end;
  1216. end;
  1217. mergedstabsec.write(hstab,sizeof(hstab));
  1218. end;
  1219. end;
  1220. end;
  1221. { Unload stabs }
  1222. if assigned(currstabstrsec) then
  1223. currstabsec.objdata.removesection(currstabstrsec);
  1224. currstabsec.objdata.removesection(currstabsec);
  1225. end;
  1226. { Generate new HdrSym }
  1227. if mergedstabsec.Size>0 then
  1228. begin
  1229. hstab.strpos:=1;
  1230. hstab.ntype:=0;
  1231. hstab.nother:=0;
  1232. hstab.ndesc:=word((mergedstabsec.Size div sizeof(TObjStabEntry))-1);
  1233. hstab.nvalue:=mergedstabstrsec.Size;
  1234. mergedstabsec.data.seek(0);
  1235. mergedstabsec.data.write(hstab,sizeof(hstab));
  1236. end;
  1237. { Replace all sections with our combined stabsec }
  1238. stabexesec.ObjSectionList.Clear;
  1239. stabstrexesec.ObjSectionList.Clear;
  1240. stabexesec.AddObjSection(mergedstabsec);
  1241. stabstrexesec.AddObjSection(mergedstabstrsec);
  1242. end;
  1243. procedure TExeOutput.CalcPos_ExeSection(const aname:string);
  1244. var
  1245. i : longint;
  1246. objsec : TObjSection;
  1247. begin
  1248. { Section can be removed }
  1249. FCurrExeSec:=FindExeSection(aname);
  1250. if not assigned(CurrExeSec) then
  1251. exit;
  1252. { Alignment of ExeSection }
  1253. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1254. CurrExeSec.MemPos:=CurrMemPos;
  1255. if (oso_data in currexesec.SecOptions) then
  1256. begin
  1257. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1258. CurrExeSec.DataPos:=CurrDataPos;
  1259. end;
  1260. { set position of object ObjSections }
  1261. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1262. begin
  1263. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1264. { Position in memory }
  1265. objsec.setmempos(CurrMemPos);
  1266. { Position in File }
  1267. if (oso_data in objsec.SecOptions) then
  1268. begin
  1269. if not (oso_data in currexesec.SecOptions) then
  1270. internalerror(200603043);
  1271. if not assigned(objsec.Data) then
  1272. internalerror(200603044);
  1273. objsec.setdatapos(CurrDataPos);
  1274. end;
  1275. end;
  1276. { calculate size of the section }
  1277. CurrExeSec.Size:=CurrMemPos-CurrExeSec.MemPos;
  1278. end;
  1279. procedure TExeOutput.CalcPos_EndExeSection;
  1280. begin
  1281. if not assigned(CurrExeSec) then
  1282. exit;
  1283. FCurrExeSec:=nil;
  1284. end;
  1285. procedure TExeOutput.CalcPos_Start;
  1286. begin
  1287. CurrMemPos:=0;
  1288. CurrDataPos:=0;
  1289. end;
  1290. procedure TExeOutput.CalcPos_Header;
  1291. begin
  1292. end;
  1293. procedure TExeOutput.CalcPos_Symbols;
  1294. begin
  1295. end;
  1296. procedure TExeOutput.ResolveSymbols;
  1297. var
  1298. objdata : TObjData;
  1299. exesym : TExeSymbol;
  1300. objsym,
  1301. commonsym : TObjSymbol;
  1302. firstcommon : boolean;
  1303. i,j : longint;
  1304. begin
  1305. {
  1306. The symbol calculation is done in 3 steps:
  1307. 1. register globals
  1308. register externals
  1309. register commons
  1310. 2. try to find commons, if not found then
  1311. add to the globals (so externals can be resolved)
  1312. 3. try to find externals
  1313. }
  1314. { Step 1, Register symbols }
  1315. for i:=0 to ObjDataList.Count-1 do
  1316. begin
  1317. objdata:=TObjData(ObjDataList[i]);
  1318. for j:=0 to objdata.ObjSymbolList.Count-1 do
  1319. begin
  1320. objsym:=TObjSymbol(objdata.ObjSymbolList[j]);
  1321. { Skip local symbols }
  1322. if objsym.bind=AB_LOCAL then
  1323. continue;
  1324. exesym:=texesymbol(FExeSymbolDict.search(objsym.name));
  1325. if not assigned(exesym) then
  1326. begin
  1327. exesym:=texesymbol.createname(objsym.name);
  1328. FExeSymbolDict.insert(exesym);
  1329. FExeSymbolList.Add(exesym);
  1330. end;
  1331. { Defining the symbol? }
  1332. if objsym.bind=AB_GLOBAL then
  1333. begin
  1334. if not assigned(exesym.ObjSymbol) then
  1335. exesym.ObjSymbol:=objsym
  1336. else
  1337. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  1338. end;
  1339. objsym.exesymbol:=exesym;
  1340. case objsym.bind of
  1341. AB_EXTERNAL :
  1342. ExternalObjSymbols.add(objsym);
  1343. AB_COMMON :
  1344. CommonObjSymbols.add(objsym);
  1345. end;
  1346. end;
  1347. end;
  1348. { Step 2, Match common symbols or add to the globals }
  1349. firstcommon:=true;
  1350. for i:=0 to CommonObjSymbols.count-1 do
  1351. begin
  1352. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1353. if assigned(objsym.exesymbol.objsymbol) then
  1354. begin
  1355. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  1356. internalerror(200206301)
  1357. else
  1358. begin
  1359. { allocate new objsymbol in .bss of *COMMON* and assign
  1360. it to the exesymbol }
  1361. if firstcommon then
  1362. begin
  1363. if assigned(exemap) then
  1364. exemap.AddCommonSymbolsHeader;
  1365. firstcommon:=false;
  1366. end;
  1367. internalobjdata.setsection(commonObjSection);
  1368. commonsym:=internalobjdata.symboldefine(objsym.name,AB_GLOBAL,AT_FUNCTION);
  1369. commonsym.size:=objsym.size;
  1370. internalobjdata.alloc(objsym.size);
  1371. if assigned(exemap) then
  1372. exemap.AddCommonSymbol(commonsym);
  1373. { Assign to the exesymbol }
  1374. objsym.exesymbol.objsymbol:=commonsym
  1375. end;
  1376. end;
  1377. end;
  1378. { Generate a list of Unresolved External symbols }
  1379. for i:=0 to ExeSymbolList.count-1 do
  1380. begin
  1381. exesym:=TExeSymbol(ExeSymbolList[i]);
  1382. if exesym.objsymbol=nil then
  1383. UnresolvedExeSymbols.Add(exesym);
  1384. end;
  1385. Comment(V_Debug,'Number of unresolved externals in objects '+tostr(UnresolvedExeSymbols.Count));
  1386. { Find entry symbol and print in map }
  1387. exesym:=texesymbol(ExeSymbolDict.search(EntryName));
  1388. if assigned(exesym) then
  1389. begin
  1390. EntrySym:=exesym.ObjSymbol;
  1391. if assigned(exemap) then
  1392. begin
  1393. exemap.Add('');
  1394. exemap.Add('Entry symbol '+EntryName);
  1395. end;
  1396. end
  1397. else
  1398. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  1399. end;
  1400. procedure TExeOutput.ResolveExternals(const libname:string);
  1401. begin
  1402. end;
  1403. procedure TExeOutput.PrintMemoryMap;
  1404. var
  1405. exesec : TExeSection;
  1406. objsec : TObjSection;
  1407. objsym : TObjSymbol;
  1408. i,j,k : longint;
  1409. begin
  1410. if not assigned(exemap) then
  1411. exit;
  1412. exemap.AddMemoryMapHeader(ImageBase);
  1413. for i:=0 to ExeSections.Count-1 do
  1414. begin
  1415. exesec:=TExeSection(ExeSections[i]);
  1416. exemap.AddMemoryMapExeSection(exesec);
  1417. for j:=0 to exesec.ObjSectionList.count-1 do
  1418. begin
  1419. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1420. exemap.AddMemoryMapObjectSection(objsec);
  1421. for k:=0 to objsec.ObjSymbolDefines.Count-1 do
  1422. begin
  1423. objsym:=TObjSymbol(objsec.ObjSymbolDefines[k]);
  1424. exemap.AddMemoryMapSymbol(objsym);
  1425. end;
  1426. end;
  1427. end;
  1428. end;
  1429. procedure TExeOutput.FixUpSymbols;
  1430. var
  1431. i : longint;
  1432. sym : TObjSymbol;
  1433. begin
  1434. { Update ImageBase to ObjData so it can access from ObjSymbols }
  1435. for i:=0 to ObjDataList.Count-1 do
  1436. TObjData(ObjDataList[i]).imagebase:=imagebase;
  1437. {
  1438. Fixing up symbols is done in the following steps:
  1439. 1. Update common references
  1440. 2. Update external references
  1441. }
  1442. { Step 1, Update commons }
  1443. for i:=0 to CommonObjSymbols.count-1 do
  1444. begin
  1445. sym:=TObjSymbol(CommonObjSymbols[i]);
  1446. if sym.bind=AB_COMMON then
  1447. begin
  1448. { update this symbol }
  1449. sym.bind:=sym.exesymbol.ObjSymbol.bind;
  1450. sym.offset:=sym.exesymbol.ObjSymbol.offset;
  1451. sym.size:=sym.exesymbol.ObjSymbol.size;
  1452. sym.typ:=sym.exesymbol.ObjSymbol.typ;
  1453. sym.ObjSection:=sym.exesymbol.ObjSymbol.ObjSection;
  1454. end;
  1455. end;
  1456. { Step 2, Update externals }
  1457. for i:=0 to ExternalObjSymbols.count-1 do
  1458. begin
  1459. sym:=TObjSymbol(ExternalObjSymbols[i]);
  1460. if sym.bind=AB_EXTERNAL then
  1461. begin
  1462. if assigned(sym.exesymbol.ObjSymbol) then
  1463. begin
  1464. { update this symbol }
  1465. sym.bind:=sym.exesymbol.ObjSymbol.bind;
  1466. sym.offset:=sym.exesymbol.ObjSymbol.offset;
  1467. sym.size:=sym.exesymbol.ObjSymbol.size;
  1468. sym.typ:=sym.exesymbol.ObjSymbol.typ;
  1469. sym.ObjSection:=sym.exesymbol.ObjSymbol.ObjSection;
  1470. end
  1471. else
  1472. Comment(V_Error,'Undefined symbol: '+sym.name);
  1473. end;
  1474. end;
  1475. end;
  1476. procedure TExeOutput.RemoveEmptySections;
  1477. var
  1478. i : longint;
  1479. exesec : TExeSection;
  1480. begin
  1481. for i:=0 to ExeSections.Count-1 do
  1482. begin
  1483. exesec:=TExeSection(ExeSections[i]);
  1484. if not(oso_keep in exesec.SecOptions) and
  1485. (
  1486. (exesec.ObjSectionlist.count=0) or
  1487. (
  1488. (cs_link_strip in aktglobalswitches) and
  1489. (oso_debug in exesec.SecOptions)
  1490. )
  1491. ) then
  1492. begin
  1493. Comment(V_Debug,'Deleting empty section '+exesec.name);
  1494. FExeSectionDict.Delete(exesec.name);
  1495. FExeSectionList[i]:=nil;
  1496. end;
  1497. end;
  1498. ExeSections.Pack;
  1499. end;
  1500. procedure TExeOutput.RemoveUnreferencedSections;
  1501. var
  1502. ObjSectionWorkList : TFPObjectList;
  1503. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  1504. begin
  1505. if not aobjsec.Used then
  1506. begin
  1507. aobjsec.Used:=true;
  1508. ObjSectionWorkList.Add(aobjsec);
  1509. end;
  1510. end;
  1511. var
  1512. i,j : longint;
  1513. exesec : TExeSection;
  1514. objdata : TObjData;
  1515. refobjsec,
  1516. objsec : TObjSection;
  1517. objsym : TObjSymbol;
  1518. begin
  1519. ObjSectionWorkList:=TFPObjectList.Create(false);
  1520. if assigned(exemap) then
  1521. exemap.AddHeader('Removing unreferenced sections');
  1522. { Initialize by marking all sections unused and
  1523. adding the sections with oso_keep flags to the ObjSectionWorkList }
  1524. for i:=0 to ObjDataList.Count-1 do
  1525. begin
  1526. objdata:=TObjData(ObjDataList[i]);
  1527. for j:=0 to objdata.ObjSectionList.Count-1 do
  1528. begin
  1529. objsec:=TObjSection(objdata.ObjSectionList[j]);
  1530. objsec.Used:=false;
  1531. {$warning TODO remove debug section always keep}
  1532. if oso_debug in objsec.secoptions then
  1533. objsec.Used:=true;
  1534. if (oso_keep in objsec.secoptions) then
  1535. AddToObjSectionWorkList(objsec);
  1536. end;
  1537. end;
  1538. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  1539. { Process all sections, add new sections to process based
  1540. on the symbol references }
  1541. while ObjSectionWorkList.Count>0 do
  1542. begin
  1543. objsec:=TObjSection(ObjSectionWorkList.Last);
  1544. if assigned(exemap) then
  1545. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjSymbolRefs.Count)+' references');
  1546. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  1547. for i:=0 to objsec.ObjSymbolRefs.count-1 do
  1548. begin
  1549. objsym:=TObjSymbol(objsec.ObjSymbolRefs[i]);
  1550. if objsym.bind=AB_LOCAL then
  1551. begin
  1552. if not assigned(objsym.objsection) then
  1553. internalerror(200603062);
  1554. refobjsec:=objsym.objsection
  1555. end
  1556. else
  1557. begin
  1558. if not(assigned(objsym.exesymbol) and
  1559. assigned(objsym.exesymbol.objsymbol)) then
  1560. internalerror(200603063);
  1561. refobjsec:=objsym.exesymbol.objsymbol.objsection;
  1562. end;
  1563. if assigned(exemap) then
  1564. exemap.Add(' References '+refobjsec.fullname);
  1565. AddToObjSectionWorkList(refobjsec);
  1566. end;
  1567. end;
  1568. ObjSectionWorkList.Free;
  1569. ObjSectionWorkList:=nil;
  1570. { Remove unused objsections from exesections }
  1571. for i:=0 to ExeSections.Count-1 do
  1572. begin
  1573. exesec:=TExeSection(ExeSections[i]);
  1574. for j:=0 to exesec.ObjSectionlist.count-1 do
  1575. begin
  1576. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  1577. if not objsec.used then
  1578. begin
  1579. if assigned(exemap) then
  1580. exemap.Add('Removing '+objsec.FullName);
  1581. exesec.ObjSectionlist[j]:=nil;
  1582. end;
  1583. end;
  1584. exesec.ObjSectionlist.Pack;
  1585. end;
  1586. end;
  1587. procedure TExeOutput.FixUpRelocations;
  1588. var
  1589. i : longint;
  1590. objdata : TObjData;
  1591. begin
  1592. for i:=0 to ObjDataList.Count-1 do
  1593. begin
  1594. objdata:=TObjData(ObjDataList[i]);
  1595. objdata.fixuprelocs;
  1596. end;
  1597. end;
  1598. {****************************************************************************
  1599. TObjInput
  1600. ****************************************************************************}
  1601. constructor TObjInput.create;
  1602. begin
  1603. { init reader }
  1604. FReader:=TObjectreader.create;
  1605. end;
  1606. destructor TObjInput.destroy;
  1607. begin
  1608. FReader.free;
  1609. inherited destroy;
  1610. end;
  1611. function TObjInput.newObjData(const n:string):TObjData;
  1612. begin
  1613. result:=CObjData.create(n);
  1614. end;
  1615. function TObjInput.readobjectfile(const fn:string;data:TObjData):boolean;
  1616. begin
  1617. result:=false;
  1618. { start the reader }
  1619. if FReader.openfile(fn) then
  1620. begin
  1621. result:=readObjData(data);
  1622. FReader.closefile;
  1623. end;
  1624. end;
  1625. end.