| 92 | } |
| 93 | |
| 94 | ssize_t elf_binary_size(const char* filename) { |
| 95 | std::ifstream ifs(filename); |
| 96 | |
| 97 | if (!ifs) { |
| 98 | log_error("could not open file\n"); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | // for the beginning, we just need to read e_ident to determine ELF class (i.e., either 32-bit or 64-bit) |
| 103 | // that way, we can decide which way to go |
| 104 | // the easiest way is to just use the ELF API |
| 105 | Elf64_Ehdr ehdr; |
| 106 | |
| 107 | ifs.read(reinterpret_cast<char*>(&ehdr.e_ident), EI_NIDENT); |
| 108 | |
| 109 | if (!ifs) { |
| 110 | log_error("failed to read e_ident from ELF file\n"); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | switch (ehdr.e_ident[EI_CLASS]) { |
| 115 | case ELFCLASS32: { |
| 116 | return get_elf_size<Elf32_Ehdr, Elf32_Shdr>(ifs); |
| 117 | } |
| 118 | case ELFCLASS64: { |
| 119 | return get_elf_size<Elf64_Ehdr, Elf64_Shdr>(ifs); |
| 120 | } |
| 121 | default: { |
| 122 | log_error("unknown ELF class\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | } |
| 126 | } |