[llvm] d4035af - [llvm-readelf/obj] - Print section symbol names properly when dumping relocations.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 03:33:51 PDT 2020


Author: Georgii Rymar
Date: 2020-09-23T13:25:39+03:00
New Revision: d4035af2537432da41b2728829f8cd2fca9a9de8

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

LOG: [llvm-readelf/obj] - Print section symbol names properly when dumping relocations.

Currently `--relocations` ignores section symbol names and always prints
section names for them. This is inconsistent with GNU readelf and with `--symbols`.

We have a code in `getFullSymbolName` (which is used for `--symbols`) which can be
reused for `getRelocationTarget` (used for `--relocations`).
With that the issue described is fixed and code becomes a bit shorter.
Also with this change we start to print more relocations (in situations when we just
showed warnings instead before) and also start to report more diagnostic warnings
(see reloc-zero-name-or-value.test).

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

Added: 
    

Modified: 
    llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
    llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
    llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
    llvm/test/tools/llvm-readobj/ELF/section-symbols.test
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml b/llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
index 04855e0d2f66..888e5a50ebbd 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
+++ b/llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
@@ -16,10 +16,10 @@ Sections:
     Info:            .debug_foo
     Relocations:
       - Offset:          0x1
-        Symbol:          .debug_foo
+        Symbol:          1
         Type:            R_X86_64_32
       - Offset:          0x2
-        Symbol:          .notdebug_foo
+        Symbol:          2
         Type:            R_X86_64_32
 ## This section should not be involved in relocations.
   - Name:            .debug_bar
@@ -44,12 +44,10 @@ Sections:
     Flags:           [ SHF_GROUP ]
     Content:         '00'
 Symbols:
-  - Name:    .debug_foo
-    Type:    STT_SECTION
+  - Type:    STT_SECTION
     Section: .debug_foo
     Binding:  STB_GLOBAL
-  - Name:    .notdebug_foo
-    Type:    STT_SECTION
+  - Type:    STT_SECTION
     Section: .notdebug_foo
     Binding:  STB_GLOBAL
   - Name:    .Linfo_string0

diff  --git a/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test b/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
index 062f67b80046..24e83ad6598d 100644
--- a/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
+++ b/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
@@ -3,14 +3,15 @@
 ## name, we print it. Test for both static and dynamic relocation printing.
 
 # RUN: yaml2obj %s -o %t
-# RUN: llvm-readelf --relocations --dyn-relocations %t | FileCheck %s
+# RUN: llvm-readelf --relocations --dyn-relocations %t 2>&1 | FileCheck %s -DFILE=%t
 
 # CHECK:      Relocation section '.rela.text' at offset {{.*}} contains 5 entries:
 # CHECK-NEXT:     Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
 # CHECK-NEXT: 0000000000000000  0000000000000000 R_X86_64_NONE                           1
 # CHECK-NEXT: 0000000000000000  0000000100000000 R_X86_64_NONE          0000000000000000 sym + 1
 # CHECK-NEXT: 0000000000000000  0000000200000000 R_X86_64_NONE          0000000000000123 456
-# CHECK-NEXT: 0000000000000000  0000000300000000 R_X86_64_NONE          0000000000000000 678
+# CHECK-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# CHECK-NEXT: 0000000000000000  0000000300000000 R_X86_64_NONE          0000000000000000 <?> + 678
 # CHECK-NEXT: 0000000000000000  0000000400000000 R_X86_64_NONE          0000000000000000 2
 
 # CHECK:      Relocation section '.rela.dyn' at offset {{.*}} contains 5 entries:
@@ -18,7 +19,7 @@
 # CHECK-NEXT: 0000000000000000  0000000000000000 R_X86_64_NONE                           1
 # CHECK-NEXT: 0000000000000000  0000000100000000 R_X86_64_NONE          0000000000000000 sym + 1
 # CHECK-NEXT: 0000000000000000  0000000200000000 R_X86_64_NONE          0000000000000123 456
-# CHECK-NEXT: 0000000000000000  0000000300000000 R_X86_64_NONE          0000000000000000 678
+# CHECK-NEXT: 0000000000000000  0000000300000000 R_X86_64_NONE          0000000000000000 <?> + 678
 # CHECK-NEXT: 0000000000000000  0000000400000000 R_X86_64_NONE          0000000000000000 2
 
 # CHECK:      'RELA' relocation section at offset {{.*}} contains 120 bytes:

