ogelf.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary elf writer
  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 ogelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,aasmtai,assemble,
  27. { output }
  28. ogbase;
  29. type
  30. TElf32ObjSection = class(TObjSection)
  31. public
  32. secshidx : longint; { index for the section in symtab }
  33. shstridx,
  34. shtype,
  35. shflags,
  36. shlink,
  37. shinfo,
  38. shentsize : longint;
  39. { relocation }
  40. relocsect : TElf32ObjSection;
  41. constructor create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  42. constructor create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  43. destructor destroy;override;
  44. end;
  45. TElf32ObjData = class(TObjData)
  46. public
  47. symtabsect,
  48. strtabsect,
  49. shstrtabsect,
  50. gotpcsect,
  51. gotoffsect,
  52. goTSect,
  53. plTSect,
  54. symsect : TElf32ObjSection;
  55. syms : Tdynamicarray;
  56. constructor create(const n:string);override;
  57. destructor destroy;override;
  58. function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
  59. function sectiontype2align(atype:TAsmSectiontype):shortint;override;
  60. procedure CreateDebugSections;override;
  61. procedure writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);override;
  62. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
  63. end;
  64. TElf32ObjectOutput = class(tObjOutput)
  65. private
  66. elf32data : TElf32ObjData;
  67. symidx,
  68. localsyms : longint;
  69. procedure createrelocsection(s:TElf32ObjSection);
  70. procedure createshstrtab;
  71. procedure createsymtab;
  72. procedure writesectionheader(s:TElf32ObjSection);
  73. procedure writesectiondata(s:TElf32ObjSection);
  74. procedure section_write_symbol(p,arg:pointer);
  75. procedure section_write_sh_string(p,arg:pointer);
  76. procedure section_count_sections(p,arg:pointer);
  77. procedure section_create_relocsec(p,arg:pointer);
  78. procedure section_set_datapos(p,arg:pointer);
  79. procedure section_relocsec_set_datapos(p,arg:pointer);
  80. procedure section_write_data(p,arg:pointer);
  81. procedure section_write_sechdr(p,arg:pointer);
  82. procedure section_write_relocsec(p,arg:pointer);
  83. protected
  84. function writedata(data:TObjData):boolean;override;
  85. public
  86. constructor Create(smart:boolean);override;
  87. end;
  88. TElf32assembler = class(tinternalassembler)
  89. constructor create(smart:boolean);override;
  90. end;
  91. implementation
  92. uses
  93. strings,
  94. verbose,
  95. cutils,globals,fmodule;
  96. const
  97. symbolresize = 200*18;
  98. const
  99. R_386_32 = 1; { ordinary absolute relocation }
  100. R_386_PC32 = 2; { PC-relative relocation }
  101. R_386_GOT32 = 3; { an offset into GOT }
  102. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  103. R_386_GOTOFF = 9; { an offset from GOT base }
  104. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  105. SHN_UNDEF = 0;
  106. SHN_ABS = $fff1;
  107. SHN_COMMON = $fff2;
  108. SHT_NULL = 0;
  109. SHT_PROGBITS = 1;
  110. SHT_SYMTAB = 2;
  111. SHT_STRTAB = 3;
  112. SHT_RELA = 4;
  113. SHT_HASH = 5;
  114. SHT_DYNAMIC = 6;
  115. SHT_NOTE = 7;
  116. SHT_NOBITS = 8;
  117. SHT_REL = 9;
  118. SHT_SHLIB = 10;
  119. SHT_DYNSYM = 11;
  120. SHF_WRITE = 1;
  121. SHF_ALLOC = 2;
  122. SHF_EXECINSTR = 4;
  123. STB_LOCAL = 0;
  124. STB_GLOBAL = 1;
  125. STB_WEAK = 2;
  126. STT_NOTYPE = 0;
  127. STT_OBJECT = 1;
  128. STT_FUNC = 2;
  129. STT_SECTION = 3;
  130. STT_FILE = 4;
  131. type
  132. { Structures which are written directly to the output file }
  133. TElf32header=packed record
  134. magic0123 : longint;
  135. file_class : byte;
  136. data_encoding : byte;
  137. file_version : byte;
  138. padding : array[$07..$0f] of byte;
  139. e_type : word;
  140. e_machine : word;
  141. e_version : longint;
  142. e_entry : longint; { entrypoint }
  143. e_phoff : longint; { program header offset }
  144. e_shoff : longint; { sections header offset }
  145. e_flags : longint;
  146. e_ehsize : word; { elf header size in bytes }
  147. e_phentsize : word; { size of an entry in the program header array }
  148. e_phnum : word; { 0..e_phnum-1 of entrys }
  149. e_shentsize : word; { size of an entry in sections header array }
  150. e_shnum : word; { 0..e_shnum-1 of entrys }
  151. e_shstrndx : word; { index of string section header }
  152. end;
  153. TElf32sechdr=packed record
  154. sh_name : longint;
  155. sh_type : longint;
  156. sh_flags : longint;
  157. sh_addr : longint;
  158. sh_offset : longint;
  159. sh_size : longint;
  160. sh_link : longint;
  161. sh_info : longint;
  162. sh_addralign : longint;
  163. sh_entsize : longint;
  164. end;
  165. TElf32reloc=packed record
  166. address : longint;
  167. info : longint; { bit 0-7: type, 8-31: symbol }
  168. end;
  169. TElf32symbol=packed record
  170. st_name : longint;
  171. st_value : longint;
  172. st_size : longint;
  173. st_info : byte; { bit 0-3: type, 4-7: bind }
  174. st_other : byte;
  175. st_shndx : word;
  176. end;
  177. {****************************************************************************
  178. Helpers
  179. ****************************************************************************}
  180. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  181. begin
  182. { Section Type }
  183. AshType:=SHT_PROGBITS;
  184. if oso_strings in aoptions then
  185. AshType:=SHT_STRTAB
  186. else if not(oso_data in aoptions) then
  187. AshType:=SHT_NOBITS;
  188. { Section Flags }
  189. Ashflags:=0;
  190. if oso_load in aoptions then
  191. Ashflags:=Ashflags or SHF_ALLOC;
  192. if oso_executable in aoptions then
  193. Ashflags:=Ashflags or SHF_EXECINSTR;
  194. if oso_write in aoptions then
  195. Ashflags:=Ashflags or SHF_WRITE;
  196. end;
  197. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  198. begin
  199. aoptions:=[];
  200. { Section Type }
  201. if AshType<>SHT_NOBITS then
  202. include(aoptions,oso_data);
  203. if AshType=SHT_STRTAB then
  204. include(aoptions,oso_strings);
  205. { Section Flags }
  206. if Ashflags and SHF_ALLOC<>0 then
  207. include(aoptions,oso_load)
  208. else
  209. include(aoptions,oso_noload);
  210. if Ashflags and SHF_WRITE<>0 then
  211. include(aoptions,oso_write)
  212. else
  213. include(aoptions,oso_readonly);
  214. end;
  215. {****************************************************************************
  216. TSection
  217. ****************************************************************************}
  218. constructor TElf32ObjSection.create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  219. begin
  220. inherited create(Aname,Aalign,aoptions);
  221. secshidx:=0;
  222. shstridx:=0;
  223. encodesechdrflags(aoptions,shtype,shflags);
  224. shlink:=0;
  225. shinfo:=0;
  226. if name='.stab' then
  227. shentsize:=sizeof(TObjStabEntry);
  228. relocsect:=nil;
  229. end;
  230. constructor TElf32ObjSection.create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  231. var
  232. aoptions : TObjSectionOptions;
  233. begin
  234. decodesechdrflags(Ashtype,Ashflags,aoptions);
  235. inherited create(Aname,Aalign,aoptions);
  236. secshidx:=0;
  237. shstridx:=0;
  238. shtype:=AshType;
  239. shflags:=AshFlags;
  240. shlink:=Ashlink;
  241. shinfo:=Ashinfo;
  242. shentsize:=Aentsize;
  243. relocsect:=nil;
  244. end;
  245. destructor TElf32ObjSection.destroy;
  246. begin
  247. if assigned(relocsect) then
  248. relocsect.free;
  249. inherited destroy;
  250. end;
  251. {****************************************************************************
  252. TElf32ObjData
  253. ****************************************************************************}
  254. constructor TElf32ObjData.create(const n:string);
  255. begin
  256. inherited create(n);
  257. CObjSection:=TElf32ObjSection;
  258. { reset }
  259. Syms:=TDynamicArray.Create(symbolresize);
  260. { default sections }
  261. symtabsect:=TElf32ObjSection.create_ext('.symtab',2,0,0,0,4,16);
  262. strtabsect:=TElf32ObjSection.create_ext('.strtab',3,0,0,0,1,0);
  263. shstrtabsect:=TElf32ObjSection.create_ext('.shstrtab',3,0,0,0,1,0);
  264. { insert the empty and filename as first in strtab }
  265. strtabsect.writestr(#0);
  266. strtabsect.writestr(SplitFileName(current_module.mainsource^)+#0);
  267. { we need at least the following sections }
  268. createsection(sec_code,'');
  269. createsection(sec_data,'');
  270. createsection(sec_bss,'');
  271. if tf_section_threadvars in target_info.flags then
  272. createsection(sec_threadvar,'');
  273. end;
  274. destructor TElf32ObjData.destroy;
  275. begin
  276. Syms.Free;
  277. symtabsect.free;
  278. strtabsect.free;
  279. shstrtabsect.free;
  280. inherited destroy;
  281. end;
  282. function TElf32ObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
  283. const
  284. secnames : array[TAsmSectiontype] of string[13] = ('',
  285. {$ifdef userodata}
  286. '.text','.data','.rodata','.bss','.threadvar',
  287. {$else userodata}
  288. '.text','.data','.data','.bss','.threadvar',
  289. {$endif userodata}
  290. '.text', { darwin stubs }
  291. '.stab','.stabstr',
  292. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  293. '.eh_frame',
  294. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  295. 'fpc',
  296. ''
  297. );
  298. begin
  299. if (use_smartlink_section and
  300. (aname<>'')) or (atype=sec_fpc) then
  301. result:=secnames[atype]+'.'+aname
  302. else
  303. result:=secnames[atype];
  304. end;
  305. function TElf32ObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  306. begin
  307. if atype=sec_stabstr then
  308. result:=1
  309. else
  310. result:=sizeof(aint);
  311. end;
  312. procedure TElf32ObjData.CreateDebugSections;
  313. begin
  314. stabssec:=createsection(sec_stab,'');
  315. stabstrsec:=createsection(sec_stabstr,'');
  316. end;
  317. procedure TElf32ObjData.writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);
  318. var
  319. symaddr : longint;
  320. begin
  321. if CurrObjSec=nil then
  322. internalerror(200403292);
  323. {$ifdef userodata}
  324. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  325. internalerror(200408252);
  326. {$endif userodata}
  327. if assigned(p) then
  328. begin
  329. { real address of the symbol }
  330. symaddr:=p.address;
  331. { Local ObjSymbols can be resolved already or need a section reloc }
  332. if (p.bind=AB_LOCAL) then
  333. begin
  334. { For a relative relocation in the same section the
  335. value can be calculated }
  336. if (p.objsection=CurrObjSec) and
  337. (relative=RELOC_RELATIVE) then
  338. inc(data,symaddr-len-CurrObjSec.Size)
  339. else
  340. begin
  341. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,relative);
  342. inc(data,symaddr);
  343. end;
  344. end
  345. else
  346. begin
  347. CurrObjSec.addsymreloc(CurrObjSec.Size,p,relative);
  348. if relative=RELOC_RELATIVE then
  349. dec(data,len);
  350. end;
  351. end;
  352. CurrObjSec.write(data,len);
  353. end;
  354. procedure TElf32ObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  355. var
  356. stab : TObjStabEntry;
  357. begin
  358. if not assigned(StabsSec) then
  359. internalerror(200602271);
  360. fillchar(stab,sizeof(TObjStabEntry),0);
  361. if assigned(p) and (p[0]<>#0) then
  362. begin
  363. stab.strpos:=stabstrsec.Size;
  364. stabstrsec.write(p^,strlen(p)+1);
  365. end;
  366. stab.ntype:=nidx;
  367. stab.ndesc:=ndesc;
  368. stab.nother:=nother;
  369. stab.nvalue:=offset;
  370. stabssec.write(stab,sizeof(stab));
  371. if assigned(ps) then
  372. stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
  373. end;
  374. {****************************************************************************
  375. TElf32ObjectOutput
  376. ****************************************************************************}
  377. constructor TElf32ObjectOutput.create(smart:boolean);
  378. begin
  379. inherited Create(smart);
  380. CObjData:=TElf32ObjData;
  381. end;
  382. procedure TElf32ObjectOutput.createrelocsection(s:TElf32ObjSection);
  383. var
  384. rel : TElf32reloc;
  385. r : TObjRelocation;
  386. relsym,reltyp : longint;
  387. begin
  388. with elf32data do
  389. begin
  390. {$ifdef userodata}
  391. { rodata can't have relocations }
  392. if s.sectype=sec_rodata then
  393. begin
  394. if assigned(s.relocations.first) then
  395. internalerror(200408251);
  396. exit;
  397. end;
  398. {$endif userodata}
  399. { create the reloc section }
  400. s.relocsect:=TElf32ObjSection.create_ext('.rel'+s.name,9,0,symtabsect.secshidx,s.secshidx,4,8);
  401. { add the relocations }
  402. r:=TObjRelocation(s.relocations.first);
  403. while assigned(r) do
  404. begin
  405. rel.address:=r.dataoffset;
  406. if assigned(r.symbol) then
  407. begin
  408. if (r.symbol.bind=AB_LOCAL) then
  409. relsym:=r.symbol.objsection.secsymidx
  410. else
  411. begin
  412. if r.symbol.symidx=-1 then
  413. internalerror(200603012);
  414. relsym:=r.symbol.symidx;
  415. end;
  416. end
  417. else
  418. if r.objsection<>nil then
  419. relsym:=r.objsection.secsymidx
  420. else
  421. relsym:=SHN_UNDEF;
  422. case r.typ of
  423. RELOC_RELATIVE :
  424. reltyp:=R_386_PC32;
  425. RELOC_ABSOLUTE :
  426. reltyp:=R_386_32;
  427. end;
  428. rel.info:=(relsym shl 8) or reltyp;
  429. { write reloc }
  430. s.relocsect.write(rel,sizeof(rel));
  431. r:=TObjRelocation(r.next);
  432. end;
  433. end;
  434. end;
  435. procedure TElf32ObjectOutput.section_write_symbol(p,arg:pointer);
  436. var
  437. elfsym : TElf32symbol;
  438. begin
  439. fillchar(elfsym,sizeof(elfsym),0);
  440. elfsym.st_name:=TElf32ObjSection(p).shstridx;
  441. elfsym.st_info:=STT_SECTION;
  442. elfsym.st_shndx:=TElf32ObjSection(p).secshidx;
  443. TObjSection(p).secsymidx:=symidx;
  444. inc(symidx);
  445. inc(localsyms);
  446. elf32data.symtabsect.write(elfsym,sizeof(elfsym));
  447. end;
  448. procedure TElf32ObjectOutput.createsymtab;
  449. var
  450. elfsym : TElf32symbol;
  451. i : longint;
  452. objsym : TObjSymbol;
  453. begin
  454. with elf32data do
  455. begin
  456. symidx:=0;
  457. localsyms:=0;
  458. { empty entry }
  459. fillchar(elfsym,sizeof(elfsym),0);
  460. symtabsect.write(elfsym,sizeof(elfsym));
  461. inc(symidx);
  462. inc(localsyms);
  463. { filename entry }
  464. elfsym.st_name:=1;
  465. elfsym.st_info:=STT_FILE;
  466. elfsym.st_shndx:=SHN_ABS;
  467. symtabsect.write(elfsym,sizeof(elfsym));
  468. inc(symidx);
  469. inc(localsyms);
  470. { section }
  471. ObjSectionList.foreach(@section_write_symbol,nil);
  472. { ObjSymbols }
  473. for i:=0 to ObjSymbolList.Count-1 do
  474. begin
  475. objsym:=TObjSymbol(ObjSymbolList[i]);
  476. if objsym.bind<>AB_LOCAL then
  477. begin
  478. fillchar(elfsym,sizeof(elfsym),0);
  479. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  480. elfsym.st_name:=strtabsect.Size;
  481. strtabsect.writestr(objsym.name);
  482. strtabsect.writestr(#0);
  483. elfsym.st_size:=objsym.size;
  484. case objsym.bind of
  485. AB_LOCAL :
  486. begin
  487. elfsym.st_value:=objsym.address;
  488. elfsym.st_info:=STB_LOCAL shl 4;
  489. inc(localsyms);
  490. end;
  491. AB_COMMON :
  492. begin
  493. elfsym.st_value:=$10;
  494. elfsym.st_info:=STB_GLOBAL shl 4;
  495. end;
  496. AB_EXTERNAL :
  497. elfsym.st_info:=STB_GLOBAL shl 4;
  498. AB_GLOBAL :
  499. begin
  500. elfsym.st_value:=objsym.address;
  501. elfsym.st_info:=STB_GLOBAL shl 4;
  502. end;
  503. end;
  504. if (objsym.bind<>AB_EXTERNAL) and
  505. not(assigned(objsym.objsection) and
  506. not(oso_data in objsym.objsection.secoptions)) then
  507. begin
  508. case objsym.typ of
  509. AT_FUNCTION :
  510. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  511. AT_DATA :
  512. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  513. end;
  514. end;
  515. if objsym.bind=AB_COMMON then
  516. elfsym.st_shndx:=SHN_COMMON
  517. else
  518. begin
  519. if assigned(objsym.objsection) then
  520. elfsym.st_shndx:=TElf32ObjSection(objsym.objsection).secshidx
  521. else
  522. elfsym.st_shndx:=SHN_UNDEF;
  523. end;
  524. objsym.symidx:=symidx;
  525. inc(symidx);
  526. symtabsect.write(elfsym,sizeof(elfsym));
  527. end;
  528. end;
  529. { update the .symtab section header }
  530. symtabsect.shlink:=strtabsect.secshidx;
  531. symtabsect.shinfo:=localsyms;
  532. end;
  533. end;
  534. procedure TElf32ObjectOutput.section_write_sh_string(p,arg:pointer);
  535. begin
  536. TElf32ObjSection(p).shstridx:=elf32data.shstrtabsect.writestr(TObjSection(p).name+#0);
  537. if assigned(TElf32ObjSection(p).relocsect) then
  538. TElf32ObjSection(p).relocsect.shstridx:=elf32data.shstrtabsect.writestr(TElf32ObjSection(p).relocsect.name+#0);
  539. end;
  540. procedure TElf32ObjectOutput.createshstrtab;
  541. begin
  542. with elf32data do
  543. begin
  544. with shstrtabsect do
  545. begin
  546. writestr(#0);
  547. symtabsect.shstridx:=writestr('.symtab'#0);
  548. strtabsect.shstridx:=writestr('.strtab'#0);
  549. shstrtabsect.shstridx:=writestr('.shstrtab'#0);
  550. ObjSectionList.foreach(@section_write_sh_string,nil);
  551. end;
  552. end;
  553. end;
  554. procedure TElf32ObjectOutput.writesectionheader(s:TElf32ObjSection);
  555. var
  556. sechdr : TElf32sechdr;
  557. begin
  558. fillchar(sechdr,sizeof(sechdr),0);
  559. sechdr.sh_name:=s.shstridx;
  560. sechdr.sh_type:=s.shtype;
  561. sechdr.sh_flags:=s.shflags;
  562. sechdr.sh_offset:=s.datapos;
  563. sechdr.sh_size:=s.Size;
  564. sechdr.sh_link:=s.shlink;
  565. sechdr.sh_info:=s.shinfo;
  566. sechdr.sh_addralign:=s.secalign;
  567. sechdr.sh_entsize:=s.shentsize;
  568. writer.write(sechdr,sizeof(sechdr));
  569. end;
  570. procedure TElf32ObjectOutput.writesectiondata(s:TElf32ObjSection);
  571. begin
  572. FWriter.writezeros(s.dataalignbytes);
  573. FWriter.writearray(s.data);
  574. end;
  575. procedure TElf32ObjectOutput.section_count_sections(p,arg:pointer);
  576. begin
  577. TElf32ObjSection(p).secshidx:=pword(arg)^;
  578. inc(pword(arg)^);
  579. if TElf32ObjSection(p).relocations.count>0 then
  580. inc(pword(arg)^);
  581. end;
  582. procedure TElf32ObjectOutput.section_create_relocsec(p,arg:pointer);
  583. begin
  584. if (TElf32ObjSection(p).relocations.count>0) then
  585. createrelocsection(TElf32ObjSection(p));
  586. end;
  587. procedure TElf32ObjectOutput.section_set_datapos(p,arg:pointer);
  588. begin
  589. TObjSection(p).setdatapos(paint(arg)^);
  590. end;
  591. procedure TElf32ObjectOutput.section_relocsec_set_datapos(p,arg:pointer);
  592. begin
  593. if assigned(TElf32ObjSection(p).relocsect) then
  594. TElf32ObjSection(p).relocsect.setdatapos(paint(arg)^);
  595. end;
  596. procedure TElf32ObjectOutput.section_write_data(p,arg:pointer);
  597. begin
  598. if (oso_data in TObjSection(p).secoptions) then
  599. begin
  600. if TObjSection(p).data=nil then
  601. internalerror(200403073);
  602. writesectiondata(TElf32ObjSection(p));
  603. end;
  604. end;
  605. procedure TElf32ObjectOutput.section_write_sechdr(p,arg:pointer);
  606. begin
  607. writesectionheader(TElf32ObjSection(p));
  608. if assigned(TElf32ObjSection(p).relocsect) then
  609. writesectionheader(TElf32ObjSection(p).relocsect);
  610. end;
  611. procedure TElf32ObjectOutput.section_write_relocsec(p,arg:pointer);
  612. begin
  613. if assigned(TElf32ObjSection(p).relocsect) then
  614. writesectiondata(TElf32ObjSection(p).relocsect);
  615. end;
  616. function TElf32ObjectOutput.writedata(data:TObjData):boolean;
  617. var
  618. header : TElf32header;
  619. shoffset,
  620. datapos : aint;
  621. nsections : word;
  622. begin
  623. result:=false;
  624. elf32data:=TElf32ObjData(data);
  625. with elf32data do
  626. begin
  627. { calc amount of sections we have }
  628. nsections:=1;
  629. { also create the index in the section header table }
  630. ObjSectionList.foreach(@section_count_sections,@nsections);
  631. { add default sections }
  632. shstrtabsect.secshidx:=nsections;
  633. inc(nsections);
  634. symtabsect.secshidx:=nsections;
  635. inc(nsections);
  636. strtabsect.secshidx:=nsections;
  637. inc(nsections);
  638. { create .symtab and .strtab }
  639. createsymtab;
  640. { Create the relocation sections }
  641. ObjSectionList.foreach(@section_create_relocsec,nil);
  642. { create .shstrtab }
  643. createshstrtab;
  644. { Calculate the filepositions }
  645. datapos:=$40; { elfheader + alignment }
  646. { sections first }
  647. ObjSectionList.foreach(@section_set_datapos,@datapos);
  648. { shstrtab }
  649. shstrtabsect.setdatapos(datapos);
  650. { section headers }
  651. shoffset:=datapos;
  652. inc(datapos,nsections*sizeof(TElf32sechdr));
  653. { symtab }
  654. symtabsect.setdatapos(datapos);
  655. { strtab }
  656. strtabsect.setdatapos(datapos);
  657. { .rel sections }
  658. ObjSectionList.foreach(@section_relocsec_set_datapos,@datapos);
  659. { Write ELF Header }
  660. fillchar(header,sizeof(header),0);
  661. header.magic0123:=$464c457f; { = #127'ELF' }
  662. header.file_class:=1;
  663. header.data_encoding:=1;
  664. header.file_version:=1;
  665. header.e_type:=1;
  666. header.e_machine:=3;
  667. header.e_version:=1;
  668. header.e_shoff:=shoffset;
  669. header.e_shstrndx:=shstrtabsect.secshidx;
  670. header.e_shnum:=nsections;
  671. header.e_ehsize:=sizeof(TElf32header);
  672. header.e_shentsize:=sizeof(TElf32sechdr);
  673. writer.write(header,sizeof(header));
  674. writer.writezeros($40-sizeof(header)); { align }
  675. { Sections }
  676. ObjSectionList.foreach(@section_write_data,nil);
  677. { .shstrtab }
  678. writesectiondata(shstrtabsect);
  679. { section headers, start with an empty header for sh_undef }
  680. writer.writezeros(sizeof(TElf32sechdr));
  681. ObjSectionList.foreach(@section_write_sechdr,nil);
  682. writesectionheader(shstrtabsect);
  683. writesectionheader(symtabsect);
  684. writesectionheader(strtabsect);
  685. { .symtab }
  686. writesectiondata(symtabsect);
  687. { .strtab }
  688. writesectiondata(strtabsect);
  689. { .rel sections }
  690. ObjSectionList.foreach(@section_write_relocsec,nil);
  691. end;
  692. result:=true;
  693. end;
  694. {****************************************************************************
  695. TELFAssembler
  696. ****************************************************************************}
  697. constructor TElf32Assembler.Create(smart:boolean);
  698. begin
  699. inherited Create(smart);
  700. CObjOutput:=TElf32ObjectOutput;
  701. end;
  702. {*****************************************************************************
  703. Initialize
  704. *****************************************************************************}
  705. const
  706. as_i386_elf32_info : tasminfo =
  707. (
  708. id : as_i386_elf32;
  709. idtxt : 'ELF';
  710. asmbin : '';
  711. asmcmd : '';
  712. supported_target : system_any; //target_i386_linux;
  713. flags : [af_outputbinary,af_smartlink_sections];
  714. labelprefix : '.L';
  715. comment : '';
  716. );
  717. initialization
  718. RegisterAssembler(as_i386_elf32_info,TElf32Assembler);
  719. end.