[llvm] 7ef1763 - [llvm-readobj] - Stop using unwrapOrError() for all program_headers() calls.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 04:17:26 PDT 2020


Author: Georgii Rymar
Date: 2020-07-14T14:16:57+03:00
New Revision: 7ef17638d53bf477612bbb1a615bfb41a07f94fa

URL: https://github.com/llvm/llvm-project/commit/7ef17638d53bf477612bbb1a615bfb41a07f94fa
DIFF: https://github.com/llvm/llvm-project/commit/7ef17638d53bf477612bbb1a615bfb41a07f94fa.diff

LOG: [llvm-readobj] - Stop using unwrapOrError() for all program_headers() calls.

program_headers() returns the list of program headers. This change allows
to continue attempt of dumping when something is wrong with program headers.

Differential revision: https://reviews.llvm.org/D83554

Added: 
    

Modified: 
    llvm/test/Object/invalid.test
    llvm/test/tools/llvm-readobj/ELF/dynamic-tags.test
    llvm/test/tools/llvm-readobj/ELF/gnu-notes.test
    llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test
    llvm/test/tools/llvm-readobj/ELF/gnu-section-mapping.test
    llvm/test/tools/llvm-readobj/ELF/program-headers.test
    llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test
index 3a7ddab8d043..475143102872 100644
--- a/llvm/test/Object/invalid.test
+++ b/llvm/test/Object/invalid.test
@@ -228,12 +228,12 @@ Sections:
     Link: .symtab
 Symbols: []
 
-## Check that llvm-readobj reports an error if the e_phentsize field is broken.
+## Check that llvm-readobj reports a warning when the e_phentsize field is broken.
 
-# RUN: not llvm-readobj --program-headers %p/Inputs/invalid-e_shnum.elf 2>&1 | \
+# RUN: llvm-readobj --program-headers %p/Inputs/invalid-e_shnum.elf 2>&1 | \
 # RUN:  FileCheck -DFILE=%p/Inputs/invalid-e_shnum.elf --check-prefix=INVALID-PH-ENTSIZE %s
 
-# INVALID-PH-ENTSIZE: error: '[[FILE]]': invalid e_phentsize: 12336
+# INVALID-PH-ENTSIZE: warning: '[[FILE]]': unable to dump program headers: invalid e_phentsize: 12336
 
 ## Check that llvm-readobj reports a warning when we have no SHT_SYMTAB_SHNDX section,
 ## but have a symbol referencing it.
@@ -409,10 +409,10 @@ DynamicSymbols:
 ## ELF header contains e_phentsize field with a value != sizeof(Elf_Phdr).
 ## Check llvm-readobj reports it.
 
-# RUN: not llvm-readobj -l %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 \
+# RUN: llvm-readobj -l %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 \
 # RUN:   | FileCheck -DFILE=%p/Inputs/corrupt-invalid-phentsize.elf.x86-64 --check-prefix=PHENTSIZE %s
 
-# PHENTSIZE: error: '[[FILE]]': invalid e_phentsize: 57
+# PHENTSIZE: warning: '[[FILE]]': unable to read program headers to locate the PT_DYNAMIC segment: invalid e_phentsize: 57
 
 ## The dynamic table contains DT_STRTAB with a value that is not in any loadable segment.
 ## Check llvm-readobj reports it.

diff  --git a/llvm/test/tools/llvm-readobj/ELF/dynamic-tags.test b/llvm/test/tools/llvm-readobj/ELF/dynamic-tags.test
index c7bd551eb754..5f8b709cef5b 100644
--- a/llvm/test/tools/llvm-readobj/ELF/dynamic-tags.test
+++ b/llvm/test/tools/llvm-readobj/ELF/dynamic-tags.test
@@ -140,10 +140,11 @@
 
 --- !ELF
 FileHeader:
-  Class:   ELFCLASS64
-  Data:    ELFDATA2LSB
-  Type:    ET_EXEC
-  Machine: EM_X86_64
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_EXEC
+  Machine:    EM_X86_64
+  EPhEntSize: [[PHENTSIZE=56]]
 Sections:
   - Name:    .dynstr
     Type:    SHT_STRTAB
@@ -637,3 +638,154 @@ Sections:
        Value: 0x1
      - Tag:   DT_NULL
        Value: 0x0
