ogelf.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. { relocation }
  41. relocsect : TElfObjSection;
  42. constructor create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  43. constructor create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  44. destructor destroy;override;
  45. end;
  46. TElfObjData = class(TObjData)
  47. public
  48. symtabsect,
  49. strtabsect,
  50. shstrtabsect,
  51. gotpcsect,
  52. gotoffsect,
  53. goTSect,
  54. plTSect,
  55. symsect : TElfObjSection;
  56. syms : Tdynamicarray;
  57. constructor create(const n:string);override;
  58. destructor destroy;override;
  59. function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
  60. function sectiontype2align(atype:TAsmSectiontype):shortint;override;
  61. procedure CreateDebugSections;override;
  62. procedure writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);override;
  63. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
  64. end;
  65. TElfObjectOutput = class(tObjOutput)
  66. private
  67. elf32data : TElfObjData;
  68. symidx,
  69. localsyms : longint;
  70. procedure createrelocsection(s:TElfObjSection);
  71. procedure createshstrtab;
  72. procedure createsymtab;
  73. procedure writesectionheader(s:TElfObjSection);
  74. procedure writesectiondata(s:TElfObjSection);
  75. procedure write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  76. procedure section_write_symbol(p:TObject;arg:pointer);
  77. procedure section_write_sh_string(p:TObject;arg:pointer);
  78. procedure section_count_sections(p:TObject;arg:pointer);
  79. procedure section_create_relocsec(p:TObject;arg:pointer);
  80. procedure section_set_datapos(p:TObject;arg:pointer);
  81. procedure section_relocsec_set_datapos(p:TObject;arg:pointer);
  82. procedure section_write_data(p:TObject;arg:pointer);
  83. procedure section_write_sechdr(p:TObject;arg:pointer);
  84. procedure section_write_relocsec(p:TObject;arg:pointer);
  85. protected
  86. function writedata(data:TObjData):boolean;override;
  87. public
  88. constructor Create(AWriter:TObjectWriter);override;
  89. end;
  90. TElfAssembler = class(tinternalassembler)
  91. constructor create(smart:boolean);override;
  92. end;
  93. implementation
  94. uses
  95. strings,
  96. verbose,
  97. cutils,globals,fmodule;
  98. const
  99. symbolresize = 200*18;
  100. const
  101. R_386_32 = 1; { ordinary absolute relocation }
  102. R_386_PC32 = 2; { PC-relative relocation }
  103. R_386_GOT32 = 3; { an offset into GOT }
  104. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  105. R_386_GOTOFF = 9; { an offset from GOT base }
  106. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  107. R_SPARC_32 = 3;
  108. R_SPARC_WDISP30 = 7;
  109. R_SPARC_HI22 = 9;
  110. R_SPARC_LO10 = 12;
  111. { AMD64 relocations }
  112. R_X86_64_NONE = 0;
  113. { Direct 64 bit }
  114. R_X86_64_64 = 1;
  115. { PC relative 32 bit signed }
  116. R_X86_64_PC32 = 2;
  117. { 32 bit GOT entry }
  118. R_X86_64_GOT32 = 3;
  119. { 32 bit PLT address }
  120. R_X86_64_PLT32 = 4;
  121. { Copy symbol at runtime }
  122. R_X86_64_COPY = 5;
  123. { Create GOT entry }
  124. R_X86_64_GLOB_DAT = 6;
  125. { Create PLT entry }
  126. R_X86_64_JUMP_SLOT = 7;
  127. { Adjust by program base }
  128. R_X86_64_RELATIVE = 8;
  129. { 32 bit signed PC relative offset to GOT }
  130. R_X86_64_GOTPCREL = 9;
  131. { Direct 32 bit zero extended }
  132. R_X86_64_32 = 10;
  133. { Direct 32 bit sign extended }
  134. R_X86_64_32S = 11;
  135. { Direct 16 bit zero extended }
  136. R_X86_64_16 = 12;
  137. { 16 bit sign extended pc relative }
  138. R_X86_64_PC16 = 13;
  139. { Direct 8 bit sign extended }
  140. R_X86_64_8 = 14;
  141. { 8 bit sign extended pc relative }
  142. R_X86_64_PC8 = 15;
  143. { ID of module containing symbol }
  144. R_X86_64_DTPMOD64 = 16;
  145. { Offset in module's TLS block }
  146. R_X86_64_DTPOFF64 = 17;
  147. { Offset in initial TLS block }
  148. R_X86_64_TPOFF64 = 18;
  149. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  150. R_X86_64_TLSGD = 19;
  151. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  152. R_X86_64_TLSLD = 20;
  153. { Offset in TLS block }
  154. R_X86_64_DTPOFF32 = 21;
  155. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  156. R_X86_64_GOTTPOFF = 22;
  157. { Offset in initial TLS block }
  158. R_X86_64_TPOFF32 = 23;
  159. R_X86_64_NUM = 24;
  160. SHN_UNDEF = 0;
  161. SHN_ABS = $fff1;
  162. SHN_COMMON = $fff2;
  163. SHT_NULL = 0;
  164. SHT_PROGBITS = 1;
  165. SHT_SYMTAB = 2;
  166. SHT_STRTAB = 3;
  167. SHT_RELA = 4;
  168. SHT_HASH = 5;
  169. SHT_DYNAMIC = 6;
  170. SHT_NOTE = 7;
  171. SHT_NOBITS = 8;
  172. SHT_REL = 9;
  173. SHT_SHLIB = 10;
  174. SHT_DYNSYM = 11;
  175. SHF_WRITE = 1;
  176. SHF_ALLOC = 2;
  177. SHF_EXECINSTR = 4;
  178. STB_LOCAL = 0;
  179. STB_GLOBAL = 1;
  180. STB_WEAK = 2;
  181. STT_NOTYPE = 0;
  182. STT_OBJECT = 1;
  183. STT_FUNC = 2;
  184. STT_SECTION = 3;
  185. STT_FILE = 4;
  186. type
  187. { Structures which are written directly to the output file }
  188. TElf32header=packed record
  189. magic0123 : longint;
  190. file_class : byte;
  191. data_encoding : byte;
  192. file_version : byte;
  193. padding : array[$07..$0f] of byte;
  194. e_type : word;
  195. e_machine : word;
  196. e_version : longint;
  197. e_entry : longint; { entrypoint }
  198. e_phoff : longint; { program header offset }
  199. e_shoff : longint; { sections header offset }
  200. e_flags : longint;
  201. e_ehsize : word; { elf header size in bytes }
  202. e_phentsize : word; { size of an entry in the program header array }
  203. e_phnum : word; { 0..e_phnum-1 of entrys }
  204. e_shentsize : word; { size of an entry in sections header array }
  205. e_shnum : word; { 0..e_shnum-1 of entrys }
  206. e_shstrndx : word; { index of string section header }
  207. end;
  208. TElf32sechdr=packed record
  209. sh_name : longint;
  210. sh_type : longint;
  211. sh_flags : longint;
  212. sh_addr : longint;
  213. sh_offset : longint;
  214. sh_size : longint;
  215. sh_link : longint;
  216. sh_info : longint;
  217. sh_addralign : longint;
  218. sh_entsize : longint;
  219. end;
  220. TElf32reloc=packed record
  221. address : longint;
  222. info : longint; { bit 0-7: type, 8-31: symbol }
  223. end;
  224. TElf32symbol=packed record
  225. st_name : longint;
  226. st_value : longint;
  227. st_size : longint;
  228. st_info : byte; { bit 0-3: type, 4-7: bind }
  229. st_other : byte;
  230. st_shndx : word;
  231. end;
  232. telf64header=packed record
  233. magic0123 : longint;
  234. file_class : byte;
  235. data_encoding : byte;
  236. file_version : byte;
  237. padding : array[$07..$0f] of byte;
  238. e_type : word;
  239. e_machine : word;
  240. e_version : longint;
  241. e_entry : qword; { entrypoint }
  242. e_phoff : qword; { program header offset }
  243. e_shoff : qword; { sections header offset }
  244. e_flags : longint;
  245. e_ehsize : word; { elf header size in bytes }
  246. e_phentsize : word; { size of an entry in the program header array }
  247. e_phnum : word; { 0..e_phnum-1 of entrys }
  248. e_shentsize : word; { size of an entry in sections header array }
  249. e_shnum : word; { 0..e_shnum-1 of entrys }
  250. e_shstrndx : word; { index of string section header }
  251. end;
  252. telf64sechdr=packed record
  253. sh_name : longint;
  254. sh_type : longint;
  255. sh_flags : qword;
  256. sh_addr : qword;
  257. sh_offset : qword;
  258. sh_size : qword;
  259. sh_link : longint;
  260. sh_info : longint;
  261. sh_addralign : qword;
  262. sh_entsize : qword;
  263. end;
  264. telf64reloc=packed record
  265. address : qword;
  266. info : qword; { bit 0-7: type, 8-31: symbol }
  267. addend : qword;
  268. end;
  269. telf64symbol=packed record
  270. st_name : longint;
  271. st_info : byte; { bit 0-3: type, 4-7: bind }
  272. st_other : byte;
  273. st_shndx : word;
  274. st_value : qword;
  275. st_size : qword;
  276. end;
  277. telf64stab=packed record
  278. strpos : longint;
  279. ntype : byte;
  280. nother : byte;
  281. ndesc : word;
  282. nvalue : longint;
  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(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  459. begin
  460. inherited create(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. relocsect:=nil;
  469. end;
  470. constructor TElfObjSection.create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  471. var
  472. aoptions : TObjSectionOptions;
  473. begin
  474. decodesechdrflags(Ashtype,Ashflags,aoptions);
  475. inherited create(Aname,Aalign,aoptions);
  476. secshidx:=0;
  477. shstridx:=0;
  478. shtype:=AshType;
  479. shflags:=AshFlags;
  480. shlink:=Ashlink;
  481. shinfo:=Ashinfo;
  482. shentsize:=Aentsize;
  483. relocsect:=nil;
  484. end;
  485. destructor TElfObjSection.destroy;
  486. begin
  487. if assigned(relocsect) then
  488. relocsect.free;
  489. inherited destroy;
  490. end;
  491. {****************************************************************************
  492. TElfObjData
  493. ****************************************************************************}
  494. constructor TElfObjData.create(const n:string);
  495. begin
  496. inherited create(n);
  497. CObjSection:=TElfObjSection;
  498. { reset }
  499. Syms:=TDynamicArray.Create(symbolresize);
  500. { default sections }
  501. symtabsect:=TElfObjSection.create_ext('.symtab',2,0,0,0,4,sizeof(telfsymbol));
  502. strtabsect:=TElfObjSection.create_ext('.strtab',3,0,0,0,1,0);
  503. shstrtabsect:=TElfObjSection.create_ext('.shstrtab',3,0,0,0,1,0);
  504. { insert the empty and filename as first in strtab }
  505. strtabsect.writestr(#0);
  506. strtabsect.writestr(SplitFileName(current_module.mainsource^)+#0);
  507. { we need at least the following sections }
  508. createsection(sec_code,'');
  509. createsection(sec_data,'');
  510. createsection(sec_bss,'');
  511. if tf_section_threadvars in target_info.flags then
  512. createsection(sec_threadvar,'');
  513. if (tf_needs_dwarf_cfi in target_info.flags) and
  514. (af_supports_dwarf in target_asm.flags) then
  515. createsection(sec_debug_frame,'');
  516. end;
  517. destructor TElfObjData.destroy;
  518. begin
  519. Syms.Free;
  520. symtabsect.free;
  521. strtabsect.free;
  522. shstrtabsect.free;
  523. inherited destroy;
  524. end;
  525. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
  526. const
  527. secnames : array[TAsmSectiontype] of string[13] = ('',
  528. {$ifdef userodata}
  529. '.text','.data','.rodata','.bss','.threadvar',
  530. {$else userodata}
  531. '.text','.data','.data','.bss','.threadvar',
  532. {$endif userodata}
  533. '.text', { darwin stubs }
  534. '.stab','.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. ''
  540. );
  541. begin
  542. if (use_smartlink_section and
  543. (aname<>'')) or (atype=sec_fpc) then
  544. result:=secnames[atype]+'.'+aname
  545. else
  546. result:=secnames[atype];
  547. end;
  548. function TElfObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  549. begin
  550. if atype=sec_stabstr then
  551. result:=1
  552. else
  553. result:=sizeof(aint);
  554. end;
  555. procedure TElfObjData.CreateDebugSections;
  556. begin
  557. stabssec:=createsection(sec_stab,'');
  558. stabstrsec:=createsection(sec_stabstr,'');
  559. end;
  560. procedure TElfObjData.writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);
  561. var
  562. symaddr : longint;
  563. begin
  564. if CurrObjSec=nil then
  565. internalerror(200403292);
  566. {$ifdef userodata}
  567. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  568. internalerror(200408252);
  569. {$endif userodata}
  570. if assigned(p) then
  571. begin
  572. { real address of the symbol }
  573. symaddr:=p.address;
  574. { Local ObjSymbols can be resolved already or need a section reloc }
  575. if (p.bind=AB_LOCAL) then
  576. begin
  577. { For a relative relocation in the same section the
  578. value can be calculated }
  579. if (p.objsection=CurrObjSec) and
  580. (relative=RELOC_RELATIVE) then
  581. inc(data,symaddr-len-CurrObjSec.Size)
  582. else
  583. begin
  584. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,relative);
  585. inc(data,symaddr);
  586. end;
  587. end
  588. else
  589. begin
  590. CurrObjSec.addsymreloc(CurrObjSec.Size,p,relative);
  591. {$ifndef x86_64}
  592. if relative=RELOC_RELATIVE then
  593. dec(data,len);
  594. {$endif x86_64}
  595. end;
  596. end;
  597. CurrObjSec.write(data,len);
  598. end;
  599. procedure TElfObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  600. var
  601. stab : TObjStabEntry;
  602. begin
  603. if not assigned(StabsSec) then
  604. internalerror(200602271);
  605. fillchar(stab,sizeof(TObjStabEntry),0);
  606. if assigned(p) and (p[0]<>#0) then
  607. begin
  608. stab.strpos:=stabstrsec.Size;
  609. stabstrsec.write(p^,strlen(p)+1);
  610. end;
  611. stab.ntype:=nidx;
  612. stab.ndesc:=ndesc;
  613. stab.nother:=nother;
  614. stab.nvalue:=offset;
  615. stabssec.write(stab,sizeof(stab));
  616. if assigned(ps) then
  617. stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
  618. end;
  619. {****************************************************************************
  620. TElfObjectOutput
  621. ****************************************************************************}
  622. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  623. begin
  624. inherited Create(AWriter);
  625. CObjData:=TElfObjData;
  626. end;
  627. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection);
  628. var
  629. {$ifdef ver2_0_0}
  630. relnative,
  631. {$endif ver2_0_0}
  632. rel : telfreloc;
  633. r : TObjRelocation;
  634. relsym,reltyp : longint;
  635. begin
  636. with elf32data do
  637. begin
  638. {$ifdef userodata}
  639. { rodata can't have relocations }
  640. if s.sectype=sec_rodata then
  641. begin
  642. if assigned(s.relocations.first) then
  643. internalerror(200408251);
  644. exit;
  645. end;
  646. {$endif userodata}
  647. { create the reloc section }
  648. {$ifdef i386}
  649. s.relocsect:=TElfObjSection.create_ext('.rel'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  650. {$else i386}
  651. s.relocsect:=TElfObjSection.create_ext('.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  652. {$endif i386}
  653. { add the relocations }
  654. r:=TObjRelocation(s.relocations.first);
  655. while assigned(r) do
  656. begin
  657. rel.address:=r.dataoffset;
  658. if assigned(r.symbol) then
  659. begin
  660. if (r.symbol.bind=AB_LOCAL) then
  661. relsym:=r.symbol.objsection.secsymidx
  662. else
  663. begin
  664. if r.symbol.symidx=-1 then
  665. internalerror(200603012);
  666. relsym:=r.symbol.symidx;
  667. end;
  668. end
  669. else
  670. if r.objsection<>nil then
  671. relsym:=r.objsection.secsymidx
  672. else
  673. relsym:=SHN_UNDEF;
  674. { when things settle down, we can create processor specific
  675. derived classes
  676. }
  677. {$ifdef i386}
  678. case r.typ of
  679. RELOC_RELATIVE :
  680. reltyp:=R_386_PC32;
  681. RELOC_ABSOLUTE :
  682. reltyp:=R_386_32;
  683. end;
  684. {$endif i386}
  685. {$ifdef sparc}
  686. case r.typ of
  687. { RELOC_RELATIVE :
  688. reltyp:=R_386_PC32;
  689. }
  690. RELOC_ABSOLUTE :
  691. reltyp:=R_SPARC_32;
  692. else
  693. internalerror(200410201);
  694. end;
  695. {$endif sparc}
  696. {$ifdef x86_64}
  697. case r.typ of
  698. RELOC_RELATIVE :
  699. begin
  700. reltyp:=R_X86_64_PC32;
  701. { length of the relocated location is handled here }
  702. rel.addend:=qword(-4);
  703. end;
  704. RELOC_ABSOLUTE :
  705. reltyp:=R_X86_64_64;
  706. else
  707. internalerror(200602261);
  708. end;
  709. {$endif x86_64}
  710. {$ifdef cpu64bit}
  711. rel.info:=(qword(relsym) shl 32) or reltyp;
  712. {$else cpu64bit}
  713. rel.info:=(relsym shl 8) or reltyp;
  714. {$endif cpu64bit}
  715. { write reloc }
  716. {$ifdef ver2_0_0}
  717. relnative:=MaybeSwapElfReloc(rel);
  718. s.relocsect.write(relnative,sizeof(rel));
  719. {$else}
  720. s.relocsect.write(MaybeSwapElfReloc(rel),sizeof(rel));
  721. {$endif ver2_0_0}
  722. r:=TObjRelocation(r.next);
  723. end;
  724. end;
  725. end;
  726. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  727. var
  728. {$ifdef ver2_0_0}
  729. elfsymnative,
  730. {$endif ver2_0_0}
  731. elfsym : telfsymbol;
  732. begin
  733. fillchar(elfsym,sizeof(elfsym),0);
  734. elfsym.st_name:=astridx;
  735. elfsym.st_info:=ainfo;
  736. elfsym.st_shndx:=ashndx;
  737. inc(symidx);
  738. inc(localsyms);
  739. {$ifdef ver2_0_0}
  740. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  741. elf32data.symtabsect.write(elfsymnative,sizeof(elfsym));
  742. {$else}
  743. elf32data.symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  744. {$endif ver2_0_0}
  745. end;
  746. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  747. begin
  748. TObjSection(p).secsymidx:=symidx;
  749. write_internal_symbol(TElfObjSection(p).shstridx,STT_SECTION,TElfObjSection(p).secshidx);
  750. end;
  751. procedure TElfObjectOutput.createsymtab;
  752. var
  753. {$ifdef ver2_0_0}
  754. elfsymnative,
  755. {$endif}
  756. elfsym : telfsymbol;
  757. i : longint;
  758. objsym : TObjSymbol;
  759. begin
  760. with elf32data do
  761. begin
  762. symidx:=0;
  763. localsyms:=0;
  764. { empty entry }
  765. write_internal_symbol(0,0,0);
  766. { filename entry }
  767. write_internal_symbol(1,STT_FILE,SHN_ABS);
  768. { section }
  769. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  770. { ObjSymbols }
  771. for i:=0 to ObjSymbolList.Count-1 do
  772. begin
  773. objsym:=TObjSymbol(ObjSymbolList[i]);
  774. if objsym.bind<>AB_LOCAL then
  775. begin
  776. fillchar(elfsym,sizeof(elfsym),0);
  777. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  778. elfsym.st_name:=strtabsect.Size;
  779. strtabsect.writestr(objsym.name);
  780. strtabsect.writestr(#0);
  781. elfsym.st_size:=objsym.size;
  782. case objsym.bind of
  783. AB_LOCAL :
  784. begin
  785. elfsym.st_value:=objsym.address;
  786. elfsym.st_info:=STB_LOCAL shl 4;
  787. inc(localsyms);
  788. end;
  789. AB_COMMON :
  790. begin
  791. elfsym.st_value:=$10;
  792. elfsym.st_info:=STB_GLOBAL shl 4;
  793. end;
  794. AB_EXTERNAL :
  795. elfsym.st_info:=STB_GLOBAL shl 4;
  796. AB_GLOBAL :
  797. begin
  798. elfsym.st_value:=objsym.address;
  799. elfsym.st_info:=STB_GLOBAL shl 4;
  800. end;
  801. end;
  802. if (objsym.bind<>AB_EXTERNAL) and
  803. not(assigned(objsym.objsection) and
  804. not(oso_data in objsym.objsection.secoptions)) then
  805. begin
  806. case objsym.typ of
  807. AT_FUNCTION :
  808. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  809. AT_DATA :
  810. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  811. end;
  812. end;
  813. if objsym.bind=AB_COMMON then
  814. elfsym.st_shndx:=SHN_COMMON
  815. else
  816. begin
  817. if assigned(objsym.objsection) then
  818. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  819. else
  820. elfsym.st_shndx:=SHN_UNDEF;
  821. end;
  822. objsym.symidx:=symidx;
  823. inc(symidx);
  824. {$ifdef ver2_0_0}
  825. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  826. symtabsect.write(elfsymnative,sizeof(elfsym));
  827. {$else}
  828. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  829. {$endif ver2_0_0}
  830. end;
  831. end;
  832. { update the .symtab section header }
  833. symtabsect.shlink:=strtabsect.secshidx;
  834. symtabsect.shinfo:=localsyms;
  835. end;
  836. end;
  837. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  838. begin
  839. TElfObjSection(p).shstridx:=elf32data.shstrtabsect.writestr(TObjSection(p).name+#0);
  840. if assigned(TElfObjSection(p).relocsect) then
  841. TElfObjSection(p).relocsect.shstridx:=elf32data.shstrtabsect.writestr(TElfObjSection(p).relocsect.name+#0);
  842. end;
  843. procedure TElfObjectOutput.createshstrtab;
  844. begin
  845. with elf32data do
  846. begin
  847. with shstrtabsect do
  848. begin
  849. writestr(#0);
  850. symtabsect.shstridx:=writestr('.symtab'#0);
  851. strtabsect.shstridx:=writestr('.strtab'#0);
  852. shstrtabsect.shstridx:=writestr('.shstrtab'#0);
  853. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  854. end;
  855. end;
  856. end;
  857. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  858. var
  859. {$ifdef ver2_0_0}
  860. sechdrnative,
  861. {$endif ver2_0_0}
  862. sechdr : telfsechdr;
  863. begin
  864. fillchar(sechdr,sizeof(sechdr),0);
  865. sechdr.sh_name:=s.shstridx;
  866. sechdr.sh_type:=s.shtype;
  867. sechdr.sh_flags:=s.shflags;
  868. sechdr.sh_offset:=s.datapos;
  869. sechdr.sh_size:=s.Size;
  870. sechdr.sh_link:=s.shlink;
  871. sechdr.sh_info:=s.shinfo;
  872. sechdr.sh_addralign:=s.secalign;
  873. sechdr.sh_entsize:=s.shentsize;
  874. {$ifdef ver2_0_0}
  875. sechdrnative:=MaybeSwapSecHeader(sechdr);
  876. writer.write(sechdrnative,sizeof(sechdr));
  877. {$else}
  878. writer.write(MaybeSwapSecHeader(sechdr),sizeof(sechdr));
  879. {$endif ver2_0_0}
  880. end;
  881. procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
  882. begin
  883. FWriter.writezeros(s.dataalignbytes);
  884. FWriter.writearray(s.data);
  885. end;
  886. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  887. begin
  888. TElfObjSection(p).secshidx:=pword(arg)^;
  889. inc(pword(arg)^);
  890. if TElfObjSection(p).relocations.count>0 then
  891. inc(pword(arg)^);
  892. end;
  893. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  894. begin
  895. if (TElfObjSection(p).relocations.count>0) then
  896. createrelocsection(TElfObjSection(p));
  897. end;
  898. procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
  899. begin
  900. TObjSection(p).setdatapos(paint(arg)^);
  901. end;
  902. procedure TElfObjectOutput.section_relocsec_set_datapos(p:TObject;arg:pointer);
  903. begin
  904. if assigned(TElfObjSection(p).relocsect) then
  905. TElfObjSection(p).relocsect.setdatapos(paint(arg)^);
  906. end;
  907. procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
  908. begin
  909. if (oso_data in TObjSection(p).secoptions) then
  910. begin
  911. if TObjSection(p).data=nil then
  912. internalerror(200403073);
  913. writesectiondata(TElfObjSection(p));
  914. end;
  915. end;
  916. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  917. begin
  918. writesectionheader(TElfObjSection(p));
  919. if assigned(TElfObjSection(p).relocsect) then
  920. writesectionheader(TElfObjSection(p).relocsect);
  921. end;
  922. procedure TElfObjectOutput.section_write_relocsec(p:TObject;arg:pointer);
  923. begin
  924. if assigned(TElfObjSection(p).relocsect) then
  925. writesectiondata(TElfObjSection(p).relocsect);
  926. end;
  927. function TElfObjectOutput.writedata(data:TObjData):boolean;
  928. var
  929. {$ifdef ver2_0_0}
  930. headernative,
  931. {$endif ver2_0_0}
  932. header : telfheader;
  933. shoffset,
  934. datapos : aint;
  935. nsections : word;
  936. begin
  937. result:=false;
  938. elf32data:=TElfObjData(data);
  939. with elf32data do
  940. begin
  941. { calc amount of sections we have }
  942. nsections:=1;
  943. { also create the index in the section header table }
  944. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  945. { add default sections }
  946. shstrtabsect.secshidx:=nsections;
  947. inc(nsections);
  948. symtabsect.secshidx:=nsections;
  949. inc(nsections);
  950. strtabsect.secshidx:=nsections;
  951. inc(nsections);
  952. { create .symtab and .strtab }
  953. createsymtab;
  954. { Create the relocation sections }
  955. ObjSectionList.ForEachCall(@section_create_relocsec,nil);
  956. { create .shstrtab }
  957. createshstrtab;
  958. { Calculate the filepositions }
  959. datapos:=$40; { elfheader + alignment }
  960. { sections first }
  961. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  962. { shstrtab }
  963. shstrtabsect.setdatapos(datapos);
  964. { section headers }
  965. shoffset:=datapos;
  966. inc(datapos,nsections*sizeof(telfsechdr));
  967. { symtab }
  968. symtabsect.setdatapos(datapos);
  969. { strtab }
  970. strtabsect.setdatapos(datapos);
  971. { .rel sections }
  972. ObjSectionList.ForEachCall(@section_relocsec_set_datapos,@datapos);
  973. { Write ELF Header }
  974. fillchar(header,sizeof(header),0);
  975. header.magic0123:=$464c457f; { = #127'ELF' }
  976. {$ifdef cpu64bit}
  977. header.file_class:=2;
  978. {$else cpu64bit}
  979. header.file_class:=1;
  980. {$endif cpu64bit}
  981. if target_info.endian=endian_big then
  982. header.data_encoding:=2
  983. else
  984. header.data_encoding:=1;
  985. header.file_version:=1;
  986. header.e_type:=1;
  987. {$ifdef sparc}
  988. header.e_machine:=2;
  989. {$endif sparc}
  990. {$ifdef i386}
  991. header.e_machine:=3;
  992. {$endif i386}
  993. {$ifdef m68k}
  994. header.e_machine:=4;
  995. {$endif m68k}
  996. {$ifdef powerpc}
  997. header.e_machine:=20;
  998. {$endif powerpc}
  999. {$ifdef arm}
  1000. header.e_machine:=40;
  1001. {$endif arm}
  1002. {$ifdef x86_64}
  1003. header.e_machine:=62;
  1004. {$endif x86_64}
  1005. header.e_version:=1;
  1006. header.e_shoff:=shoffset;
  1007. header.e_shstrndx:=shstrtabsect.secshidx;
  1008. header.e_shnum:=nsections;
  1009. header.e_ehsize:=sizeof(telfheader);
  1010. header.e_shentsize:=sizeof(telfsechdr);
  1011. {$ifdef ver2_0_0}
  1012. headernative:=MaybeSwapHeader(header);
  1013. writer.write(headernative,sizeof(header));
  1014. {$else}
  1015. writer.write(MaybeSwapHeader(header),sizeof(header));
  1016. {$endif ver2_0_0}
  1017. writer.writezeros($40-sizeof(header)); { align }
  1018. { Sections }
  1019. ObjSectionList.ForEachCall(@section_write_data,nil);
  1020. { .shstrtab }
  1021. writesectiondata(shstrtabsect);
  1022. { section headers, start with an empty header for sh_undef }
  1023. writer.writezeros(sizeof(telfsechdr));
  1024. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1025. writesectionheader(shstrtabsect);
  1026. writesectionheader(symtabsect);
  1027. writesectionheader(strtabsect);
  1028. { .symtab }
  1029. writesectiondata(symtabsect);
  1030. { .strtab }
  1031. writesectiondata(strtabsect);
  1032. { .rel sections }
  1033. ObjSectionList.ForEachCall(@section_write_relocsec,nil);
  1034. end;
  1035. result:=true;
  1036. end;
  1037. {****************************************************************************
  1038. TELFAssembler
  1039. ****************************************************************************}
  1040. constructor TElfAssembler.Create(smart:boolean);
  1041. begin
  1042. inherited Create(smart);
  1043. CObjOutput:=TElfObjectOutput;
  1044. end;
  1045. {*****************************************************************************
  1046. Initialize
  1047. *****************************************************************************}
  1048. {$ifdef i386}
  1049. const
  1050. as_i386_elf32_info : tasminfo =
  1051. (
  1052. id : as_i386_elf32;
  1053. idtxt : 'ELF';
  1054. asmbin : '';
  1055. asmcmd : '';
  1056. supported_target : system_any; //target_i386_linux;
  1057. flags : [af_outputbinary,af_smartlink_sections];
  1058. labelprefix : '.L';
  1059. comment : '';
  1060. );
  1061. {$endif i386}
  1062. {$ifdef x86_64}
  1063. const
  1064. as_x86_64_elf64_info : tasminfo =
  1065. (
  1066. id : as_x86_64_elf64;
  1067. idtxt : 'ELF';
  1068. asmbin : '';
  1069. asmcmd : '';
  1070. supported_target : system_any; //target_i386_linux;
  1071. flags : [af_outputbinary,af_smartlink_sections];
  1072. labelprefix : '.L';
  1073. comment : '';
  1074. );
  1075. {$endif x86_64}
  1076. {$ifdef sparc}
  1077. const
  1078. as_sparc_elf32_info : tasminfo =
  1079. (
  1080. id : as_sparc_elf32;
  1081. idtxt : 'ELF';
  1082. asmbin : '';
  1083. asmcmd : '';
  1084. supported_target : system_any; //target_i386_linux;
  1085. // flags : [af_outputbinary,af_smartlink_sections];
  1086. flags : [af_outputbinary];
  1087. labelprefix : '.L';
  1088. comment : '';
  1089. );
  1090. {$endif sparc}
  1091. initialization
  1092. {$ifdef i386}
  1093. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1094. {$endif i386}
  1095. {$ifdef sparc}
  1096. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1097. {$endif sparc}
  1098. {$ifdef x86_64}
  1099. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1100. {$endif x86_64}
  1101. end.