Get the headers from the buffer. * Return -1 in the event of an error. * The section headers are optional; if NULL * is passed in for pshdr they won't be parsed. * We don't (yet) make payload parsing optional * because we've never seen a use case. */
| 579 | * because we've never seen a use case. |
| 580 | */ |
| 581 | int |
| 582 | elf_headers(const struct buffer *pinput, |
| 583 | Elf64_Ehdr *ehdr, |
| 584 | Elf64_Phdr **pphdr, |
| 585 | Elf64_Shdr **pshdr) |
| 586 | { |
| 587 | struct parsed_elf pelf; |
| 588 | int flags; |
| 589 | |
| 590 | flags = ELF_PARSE_PHDR; |
| 591 | |
| 592 | if (pshdr != NULL) |
| 593 | flags |= ELF_PARSE_SHDR; |
| 594 | |
| 595 | if (parse_elf(pinput, &pelf, flags)) |
| 596 | return -1; |
| 597 | |
| 598 | /* Copy out the parsed elf header. */ |
| 599 | memcpy(ehdr, &pelf.ehdr, sizeof(*ehdr)); |
| 600 | |
| 601 | *pphdr = calloc(ehdr->e_phnum, sizeof(Elf64_Phdr)); |
| 602 | memcpy(*pphdr, pelf.phdr, ehdr->e_phnum * sizeof(Elf64_Phdr)); |
| 603 | |
| 604 | if (pshdr != NULL) { |
| 605 | *pshdr = calloc(ehdr->e_shnum, sizeof(Elf64_Shdr)); |
| 606 | memcpy(*pshdr, pelf.shdr, ehdr->e_shnum * sizeof(Elf64_Shdr)); |
| 607 | } |
| 608 | |
| 609 | parsed_elf_destroy(&pelf); |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* ELF Writing Support |
| 615 | * |
no test coverage detected