blob: 1eb1308c2b832534fb2905efe31640c92e6a65c8 [file] [log] [blame]
Dan Gohman18eafb62017-02-22 01:23:18 +00001//===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman18eafb62017-02-22 01:23:18 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file handles Wasm-specific object emission, converting LLVM's
Dan Gohman18eafb62017-02-22 01:23:18 +000011/// internal fixups into the appropriate relocations.
12///
13//===----------------------------------------------------------------------===//
14
Dan Gohmand934cb82017-02-24 23:18:00 +000015#include "MCTargetDesc/WebAssemblyFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/Wasm.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000018#include "llvm/MC/MCAsmBackend.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000019#include "llvm/MC/MCFixup.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000020#include "llvm/MC/MCFixupKindInfo.h"
Derek Schuff669300d2017-10-10 17:31:43 +000021#include "llvm/MC/MCObjectWriter.h"
Sam Clegg6a31a0d2018-04-26 19:27:28 +000022#include "llvm/MC/MCSectionWasm.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000023#include "llvm/MC/MCSymbolWasm.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000024#include "llvm/MC/MCValue.h"
Sam Clegg6a31a0d2018-04-26 19:27:28 +000025#include "llvm/MC/MCWasmObjectWriter.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 Clegg6a31a0d2018-04-26 19:27:28 +000064static const MCSection *GetFixupSection(const MCExpr *Expr) {
65 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
66 if (SyExp->getSymbol().isInSection())
67 return &SyExp->getSymbol().getSection();
68 return nullptr;
69 }
70
71 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
72 auto SectionLHS = GetFixupSection(BinOp->getLHS());
73 auto SectionRHS = GetFixupSection(BinOp->getRHS());
74 return SectionLHS == SectionRHS ? nullptr : SectionLHS;
75 }
76
77 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
78 return GetFixupSection(UnOp->getSubExpr());
79
80 return nullptr;
81}
82
Nicholas Wilsone408a892018-08-03 14:33:37 +000083static bool IsGlobalType(const MCValue &Target) {
84 const MCSymbolRefExpr *RefA = Target.getSymA();
85 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_GLOBAL;
86}
87
Heejin Ahnda419bd2018-11-14 02:46:21 +000088static bool IsEventType(const MCValue &Target) {
89 const MCSymbolRefExpr *RefA = Target.getSymA();
90 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_EVENT;
91}
92
Heejin Ahnf208f632018-09-05 01:27:38 +000093unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
94 const MCFixup &Fixup) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000095 // WebAssembly functions are not allocated in the data address space. To
96 // resolve a pointer to a function, we must use a special relocation type.
97 bool IsFunction = IsFunctionExpr(Fixup.getValue());
98
Dan Gohmand934cb82017-02-24 23:18:00 +000099 switch (unsigned(Fixup.getKind())) {
100 case WebAssembly::fixup_code_sleb128_i32:
101 if (IsFunction)
102 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
Sam Clegg13a2e892017-09-01 17:32:01 +0000103 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000104 case WebAssembly::fixup_code_sleb128_i64:
105 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
106 case WebAssembly::fixup_code_uleb128_i32:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000107 if (IsGlobalType(Target))
108 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000109 if (IsFunctionType(Target))
110 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000111 if (IsFunction)
112 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000113 if (IsEventType(Target))
114 return wasm::R_WEBASSEMBLY_EVENT_INDEX_LEB;
Sam Clegg13a2e892017-09-01 17:32:01 +0000115 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000116 case FK_Data_4:
117 if (IsFunction)
118 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000119 if (auto Section = static_cast<const MCSectionWasm *>(
120 GetFixupSection(Fixup.getValue()))) {
121 if (Section->getKind().isText())
122 return wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32;
123 else if (!Section->isWasmData())
124 return wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32;
125 }
Sam Clegg13a2e892017-09-01 17:32:01 +0000126 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000127 case FK_Data_8:
128 llvm_unreachable("FK_Data_8 not implemented yet");
129 default:
130 llvm_unreachable("unimplemented fixup kind");
131 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000132}
133
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000134std::unique_ptr<MCObjectTargetWriter>
135llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) {
136 return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
Dan Gohman18eafb62017-02-22 01:23:18 +0000137}