ogelf.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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,aasmdata,assemble,
  27. { output }
  28. ogbase,
  29. owbase;
  30. type
  31. TElfObjSection = class(TObjSection)
  32. public
  33. secshidx : longint; { index for the section in symtab }
  34. shstridx,
  35. shtype,
  36. shflags,
  37. shlink,
  38. shinfo,
  39. shentsize : longint;
  40. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  41. constructor create_ext(AList:TFPHashObjectList;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  42. end;
  43. TElfObjData = class(TObjData)
  44. public
  45. constructor create(const n:string);override;
  46. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  47. procedure CreateDebugSections;override;
  48. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  49. end;
  50. TElfObjectOutput = class(tObjOutput)
  51. private
  52. symtabsect,
  53. strtabsect,
  54. shstrtabsect: TElfObjSection;
  55. {gotpcsect,
  56. gotoffsect,
  57. goTSect,
  58. plTSect,
  59. symsect : TElfObjSection;}
  60. symidx,
  61. localsyms : longint;
  62. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  63. procedure createshstrtab(data:TObjData);
  64. procedure createsymtab(data: TObjData);
  65. procedure writesectionheader(s:TElfObjSection);
  66. procedure write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  67. procedure section_write_symbol(p:TObject;arg:pointer);
  68. procedure section_write_sh_string(p:TObject;arg:pointer);
  69. procedure section_count_sections(p:TObject;arg:pointer);
  70. procedure section_create_relocsec(p:TObject;arg:pointer);
  71. procedure section_write_sechdr(p:TObject;arg:pointer);
  72. protected
  73. function writedata(data:TObjData):boolean;override;
  74. public
  75. constructor Create(AWriter:TObjectWriter);override;
  76. end;
  77. TElfAssembler = class(tinternalassembler)
  78. constructor create(smart:boolean);override;
  79. end;
  80. implementation
  81. uses
  82. SysUtils,
  83. verbose,
  84. cutils,globals,fmodule;
  85. const
  86. symbolresize = 200*18;
  87. const
  88. { Relocation types }
  89. {$ifdef i386}
  90. R_386_32 = 1; { ordinary absolute relocation }
  91. R_386_PC32 = 2; { PC-relative relocation }
  92. R_386_GOT32 = 3; { an offset into GOT }
  93. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  94. R_386_GOTOFF = 9; { an offset from GOT base }
  95. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  96. R_386_GNU_VTINHERIT = 250;
  97. R_386_GNU_VTENTRY = 251;
  98. {$endif i386}
  99. {$ifdef sparc}
  100. R_SPARC_32 = 3;
  101. R_SPARC_WDISP30 = 7;
  102. R_SPARC_HI22 = 9;
  103. R_SPARC_LO10 = 12;
  104. R_SPARC_GNU_VTINHERIT = 250;
  105. R_SPARC_GNU_VTENTRY = 251;
  106. {$endif sparc}
  107. {$ifdef x86_64}
  108. R_X86_64_NONE = 0;
  109. { Direct 64 bit }
  110. R_X86_64_64 = 1;
  111. { PC relative 32 bit signed }
  112. R_X86_64_PC32 = 2;
  113. { 32 bit GOT entry }
  114. R_X86_64_GOT32 = 3;
  115. { 32 bit PLT address }
  116. R_X86_64_PLT32 = 4;
  117. { Copy symbol at runtime }
  118. R_X86_64_COPY = 5;
  119. { Create GOT entry }
  120. R_X86_64_GLOB_DAT = 6;
  121. { Create PLT entry }
  122. R_X86_64_JUMP_SLOT = 7;
  123. { Adjust by program base }
  124. R_X86_64_RELATIVE = 8;
  125. { 32 bit signed PC relative offset to GOT }
  126. R_X86_64_GOTPCREL = 9;
  127. { Direct 32 bit zero extended }
  128. R_X86_64_32 = 10;
  129. { Direct 32 bit sign extended }
  130. R_X86_64_32S = 11;
  131. { Direct 16 bit zero extended }
  132. R_X86_64_16 = 12;
  133. { 16 bit sign extended PC relative }
  134. R_X86_64_PC16 = 13;
  135. { Direct 8 bit sign extended }
  136. R_X86_64_8 = 14;
  137. { 8 bit sign extended PC relative }
  138. R_X86_64_PC8 = 15;
  139. { ID of module containing symbol }
  140. R_X86_64_DTPMOD64 = 16;
  141. { Offset in module's TLS block }
  142. R_X86_64_DTPOFF64 = 17;
  143. { Offset in initial TLS block }
  144. R_X86_64_TPOFF64 = 18;
  145. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  146. R_X86_64_TLSGD = 19;
  147. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  148. R_X86_64_TLSLD = 20;
  149. { Offset in TLS block }
  150. R_X86_64_DTPOFF32 = 21;
  151. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  152. R_X86_64_GOTTPOFF = 22;
  153. { Offset in initial TLS block }
  154. R_X86_64_TPOFF32 = 23;
  155. { GNU extension to record C++ vtable hierarchy }
  156. R_X86_64_GNU_VTINHERIT = 24;
  157. { GNU extension to record C++ vtable member usage }
  158. R_X86_64_GNU_VTENTRY = 25;
  159. {$endif x86_64}
  160. { ELFHeader.file_class }
  161. ELFCLASSNONE = 0;
  162. ELFCLASS32 = 1;
  163. ELFCLASS64 = 2;
  164. { ELFHeader.e_type }
  165. ET_NONE = 0;
  166. ET_REL = 1;
  167. ET_EXEC = 2;
  168. ET_DYN = 3;
  169. ET_CORE = 4;
  170. { ELFHeader.e_machine }
  171. EM_SPARC = 2;
  172. EM_386 = 3;
  173. EM_M68K = 4;
  174. EM_PPC = 20;
  175. EM_ARM = 40;
  176. EM_X86_64 = 62;
  177. {$ifdef sparc}
  178. ELFMACHINE = EM_SPARC;
  179. {$endif sparc}
  180. {$ifdef i386}
  181. ELFMACHINE = EM_386;
  182. {$endif i386}
  183. {$ifdef m68k}
  184. ELFMACHINE = EM_M68K;
  185. {$endif m68k}
  186. {$ifdef powerpc}
  187. ELFMACHINE = EM_PPC;
  188. {$endif powerpc}
  189. {$ifdef arm}
  190. ELFMACHINE = EM_ARM;
  191. {$endif arm}
  192. {$ifdef x86_64}
  193. ELFMACHINE = EM_X86_64;
  194. {$endif x86_64}
  195. SHN_UNDEF = 0;
  196. SHN_ABS = $fff1;
  197. SHN_COMMON = $fff2;
  198. SHT_NULL = 0;
  199. SHT_PROGBITS = 1;
  200. SHT_SYMTAB = 2;
  201. SHT_STRTAB = 3;
  202. SHT_RELA = 4;
  203. SHT_HASH = 5;
  204. SHT_DYNAMIC = 6;
  205. SHT_NOTE = 7;
  206. SHT_NOBITS = 8;
  207. SHT_REL = 9;
  208. SHT_SHLIB = 10;
  209. SHT_DYNSYM = 11;
  210. SHF_WRITE = 1;
  211. SHF_ALLOC = 2;
  212. SHF_EXECINSTR = 4;
  213. STB_LOCAL = 0;
  214. STB_GLOBAL = 1;
  215. STB_WEAK = 2;
  216. STT_NOTYPE = 0;
  217. STT_OBJECT = 1;
  218. STT_FUNC = 2;
  219. STT_SECTION = 3;
  220. STT_FILE = 4;
  221. type
  222. { Structures which are written directly to the output file }
  223. TElf32header=packed record
  224. magic : array[0..3] of byte;
  225. file_class : byte;
  226. data_encoding : byte;
  227. file_version : byte;
  228. padding : array[$07..$0f] of byte;
  229. e_type : word;
  230. e_machine : word;
  231. e_version : longint;
  232. e_entry : longint; { entrypoint }
  233. e_phoff : longint; { program header offset }
  234. e_shoff : longint; { sections header offset }
  235. e_flags : longint;
  236. e_ehsize : word; { elf header size in bytes }
  237. e_phentsize : word; { size of an entry in the program header array }
  238. e_phnum : word; { 0..e_phnum-1 of entrys }
  239. e_shentsize : word; { size of an entry in sections header array }
  240. e_shnum : word; { 0..e_shnum-1 of entrys }
  241. e_shstrndx : word; { index of string section header }
  242. end;
  243. TElf32sechdr=packed record
  244. sh_name : longint;
  245. sh_type : longint;
  246. sh_flags : longint;
  247. sh_addr : longint;
  248. sh_offset : longint;
  249. sh_size : longint;
  250. sh_link : longint;
  251. sh_info : longint;
  252. sh_addralign : longint;
  253. sh_entsize : longint;
  254. end;
  255. TElf32proghdr=packed record
  256. p_type : longword;
  257. p_offset : longword;
  258. p_vaddr : longword;
  259. p_paddr : longword;
  260. p_filesz : longword;
  261. p_memsz : longword;
  262. p_flags : longword;
  263. p_align : longword;
  264. end;
  265. TElf32reloc=packed record
  266. address : longint;
  267. info : longint; { bit 0-7: type, 8-31: symbol }
  268. end;
  269. TElf32symbol=packed record
  270. st_name : longint;
  271. st_value : longint;
  272. st_size : longint;
  273. st_info : byte; { bit 0-3: type, 4-7: bind }
  274. st_other : byte;
  275. st_shndx : word;
  276. end;
  277. TElf32Dyn=packed record
  278. d_tag: longword;
  279. case integer of
  280. 0: (d_val: longword);
  281. 1: (d_ptr: longword);
  282. end;
  283. telf64header=packed record
  284. magic : array[0..3] of byte;
  285. file_class : byte;
  286. data_encoding : byte;
  287. file_version : byte;
  288. padding : array[$07..$0f] of byte;
  289. e_type : word;
  290. e_machine : word;
  291. e_version : longint;
  292. e_entry : qword; { entrypoint }
  293. e_phoff : qword; { program header offset }
  294. e_shoff : qword; { sections header offset }
  295. e_flags : longint;
  296. e_ehsize : word; { elf header size in bytes }
  297. e_phentsize : word; { size of an entry in the program header array }
  298. e_phnum : word; { 0..e_phnum-1 of entrys }
  299. e_shentsize : word; { size of an entry in sections header array }
  300. e_shnum : word; { 0..e_shnum-1 of entrys }
  301. e_shstrndx : word; { index of string section header }
  302. end;
  303. telf64sechdr=packed record
  304. sh_name : longint;
  305. sh_type : longint;
  306. sh_flags : qword;
  307. sh_addr : qword;
  308. sh_offset : qword;
  309. sh_size : qword;
  310. sh_link : longint;
  311. sh_info : longint;
  312. sh_addralign : qword;
  313. sh_entsize : qword;
  314. end;
  315. telf64proghdr=packed record
  316. p_type : longword;
  317. p_flags : longword;
  318. p_offset : qword;
  319. p_vaddr : qword;
  320. p_paddr : qword;
  321. p_filesz : qword;
  322. p_memsz : qword;
  323. p_align : qword;
  324. end;
  325. telf64reloc=packed record
  326. address : qword;
  327. info : qword; { bit 0-31: type, 32-63: symbol }
  328. addend : int64; { signed! }
  329. end;
  330. telf64symbol=packed record
  331. st_name : longint;
  332. st_info : byte; { bit 0-3: type, 4-7: bind }
  333. st_other : byte;
  334. st_shndx : word;
  335. st_value : qword;
  336. st_size : qword;
  337. end;
  338. TElf64Dyn=packed record
  339. d_tag: qword;
  340. case integer of
  341. 0: (d_val: qword);
  342. 1: (d_ptr: qword);
  343. end;
  344. {$ifdef cpu64bitaddr}
  345. const
  346. ELFCLASS = ELFCLASS64;
  347. type
  348. telfheader = telf64header;
  349. telfreloc = telf64reloc;
  350. telfsymbol = telf64symbol;
  351. telfsechdr = telf64sechdr;
  352. telfproghdr = telf64proghdr;
  353. telfdyn = telf64dyn;
  354. {$else cpu64bitaddr}
  355. const
  356. ELFCLASS = ELFCLASS32;
  357. type
  358. telfheader = telf32header;
  359. telfreloc = telf32reloc;
  360. telfsymbol = telf32symbol;
  361. telfsechdr = telf32sechdr;
  362. telfproghdr = telf32proghdr;
  363. telfdyn = telf32dyn;
  364. {$endif cpu64bitaddr}
  365. procedure MayBeSwapHeader(var h : telf32header);
  366. begin
  367. if source_info.endian<>target_info.endian then
  368. with h do
  369. begin
  370. e_type:=swapendian(e_type);
  371. e_machine:=swapendian(e_machine);
  372. e_version:=swapendian(e_version);
  373. e_entry:=swapendian(e_entry);
  374. e_phoff:=swapendian(e_phoff);
  375. e_shoff:=swapendian(e_shoff);
  376. e_flags:=swapendian(e_flags);
  377. e_ehsize:=swapendian(e_ehsize);
  378. e_phentsize:=swapendian(e_phentsize);
  379. e_phnum:=swapendian(e_phnum);
  380. e_shentsize:=swapendian(e_shentsize);
  381. e_shnum:=swapendian(e_shnum);
  382. e_shstrndx:=swapendian(e_shstrndx);
  383. end;
  384. end;
  385. procedure MayBeSwapHeader(var h : telf64header);
  386. begin
  387. if source_info.endian<>target_info.endian then
  388. with h do
  389. begin
  390. e_type:=swapendian(e_type);
  391. e_machine:=swapendian(e_machine);
  392. e_version:=swapendian(e_version);
  393. e_entry:=swapendian(e_entry);
  394. e_phoff:=swapendian(e_phoff);
  395. e_shoff:=swapendian(e_shoff);
  396. e_flags:=swapendian(e_flags);
  397. e_ehsize:=swapendian(e_ehsize);
  398. e_phentsize:=swapendian(e_phentsize);
  399. e_phnum:=swapendian(e_phnum);
  400. e_shentsize:=swapendian(e_shentsize);
  401. e_shnum:=swapendian(e_shnum);
  402. e_shstrndx:=swapendian(e_shstrndx);
  403. end;
  404. end;
  405. procedure MayBeSwapHeader(var h : telf32proghdr);
  406. begin
  407. if source_info.endian<>target_info.endian then
  408. with h do
  409. begin
  410. p_align:=swapendian(p_align);
  411. p_filesz:=swapendian(p_filesz);
  412. p_flags:=swapendian(p_flags);
  413. p_memsz:=swapendian(p_memsz);
  414. p_offset:=swapendian(p_offset);
  415. p_paddr:=swapendian(p_paddr);
  416. p_type:=swapendian(p_type);
  417. p_vaddr:=swapendian(p_vaddr);
  418. end;
  419. end;
  420. procedure MayBeSwapHeader(var h : telf64proghdr);
  421. begin
  422. if source_info.endian<>target_info.endian then
  423. with h do
  424. begin
  425. p_align:=swapendian(p_align);
  426. p_filesz:=swapendian(p_filesz);
  427. p_flags:=swapendian(p_flags);
  428. p_memsz:=swapendian(p_memsz);
  429. p_offset:=swapendian(p_offset);
  430. p_paddr:=swapendian(p_paddr);
  431. p_type:=swapendian(p_type);
  432. p_vaddr:=swapendian(p_vaddr);
  433. end;
  434. end;
  435. procedure MaybeSwapSecHeader(var h : telf32sechdr);
  436. begin
  437. if source_info.endian<>target_info.endian then
  438. with h do
  439. begin
  440. sh_name:=swapendian(sh_name);
  441. sh_type:=swapendian(sh_type);
  442. sh_flags:=swapendian(sh_flags);
  443. sh_addr:=swapendian(sh_addr);
  444. sh_offset:=swapendian(sh_offset);
  445. sh_size:=swapendian(sh_size);
  446. sh_link:=swapendian(sh_link);
  447. sh_info:=swapendian(sh_info);
  448. sh_addralign:=swapendian(sh_addralign);
  449. sh_entsize:=swapendian(sh_entsize);
  450. end;
  451. end;
  452. procedure MaybeSwapSecHeader(var h : telf64sechdr);
  453. begin
  454. if source_info.endian<>target_info.endian then
  455. with h do
  456. begin
  457. sh_name:=swapendian(sh_name);
  458. sh_type:=swapendian(sh_type);
  459. sh_flags:=swapendian(sh_flags);
  460. sh_addr:=swapendian(sh_addr);
  461. sh_offset:=swapendian(sh_offset);
  462. sh_size:=swapendian(sh_size);
  463. sh_link:=swapendian(sh_link);
  464. sh_info:=swapendian(sh_info);
  465. sh_addralign:=swapendian(sh_addralign);
  466. sh_entsize:=swapendian(sh_entsize);
  467. end;
  468. end;
  469. procedure MaybeSwapElfSymbol(var h : telf32symbol);
  470. begin
  471. if source_info.endian<>target_info.endian then
  472. with h do
  473. begin
  474. st_name:=swapendian(st_name);
  475. st_value:=swapendian(st_value);
  476. st_size:=swapendian(st_size);
  477. st_shndx:=swapendian(st_shndx);
  478. end;
  479. end;
  480. procedure MaybeSwapElfSymbol(var h : telf64symbol);
  481. begin
  482. if source_info.endian<>target_info.endian then
  483. with h do
  484. begin
  485. st_name:=swapendian(st_name);
  486. st_value:=swapendian(st_value);
  487. st_size:=swapendian(st_size);
  488. st_shndx:=swapendian(st_shndx);
  489. end;
  490. end;
  491. procedure MaybeSwapElfReloc(var h : telf32reloc);
  492. begin
  493. if source_info.endian<>target_info.endian then
  494. with h do
  495. begin
  496. address:=swapendian(address);
  497. info:=swapendian(info);
  498. end;
  499. end;
  500. procedure MaybeSwapElfReloc(var h : telf64reloc);
  501. begin
  502. if source_info.endian<>target_info.endian then
  503. with h do
  504. begin
  505. address:=swapendian(address);
  506. info:=swapendian(info);
  507. addend:=swapendian(addend);
  508. end;
  509. end;
  510. procedure MaybeSwapElfDyn(var h : telf32dyn);
  511. begin
  512. if source_info.endian<>target_info.endian then
  513. with h do
  514. begin
  515. d_tag:=swapendian(d_tag);
  516. d_val:=swapendian(d_val);
  517. end;
  518. end;
  519. procedure MaybeSwapElfDyn(var h : telf64dyn);
  520. begin
  521. if source_info.endian<>target_info.endian then
  522. with h do
  523. begin
  524. d_tag:=swapendian(d_tag);
  525. d_val:=swapendian(d_val);
  526. end;
  527. end;
  528. {****************************************************************************
  529. Helpers
  530. ****************************************************************************}
  531. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  532. begin
  533. { Section Type }
  534. AshType:=SHT_PROGBITS;
  535. if oso_strings in aoptions then
  536. AshType:=SHT_STRTAB
  537. else if not(oso_data in aoptions) then
  538. AshType:=SHT_NOBITS;
  539. { Section Flags }
  540. Ashflags:=0;
  541. if oso_load in aoptions then
  542. Ashflags:=Ashflags or SHF_ALLOC;
  543. if oso_executable in aoptions then
  544. Ashflags:=Ashflags or SHF_EXECINSTR;
  545. if oso_write in aoptions then
  546. Ashflags:=Ashflags or SHF_WRITE;
  547. end;
  548. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  549. begin
  550. aoptions:=[];
  551. { Section Type }
  552. if AshType<>SHT_NOBITS then
  553. include(aoptions,oso_data);
  554. if AshType=SHT_STRTAB then
  555. include(aoptions,oso_strings);
  556. { Section Flags }
  557. if Ashflags and SHF_ALLOC<>0 then
  558. include(aoptions,oso_load)
  559. else
  560. include(aoptions,oso_noload);
  561. if Ashflags and SHF_WRITE<>0 then
  562. include(aoptions,oso_write)
  563. else
  564. include(aoptions,oso_readonly);
  565. if Ashflags and SHF_EXECINSTR<>0 then
  566. include(aoptions,oso_executable);
  567. end;
  568. {****************************************************************************
  569. TElfObjSection
  570. ****************************************************************************}
  571. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  572. begin
  573. inherited create(AList,Aname,Aalign,aoptions);
  574. secshidx:=0;
  575. shstridx:=0;
  576. encodesechdrflags(aoptions,shtype,shflags);
  577. shlink:=0;
  578. shinfo:=0;
  579. if name='.stab' then
  580. shentsize:=sizeof(TObjStabEntry);
  581. end;
  582. constructor TElfObjSection.create_ext(AList:TFPHashObjectList;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  583. var
  584. aoptions : TObjSectionOptions;
  585. begin
  586. decodesechdrflags(Ashtype,Ashflags,aoptions);
  587. inherited create(AList,Aname,Aalign,aoptions);
  588. secshidx:=0;
  589. shstridx:=0;
  590. shtype:=AshType;
  591. shflags:=AshFlags;
  592. shlink:=Ashlink;
  593. shinfo:=Ashinfo;
  594. shentsize:=Aentsize;
  595. end;
  596. {****************************************************************************
  597. TElfObjData
  598. ****************************************************************************}
  599. constructor TElfObjData.create(const n:string);
  600. begin
  601. inherited create(n);
  602. CObjSection:=TElfObjSection;
  603. { we need at least the following sections }
  604. createsection(sec_code);
  605. { always a non-PIC data section (will remain empty if doing PIC) }
  606. createsection('.data',sizeof(pint),sectiontype2options(sec_data));
  607. createsection(sec_bss);
  608. if (cs_create_pic in current_settings.moduleswitches) and
  609. not(target_info.system in systems_darwin) then
  610. createsection(sec_data);
  611. if tf_section_threadvars in target_info.flags then
  612. createsection(sec_threadvar);
  613. if (tf_needs_dwarf_cfi in target_info.flags) and
  614. (af_supports_dwarf in target_asm.flags) then
  615. createsection(sec_debug_frame);
  616. end;
  617. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  618. const
  619. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  620. {$ifdef userodata}
  621. '.text','.data','.data','.rodata','.bss','.threadvar',
  622. {$else userodata}
  623. '.text','.data','.data','.data','.bss','.threadvar',
  624. {$endif userodata}
  625. '.pdata',
  626. '.text', { darwin stubs }
  627. '__DATA,__nl_symbol_ptr',
  628. '__DATA,__la_symbol_ptr',
  629. '__DATA,__mod_init_func',
  630. '__DATA,__mod_term_func',
  631. '.stab','.stabstr',
  632. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  633. '.eh_frame',
  634. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  635. '.fpc',
  636. '.toc',
  637. '.init',
  638. '.fini',
  639. '.objc_class',
  640. '.objc_meta_class',
  641. '.objc_cat_cls_meth',
  642. '.objc_cat_inst_meth',
  643. '.objc_protocol',
  644. '.objc_string_object',
  645. '.objc_cls_meth',
  646. '.objc_inst_meth',
  647. '.objc_cls_refs',
  648. '.objc_message_refs',
  649. '.objc_symbols',
  650. '.objc_category',
  651. '.objc_class_vars',
  652. '.objc_instance_vars',
  653. '.objc_module_info',
  654. '.objc_class_names',
  655. '.objc_meth_var_types',
  656. '.objc_meth_var_names',
  657. '.objc_selector_strs',
  658. '.objc_protocol_ext',
  659. '.objc_class_ext',
  660. '.objc_property',
  661. '.objc_image_info',
  662. '.objc_cstring_object',
  663. '.objc_sel_fixup',
  664. '__DATA,__objc_data',
  665. '__DATA,__objc_const',
  666. '.objc_superrefs',
  667. '__DATA, __datacoal_nt,coalesced',
  668. '.objc_classlist',
  669. '.objc_nlclasslist',
  670. '.objc_catlist',
  671. '.obcj_nlcatlist',
  672. '.objc_protolist'
  673. );
  674. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  675. '.text',
  676. '.data.rel',
  677. '.data.rel',
  678. '.data.rel',
  679. '.bss',
  680. '.threadvar',
  681. '.pdata',
  682. '', { stubs }
  683. '__DATA,__nl_symbol_ptr',
  684. '__DATA,__la_symbol_ptr',
  685. '__DATA,__mod_init_func',
  686. '__DATA,__mod_term_func',
  687. '.stab',
  688. '.stabstr',
  689. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  690. '.eh_frame',
  691. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  692. '.fpc',
  693. '.toc',
  694. '.init',
  695. '.fini',
  696. '.objc_class',
  697. '.objc_meta_class',
  698. '.objc_cat_cls_meth',
  699. '.objc_cat_inst_meth',
  700. '.objc_protocol',
  701. '.objc_string_object',
  702. '.objc_cls_meth',
  703. '.objc_inst_meth',
  704. '.objc_cls_refs',
  705. '.objc_message_refs',
  706. '.objc_symbols',
  707. '.objc_category',
  708. '.objc_class_vars',
  709. '.objc_instance_vars',
  710. '.objc_module_info',
  711. '.objc_class_names',
  712. '.objc_meth_var_types',
  713. '.objc_meth_var_names',
  714. '.objc_selector_strs',
  715. '.objc_protocol_ext',
  716. '.objc_class_ext',
  717. '.objc_property',
  718. '.objc_image_info',
  719. '.objc_cstring_object',
  720. '.objc_sel_fixup',
  721. '__DATA,__objc_data',
  722. '__DATA,__objc_const',
  723. '.objc_superrefs',
  724. '__DATA, __datacoal_nt,coalesced',
  725. '.objc_classlist',
  726. '.objc_nlclasslist',
  727. '.objc_catlist',
  728. '.obcj_nlcatlist',
  729. '.objc_protolist'
  730. );
  731. var
  732. sep : string[3];
  733. secname : string;
  734. begin
  735. { section type user gives the user full controll on the section name }
  736. if atype=sec_user then
  737. result:=aname
  738. else
  739. begin
  740. if (cs_create_pic in current_settings.moduleswitches) and
  741. not(target_info.system in systems_darwin) then
  742. secname:=secnames_pic[atype]
  743. else
  744. secname:=secnames[atype];
  745. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  746. begin
  747. result:=secname+'.'+aname;
  748. exit;
  749. end;
  750. if create_smartlink_sections and (aname<>'') then
  751. begin
  752. case aorder of
  753. secorder_begin :
  754. sep:='.b_';
  755. secorder_end :
  756. sep:='.z_';
  757. else
  758. sep:='.n_';
  759. end;
  760. result:=secname+sep+aname
  761. end
  762. else
  763. result:=secname;
  764. end;
  765. end;
  766. procedure TElfObjData.CreateDebugSections;
  767. begin
  768. if target_dbg.id=dbg_stabs then
  769. begin
  770. stabssec:=createsection(sec_stab);
  771. stabstrsec:=createsection(sec_stabstr);
  772. end;
  773. end;
  774. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  775. var
  776. symaddr : aint;
  777. begin
  778. if CurrObjSec=nil then
  779. internalerror(200403292);
  780. {$ifdef userodata}
  781. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  782. internalerror(200408252);
  783. {$endif userodata}
  784. { Using RELOC_RVA to map 32-bit RELOC_ABSOLUTE to R_X86_64_32
  785. (RELOC_ABSOLUTE maps to R_X86_64_32S) }
  786. if (reltype=RELOC_ABSOLUTE) and (len<>sizeof(pint)) then
  787. reltype:=RELOC_RVA;
  788. if assigned(p) then
  789. begin
  790. { real address of the symbol }
  791. symaddr:=p.address;
  792. { Local ObjSymbols can be resolved already or need a section reloc }
  793. if (p.bind=AB_LOCAL) and
  794. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32,RELOC_RVA{$endif x86_64}]) then
  795. begin
  796. { For a reltype relocation in the same section the
  797. value can be calculated }
  798. if (p.objsection=CurrObjSec) and
  799. (reltype=RELOC_RELATIVE) then
  800. inc(data,symaddr-len-CurrObjSec.Size)
  801. else
  802. begin
  803. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,reltype);
  804. inc(data,symaddr);
  805. end;
  806. end
  807. else
  808. begin
  809. CurrObjSec.addsymreloc(CurrObjSec.Size,p,reltype);
  810. {$ifndef x86_64}
  811. if (reltype=RELOC_RELATIVE) or (reltype=RELOC_PLT32) then
  812. dec(data,len);
  813. {$endif x86_64}
  814. end;
  815. end;
  816. CurrObjSec.write(data,len);
  817. end;
  818. {****************************************************************************
  819. TElfObjectOutput
  820. ****************************************************************************}
  821. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  822. begin
  823. inherited Create(AWriter);
  824. CObjData:=TElfObjData;
  825. end;
  826. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  827. var
  828. i : longint;
  829. rel : telfreloc;
  830. objreloc : TObjRelocation;
  831. relsym,
  832. reltyp : longint;
  833. relocsect : TObjSection;
  834. {$ifdef x86_64}
  835. tmp: aint;
  836. asize: longint;
  837. {$endif x86_64}
  838. begin
  839. with data do
  840. begin
  841. {$ifdef userodata}
  842. { rodata can't have relocations }
  843. if s.sectype=sec_rodata then
  844. begin
  845. if assigned(s.relocations.first) then
  846. internalerror(200408251);
  847. exit;
  848. end;
  849. {$endif userodata}
  850. { create the reloc section }
  851. {$ifdef i386}
  852. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  853. {$else i386}
  854. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  855. {$endif i386}
  856. { add the relocations }
  857. for i:=0 to s.Objrelocations.count-1 do
  858. begin
  859. objreloc:=TObjRelocation(s.Objrelocations[i]);
  860. fillchar(rel,sizeof(rel),0);
  861. rel.address:=objreloc.dataoffset;
  862. { when things settle down, we can create processor specific
  863. derived classes }
  864. case objreloc.typ of
  865. {$ifdef i386}
  866. RELOC_RELATIVE :
  867. reltyp:=R_386_PC32;
  868. RELOC_ABSOLUTE :
  869. reltyp:=R_386_32;
  870. RELOC_GOT32 :
  871. reltyp:=R_386_GOT32;
  872. RELOC_GOTPC :
  873. reltyp:=R_386_GOTPC;
  874. RELOC_PLT32 :
  875. begin
  876. reltyp:=R_386_PLT32;
  877. end;
  878. {$endif i386}
  879. {$ifdef sparc}
  880. RELOC_ABSOLUTE :
  881. reltyp:=R_SPARC_32;
  882. {$endif sparc}
  883. {$ifdef x86_64}
  884. RELOC_RELATIVE :
  885. begin
  886. reltyp:=R_X86_64_PC32;
  887. { length of the relocated location is handled here }
  888. rel.addend:=-4;
  889. end;
  890. RELOC_ABSOLUTE :
  891. reltyp:=R_X86_64_64;
  892. RELOC_ABSOLUTE32 :
  893. reltyp:=R_X86_64_32S;
  894. RELOC_RVA :
  895. reltyp:=R_X86_64_32;
  896. RELOC_GOTPCREL :
  897. begin
  898. reltyp:=R_X86_64_GOTPCREL;
  899. { length of the relocated location is handled here }
  900. rel.addend:=-4;
  901. end;
  902. RELOC_PLT32 :
  903. begin
  904. reltyp:=R_X86_64_PLT32;
  905. { length of the relocated location is handled here }
  906. rel.addend:=-4;
  907. end;
  908. {$endif x86_64}
  909. else
  910. internalerror(200602261);
  911. end;
  912. { This handles ELF 'rela'-styled relocations, which are currently used only for x86_64,
  913. but can be used other targets, too. }
  914. {$ifdef x86_64}
  915. s.Data.Seek(objreloc.dataoffset);
  916. if objreloc.typ=RELOC_ABSOLUTE then
  917. begin
  918. asize:=8;
  919. s.Data.Read(tmp,8);
  920. rel.addend:=rel.addend+tmp;
  921. end
  922. else
  923. begin
  924. asize:=4;
  925. s.Data.Read(tmp,4);
  926. rel.addend:=rel.addend+longint(tmp);
  927. end;
  928. { and zero the data member out }
  929. tmp:=0;
  930. s.Data.Seek(objreloc.dataoffset);
  931. s.Data.Write(tmp,asize);
  932. {$endif}
  933. { Symbol }
  934. if assigned(objreloc.symbol) then
  935. begin
  936. if objreloc.symbol.symidx=-1 then
  937. begin
  938. writeln(objreloc.symbol.Name);
  939. internalerror(200603012);
  940. end;
  941. relsym:=objreloc.symbol.symidx;
  942. end
  943. else
  944. begin
  945. if objreloc.objsection<>nil then
  946. relsym:=objreloc.objsection.secsymidx
  947. else
  948. relsym:=SHN_UNDEF;
  949. end;
  950. {$ifdef cpu64bitaddr}
  951. rel.info:=(qword(relsym) shl 32) or reltyp;
  952. {$else cpu64bitaddr}
  953. rel.info:=(relsym shl 8) or reltyp;
  954. {$endif cpu64bitaddr}
  955. { write reloc }
  956. MaybeSwapElfReloc(rel);
  957. relocsect.write(rel,sizeof(rel));
  958. end;
  959. end;
  960. end;
  961. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  962. var
  963. elfsym : telfsymbol;
  964. begin
  965. fillchar(elfsym,sizeof(elfsym),0);
  966. elfsym.st_name:=astridx;
  967. elfsym.st_info:=ainfo;
  968. elfsym.st_shndx:=ashndx;
  969. inc(symidx);
  970. inc(localsyms);
  971. MaybeSwapElfSymbol(elfsym);
  972. symtabsect.write(elfsym,sizeof(elfsym));
  973. end;
  974. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  975. begin
  976. TObjSection(p).secsymidx:=symidx;
  977. write_internal_symbol(0,STT_SECTION,TElfObjSection(p).secshidx);
  978. end;
  979. procedure TElfObjectOutput.createsymtab(data: TObjData);
  980. procedure WriteSym(objsym:TObjSymbol);
  981. var
  982. elfsym : telfsymbol;
  983. begin
  984. fillchar(elfsym,sizeof(elfsym),0);
  985. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  986. elfsym.st_name:=strtabsect.Size;
  987. strtabsect.writestr(objsym.name);
  988. strtabsect.writestr(#0);
  989. elfsym.st_size:=objsym.size;
  990. case objsym.bind of
  991. AB_LOCAL :
  992. begin
  993. elfsym.st_value:=objsym.address;
  994. elfsym.st_info:=STB_LOCAL shl 4;
  995. inc(localsyms);
  996. end;
  997. AB_COMMON :
  998. begin
  999. elfsym.st_value:=$10;
  1000. elfsym.st_info:=STB_GLOBAL shl 4;
  1001. end;
  1002. AB_EXTERNAL :
  1003. elfsym.st_info:=STB_GLOBAL shl 4;
  1004. AB_WEAK_EXTERNAL :
  1005. elfsym.st_info:=STB_WEAK shl 4;
  1006. AB_GLOBAL :
  1007. begin
  1008. elfsym.st_value:=objsym.address;
  1009. elfsym.st_info:=STB_GLOBAL shl 4;
  1010. end;
  1011. end;
  1012. if (objsym.bind<>AB_EXTERNAL) {and
  1013. not(assigned(objsym.objsection) and
  1014. not(oso_data in objsym.objsection.secoptions))} then
  1015. begin
  1016. case objsym.typ of
  1017. AT_FUNCTION :
  1018. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  1019. AT_DATA :
  1020. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  1021. end;
  1022. end;
  1023. if objsym.bind=AB_COMMON then
  1024. elfsym.st_shndx:=SHN_COMMON
  1025. else
  1026. begin
  1027. if assigned(objsym.objsection) then
  1028. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  1029. else
  1030. elfsym.st_shndx:=SHN_UNDEF;
  1031. end;
  1032. objsym.symidx:=symidx;
  1033. inc(symidx);
  1034. MaybeSwapElfSymbol(elfsym);
  1035. symtabsect.write(elfsym,sizeof(elfsym));
  1036. end;
  1037. var
  1038. i : longint;
  1039. objsym : TObjSymbol;
  1040. begin
  1041. with data do
  1042. begin
  1043. symidx:=0;
  1044. localsyms:=0;
  1045. { empty entry }
  1046. write_internal_symbol(0,0,0);
  1047. { filename entry }
  1048. write_internal_symbol(1,STT_FILE,SHN_ABS);
  1049. { section }
  1050. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  1051. { First the Local Symbols, this is required by ELF. The localsyms
  1052. count stored in shinfo is used to skip the local symbols
  1053. when traversing the symtab }
  1054. for i:=0 to ObjSymbolList.Count-1 do
  1055. begin
  1056. objsym:=TObjSymbol(ObjSymbolList[i]);
  1057. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  1058. WriteSym(objsym);
  1059. end;
  1060. { Global Symbols }
  1061. for i:=0 to ObjSymbolList.Count-1 do
  1062. begin
  1063. objsym:=TObjSymbol(ObjSymbolList[i]);
  1064. if (objsym.bind<>AB_LOCAL) then
  1065. WriteSym(objsym);
  1066. end;
  1067. { update the .symtab section header }
  1068. symtabsect.shlink:=strtabsect.secshidx;
  1069. symtabsect.shinfo:=localsyms;
  1070. end;
  1071. end;
  1072. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  1073. begin
  1074. TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
  1075. end;
  1076. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  1077. begin
  1078. with data do
  1079. begin
  1080. shstrtabsect.writestr(#0);
  1081. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  1082. end;
  1083. end;
  1084. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  1085. var
  1086. sechdr : telfsechdr;
  1087. begin
  1088. fillchar(sechdr,sizeof(sechdr),0);
  1089. sechdr.sh_name:=s.shstridx;
  1090. sechdr.sh_type:=s.shtype;
  1091. sechdr.sh_flags:=s.shflags;
  1092. sechdr.sh_offset:=s.datapos;
  1093. sechdr.sh_size:=s.Size;
  1094. sechdr.sh_link:=s.shlink;
  1095. sechdr.sh_info:=s.shinfo;
  1096. sechdr.sh_addralign:=s.secalign;
  1097. sechdr.sh_entsize:=s.shentsize;
  1098. MaybeSwapSecHeader(sechdr);
  1099. writer.write(sechdr,sizeof(sechdr));
  1100. end;
  1101. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  1102. begin
  1103. TElfObjSection(p).secshidx:=pword(arg)^;
  1104. inc(pword(arg)^);
  1105. end;
  1106. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  1107. begin
  1108. if (TElfObjSection(p).ObjRelocations.count>0) then
  1109. createrelocsection(TElfObjSection(p),TObjData(arg));
  1110. end;
  1111. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  1112. begin
  1113. writesectionheader(TElfObjSection(p));
  1114. end;
  1115. function TElfObjectOutput.writedata(data:TObjData):boolean;
  1116. var
  1117. header : telfheader;
  1118. shoffset,
  1119. datapos : aword;
  1120. nsections : word;
  1121. begin
  1122. result:=false;
  1123. with data do
  1124. begin
  1125. { default sections }
  1126. symtabsect:=TElfObjSection.create_ext(ObjSectionList,'.symtab',SHT_SYMTAB,0,0,0,4,sizeof(telfsymbol));
  1127. strtabsect:=TElfObjSection.create_ext(ObjSectionList,'.strtab',SHT_STRTAB,0,0,0,1,0);
  1128. shstrtabsect:=TElfObjSection.create_ext(ObjSectionList,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  1129. { "no executable stack" marker for Linux }
  1130. if (target_info.system in systems_linux) and
  1131. not(cs_executable_stack in current_settings.moduleswitches) then
  1132. TElfObjSection.create_ext(ObjSectionList,'.note.GNU-stack',SHT_PROGBITS,0,0,0,1,0);
  1133. { insert the empty and filename as first in strtab }
  1134. strtabsect.writestr(#0);
  1135. strtabsect.writestr(ExtractFileName(current_module.mainsource)+#0);
  1136. { calc amount of sections we have }
  1137. nsections:=1;
  1138. { also create the index in the section header table }
  1139. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1140. { create .symtab and .strtab }
  1141. createsymtab(data);
  1142. { Create the relocation sections, this needs valid secidx and symidx }
  1143. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  1144. { recalc nsections to incude the reloc sections }
  1145. nsections:=1;
  1146. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1147. { create .shstrtab }
  1148. createshstrtab(data);
  1149. { Calculate the filepositions }
  1150. datapos:=$40; { elfheader + alignment }
  1151. { section data }
  1152. layoutsections(datapos);
  1153. { section headers }
  1154. shoffset:=datapos;
  1155. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  1156. { Write ELF Header }
  1157. fillchar(header,sizeof(header),0);
  1158. header.magic[0]:=$7f; { = #127'ELF' }
  1159. header.magic[1]:=$45;
  1160. header.magic[2]:=$4c;
  1161. header.magic[3]:=$46;
  1162. header.file_class:=ELFCLASS;
  1163. if target_info.endian=endian_big then
  1164. header.data_encoding:=2
  1165. else
  1166. header.data_encoding:=1;
  1167. header.file_version:=1;
  1168. header.e_type:=ET_REL;
  1169. header.e_machine:=ELFMACHINE;
  1170. {$ifdef arm}
  1171. if (current_settings.fputype=cpu_soft) then
  1172. header.e_flags:=$600;
  1173. {$endif arm}
  1174. header.e_version:=1;
  1175. header.e_shoff:=shoffset;
  1176. header.e_shstrndx:=shstrtabsect.secshidx;
  1177. header.e_shnum:=nsections;
  1178. header.e_ehsize:=sizeof(telfheader);
  1179. header.e_shentsize:=sizeof(telfsechdr);
  1180. MaybeSwapHeader(header);
  1181. writer.write(header,sizeof(header));
  1182. writer.writezeros($40-sizeof(header)); { align }
  1183. { Sections }
  1184. WriteSectionContent(data);
  1185. { section headers, start with an empty header for sh_undef }
  1186. writer.writezeros(sizeof(telfsechdr));
  1187. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1188. end;
  1189. result:=true;
  1190. end;
  1191. {****************************************************************************
  1192. TELFAssembler
  1193. ****************************************************************************}
  1194. constructor TElfAssembler.Create(smart:boolean);
  1195. begin
  1196. inherited Create(smart);
  1197. CObjOutput:=TElfObjectOutput;
  1198. end;
  1199. {*****************************************************************************
  1200. Initialize
  1201. *****************************************************************************}
  1202. {$ifdef i386}
  1203. const
  1204. as_i386_elf32_info : tasminfo =
  1205. (
  1206. id : as_i386_elf32;
  1207. idtxt : 'ELF';
  1208. asmbin : '';
  1209. asmcmd : '';
  1210. supported_targets : [system_i386_linux,system_i386_beos,
  1211. system_i386_freebsd,system_i386_haiku,
  1212. system_i386_openbsd,system_i386_netbsd,
  1213. system_i386_Netware,system_i386_netwlibc,
  1214. system_i386_solaris,system_i386_embedded];
  1215. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1216. labelprefix : '.L';
  1217. comment : '';
  1218. dollarsign: '$';
  1219. );
  1220. {$endif i386}
  1221. {$ifdef x86_64}
  1222. const
  1223. as_x86_64_elf64_info : tasminfo =
  1224. (
  1225. id : as_x86_64_elf64;
  1226. idtxt : 'ELF';
  1227. asmbin : '';
  1228. asmcmd : '';
  1229. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  1230. system_x86_64_openbsd,system_x86_64_netbsd];
  1231. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1232. labelprefix : '.L';
  1233. comment : '';
  1234. dollarsign: '$';
  1235. );
  1236. {$endif x86_64}
  1237. {$ifdef sparc}
  1238. const
  1239. as_sparc_elf32_info : tasminfo =
  1240. (
  1241. id : as_sparc_elf32;
  1242. idtxt : 'ELF';
  1243. asmbin : '';
  1244. asmcmd : '';
  1245. supported_targets : [];
  1246. // flags : [af_outputbinary,af_smartlink_sections];
  1247. flags : [af_outputbinary,af_supports_dwarf];
  1248. labelprefix : '.L';
  1249. comment : '';
  1250. dollarsign: '$';
  1251. );
  1252. {$endif sparc}
  1253. initialization
  1254. {$ifdef i386}
  1255. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1256. {$endif i386}
  1257. {$ifdef sparc}
  1258. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1259. {$endif sparc}
  1260. {$ifdef x86_64}
  1261. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1262. {$endif x86_64}
  1263. end.