blob: 763e30be8e02002f241450e1ccb8e967a37d9cb1 [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file handles Wasm-specific object emission, converting LLVM's
Dan Gohman18eafb62017-02-22 01:23:18 +000012/// 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"
Sam Clegg6a31a0d2018-04-26 19:27:28 +000023#include "llvm/MC/MCSectionWasm.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000024#include "llvm/MC/MCSymbolWasm.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000025#include "llvm/MC/MCValue.h"
Sam Clegg6a31a0d2018-04-26 19:27:28 +000026#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000027#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000028#include "llvm/Support/ErrorHandling.h"
Sam Cleggae03c1e72017-06-13 18:51:50 +000029
Dan Gohman18eafb62017-02-22 01:23:18 +000030using namespace llvm;
31
32namespace {
33class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
34public:
35 explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
36
37private:
Sam Cleggae03c1e72017-06-13 18:51:50 +000038 unsigned getRelocType(const MCValue &Target,
39 const MCFixup &Fixup) const override;
Dan Gohman18eafb62017-02-22 01:23:18 +000040};
41} // end anonymous namespace
42
43WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
44 : MCWasmObjectTargetWriter(Is64Bit) {}
45
Dan Gohmand934cb82017-02-24 23:18:00 +000046// Test whether the given expression computes a function address.
47static bool IsFunctionExpr(const MCExpr *Expr) {
Sam Cleggae03c1e72017-06-13 18:51:50 +000048 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000049 return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
50
Sam Cleggae03c1e72017-06-13 18:51:50 +000051 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000052 return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
53
Sam Cleggae03c1e72017-06-13 18:51:50 +000054 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
Dan Gohmand934cb82017-02-24 23:18:00 +000055 return IsFunctionExpr(UnOp->getSubExpr());
56
57 return false;
58}
59
Sam Cleggacd7d2b2017-06-06 19:15:05 +000060static bool IsFunctionType(const MCValue &Target) {
61 const MCSymbolRefExpr *RefA = Target.getSymA();
62 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
63}
64
Sam Clegg6a31a0d2018-04-26 19:27:28 +000065static const MCSection *GetFixupSection(const MCExpr *Expr) {
66 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
67 if (SyExp->getSymbol().isInSection())
68 return &SyExp->getSymbol().getSection();
69 return nullptr;
70 }
71
72 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
73 auto SectionLHS = GetFixupSection(BinOp->getLHS());
74 auto SectionRHS = GetFixupSection(BinOp->getRHS());
75 return SectionLHS == SectionRHS ? nullptr : SectionLHS;
76 }
77
78 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
79 return GetFixupSection(UnOp->getSubExpr());
80
81 return nullptr;
82}
83
Nicholas Wilsone408a892018-08-03 14:33:37 +000084static bool IsGlobalType(const MCValue &Target) {
85 const MCSymbolRefExpr *RefA = Target.getSymA();
86 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_GLOBAL;
87}
88
Heejin Ahnda419bd2018-11-14 02:46:21 +000089static bool IsEventType(const MCValue &Target) {
90 const MCSymbolRefExpr *RefA = Target.getSymA();
91 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_EVENT;
92}
93
Heejin Ahnf208f632018-09-05 01:27:38 +000094unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
95 const MCFixup &Fixup) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000096 // WebAssembly functions are not allocated in the data address space. To
97 // resolve a pointer to a function, we must use a special relocation type.
98 bool IsFunction = IsFunctionExpr(Fixup.getValue());
99
Dan Gohmand934cb82017-02-24 23:18:00 +0000100 switch (unsigned(Fixup.getKind())) {
101 case WebAssembly::fixup_code_sleb128_i32:
102 if (IsFunction)
103 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
Sam Clegg13a2e892017-09-01 17:32:01 +0000104 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000105 case WebAssembly::fixup_code_sleb128_i64:
106 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
107 case WebAssembly::fixup_code_uleb128_i32:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000108 if (IsGlobalType(Target))
109 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000110 if (IsFunctionType(Target))
111 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000112 if (IsFunction)
113 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000114 if (IsEventType(Target))
115 return wasm::R_WEBASSEMBLY_EVENT_INDEX_LEB;
Sam Clegg13a2e892017-09-01 17:32:01 +0000116 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +0000117 case FK_Data_4:
118 if (IsFunction)
119 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000120 if (auto Section = static_cast<const MCSectionWasm *>(
121 GetFixupSection(Fixup.getValue()))) {
122 if (Section->getKind().isText())
123 return wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32;
124 else if (!Section->isWasmData())
125 return wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32;
126 }
Sam Clegg13a2e892017-09-01 17:32:01 +0000127 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000128 case FK_Data_8:
129 llvm_unreachable("FK_Data_8 not implemented yet");
130 default:
131 llvm_unreachable("unimplemented fixup kind");
132 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000133}
134
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000135std::unique_ptr<MCObjectTargetWriter>
136llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) {
137 return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
Dan Gohman18eafb62017-02-22 01:23:18 +0000138}