LCOV - code coverage report
Current view: top level - ballet/elf - fd_elf.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 37 37 100.0 %
Date: 2025-09-19 04:41:14 Functions: 1 118 0.8 %

          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           9 : #define FD_ELF_EI_CLASS       4
      16           9 : #define FD_ELF_EI_DATA        5
      17           9 : #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           9 : #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           9 : #define FD_ELF_CLASS_64   2
      32             : 
      33             : /* FD_ELF_DATA: Endianness */
      34             : 
      35             : #define FD_ELF_DATA_NONE  0
      36           9 : #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         273 : #define FD_ELF_PT_LOAD    1
      65          84 : #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         810 : #define FD_ELF_SHT_DYNAMIC   6
      76        6735 : #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         900 : #define FD_ELF_SHF_WRITE     0x1
      83         900 : #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          66 : #define FD_ELF_DT_SYMTAB   6
      90          51 : #define FD_ELF_DT_REL     17
      91          51 : #define FD_ELF_DT_RELSZ   18
      92          51 : #define FD_ELF_DT_RELENT  19
      93             : 
      94             : /* FD_ELF64_ST_TYPE extracts the symbol type from symbol st_info */
      95             : 
      96        1920 : #define FD_ELF64_ST_TYPE(i) ((i)&0xF)
      97             : 
      98             : /* FD_ELF_STT: Symbol type */
      99             : 
     100             : #define FD_ELF_STT_NOTYPE  0
     101        1920 : #define FD_ELF_STT_FUNC    2
     102             : 
     103             : /* FD_ELF64_R_SYM extracts the symbol index from reloc r_info.
     104             :    FD_ELF64_R_TYPE extracts the relocation type from reloc r_info. */
     105             : 
     106        1926 : #define FD_ELF64_R_SYM(i)  ((uint)((ulong)(i) >> 32))
     107        7494 : #define FD_ELF64_R_TYPE(i) ((uint)((ulong)(i) & 0xFFFFFFFF))
     108             : 
     109             : /* FD_ELF_R_BPF: BPF relocation types */
     110             : 
     111           6 : #define FD_ELF_R_BPF_64_64        1 /* 64-bit immediate (lddw form) */
     112        5568 : #define FD_ELF_R_BPF_64_RELATIVE  8
     113        1920 : #define FD_ELF_R_BPF_64_32       10
     114             : 
     115             : FD_PROTOTYPES_BEGIN
     116             : 
     117             : /* fd_elf_read_cstr: Validate cstr and return pointer.  Given memory
     118             :    region buf of size buf_sz, attempt to read cstr at offset off in
     119             :    [0,buf_sz)  If buf_sz is 0, buf may be an invalid pointer.  Returns
     120             :    pointer to first byte of cstr in buf on success, and NULL on failure.
     121             :    Reasons for failure include: off or cstr is out-of-bounds, footprint
     122             :    of cstr (including NUL) greater than max_sz. */
     123             : 
     124             : FD_FN_PURE static inline char const *
     125             : fd_elf_read_cstr( void const * buf,
     126             :                   ulong        buf_sz,
     127             :                   ulong        off,
     128          27 :                   ulong        max_sz ) {
     129             : 
     130          27 :   if( FD_UNLIKELY( off>=buf_sz ) )
     131           3 :     return NULL;
     132             : 
     133          24 :   char const * str    = (char const *)( (ulong)buf + off );
     134          24 :   ulong        str_sz = buf_sz - off;
     135             : 
     136          24 :   ulong n = fd_ulong_min( str_sz, max_sz );
     137          24 :   if( FD_UNLIKELY( fd_cstr_nlen( str, n )==n ) )
     138          15 :     return NULL;
     139             : 
     140           9 :   return str;
     141          24 : }
     142             : 
     143             : FD_PROTOTYPES_END
     144             : 
     145             : /* Re-export sibling headers for convenience */
     146             : 
     147             : #include "fd_elf64.h"
     148             : 
     149             : #endif /* HEADER_fd_src_ballet_elf_fd_elf_h */
     150             : 

Generated by: LCOV version 1.14