+
+## Check how we dump dynamic tags when we are unable to read program headers.
+# RUN: yaml2obj --docnum=1 -DPHENTSIZE=1 %s -o %t-phentsize-err
+# RUN: llvm-readobj --dynamic-table %t-phentsize-err 2>&1 | \
+# RUN:   FileCheck %s --implicit-check-not=warning: -DFILE=%t-phentsize-err \
+# RUN:     --check-prefixes=PHENTSIZE-WARN,PHENTSIZE-LLVM
+# RUN: llvm-readelf --dynamic-table %t-phentsize-err 2>&1 | \
+# RUN:   FileCheck %s --implicit-check-not=warning: -DFILE=%t-phentsize-err \
+# RUN:     --check-prefixes=PHENTSIZE-WARN,PHENTSIZE-GNU
+
+# PHENTSIZE-WARN:      warning: '[[FILE]]': unable to read program headers to locate the PT_DYNAMIC segment: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_HASH: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_STRTAB: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_SYMTAB: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_RELA: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': DT_SYMENT value of 0x987 is not the size of a symbol (0x18)
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_REL: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_JMPREL: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_RELR: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_ANDROID_RELR: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': Unable to parse DT_GNU_HASH: invalid e_phentsize: 1
+# PHENTSIZE-WARN-NEXT: warning: '[[FILE]]': string table was not found
+
+# PHENTSIZE-LLVM:      DynamicSection [ (61 entries)
+# PHENTSIZE-LLVM-NEXT:   Tag                Type                 Name/Value
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000001 NEEDED               Shared library: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000002 PLTRELSZ             16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000003 PLTGOT               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000004 HASH                 0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000005 STRTAB               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000006 SYMTAB               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000007 RELA                 0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000008 RELASZ               16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000009 RELAENT              1929 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000A STRSZ                16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000B SYMENT               2439 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000C INIT                 0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000D FINI                 0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000E SONAME               Library soname: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x000000000000000F RPATH                Library rpath: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000010 SYMBOLIC             0x1234567890ABCDEF
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000011 REL                  0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000012 RELSZ                16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000013 RELENT               291 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000014 PLTREL               RELA
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000015 DEBUG                0xFEDCBA0987654321
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000016 TEXTREL              0x1122334455667788
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000017 JMPREL               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000018 BIND_NOW             0x8877665544332211
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000019 INIT_ARRAY           0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000000000001A FINI_ARRAY           0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000000000001B INIT_ARRAYSZ         16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000000000001C FINI_ARRAYSZ         16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000000000001D RUNPATH              Library runpath: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x000000000000001E FLAGS                ORIGIN SYMBOLIC TEXTREL BIND_NOW STATIC_TLS
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000020 PREINIT_ARRAY        0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000021 PREINIT_ARRAYSZ      16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000022 SYMTAB_SHNDX         0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000023 RELRSZ               0x10
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000024 RELR                 0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000025 RELRENT              0x4321
+# PHENTSIZE-LLVM-NEXT:   0x000000006000000F ANDROID_REL          0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000060000010 ANDROID_RELSZ        16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x0000000060000011 ANDROID_RELA         0x1000
+# PHENTSIZE-LLVM-NEXT:   0x0000000060000012 ANDROID_RELASZ       16 (bytes)
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFE000 ANDROID_RELR         0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFE001 ANDROID_RELRSZ       0x10
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFE003 ANDROID_RELRENT      0x1234
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFEF5 GNU_HASH             0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFEF6 TLSDESC_PLT          0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFEF7 TLSDESC_GOT          0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFF9 RELACOUNT            0
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFA RELCOUNT             0
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFB FLAGS_1              NOW GLOBAL GROUP NODELETE LOADFLTR INITFIRST NOOPEN ORIGIN DIRECT TRANS INTERPOSE NODEFLIB NODUMP CONFALT ENDFILTEE DISPRELDNE DISPRELPND NODIRECT IGNMULDEF NOKSYMS NOHDR EDITED NORELOC SYMINTPOSE GLOBAUDIT SINGLETON PIE {{$}}
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFF0 VERSYM               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFC VERDEF               0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFD VERDEFNUM            0
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFE VERNEED              0x1000
+# PHENTSIZE-LLVM-NEXT:   0x000000006FFFFFFF VERNEEDNUM           0
+# PHENTSIZE-LLVM-NEXT:   0x000000007FFFFFFD AUXILIARY            Auxiliary library: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x000000007FFFFFFE USED                 Not needed object: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x000000007FFFFFFF FILTER               Filter library: [<?>]
+# PHENTSIZE-LLVM-NEXT:   0x0000000012345678 <unknown:>0x12345678 0x8765432187654321
+# PHENTSIZE-LLVM-NEXT:   0x000000006ABCDEF0 <unknown:>0x6abcdef0 0x9988776655443322
+# PHENTSIZE-LLVM-NEXT:   0x0000000076543210 <unknown:>0x76543210 0x5555666677778888
+# PHENTSIZE-LLVM-NEXT:   0x0000000000000000 NULL                 0x0
+# PHENTSIZE-LLVM-NEXT: ]
+
+# PHENTSIZE-GNU:      Dynamic section at offset 0xc0 contains 61 entries:
+# PHENTSIZE-GNU-NEXT:   Tag                Type                   Name/Value
+# PHENTSIZE-GNU-NEXT:   0x0000000000000001 (NEEDED)               Shared library: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x0000000000000002 (PLTRELSZ)             16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000000000003 (PLTGOT)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000004 (HASH)                 0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000005 (STRTAB)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000006 (SYMTAB)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000007 (RELA)                 0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000008 (RELASZ)               16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000000000009 (RELAENT)              1929 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000000000000a (STRSZ)                16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000000000000b (SYMENT)               2439 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000000000000c (INIT)                 0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000000000000d (FINI)                 0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000000000000e (SONAME)               Library soname: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x000000000000000f (RPATH)                Library rpath: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x0000000000000010 (SYMBOLIC)             0x1234567890abcdef
+# PHENTSIZE-GNU-NEXT:   0x0000000000000011 (REL)                  0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000012 (RELSZ)                16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000000000013 (RELENT)               291 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000000000014 (PLTREL)               RELA
+# PHENTSIZE-GNU-NEXT:   0x0000000000000015 (DEBUG)                0xfedcba0987654321
+# PHENTSIZE-GNU-NEXT:   0x0000000000000016 (TEXTREL)              0x1122334455667788
+# PHENTSIZE-GNU-NEXT:   0x0000000000000017 (JMPREL)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000018 (BIND_NOW)             0x8877665544332211
+# PHENTSIZE-GNU-NEXT:   0x0000000000000019 (INIT_ARRAY)           0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000000000001a (FINI_ARRAY)           0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000000000001b (INIT_ARRAYSZ)         16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000000000001c (FINI_ARRAYSZ)         16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000000000001d (RUNPATH)              Library runpath: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x000000000000001e (FLAGS)                ORIGIN SYMBOLIC TEXTREL BIND_NOW STATIC_TLS
+# PHENTSIZE-GNU-NEXT:   0x0000000000000020 (PREINIT_ARRAY)        0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000021 (PREINIT_ARRAYSZ)      16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000000000022 (SYMTAB_SHNDX)         0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000023 (RELRSZ)               0x10
+# PHENTSIZE-GNU-NEXT:   0x0000000000000024 (RELR)                 0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000000000025 (RELRENT)              0x4321
+# PHENTSIZE-GNU-NEXT:   0x000000006000000f (ANDROID_REL)          0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000060000010 (ANDROID_RELSZ)        16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x0000000060000011 (ANDROID_RELA)         0x1000
+# PHENTSIZE-GNU-NEXT:   0x0000000060000012 (ANDROID_RELASZ)       16 (bytes)
+# PHENTSIZE-GNU-NEXT:   0x000000006fffe000 (ANDROID_RELR)         0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006fffe001 (ANDROID_RELRSZ)       0x10
+# PHENTSIZE-GNU-NEXT:   0x000000006fffe003 (ANDROID_RELRENT)      0x1234
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffef5 (GNU_HASH)             0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffef6 (TLSDESC_PLT)          0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffef7 (TLSDESC_GOT)          0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffff9 (RELACOUNT)            0
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffffa (RELCOUNT)             0
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffffb (FLAGS_1)              NOW GLOBAL GROUP NODELETE LOADFLTR INITFIRST NOOPEN ORIGIN DIRECT TRANS INTERPOSE NODEFLIB NODUMP CONFALT ENDFILTEE DISPRELDNE DISPRELPND NODIRECT IGNMULDEF NOKSYMS NOHDR EDITED NORELOC SYMINTPOSE GLOBAUDIT SINGLETON PIE
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffff0 (VERSYM)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffffc (VERDEF)               0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffffd (VERDEFNUM)            0
+# PHENTSIZE-GNU-NEXT:   0x000000006ffffffe (VERNEED)              0x1000
+# PHENTSIZE-GNU-NEXT:   0x000000006fffffff (VERNEEDNUM)           0
+# PHENTSIZE-GNU-NEXT:   0x000000007ffffffd (AUXILIARY)            Auxiliary library: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x000000007ffffffe (USED)                 Not needed object: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x000000007fffffff (FILTER)               Filter library: [<?>]
+# PHENTSIZE-GNU-NEXT:   0x0000000012345678 (<unknown:>0x12345678) 0x8765432187654321
+# PHENTSIZE-GNU-NEXT:   0x000000006abcdef0 (<unknown:>0x6abcdef0) 0x9988776655443322
+# PHENTSIZE-GNU-NEXT:   0x0000000076543210 (<unknown:>0x76543210) 0x5555666677778888
+# PHENTSIZE-GNU-NEXT:   0x0000000000000000 (NULL)                 0x0

