|
| 1 | + |
| 2 | +#include <fcntl.h> |
| 3 | +#include <libelf.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <sys/types.h> |
| 6 | + |
| 7 | +#include <udb/elf_reader.hpp> |
| 8 | +#include <udb/memory.hpp> |
| 9 | + |
| 10 | +udb::ElfReader::ElfReader(const std::string& path) { |
| 11 | + if (elf_version(EV_CURRENT) == EV_NONE) { |
| 12 | + throw ElfException("Bad Elf version"); |
| 13 | + } |
| 14 | + |
| 15 | + m_fd = open(path.c_str(), O_RDONLY, 0); |
| 16 | + if (m_fd < 0) { |
| 17 | + throw ElfException("Could not open ELF file"); |
| 18 | + } |
| 19 | + |
| 20 | + m_elf = elf_begin(m_fd, ELF_C_READ, NULL); |
| 21 | + if (m_elf == nullptr) { |
| 22 | + throw ElfException("Could not begin reading ELF"); |
| 23 | + } |
| 24 | + |
| 25 | + if (elf_kind(m_elf) != ELF_K_ELF) { |
| 26 | + throw ElfException("Not an ELF file"); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +udb::ElfReader::~ElfReader() { |
| 31 | + if (m_elf != nullptr) { |
| 32 | + elf_end(m_elf); |
| 33 | + close(m_fd); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +bool udb::ElfReader::getSym(const std::string& name, Elf64_Addr* result) { |
| 38 | + size_t num_sections; |
| 39 | + if (elf_getshdrnum(m_elf, &num_sections) != 0) { |
| 40 | + throw ElfException("Could not determine number of sections"); |
| 41 | + } |
| 42 | + size_t shstrtab_index; |
| 43 | + if (elf_getshdrstrndx(m_elf, &shstrtab_index) != 0) { |
| 44 | + throw ElfException("Could not get Section Header String Table"); |
| 45 | + } |
| 46 | + // first, find the strtab |
| 47 | + int strtab_index; |
| 48 | + for (size_t i = 0; i < num_sections; i++) { |
| 49 | + auto* strtab_section = elf_getscn(m_elf, i); |
| 50 | + Elf64_Shdr* header = elf64_getshdr(strtab_section); |
| 51 | + if (strcmp(elf_strptr(m_elf, shstrtab_index, header->sh_name), ".strtab") == |
| 52 | + 0) { |
| 53 | + strtab_index = i; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + // now, get the symtab |
| 58 | + for (size_t i = 0; i < num_sections; i++) { |
| 59 | + Elf_Scn* section; |
| 60 | + section = elf_getscn(m_elf, i); |
| 61 | + Elf64_Shdr* section_header = elf64_getshdr(section); |
| 62 | + if (strcmp(elf_strptr(m_elf, shstrtab_index, section_header->sh_name), |
| 63 | + ".symtab") == 0) { |
| 64 | + unsigned num_syms = section_header->sh_size / section_header->sh_entsize; |
| 65 | + Elf64_Sym* symtab; |
| 66 | + Elf_Data* data; |
| 67 | + if ((data = elf_getdata(section, nullptr)) == nullptr) { |
| 68 | + throw ElfException(fmt::format("Could not get symtab data. {}", |
| 69 | + elf_errmsg(elf_errno())) |
| 70 | + .c_str()); |
| 71 | + } |
| 72 | + symtab = (Elf64_Sym*)data->d_buf; |
| 73 | + for (unsigned j = 0; j < num_syms; j++) { |
| 74 | + if (strcmp(elf_strptr(m_elf, strtab_index, symtab[j].st_name), |
| 75 | + name.c_str()) == 0) { |
| 76 | + *result = symtab[j].st_value; |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return false; |
| 83 | +} |
| 84 | + |
| 85 | +// returns start address |
| 86 | +uint64_t udb::ElfReader::loadLoadableSegments(Memory& m) { |
| 87 | + Elf64_Phdr* phdr; |
| 88 | + size_t n; |
| 89 | + |
| 90 | + if (elf_getphdrnum(m_elf, &n) != 0) { |
| 91 | + throw ElfException("Could not find number of Program Headers"); |
| 92 | + } |
| 93 | + |
| 94 | + phdr = elf64_getphdr(m_elf); |
| 95 | + for (size_t i = 0; i < n; i++) { |
| 96 | + if (phdr[i].p_type == PT_LOAD) { |
| 97 | + Elf_Data* d = elf_getdata_rawchunk(m_elf, phdr[i].p_offset, |
| 98 | + phdr[i].p_filesz, ELF_T_BYTE); |
| 99 | + m.memcpy_from_host(phdr[i].p_vaddr, d->d_buf, d->d_size); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return elf64_getehdr(m_elf)->e_entry; |
| 104 | +} |
0 commit comments