[llvm] 0b511c2 - [llvm-readobj] - Report warnings instead of errors for broken relocations.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 29 01:51:05 PST 2020


Author: Georgii Rymar
Date: 2020-02-29T12:50:32+03:00
New Revision: 0b511c230213005051d0fb716cd01aa85cc479ea

URL: https://github.com/llvm/llvm-project/commit/0b511c230213005051d0fb716cd01aa85cc479ea
DIFF: https://github.com/llvm/llvm-project/commit/0b511c230213005051d0fb716cd01aa85cc479ea.diff

LOG: [llvm-readobj] - Report warnings instead of errors for broken relocations.

This is a follow-up for https://reviews.llvm.org/D74545.

It adds test cases for each incorrect case returned in `getRelocationTarget`.

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

Added: 
    llvm/test/tools/llvm-readobj/ELF/relocation-errors.test

Modified: 
    llvm/include/llvm/Object/ELF.h
    llvm/test/Object/invalid.test
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index 42c5b67ac3fa..cc7abf9812fa 100644
--- a/llvm/include/llvm/Object/ELF.h
+++ b/llvm/include/llvm/Object/ELF.h
@@ -670,7 +670,6 @@ ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
                                        Elf_Shdr_Range Sections) const {
 
   if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
-    // TODO: this error is untested.
     return createError(
         "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
   auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link);

diff  --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test
index 442d38d46d06..2c80bd6f7453 100644
--- a/llvm/test/Object/invalid.test
+++ b/llvm/test/Object/invalid.test
@@ -312,30 +312,6 @@ FileHeader:
   Machine: EM_X86_64
   SHNum:   0xFF
 
-## Check that llvm-readobj reports an error if a relocation contains an
-## incorrect (too large) symbol index.
-
-# RUN: yaml2obj %s --docnum=16 -o %t16
-# RUN: not llvm-readobj -r %t16 2>&1 | FileCheck -DFILE=%t16 --check-prefix=INVALID-REL-SYM %s
-
-# INVALID-REL-SYM: error: '[[FILE]]': unable to access section [index 2] data at 0x18000040: offset goes past the end of file
-
---- !ELF
-FileHeader:
-  Class:   ELFCLASS64
-  Data:    ELFDATA2LSB
-  Type:    ET_REL
-  Machine: EM_X86_64
-Sections:
-  - Name: .rela.text
-    Type: SHT_RELA
-    Info: 0
-    Relocations:
-      - Offset: 0x0
-        Type:   R_X86_64_64
-        Symbol: 0xFFFFFF
-Symbols: []
-
 ## Check llvm-readobj does not crash on a truncated ELF.
 
 ## Create a truncated ELF object with ELFCLASSNONE class using echo.
@@ -352,8 +328,8 @@ Symbols: []
 ## Check that llvm-readobj reports an error if section name offset
 ## overflows the section name string table.
 
-# RUN: yaml2obj %s --docnum=17 -o %t17
-# RUN: llvm-readobj --sections %t17 2>&1 | FileCheck -DFILE=%t17 --check-prefix=BROKEN-SECNAME %s
+# RUN: yaml2obj %s --docnum=16 -o %t16
+# RUN: llvm-readobj --sections %t16 2>&1 | FileCheck -DFILE=%t16 --check-prefix=BROKEN-SECNAME %s
 
 ## BROKEN-SECNAME: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0xb) offset which goes past the end of the section name string table
 
@@ -371,9 +347,9 @@ Sections:
 ## Check that llvm-readobj reports an error if a section has a broken offset
 ## that goes past the end of the file.
 
-# RUN: yaml2obj %s --docnum=18 -o %t18
-# RUN: not llvm-readobj --sections --section-data %t18 2>&1 \
-# RUN:  | FileCheck -DFILE=%t18 --check-prefix=BROKEN-SECSHOFFSET %s
+# RUN: yaml2obj %s --docnum=17 -o %t17
+# RUN: not llvm-readobj --sections --section-data %t17 2>&1 \
+# RUN:  | FileCheck -DFILE=%t17 --check-prefix=BROKEN-SECSHOFFSET %s
 
 # BROKEN-SECSHOFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0xffff0000) + sh_size (0x0) that is greater than the file size (0x160)
 
@@ -391,8 +367,8 @@ Sections:
 ## Check that llvm-readobj reports an error if symbol name
 ## offset goes past the end of the symbol string table.
 
-# RUN: yaml2obj %s --docnum=19 -o %t19
-# RUN: not llvm-readobj --symbols %t19 2>&1 | FileCheck -DFILE=%t19 --check-prefix=INVALID-SYM-NAME %s
+# RUN: yaml2obj %s --docnum=18 -o %t18
+# RUN: not llvm-readobj --symbols %t18 2>&1 | FileCheck -DFILE=%t18 --check-prefix=INVALID-SYM-NAME %s
 
 # INVALID-SYM-NAME: error: '[[FILE]]': st_name (0x1) is past the end of the string table of size 0x1
 
@@ -412,8 +388,8 @@ Symbols:
 ## Version index in .gnu.version overflows the version map.
 ## Check llvm-readobj reports it.
 
-# RUN: yaml2obj %s --docnum=20 -o %t20
-# RUN: llvm-readobj -dt %t20 2>&1 | FileCheck -DFILE=%t20 --check-prefix=INVALID-VERSION %s
+# RUN: yaml2obj %s --docnum=19 -o %t19
+# RUN: llvm-readobj -dt %t19 2>&1 | FileCheck -DFILE=%t19 --check-prefix=INVALID-VERSION %s
 
 # INVALID-VERSION: warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 255 which is missing
 
@@ -441,8 +417,8 @@ DynamicSymbols:
 ## The dynamic table contains DT_STRTAB with a value that is not in any loadable segment.
 ## Check llvm-readobj reports it.
 
-# RUN: yaml2obj %s --docnum=21 -o %t21
-# RUN: llvm-readobj --dynamic-table %t21 2>&1 | FileCheck -DFILE=%t21 --check-prefix=INVALID-DTSTRTAB %s
+# RUN: yaml2obj %s --docnum=20 -o %t20
+# RUN: llvm-readobj --dynamic-table %t20 2>&1 | FileCheck -DFILE=%t20 --check-prefix=INVALID-DTSTRTAB %s
 
 # INVALID-DTSTRTAB: warning: '[[FILE]]': Unable to parse DT_STRTAB: virtual address is not in any segment: 0xffff0000
 
@@ -479,8 +455,8 @@ ProgramHeaders:
 ## Check that llvm-readobj reports a warning when .dynamic section has an invalid
 ## size, which isn't a multiple of the dynamic entry size. 
 
-# RUN: yaml2obj %s --docnum=22 -o %t22
-# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck -DFILE=%t22 --check-prefix=DYN-TABLE-SIZE %s
+# RUN: yaml2obj %s --docnum=21 -o %t21
+# RUN: llvm-readobj --dyn-relocations %t21 2>&1 | FileCheck -DFILE=%t21 --check-prefix=DYN-TABLE-SIZE %s
 
 # DYN-TABLE-SIZE: warning: '[[FILE]]': section with index 1 has invalid size (0x1){{$}}
 
@@ -498,8 +474,8 @@ Sections:
 ## PT_DYNAMIC's p_offset field is so large that p_offset + p_filesz is larger
 ## than the object size. Check llvm-readobj reports it.
 
-# RUN: yaml2obj %s --docnum=23 -o %t23
-# RUN: llvm-readobj --dyn-relocations %t23 2>&1 | FileCheck -DFILE=%t23 --check-prefix=DYN-TABLE-PHDR %s
+# RUN: yaml2obj %s --docnum=22 -o %t22
+# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck -DFILE=%t22 --check-prefix=DYN-TABLE-PHDR %s
 
 # DYN-TABLE-PHDR: warning: '[[FILE]]': PT_DYNAMIC segment offset + size exceeds the size of the file
 
@@ -524,9 +500,9 @@ ProgramHeaders:
 ## PT_DYNAMIC's p_filesz field is so large that p_offset + p_filesz is larger
 ## than the object size. Check llvm-readobj reports it.
 
-# RUN: yaml2obj %s --docnum=24 -o %t24
-# RUN: llvm-readobj --dyn-relocations %t24 2>&1 \
-# RUN:  | FileCheck -DFILE=%t24 --check-prefix=DYN-TABLE-PHDR %s
+# RUN: yaml2obj %s --docnum=23 -o %t23
+# RUN: llvm-readobj --dyn-relocations %t23 2>&1 \
+# RUN:  | FileCheck -DFILE=%t23 --check-prefix=DYN-TABLE-PHDR %s
 
 --- !ELF
 FileHeader:
@@ -546,8 +522,8 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-# RUN: yaml2obj --docnum=25 %s -o %t25
-# RUN: not obj2yaml 2>&1 %t25 | FileCheck %s -DFILE=%t25 --check-prefix=INVALID-SHSTRNDX
+# RUN: yaml2obj --docnum=24 %s -o %t24
+# RUN: not obj2yaml 2>&1 %t24 | FileCheck %s -DFILE=%t24 --check-prefix=INVALID-SHSTRNDX
 
 # INVALID-SHSTRNDX: Error reading file: [[FILE]]: section header string table index 255 does not exist
 
@@ -566,8 +542,8 @@ Sections:
 ## is greater than UINT64_MAX / sizeof(Elf_Shdr) == 288230376151711743.
 ## Here we check that do not crash on a border value.
 
-# RUN: yaml2obj --docnum=26 %s -o %t26
-# RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM1 %s
+# RUN: yaml2obj --docnum=25 %s -o %t25
+# RUN: not llvm-readobj -h %t25 2>&1 | FileCheck -DFILE=%t25 --check-prefix=INVALID-SEC-NUM1 %s
 
 # INVALID-SEC-NUM1: error: '[[FILE]]': invalid section header table offset (e_shoff = 0x58) or invalid number of sections specified in the first section header's sh_size field (0x3ffffffffffffff)
 
@@ -585,8 +561,8 @@ Sections:
 ## See above, but now we test the UINT64_MAX / sizeof(Elf_Shdr) value.
 ## The error is slightly 
diff erent in this case.
 
-# RUN: yaml2obj --docnum=27 %s -o %t27
-# RUN: not llvm-readobj -h %t27 2>&1 | FileCheck -DFILE=%t27 --check-prefix=INVALID-SEC-NUM2 %s
+# RUN: yaml2obj --docnum=26 %s -o %t26
+# RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM2 %s
 
 # INVALID-SEC-NUM2: error: '[[FILE]]': invalid number of sections specified in the NULL section's sh_size field (288230376151711744)
 
@@ -603,8 +579,8 @@ Sections:
 
 ## Check the case when SHOff is too large. SHOff + sizeof(Elf_Shdr) overflows the uint64 type.
 
-# RUN: yaml2obj --docnum=28 %s -o %t28
-# RUN: not llvm-readobj -h %t28 2>&1 | FileCheck -DFILE=%t28 --check-prefix=INVALID-SEC-NUM3 %s
+# RUN: yaml2obj --docnum=27 %s -o %t27
+# RUN: not llvm-readobj -h %t27 2>&1 | FileCheck -DFILE=%t27 --check-prefix=INVALID-SEC-NUM3 %s
 
 # INVALID-SEC-NUM3: error: '[[FILE]]': section header table goes past the end of the file: e_shoff = 0xffffffffffffffff
  
@@ -619,8 +595,8 @@ FileHeader:
 ## Check that llvm-objdump reports an error when it tries to dump a
 ## symbol name and .strtab is empty.
 
-# RUN: yaml2obj %s --docnum=29 -o %t29
-# RUN: not llvm-objdump -syms %t29 2>&1 | FileCheck -DFILE=%t29 --check-prefix=STRTAB-EMPTY2 %s
+# RUN: yaml2obj %s --docnum=28 -o %t28
+# RUN: not llvm-objdump -syms %t28 2>&1 | FileCheck -DFILE=%t28 --check-prefix=STRTAB-EMPTY2 %s
 
 # STRTAB-EMPTY2: error: '[[FILE]]': SHT_STRTAB string table section [index 1] is empty
 
@@ -640,8 +616,8 @@ Symbols:
 ## Check that we report a warning if SHT_GNU_versym has invalid
 ## sh_entsize value (3 instead of 2) when trying to access the entries.
 
-# RUN: yaml2obj %s --docnum=30 -o %t30
-# RUN: llvm-readobj -V %t30 2>&1 | FileCheck -DFILE=%t30 --check-prefix=INVALID-VER-SHENTSIZE %s
+# RUN: yaml2obj %s --docnum=29 -o %t29
+# RUN: llvm-readobj -V %t29 2>&1 | FileCheck -DFILE=%t29 --check-prefix=INVALID-VER-SHENTSIZE %s
 
 # INVALID-VER-SHENTSIZE: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has an invalid sh_entsize: 3
 
@@ -666,8 +642,8 @@ DynamicSymbols:
 ## 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=31 %s -o %t31
-# RUN: not llvm-objcopy %t31 2>&1 | FileCheck %s -DFILE=%t31 --check-prefix=INVALID-SHSTRTAB-INDEX
+# 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
 

diff  --git a/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test b/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
new file mode 100644
index 000000000000..add8b9a3342d
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
@@ -0,0 +1,94 @@
+## Check how we report warnings when dumping an object with broken relocations.
+
+# RUN: yaml2obj %s -o %t64
+# RUN: llvm-readobj --relocations %t64 2>&1 | FileCheck %s -DFILE=%t64 --check-prefix=LLVM
+# RUN: llvm-readelf --relocations %t64 2>&1 | FileCheck %s -DFILE=%t64 --check-prefix=GNU
+
+# LLVM:      Relocations [
+# LLVM-NEXT:   Section (3) .rel.text {
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in section 3: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 2 in section 3: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file
+# LLVM-NEXT:     0x2 R_X86_64_NONE - 0x0
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 4 in section 3: invalid section index: 255
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 5 in section 3: a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table
+# LLVM-NEXT:   }
+# LLVM-NEXT:   Section (4) .rela.text {
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in section 4: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM
+# LLVM-NEXT:   }
+# LLVM-NEXT: ]
+
+# GNU:       Relocation section '.rel.text' at offset 0x41 contains 5 entries:
+# GNU-NEXT:      Offset             Info             Type               Symbol's Value  Symbol's Name
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 1 in section 3: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 2 in section 3: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file
+# GNU-NEXT:  0000000000000002  0000000000000000 R_X86_64_NONE
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 4 in section 3: invalid section index: 255
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 5 in section 3: a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table
+# GNU-EMPTY:
+# GNU-NEXT:  Relocation section '.rela.text' at offset 0x91 contains 1 entries:
+# GNU-NEXT:      Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 1 in section 4: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name:    .text
+    Type:    SHT_PROGBITS
+    Content: '00'
+  - Name:   .foo
+    Type:   SHT_PROGBITS
+    ShName: 0xFEFEFEFE
+  - Name: .rel.text
+    Type: SHT_REL
+    Info: .text
+    Relocations:
+## Case 1: There is no symbol with index 0xFEFEFEFE.
+      - Offset: 0x0
+        Symbol: 0xFEFEFEFE
+        Type:   R_X86_64_NONE
+      - Offset: 0x0
+        Symbol: 0xFEFEFEFE
+        Type:   R_X86_64_NONE
+## Case 2: Test that no warning is reported for a relocation against a symbol with index 0.
+      - Offset: 0x2
+        Symbol: 0
+        Type:   R_X86_64_NONE
+## Case 3: Test a relocation against a section symbol that has an invalid
+##         section index (larger than the number of sections).
+      - Offset: 0x3
+        Symbol: .sec.symbol1
+        Type:   R_X86_64_NONE
+## Case 4: Test a relocation against a section symbol that has an invalid
+##         sh_name offset that goes past the end of the section name string table.
+      - Offset: 0x4
+        Symbol: .sec.symbol2
+        Type:   R_X86_64_NONE
+## Case 5: Test a relocation in a section that is linked to a symbol table that
+##         has a section type that is neither SHT_SYMTAB nor SHT_DYNSYM.
+##         In this case the code fails to find a corresponding symbol string table.
+  - Name: .rela.text
+    Type: SHT_RELA
+    Info: .text
+    Link: .fake.symtab
+    Relocations:
+      - Offset: 0x5
+        Symbol: symbol
+        Type:   R_X86_64_NONE
+  - Name:    .fake.symtab
+    Type:    SHT_PROGBITS
+    EntSize: 24
+    Size:    48
+Symbols:
+  - Name:    symbol
+    Section: .text
+    Value:   0
+  - Name:  .sec.symbol1
+    Type:  STT_SECTION
+    Index: 0xFF
+  - Name:  .sec.symbol2
+    Type:  STT_SECTION
+    Index: 0x2

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 93ec47751b6b..bad5f7185074 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -873,8 +873,9 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
   void printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym, uint32_t Sym,
                          StringRef StrTable, uint32_t Bucket);
   void printRelocHeader(unsigned SType);
