[PATCH] D15965: Add support for dumping relocations in non-relocatable files

Colin LeMahieu via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 12:20:07 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL263971: [llvm-objdump] Printing relocations in executable and shared object files. (authored by colinl).

Changed prior to commit:
  http://reviews.llvm.org/D15965?vs=50459&id=51211#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D15965

Files:
  llvm/trunk/include/llvm/Object/ELFObjectFile.h
  llvm/trunk/test/Object/objdump-reloc-shared.test
  llvm/trunk/test/tools/llvm-objdump/X86/Inputs/relocations-in-nonrelocatable.elf-x86_64
  llvm/trunk/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test
  llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Index: llvm/trunk/include/llvm/Object/ELFObjectFile.h
===================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h
@@ -640,9 +640,6 @@
 template <class ELFT>
 section_iterator
 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
-  if (EF.getHeader()->e_type != ELF::ET_REL)
-    return section_end();
-
   const Elf_Shdr *EShdr = getSection(Sec);
   uintX_t Type = EShdr->sh_type;
   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
@@ -682,14 +679,20 @@
 }
 
 template <class ELFT>
-uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
-  assert(EF.getHeader()->e_type == ELF::ET_REL &&
-         "Only relocatable object files have relocation offsets");
-  const Elf_Shdr *sec = getRelSection(Rel);
-  if (sec->sh_type == ELF::SHT_REL)
-    return getRel(Rel)->r_offset;
-
-  return getRela(Rel)->r_offset;
+uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
+  const Elf_Shdr *sec = getRelSection(Rel);
+  uint64_t Offset = sec->sh_type == ELF::SHT_REL ?
+                    getRel(Rel)->r_offset :
+                    getRela(Rel)->r_offset;
+  if (EF.getHeader()->e_type == ELF::ET_EXEC ||
+      EF.getHeader()->e_type == ELF::ET_DYN) {
+    // For an executable file or a shared object, the value is the virtual
+    // address of the storage unit affected by the relocation.
+    auto SectionIter = getRelocatedSection(toDRI(sec));
+    if (SectionIter != section_end())
+      Offset -= SectionIter->getAddress();
+  }
+  return Offset;
 }
 
 template <class ELFT>
Index: llvm/trunk/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test
===================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test
+++ llvm/trunk/test/tools/llvm-objdump/X86/relocations-in-nonrelocatable.test
@@ -0,0 +1,9 @@
+// This test checks that relocation in nonrelocatable files are printed
+// RUN: llvm-objdump -r %p/Inputs/relocations-in-nonrelocatable.elf-x86_64 | FileCheck %s
+
+// (main.c)
+// void g(void){}
+// int main(void) { g(); };
+// gcc main.c -o main -Wl,--emit-relocs
+
+CHECK: 00000000000000f8 R_X86_64_PC32 g-4-P
\ No newline at end of file
Index: llvm/trunk/test/Object/objdump-reloc-shared.test
===================================================================
--- llvm/trunk/test/Object/objdump-reloc-shared.test
+++ llvm/trunk/test/Object/objdump-reloc-shared.test
@@ -1,5 +0,0 @@
-RUN: llvm-objdump -r %p/Inputs/elf-reloc-no-sym.x86_64 \
-RUN:              | FileCheck %s
-
-; CHECK: elf-reloc-no-sym.x86_64:       file format ELF64-x86-64
-; CHECK-NOT: {{.}}
Index: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
===================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
@@ -1188,10 +1188,6 @@
 void llvm::PrintRelocations(const ObjectFile *Obj) {
   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
                                                  "%08" PRIx64;
-  // Regular objdump doesn't print relocations in non-relocatable object
-  // files.
-  if (!Obj->isRelocatableObject())
-    return;
 
   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     if (Section.relocation_begin() == Section.relocation_end())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15965.51211.patch
Type: text/x-patch
Size: 3456 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160321/91038b69/attachment.bin>


More information about the llvm-commits mailing list