ogbase.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. result:=sizeof(aint);
  648. end;
  649. function TObjData.createsection(atype:TAsmSectionType;const aname:string):TObjSection;
  650. begin
  651. result:=createsection(sectionname(atype,aname),sectiontype2align(atype),sectiontype2options(atype));
  652. end;
  653. function TObjData.createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions):TObjSection;
  654. begin
  655. result:=TObjSection(FObjSectionDict.search(aname));
  656. if not assigned(result) then
  657. begin
  658. result:=CObjSection.create(aname,aalign,aoptions);
  659. FObjSectionDict.Insert(result);
  660. FObjSectionList.Add(result);
  661. result.ObjData:=self;
  662. end;
  663. FCurrObjSec:=result;
  664. end;
  665. procedure TObjData.CreateDebugSections;
  666. begin
  667. end;
  668. function TObjData.FindSection(const aname:string):TObjSection;
  669. begin
  670. result:=TObjSection(FObjSectionDict.Search(aname));
  671. end;
  672. procedure TObjData.removesection(asec:TObjSection);
  673. begin
  674. FObjSectionDict.Delete(asec.name);
  675. FObjSectionList.Remove(asec);
  676. end;
  677. procedure TObjData.setsection(asec:TObjSection);
  678. begin
  679. if asec.ObjData<>self then
  680. internalerror(200403041);
  681. FCurrObjSec:=asec;
  682. end;
  683. function TObjData.createsymbol(const aname:string):TObjSymbol;
  684. begin
  685. result:=TObjSymbol(FObjSymbolDict.search(aname));
  686. if not assigned(result) then
  687. begin
  688. result:=TObjSymbol.Create(aname);
  689. FObjSymbolDict.Insert(result);
  690. FObjSymbolList.Add(result);
  691. end;
  692. end;
  693. function TObjData.symboldefine(asmsym:TAsmSymbol):TObjSymbol;
  694. begin
  695. if assigned(asmsym) then
  696. begin
  697. if not assigned(asmsym.cachedObjSymbol) then
  698. begin
  699. result:=symboldefine(asmsym.name,asmsym.bind,asmsym.typ);
  700. asmsym.cachedObjSymbol:=result;
  701. FCachedAsmSymbolList.add(asmsym);
  702. end
  703. else
  704. begin
  705. result:=TObjSymbol(asmsym.cachedObjSymbol);
  706. result.SetAddress(CurrPass,CurrObjSec,asmsym.bind,asmsym.typ);
  707. { Register also in TObjSection }
  708. CurrObjSec.AddSymbolDefine(result);
  709. end;
  710. end
  711. else
  712. result:=nil;
  713. end;
  714. function TObjData.symboldefine(const aname:string;abind:TAsmsymbind;atyp:Tasmsymtype):TObjSymbol;
  715. begin
  716. if not assigned(CurrObjSec) then
  717. internalerror(200603051);
  718. result:=CreateSymbol(aname);
  719. { Register also in TObjSection }
  720. CurrObjSec.AddSymbolDefine(result);
  721. result.SetAddress(CurrPass,CurrObjSec,abind,atyp);
  722. end;
  723. function TObjData.symbolref(asmsym:TAsmSymbol):TObjSymbol;
  724. begin
  725. if assigned(asmsym) then
  726. begin
  727. if not assigned(asmsym.cachedObjSymbol) then
  728. begin
  729. result:=symbolref(asmsym.name);
  730. asmsym.cachedObjSymbol:=result;
  731. FCachedAsmSymbolList.add(asmsym);
  732. end
  733. else
  734. begin
  735. result:=TObjSymbol(asmsym.cachedObjSymbol);
  736. { Register also in TObjSection }
  737. CurrObjSec.AddSymbolRef(result);
  738. end;
  739. end
  740. else
  741. result:=nil;
  742. end;
  743. function TObjData.symbolref(const aname:string):TObjSymbol;
  744. begin
  745. if not assigned(CurrObjSec) then
  746. internalerror(200603052);
  747. result:=CreateSymbol(aname);
  748. { Register also in TObjSection }
  749. CurrObjSec.AddSymbolRef(result);
  750. end;
  751. procedure TObjData.ResetCachedAsmSymbols;
  752. var
  753. i : longint;
  754. begin
  755. for i:=0 to FCachedAsmSymbolList.Count-1 do
  756. tasmsymbol(FCachedAsmSymbolList[i]).cachedObjSymbol:=nil;
  757. FCachedAsmSymbolList.Clear;
  758. end;
  759. procedure TObjData.writebytes(const data;len:aint);
  760. begin
  761. if not assigned(CurrObjSec) then
  762. internalerror(200402251);
  763. CurrObjSec.write(data,len);
  764. end;
  765. procedure TObjData.alloc(len:aint);
  766. begin
  767. if not assigned(CurrObjSec) then
  768. internalerror(200402252);
  769. CurrObjSec.alloc(len);
  770. end;
  771. procedure TObjData.allocalign(len:shortint);
  772. begin
  773. if not assigned(CurrObjSec) then
  774. internalerror(200402253);
  775. CurrObjSec.alloc(align(CurrObjSec.size,len)-CurrObjSec.size);
  776. end;
  777. procedure TObjData.allocstab(p:pchar);
  778. begin
  779. if not(assigned(FStabsObjSec) and assigned(FStabStrObjSec)) then
  780. internalerror(200402254);
  781. FStabsObjSec.alloc(sizeof(TObjStabEntry));
  782. if assigned(p) and (p[0]<>#0) then
  783. FStabStrObjSec.alloc(strlen(p)+1);
  784. end;
  785. procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
  786. begin
  787. with TObjSection(p) do
  788. alloc(align(size,secalign)-size);
  789. end;
  790. procedure TObjData.section_afterwrite(p:TObject;arg:pointer);
  791. begin
  792. with TObjSection(p) do
  793. begin
  794. if assigned(data) then
  795. writezeros(align(size,secalign)-size);
  796. end;
  797. end;
  798. procedure TObjData.section_reset(p:TObject;arg:pointer);
  799. begin
  800. with TObjSection(p) do
  801. begin
  802. Size:=0;
  803. datapos:=0;
  804. mempos:=0;
  805. end;
  806. end;
  807. procedure TObjData.section_fixuprelocs(p:TObject;arg:pointer);
  808. begin
  809. if TObjSection(p).Used then
  810. TObjSection(p).fixuprelocs;
  811. end;
  812. procedure TObjData.beforealloc;
  813. begin
  814. { create stabs sections if debugging }
  815. if assigned(StabsSec) then
  816. begin
  817. StabsSec.Alloc(sizeof(TObjStabEntry));
  818. StabStrSec.Alloc(1);
  819. end;
  820. end;
  821. procedure TObjData.beforewrite;
  822. var
  823. s : string[1];
  824. begin
  825. { create stabs sections if debugging }
  826. if assigned(StabsSec) then
  827. begin
  828. writestab(0,nil,0,0,0,nil);
  829. s:=#0;
  830. stabstrsec.write(s[1],length(s));
  831. end;
  832. end;
  833. procedure TObjData.afteralloc;
  834. begin
  835. FObjSectionList.ForEachCall(@section_afteralloc,nil);
  836. end;
  837. procedure TObjData.afterwrite;
  838. var
  839. s : string[1];
  840. hstab : TObjStabEntry;
  841. begin
  842. FObjSectionList.ForEachCall(@section_afterwrite,nil);
  843. { For the stab section we need an HdrSym which can now be
  844. calculated more easily }
  845. if assigned(StabsSec) then
  846. begin
  847. { header stab }
  848. s:=#0;
  849. stabstrsec.write(s[1],length(s));
  850. hstab.strpos:=1;
  851. hstab.ntype:=0;
  852. hstab.nother:=0;
  853. hstab.ndesc:=(StabsSec.Size div sizeof(TObjStabEntry))-1;
  854. hstab.nvalue:=StabStrSec.Size;
  855. StabsSec.data.seek(0);
  856. StabsSec.data.write(hstab,sizeof(hstab));
  857. end;
  858. end;
  859. procedure TObjData.resetsections;
  860. begin
  861. FObjSectionList.ForEachCall(@section_reset,nil);
  862. end;
  863. procedure TObjData.fixuprelocs;
  864. begin
  865. FObjSectionList.ForEachCall(@section_fixuprelocs,nil);
  866. end;
  867. {****************************************************************************
  868. TObjOutput
  869. ****************************************************************************}
  870. constructor TObjOutput.create(AWriter:TObjectWriter);
  871. begin
  872. FWriter:=AWriter;
  873. CObjData:=TObjData;
  874. end;
  875. destructor TObjOutput.destroy;
  876. begin
  877. inherited destroy;
  878. end;
  879. function TObjOutput.newObjData(const n:string):TObjData;
  880. begin
  881. result:=CObjData.create(n);
  882. if (cs_use_lineinfo in aktglobalswitches) or
  883. (cs_debuginfo in aktmoduleswitches) then
  884. result.CreateDebugSections;
  885. end;
  886. function TObjOutput.startObjectfile(const fn:string):boolean;
  887. begin
  888. result:=false;
  889. { start the writer already, so the .a generation can initialize
  890. the position of the current objectfile }
  891. if not FWriter.createfile(fn) then
  892. Comment(V_Fatal,'Can''t create object '+fn);
  893. result:=true;
  894. end;
  895. function TObjOutput.writeobjectfile(data:TObjData):boolean;
  896. begin
  897. if errorcount=0 then
  898. result:=writedata(data)
  899. else
  900. result:=true;
  901. { close the writer }
  902. FWriter.closefile;
  903. end;
  904. procedure TObjOutput.exportsymbol(p:TObjSymbol);
  905. begin
  906. { export globals and common symbols, this is needed
  907. for .a files }
  908. if p.bind in [AB_GLOBAL,AB_COMMON] then
  909. FWriter.writesym(p.name);
  910. end;
  911. {****************************************************************************
  912. TExeSymbol
  913. ****************************************************************************}
  914. constructor TExeSymbol.create(sym:TObjSymbol);
  915. begin
  916. inherited createname(sym.name);
  917. ObjSymbol:=sym;
  918. end;
  919. {****************************************************************************
  920. tExeSection
  921. ****************************************************************************}
  922. constructor tExeSection.create(const n:string);
  923. begin
  924. inherited createname(n);
  925. Size:=0;
  926. MemPos:=0;
  927. DataPos:=0;
  928. FSecSymIdx:=0;
  929. FObjSectionList:=TFPObjectList.Create(false);
  930. end;
  931. destructor tExeSection.destroy;
  932. begin
  933. ObjSectionList.Free;
  934. inherited destroy;
  935. end;
  936. procedure tExeSection.AddObjSection(objsec:TObjSection);
  937. begin
  938. ObjSectionList.Add(objsec);
  939. if (SecOptions<>[]) then
  940. begin
  941. if (oso_data in SecOptions)<>(oso_data in objsec.SecOptions) then
  942. Comment(V_Error,'Incompatible section options');
  943. end
  944. else
  945. begin
  946. { inherit section options }
  947. SecAlign:=objsec.SecAlign;
  948. SecOptions:=SecOptions+objsec.SecOptions;
  949. end;
  950. { relate ObjSection to ExeSection }
  951. objsec.ExeSection:=self;
  952. end;
  953. {****************************************************************************
  954. TExeOutput
  955. ****************************************************************************}
  956. constructor TExeOutput.create;
  957. begin
  958. { init writer }
  959. FWriter:=TObjectwriter.create;
  960. { object files }
  961. FObjDataList:=TFPObjectList.Create(true);
  962. { symbols }
  963. FExeSymbolDict:=tdictionary.create;
  964. FExeSymbolDict.noclear:=true;
  965. FExeSymbolDict.usehash;
  966. FExeSymbolList:=TFPObjectList.Create(true);
  967. FUnresolvedExeSymbols:=TFPObjectList.Create(false);
  968. FExternalObjSymbols:=TFPObjectList.Create(false);
  969. FCommonObjSymbols:=TFPObjectList.Create(false);
  970. FEntryName:='start';
  971. { sections }
  972. FExeSectionDict:=TDictionary.create;
  973. FExeSectionDict.noclear:=true;
  974. FExeSectionList:=TFPObjectList.Create(true);
  975. FImageBase:=0;
  976. SectionMemAlign:=$1000;
  977. SectionDataAlign:=$200;
  978. FCExeSection:=TExeSection;
  979. FCObjData:=TObjData;
  980. end;
  981. destructor TExeOutput.destroy;
  982. begin
  983. FExeSymbolDict.free;
  984. FExeSymbolList.free;
  985. UnresolvedExeSymbols.free;
  986. ExternalObjSymbols.free;
  987. CommonObjSymbols.free;
  988. FExeSectionDict.free;
  989. FExeSectionList.free;
  990. objdatalist.free;
  991. FWriter.free;
  992. inherited destroy;
  993. end;
  994. function TExeOutput.writeexefile(const fn:string):boolean;
  995. begin
  996. result:=false;
  997. if FWriter.createfile(fn) then
  998. begin
  999. { Only write the .o if there are no errors }
  1000. if errorcount=0 then
  1001. result:=writedata
  1002. else
  1003. result:=true;
  1004. { close the writer }
  1005. FWriter.closefile;
  1006. end
  1007. else
  1008. Comment(V_Fatal,'Can''t create executable '+fn);
  1009. end;
  1010. procedure TExeOutput.AddObjData(objdata:TObjData);
  1011. begin
  1012. if objdata.classtype<>FCObjData then
  1013. Comment(V_Error,'Invalid input object format for '+objdata.name+' got '+objdata.classname+' expected '+FCObjData.classname);
  1014. ObjDataList.Add(objdata);
  1015. end;
  1016. function TExeOutput.FindExeSection(const aname:string):TExeSection;
  1017. begin
  1018. result:=TExeSection(FExeSectionDict.Search(aname));
  1019. end;
  1020. procedure TExeOutput.Load_Start;
  1021. begin
  1022. ObjDataList.Clear;
  1023. { Globals defined in the linker script }
  1024. if not assigned(internalobjdata) then
  1025. internalobjdata:=CObjData.create('*Internal*');
  1026. AddObjData(internalobjdata);
  1027. { Common data section }
  1028. commonObjSection:=internalobjdata.createsection(sec_bss,'');
  1029. end;
  1030. procedure TExeOutput.Load_EntryName(const aname:string);
  1031. begin
  1032. EntryName:=aname;
  1033. end;
  1034. procedure TExeOutput.Load_Symbol(const aname:string);
  1035. begin
  1036. internalobjdata.createsection('*'+aname,0,[]);
  1037. internalobjdata.SymbolDefine(aname,AB_GLOBAL,AT_FUNCTION);
  1038. end;
  1039. procedure TExeOutput.Order_Start;
  1040. begin
  1041. end;
  1042. procedure TExeOutput.Order_End;
  1043. begin
  1044. internalobjdata.afterwrite;
  1045. end;
  1046. procedure TExeOutput.Order_ExeSection(const aname:string);
  1047. var
  1048. sec : TExeSection;
  1049. begin
  1050. sec:=FindExeSection(aname);
  1051. if not assigned(sec) then
  1052. begin
  1053. sec:=CExeSection.create(aname);
  1054. FExeSectionDict.Insert(sec);
  1055. FExeSectionList.Add(sec);
  1056. end;
  1057. { Clear ExeSection contents }
  1058. FCurrExeSec:=sec;
  1059. end;
  1060. procedure TExeOutput.Order_EndExeSection;
  1061. begin
  1062. if not assigned(CurrExeSec) then
  1063. internalerror(200602184);
  1064. FCurrExeSec:=nil;
  1065. end;
  1066. procedure TExeOutput.Order_ObjSection(const aname:string);
  1067. var
  1068. i,j : longint;
  1069. objdata : TObjData;
  1070. objsec : TObjSection;
  1071. begin
  1072. if not assigned(CurrExeSec) then
  1073. internalerror(200602181);
  1074. for i:=0 to ObjDataList.Count-1 do
  1075. begin
  1076. objdata:=TObjData(ObjDataList[i]);
  1077. for j:=0 to objdata.ObjSectionList.Count-1 do
  1078. begin
  1079. objsec:=TObjSection(objdata.ObjSectionList[j]);
  1080. if MatchPattern(aname,objsec.name) then
  1081. CurrExeSec.AddObjSection(objsec);
  1082. end;
  1083. end;
  1084. end;
  1085. procedure TExeOutput.Order_Symbol(const aname:string);
  1086. var
  1087. ObjSection : TObjSection;
  1088. begin
  1089. ObjSection:=internalobjdata.findsection('*'+aname);
  1090. if not assigned(ObjSection) then
  1091. internalerror(200603041);
  1092. ObjSection.SecOptions:=CurrExeSec.SecOptions;
  1093. CurrExeSec.AddObjSection(ObjSection);
  1094. end;
  1095. procedure TExeOutput.Order_Align(const aname:string);
  1096. var
  1097. code : integer;
  1098. alignval : longint;
  1099. objsec : TObjSection;
  1100. begin
  1101. val(aname,alignval,code);
  1102. if alignval<=0 then
  1103. exit;
  1104. { Create an empty section with the required aligning }
  1105. inc(Fzeronr);
  1106. objsec:=internalobjdata.createsection('*align'+tostr(Fzeronr),alignval,CurrExeSec.SecOptions+[oso_data,oso_keep]);
  1107. CurrExeSec.AddObjSection(objsec);
  1108. end;
  1109. procedure TExeOutput.Order_Zeros(const aname:string);
  1110. var
  1111. zeros : array[0..1023] of byte;
  1112. code : integer;
  1113. len : longint;
  1114. objsec : TObjSection;
  1115. begin
  1116. val(aname,len,code);
  1117. if len<=0 then
  1118. exit;
  1119. if len>sizeof(zeros) then
  1120. internalerror(200602254);
  1121. fillchar(zeros,len,0);
  1122. inc(Fzeronr);
  1123. objsec:=internalobjdata.createsection('*zeros'+tostr(Fzeronr),0,CurrExeSec.SecOptions+[oso_data,oso_keep]);
  1124. internalobjdata.writebytes(zeros,len);
  1125. CurrExeSec.AddObjSection(objsec);
  1126. end;
  1127. procedure TExeOutput.Order_Stabs;
  1128. var
  1129. stabexesec,
  1130. stabstrexesec : TExeSection;
  1131. currstabsec,
  1132. currstabstrsec,
  1133. mergedstabsec,
  1134. mergedstabstrsec : TObjSection;
  1135. nextstabreloc,
  1136. currstabreloc : TObjRelocation;
  1137. i,j,
  1138. stabcnt : longint;
  1139. skipstab : boolean;
  1140. hstab : TObjStabEntry;
  1141. stabrelocofs : longint;
  1142. buf : array[0..1023] of byte;
  1143. bufend,
  1144. bufsize : longint;
  1145. begin
  1146. stabexesec:=FindExeSection('.stab');
  1147. stabstrexesec:=FindExeSection('.stabstr');
  1148. if (stabexesec=nil) or
  1149. (stabstrexesec=nil) or
  1150. (stabexesec.ObjSectionlist.count=0) then
  1151. exit;
  1152. { Create new stabsection }
  1153. stabrelocofs:[email protected]@hstab;
  1154. mergedstabsec:=internalobjdata.CreateSection(sec_stab,'');
  1155. mergedstabstrsec:=internalobjdata.CreateSection(sec_stabstr,'');
  1156. { write stab for hdrsym }
  1157. fillchar(hstab,sizeof(TObjStabEntry),0);
  1158. mergedstabsec.write(hstab,sizeof(TObjStabEntry));
  1159. { .stabstr starts with a #0 }
  1160. buf[0]:=0;
  1161. mergedstabstrsec.write(buf[0],1);
  1162. { Copy stabs and corresponding relocations }
  1163. for i:=0 to stabexesec.ObjSectionList.Count-1 do
  1164. begin
  1165. currstabsec:=TObjSection(stabexesec.ObjSectionList[i]);
  1166. currstabstrsec:=currstabsec.objdata.findsection('.stabstr');
  1167. if assigned(currstabstrsec) then
  1168. begin
  1169. stabcnt:=currstabsec.data.size div sizeof(TObjStabEntry);
  1170. currstabsec.data.seek(0);
  1171. currstabreloc:=TObjRelocation(currstabsec.relocations.first);
  1172. for j:=0 to stabcnt-1 do
  1173. begin
  1174. skipstab:=false;
  1175. currstabsec.data.read(hstab,sizeof(TObjStabEntry));
  1176. { Only include first hdrsym stab }
  1177. if hstab.ntype=0 then
  1178. skipstab:=true;
  1179. if not skipstab then
  1180. begin
  1181. { Copy string in stabstr }
  1182. if hstab.strpos<>0 then
  1183. begin
  1184. currstabstrsec.data.seek(hstab.strpos);
  1185. hstab.strpos:=mergedstabstrsec.Size;
  1186. repeat
  1187. bufsize:=currstabstrsec.data.read(buf,sizeof(buf));
  1188. bufend:=indexbyte(buf,bufsize,0);
  1189. if bufend=-1 then
  1190. bufend:=bufsize
  1191. else
  1192. begin
  1193. { include the #0 }
  1194. inc(bufend);
  1195. end;
  1196. mergedstabstrsec.write(buf,bufend);
  1197. until (bufend<>-1) or (bufsize<sizeof(buf));
  1198. end;
  1199. { Copy relocation }
  1200. while assigned(currstabreloc) and
  1201. (currstabreloc.dataoffset<j*sizeof(TObjStabEntry)+stabrelocofs) do
  1202. currstabreloc:=TObjRelocation(currstabreloc.next);
  1203. if assigned(currstabreloc) then
  1204. begin
  1205. if (currstabreloc.dataoffset=j*sizeof(TObjStabEntry)+stabrelocofs) then
  1206. begin
  1207. currstabreloc.dataoffset:=mergedstabsec.Size+stabrelocofs;
  1208. nextstabreloc:=TObjRelocation(currstabreloc.next);
  1209. currstabsec.relocations.remove(currstabreloc);
  1210. mergedstabsec.relocations.concat(currstabreloc);
  1211. currstabreloc:=nextstabreloc;
  1212. end;
  1213. end;
  1214. mergedstabsec.write(hstab,sizeof(hstab));
  1215. end;
  1216. end;
  1217. end;
  1218. { Unload stabs }
  1219. if assigned(currstabstrsec) then
  1220. currstabsec.objdata.removesection(currstabstrsec);
  1221. currstabsec.objdata.removesection(currstabsec);
  1222. end;
  1223. { Generate new HdrSym }
  1224. if mergedstabsec.Size>0 then
  1225. begin
  1226. hstab.strpos:=1;
  1227. hstab.ntype:=0;
  1228. hstab.nother:=0;
  1229. hstab.ndesc:=word((mergedstabsec.Size div sizeof(TObjStabEntry))-1);
  1230. hstab.nvalue:=mergedstabstrsec.Size;
  1231. mergedstabsec.data.seek(0);
  1232. mergedstabsec.data.write(hstab,sizeof(hstab));
  1233. end;
  1234. { Replace all sections with our combined stabsec }
  1235. stabexesec.ObjSectionList.Clear;
  1236. stabstrexesec.ObjSectionList.Clear;
  1237. stabexesec.AddObjSection(mergedstabsec);
  1238. stabstrexesec.AddObjSection(mergedstabstrsec);
  1239. end;
  1240. procedure TExeOutput.CalcPos_ExeSection(const aname:string);
  1241. var
  1242. i : longint;
  1243. objsec : TObjSection;
  1244. begin
  1245. { Section can be removed }
  1246. FCurrExeSec:=FindExeSection(aname);
  1247. if not assigned(CurrExeSec) then
  1248. exit;
  1249. { Alignment of ExeSection }
  1250. CurrMemPos:=align(CurrMemPos,SectionMemAlign);
  1251. CurrExeSec.MemPos:=CurrMemPos;
  1252. if (oso_data in currexesec.SecOptions) then
  1253. begin
  1254. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  1255. CurrExeSec.DataPos:=CurrDataPos;
  1256. end;
  1257. { set position of object ObjSections }
  1258. for i:=0 to CurrExeSec.ObjSectionList.Count-1 do
  1259. begin
  1260. objsec:=TObjSection(CurrExeSec.ObjSectionList[i]);
  1261. { Position in memory }
  1262. objsec.setmempos(CurrMemPos);
  1263. { Position in File }
  1264. if (oso_data in objsec.SecOptions) then
  1265. begin
  1266. if not (oso_data in currexesec.SecOptions) then
  1267. internalerror(200603043);
  1268. if not assigned(objsec.Data) then
  1269. internalerror(200603044);
  1270. objsec.setdatapos(CurrDataPos);
  1271. end;
  1272. end;
  1273. { calculate size of the section }
  1274. CurrExeSec.Size:=CurrMemPos-CurrExeSec.MemPos;
  1275. end;
  1276. procedure TExeOutput.CalcPos_EndExeSection;
  1277. begin
  1278. if not assigned(CurrExeSec) then
  1279. exit;
  1280. FCurrExeSec:=nil;
  1281. end;
  1282. procedure TExeOutput.CalcPos_Start;
  1283. begin
  1284. CurrMemPos:=0;
  1285. CurrDataPos:=0;
  1286. end;
  1287. procedure TExeOutput.CalcPos_Header;
  1288. begin
  1289. end;
  1290. procedure TExeOutput.CalcPos_Symbols;
  1291. begin
  1292. end;
  1293. procedure TExeOutput.ResolveSymbols;
  1294. var
  1295. objdata : TObjData;
  1296. exesym : TExeSymbol;
  1297. objsym,
  1298. commonsym : TObjSymbol;
  1299. firstcommon : boolean;
  1300. i,j : longint;
  1301. begin
  1302. {
  1303. The symbol calculation is done in 3 steps:
  1304. 1. register globals
  1305. register externals
  1306. register commons
  1307. 2. try to find commons, if not found then
  1308. add to the globals (so externals can be resolved)
  1309. 3. try to find externals
  1310. }
  1311. { Step 1, Register symbols }
  1312. for i:=0 to ObjDataList.Count-1 do
  1313. begin
  1314. objdata:=TObjData(ObjDataList[i]);
  1315. for j:=0 to objdata.ObjSymbolList.Count-1 do
  1316. begin
  1317. objsym:=TObjSymbol(objdata.ObjSymbolList[j]);
  1318. { Skip local symbols }
  1319. if objsym.bind=AB_LOCAL then
  1320. continue;
  1321. exesym:=texesymbol(FExeSymbolDict.search(objsym.name));
  1322. if not assigned(exesym) then
  1323. begin
  1324. exesym:=texesymbol.createname(objsym.name);
  1325. FExeSymbolDict.insert(exesym);
  1326. FExeSymbolList.Add(exesym);
  1327. end;
  1328. { Defining the symbol? }
  1329. if objsym.bind=AB_GLOBAL then
  1330. begin
  1331. if not assigned(exesym.ObjSymbol) then
  1332. exesym.ObjSymbol:=objsym
  1333. else
  1334. Comment(V_Error,'Multiple defined symbol '+objsym.name);
  1335. end;
  1336. objsym.exesymbol:=exesym;
  1337. case objsym.bind of
  1338. AB_EXTERNAL :
  1339. ExternalObjSymbols.add(objsym);
  1340. AB_COMMON :
  1341. CommonObjSymbols.add(objsym);
  1342. end;
  1343. end;
  1344. end;
  1345. { Step 2, Match common symbols or add to the globals }
  1346. firstcommon:=true;
  1347. for i:=0 to CommonObjSymbols.count-1 do
  1348. begin
  1349. objsym:=TObjSymbol(CommonObjSymbols[i]);
  1350. if assigned(objsym.exesymbol.objsymbol) then
  1351. begin
  1352. if objsym.exesymbol.ObjSymbol.size<>objsym.size then
  1353. internalerror(200206301)
  1354. else
  1355. begin
  1356. { allocate new objsymbol in .bss of *COMMON* and assign
  1357. it to the exesymbol }
  1358. if firstcommon then
  1359. begin
  1360. if assigned(exemap) then
  1361. exemap.AddCommonSymbolsHeader;
  1362. firstcommon:=false;
  1363. end;
  1364. internalobjdata.setsection(commonObjSection);
  1365. commonsym:=internalobjdata.symboldefine(objsym.name,AB_GLOBAL,AT_FUNCTION);
  1366. commonsym.size:=objsym.size;
  1367. internalobjdata.alloc(objsym.size);
  1368. if assigned(exemap) then
  1369. exemap.AddCommonSymbol(commonsym);
  1370. { Assign to the exesymbol }
  1371. objsym.exesymbol.objsymbol:=commonsym
  1372. end;
  1373. end;
  1374. end;
  1375. { Generate a list of Unresolved External symbols }
  1376. for i:=0 to ExeSymbolList.count-1 do
  1377. begin
  1378. exesym:=TExeSymbol(ExeSymbolList[i]);
  1379. if exesym.objsymbol=nil then
  1380. UnresolvedExeSymbols.Add(exesym);
  1381. end;
  1382. Comment(V_Debug,'Number of unresolved externals in objects '+tostr(UnresolvedExeSymbols.Count));
  1383. { Find entry symbol and print in map }
  1384. exesym:=texesymbol(ExeSymbolDict.search(EntryName));
  1385. if assigned(exesym) then
  1386. begin
  1387. EntrySym:=exesym.ObjSymbol;
  1388. if assigned(exemap) then
  1389. begin
  1390. exemap.Add('');
  1391. exemap.Add('Entry symbol '+EntryName);
  1392. end;
  1393. end
  1394. else
  1395. Comment(V_Error,'Entrypoint '+EntryName+' not defined');
  1396. end;
  1397. procedure TExeOutput.ResolveExternals(const libname:string);
  1398. begin
  1399. end;
  1400. procedure TExeOutput.PrintMemoryMap;
  1401. var
  1402. exesec : TExeSection;
  1403. objsec : TObjSection;
  1404. objsym : TObjSymbol;
  1405. i,j,k : longint;
  1406. begin
  1407. if not assigned(exemap) then
  1408. exit;
  1409. exemap.AddMemoryMapHeader(ImageBase);
  1410. for i:=0 to ExeSections.Count-1 do
  1411. begin
  1412. exesec:=TExeSection(ExeSections[i]);
  1413. exemap.AddMemoryMapExeSection(exesec);
  1414. for j:=0 to exesec.ObjSectionList.count-1 do
  1415. begin
  1416. objsec:=TObjSection(exesec.ObjSectionList[j]);
  1417. exemap.AddMemoryMapObjectSection(objsec);
  1418. for k:=0 to objsec.ObjSymbolDefines.Count-1 do
  1419. begin
  1420. objsym:=TObjSymbol(objsec.ObjSymbolDefines[k]);
  1421. exemap.AddMemoryMapSymbol(objsym);
  1422. end;
  1423. end;
  1424. end;
  1425. end;
  1426. procedure TExeOutput.FixUpSymbols;
  1427. var
  1428. i : longint;
  1429. sym : TObjSymbol;
  1430. begin
  1431. { Update ImageBase to ObjData so it can access from ObjSymbols }
  1432. for i:=0 to ObjDataList.Count-1 do
  1433. TObjData(ObjDataList[i]).imagebase:=imagebase;
  1434. {
  1435. Fixing up symbols is done in the following steps:
  1436. 1. Update common references
  1437. 2. Update external references
  1438. }
  1439. { Step 1, Update commons }
  1440. for i:=0 to CommonObjSymbols.count-1 do
  1441. begin
  1442. sym:=TObjSymbol(CommonObjSymbols[i]);
  1443. if sym.bind=AB_COMMON then
  1444. begin
  1445. { update this symbol }
  1446. sym.bind:=sym.exesymbol.ObjSymbol.bind;
  1447. sym.offset:=sym.exesymbol.ObjSymbol.offset;
  1448. sym.size:=sym.exesymbol.ObjSymbol.size;
  1449. sym.typ:=sym.exesymbol.ObjSymbol.typ;
  1450. sym.ObjSection:=sym.exesymbol.ObjSymbol.ObjSection;
  1451. end;
  1452. end;
  1453. { Step 2, Update externals }
  1454. for i:=0 to ExternalObjSymbols.count-1 do
  1455. begin
  1456. sym:=TObjSymbol(ExternalObjSymbols[i]);
  1457. if sym.bind=AB_EXTERNAL then
  1458. begin
  1459. if assigned(sym.exesymbol.ObjSymbol) then
  1460. begin
  1461. { update this symbol }
  1462. sym.bind:=sym.exesymbol.ObjSymbol.bind;
  1463. sym.offset:=sym.exesymbol.ObjSymbol.offset;
  1464. sym.size:=sym.exesymbol.ObjSymbol.size;
  1465. sym.typ:=sym.exesymbol.ObjSymbol.typ;
  1466. sym.ObjSection:=sym.exesymbol.ObjSymbol.ObjSection;
  1467. end
  1468. else
  1469. Comment(V_Error,'Undefined symbol: '+sym.name);
  1470. end;
  1471. end;
  1472. end;
  1473. procedure TExeOutput.RemoveEmptySections;
  1474. var
  1475. i : longint;
  1476. exesec : TExeSection;
  1477. begin
  1478. for i:=0 to ExeSections.Count-1 do
  1479. begin
  1480. exesec:=TExeSection(ExeSections[i]);
  1481. if not(oso_keep in exesec.SecOptions) and
  1482. (
  1483. (exesec.ObjSectionlist.count=0) or
  1484. (
  1485. (cs_link_strip in aktglobalswitches) and
  1486. (oso_debug in exesec.SecOptions)
  1487. )
  1488. ) then
  1489. begin
  1490. Comment(V_Debug,'Deleting empty section '+exesec.name);
  1491. FExeSectionDict.Delete(exesec.name);
  1492. FExeSectionList[i]:=nil;
  1493. end;
  1494. end;
  1495. ExeSections.Pack;
  1496. end;
  1497. procedure TExeOutput.RemoveUnreferencedSections;
  1498. var
  1499. ObjSectionWorkList : TFPObjectList;
  1500. procedure AddToObjSectionWorkList(aobjsec:TObjSection);
  1501. begin
  1502. if not aobjsec.Used then
  1503. begin
  1504. aobjsec.Used:=true;
  1505. ObjSectionWorkList.Add(aobjsec);
  1506. end;
  1507. end;
  1508. var
  1509. i,j : longint;
  1510. exesec : TExeSection;
  1511. objdata : TObjData;
  1512. refobjsec,
  1513. objsec : TObjSection;
  1514. objsym : TObjSymbol;
  1515. begin
  1516. ObjSectionWorkList:=TFPObjectList.Create(false);
  1517. if assigned(exemap) then
  1518. exemap.AddHeader('Removing unreferenced sections');
  1519. { Initialize by marking all sections unused and
  1520. adding the sections with oso_keep flags to the ObjSectionWorkList }
  1521. for i:=0 to ObjDataList.Count-1 do
  1522. begin
  1523. objdata:=TObjData(ObjDataList[i]);
  1524. for j:=0 to objdata.ObjSectionList.Count-1 do
  1525. begin
  1526. objsec:=TObjSection(objdata.ObjSectionList[j]);
  1527. objsec.Used:=false;
  1528. {$warning TODO remove debug section always keep}
  1529. if oso_debug in objsec.secoptions then
  1530. objsec.Used:=true;
  1531. if (oso_keep in objsec.secoptions) then
  1532. AddToObjSectionWorkList(objsec);
  1533. end;
  1534. end;
  1535. AddToObjSectionWorkList(entrysym.exesymbol.objsymbol.objsection);
  1536. { Process all sections, add new sections to process based
  1537. on the symbol references }
  1538. while ObjSectionWorkList.Count>0 do
  1539. begin
  1540. objsec:=TObjSection(ObjSectionWorkList.Last);
  1541. if assigned(exemap) then
  1542. exemap.Add('Keeping '+objsec.FullName+' '+ToStr(objsec.ObjSymbolRefs.Count)+' references');
  1543. ObjSectionWorkList.Delete(ObjSectionWorkList.Count-1);
  1544. for i:=0 to objsec.ObjSymbolRefs.count-1 do
  1545. begin
  1546. objsym:=TObjSymbol(objsec.ObjSymbolRefs[i]);
  1547. if objsym.bind=AB_LOCAL then
  1548. begin
  1549. if not assigned(objsym.objsection) then
  1550. internalerror(200603062);
  1551. refobjsec:=objsym.objsection
  1552. end
  1553. else
  1554. begin
  1555. if not(assigned(objsym.exesymbol) and
  1556. assigned(objsym.exesymbol.objsymbol)) then
  1557. internalerror(200603063);
  1558. refobjsec:=objsym.exesymbol.objsymbol.objsection;
  1559. end;
  1560. if assigned(exemap) then
  1561. exemap.Add(' References '+refobjsec.fullname);
  1562. AddToObjSectionWorkList(refobjsec);
  1563. end;
  1564. end;
  1565. ObjSectionWorkList.Free;
  1566. ObjSectionWorkList:=nil;
  1567. { Remove unused objsections from exesections }
  1568. for i:=0 to ExeSections.Count-1 do
  1569. begin
  1570. exesec:=TExeSection(ExeSections[i]);
  1571. for j:=0 to exesec.ObjSectionlist.count-1 do
  1572. begin
  1573. objsec:=TObjSection(exesec.ObjSectionlist[j]);
  1574. if not objsec.used then
  1575. begin
  1576. if assigned(exemap) then
  1577. exemap.Add('Removing '+objsec.FullName);
  1578. exesec.ObjSectionlist[j]:=nil;
  1579. end;
  1580. end;
  1581. exesec.ObjSectionlist.Pack;
  1582. end;
  1583. end;
  1584. procedure TExeOutput.FixUpRelocations;
  1585. var
  1586. i : longint;
  1587. objdata : TObjData;
  1588. begin
  1589. for i:=0 to ObjDataList.Count-1 do
  1590. begin
  1591. objdata:=TObjData(ObjDataList[i]);
  1592. objdata.fixuprelocs;
  1593. end;
  1594. end;
  1595. {****************************************************************************
  1596. TObjInput
  1597. ****************************************************************************}
  1598. constructor TObjInput.create;
  1599. begin
  1600. { init reader }
  1601. FReader:=TObjectreader.create;
  1602. end;
  1603. destructor TObjInput.destroy;
  1604. begin
  1605. FReader.free;
  1606. inherited destroy;
  1607. end;
  1608. function TObjInput.newObjData(const n:string):TObjData;
  1609. begin
  1610. result:=CObjData.create(n);
  1611. end;
  1612. function TObjInput.readobjectfile(const fn:string;data:TObjData):boolean;
  1613. begin
  1614. result:=false;
  1615. { start the reader }
  1616. if FReader.openfile(fn) then
  1617. begin
  1618. result:=readObjData(data);
  1619. FReader.closefile;
  1620. end;
  1621. end;
  1622. end.