1*18eafb6cSDan Gohman //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
2*18eafb6cSDan Gohman //
3*18eafb6cSDan Gohman //                     The LLVM Compiler Infrastructure
4*18eafb6cSDan Gohman //
5*18eafb6cSDan Gohman // This file is distributed under the University of Illinois Open Source
6*18eafb6cSDan Gohman // License. See LICENSE.TXT for details.
7*18eafb6cSDan Gohman //
8*18eafb6cSDan Gohman //===----------------------------------------------------------------------===//
9*18eafb6cSDan Gohman //
10*18eafb6cSDan Gohman // This file implements Wasm object file writer information.
11*18eafb6cSDan Gohman //
12*18eafb6cSDan Gohman //===----------------------------------------------------------------------===//
13*18eafb6cSDan Gohman 
14*18eafb6cSDan Gohman #include "llvm/ADT/STLExtras.h"
15*18eafb6cSDan Gohman #include "llvm/ADT/SmallPtrSet.h"
16*18eafb6cSDan Gohman #include "llvm/ADT/SmallString.h"
17*18eafb6cSDan Gohman #include "llvm/ADT/StringMap.h"
18*18eafb6cSDan Gohman #include "llvm/MC/MCAsmBackend.h"
19*18eafb6cSDan Gohman #include "llvm/MC/MCAsmInfo.h"
20*18eafb6cSDan Gohman #include "llvm/MC/MCAsmLayout.h"
21*18eafb6cSDan Gohman #include "llvm/MC/MCAssembler.h"
22*18eafb6cSDan Gohman #include "llvm/MC/MCContext.h"
23*18eafb6cSDan Gohman #include "llvm/MC/MCExpr.h"
24*18eafb6cSDan Gohman #include "llvm/MC/MCFixupKindInfo.h"
25*18eafb6cSDan Gohman #include "llvm/MC/MCObjectFileInfo.h"
26*18eafb6cSDan Gohman #include "llvm/MC/MCObjectWriter.h"
27*18eafb6cSDan Gohman #include "llvm/MC/MCSectionWasm.h"
28*18eafb6cSDan Gohman #include "llvm/MC/MCSymbolWasm.h"
29*18eafb6cSDan Gohman #include "llvm/MC/MCValue.h"
30*18eafb6cSDan Gohman #include "llvm/MC/MCWasmObjectWriter.h"
31*18eafb6cSDan Gohman #include "llvm/Support/Debug.h"
32*18eafb6cSDan Gohman #include "llvm/Support/Endian.h"
33*18eafb6cSDan Gohman #include "llvm/Support/ErrorHandling.h"
34*18eafb6cSDan Gohman #include "llvm/Support/StringSaver.h"
35*18eafb6cSDan Gohman #include <vector>
36*18eafb6cSDan Gohman 
37*18eafb6cSDan Gohman using namespace llvm;
38*18eafb6cSDan Gohman 
39*18eafb6cSDan Gohman #undef DEBUG_TYPE
40*18eafb6cSDan Gohman #define DEBUG_TYPE "reloc-info"
41*18eafb6cSDan Gohman 
42*18eafb6cSDan Gohman namespace {
43*18eafb6cSDan Gohman typedef DenseMap<const MCSectionWasm *, uint32_t> SectionIndexMapTy;
44*18eafb6cSDan Gohman 
45*18eafb6cSDan Gohman class WasmObjectWriter : public MCObjectWriter {
46*18eafb6cSDan Gohman   /// Helper struct for containing some precomputed information on symbols.
47*18eafb6cSDan Gohman   struct WasmSymbolData {
48*18eafb6cSDan Gohman     const MCSymbolWasm *Symbol;
49*18eafb6cSDan Gohman     StringRef Name;
50*18eafb6cSDan Gohman 
51*18eafb6cSDan Gohman     // Support lexicographic sorting.
52*18eafb6cSDan Gohman     bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
53*18eafb6cSDan Gohman   };
54*18eafb6cSDan Gohman 
55*18eafb6cSDan Gohman   /// The target specific Wasm writer instance.
56*18eafb6cSDan Gohman   std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
57*18eafb6cSDan Gohman 
58*18eafb6cSDan Gohman   // TargetObjectWriter wrappers.
59*18eafb6cSDan Gohman   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
60*18eafb6cSDan Gohman   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
61*18eafb6cSDan Gohman                         const MCFixup &Fixup, bool IsPCRel) const {
62*18eafb6cSDan Gohman     return TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
63*18eafb6cSDan Gohman   }
64*18eafb6cSDan Gohman 
65*18eafb6cSDan Gohman public:
66*18eafb6cSDan Gohman   WasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
67*18eafb6cSDan Gohman       : MCObjectWriter(OS, /*IsLittleEndian=*/true), TargetObjectWriter(MOTW) {}
68*18eafb6cSDan Gohman 
69*18eafb6cSDan Gohman   void reset() override {
70*18eafb6cSDan Gohman     MCObjectWriter::reset();
71*18eafb6cSDan Gohman   }
72*18eafb6cSDan Gohman 
73*18eafb6cSDan Gohman   ~WasmObjectWriter() override;
74*18eafb6cSDan Gohman 
75*18eafb6cSDan Gohman   void writeHeader(const MCAssembler &Asm);
76*18eafb6cSDan Gohman 
77*18eafb6cSDan Gohman   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
78*18eafb6cSDan Gohman                         const MCFragment *Fragment, const MCFixup &Fixup,
79*18eafb6cSDan Gohman                         MCValue Target, bool &IsPCRel,
80*18eafb6cSDan Gohman                         uint64_t &FixedValue) override;
81*18eafb6cSDan Gohman 
82*18eafb6cSDan Gohman   void executePostLayoutBinding(MCAssembler &Asm,
83*18eafb6cSDan Gohman                                 const MCAsmLayout &Layout) override;
84*18eafb6cSDan Gohman 
85*18eafb6cSDan Gohman   void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
86*18eafb6cSDan Gohman };
87*18eafb6cSDan Gohman } // end anonymous namespace
88*18eafb6cSDan Gohman 
89*18eafb6cSDan Gohman WasmObjectWriter::~WasmObjectWriter() {}
90*18eafb6cSDan Gohman 
91*18eafb6cSDan Gohman // Emit the Wasm header.
92*18eafb6cSDan Gohman void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
93*18eafb6cSDan Gohman   // TODO: write the magic cookie and the version.
94*18eafb6cSDan Gohman }
95*18eafb6cSDan Gohman 
96*18eafb6cSDan Gohman void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
97*18eafb6cSDan Gohman                                                 const MCAsmLayout &Layout) {
98*18eafb6cSDan Gohman }
99*18eafb6cSDan Gohman 
100*18eafb6cSDan Gohman void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
101*18eafb6cSDan Gohman                                         const MCAsmLayout &Layout,
102*18eafb6cSDan Gohman                                         const MCFragment *Fragment,
103*18eafb6cSDan Gohman                                         const MCFixup &Fixup, MCValue Target,
104*18eafb6cSDan Gohman                                         bool &IsPCRel, uint64_t &FixedValue) {
105*18eafb6cSDan Gohman   // TODO: Implement
106*18eafb6cSDan Gohman }
107*18eafb6cSDan Gohman 
108*18eafb6cSDan Gohman void WasmObjectWriter::writeObject(MCAssembler &Asm,
109*18eafb6cSDan Gohman                                    const MCAsmLayout &Layout) {
110*18eafb6cSDan Gohman   // Write out the Wasm header.
111*18eafb6cSDan Gohman   writeHeader(Asm);
112*18eafb6cSDan Gohman 
113*18eafb6cSDan Gohman   // TODO: Write the contents.
114*18eafb6cSDan Gohman }
115*18eafb6cSDan Gohman 
116*18eafb6cSDan Gohman MCObjectWriter *llvm::createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW,
117*18eafb6cSDan Gohman                                              raw_pwrite_stream &OS) {
118*18eafb6cSDan Gohman   return new WasmObjectWriter(MOTW, OS);
119*18eafb6cSDan Gohman }
120