blob: 2846ec5e933773e0349d415e74bf91d2010d9998 [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
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000017#include "MCTargetDesc/WebAssemblyFixupKinds.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
Dan Gohman18eafb62017-02-22 01:23:18 +000057unsigned WebAssemblyWasmObjectWriter::getRelocType(MCContext &Ctx,
58 const MCValue &Target,
59 const MCFixup &Fixup,
60 bool IsPCRel) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000061 // WebAssembly functions are not allocated in the data address space. To
62 // resolve a pointer to a function, we must use a special relocation type.
63 bool IsFunction = IsFunctionExpr(Fixup.getValue());
64
65 assert(!IsPCRel);
66 switch (unsigned(Fixup.getKind())) {
67 case WebAssembly::fixup_code_sleb128_i32:
68 if (IsFunction)
69 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
70 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB;
71 case WebAssembly::fixup_code_sleb128_i64:
72 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
73 case WebAssembly::fixup_code_uleb128_i32:
74 if (IsFunction)
75 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
76 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB;
77 case FK_Data_4:
78 if (IsFunction)
79 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
80 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32;
81 case FK_Data_8:
82 llvm_unreachable("FK_Data_8 not implemented yet");
83 default:
84 llvm_unreachable("unimplemented fixup kind");
85 }
Dan Gohman18eafb62017-02-22 01:23:18 +000086}
87
88MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
89 bool Is64Bit) {
90 MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
91 return createWasmObjectWriter(MOTW, OS);
92}