LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_svm_elfgen.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 73 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 2 0.0 %

          Line data    Source code
       1             : #include "fd_svm_elfgen.h"
       2             : #include "../../../ballet/elf/fd_elf64.h"
       3             : 
       4             : static char const shstrtab[] = "\0.text\0.rodata\0.shstrtab\0";
       5           0 : #define SHSTRTAB_OFF_TEXT    1  /* byte offset of ".text"     in shstrtab */
       6           0 : #define SHSTRTAB_OFF_RODATA  7  /* byte offset of ".rodata"   in shstrtab */
       7           0 : #define SHSTRTAB_OFF_SHSTR  15  /* byte offset of ".shstrtab" in shstrtab */
       8             : 
       9             : ulong
      10             : fd_svm_elfgen_sz( ulong text_sz,
      11           0 :                   ulong rodata_sz ) {
      12           0 :   ulong phdr_file_off     = sizeof(fd_elf64_ehdr);
      13           0 :   ulong text_file_off     = fd_ulong_align_up( phdr_file_off + sizeof(fd_elf64_phdr), 8UL );
      14           0 :   ulong rodata_file_off   = text_file_off + text_sz;
      15           0 :   ulong shstrtab_file_off = rodata_file_off + rodata_sz;
      16           0 :   ulong shdr_file_off     = fd_ulong_align_up( shstrtab_file_off + sizeof(shstrtab), 8UL );
      17           0 :   ulong total_sz          = shdr_file_off + 4UL * sizeof(fd_elf64_shdr);
      18           0 :   return total_sz;
      19           0 : }
      20             : 
      21             : void
      22             : fd_svm_elfgen( uchar *       buf,
      23             :                ulong         buf_sz,
      24             :                uchar const * text_data,
      25             :                ulong         text_sz,
      26             :                uchar const * rodata,
      27           0 :                ulong         rodata_sz ) {
      28           0 :   ulong phdr_file_off     = sizeof(fd_elf64_ehdr);
      29           0 :   ulong text_file_off     = fd_ulong_align_up( phdr_file_off + sizeof(fd_elf64_phdr), 8UL );
      30           0 :   ulong rodata_file_off   = text_file_off + text_sz;
      31           0 :   ulong segment_sz        = text_sz + rodata_sz;
      32           0 :   ulong shstrtab_file_off = rodata_file_off + rodata_sz;
      33           0 :   ulong shdr_file_off     = fd_ulong_align_up( shstrtab_file_off + sizeof(shstrtab), 8UL );
      34           0 :   ulong total_sz          = shdr_file_off + 4UL * sizeof(fd_elf64_shdr);
      35             : 
      36           0 :   FD_TEST( total_sz <= buf_sz );
      37           0 :   memset( buf, 0, total_sz );
      38             : 
      39           0 :   FD_STORE( fd_elf64_ehdr, buf, ((fd_elf64_ehdr) {
      40           0 :     .e_ident = { 0x7f,'E','L','F', FD_ELF_CLASS_64, FD_ELF_DATA_LE, 1, FD_ELF_OSABI_NONE },
      41           0 :     .e_type  = FD_ELF_ET_DYN,
      42           0 :     .e_machine   = FD_ELF_EM_BPF,
      43           0 :     .e_version   = 1,
      44           0 :     .e_entry     = text_file_off,
      45           0 :     .e_phoff     = phdr_file_off,
      46           0 :     .e_shoff     = shdr_file_off,
      47           0 :     .e_flags     = 0,
      48           0 :     .e_ehsize    = sizeof(fd_elf64_ehdr),
      49           0 :     .e_phentsize = sizeof(fd_elf64_phdr),
      50           0 :     .e_phnum     = 1,
      51           0 :     .e_shentsize = sizeof(fd_elf64_shdr),
      52           0 :     .e_shnum     = 4,
      53           0 :     .e_shstrndx  = 3
      54           0 :   }) );
      55             : 
      56             :   /* Single PT_LOAD segment covering .text and .rodata */
      57           0 :   fd_elf64_phdr * phdr = (fd_elf64_phdr *)(buf + phdr_file_off);
      58           0 :   phdr->p_type   = FD_ELF_PT_LOAD;
      59           0 :   phdr->p_flags  = 5;
      60           0 :   phdr->p_offset = text_file_off;
      61           0 :   phdr->p_vaddr  = text_file_off;
      62           0 :   phdr->p_paddr  = text_file_off;
      63           0 :   phdr->p_filesz = segment_sz;
      64           0 :   phdr->p_memsz  = segment_sz;
      65           0 :   phdr->p_align  = 8;
      66             : 
      67             :   /* Section 1: .text (sh_addr must equal sh_offset for the loader) */
      68           0 :   fd_elf64_shdr * shdr_text = (fd_elf64_shdr *)(buf + shdr_file_off + sizeof(fd_elf64_shdr));
      69           0 :   shdr_text->sh_name      = SHSTRTAB_OFF_TEXT;
      70           0 :   shdr_text->sh_type      = FD_ELF_SHT_PROGBITS;
      71           0 :   shdr_text->sh_flags     = 0x6;
      72           0 :   shdr_text->sh_addr      = text_file_off;
      73           0 :   shdr_text->sh_offset    = text_file_off;
      74           0 :   shdr_text->sh_size      = text_sz;
      75           0 :   shdr_text->sh_addralign = 8;
      76             : 
      77             :   /* Section 2: .rodata */
      78           0 :   fd_elf64_shdr * shdr_ro = (fd_elf64_shdr *)(buf + shdr_file_off + 2UL * sizeof(fd_elf64_shdr));
      79           0 :   shdr_ro->sh_name      = SHSTRTAB_OFF_RODATA;
      80           0 :   shdr_ro->sh_type      = FD_ELF_SHT_PROGBITS;
      81           0 :   shdr_ro->sh_flags     = 0x2;
      82           0 :   shdr_ro->sh_addr      = rodata_file_off;
      83           0 :   shdr_ro->sh_offset    = rodata_file_off;
      84           0 :   shdr_ro->sh_size      = rodata_sz;
      85           0 :   shdr_ro->sh_addralign = 1;
      86             : 
      87             :   /* Section 3: .shstrtab */
      88           0 :   fd_elf64_shdr * shdr_strtab = (fd_elf64_shdr *)(buf + shdr_file_off + 3UL * sizeof(fd_elf64_shdr));
      89           0 :   shdr_strtab->sh_name      = SHSTRTAB_OFF_SHSTR;
      90           0 :   shdr_strtab->sh_type      = FD_ELF_SHT_STRTAB;
      91           0 :   shdr_strtab->sh_offset    = shstrtab_file_off;
      92           0 :   shdr_strtab->sh_size      = sizeof(shstrtab);
      93           0 :   shdr_strtab->sh_addralign = 1;
      94             : 
      95           0 :   memcpy( buf + text_file_off, text_data, text_sz );
      96           0 :   if( rodata_sz ) memcpy( buf + rodata_file_off, rodata, rodata_sz );
      97           0 :   memcpy( buf + shstrtab_file_off, shstrtab, sizeof(shstrtab) );
      98           0 : }

Generated by: LCOV version 1.14