summaryrefslogtreecommitdiff
path: root/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
blob: 995984b0361646194921bca410268abf5acb9695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file handles Wasm-specific object emission, converting LLVM's
/// internal fixups into the appropriate relocations.
///
//===----------------------------------------------------------------------===//

#include "MCTargetDesc/WebAssemblyFixupKinds.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCSymbolWasm.h"
#include "llvm/MC/MCWasmObjectWriter.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"

using namespace llvm;

namespace {
class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
public:
  explicit WebAssemblyWasmObjectWriter(bool Is64Bit);

private:
  unsigned getRelocType(const MCValue &Target,
                        const MCFixup &Fixup) const override;
};
} // end anonymous namespace

WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
    : MCWasmObjectTargetWriter(Is64Bit) {}

// Test whether the given expression computes a function address.
static bool IsFunctionExpr(const MCExpr *Expr) {
  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
    return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();

  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
    return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());

  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
    return IsFunctionExpr(UnOp->getSubExpr());

  return false;
}

static bool IsFunctionType(const MCValue &Target) {
  const MCSymbolRefExpr *RefA = Target.getSymA();
  return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
}

unsigned
WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
                                          const MCFixup &Fixup) const {
  // WebAssembly functions are not allocated in the data address space. To
  // resolve a pointer to a function, we must use a special relocation type.
  bool IsFunction = IsFunctionExpr(Fixup.getValue());

  switch (unsigned(Fixup.getKind())) {
  case WebAssembly::fixup_code_global_index:
    return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
  case WebAssembly::fixup_code_sleb128_i32:
    if (IsFunction)
      return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
    return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
  case WebAssembly::fixup_code_sleb128_i64:
    llvm_unreachable("fixup_sleb128_i64 not implemented yet");
  case WebAssembly::fixup_code_uleb128_i32:
    if (IsFunctionType(Target))
      return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
    if (IsFunction)
      return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
    return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
  case FK_Data_4:
    if (IsFunction)
      return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
    return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
  case FK_Data_8:
    llvm_unreachable("FK_Data_8 not implemented yet");
  default:
    llvm_unreachable("unimplemented fixup kind");
  }
}

MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
                                                        bool Is64Bit) {
  MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
  return createWasmObjectWriter(MOTW, OS);
}