blob: 995984b0361646194921bca410268abf5acb9695 [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())) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000071 case WebAssembly::fixup_code_global_index:
72 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000073 case WebAssembly::fixup_code_sleb128_i32:
74 if (IsFunction)
75 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
Sam Clegg13a2e892017-09-01 17:32:01 +000076 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000077 case WebAssembly::fixup_code_sleb128_i64:
78 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
79 case WebAssembly::fixup_code_uleb128_i32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +000080 if (IsFunctionType(Target))
81 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000082 if (IsFunction)
83 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
Sam Clegg13a2e892017-09-01 17:32:01 +000084 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000085 case FK_Data_4:
86 if (IsFunction)
87 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
Sam Clegg13a2e892017-09-01 17:32:01 +000088 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
Dan Gohmand934cb82017-02-24 23:18:00 +000089 case FK_Data_8:
90 llvm_unreachable("FK_Data_8 not implemented yet");
91 default:
92 llvm_unreachable("unimplemented fixup kind");
93 }
Dan Gohman18eafb62017-02-22 01:23:18 +000094}
95
96MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
97 bool Is64Bit) {
98 MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
99 return createWasmObjectWriter(MOTW, OS);
100}