xref: /linux/tools/perf/util/genelf.h (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __GENELF_H__
3 #define __GENELF_H__
4 
5 /* genelf.c */
6 int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
7 		  const void *code, int csize, void *debug, int nr_debug_entries,
8 		  void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
9 #ifdef HAVE_DWARF_SUPPORT
10 /* genelf_debug.c */
11 int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries);
12 #endif
13 
14 #if   defined(__arm__)
15 #define GEN_ELF_ARCH	EM_ARM
16 #define GEN_ELF_CLASS	ELFCLASS32
17 #elif defined(__aarch64__)
18 #define GEN_ELF_ARCH	EM_AARCH64
19 #define GEN_ELF_CLASS	ELFCLASS64
20 #elif defined(__x86_64__)
21 #define GEN_ELF_ARCH	EM_X86_64
22 #define GEN_ELF_CLASS	ELFCLASS64
23 #elif defined(__i386__)
24 #define GEN_ELF_ARCH	EM_386
25 #define GEN_ELF_CLASS	ELFCLASS32
26 #elif defined(__powerpc64__)
27 #define GEN_ELF_ARCH	EM_PPC64
28 #define GEN_ELF_CLASS	ELFCLASS64
29 #elif defined(__powerpc__)
30 #define GEN_ELF_ARCH	EM_PPC
31 #define GEN_ELF_CLASS	ELFCLASS32
32 #elif defined(__sparc__) && defined(__arch64__)
33 #define GEN_ELF_ARCH	EM_SPARCV9
34 #define GEN_ELF_CLASS	ELFCLASS64
35 #elif defined(__sparc__)
36 #define GEN_ELF_ARCH	EM_SPARC
37 #define GEN_ELF_CLASS	ELFCLASS32
38 #elif defined(__s390x__)
39 #define GEN_ELF_ARCH	EM_S390
40 #define GEN_ELF_CLASS	ELFCLASS64
41 #elif defined(__riscv) && __riscv_xlen == 64
42 #define GEN_ELF_ARCH	EM_RISCV
43 #define GEN_ELF_CLASS	ELFCLASS64
44 #else
45 #error "unsupported architecture"
46 #endif
47 
48 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
49 #define GEN_ELF_ENDIAN	ELFDATA2MSB
50 #else
51 #define GEN_ELF_ENDIAN	ELFDATA2LSB
52 #endif
53 
54 #if GEN_ELF_CLASS == ELFCLASS64
55 #define elf_newehdr	elf64_newehdr
56 #define elf_newphdr	elf64_newphdr
57 #define elf_getshdr	elf64_getshdr
58 #define Elf_Ehdr	Elf64_Ehdr
59 #define Elf_Phdr	Elf64_Phdr
60 #define Elf_Shdr	Elf64_Shdr
61 #define Elf_Sym		Elf64_Sym
62 #define ELF_ST_TYPE(a)	ELF64_ST_TYPE(a)
63 #define ELF_ST_BIND(a)	ELF64_ST_BIND(a)
64 #define ELF_ST_VIS(a)	ELF64_ST_VISIBILITY(a)
65 #else
66 #define elf_newehdr	elf32_newehdr
67 #define elf_newphdr	elf32_newphdr
68 #define elf_getshdr	elf32_getshdr
69 #define Elf_Ehdr	Elf32_Ehdr
70 #define Elf_Phdr	Elf32_Phdr
71 #define Elf_Shdr	Elf32_Shdr
72 #define Elf_Sym		Elf32_Sym
73 #define ELF_ST_TYPE(a)	ELF32_ST_TYPE(a)
74 #define ELF_ST_BIND(a)	ELF32_ST_BIND(a)
75 #define ELF_ST_VIS(a)	ELF32_ST_VISIBILITY(a)
76 #endif
77 
78 /* The .text section is directly after the ELF header */
79 #define GEN_ELF_TEXT_OFFSET sizeof(Elf_Ehdr)
80 
81 #endif
82