[llvm-branch-commits] [llvm] [llvm-readobj, ELF] Support reading binary with more than PN_XNUM segments. (PR #165278)
James Henderson via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Mar 4 02:41:37 PST 2026
================
@@ -0,0 +1,49 @@
+## When an ELF object has PN_XNUM (0xffff) or more sections, its e_phnum field
+## should be PN_XNUM and sh_info of the section header at index 0 is used
+## to store the value.
+
+# RUN: yaml2obj --docnum=1 %s -o %t1
+# RUN: llvm-readelf --file-headers %t1 | FileCheck %s --check-prefix=VALID-GNU
+# RUN: llvm-readobj --file-headers %t1 | FileCheck %s --check-prefix=VALID-LLVM
+
+## The ELF header should have e_phnum == PN_XNUM
+# VALID-LLVM: ProgramHeaderCount: 65535 (2)
+# VALID-GNU: Number of program headers: 65535 (2)
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ EPhNum: 65535
+Sections:
+ - Type: SHT_NULL
+ Info: 2
+ProgramHeaders:
+ - Type: PT_LOAD
+ - Type: PT_LOAD
+
+## Check the detection and reporting of warnings for malformed program header (in this case the program header does not exist).
+## TODO: Consolidate error reporting; INVALID-LLVM-DUPLICATE and INVALID-GNU-DUPLICATE are currently extraneous.
+
+# RUN: yaml2obj --docnum=2 %s -o %t2
+# RUN: llvm-readobj --file-headers --program-headers %t2 2>&1 | FileCheck %s --implicit-check-not=warning: -DFILE=%t2 --check-prefix=INVALID-LLVM,INVALID-LLVM-DUPLICATE
+# RUN: llvm-readelf --file-headers --program-headers %t2 2>&1 | FileCheck %s --implicit-check-not=warning: -DFILE=%t2 --check-prefix=INVALID-GNU,INVALID-GNU-DUPLICATE
+
+# INVALID-LLVM: warning: '[[FILE]]': unable to read program headers to locate the PT_DYNAMIC segment: invalid e_phentsize: 0
----------------
jh7370 wrote:
I don't think we should land this PR with these warnings. They are completely unrelated and are an artifact of yaml2obj behaviour: I took a quick look at the code and it appears by design that if there are no program header entries in the YAML, then e_phentsize is set to 0 instead of the program header size. Given this, let's reinstate one program header entry in the YAML and add a comment explaining it is there to ensure we have a non-zero e_phentsize.
What we should expect are warnings about the program headers not fitting in the ELF or something to that effect. I'd also like something showing what, if anything we do attempt to dump from the --program-headers option, potentially leveraging CHECK-NOT or similar to show we don't produce some program header output.
https://github.com/llvm/llvm-project/pull/165278
More information about the llvm-branch-commits
mailing list