ogelf.pas 47 KB

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