LLVM  6.0.0svn
WebAssemblyELFObjectWriter.cpp
Go to the documentation of this file.
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 
18 #include "llvm/MC/MCFixup.h"
19 #include "llvm/MC/MCObjectWriter.h"
21 using namespace llvm;
22 
23 namespace {
24 class WebAssemblyELFObjectWriter final : public MCELFObjectTargetWriter {
25 public:
26  WebAssemblyELFObjectWriter(bool Is64Bit, uint8_t OSABI);
27 
28 protected:
29  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30  const MCFixup &Fixup, bool IsPCRel) const override;
31 };
32 } // end anonymous namespace
33 
34 WebAssemblyELFObjectWriter::WebAssemblyELFObjectWriter(bool Is64Bit,
35  uint8_t OSABI)
36  : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_WEBASSEMBLY,
37  /*HasRelocationAddend=*/false) {}
38 
40  const MCValue &Target,
41  const MCFixup &Fixup,
42  bool IsPCRel) const {
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  }
60 }
61 
62 std::unique_ptr<MCObjectWriter>
64  bool Is64Bit,
65  uint8_t OSABI) {
66  auto MOTW = llvm::make_unique<WebAssemblyELFObjectWriter>(Is64Bit, OSABI);
67  return createELFObjectWriter(std::move(MOTW), OS, /*IsLittleEndian=*/true);
68 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:59
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
static bool is64Bit(const char *name)
This file provides WebAssembly-specific target descriptions.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::unique_ptr< MCObjectWriter > createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, uint8_t OSABI)
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getValue() const
Definition: MCFixup.h:98
MCFixupKind getKind() const
Definition: MCFixup.h:93