blob: c773e57402e064b682f59615fb5e4b46e86ec260 [file] [log] [blame]
Dan Gohman18eafb62017-02-22 01:23:18 +00001//===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file handles Wasm-specific object emission, converting LLVM's
12/// internal fixups into the appropriate relocations.
13///
14//===----------------------------------------------------------------------===//
15
Dan Gohmand934cb82017-02-24 23:18:00 +000016#include "MCTargetDesc/WebAssemblyFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000018#include "llvm/MC/MCFixup.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000019#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000020#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000021#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000022#include "llvm/Support/ErrorHandling.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000023#include "llvm/Support/Wasm.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000024using namespace llvm;
25
26namespace {
27class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
28public:
29 explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
30
31private:
32 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33 const MCFixup &Fixup, bool IsPCRel) const override;
34};
35} // end anonymous namespace
36
37WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
38 : MCWasmObjectTargetWriter(Is64Bit) {}
39
Dan Gohmand934cb82017-02-24 23:18:00 +000040// Test whether the given expression computes a function address.
41static bool IsFunctionExpr(const MCExpr *Expr) {
42 if (const MCSymbolRefExpr *SyExp =
43 dyn_cast<MCSymbolRefExpr>(Expr))
44 return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
45
46 if (const MCBinaryExpr *BinOp =
47 dyn_cast<MCBinaryExpr>(Expr))
48 return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
49
50 if (const MCUnaryExpr *UnOp =
51 dyn_cast<MCUnaryExpr>(Expr))
52 return IsFunctionExpr(UnOp->getSubExpr());
53
54 return false;
55}
56
Sam Cleggacd7d2b2017-06-06 19:15:05 +000057static bool IsFunctionType(const MCValue &Target) {
58 const MCSymbolRefExpr *RefA = Target.getSymA();
59 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
60}
61
Dan Gohman18eafb62017-02-22 01:23:18 +000062unsigned WebAssemblyWasmObjectWriter::getRelocType(MCContext &Ctx,
63 const MCValue &Target,
64 const MCFixup &Fixup,
65 bool IsPCRel) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000066 // WebAssembly functions are not allocated in the data address space. To
67 // resolve a pointer to a function, we must use a special relocation type.
68 bool IsFunction = IsFunctionExpr(Fixup.getValue());
69
70 assert(!IsPCRel);
71 switch (unsigned(Fixup.getKind())) {
72 case WebAssembly::fixup_code_sleb128_i32:
73 if (IsFunction)
74 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
75 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB;
76 case WebAssembly::fixup_code_sleb128_i64:
77 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
78 case WebAssembly::fixup_code_uleb128_i32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +000079 if (IsFunctionType(Target))
80 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000081 if (IsFunction)
82 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
83 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB;
84 case FK_Data_4:
85 if (IsFunction)
86 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
87 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32;
88 case FK_Data_8:
89 llvm_unreachable("FK_Data_8 not implemented yet");
90 default:
91 llvm_unreachable("unimplemented fixup kind");
92 }
Dan Gohman18eafb62017-02-22 01:23:18 +000093}
94
95MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
96 bool Is64Bit) {
97 MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
98 return createWasmObjectWriter(MOTW, OS);
99}