| 484 | } |
| 485 | |
| 486 | int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags) |
| 487 | { |
| 488 | struct xdr *xdr = &xdr_le; |
| 489 | int bit64 = 0; |
| 490 | struct buffer input; |
| 491 | Elf64_Ehdr *ehdr; |
| 492 | |
| 493 | /* Zero out the parsed elf structure. */ |
| 494 | memset(pelf, 0, sizeof(*pelf)); |
| 495 | |
| 496 | if (buffer_size(pinput) < sizeof(*ehdr)) { |
| 497 | DEBUG("The stage file is too short!\n"); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | if (!iself(buffer_get(pinput))) { |
| 502 | DEBUG("The stage file is not in ELF format!\n"); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | buffer_clone(&input, pinput); |
| 507 | ehdr = &pelf->ehdr; |
| 508 | elf_eident(&input, ehdr); |
| 509 | bit64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64; |
| 510 | /* Assume LE unless we are sure otherwise. |
| 511 | * We're not going to take on the task of |
| 512 | * fully validating the ELF file. That way |
| 513 | * lies madness. |
| 514 | */ |
| 515 | if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
| 516 | xdr = &xdr_be; |
| 517 | |
| 518 | elf_ehdr(&input, ehdr, xdr, bit64); |
| 519 | |
| 520 | /* Relocation processing requires section header parsing. */ |
| 521 | if (flags & ELF_PARSE_RELOC) |
| 522 | flags |= ELF_PARSE_SHDR; |
| 523 | |
| 524 | /* String table processing requires section header parsing. */ |
| 525 | if (flags & ELF_PARSE_STRTAB) |
| 526 | flags |= ELF_PARSE_SHDR; |
| 527 | |
| 528 | /* Symbole table processing requires section header parsing. */ |
| 529 | if (flags & ELF_PARSE_SYMTAB) |
| 530 | flags |= ELF_PARSE_SHDR; |
| 531 | |
| 532 | if ((flags & ELF_PARSE_PHDR) && phdr_read(pinput, pelf, xdr, bit64)) |
| 533 | goto fail; |
| 534 | |
| 535 | if ((flags & ELF_PARSE_SHDR) && shdr_read(pinput, pelf, xdr, bit64)) |
| 536 | goto fail; |
| 537 | |
| 538 | if ((flags & ELF_PARSE_RELOC) && reloc_read(pinput, pelf, xdr, bit64)) |
| 539 | goto fail; |
| 540 | |
| 541 | if ((flags & ELF_PARSE_STRTAB) && strtab_read(pinput, pelf)) |
| 542 | goto fail; |
| 543 |
no test coverage detected