-  void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
-                       const Elf_Rela &R, bool IsRela);
+  void printRelocation(const ELFO *Obj, unsigned SecIndex,
+                       const Elf_Shdr *SymTab, const Elf_Rela &R,
+                       unsigned RelIndex, bool IsRela);
   void printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
                        StringRef SymbolName, const Elf_Rela &R, bool IsRela);
   void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
@@ -939,7 +940,8 @@ template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
   void printMipsABIFlags(const ELFObjectFile<ELFT> *Obj) override;
 
 private:
-  void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
+  void printRelocation(const ELFO *Obj, unsigned SecIndex, Elf_Rela Rel,
+                       unsigned RelIndex, const Elf_Shdr *SymTab);
   void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
   void printSymbols(const ELFO *Obj);
   void printDynamicSymbols(const ELFO *Obj);
@@ -3327,13 +3329,18 @@ template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
 }
 
 template <class ELFT>
-void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
-                                     const Elf_Rela &R, bool IsRela) {
-  const typename ELFT::Sym *Sym;
-  std::string Name;
-  std::tie(Sym, Name) = unwrapOrError(
-      this->FileName, this->dumper()->getRelocationTarget(SymTab, R));
-  printRelocation(Obj, Sym, Name, R, IsRela);
+void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, unsigned SecIndex,
+                                     const Elf_Shdr *SymTab, const Elf_Rela &R,
+                                     unsigned RelIndex, bool IsRela) {
+  Expected<std::pair<const typename ELFT::Sym *, std::string>> Target =
+      this->dumper()->getRelocationTarget(SymTab, R);
+  if (!Target)
+    this->reportUniqueWarning(createError(
+        "unable to print relocation " + Twine(RelIndex) + " in section " +
+        Twine(SecIndex) + ": " + toString(Target.takeError())));
+  else
+    printRelocation(Obj, /*Sym=*/Target->first, /*Name=*/Target->second, R,
+                    IsRela);
 }
 
 template <class ELFT>
