ogelf.pas 46 KB

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