LLVM  4.0.0
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"
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(MCContext &Ctx, const MCValue &Target,
29  const MCFixup &Fixup, bool IsPCRel) const override;
30 };
31 } // end anonymous namespace
32 
33 WebAssemblyELFObjectWriter::WebAssemblyELFObjectWriter(bool Is64Bit,
34  uint8_t OSABI)
35  : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_WEBASSEMBLY,
36  /*HasRelocationAddend=*/false) {}
37 
39  const MCValue &Target,
40  const MCFixup &Fixup,
41  bool IsPCRel) const {
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  }
59 }
60 
62  bool Is64Bit,
63  uint8_t OSABI) {
65  new WebAssemblyELFObjectWriter(Is64Bit, OSABI);
66  return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
67 }
This represents an "assembler immediate".
Definition: MCValue.h:40
Defines the object file and target independent interfaces used by the assembler backend to write nati...
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:161
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:51
Function Alias Analysis false
const MCExpr * getValue() const
Definition: MCFixup.h:98
static bool is64Bit(const char *name)
This file provides WebAssembly-specific target descriptions.
MCFixupKind getKind() const
Definition: MCFixup.h:93
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
MCObjectWriter * createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, uint8_t OSABI)
Target - Wrapper for Target specific information.
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
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:333
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())