xref: /llvm-project/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp (revision 264b5d9e8817fce8c47d2b06aba2d9244e426794)
1 //===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm 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 Wasm-specific object emission, converting LLVM's
12 /// internal fixups into the appropriate relocations.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "MCTargetDesc/WebAssemblyFixupKinds.h"
17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCSymbolWasm.h"
21 #include "llvm/MC/MCWasmObjectWriter.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 using namespace llvm;
25 
26 namespace {
27 class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
28 public:
29   explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
30 
31 private:
32   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33                         const MCFixup &Fixup, bool IsPCRel) const override;
34 };
35 } // end anonymous namespace
36 
37 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
38     : MCWasmObjectTargetWriter(Is64Bit) {}
39 
40 // Test whether the given expression computes a function address.
41 static bool IsFunctionExpr(const MCExpr *Expr) {
42   if (const MCSymbolRefExpr *SyExp =
43           dyn_cast<MCSymbolRefExpr>(Expr))
44     return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
45 
46   if (const MCBinaryExpr *BinOp =
47           dyn_cast<MCBinaryExpr>(Expr))
48     return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
49 
50   if (const MCUnaryExpr *UnOp =
51           dyn_cast<MCUnaryExpr>(Expr))
52     return IsFunctionExpr(UnOp->getSubExpr());
53 
54   return false;
55 }
56 
57 static bool IsFunctionType(const MCValue &Target) {
58   const MCSymbolRefExpr *RefA = Target.getSymA();
59   return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
60 }
61 
62 unsigned WebAssemblyWasmObjectWriter::getRelocType(MCContext &Ctx,
63                                                    const MCValue &Target,
64                                                    const MCFixup &Fixup,
65                                                    bool IsPCRel) const {
66   // WebAssembly functions are not allocated in the data address space. To
67   // resolve a pointer to a function, we must use a special relocation type.
68   bool IsFunction = IsFunctionExpr(Fixup.getValue());
69 
70   assert(!IsPCRel);
71   switch (unsigned(Fixup.getKind())) {
72   case WebAssembly::fixup_code_sleb128_i32:
73     if (IsFunction)
74       return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
75     return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB;
76   case WebAssembly::fixup_code_sleb128_i64:
77     llvm_unreachable("fixup_sleb128_i64 not implemented yet");
78   case WebAssembly::fixup_code_uleb128_i32:
79     if (IsFunctionType(Target))
80       return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
81     if (IsFunction)
82       return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
83     return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB;
84   case FK_Data_4:
85     if (IsFunction)
86       return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
87     return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32;
88   case FK_Data_8:
89     llvm_unreachable("FK_Data_8 not implemented yet");
90   default:
91     llvm_unreachable("unimplemented fixup kind");
92   }
93 }
94 
95 MCObjectWriter *llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
96                                                         bool Is64Bit) {
97   MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
98   return createWasmObjectWriter(MOTW, OS);
99 }
100