blob: 1a9c714ed0f617264adde25a88fa91f507ce6684 [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
Sam Clegg8fffa1d2019-02-22 22:29:34 +000045static bool isFunctionSignatureRef(const MCSymbolRefExpr *Ref) {
46 return Ref->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
Sam Cleggacd7d2b2017-06-06 19:15:05 +000047}
48
Sam Clegg492f7522019-03-26 19:46:15 +000049static bool isGOTRef(const MCSymbolRefExpr *Ref) {
50 return Ref->getKind() == MCSymbolRefExpr::VK_GOT;
51}
52
Heejin Ahn18c56a02019-02-04 19:13:39 +000053static const MCSection *getFixupSection(const MCExpr *Expr) {
Sam Clegg6a31a0d2018-04-26 19:27:28 +000054 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
55 if (SyExp->getSymbol().isInSection())
56 return &SyExp->getSymbol().getSection();
57 return nullptr;
58 }
59
60 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
Heejin Ahn18c56a02019-02-04 19:13:39 +000061 auto SectionLHS = getFixupSection(BinOp->getLHS());
62 auto SectionRHS = getFixupSection(BinOp->getRHS());
Sam Clegg6a31a0d2018-04-26 19:27:28 +000063 return SectionLHS == SectionRHS ? nullptr : SectionLHS;
64 }
65
66 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
Heejin Ahn18c56a02019-02-04 19:13:39 +000067 return getFixupSection(UnOp->getSubExpr());
Sam Clegg6a31a0d2018-04-26 19:27:28 +000068
69 return nullptr;
70}
71
Heejin Ahnf208f632018-09-05 01:27:38 +000072unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
73 const MCFixup &Fixup) const {
Sam Clegg8fffa1d2019-02-22 22:29:34 +000074 const MCSymbolRefExpr *RefA = Target.getSymA();
75 assert(RefA);
76 auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
Dan Gohmand934cb82017-02-24 23:18:00 +000077
Dan Gohmand934cb82017-02-24 23:18:00 +000078 switch (unsigned(Fixup.getKind())) {
79 case WebAssembly::fixup_code_sleb128_i32:
Sam Clegg8fffa1d2019-02-22 22:29:34 +000080 if (SymA.isFunction())
Sam Cleggd1152a22019-02-04 17:28:46 +000081 return wasm::R_WASM_TABLE_INDEX_SLEB;
82 return wasm::R_WASM_MEMORY_ADDR_SLEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000083 case WebAssembly::fixup_code_sleb128_i64:
84 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
85 case WebAssembly::fixup_code_uleb128_i32:
Sam Clegg492f7522019-03-26 19:46:15 +000086 if (SymA.isGlobal() || isGOTRef(RefA))
87 return wasm::R_WASM_GLOBAL_INDEX_LEB;
Sam Clegg8fffa1d2019-02-22 22:29:34 +000088 if (SymA.isFunction()) {
89 if (isFunctionSignatureRef(RefA))
90 return wasm::R_WASM_TYPE_INDEX_LEB;
91 else
92 return wasm::R_WASM_FUNCTION_INDEX_LEB;
93 }
Sam Clegg8fffa1d2019-02-22 22:29:34 +000094 if (SymA.isEvent())
Sam Cleggd1152a22019-02-04 17:28:46 +000095 return wasm::R_WASM_EVENT_INDEX_LEB;
96 return wasm::R_WASM_MEMORY_ADDR_LEB;
Dan Gohmand934cb82017-02-24 23:18:00 +000097 case FK_Data_4:
Sam Clegg8fffa1d2019-02-22 22:29:34 +000098 if (SymA.isFunction())
Sam Cleggd1152a22019-02-04 17:28:46 +000099 return wasm::R_WASM_TABLE_INDEX_I32;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000100 if (auto Section = static_cast<const MCSectionWasm *>(
Heejin Ahn18c56a02019-02-04 19:13:39 +0000101 getFixupSection(Fixup.getValue()))) {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000102 if (Section->getKind().isText())
Sam Cleggd1152a22019-02-04 17:28:46 +0000103 return wasm::R_WASM_FUNCTION_OFFSET_I32;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000104 else if (!Section->isWasmData())
Sam Cleggd1152a22019-02-04 17:28:46 +0000105 return wasm::R_WASM_SECTION_OFFSET_I32;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000106 }
Sam Cleggd1152a22019-02-04 17:28:46 +0000107 return wasm::R_WASM_MEMORY_ADDR_I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000108 case FK_Data_8:
109 llvm_unreachable("FK_Data_8 not implemented yet");
110 default:
111 llvm_unreachable("unimplemented fixup kind");
112 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000113}
114
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000115std::unique_ptr<MCObjectTargetWriter>
116llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) {
117 return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
Dan Gohman18eafb62017-02-22 01:23:18 +0000118}