[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
    
    
  
================
@@ -891,6 +925,49 @@ Expected<uint64_t> ELFFile<ELFT>::getDynSymtabSize() const {
 
 template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
 
+template <class ELFT> Error ELFFile<ELFT>::readShdrZero() {
+  const Elf_Ehdr &Header = getHeader();
+
+  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();
+    if (!SecsOrErr) {
+      RealShNum = std::nullopt;
+      return SecsOrErr.takeError();
+    }
+
+    // We can really have 0 number of seciton
+    if ((*SecsOrErr).size() == 0) {
+      if (Header.e_phnum == ELF::PN_XNUM ||
+          Header.e_shstrndx == ELF::SHN_XINDEX) {
+        return createError("Unable to find Section 0");
----------------
jh7370 wrote:
I'm pretty sure it's a genuine failure, i.e. you've done something in this PR that is incorrect. For context, the failing test case is:
```
## Check the case when e_shstrndx == SHN_XINDEX, but null section's sh_link contains
## the index of a section header string table that is larger than the number of the sections.
# RUN: yaml2obj --docnum=30 %s -o %t30
# RUN: not llvm-objcopy %t30 2>&1 | FileCheck %s -DFILE=%t30 --check-prefix=INVALID-SHSTRTAB-INDEX
# INVALID-SHSTRTAB-INDEX: error: section header string table index 255 does not exist
--- !ELF
FileHeader:
  Class:     ELFCLASS64
  Data:      ELFDATA2LSB
  Type:      ET_REL
  Machine:   EM_X86_64
## SHN_XINDEX == 0xffff.
  EShStrNdx: 0xffff
Sections:
  - Type: SHT_NULL
    Link: 0xff
```
Section header 0 clearly should be readable in this case, but the test is failing with that unreadable section error you've introduced. The failure would come later on, when attempting to use the section header string table index - see https://github.com/llvm/llvm-project/blob/ec26f219acce77fb9b3d52abd31b0e639e788514/llvm/include/llvm/Object/ELF.h#L794. (Aside: `getSectionStringTable` and similar functions should be modified in this PR to reference `Real*` and call this method if the values are unitialised and not reference e_shnum directly).
The obj2yaml test failure also looks genuine, probably for the same reason.
https://github.com/llvm/llvm-project/pull/162288
    
    
More information about the llvm-commits
mailing list