]> granicus.if.org Git - llvm/commitdiff
[WebAssembly] Add support for printing relocations with llvm-objdump
authorSam Clegg <sbc@chromium.org>
Tue, 27 Jun 2017 20:40:53 +0000 (20:40 +0000)
committerSam Clegg <sbc@chromium.org>
Tue, 27 Jun 2017 20:40:53 +0000 (20:40 +0000)
Differential Revision: https://reviews.llvm.org/D34658

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306461 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/Wasm.h
lib/Object/WasmObjectFile.cpp
test/tools/llvm-objdump/WebAssembly/relocations.test [new file with mode: 0644]
tools/llvm-objdump/llvm-objdump.cpp

index cfad7e2c3670b3c6b5a00947c4b85454f7ed92df..5c8445f10f44815545b250b9ec2ce2e035d70c1f 100644 (file)
@@ -215,6 +215,7 @@ private:
   std::vector<WasmSymbol> Symbols;
   ArrayRef<uint8_t> CodeSection;
   uint32_t StartFunction = -1;
+  bool HasLinkingSection = false;
   wasm::WasmLinkingData LinkingData;
 
   StringMap<uint32_t> SymbolMap;
index e87dd48cb01f72fa5442f473657b2955a4adc2b6..fff497ba5564773de6412b8e80a6e169436a4a46 100644 (file)
@@ -294,6 +294,7 @@ Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
 
 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);
@@ -950,7 +951,9 @@ SubtargetFeatures WasmObjectFile::getFeatures() const {
   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());
diff --git a/test/tools/llvm-objdump/WebAssembly/relocations.test b/test/tools/llvm-objdump/WebAssembly/relocations.test
new file mode 100644 (file)
index 0000000..07a167c
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: llc -mtriple=wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s
+
+@foo1 = hidden global i32 1, align 4
+@foo2 = hidden global i32 1, align 4
+@bar = hidden global i32* @foo2, align 4
+
+; CHECK:      RELOCATION RECORDS FOR [DATA]:
+; CHECK-NEXT: 0000000e R_WEBASSEMBLY_GLOBAL_ADDR_I32 1+0
index 6a91515709089a5ea663ad1c92f628fa37997409..be5635a3d4c69129f16b443dc1c35cda583e48b3 100644 (file)
@@ -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 @@ static void printRelocationTargetName(const MachOObjectFile *O,
   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 @@ static std::error_code getRelocationValueString(const RelocationRef &Rel,
     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