[llvm-branch-commits] [llvm] [llvm-readobj, ELF] Support reading binary with more than PN_XNUM segments. (PR #165278)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Feb 27 08:12:06 PST 2026


================
@@ -4853,10 +4875,16 @@ template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
   const Elf_Ehdr &Header = this->Obj.getHeader();
   Field Fields[8] = {2,         17,        26,        37 + Bias,
                      48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
+  uint32_t PhNum = 0;
+  if (Expected<uint32_t> PhNumOrErr = this->Obj.getPhNum(); PhNumOrErr)
+    PhNum = *PhNumOrErr;
+  else
+    this->reportUniqueWarning(PhNumOrErr.takeError());
----------------
aokblast wrote:

Sorry that I missed it.

This is the output from gnu:

```bash
# x86_64-unknown-freebsd16.0-readelf --program-headers /home/blast/Gits/llvm-project/build/test/tools/llvm-readobj/ELF/Output/file-headers.test.tmp.invalid3

Elf file type is REL (Relocatable file)
Entry point 0x0
There are 65535 program headers, starting at offset 0
readelf: Error: Too many program headers - 0xffff - the file is not that big
```

And this is the output from us:

```bash
# /home/blast/Gits/llvm-project/build/bin/llvm-readelf --program-headers /home/blast/Gits/llvm-project/build/test/tools/llvm-readobj/ELF/Output/file-headers.test.tmp.invalid3


/home/blast/Gits/llvm-project/build/bin/llvm-readelf: warning: '/home/blast/Gits/llvm-project/build/test/tools/llvm-readobj/ELF/Output/file-headers.test.tmp.invalid3': unable to read program headers to locate the PT_DYNAMIC segment: invalid section index: 0
/home/blast/Gits/llvm-project/build/bin/llvm-readelf: warning: '/home/blast/Gits/llvm-project/build/test/tools/llvm-readobj/ELF/Output/file-headers.test.tmp.invalid3': invalid section index: 0

 Section to Segment mapping:
  Segment Sections...
/home/blast/Gits/llvm-project/build/bin/llvm-readelf: warning: '/home/blast/Gits/llvm-project/build/test/tools/llvm-readobj/ELF/Output/file-headers.test.tmp.invalid3': can't read program headers to build section to segment mapping: invalid section index: 0
```

This function is called by the following code:

```c++
    Expected<uint32_t> PhNumOrErr = this->Obj.getPhNum();
    if (!PhNumOrErr) {
      this->reportUniqueWarning(PhNumOrErr.takeError());
    } else if (*PhNumOrErr == 0) {
      OS << "\nThere are no program headers in this file.\n";
    } else {
      printProgramHeaders();
    }
```

Which means it is impossible to reach here since the caller have already check that.

https://github.com/llvm/llvm-project/pull/165278


More information about the llvm-branch-commits mailing list