2017-02-22 09:23:18 +08:00
|
|
|
//===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file handles Wasm-specific object emission, converting LLVM's
|
2017-02-22 09:23:18 +08:00
|
|
|
/// internal fixups into the appropriate relocations.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyFixupKinds.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Wasm.h"
|
2017-06-14 02:51:50 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2017-06-14 02:51:50 +08:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
2017-10-11 01:31:43 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2018-04-27 03:27:28 +08:00
|
|
|
#include "llvm/MC/MCSectionWasm.h"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "llvm/MC/MCSymbolWasm.h"
|
2017-06-14 02:51:50 +08:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2018-04-27 03:27:28 +08:00
|
|
|
#include "llvm/MC/MCWasmObjectWriter.h"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-06-14 02:51:50 +08:00
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
|
|
|
|
public:
|
|
|
|
explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
|
|
|
|
|
|
|
|
private:
|
2017-06-14 02:51:50 +08:00
|
|
|
unsigned getRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup) const override;
|
2017-02-22 09:23:18 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
|
|
|
|
: MCWasmObjectTargetWriter(Is64Bit) {}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Test whether the given expression computes a function address.
|
|
|
|
static bool IsFunctionExpr(const MCExpr *Expr) {
|
2017-06-14 02:51:50 +08:00
|
|
|
if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
|
2017-02-25 07:18:00 +08:00
|
|
|
return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
|
|
|
|
|
2017-06-14 02:51:50 +08:00
|
|
|
if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
|
2017-02-25 07:18:00 +08:00
|
|
|
return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
|
|
|
|
|
2017-06-14 02:51:50 +08:00
|
|
|
if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
|
2017-02-25 07:18:00 +08:00
|
|
|
return IsFunctionExpr(UnOp->getSubExpr());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
static bool IsFunctionType(const MCValue &Target) {
|
|
|
|
const MCSymbolRefExpr *RefA = Target.getSymA();
|
|
|
|
return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
|
|
|
|
}
|
|
|
|
|
2018-04-27 03:27:28 +08:00
|
|
|
static const MCSection *GetFixupSection(const MCExpr *Expr) {
|
|
|
|
if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
|
|
|
|
if (SyExp->getSymbol().isInSection())
|
|
|
|
return &SyExp->getSymbol().getSection();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
|
|
|
|
auto SectionLHS = GetFixupSection(BinOp->getLHS());
|
|
|
|
auto SectionRHS = GetFixupSection(BinOp->getRHS());
|
|
|
|
return SectionLHS == SectionRHS ? nullptr : SectionLHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
|
|
|
|
return GetFixupSection(UnOp->getSubExpr());
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:33:37 +08:00
|
|
|
static bool IsGlobalType(const MCValue &Target) {
|
|
|
|
const MCSymbolRefExpr *RefA = Target.getSymA();
|
|
|
|
return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_GLOBAL;
|
|
|
|
}
|
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup) const {
|
2017-02-25 07:18:00 +08:00
|
|
|
// WebAssembly functions are not allocated in the data address space. To
|
|
|
|
// resolve a pointer to a function, we must use a special relocation type.
|
|
|
|
bool IsFunction = IsFunctionExpr(Fixup.getValue());
|
|
|
|
|
|
|
|
switch (unsigned(Fixup.getKind())) {
|
|
|
|
case WebAssembly::fixup_code_sleb128_i32:
|
|
|
|
if (IsFunction)
|
|
|
|
return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
|
2017-09-02 01:32:01 +08:00
|
|
|
return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
|
2017-02-25 07:18:00 +08:00
|
|
|
case WebAssembly::fixup_code_sleb128_i64:
|
|
|
|
llvm_unreachable("fixup_sleb128_i64 not implemented yet");
|
|
|
|
case WebAssembly::fixup_code_uleb128_i32:
|
2018-08-03 22:33:37 +08:00
|
|
|
if (IsGlobalType(Target))
|
|
|
|
return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
|
2017-06-07 03:15:05 +08:00
|
|
|
if (IsFunctionType(Target))
|
|
|
|
return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
|
2017-02-25 07:18:00 +08:00
|
|
|
if (IsFunction)
|
|
|
|
return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
|
2017-09-02 01:32:01 +08:00
|
|
|
return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
|
2017-02-25 07:18:00 +08:00
|
|
|
case FK_Data_4:
|
|
|
|
if (IsFunction)
|
|
|
|
return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
|
2018-04-27 03:27:28 +08:00
|
|
|
if (auto Section = static_cast<const MCSectionWasm *>(
|
|
|
|
GetFixupSection(Fixup.getValue()))) {
|
|
|
|
if (Section->getKind().isText())
|
|
|
|
return wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32;
|
|
|
|
else if (!Section->isWasmData())
|
|
|
|
return wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32;
|
|
|
|
}
|
2017-09-02 01:32:01 +08:00
|
|
|
return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
|
2017-02-25 07:18:00 +08:00
|
|
|
case FK_Data_8:
|
|
|
|
llvm_unreachable("FK_Data_8 not implemented yet");
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unimplemented fixup kind");
|
|
|
|
}
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:20:29 +08:00
|
|
|
std::unique_ptr<MCObjectTargetWriter>
|
|
|
|
llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) {
|
|
|
|
return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|