[PATCH] Fix "llvm-objdump -d -r" to show relocations inline for ELF files

Mark Seaborn mseaborn at chromium.org
Wed Jan 15 14:56:28 PST 2014


Hi rafael.espindola, rafael,

Fix "llvm-objdump -d -r" to show relocations inline for ELF files

This fixes a regression introduced by r182908, which broke
llvm-objdump's ability to display relocations inline in a disassembly
dump for ELF object files.

That change removed a SectionRelocMap from Object/ELF.h, which we
recreate in llvm-objdump.cpp.

I discovered this regression via an out-of-tree test
(test/NaCl/X86/pnacl-hides-sandbox-x86-64.ll) which used llvm-objdump.

Note that the "Unknown" string in the test output on i386 isn't quite
right, but this appears to be a pre-existing bug.


http://llvm-reviews.chandlerc.com/D2559

Files:
  test/Object/X86/objdump-disassembly-inline-relocations.test
  tools/llvm-objdump/llvm-objdump.cpp

Index: test/Object/X86/objdump-disassembly-inline-relocations.test
===================================================================
--- test/Object/X86/objdump-disassembly-inline-relocations.test
+++ test/Object/X86/objdump-disassembly-inline-relocations.test
@@ -6,6 +6,10 @@
 RUN:              | FileCheck %s -check-prefix MACHO-i386
 RUN: llvm-objdump -d -r %p/../Inputs/trivial-object-test.macho-x86-64 \
 RUN:              | FileCheck %s -check-prefix MACHO-x86-64
+RUN: llvm-objdump -d -r %p/../Inputs/trivial-object-test.elf-i386 \
+RUN:              | FileCheck %s -check-prefix ELF-i386
+RUN: llvm-objdump -d -r %p/../Inputs/trivial-object-test.elf-x86-64 \
+RUN:              | FileCheck %s -check-prefix ELF-x86-64
 
 COFF-i386: file format COFF-i386
 COFF-i386: Disassembly of section .text:
@@ -65,3 +69,34 @@
 MACHO-x86-64:       1f:       8b 44 24 04                                     movl    4(%rsp), %eax
 MACHO-x86-64:       23:       48 83 c4 08                                     addq    $8, %rsp
 MACHO-x86-64:       27:       c3                                              ret
+
+ELF-i386: file format ELF32-i386
+ELF-i386: Disassembly of section .text:
+ELF-i386: main:
+ELF-i386:        0:     83 ec 0c                                        subl    $12, %esp
+ELF-i386:        3:     c7 44 24 08 00 00 00 00                         movl    $0, 8(%esp)
+ELF-i386:        b:     c7 04 24 00 00 00 00                            movl    $0, (%esp)
+ELF-i386:                              e: R_386_32      Unknown
+ELF-i386:       12:     e8 fc ff ff ff                                  calll   -4
+ELF-i386:                             13: R_386_PC32    Unknown
+ELF-i386:       17:     e8 fc ff ff ff                                  calll   -4
+ELF-i386:                             18: R_386_PC32    Unknown
+ELF-i386:       1c:     8b 44 24 08                                     movl    8(%esp), %eax
+ELF-i386:       20:     83 c4 0c                                        addl    $12, %esp
+ELF-i386:       23:     c3                                              retl
+
+ELF-x86-64: file format ELF64-x86-64
+ELF-x86-64: Disassembly of section .text:
+ELF-x86-64: main:
+ELF-x86-64:        0:   48 83 ec 08                                     subq    $8, %rsp
+ELF-x86-64:        4:   c7 44 24 04 00 00 00 00                         movl    $0, 4(%rsp)
+ELF-x86-64:        c:   bf 00 00 00 00                                  movl    $0, %edi
+ELF-x86-64:                            d: R_X86_64_32S  .rodata.str1.1+0
+ELF-x86-64:       11:   e8 00 00 00 00                                  callq   0
+ELF-x86-64:                           12: R_X86_64_PC32 puts-4-P
+ELF-x86-64:       16:   30 c0                                           xorb    %al, %al
+ELF-x86-64:       18:   e8 00 00 00 00                                  callq   0
+ELF-x86-64:                           19: R_X86_64_PC32 SomeOtherFunction-4-P
+ELF-x86-64:       1d:   8b 44 24 04                                     movl    4(%rsp), %eax
+ELF-x86-64:       21:   48 83 c4 08                                     addq    $8, %rsp
+ELF-x86-64:       25:   c3                                              retq
Index: tools/llvm-objdump/llvm-objdump.cpp
===================================================================
--- tools/llvm-objdump/llvm-objdump.cpp
+++ tools/llvm-objdump/llvm-objdump.cpp
@@ -382,7 +382,19 @@
   }
 
 
+  // Create a mapping, S2 = SectionRelocMap[S1], where section S2
+  // contains the relocations for section S1.
   error_code ec;
+  std::map<SectionRef, SectionRef> SectionRelocMap;
+  for (section_iterator i = Obj->begin_sections(),
+                        e = Obj->end_sections();
+                        i != e; i.increment(ec)) {
+    if (error(ec)) break;
+    section_iterator Sec2 = i->getRelocatedSection();
+    if (Sec2 != Obj->end_sections())
+      SectionRelocMap.insert(std::pair<SectionRef, SectionRef>(*Sec2, *i));
+  }
+
   for (section_iterator i = Obj->begin_sections(),
                         e = Obj->end_sections();
                         i != e; i.increment(ec)) {
@@ -418,8 +430,9 @@
     // Make a list of all the relocations for this section.
     std::vector<RelocationRef> Rels;
     if (InlineRelocs) {
-      for (relocation_iterator ri = i->begin_relocations(),
-                               re = i->end_relocations();
+      SectionRef RelocSec = SectionRelocMap[*i];
+      for (relocation_iterator ri = RelocSec.begin_relocations(),
+                               re = RelocSec.end_relocations();
                                ri != re; ri.increment(ec)) {
         if (error(ec)) break;
         Rels.push_back(*ri);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2559.1.patch
Type: text/x-patch
Size: 4703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140115/28d04661/attachment.bin>


More information about the llvm-commits mailing list