blob: 19e14f3261aa7d1ca0c954d66a782d5c73f668a1 [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/Wasm.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000019#include "llvm/MC/MCAsmBackend.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000020#include "llvm/MC/MCFixup.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000021#include "llvm/MC/MCFixupKindInfo.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000022#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000023#include "llvm/MC/MCWasmObjectWriter.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000024#include "llvm/MC/MCValue.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000025#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000026#include "llvm/Support/ErrorHandling.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000027
Dan Gohman18eafb62017-02-22 01:23:18 +000028using namespace llvm;
29
30namespace {
31class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
32public:
33 explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
34
35private:
Sam Cleggae03c1e72017-06-13 18:51:50 +000036 unsigned getRelocType(const MCValue &Target,
37 const MCFixup &Fixup) const override;
Dan Gohman18eafb62017-02-22 01:23:18 +000038};
39} // end anonymous namespace
40
41WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
42 : MCWasmObjectTargetWriter(Is64Bit) {}
43
Dan Gohmand934cb82017-02-24 23:18:00 +000044// Test whether the given expression computes a function address.
45static bool IsFunctionExpr(const MCExpr *Expr) {
Sam Cleggae03c1e72017-06-13 18:51:50 +000046 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000047 return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
48
Sam Cleggae03c1e72017-06-13 18:51:50 +000049 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000050 return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
51
Sam Cleggae03c1e72017-06-13 18:51:50 +000052 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000053 return IsFunctionExpr(UnOp->getSubExpr());
54
55 return false;
56}
57
Sam Cleggacd7d2b2017-06-06 19:15:05 +000058static bool IsFunctionType(const MCValue &Target) {
59 const MCSymbolRefExpr *RefA = Target.getSymA();
60 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
61}
62
Sam Cleggae03c1e72017-06-13 18:51:50 +000063unsigned
64WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
65 const MCFixup &Fixup) 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
Dan Gohmand934cb82017-02-24 23:18:00 +000070 switch (unsigned(Fixup.getKind())) {
71 case WebAssembly::fixup_code_sleb128_i32:
72 if (IsFunction)
73 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
74 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB;
75 case WebAssembly::fixup_code_sleb128_i64:
76 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
77 case WebAssembly::fixup_code_uleb128_i32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +000078 if (IsFunctionType(Target))
79 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000080 if (IsFunction)
81 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
82 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB;
83 case FK_Data_4:
84 if (IsFunction)
85 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
86 return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32;
87 case FK_Data_8:
88 llvm_unreachable("FK_Data_8 not implemented yet");
89 default:
90 llvm_unreachable("unimplemented fixup kind");
91 }
Dan Gohman18eafb62017-02-22 01:23:18 +000092}
93
94MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
95 bool Is64Bit) {
96 MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
97 return createWasmObjectWriter(MOTW, OS);
98}