diff  --git a/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test b/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
index 59cf7155e2eb..ef9c4a1a90ce 100644
--- a/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
+++ b/llvm/test/tools/llvm-readobj/ELF/relocation-errors.test
@@ -6,26 +6,34 @@
 
 # LLVM:      Relocations [
 # LLVM-NEXT:   Section (3) .rel.text {
-# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 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 SHT_REL section with index 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 1 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file
+# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: 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 SHT_REL section with index 3: invalid section index: 255
-# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 5 in SHT_REL section with index 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:     0x3 R_X86_64_NONE .sec.symbol1 0x0
+# LLVM-NEXT: warning: '[[FILE]]': invalid section index: 255
+# LLVM-NEXT:     0x4 R_X86_64_NONE <section 255> 0x0
+# LLVM-NEXT:     0x5 R_X86_64_NONE .sec.symbol2 0x0
+# LLVM-NEXT: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table
+# LLVM-NEXT:     0x6 R_X86_64_NONE <section 2> 0x0
 # LLVM-NEXT:   }
 # LLVM-NEXT:   Section (4) .rela.text {
 # LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_RELA section with index 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:       Relocation section '.rel.text' at offset 0x41 contains 7 entries:
 # GNU-NEXT:      Offset             Info             Type               Symbol's Value  Symbol's Name
-# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 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 SHT_REL section with index 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 1 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file
+# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: 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 SHT_REL section with index 3: invalid section index: 255
-# GNU-NEXT:  warning: '[[FILE]]': unable to print relocation 5 in SHT_REL section with index 3: a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table
+# GNU-NEXT:  0000000000000003  0000000200000000 R_X86_64_NONE 0000000000000000 .sec.symbol1
+# GNU-NEXT:  warning: '[[FILE]]': invalid section index: 255
+# GNU-NEXT:  0000000000000004  0000000400000000 R_X86_64_NONE 0000000000000000 <section 255>
+# GNU-NEXT:  0000000000000005  0000000300000000 R_X86_64_NONE 0000000000000000 .sec.symbol2
+# GNU-NEXT:  warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table
+# GNU-NEXT:  0000000000000006  0000000500000000 R_X86_64_NONE 0000000000000000 <section 2>
 # GNU-EMPTY:
-# GNU-NEXT:  Relocation section '.rela.text' at offset 0x91 contains 1 entries:
+# GNU-NEXT:  Relocation section '.rela.text' at offset 0xb1 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 SHT_RELA section with index 4: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM
 
@@ -55,17 +63,27 @@ Sections:
       - Offset: 0x2
         Symbol: 0
         Type:   R_X86_64_NONE
-## Case 3: Test a relocation against a section symbol that has an invalid
+## Case 3: Test a relocation against a named 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.
+## Case 4: Test a relocation against an unnamed section symbol that has an invalid
+##         section index (larger than the number of sections).
       - Offset: 0x4
+        Symbol: 4
+        Type:   R_X86_64_NONE
+## Case 4: Test a relocation against a named section symbol with a section with an invalid
+##         sh_name offset that goes past the end of the section name string table.
+      - Offset: 0x5
         Symbol: .sec.symbol2
         Type:   R_X86_64_NONE
-## Case 5: Test a relocation in a section that is linked to a symbol table that
+## Case 5: Test a relocation against an unnamed section symbol with a section with an invalid
+##         sh_name offset that goes past the end of the section name string table.
+      - Offset: 0x6
+        Symbol: 5
+        Type:   R_X86_64_NONE
+## Case 6: 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
@@ -73,7 +91,7 @@ Sections:
     Info: .text
     Link: .fake.symtab
     Relocations:
-      - Offset: 0x5
+      - Offset: 0x7
         Symbol: symbol
         Type:   R_X86_64_NONE
   - Name:    .fake.symtab
@@ -90,3 +108,7 @@ Symbols:
   - Name:  .sec.symbol2
     Type:  STT_SECTION
     Index: 0x2
