Line data Source code
1 : #ifndef HEADER_fd_src_ballet_elf_fd_elf_h 2 : #define HEADER_fd_src_ballet_elf_fd_elf_h 3 : 4 : /* Executable and Linking Format (ELF) */ 5 : 6 : #include "../../util/fd_util.h" 7 : #include <string.h> 8 : 9 : /* FD_ELF_EI: File type related */ 10 : 11 : #define FD_ELF_EI_MAG0 0 12 : #define FD_ELF_EI_MAG1 1 13 : #define FD_ELF_EI_MAG2 2 14 : #define FD_ELF_EI_MAG3 3 15 102 : #define FD_ELF_EI_CLASS 4 16 102 : #define FD_ELF_EI_DATA 5 17 102 : #define FD_ELF_EI_VERSION 6 18 9 : #define FD_ELF_EI_OSABI 7 19 : #define FD_ELF_EI_ABIVERSION 8 20 : #define FD_ELF_EI_NIDENT 16 21 : 22 : /* FD_ELF_MAX: Magic number */ 23 : 24 : /* [127, 69, 76, 70] -> [0x7f, 0x45, 0x4c, 0x46] */ 25 102 : #define FD_ELF_MAG_LE 0x464c457fU 26 : 27 : /* FD_ELF_CLASS: 32-bit/64-bit architecture */ 28 : 29 : #define FD_ELF_CLASS_NONE 0 30 : #define FD_ELF_CLASS_32 1 31 102 : #define FD_ELF_CLASS_64 2 32 : 33 : /* FD_ELF_DATA: Endianness */ 34 : 35 : #define FD_ELF_DATA_NONE 0 36 102 : #define FD_ELF_DATA_LE 1 37 : #define FD_ELF_DATA_BE 2 38 : 39 : /* FD_ELF_OSABI */ 40 : 41 9 : #define FD_ELF_OSABI_NONE 0 42 : 43 : /* FD_ELF_ET: ELF file type */ 44 : 45 : #define FD_ELF_ET_NONE 0 46 : #define FD_ELF_ET_REL 1 /* relocatable static object */ 47 : #define FD_ELF_ET_EXEC 2 /* executable */ 48 9 : #define FD_ELF_ET_DYN 3 /* shared object */ 49 : #define FD_ELF_ET_CORE 4 /* core dump */ 50 : 51 : /* FD_ELF_EM: Machine type */ 52 : 53 : #define FD_ELF_EM_NONE 0 54 : #define FD_ELF_EM_BPF 247 55 9 : #define FD_ELF_EM_SBPF 263 56 : 57 : /* FD_ELF_EF: ELF flags */ 58 : 59 : #define FD_ELF_EF_SBPF_V2 32 60 : 61 : /* FD_ELF_PT: Program header type */ 62 : 63 : #define FD_ELF_PT_NULL 0 64 36 : #define FD_ELF_PT_LOAD 1 65 168 : #define FD_ELF_PT_DYNAMIC 2 66 : 67 : /* FD_ELF_SHT: Section header type */ 68 : 69 : #define FD_ELF_SHT_NULL 0 70 : #define FD_ELF_SHT_PROGBITS 1 71 : #define FD_ELF_SHT_SYMTAB 2 72 : #define FD_ELF_SHT_STRTAB 3 73 : #define FD_ELF_SHT_RELA 4 74 : #define FD_ELF_SHT_HASH 5 75 1716 : #define FD_ELF_SHT_DYNAMIC 6 76 6045 : #define FD_ELF_SHT_NOBITS 8 77 : #define FD_ELF_SHT_REL 9 78 : #define FD_ELF_SHT_DYNSYM 11 79 : 80 : /* FD_ELF_SHF: Section header flags */ 81 : 82 : #define FD_ELF_SHF_WRITE 0x1 83 : #define FD_ELF_SHF_ALLOC 0x2 84 : #define FD_ELF_SHF_EXECINSTR 0x4 85 : 86 : /* FD_ELF_DT: Dynamic entry type */ 87 : 88 : #define FD_ELF_DT_NULL 0 89 81 : #define FD_ELF_DT_SYMTAB 6 90 81 : #define FD_ELF_DT_REL 17 91 66 : #define FD_ELF_DT_RELSZ 18 92 : #define FD_ELF_DT_RELENT 19 93 : #define FD_ELF_DT_NUM 35 94 : 95 : /* FD_ELF64_ST_TYPE extracts the symbol type from symbol st_info */ 96 : 97 1920 : #define FD_ELF64_ST_TYPE(i) ((i)&0xF) 98 : 99 : /* FD_ELF_STT: Symbol type */ 100 : 101 : #define FD_ELF_STT_NOTYPE 0 102 1920 : #define FD_ELF_STT_FUNC 2 103 : 104 : /* FD_ELF64_R_SYM extracts the symbol index from reloc r_info. 105 : FD_ELF64_R_TYPE extracts the relocation type from reloc r_info. */ 106 : 107 1926 : #define FD_ELF64_R_SYM(i) ((uint)((ulong)(i) >> 32)) 108 7494 : #define FD_ELF64_R_TYPE(i) ((uint)((ulong)(i) & 0xFFFFFFFF)) 109 : 110 : /* FD_ELF_R_BPF: BPF relocation types */ 111 : 112 6 : #define FD_ELF_R_BPF_64_64 1 /* 64-bit immediate (lddw form) */ 113 5568 : #define FD_ELF_R_BPF_64_RELATIVE 8 114 1920 : #define FD_ELF_R_BPF_64_32 10 115 : 116 : FD_PROTOTYPES_BEGIN 117 : 118 : /* fd_elf_read_cstr: Validate cstr and return pointer. Given memory 119 : region buf of size buf_sz, attempt to read cstr at offset off in 120 : [0,buf_sz) If buf_sz is 0, buf may be an invalid pointer. Returns 121 : pointer to first byte of cstr in buf on success, and NULL on failure. 122 : Reasons for failure include: off or cstr is out-of-bounds, footprint 123 : of cstr (including NUL) greater than max_sz. */ 124 : 125 : FD_FN_PURE static inline char const * 126 : fd_elf_read_cstr( void const * buf, 127 : ulong buf_sz, 128 : ulong off, 129 27 : ulong max_sz ) { 130 : 131 27 : if( FD_UNLIKELY( off>=buf_sz ) ) 132 3 : return NULL; 133 : 134 24 : char const * str = (char const *)( (ulong)buf + off ); 135 24 : ulong str_sz = buf_sz - off; 136 : 137 24 : ulong n = fd_ulong_min( str_sz, max_sz ); 138 24 : if( FD_UNLIKELY( fd_cstr_nlen( str, n )==n ) ) 139 15 : return NULL; 140 : 141 9 : return str; 142 24 : } 143 : 144 : FD_PROTOTYPES_END 145 : 146 : /* Re-export sibling headers for convenience */ 147 : 148 : #include "fd_elf64.h" 149 : 150 : #endif /* HEADER_fd_src_ballet_elf_fd_elf_h */ 151 :