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