blob: 39abde26df7fa6e189bf8ea864a86abf7eba7685 [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"
Derek Schuff669300d2017-10-10 17:31:43 +000022#include "llvm/MC/MCObjectWriter.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000023#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000024#include "llvm/MC/MCWasmObjectWriter.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000025#include "llvm/MC/MCValue.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000026#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000027#include "llvm/Support/ErrorHandling.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000028
Dan Gohman18eafb62017-02-22 01:23:18 +000029using namespace llvm;
30
31namespace {
32class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
33public:
34 explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
35
36private:
Sam Cleggae03c1e72017-06-13 18:51:50 +000037 unsigned getRelocType(const MCValue &Target,
38 const MCFixup &Fixup) const override;
Dan Gohman18eafb62017-02-22 01:23:18 +000039};
40} // end anonymous namespace
41
42WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
43 : MCWasmObjectTargetWriter(Is64Bit) {}
44
Dan Gohmand934cb82017-02-24 23:18:00 +000045// Test whether the given expression computes a function address.
46static bool IsFunctionExpr(const MCExpr *Expr) {
Sam Cleggae03c1e72017-06-13 18:51:50 +000047 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000048 return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
49
Sam Cleggae03c1e72017-06-13 18:51:50 +000050 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000051 return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
52
Sam Cleggae03c1e72017-06-13 18:51:50 +000053 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000054 return IsFunctionExpr(UnOp->getSubExpr());
55
56 return false;
57}
58
Sam Cleggacd7d2b2017-06-06 19:15:05 +000059static bool IsFunctionType(const MCValue &Target) {
60 const MCSymbolRefExpr *RefA = Target.getSymA();
61 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
62}
63
Sam Cleggae03c1e72017-06-13 18:51:50 +000064unsigned
65WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
66 const MCFixup &Fixup) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000067 // WebAssembly functions are not allocated in the data address space. To
68 // resolve a pointer to a function, we must use a special relocation type.
69 bool IsFunction = IsFunctionExpr(Fixup.getValue());
70
Dan Gohmand934cb82017-02-24 23:18:00 +000071 switch (unsigned(Fixup.getKind())) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000072 case WebAssembly::fixup_code_global_index:
73 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000074 case WebAssembly::fixup_code_sleb128_i32:
75 if (IsFunction)
76 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
Sam Clegg13a2e892017-09-01 17:32:01 +000077 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000078 case WebAssembly::fixup_code_sleb128_i64:
79 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
80 case WebAssembly::fixup_code_uleb128_i32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +000081 if (IsFunctionType(Target))
82 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000083 if (IsFunction)
84 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
Sam Clegg13a2e892017-09-01 17:32:01 +000085 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000086 case FK_Data_4:
87 if (IsFunction)
88 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
Sam Clegg13a2e892017-09-01 17:32:01 +000089 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
Dan Gohmand934cb82017-02-24 23:18:00 +000090 case FK_Data_8:
91 llvm_unreachable("FK_Data_8 not implemented yet");
92 default:
93 llvm_unreachable("unimplemented fixup kind");
94 }
Dan Gohman18eafb62017-02-22 01:23:18 +000095}
96
Derek Schuff669300d2017-10-10 17:31:43 +000097std::unique_ptr<MCObjectWriter>
98llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
99 bool Is64Bit) {
Lang Hames1301a872017-10-10 01:15:10 +0000100 auto MOTW = llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
101 return createWasmObjectWriter(std::move(MOTW), OS);
Dan Gohman18eafb62017-02-22 01:23:18 +0000102}