ogelf.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. destructor destroy;override;
  43. end;
  44. TElfObjData = class(TObjData)
  45. public
  46. constructor create(const n:string);override;
  47. destructor destroy;override;
  48. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  49. procedure CreateDebugSections;override;
  50. procedure writereloc(data,len:aint;p:TObjSymbol;reltype:TObjRelocationType);override;
  51. end;
  52. TElfObjectOutput = class(tObjOutput)
  53. private
  54. symtabsect,
  55. strtabsect,
  56. shstrtabsect,
  57. gotpcsect,
  58. gotoffsect,
  59. goTSect,
  60. plTSect,
  61. symsect : TElfObjSection;
  62. elf32data : TElfObjData;
  63. symidx,
  64. localsyms : longint;
  65. procedure createrelocsection(s:TElfObjSection);
  66. procedure createshstrtab;
  67. procedure createsymtab;
  68. procedure writesectionheader(s:TElfObjSection);
  69. procedure writesectiondata(s:TElfObjSection);
  70. procedure write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  71. procedure section_write_symbol(p:TObject;arg:pointer);
  72. procedure section_write_sh_string(p:TObject;arg:pointer);
  73. procedure section_count_sections(p:TObject;arg:pointer);
  74. procedure section_create_relocsec(p:TObject;arg:pointer);
  75. procedure section_set_datapos(p:TObject;arg:pointer);
  76. procedure section_write_data(p:TObject;arg:pointer);
  77. procedure section_write_sechdr(p:TObject;arg:pointer);
  78. protected
  79. function writedata(data:TObjData):boolean;override;
  80. public
  81. constructor Create(AWriter:TObjectWriter);override;
  82. destructor Destroy;override;
  83. end;
  84. TElfAssembler = class(tinternalassembler)
  85. constructor create(smart:boolean);override;
  86. end;
  87. implementation
  88. uses
  89. SysUtils,
  90. verbose,
  91. cutils,globals,fmodule;
  92. const
  93. symbolresize = 200*18;
  94. const
  95. { Relocation types }
  96. {$ifdef i386}
  97. R_386_32 = 1; { ordinary absolute relocation }
  98. R_386_PC32 = 2; { PC-relative relocation }
  99. R_386_GOT32 = 3; { an offset into GOT }
  100. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  101. R_386_GOTOFF = 9; { an offset from GOT base }
  102. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  103. R_386_GNU_VTINHERIT = 250;
  104. R_386_GNU_VTENTRY = 251;
  105. {$endif i386}
  106. {$ifdef sparc}
  107. R_SPARC_32 = 3;
  108. R_SPARC_WDISP30 = 7;
  109. R_SPARC_HI22 = 9;
  110. R_SPARC_LO10 = 12;
  111. R_SPARC_GNU_VTINHERIT = 250;
  112. R_SPARC_GNU_VTENTRY = 251;
  113. {$endif sparc}
  114. {$ifdef x86_64}
  115. R_X86_64_NONE = 0;
  116. { Direct 64 bit }
  117. R_X86_64_64 = 1;
  118. { PC relative 32 bit signed }
  119. R_X86_64_PC32 = 2;
  120. { 32 bit GOT entry }
  121. R_X86_64_GOT32 = 3;
  122. { 32 bit PLT address }
  123. R_X86_64_PLT32 = 4;
  124. { Copy symbol at runtime }
  125. R_X86_64_COPY = 5;
  126. { Create GOT entry }
  127. R_X86_64_GLOB_DAT = 6;
  128. { Create PLT entry }
  129. R_X86_64_JUMP_SLOT = 7;
  130. { Adjust by program base }
  131. R_X86_64_RELATIVE = 8;
  132. { 32 bit signed PC relative offset to GOT }
  133. R_X86_64_GOTPCREL = 9;
  134. { Direct 32 bit zero extended }
  135. R_X86_64_32 = 10;
  136. { Direct 32 bit sign extended }
  137. R_X86_64_32S = 11;
  138. { Direct 16 bit zero extended }
  139. R_X86_64_16 = 12;
  140. { 16 bit sign extended PC relative }
  141. R_X86_64_PC16 = 13;
  142. { Direct 8 bit sign extended }
  143. R_X86_64_8 = 14;
  144. { 8 bit sign extended PC relative }
  145. R_X86_64_PC8 = 15;
  146. { ID of module containing symbol }
  147. R_X86_64_DTPMOD64 = 16;
  148. { Offset in module's TLS block }
  149. R_X86_64_DTPOFF64 = 17;
  150. { Offset in initial TLS block }
  151. R_X86_64_TPOFF64 = 18;
  152. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  153. R_X86_64_TLSGD = 19;
  154. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  155. R_X86_64_TLSLD = 20;
  156. { Offset in TLS block }
  157. R_X86_64_DTPOFF32 = 21;
  158. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  159. R_X86_64_GOTTPOFF = 22;
  160. { Offset in initial TLS block }
  161. R_X86_64_TPOFF32 = 23;
  162. { GNU extension to record C++ vtable hierarchy }
  163. R_X86_64_GNU_VTINHERIT = 24;
  164. { GNU extension to record C++ vtable member usage }
  165. R_X86_64_GNU_VTENTRY = 25;
  166. {$endif x86_64}
  167. SHN_UNDEF = 0;
  168. SHN_ABS = $fff1;
  169. SHN_COMMON = $fff2;
  170. SHT_NULL = 0;
  171. SHT_PROGBITS = 1;
  172. SHT_SYMTAB = 2;
  173. SHT_STRTAB = 3;
  174. SHT_RELA = 4;
  175. SHT_HASH = 5;
  176. SHT_DYNAMIC = 6;
  177. SHT_NOTE = 7;
  178. SHT_NOBITS = 8;
  179. SHT_REL = 9;
  180. SHT_SHLIB = 10;
  181. SHT_DYNSYM = 11;
  182. SHF_WRITE = 1;
  183. SHF_ALLOC = 2;
  184. SHF_EXECINSTR = 4;
  185. STB_LOCAL = 0;
  186. STB_GLOBAL = 1;
  187. STB_WEAK = 2;
  188. STT_NOTYPE = 0;
  189. STT_OBJECT = 1;
  190. STT_FUNC = 2;
  191. STT_SECTION = 3;
  192. STT_FILE = 4;
  193. type
  194. { Structures which are written directly to the output file }
  195. TElf32header=packed record
  196. magic0123 : longint;
  197. file_class : byte;
  198. data_encoding : byte;
  199. file_version : byte;
  200. padding : array[$07..$0f] of byte;
  201. e_type : word;
  202. e_machine : word;
  203. e_version : longint;
  204. e_entry : longint; { entrypoint }
  205. e_phoff : longint; { program header offset }
  206. e_shoff : longint; { sections header offset }
  207. e_flags : longint;
  208. e_ehsize : word; { elf header size in bytes }
  209. e_phentsize : word; { size of an entry in the program header array }
  210. e_phnum : word; { 0..e_phnum-1 of entrys }
  211. e_shentsize : word; { size of an entry in sections header array }
  212. e_shnum : word; { 0..e_shnum-1 of entrys }
  213. e_shstrndx : word; { index of string section header }
  214. end;
  215. TElf32sechdr=packed record
  216. sh_name : longint;
  217. sh_type : longint;
  218. sh_flags : longint;
  219. sh_addr : longint;
  220. sh_offset : longint;
  221. sh_size : longint;
  222. sh_link : longint;
  223. sh_info : longint;
  224. sh_addralign : longint;
  225. sh_entsize : longint;
  226. end;
  227. TElf32reloc=packed record
  228. address : longint;
  229. info : longint; { bit 0-7: type, 8-31: symbol }
  230. end;
  231. TElf32symbol=packed record
  232. st_name : longint;
  233. st_value : longint;
  234. st_size : longint;
  235. st_info : byte; { bit 0-3: type, 4-7: bind }
  236. st_other : byte;
  237. st_shndx : word;
  238. end;
  239. telf64header=packed record
  240. magic0123 : longint;
  241. file_class : byte;
  242. data_encoding : byte;
  243. file_version : byte;
  244. padding : array[$07..$0f] of byte;
  245. e_type : word;
  246. e_machine : word;
  247. e_version : longint;
  248. e_entry : qword; { entrypoint }
  249. e_phoff : qword; { program header offset }
  250. e_shoff : qword; { sections header offset }
  251. e_flags : longint;
  252. e_ehsize : word; { elf header size in bytes }
  253. e_phentsize : word; { size of an entry in the program header array }
  254. e_phnum : word; { 0..e_phnum-1 of entrys }
  255. e_shentsize : word; { size of an entry in sections header array }
  256. e_shnum : word; { 0..e_shnum-1 of entrys }
  257. e_shstrndx : word; { index of string section header }
  258. end;
  259. telf64sechdr=packed record
  260. sh_name : longint;
  261. sh_type : longint;
  262. sh_flags : qword;
  263. sh_addr : qword;
  264. sh_offset : qword;
  265. sh_size : qword;
  266. sh_link : longint;
  267. sh_info : longint;
  268. sh_addralign : qword;
  269. sh_entsize : qword;
  270. end;
  271. telf64reloc=packed record
  272. address : qword;
  273. info : qword; { bit 0-7: type, 8-31: symbol }
  274. addend : qword;
  275. end;
  276. telf64symbol=packed record
  277. st_name : longint;
  278. st_info : byte; { bit 0-3: type, 4-7: bind }
  279. st_other : byte;
  280. st_shndx : word;
  281. st_value : qword;
  282. st_size : qword;
  283. end;
  284. {$ifdef cpu64bit}
  285. telfheader = telf64header;
  286. telfreloc = telf64reloc;
  287. telfsymbol = telf64symbol;
  288. telfsechdr = telf64sechdr;
  289. {$else cpu64bit}
  290. telfheader = telf32header;
  291. telfreloc = telf32reloc;
  292. telfsymbol = telf32symbol;
  293. telfsechdr = telf32sechdr;
  294. {$endif cpu64bit}
  295. function MayBeSwapHeader(h : telf32header) : telf32header;
  296. begin
  297. result:=h;
  298. if source_info.endian<>target_info.endian then
  299. with h do
  300. begin
  301. result.e_type:=swapword(e_type);
  302. result.e_machine:=swapword(e_machine);
  303. result.e_version:=swaplong(e_version);
  304. result.e_entry:=swaplong(e_entry);
  305. result.e_phoff:=swaplong(e_phoff);
  306. result.e_shoff:=swaplong(e_shoff);
  307. result.e_flags:=swaplong(e_flags);
  308. result.e_ehsize:=swapword(e_ehsize);
  309. result.e_phentsize:=swapword(e_phentsize);
  310. result.e_phnum:=swapword(e_phnum);
  311. result.e_shentsize:=swapword(e_shentsize);
  312. result.e_shnum:=swapword(e_shnum);
  313. result.e_shstrndx:=swapword(e_shstrndx);
  314. end;
  315. end;
  316. function MayBeSwapHeader(h : telf64header) : telf64header;
  317. begin
  318. result:=h;
  319. if source_info.endian<>target_info.endian then
  320. with h do
  321. begin
  322. result.e_type:=swapword(e_type);
  323. result.e_machine:=swapword(e_machine);
  324. result.e_version:=swaplong(e_version);
  325. result.e_entry:=swapqword(e_entry);
  326. result.e_phoff:=swapqword(e_phoff);
  327. result.e_shoff:=swapqword(e_shoff);
  328. result.e_flags:=swaplong(e_flags);
  329. result.e_ehsize:=swapword(e_ehsize);
  330. result.e_phentsize:=swapword(e_phentsize);
  331. result.e_phnum:=swapword(e_phnum);
  332. result.e_shentsize:=swapword(e_shentsize);
  333. result.e_shnum:=swapword(e_shnum);
  334. result.e_shstrndx:=swapword(e_shstrndx);
  335. end;
  336. end;
  337. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  338. begin
  339. result:=h;
  340. if source_info.endian<>target_info.endian then
  341. with h do
  342. begin
  343. result.sh_name:=SwapLong(sh_name);
  344. result.sh_type:=SwapLong(sh_type);
  345. result.sh_flags:=SwapLong(sh_flags);
  346. result.sh_addr:=SwapLong(sh_addr);
  347. result.sh_offset:=SwapLong(sh_offset);
  348. result.sh_size:=SwapLong(sh_size);
  349. result.sh_link:=SwapLong(sh_link);
  350. result.sh_info:=SwapLong(sh_info);
  351. result.sh_addralign:=SwapLong(sh_addralign);
  352. result.sh_entsize:=SwapLong(sh_entsize);
  353. end;
  354. end;
  355. function MaybeSwapSecHeader(h : telf64sechdr) : telf64sechdr;
  356. begin
  357. result:=h;
  358. if source_info.endian<>target_info.endian then
  359. with h do
  360. begin
  361. result.sh_name:=SwapLong(sh_name);
  362. result.sh_type:=SwapLong(sh_type);
  363. result.sh_flags:=SwapQWord(sh_flags);
  364. result.sh_addr:=SwapQWord(sh_addr);
  365. result.sh_offset:=SwapQWord(sh_offset);
  366. result.sh_size:=SwapQWord(sh_size);
  367. result.sh_link:=SwapLong(sh_link);
  368. result.sh_info:=SwapLong(sh_info);
  369. result.sh_addralign:=SwapQWord(sh_addralign);
  370. result.sh_entsize:=SwapQWord(sh_entsize);
  371. end;
  372. end;
  373. function MaybeSwapElfSymbol(h : telf32symbol) : telf32symbol;
  374. begin
  375. result:=h;
  376. if source_info.endian<>target_info.endian then
  377. with h do
  378. begin
  379. result.st_name:=SwapLong(st_name);
  380. result.st_value:=SwapLong(st_value);
  381. result.st_size:=SwapLong(st_size);
  382. result.st_shndx:=SwapWord(st_shndx);
  383. end;
  384. end;
  385. function MaybeSwapElfSymbol(h : telf64symbol) : telf64symbol;
  386. begin
  387. result:=h;
  388. if source_info.endian<>target_info.endian then
  389. with h do
  390. begin
  391. result.st_name:=SwapLong(st_name);
  392. result.st_value:=SwapQWord(st_value);
  393. result.st_size:=SwapQWord(st_size);
  394. result.st_shndx:=SwapWord(st_shndx);
  395. end;
  396. end;
  397. function MaybeSwapElfReloc(h : telf32reloc) : telf32reloc;
  398. begin
  399. result:=h;
  400. if source_info.endian<>target_info.endian then
  401. with h do
  402. begin
  403. result.address:=SwapLong(address);
  404. result.info:=SwapLong(info);
  405. end;
  406. end;
  407. function MaybeSwapElfReloc(h : telf64reloc) : telf64reloc;
  408. begin
  409. result:=h;
  410. if source_info.endian<>target_info.endian then
  411. with h do
  412. begin
  413. result.address:=SwapQWord(address);
  414. result.info:=SwapQWord(info);
  415. end;
  416. end;
  417. {****************************************************************************
  418. Helpers
  419. ****************************************************************************}
  420. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  421. begin
  422. { Section Type }
  423. AshType:=SHT_PROGBITS;
  424. if oso_strings in aoptions then
  425. AshType:=SHT_STRTAB
  426. else if not(oso_data in aoptions) then
  427. AshType:=SHT_NOBITS;
  428. { Section Flags }
  429. Ashflags:=0;
  430. if oso_load in aoptions then
  431. Ashflags:=Ashflags or SHF_ALLOC;
  432. if oso_executable in aoptions then
  433. Ashflags:=Ashflags or SHF_EXECINSTR;
  434. if oso_write in aoptions then
  435. Ashflags:=Ashflags or SHF_WRITE;
  436. end;
  437. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  438. begin
  439. aoptions:=[];
  440. { Section Type }
  441. if AshType<>SHT_NOBITS then
  442. include(aoptions,oso_data);
  443. if AshType=SHT_STRTAB then
  444. include(aoptions,oso_strings);
  445. { Section Flags }
  446. if Ashflags and SHF_ALLOC<>0 then
  447. include(aoptions,oso_load)
  448. else
  449. include(aoptions,oso_noload);
  450. if Ashflags and SHF_WRITE<>0 then
  451. include(aoptions,oso_write)
  452. else
  453. include(aoptions,oso_readonly);
  454. end;
  455. {****************************************************************************
  456. TSection
  457. ****************************************************************************}
  458. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  459. begin
  460. inherited create(AList,Aname,Aalign,aoptions);
  461. secshidx:=0;
  462. shstridx:=0;
  463. encodesechdrflags(aoptions,shtype,shflags);
  464. shlink:=0;
  465. shinfo:=0;
  466. if name='.stab' then
  467. shentsize:=sizeof(TObjStabEntry);
  468. end;
  469. constructor TElfObjSection.create_ext(AList:TFPHashObjectList;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  470. var
  471. aoptions : TObjSectionOptions;
  472. begin
  473. decodesechdrflags(Ashtype,Ashflags,aoptions);
  474. inherited create(AList,Aname,Aalign,aoptions);
  475. secshidx:=0;
  476. shstridx:=0;
  477. shtype:=AshType;
  478. shflags:=AshFlags;
  479. shlink:=Ashlink;
  480. shinfo:=Ashinfo;
  481. shentsize:=Aentsize;
  482. end;
  483. destructor TElfObjSection.destroy;
  484. begin
  485. inherited destroy;
  486. end;
  487. {****************************************************************************
  488. TElfObjData
  489. ****************************************************************************}
  490. constructor TElfObjData.create(const n:string);
  491. begin
  492. inherited create(n);
  493. CObjSection:=TElfObjSection;
  494. { we need at least the following sections }
  495. createsection(sec_code);
  496. createsection(sec_data);
  497. createsection(sec_bss);
  498. if tf_section_threadvars in target_info.flags then
  499. createsection(sec_threadvar);
  500. if (tf_needs_dwarf_cfi in target_info.flags) and
  501. (af_supports_dwarf in target_asm.flags) then
  502. createsection(sec_debug_frame);
  503. end;
  504. destructor TElfObjData.destroy;
  505. begin
  506. inherited destroy;
  507. end;
  508. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  509. const
  510. secnames : array[TAsmSectiontype] of string[13] = ('',
  511. {$ifdef userodata}
  512. '.text','.data','.rodata','.bss','.threadvar',
  513. {$else userodata}
  514. '.text','.data','.data','.bss','.threadvar',
  515. {$endif userodata}
  516. '.pdata',
  517. '.text', { darwin stubs }
  518. '.stab','.stabstr',
  519. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  520. '.eh_frame',
  521. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  522. 'fpc',
  523. ''
  524. );
  525. secnames_pic : array[TAsmSectiontype] of string[17] = ('',
  526. '.text',
  527. '.data.rel',
  528. '.data.rel',
  529. '.bss',
  530. '.threadvar',
  531. '.pdata',
  532. '', { stubs }
  533. '.stab',
  534. '.stabstr',
  535. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  536. '.eh_frame',
  537. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  538. '.fpc',
  539. '.toc'
  540. );
  541. var
  542. sep : string[3];
  543. secname : string;
  544. begin
  545. if (cs_create_pic in current_settings.moduleswitches) and
  546. not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  547. secname:=secnames_pic[atype]
  548. else
  549. secname:=secnames[atype];
  550. if (use_smartlink_section and
  551. (aname<>'')) or (atype=sec_fpc) then
  552. begin
  553. case aorder of
  554. secorder_begin :
  555. sep:='.b_';
  556. secorder_end :
  557. sep:='.z_';
  558. else
  559. sep:='.n_';
  560. end;
  561. result:=secname+sep+aname
  562. end
  563. else
  564. result:=secname;
  565. end;
  566. procedure TElfObjData.CreateDebugSections;
  567. begin
  568. if target_dbg.id=dbg_stabs then
  569. begin
  570. stabssec:=createsection(sec_stab);
  571. stabstrsec:=createsection(sec_stabstr);
  572. end;
  573. end;
  574. procedure TElfObjData.writereloc(data,len:aint;p:TObjSymbol;reltype:TObjRelocationType);
  575. var
  576. symaddr : longint;
  577. begin
  578. if CurrObjSec=nil then
  579. internalerror(200403292);
  580. {$ifdef userodata}
  581. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  582. internalerror(200408252);
  583. {$endif userodata}
  584. if assigned(p) then
  585. begin
  586. { real address of the symbol }
  587. symaddr:=p.address;
  588. { Local ObjSymbols can be resolved already or need a section reloc }
  589. if (p.bind=AB_LOCAL) and
  590. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32{$endif x86_64}]) then
  591. begin
  592. { For a reltype relocation in the same section the
  593. value can be calculated }
  594. if (p.objsection=CurrObjSec) and
  595. (reltype=RELOC_RELATIVE) then
  596. inc(data,symaddr-len-CurrObjSec.Size)
  597. else
  598. begin
  599. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,reltype);
  600. inc(data,symaddr);
  601. end;
  602. end
  603. else
  604. begin
  605. CurrObjSec.addsymreloc(CurrObjSec.Size,p,reltype);
  606. {$ifndef x86_64}
  607. if reltype=RELOC_RELATIVE then
  608. dec(data,len);
  609. {$endif x86_64}
  610. end;
  611. end;
  612. CurrObjSec.write(data,len);
  613. end;
  614. {****************************************************************************
  615. TElfObjectOutput
  616. ****************************************************************************}
  617. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  618. begin
  619. inherited Create(AWriter);
  620. CObjData:=TElfObjData;
  621. end;
  622. destructor TElfObjectOutput.destroy;
  623. begin
  624. inherited destroy;
  625. end;
  626. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection);
  627. var
  628. i : longint;
  629. rel : telfreloc;
  630. objreloc : TObjRelocation;
  631. relsym,
  632. reltyp : longint;
  633. relocsect : TObjSection;
  634. begin
  635. with elf32data do
  636. begin
  637. {$ifdef userodata}
  638. { rodata can't have relocations }
  639. if s.sectype=sec_rodata then
  640. begin
  641. if assigned(s.relocations.first) then
  642. internalerror(200408251);
  643. exit;
  644. end;
  645. {$endif userodata}
  646. { create the reloc section }
  647. {$ifdef i386}
  648. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  649. {$else i386}
  650. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  651. {$endif i386}
  652. { add the relocations }
  653. for i:=0 to s.Objrelocations.count-1 do
  654. begin
  655. objreloc:=TObjRelocation(s.Objrelocations[i]);
  656. fillchar(rel,sizeof(rel),0);
  657. rel.address:=objreloc.dataoffset;
  658. { when things settle down, we can create processor specific
  659. derived classes }
  660. case objreloc.typ of
  661. {$ifdef i386}
  662. RELOC_RELATIVE :
  663. reltyp:=R_386_PC32;
  664. RELOC_ABSOLUTE :
  665. reltyp:=R_386_32;
  666. {$endif i386}
  667. {$ifdef sparc}
  668. RELOC_ABSOLUTE :
  669. reltyp:=R_SPARC_32;
  670. {$endif sparc}
  671. {$ifdef x86_64}
  672. RELOC_RELATIVE :
  673. begin
  674. reltyp:=R_X86_64_PC32;
  675. { length of the relocated location is handled here }
  676. rel.addend:=qword(-4);
  677. end;
  678. RELOC_ABSOLUTE :
  679. reltyp:=R_X86_64_64;
  680. RELOC_ABSOLUTE32 :
  681. reltyp:=R_X86_64_32S;
  682. RELOC_GOTPCREL :
  683. begin
  684. reltyp:=R_X86_64_GOTPCREL;
  685. { length of the relocated location is handled here }
  686. rel.addend:=qword(-4);
  687. end;
  688. RELOC_PLT32 :
  689. begin
  690. reltyp:=R_X86_64_PLT32;
  691. { length of the relocated location is handled here }
  692. rel.addend:=qword(-4);
  693. end;
  694. {$endif x86_64}
  695. else
  696. internalerror(200602261);
  697. end;
  698. { Symbol }
  699. if assigned(objreloc.symbol) then
  700. begin
  701. if objreloc.symbol.symidx=-1 then
  702. begin
  703. writeln(objreloc.symbol.Name);
  704. internalerror(200603012);
  705. end;
  706. relsym:=objreloc.symbol.symidx;
  707. end
  708. else
  709. begin
  710. if objreloc.objsection<>nil then
  711. relsym:=objreloc.objsection.secsymidx
  712. else
  713. relsym:=SHN_UNDEF;
  714. end;
  715. {$ifdef cpu64bit}
  716. rel.info:=(qword(relsym) shl 32) or reltyp;
  717. {$else cpu64bit}
  718. rel.info:=(relsym shl 8) or reltyp;
  719. {$endif cpu64bit}
  720. { write reloc }
  721. relocsect.write(MaybeSwapElfReloc(rel),sizeof(rel));
  722. end;
  723. end;
  724. end;
  725. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  726. var
  727. elfsym : telfsymbol;
  728. begin
  729. fillchar(elfsym,sizeof(elfsym),0);
  730. elfsym.st_name:=astridx;
  731. elfsym.st_info:=ainfo;
  732. elfsym.st_shndx:=ashndx;
  733. inc(symidx);
  734. inc(localsyms);
  735. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  736. end;
  737. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  738. begin
  739. TObjSection(p).secsymidx:=symidx;
  740. write_internal_symbol(TElfObjSection(p).shstridx,STT_SECTION,TElfObjSection(p).secshidx);
  741. end;
  742. procedure TElfObjectOutput.createsymtab;
  743. procedure WriteSym(objsym:TObjSymbol);
  744. var
  745. elfsym : telfsymbol;
  746. begin
  747. with elf32data do
  748. begin
  749. fillchar(elfsym,sizeof(elfsym),0);
  750. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  751. elfsym.st_name:=strtabsect.Size;
  752. strtabsect.writestr(objsym.name);
  753. strtabsect.writestr(#0);
  754. elfsym.st_size:=objsym.size;
  755. case objsym.bind of
  756. AB_LOCAL :
  757. begin
  758. elfsym.st_value:=objsym.address;
  759. elfsym.st_info:=STB_LOCAL shl 4;
  760. inc(localsyms);
  761. end;
  762. AB_COMMON :
  763. begin
  764. elfsym.st_value:=$10;
  765. elfsym.st_info:=STB_GLOBAL shl 4;
  766. end;
  767. AB_EXTERNAL :
  768. elfsym.st_info:=STB_GLOBAL shl 4;
  769. AB_GLOBAL :
  770. begin
  771. elfsym.st_value:=objsym.address;
  772. elfsym.st_info:=STB_GLOBAL shl 4;
  773. end;
  774. end;
  775. if (objsym.bind<>AB_EXTERNAL) {and
  776. not(assigned(objsym.objsection) and
  777. not(oso_data in objsym.objsection.secoptions))} then
  778. begin
  779. case objsym.typ of
  780. AT_FUNCTION :
  781. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  782. AT_DATA :
  783. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  784. end;
  785. end;
  786. if objsym.bind=AB_COMMON then
  787. elfsym.st_shndx:=SHN_COMMON
  788. else
  789. begin
  790. if assigned(objsym.objsection) then
  791. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  792. else
  793. elfsym.st_shndx:=SHN_UNDEF;
  794. end;
  795. objsym.symidx:=symidx;
  796. inc(symidx);
  797. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  798. end;
  799. end;
  800. var
  801. i : longint;
  802. objsym : TObjSymbol;
  803. begin
  804. with elf32data do
  805. begin
  806. symidx:=0;
  807. localsyms:=0;
  808. { empty entry }
  809. write_internal_symbol(0,0,0);
  810. { filename entry }
  811. write_internal_symbol(1,STT_FILE,SHN_ABS);
  812. { section }
  813. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  814. { First the Local Symbols, this is required by ELF. The localsyms
  815. count stored in shinfo is used to skip the local symbols
  816. when traversing the symtab }
  817. for i:=0 to ObjSymbolList.Count-1 do
  818. begin
  819. objsym:=TObjSymbol(ObjSymbolList[i]);
  820. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  821. WriteSym(objsym);
  822. end;
  823. { Global Symbols }
  824. for i:=0 to ObjSymbolList.Count-1 do
  825. begin
  826. objsym:=TObjSymbol(ObjSymbolList[i]);
  827. if (objsym.bind<>AB_LOCAL) then
  828. WriteSym(objsym);
  829. end;
  830. { update the .symtab section header }
  831. symtabsect.shlink:=strtabsect.secshidx;
  832. symtabsect.shinfo:=localsyms;
  833. end;
  834. end;
  835. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  836. begin
  837. TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
  838. end;
  839. procedure TElfObjectOutput.createshstrtab;
  840. begin
  841. with elf32data do
  842. begin
  843. shstrtabsect.writestr(#0);
  844. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  845. end;
  846. end;
  847. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  848. var
  849. sechdr : telfsechdr;
  850. begin
  851. fillchar(sechdr,sizeof(sechdr),0);
  852. sechdr.sh_name:=s.shstridx;
  853. sechdr.sh_type:=s.shtype;
  854. sechdr.sh_flags:=s.shflags;
  855. sechdr.sh_offset:=s.datapos;
  856. sechdr.sh_size:=s.Size;
  857. sechdr.sh_link:=s.shlink;
  858. sechdr.sh_info:=s.shinfo;
  859. sechdr.sh_addralign:=s.secalign;
  860. sechdr.sh_entsize:=s.shentsize;
  861. writer.write(MaybeSwapSecHeader(sechdr),sizeof(sechdr));
  862. end;
  863. procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
  864. begin
  865. FWriter.writezeros(s.dataalignbytes);
  866. if s.Datapos<>FWriter.ObjSize then
  867. internalerror(200604031);
  868. FWriter.writearray(s.data);
  869. end;
  870. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  871. begin
  872. TElfObjSection(p).secshidx:=pword(arg)^;
  873. inc(pword(arg)^);
  874. end;
  875. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  876. begin
  877. if (TElfObjSection(p).ObjRelocations.count>0) then
  878. createrelocsection(TElfObjSection(p));
  879. end;
  880. procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
  881. begin
  882. TObjSection(p).setdatapos(paint(arg)^);
  883. end;
  884. procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
  885. begin
  886. if (oso_data in TObjSection(p).secoptions) then
  887. begin
  888. if TObjSection(p).data=nil then
  889. internalerror(200403073);
  890. writesectiondata(TElfObjSection(p));
  891. end;
  892. end;
  893. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  894. begin
  895. writesectionheader(TElfObjSection(p));
  896. end;
  897. function TElfObjectOutput.writedata(data:TObjData):boolean;
  898. var
  899. header : telfheader;
  900. shoffset,
  901. datapos : aint;
  902. nsections : word;
  903. begin
  904. result:=false;
  905. elf32data:=TElfObjData(data);
  906. with elf32data do
  907. begin
  908. { default sections }
  909. symtabsect:=TElfObjSection.create_ext(ObjSectionList,'.symtab',SHT_SYMTAB,0,0,0,4,sizeof(telfsymbol));
  910. strtabsect:=TElfObjSection.create_ext(ObjSectionList,'.strtab',SHT_STRTAB,0,0,0,1,0);
  911. shstrtabsect:=TElfObjSection.create_ext(ObjSectionList,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  912. { insert the empty and filename as first in strtab }
  913. strtabsect.writestr(#0);
  914. strtabsect.writestr(ExtractFileName(current_module.mainsource^)+#0);
  915. { calc amount of sections we have }
  916. nsections:=1;
  917. { also create the index in the section header table }
  918. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  919. { create .symtab and .strtab }
  920. createsymtab;
  921. { Create the relocation sections, this needs valid secidx and symidx }
  922. ObjSectionList.ForEachCall(@section_create_relocsec,nil);
  923. { recalc nsections to incude the reloc sections }
  924. nsections:=1;
  925. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  926. { create .shstrtab }
  927. createshstrtab;
  928. { Calculate the filepositions }
  929. datapos:=$40; { elfheader + alignment }
  930. { section data }
  931. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  932. { section headers }
  933. shoffset:=datapos;
  934. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  935. { Write ELF Header }
  936. fillchar(header,sizeof(header),0);
  937. header.magic0123:=$464c457f; { = #127'ELF' }
  938. {$ifdef cpu64bit}
  939. header.file_class:=2;
  940. {$else cpu64bit}
  941. header.file_class:=1;
  942. {$endif cpu64bit}
  943. if target_info.endian=endian_big then
  944. header.data_encoding:=2
  945. else
  946. header.data_encoding:=1;
  947. header.file_version:=1;
  948. header.e_type:=1;
  949. {$ifdef sparc}
  950. header.e_machine:=2;
  951. {$endif sparc}
  952. {$ifdef i386}
  953. header.e_machine:=3;
  954. {$endif i386}
  955. {$ifdef m68k}
  956. header.e_machine:=4;
  957. {$endif m68k}
  958. {$ifdef powerpc}
  959. header.e_machine:=20;
  960. {$endif powerpc}
  961. {$ifdef arm}
  962. header.e_machine:=40;
  963. {$endif arm}
  964. {$ifdef x86_64}
  965. header.e_machine:=62;
  966. {$endif x86_64}
  967. header.e_version:=1;
  968. header.e_shoff:=shoffset;
  969. header.e_shstrndx:=shstrtabsect.secshidx;
  970. header.e_shnum:=nsections;
  971. header.e_ehsize:=sizeof(telfheader);
  972. header.e_shentsize:=sizeof(telfsechdr);
  973. writer.write(MaybeSwapHeader(header),sizeof(header));
  974. writer.writezeros($40-sizeof(header)); { align }
  975. { Sections }
  976. ObjSectionList.ForEachCall(@section_write_data,nil);
  977. { section headers, start with an empty header for sh_undef }
  978. writer.writezeros(sizeof(telfsechdr));
  979. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  980. end;
  981. result:=true;
  982. end;
  983. {****************************************************************************
  984. TELFAssembler
  985. ****************************************************************************}
  986. constructor TElfAssembler.Create(smart:boolean);
  987. begin
  988. inherited Create(smart);
  989. CObjOutput:=TElfObjectOutput;
  990. end;
  991. {*****************************************************************************
  992. Initialize
  993. *****************************************************************************}
  994. {$ifdef i386}
  995. const
  996. as_i386_elf32_info : tasminfo =
  997. (
  998. id : as_i386_elf32;
  999. idtxt : 'ELF';
  1000. asmbin : '';
  1001. asmcmd : '';
  1002. supported_target : system_any; //target_i386_linux;
  1003. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1004. labelprefix : '.L';
  1005. comment : '';
  1006. );
  1007. {$endif i386}
  1008. {$ifdef x86_64}
  1009. const
  1010. as_x86_64_elf64_info : tasminfo =
  1011. (
  1012. id : as_x86_64_elf64;
  1013. idtxt : 'ELF';
  1014. asmbin : '';
  1015. asmcmd : '';
  1016. supported_target : system_any; //target_i386_linux;
  1017. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1018. labelprefix : '.L';
  1019. comment : '';
  1020. );
  1021. {$endif x86_64}
  1022. {$ifdef sparc}
  1023. const
  1024. as_sparc_elf32_info : tasminfo =
  1025. (
  1026. id : as_sparc_elf32;
  1027. idtxt : 'ELF';
  1028. asmbin : '';
  1029. asmcmd : '';
  1030. supported_target : system_any; //target_i386_linux;
  1031. // flags : [af_outputbinary,af_smartlink_sections];
  1032. flags : [af_outputbinary,af_supports_dwarf];
  1033. labelprefix : '.L';
  1034. comment : '';
  1035. );
  1036. {$endif sparc}
  1037. initialization
  1038. {$ifdef i386}
  1039. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1040. {$endif i386}
  1041. {$ifdef sparc}
  1042. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1043. {$endif sparc}
  1044. {$ifdef x86_64}
  1045. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1046. {$endif x86_64}
  1047. end.