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