@@ -3431,6 +3438,9 @@ template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
     printRelocHeader(Sec.sh_type);
     const Elf_Shdr *SymTab =
         unwrapOrError(this->FileName, Obj->getSection(Sec.sh_link));
+    unsigned SecNdx = &Sec - &cantFail(Obj->sections()).front();
+    unsigned RelNdx = 0;
+
     switch (Sec.sh_type) {
     case ELF::SHT_REL:
       for (const auto &R : unwrapOrError(this->FileName, Obj->rels(&Sec))) {
@@ -3438,12 +3448,12 @@ template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
         Rela.r_offset = R.r_offset;
         Rela.r_info = R.r_info;
         Rela.r_addend = 0;
-        printRelocation(Obj, SymTab, Rela, false);
+        printRelocation(Obj, SecNdx, SymTab, Rela, ++RelNdx, false);
       }
       break;
     case ELF::SHT_RELA:
       for (const auto &R : unwrapOrError(this->FileName, Obj->relas(&Sec)))
-        printRelocation(Obj, SymTab, R, true);
+        printRelocation(Obj, SecNdx, SymTab, R, ++RelNdx, true);
       break;
     case ELF::SHT_RELR:
     case ELF::SHT_ANDROID_RELR:
@@ -3453,12 +3463,13 @@ template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
              << "\n";
       else
         for (const auto &R : RelrRelas)
-          printRelocation(Obj, SymTab, R, false);
+          printRelocation(Obj, SecNdx, SymTab, R, ++RelNdx, false);
       break;
     case ELF::SHT_ANDROID_REL:
     case ELF::SHT_ANDROID_RELA:
       for (const auto &R : AndroidRelas)
-        printRelocation(Obj, SymTab, R, Sec.sh_type == ELF::SHT_ANDROID_RELA);
+        printRelocation(Obj, SecNdx, SymTab, R, ++RelNdx,
+                        Sec.sh_type == ELF::SHT_ANDROID_RELA);
       break;
     }
   }
