| 391 | } |
| 392 | |
| 393 | bool |
| 394 | getSectionInfo(const std::string& filename, const std::string& section_name, SectionInfo* result) |
| 395 | { |
| 396 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 397 | LOG(ERROR) << "libelf library ELF version too old"; |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | LOG(DEBUG) << "Trying to locate .PyRuntime data offset from program headers"; |
| 402 | file_unique_ptr file(fopen(filename.c_str(), "r"), fclose); |
| 403 | if (!file || fileno(file.get()) == -1) { |
| 404 | LOG(ERROR) << "Cannot open ELF file " << filename << " (" << std::strerror(errno) << ")"; |
| 405 | return false; |
| 406 | } |
| 407 | const int fd = fileno(file.get()); |
| 408 | |
| 409 | elf_unique_ptr elf = elf_unique_ptr(elf_begin(fd, ELF_C_READ_MMAP, nullptr), elf_end); |
| 410 | if (!elf) { |
| 411 | LOG(ERROR) << "Cannot read ELF file " << filename; |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | Elf* the_elf = elf.get(); |
| 416 | |
| 417 | size_t shnum; |
| 418 | size_t nphdr; |
| 419 | if (elf_getphdrnum(the_elf, &nphdr) != 0) { |
| 420 | LOG(ERROR) << "Failed to get program headers"; |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | Dwarf_Addr load_point = 0; |
| 425 | for (size_t i = 0; i < nphdr; i++) { |
| 426 | GElf_Phdr phdr; |
| 427 | if (gelf_getphdr(the_elf, i, &phdr) != &phdr) { |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | if (phdr.p_type != PT_LOAD) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | load_point = phdr.p_vaddr - phdr.p_vaddr % phdr.p_align; |
| 436 | LOG(DEBUG) << "Found load point of main Python " << filename << " at " << std::hex |
| 437 | << std::showbase << load_point; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | if (elf_getshdrnum(the_elf, &shnum) < 0) { |
| 442 | LOG(ERROR) << "Cannot determine the number of sections in the ELF file"; |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | size_t shstrndx; |
| 447 | if (elf_getshdrstrndx(the_elf, &shstrndx) < 0) { |
| 448 | LOG(ERROR) << "Cannot get the section string table"; |
| 449 | return false; |
| 450 | } |
no test coverage detected