Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1 | //===-- 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 Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/WebAssemblyFixupKinds.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCFixup.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSymbolWasm.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCWasmObjectWriter.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Casting.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Wasm.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter { |
| 28 | public: |
| 29 | explicit WebAssemblyWasmObjectWriter(bool Is64Bit); |
| 30 | |
| 31 | private: |
| 32 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 33 | const MCFixup &Fixup, bool IsPCRel) const override; |
| 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
| 37 | WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit) |
| 38 | : MCWasmObjectTargetWriter(Is64Bit) {} |
| 39 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 40 | // Test whether the given expression computes a function address. |
| 41 | static bool IsFunctionExpr(const MCExpr *Expr) { |
| 42 | if (const MCSymbolRefExpr *SyExp = |
| 43 | dyn_cast<MCSymbolRefExpr>(Expr)) |
| 44 | return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction(); |
| 45 | |
| 46 | if (const MCBinaryExpr *BinOp = |
| 47 | dyn_cast<MCBinaryExpr>(Expr)) |
| 48 | return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS()); |
| 49 | |
| 50 | if (const MCUnaryExpr *UnOp = |
| 51 | dyn_cast<MCUnaryExpr>(Expr)) |
| 52 | return IsFunctionExpr(UnOp->getSubExpr()); |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame^] | 57 | static bool IsFunctionType(const MCValue &Target) { |
| 58 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 59 | return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX; |
| 60 | } |
| 61 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 62 | unsigned WebAssemblyWasmObjectWriter::getRelocType(MCContext &Ctx, |
| 63 | const MCValue &Target, |
| 64 | const MCFixup &Fixup, |
| 65 | bool IsPCRel) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 66 | // WebAssembly functions are not allocated in the data address space. To |
| 67 | // resolve a pointer to a function, we must use a special relocation type. |
| 68 | bool IsFunction = IsFunctionExpr(Fixup.getValue()); |
| 69 | |
| 70 | assert(!IsPCRel); |
| 71 | switch (unsigned(Fixup.getKind())) { |
| 72 | case WebAssembly::fixup_code_sleb128_i32: |
| 73 | if (IsFunction) |
| 74 | return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB; |
| 75 | return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB; |
| 76 | case WebAssembly::fixup_code_sleb128_i64: |
| 77 | llvm_unreachable("fixup_sleb128_i64 not implemented yet"); |
| 78 | case WebAssembly::fixup_code_uleb128_i32: |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame^] | 79 | if (IsFunctionType(Target)) |
| 80 | return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 81 | if (IsFunction) |
| 82 | return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB; |
| 83 | return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB; |
| 84 | case FK_Data_4: |
| 85 | if (IsFunction) |
| 86 | return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32; |
| 87 | return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32; |
| 88 | case FK_Data_8: |
| 89 | llvm_unreachable("FK_Data_8 not implemented yet"); |
| 90 | default: |
| 91 | llvm_unreachable("unimplemented fixup kind"); |
| 92 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS, |
| 96 | bool Is64Bit) { |
| 97 | MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit); |
| 98 | return createWasmObjectWriter(MOTW, OS); |
| 99 | } |