+  - Type:  STT_SECTION
+    Index: 0xFF
+  - Type:  STT_SECTION
+    Index: 0x2

diff  --git a/llvm/test/tools/llvm-readobj/ELF/section-symbols.test b/llvm/test/tools/llvm-readobj/ELF/section-symbols.test
index 1aac1e6f06e8..61f5991dafcd 100644
--- a/llvm/test/tools/llvm-readobj/ELF/section-symbols.test
+++ b/llvm/test/tools/llvm-readobj/ELF/section-symbols.test
@@ -3,8 +3,6 @@
 ## are printed if the section is somehow invalid.
 
 # RUN: yaml2obj %s -o %t1
-## FIXME: 1) Relocations should print section symbol names when they are not empty.
-##        2) We should still print a relocation even when we are unable to lookup a symbol name.
 # RUN: llvm-readobj %t1 --symbols --relocations 2>&1 | \
 # RUN:   FileCheck %s -DFILE=%t1 --check-prefix=LLVM1 --implicit-check-not="warning:"
 # RUN: llvm-readelf %t1 --symbols --relocations 2>&1 | \
@@ -13,50 +11,50 @@
 # LLVM1:      Relocations [
 # LLVM1-NEXT:   Section (4) .rela.foo {
 # LLVM1-NEXT:     0x1 R_X86_64_NONE .foo 0x0
-# LLVM1-NEXT:     0x2 R_X86_64_NONE .foo 0x0
-# LLVM1-NEXT: warning: '[[FILE]]': unable to print relocation 3 in SHT_RELA section with index 4: invalid section index: 67
-# LLVM1-NEXT: warning: '[[FILE]]': unable to print relocation 4 in SHT_RELA section with index 4: invalid section index: 67
+# LLVM1-NEXT:     0x2 R_X86_64_NONE symbol1 0x0
+# LLVM1-NEXT: warning: '[[FILE]]': invalid section index: 67
+# LLVM1-NEXT:     0x3 R_X86_64_NONE <section 67> 0x0
+# LLVM1-NEXT:     0x4 R_X86_64_NONE symbol2 0x0
 # LLVM1-NEXT:     0x5 R_X86_64_NONE .bar 0x0
-# LLVM1-NEXT:     0x6 R_X86_64_NONE .bar 0x0
-# LLVM1-NEXT: warning: '[[FILE]]': unable to print relocation 7 in SHT_RELA section with index 4: invalid section index: 66
-# LLVM1-NEXT: warning: '[[FILE]]': unable to print relocation 8 in SHT_RELA section with index 4: invalid section index: 66
+# LLVM1-NEXT:     0x6 R_X86_64_NONE symbol3 0x0
+# LLVM1-NEXT: warning: '[[FILE]]': invalid section index: 66
+# LLVM1-NEXT:     0x7 R_X86_64_NONE <section 66> 0x0
+# LLVM1-NEXT:     0x8 R_X86_64_NONE symbol4 0x0
 # LLVM1-NEXT:   }
 # LLVM1-NEXT: ]
 
 # LLVM1: Name: (0)
 # LLVM1: Name: .foo (0)
 # LLVM1: Name: symbol1 (25)
-# LLVM1: warning: '[[FILE]]': invalid section index: 67
 # LLVM1: Name: <section 67> (0)
 # LLVM1: Name: symbol2 (17)
 # LLVM1: Name: .bar (0)
 # LLVM1: Name: symbol3 (9)
-# LLVM1: warning: '[[FILE]]': invalid section index: 66
 # LLVM1: Name: <section 66> (0)
 # LLVM1: Name: symbol4 (1)
 
 # GNU1:      Relocation section '.rela.foo' at offset 0x58 contains 8 entries:
 # GNU1-NEXT:  Offset     Info    Type         Sym. Value  Symbol's Name + Addend
 # GNU1-NEXT: 00000001  00000100 R_X86_64_NONE   00000000   .foo + 0
