[llvm] [Object,ELF] Implement PN_XNUM extension for program headers (PR #162288)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 21 01:54:44 PDT 2025
================
@@ -887,30 +923,49 @@ Expected<uint64_t> ELFFile<ELFT>::getDynSymtabSize() const {
return 0;
}
-template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {
+template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
+
+template <class ELFT> Error ELFFile<ELFT>::readShdrZero() {
const Elf_Ehdr &Header = getHeader();
- RealPhNum = Header.e_phnum;
- RealShNum = Header.e_shnum;
- RealShStrNdx = Header.e_shstrndx;
- if (!Header.hasPhdrNumExtension())
- return;
- // An ELF binary may report `hasExtendedHeader` as true but not actually
- // include an extended header. For example, a core dump can contain 65,535
- // segments but no sections at all. We defer reporting an error until section
- // 0 is accessed. Consumers should handle and emit the error themselves when
- // they attempt to access it.
- auto SecOrErr = getSection(0);
- if (!SecOrErr) {
- consumeError(SecOrErr.takeError());
- return;
+ if ((Header.e_phnum == ELF::PN_XNUM || Header.e_shnum == 0 ||
+ Header.e_shstrndx == ELF::SHN_XINDEX) &&
+ Header.e_shoff != 0) {
+
+ // Pretend we have section 0 or sections() would call getShNum and thus
+ // become an infinite recursion
+ RealShNum = 0;
----------------
aokblast wrote:
if we actually have sh_num == 0, and both phnum and shstrndx are valid. It should not be considered as an Error. Right? Since the search of shdr 0 is caused by shnum (not phnum or shstrndx) but we actually have 0 sections. For this case, we should not regard it as an error.
https://github.com/llvm/llvm-project/pull/162288
More information about the llvm-commits
mailing list