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