-# GNU1-NEXT: 00000002  00000200 R_X86_64_NONE   00000000   .foo + 0
-# GNU1-NEXT: warning: '[[FILE]]': unable to print relocation 3 in SHT_RELA section with index 4: invalid section index: 67
-# GNU1-NEXT: warning: '[[FILE]]': unable to print relocation 4 in SHT_RELA section with index 4: invalid section index: 67
+# GNU1-NEXT: 00000002  00000200 R_X86_64_NONE   00000000   symbol1 + 0
+# GNU1-NEXT: warning: '[[FILE]]': invalid section index: 67
+# GNU1-NEXT: 00000003  00000300 R_X86_64_NONE   00000000   <section 67> + 0
+# GNU1-NEXT: 00000004  00000400 R_X86_64_NONE   00000000   symbol2 + 0
 # GNU1-NEXT: 00000005  00000500 R_X86_64_NONE   00000000   .bar + 0
-# GNU1-NEXT: 00000006  00000600 R_X86_64_NONE   00000000   .bar + 0
-# GNU1-NEXT: warning: '[[FILE]]': unable to print relocation 7 in SHT_RELA section with index 4: invalid section index: 66
-# GNU1-NEXT: warning: '[[FILE]]': unable to print relocation 8 in SHT_RELA section with index 4: invalid section index: 66
+# GNU1-NEXT: 00000006  00000600 R_X86_64_NONE   00000000   symbol3 + 0
+# GNU1-NEXT: warning: '[[FILE]]': invalid section index: 66
+# GNU1-NEXT: 00000007  00000700 R_X86_64_NONE   00000000   <section 66> + 0
+# GNU1-NEXT: 00000008  00000800 R_X86_64_NONE   00000000   symbol4 + 0
 
 # GNU1:      Symbol table '.symtab' contains 9 entries:
 # GNU1-NEXT:    Num: {{.*}} Type    {{.*}} Ndx Name
 # GNU1-NEXT:      0: {{.*}} NOTYPE  {{.*}} UND {{$}}
 # GNU1-NEXT:      1: {{.*}} SECTION {{.*}}  1 .foo
 # GNU1-NEXT:      2: {{.*}} SECTION {{.*}}  1 symbol1
-# GNU1-NEXT: warning: '[[FILE]]': invalid section index: 67
 # GNU1-NEXT:      3: {{.*}} SECTION {{.*}} 67 <section 67>
 # GNU1-NEXT:      4: {{.*}} SECTION {{.*}} 67 symbol2
 # GNU1-NEXT:      5: {{.*}} SECTION {{.*}}  2 .bar
 # GNU1-NEXT:      6: {{.*}} SECTION {{.*}}  2 symbol3
-# GNU1-NEXT: warning: '[[FILE]]': invalid section index: 66
 # GNU1-NEXT:      7: {{.*}} SECTION {{.*}} 66 <section 66>
 # GNU1-NEXT:      8: {{.*}} SECTION {{.*}} 66 symbol4
 

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index ed777b37c309..758fae5b65f3 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1079,23 +1079,6 @@ ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
   if (!Sym)
     return RelSymbol<ELFT>(nullptr, "");
 
-  // The st_name field of a STT_SECTION is usually 0 (empty string).
-  // This code block returns the section name.
-  if (Sym->getType() == ELF::STT_SECTION) {
-    Expected<const Elf_Shdr *> SecOrErr =
-        Obj.getSection(*Sym, SymTab, ShndxTable);
-    if (!SecOrErr)
-      return SecOrErr.takeError();
-    // A section symbol describes the section at index 0.
-    if (*SecOrErr == nullptr)
-      return RelSymbol<ELFT>(Sym, "");
-
-    Expected<StringRef> NameOrErr = Obj.getSectionName(**SecOrErr);
-    if (!NameOrErr)
-      return NameOrErr.takeError();
-    return RelSymbol<ELFT>(Sym, NameOrErr->str());
-  }
-
   Expected<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*SymTab);
   if (!StrTableOrErr)
     return StrTableOrErr.takeError();
@@ -1103,7 +1086,7 @@ ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
   const Elf_Sym *FirstSym =
       cantFail(Obj.template getEntry<Elf_Sym>(*SymTab, 0));
   std::string SymbolName = getFullSymbolName(
-      *Sym, FirstSym - Sym, *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM);
+      *Sym, Sym - FirstSym, *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM);
   return RelSymbol<ELFT>(Sym, SymbolName);
 }
 


        


More information about the llvm-commits mailing list