[PATCH] D34658: [WebAssembly] Add support for printing relocations with llvm-objdump

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 27 12:30:14 PDT 2017


sbc100 updated this revision to Diff 104236.
sbc100 added a comment.

- No return after else


https://reviews.llvm.org/D34658

Files:
  include/llvm/Object/Wasm.h
  lib/Object/WasmObjectFile.cpp
  test/tools/llvm-objdump/WebAssembly/relocations.test
  tools/llvm-objdump/llvm-objdump.cpp


Index: tools/llvm-objdump/llvm-objdump.cpp
===================================================================
--- tools/llvm-objdump/llvm-objdump.cpp
+++ tools/llvm-objdump/llvm-objdump.cpp
@@ -41,6 +41,7 @@
 #include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/Wasm.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
@@ -886,6 +887,18 @@
   fmt << S;
 }
 
+static std::error_code getRelocationValueString(const WasmObjectFile *Obj,
+                                                const RelocationRef &RelRef,
+                                                SmallVectorImpl<char> &Result) {
+  const wasm::WasmRelocation& Rel = Obj->getWasmRelocation(RelRef);
+  std::string fmtbuf;
+  raw_string_ostream fmt(fmtbuf);
+  fmt << Rel.Index << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
+  fmt.flush();
+  Result.append(fmtbuf.begin(), fmtbuf.end());
+  return std::error_code();
+}
+
 static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
                                                 const RelocationRef &RelRef,
                                                 SmallVectorImpl<char> &Result) {
@@ -1071,8 +1084,11 @@
     return getRelocationValueString(ELF, Rel, Result);
   if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
     return getRelocationValueString(COFF, Rel, Result);
-  auto *MachO = cast<MachOObjectFile>(Obj);
-  return getRelocationValueString(MachO, Rel, Result);
+  if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
+    return getRelocationValueString(Wasm, Rel, Result);
+  if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
+    return getRelocationValueString(MachO, Rel, Result);
+  llvm_unreachable("unknown object file format");
 }
 
 /// @brief Indicates whether this relocation should hidden when listing
Index: test/tools/llvm-objdump/WebAssembly/relocations.test
===================================================================
--- /dev/null
+++ test/tools/llvm-objdump/WebAssembly/relocations.test
@@ -0,0 +1,7 @@
+; RUN: llc -mtriple=wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s
+
+ at foo = hidden global i32 1, align 4
+ at bar = hidden global i32* @foo, align 4
+
+; CHECK:      RELOCATION RECORDS FOR [DATA]:
+; CHECK-NEXT: 0000000a R_WEBASSEMBLY_GLOBAL_ADDR_I32 0+0
Index: lib/Object/WasmObjectFile.cpp
===================================================================
--- lib/Object/WasmObjectFile.cpp
+++ lib/Object/WasmObjectFile.cpp
@@ -291,6 +291,7 @@
 
 Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
                                           const uint8_t *End) {
+  HasLinkingSection = true;
   while (Ptr < End) {
     uint8_t Type = readVarint7(Ptr);
     uint32_t Size = readVaruint32(Ptr);
@@ -947,7 +948,9 @@
   return SubtargetFeatures();
 }
 
-bool WasmObjectFile::isRelocatableObject() const { return false; }
+bool WasmObjectFile::isRelocatableObject() const {
+  return HasLinkingSection;
+}
 
 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const {
   assert(Ref.d.a < Sections.size());
Index: include/llvm/Object/Wasm.h
===================================================================
--- include/llvm/Object/Wasm.h
+++ include/llvm/Object/Wasm.h
@@ -215,6 +215,7 @@
   std::vector<WasmSymbol> Symbols;
   ArrayRef<uint8_t> CodeSection;
   uint32_t StartFunction = -1;
+  bool HasLinkingSection = false;
   wasm::WasmLinkingData LinkingData;
 
   StringMap<uint32_t> SymbolMap;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34658.104236.patch
Type: text/x-patch
Size: 3575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170627/82d685b3/attachment.bin>


More information about the llvm-commits mailing list