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