[llvm] [Object,ELF] Implement PN_XNUM extension for program headers (PR #162288)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 21 00:53:47 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;
+ auto SecsOrErr = sections();
----------------
jh7370 wrote:
With the above suggested points, you should be able to call `getSection(0)` here.
https://github.com/llvm/llvm-project/pull/162288
More information about the llvm-commits
mailing list