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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file handles Wasm-specific object emission, converting LLVM's |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 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" |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSectionWasm.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbolWasm.h" |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCValue.h" |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCWasmObjectWriter.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Casting.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 29 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter { |
| 34 | public: |
| 35 | explicit WebAssemblyWasmObjectWriter(bool Is64Bit); |
| 36 | |
| 37 | private: |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 38 | unsigned getRelocType(const MCValue &Target, |
| 39 | const MCFixup &Fixup) const override; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 40 | }; |
| 41 | } // end anonymous namespace |
| 42 | |
| 43 | WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit) |
| 44 | : MCWasmObjectTargetWriter(Is64Bit) {} |
| 45 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 46 | // Test whether the given expression computes a function address. |
| 47 | static bool IsFunctionExpr(const MCExpr *Expr) { |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 48 | if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 49 | return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction(); |
| 50 | |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 51 | if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 52 | return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS()); |
| 53 | |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 54 | if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr)) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 55 | return IsFunctionExpr(UnOp->getSubExpr()); |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 60 | static bool IsFunctionType(const MCValue &Target) { |
| 61 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 62 | return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX; |
| 63 | } |
| 64 | |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 65 | static 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 Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 84 | static bool IsGlobalType(const MCValue &Target) { |
| 85 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 86 | return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_GLOBAL; |
| 87 | } |
| 88 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame^] | 89 | unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target, |
| 90 | const MCFixup &Fixup) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 91 | // WebAssembly functions are not allocated in the data address space. To |
| 92 | // resolve a pointer to a function, we must use a special relocation type. |
| 93 | bool IsFunction = IsFunctionExpr(Fixup.getValue()); |
| 94 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 95 | switch (unsigned(Fixup.getKind())) { |
| 96 | case WebAssembly::fixup_code_sleb128_i32: |
| 97 | if (IsFunction) |
| 98 | return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB; |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 99 | return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 100 | case WebAssembly::fixup_code_sleb128_i64: |
| 101 | llvm_unreachable("fixup_sleb128_i64 not implemented yet"); |
| 102 | case WebAssembly::fixup_code_uleb128_i32: |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 103 | if (IsGlobalType(Target)) |
| 104 | return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB; |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 105 | if (IsFunctionType(Target)) |
| 106 | return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 107 | if (IsFunction) |
| 108 | return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB; |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 109 | return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 110 | case FK_Data_4: |
| 111 | if (IsFunction) |
| 112 | return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 113 | if (auto Section = static_cast<const MCSectionWasm *>( |
| 114 | GetFixupSection(Fixup.getValue()))) { |
| 115 | if (Section->getKind().isText()) |
| 116 | return wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32; |
| 117 | else if (!Section->isWasmData()) |
| 118 | return wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32; |
| 119 | } |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 120 | return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 121 | case FK_Data_8: |
| 122 | llvm_unreachable("FK_Data_8 not implemented yet"); |
| 123 | default: |
| 124 | llvm_unreachable("unimplemented fixup kind"); |
| 125 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 128 | std::unique_ptr<MCObjectTargetWriter> |
| 129 | llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) { |
| 130 | return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 131 | } |