diff  --git a/llvm/test/tools/llvm-readobj/ELF/gnu-notes.test b/llvm/test/tools/llvm-readobj/ELF/gnu-notes.test
index 366d435e8e48..b1ea1981511f 100644
--- a/llvm/test/tools/llvm-readobj/ELF/gnu-notes.test
+++ b/llvm/test/tools/llvm-readobj/ELF/gnu-notes.test
@@ -83,10 +83,12 @@
 
 --- !ELF
 FileHeader:
-  Class:   ELFCLASS64
-  Data:    ELFDATA2LSB
-  Type:    ET_EXEC
-  Machine: EM_X86_64
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_EXEC
+  Machine:    EM_X86_64
+  EPhEntSize: [[PHENTSIZE=56]]
+  SHNum:      [[SHNUM=6]]
 Sections:
   - Name:         .note.ABI-tag
     Type:         SHT_NOTE
@@ -193,3 +195,16 @@ ProgramHeaders:
     FileSize: 0xffff0000
     Sections:
       - Section: .note
+
+## Check we report a warning when we are unable to locate the PT_NOTE
+## segment because of broken program headers.
+# RUN: yaml2obj --docnum=1 -DPHENTSIZE=1 -DSHNUM=0 %s -o %t6.so
+# RUN: llvm-readelf --notes %t6.so 2>&1 | FileCheck %s -DFILE=%t6.so --check-prefix=PHENTSIZE-WARN-GNU
+# RUN: llvm-readobj --notes %t6.so 2>&1 | FileCheck %s -DFILE=%t6.so --check-prefix=PHENTSIZE-WARN-LLVM
+
+# PHENTSIZE-WARN-GNU: warning: '[[FILE]]': unable to read program headers to locate the PT_DYNAMIC segment: invalid e_phentsize: 1
+# PHENTSIZE-WARN-GNU: warning: '[[FILE]]': unable to read program headers to locate the PT_NOTE segment: invalid e_phentsize: 1
+
+# PHENTSIZE-WARN-LLVM:      Notes [
+# PHENTSIZE-WARN-LLVM-NEXT: warning: '[[FILE]]': unable to read program headers to locate the PT_NOTE segment: invalid e_phentsize: 1
+# PHENTSIZE-WARN-LLVM-NEXT: ]

