Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyELFObjectWriter.cpp - WebAssembly ELF 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 ELF-specific object emission, converting LLVM's |
| 12 | /// internal fixups into the appropriate relocations. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 17 | #include "llvm/MC/MCELFObjectWriter.h" |
| 18 | #include "llvm/MC/MCFixup.h" |
Derek Schuff | 669300d | 2017-10-10 17:31:43 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCObjectWriter.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | class WebAssemblyELFObjectWriter final : public MCELFObjectTargetWriter { |
| 25 | public: |
| 26 | WebAssemblyELFObjectWriter(bool Is64Bit, uint8_t OSABI); |
| 27 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 28 | protected: |
JF Bastien | 664fd46 | 2016-01-13 23:36:00 +0000 | [diff] [blame] | 29 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 30 | const MCFixup &Fixup, bool IsPCRel) const override; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 31 | }; |
| 32 | } // end anonymous namespace |
| 33 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 34 | WebAssemblyELFObjectWriter::WebAssemblyELFObjectWriter(bool Is64Bit, |
| 35 | uint8_t OSABI) |
Dan Gohman | 4635017 | 2016-01-12 20:56:01 +0000 | [diff] [blame] | 36 | : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_WEBASSEMBLY, |
| 37 | /*HasRelocationAddend=*/false) {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 38 | |
JF Bastien | 664fd46 | 2016-01-13 23:36:00 +0000 | [diff] [blame] | 39 | unsigned WebAssemblyELFObjectWriter::getRelocType(MCContext &Ctx, |
| 40 | const MCValue &Target, |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 41 | const MCFixup &Fixup, |
| 42 | bool IsPCRel) const { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 43 | // WebAssembly functions are not allocated in the address space. To resolve a |
| 44 | // pointer to a function, we must use a special relocation type. |
| 45 | if (const MCSymbolRefExpr *SyExp = |
| 46 | dyn_cast<MCSymbolRefExpr>(Fixup.getValue())) |
| 47 | if (SyExp->getKind() == MCSymbolRefExpr::VK_WebAssembly_FUNCTION) |
| 48 | return ELF::R_WEBASSEMBLY_FUNCTION; |
| 49 | |
| 50 | switch (Fixup.getKind()) { |
| 51 | case FK_Data_4: |
| 52 | assert(!is64Bit() && "4-byte relocations only supported on wasm32"); |
| 53 | return ELF::R_WEBASSEMBLY_DATA; |
| 54 | case FK_Data_8: |
| 55 | assert(is64Bit() && "8-byte relocations only supported on wasm64"); |
| 56 | return ELF::R_WEBASSEMBLY_DATA; |
| 57 | default: |
| 58 | llvm_unreachable("unimplemented fixup kind"); |
| 59 | } |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Derek Schuff | 669300d | 2017-10-10 17:31:43 +0000 | [diff] [blame] | 62 | std::unique_ptr<MCObjectWriter> |
| 63 | llvm::createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS, |
| 64 | bool Is64Bit, |
| 65 | uint8_t OSABI) { |
Reid Kleckner | a11b983 | 2017-10-10 00:52:40 +0000 | [diff] [blame] | 66 | auto MOTW = llvm::make_unique<WebAssemblyELFObjectWriter>(Is64Bit, OSABI); |
| 67 | return createELFObjectWriter(std::move(MOTW), OS, /*IsLittleEndian=*/true); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 68 | } |