@@ -5705,6 +5716,8 @@ template <class ELFT>
 void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
   const Elf_Shdr *SymTab =
       unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
+  unsigned SecNdx = Sec - &cantFail(Obj->sections()).front();
+  unsigned RelNdx = 0;
 
   switch (Sec->sh_type) {
   case ELF::SHT_REL:
@@ -5713,12 +5726,12 @@ void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
       Rela.r_offset = R.r_offset;
       Rela.r_info = R.r_info;
       Rela.r_addend = 0;
-      printRelocation(Obj, Rela, SymTab);
+      printRelocation(Obj, SecNdx, Rela, ++RelNdx, SymTab);
     }
     break;
   case ELF::SHT_RELA:
     for (const Elf_Rela &R : unwrapOrError(this->FileName, Obj->relas(Sec)))
-      printRelocation(Obj, R, SymTab);
+      printRelocation(Obj, SecNdx, R, ++RelNdx, SymTab);
     break;
   case ELF::SHT_RELR:
   case ELF::SHT_ANDROID_RELR: {
@@ -5730,7 +5743,7 @@ void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
       std::vector<Elf_Rela> RelrRelas =
           unwrapOrError(this->FileName, Obj->decode_relrs(Relrs));
       for (const Elf_Rela &R : RelrRelas)
-        printRelocation(Obj, R, SymTab);
+        printRelocation(Obj, SecNdx, R, ++RelNdx, SymTab);
     }
     break;
   }
@@ -5738,19 +5751,25 @@ void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
   case ELF::SHT_ANDROID_RELA:
     for (const Elf_Rela &R :
          unwrapOrError(this->FileName, Obj->android_relas(Sec)))
-      printRelocation(Obj, R, SymTab);
+      printRelocation(Obj, SecNdx, R, ++RelNdx, SymTab);
     break;
   }
 }
 
 template <class ELFT>
-void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
+void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, unsigned SecIndex,
+                                      Elf_Rela Rel, unsigned RelIndex,
                                       const Elf_Shdr *SymTab) {
-  std::string TargetName =
-      unwrapOrError(this->FileName,
-                    this->dumper()->getRelocationTarget(SymTab, Rel))
-          .second;
+  Expected<std::pair<const typename ELFT::Sym *, std::string>> Target =
+      this->dumper()->getRelocationTarget(SymTab, Rel);
+  if (!Target) {
+    this->reportUniqueWarning(createError(
+        "unable to print relocation " + Twine(RelIndex) + " in section " +
+        Twine(SecIndex) + ": " + toString(Target.takeError())));
+    return;
+  }
 
+  std::string TargetName = Target->second;
   SmallString<32> RelocName;
   Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
 


        


More information about the llvm-commits mailing list