diff  --git a/llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test b/llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test
index cc3a38fb167b..d1d071f10ac6 100644
--- a/llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test
+++ b/llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test
@@ -354,3 +354,30 @@ ProgramHeaders:
 ## Case 5: an arbitrary large offset that goes past the end of the file.
   - Type:   PT_INTERP
     Offset: 0xAABBCCDDEEFF1122
+
+## Check we report a warning when we are unable to read program headers.
+# RUN: yaml2obj --docnum=3 %s -o %t.phdr.err
+# RUN: llvm-readelf --program-headers %t.phdr.err 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t.phdr.err --check-prefix=WARN-PHENTSIZE
+
+# WARN-PHENTSIZE:      Program Headers:
+# WARN-PHENTSIZE-NEXT:   Type Offset VirtAddr PhysAddr FileSiz  MemSiz Flg Align
+# WARN-PHENTSIZE-NEXT: warning: '[[FILE]]': unable to dump program headers: invalid e_phentsize: 1
+# WARN-PHENTSIZE:      Section to Segment mapping:
+# WARN-PHENTSIZE-NEXT:   Segment Sections...
+# WARN-PHENTSIZE-NEXT: warning: '[[FILE]]': can't read program headers to build section to segment mapping: invalid e_phentsize: 1
+
+--- !ELF
+FileHeader:
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_EXEC
+  Machine:    EM_X86_64
+  EPhEntSize: 1
+Sections:
+  - Name: .foo
+    Type: SHT_PROGBITS
+ProgramHeaders:
+  - Type: PT_PHDR
+    Sections:
+      - Section: .foo

