diff options
author | Giuliano Procida <gprocida@google.com> | 2023-10-12 16:39:42 +0100 |
---|---|---|
committer | Giuliano Procida <gprocida@google.com> | 2023-10-31 17:08:09 +0000 |
commit | 1a5b73df4f60abccf05aeb357eca0f33e742c55a (patch) | |
tree | f90a3a54ce635548d3a5e22eace15e5304ae72b7 | |
parent | 8e1dc98501cde5cb588b98714d5be0c76d0f072c (diff) | |
download | stg-1a5b73df4f60abccf05aeb357eca0f33e742c55a.tar.gz |
ELF loader: use only symbol table if unique
Some firmware images may present as EXEC but only have a static symbol
table.
Drop the now unused `GetSectionByType` helper function.
PiperOrigin-RevId: 572916484
Change-Id: I28ab0881ee536a7e1ddec6cb8e7126ee6e422e7c
-rw-r--r-- | elf_loader.cc | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/elf_loader.cc b/elf_loader.cc index 5b9a864..6575b17 100644 --- a/elf_loader.cc +++ b/elf_loader.cc @@ -218,12 +218,6 @@ Elf_Scn* MaybeGetSectionByType(Elf* elf, Elf64_Word type) { return sections[0]; } -Elf_Scn* GetSectionByType(Elf* elf, Elf64_Word type) { - Elf_Scn* section = MaybeGetSectionByType(elf, type); - Check(section != nullptr) << "no section found with type " << type; - return section; -} - Elf_Scn* GetSectionByIndex(Elf* elf, size_t index) { Elf_Scn* section = elf_getscn(elf, index); Check(section != nullptr) << "no section found with index " << index; @@ -270,17 +264,28 @@ Elf_Scn* GetSymbolTableSection(Elf* elf, bool is_linux_kernel_binary, std::cout << "ELF type: " << ElfHeaderTypeToString(elf_header.e_type) << '\n'; } - // Relocatable ELF binaries, Linux kernel and modules have their exported - // symbols in .symtab, all other ELF types have their exported symbols in - // .dynsym. - if (elf_header.e_type == ET_REL || is_linux_kernel_binary) { - return GetSectionByType(elf, SHT_SYMTAB); - } - if (elf_header.e_type == ET_DYN || elf_header.e_type == ET_EXEC) { - return GetSectionByType(elf, SHT_DYNSYM); + + Elf_Scn* symtab = MaybeGetSectionByType(elf, SHT_SYMTAB); + Elf_Scn* dynsym = MaybeGetSectionByType(elf, SHT_DYNSYM); + if (symtab != nullptr && dynsym != nullptr) { + // Relocatable ELF binaries, Linux kernel and modules have their + // exported symbols in .symtab, all other ELF types have their + // exported symbols in .dynsym. + if (elf_header.e_type == ET_REL || is_linux_kernel_binary) { + return symtab; + } + if (elf_header.e_type == ET_DYN || elf_header.e_type == ET_EXEC) { + return dynsym; + } + Die() << "unsupported ELF type: '" + << ElfHeaderTypeToString(elf_header.e_type) << "'"; + } else if (symtab != nullptr) { + return symtab; + } else if (dynsym != nullptr) { + return dynsym; + } else { + Die() << "no ELF symbol table found"; } - Die() << "unsupported ELF type: '" << ElfHeaderTypeToString(elf_header.e_type) - << "'"; } |