[WebAssembly] Define WebAssembly-specific relocation codes.
[oota-llvm.git] / lib / Target / WebAssembly / MCTargetDesc / WebAssemblyELFObjectWriter.cpp
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
27 protected:
28   unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
29                         bool IsPCRel) const override;
30 };
31 } // end anonymous namespace
32
33 // FIXME: Use EM_NONE as a temporary hack. Should we decide to pursue ELF
34 // writing seriously, we should email generic-abi@googlegroups.com and ask
35 // for our own ELF code.
36 WebAssemblyELFObjectWriter::WebAssemblyELFObjectWriter(bool Is64Bit,
37                                                        uint8_t OSABI)
38     : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_NONE,
39                               /*HasRelocationAddend=*/true) {}
40
41 unsigned WebAssemblyELFObjectWriter::GetRelocType(const MCValue &Target,
42                                                   const MCFixup &Fixup,
43                                                   bool IsPCRel) const {
44   // WebAssembly functions are not allocated in the address space. To resolve a
45   // pointer to a function, we must use a special relocation type.
46   if (const MCSymbolRefExpr *SyExp =
47           dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
48     if (SyExp->getKind() == MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
49       return ELF::R_WEBASSEMBLY_FUNCTION;
50
51   switch (Fixup.getKind()) {
52   case FK_Data_4:
53     assert(!is64Bit() && "4-byte relocations only supported on wasm32");
54     return ELF::R_WEBASSEMBLY_DATA;
55   case FK_Data_8:
56     assert(is64Bit() && "8-byte relocations only supported on wasm64");
57     return ELF::R_WEBASSEMBLY_DATA;
58   default:
59     llvm_unreachable("unimplemented fixup kind");
60   }
61 }
62
63 MCObjectWriter *llvm::createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS,
64                                                        bool Is64Bit,
65                                                        uint8_t OSABI) {
66   MCELFObjectTargetWriter *MOTW =
67       new WebAssemblyELFObjectWriter(Is64Bit, OSABI);
68   return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
69 }