diff  --git a/llvm/test/tools/llvm-readobj/ELF/gnu-section-mapping.test b/llvm/test/tools/llvm-readobj/ELF/gnu-section-mapping.test
index ddbfdd5abd93..e1c7181f1558 100644
--- a/llvm/test/tools/llvm-readobj/ELF/gnu-section-mapping.test
+++ b/llvm/test/tools/llvm-readobj/ELF/gnu-section-mapping.test
@@ -14,11 +14,11 @@
 
 --- !ELF
 FileHeader:
-  Class:   ELFCLASS64
-  Data:    ELFDATA2LSB
-  Type:    ET_EXEC
-  Machine: EM_X86_64
-  Entry:   0x12345678
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_EXEC
+  Machine:    EM_X86_64
+  EPhEntSize: [[PHENTSIZE=56]]
 Sections:
   - Name:    .foo.begin
     Type:    SHT_PROGBITS
@@ -74,3 +74,12 @@ ProgramHeaders:
 # NO-SECHDRS-NEXT:   00 {{$}}
 # NO-SECHDRS-NEXT:   01 {{$}}
 # NO-SECHDRS-NOT: {{.}}
+
+## Check we report a warning when we are unable to read program headers.
+# RUN: yaml2obj %s -DPHENTSIZE=1 -o %t64-err1.elf
+# RUN: llvm-readelf --section-mapping %t64-err1.elf 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t64-err1.elf --check-prefix=PHENTSIZE
+
+# PHENTSIZE:      Section to Segment mapping:
+# PHENTSIZE-NEXT:  Segment Sections...
+# PHENTSIZE-NEXT: warning: '[[FILE]]': can't read program headers to build section to segment mapping: invalid e_phentsize: 1

diff  --git a/llvm/test/tools/llvm-readobj/ELF/program-headers.test b/llvm/test/tools/llvm-readobj/ELF/program-headers.test
index 8ab5f65c02ce..04fd85b23c1b 100644
--- a/llvm/test/tools/llvm-readobj/ELF/program-headers.test
+++ b/llvm/test/tools/llvm-readobj/ELF/program-headers.test
@@ -168,3 +168,27 @@ ProgramHeaders:
   - Type: 0x65a3dbe6 ## PT_OPENBSD_RANDOMIZE
   - Type: 0x65a3dbe7 ## PT_OPENBSD_WXNEEDED
   - Type: 0x65a41be6 ## PT_OPENBSD_BOOTDATA
+
+## Check we report a warning when we are unable to read program headers.
+# RUN: yaml2obj --docnum=2 %s -o %t.err
+# RUN: llvm-readobj --program-headers %t.err 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t.err --check-prefix=WARN-PHENTSIZE
+
+# WARN-PHENTSIZE:      ProgramHeaders [
+# WARN-PHENTSIZE-NEXT: warning: '[[FILE]]': unable to dump program headers: invalid e_phentsize: 1
+# WARN-PHENTSIZE-NEXT: ]
+
+--- !ELF
+FileHeader:
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_EXEC
+  Machine:    EM_X86_64
+  EPhEntSize: 1
+Sections:
+  - Name: .foo
+    Type: SHT_PROGBITS
+ProgramHeaders:
+  - Type: PT_PHDR
+    Sections:
+      - Section: .foo

diff  --git a/llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml b/llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml
index f52e5731271f..a6860546bdec 100644
--- a/llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml
@@ -94,6 +94,6 @@ ProgramHeaders:
 
 ## Override the e_phentsize field.
 # RUN: yaml2obj --docnum=2 %s -DPHENTSIZE=1 -o %t8
-# RUN: not llvm-readelf --file-headers %t8 2>&1 | FileCheck %s --check-prefix=PHENTSIZE
+# RUN: llvm-readelf --file-headers %t8 | FileCheck %s --check-prefix=PHENTSIZE
 
