#ifndef ELF_H #define ELF_H /* * ELF handling * Valentin HAUDIQUET * Sources are : * - https://refspecs.linuxfoundation.org/elf/elf.pdf (ELF Reference) * - https://wiki.osdev.org/ELF (OSDev) */ #include #include #include typedef struct ELF_HEADER_32 { uint8_t ELF[4]; uint8_t bits; // 1 = 32bits, 2 = 64bits uint8_t endianness; // 1 = little, 2 = big uint8_t header_version; uint8_t abi; uint8_t padding0[8]; uint16_t exec_type; // 1 = relocatable, 2 = executable, 3 = shared, 4 = core uint16_t instruction_set; uint32_t elf_version; uint32_t entry_32; uint32_t program_header_table_32; uint32_t section_table_32; uint32_t flags; uint16_t header_size; uint16_t program_entry_size; uint16_t program_entry_amount; uint16_t section_entry_size; uint16_t section_entry_amount; uint16_t section_str_index; // Index of string table associated with section names } __attribute__((packed)) elf_header_32_t; #define ELF_LITTLE_ENDIAN 1 #define ELF_BIG_ENDIAN 2 #define BITS_32 1 #define BITS_64 2 #define INSTRUCTION_SET_X86 0x3 #define INSTRUCTION_SET_X86_64 0x3E #define INSTRUCTION_SET_RISCV 0xF3 typedef struct ELF_SECTION_HEADER_32 { uint16_t section_name; uint16_t section_type; uint16_t section_flags; uint32_t section_addr_32; uint32_t section_offset_32; uint16_t section_size; uint16_t section_link; uint16_t section_info; uint16_t section_addralign; uint16_t section_entrysize; } __attribute__((packed)) elf_section_header_32_t; typedef struct ELF_PROGRAM_HEADER_32 { uint32_t segment_type; uint32_t segment_offset; uint32_t virtual_address; uint32_t undefined; uint32_t segment_file_size; uint32_t segment_memory_size; uint32_t flags; uint32_t align; } __attribute__((packed)) elf_program_header_32_t; #define SEGMENT_TYPE_NULL 0 #define SEGMENT_TYPE_LOAD 1 #define SEGMENT_TYPE_DYNAMIC 2 #define SEGMENT_TYPE_INTERP 3 #define SEGMENT_TYPE_NOTE 4 #define SEGMENT_TYPE_RISCV_SPECIFIC_SHT_RISCV_ATTRIBUTES 0x70000003 uint32_t elf_32_load(void* file); #endif