-# PHENTSIZE: invalid e_phentsize: 1{{$}}
+# PHENTSIZE: Size of program headers: 1 (bytes)

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 56528d321de6..15076f1f8933 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1901,12 +1901,17 @@ std::pair<const typename ELFT::Phdr *, const typename ELFT::Shdr *>
 ELFDumper<ELFT>::findDynamic(const ELFFile<ELFT> *Obj) {
   // Try to locate the PT_DYNAMIC header.
   const Elf_Phdr *DynamicPhdr = nullptr;
-  for (const Elf_Phdr &Phdr :
-       unwrapOrError(ObjF->getFileName(), Obj->program_headers())) {
-    if (Phdr.p_type != ELF::PT_DYNAMIC)
-      continue;
-    DynamicPhdr = &Phdr;
-    break;
+  if (Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers()) {
+    for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
+      if (Phdr.p_type != ELF::PT_DYNAMIC)
+        continue;
+      DynamicPhdr = &Phdr;
+      break;
+    }
+  } else {
+    this->reportUniqueWarning(createError(
+        "unable to read program headers to locate the PT_DYNAMIC segment: " +
+        toString(PhdrsOrErr.takeError())));
   }
 
   // Try to locate the .dynamic section in the sections header table.
@@ -4340,8 +4345,15 @@ void GNUStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
 
   unsigned Width = ELFT::Is64Bits ? 18 : 10;
   unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
-  for (const auto &Phdr :
-       unwrapOrError(this->FileName, Obj->program_headers())) {
+
+  Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers();
+  if (!PhdrsOrErr) {
+    this->reportUniqueWarning(createError("unable to dump program headers: " +
+                                          toString(PhdrsOrErr.takeError())));
+    return;
+  }
+
+  for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
     Fields[0].Str = getElfPtType(Header->e_machine, Phdr.p_type);
     Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
     Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
@@ -4388,8 +4400,16 @@ void GNUStyle<ELFT>::printSectionMapping(const ELFO *Obj) {
   OS << "\n Section to Segment mapping:\n  Segment Sections...\n";
   DenseSet<const Elf_Shdr *> BelongsToSegment;
   int Phnum = 0;
-  for (const Elf_Phdr &Phdr :
-       unwrapOrError(this->FileName, Obj->program_headers())) {
+
+  Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers();
+  if (!PhdrsOrErr) {
+    this->reportUniqueWarning(createError(
+        "can't read program headers to build section to segment mapping: " +
+        toString(PhdrsOrErr.takeError())));
+    return;
+  }
+
+  for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
     std::string Sections;
     OS << format("   %2.2d     ", Phnum++);
     // Check if each section is in a segment and then print mapping.
@@ -5506,8 +5526,15 @@ void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
         reportError(std::move(Err), this->FileName);
     }
   } else {
-    for (const auto &P :
-         unwrapOrError(this->FileName, Obj->program_headers())) {
+    Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers();
+    if (!PhdrsOrErr) {
+      this->reportUniqueWarning(createError(
+          "unable to read program headers to locate the PT_NOTE segment: " +
+          toString(PhdrsOrErr.takeError())));
+      return;
+    }
+
+    for (const Elf_Phdr &P : *PhdrsOrErr) {
       if (P.p_type != PT_NOTE)
         continue;
       PrintHeader(/*SecName=*/None, P.p_offset, P.p_filesz);
@@ -6523,8 +6550,14 @@ template <class ELFT>
 void LLVMStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
   ListScope L(W, "ProgramHeaders");
 
-  for (const Elf_Phdr &Phdr :
-       unwrapOrError(this->FileName, Obj->program_headers())) {
+  Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers();
+  if (!PhdrsOrErr) {
+    this->reportUniqueWarning(createError("unable to dump program headers: " +
+                                          toString(PhdrsOrErr.takeError())));
+    return;
+  }
+
+  for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
     DictScope P(W, "ProgramHeader");
     W.printHex("Type",
                getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
@@ -6819,8 +6852,15 @@ void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
         reportError(std::move(Err), this->FileName);
     }
   } else {
-    for (const auto &P :
-         unwrapOrError(this->FileName, Obj->program_headers())) {
+    Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj->program_headers();
+    if (!PhdrsOrErr) {
+      this->reportUniqueWarning(createError(
+          "unable to read program headers to locate the PT_NOTE segment: " +
+          toString(PhdrsOrErr.takeError())));
+      return;
+    }
+
+    for (const Elf_Phdr &P : *PhdrsOrErr) {
       if (P.p_type != PT_NOTE)
         continue;
       DictScope D(W, "NoteSection");


        


More information about the llvm-commits mailing list