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" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/Wasm.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAsmBackend.h" |
| 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCAsmLayout.h" |
| 20 | #include "llvm/MC/MCAssembler.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCExpr.h" |
| 23 | #include "llvm/MC/MCFixupKindInfo.h" |
| 24 | #include "llvm/MC/MCObjectFileInfo.h" |
| 25 | #include "llvm/MC/MCObjectWriter.h" |
| 26 | #include "llvm/MC/MCSectionWasm.h" |
| 27 | #include "llvm/MC/MCSymbolWasm.h" |
| 28 | #include "llvm/MC/MCValue.h" |
| 29 | #include "llvm/MC/MCWasmObjectWriter.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Casting.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/LEB128.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 34 | #include "llvm/Support/StringSaver.h" |
| 35 | #include <vector> |
| 36 | |
| 37 | using namespace llvm; |
| 38 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "mc" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 42 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 43 | // For patching purposes, we need to remember where each section starts, both |
| 44 | // for patching up the section size field, and for patching up references to |
| 45 | // locations within the section. |
| 46 | struct SectionBookkeeping { |
| 47 | // Where the size of the section is written. |
| 48 | uint64_t SizeOffset; |
| 49 | // Where the contents of the section starts (after the header). |
| 50 | uint64_t ContentsOffset; |
| 51 | }; |
| 52 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 53 | // The signature of a wasm function, in a struct capable of being used as a |
| 54 | // DenseMap key. |
| 55 | struct WasmFunctionType { |
| 56 | // Support empty and tombstone instances, needed by DenseMap. |
| 57 | enum { Plain, Empty, Tombstone } State; |
| 58 | |
| 59 | // The return types of the function. |
| 60 | SmallVector<wasm::ValType, 1> Returns; |
| 61 | |
| 62 | // The parameter types of the function. |
| 63 | SmallVector<wasm::ValType, 4> Params; |
| 64 | |
| 65 | WasmFunctionType() : State(Plain) {} |
| 66 | |
| 67 | bool operator==(const WasmFunctionType &Other) const { |
| 68 | return State == Other.State && Returns == Other.Returns && |
| 69 | Params == Other.Params; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | // Traits for using WasmFunctionType in a DenseMap. |
| 74 | struct WasmFunctionTypeDenseMapInfo { |
| 75 | static WasmFunctionType getEmptyKey() { |
| 76 | WasmFunctionType FuncTy; |
| 77 | FuncTy.State = WasmFunctionType::Empty; |
| 78 | return FuncTy; |
| 79 | } |
| 80 | static WasmFunctionType getTombstoneKey() { |
| 81 | WasmFunctionType FuncTy; |
| 82 | FuncTy.State = WasmFunctionType::Tombstone; |
| 83 | return FuncTy; |
| 84 | } |
| 85 | static unsigned getHashValue(const WasmFunctionType &FuncTy) { |
| 86 | uintptr_t Value = FuncTy.State; |
| 87 | for (wasm::ValType Ret : FuncTy.Returns) |
| 88 | Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret)); |
| 89 | for (wasm::ValType Param : FuncTy.Params) |
| 90 | Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param)); |
| 91 | return Value; |
| 92 | } |
| 93 | static bool isEqual(const WasmFunctionType &LHS, |
| 94 | const WasmFunctionType &RHS) { |
| 95 | return LHS == RHS; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | // A wasm import to be written into the import section. |
| 100 | struct WasmImport { |
| 101 | StringRef ModuleName; |
| 102 | StringRef FieldName; |
| 103 | unsigned Kind; |
| 104 | int32_t Type; |
| 105 | }; |
| 106 | |
| 107 | // A wasm function to be written into the function section. |
| 108 | struct WasmFunction { |
| 109 | int32_t Type; |
| 110 | const MCSymbolWasm *Sym; |
| 111 | }; |
| 112 | |
| 113 | // A wasm export to be written into the export section. |
| 114 | struct WasmExport { |
| 115 | StringRef FieldName; |
| 116 | unsigned Kind; |
| 117 | uint32_t Index; |
| 118 | }; |
| 119 | |
| 120 | // A wasm global to be written into the global section. |
| 121 | struct WasmGlobal { |
| 122 | wasm::ValType Type; |
| 123 | bool IsMutable; |
| 124 | bool HasImport; |
| 125 | uint64_t InitialValue; |
| 126 | uint32_t ImportIndex; |
| 127 | }; |
| 128 | |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 129 | // Information about a single relocation. |
| 130 | struct WasmRelocationEntry { |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 131 | uint64_t Offset; // Where is the relocation. |
| 132 | const MCSymbolWasm *Symbol; // The symbol to relocate with. |
| 133 | int64_t Addend; // A value to add to the symbol. |
| 134 | unsigned Type; // The type of the relocation. |
| 135 | const MCSectionWasm *FixupSection;// The section the relocation is targeting. |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 136 | |
| 137 | WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol, |
| 138 | int64_t Addend, unsigned Type, |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 139 | const MCSectionWasm *FixupSection) |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 140 | : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), |
| 141 | FixupSection(FixupSection) {} |
| 142 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 143 | bool hasAddend() const { |
| 144 | switch (Type) { |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 145 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 146 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 147 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 148 | return true; |
| 149 | default: |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 154 | void print(raw_ostream &Out) const { |
Sam Clegg | 9bf73c0 | 2017-07-05 20:25:08 +0000 | [diff] [blame] | 155 | Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 156 | << ", Type=" << Type << ", FixupSection=" << FixupSection; |
| 157 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 158 | |
| 159 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 160 | LLVM_DUMP_METHOD void dump() const { print(dbgs()); } |
| 161 | #endif |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Sam Clegg | 1fb8daa | 2017-06-20 05:05:10 +0000 | [diff] [blame] | 164 | #if !defined(NDEBUG) |
Sam Clegg | 7f055de | 2017-06-20 04:47:58 +0000 | [diff] [blame] | 165 | raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 166 | Rel.print(OS); |
| 167 | return OS; |
| 168 | } |
Sam Clegg | 1fb8daa | 2017-06-20 05:05:10 +0000 | [diff] [blame] | 169 | #endif |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 170 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 171 | class WasmObjectWriter : public MCObjectWriter { |
| 172 | /// Helper struct for containing some precomputed information on symbols. |
| 173 | struct WasmSymbolData { |
| 174 | const MCSymbolWasm *Symbol; |
| 175 | StringRef Name; |
| 176 | |
| 177 | // Support lexicographic sorting. |
| 178 | bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; } |
| 179 | }; |
| 180 | |
| 181 | /// The target specific Wasm writer instance. |
| 182 | std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter; |
| 183 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 184 | // Relocations for fixing up references in the code section. |
| 185 | std::vector<WasmRelocationEntry> CodeRelocations; |
| 186 | |
| 187 | // Relocations for fixing up references in the data section. |
| 188 | std::vector<WasmRelocationEntry> DataRelocations; |
| 189 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 190 | // Index values to use for fixing up call_indirect type indices. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 191 | // Maps function symbols to the index of the type of the function |
| 192 | DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices; |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 193 | // Maps function symbols to the table element index space. Used |
| 194 | // for TABLE_INDEX relocation types (i.e. address taken functions). |
| 195 | DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices; |
| 196 | // Maps function/global symbols to the function/global index space. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 197 | DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices; |
| 198 | |
| 199 | DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo> |
| 200 | FunctionTypeIndices; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 201 | SmallVector<WasmFunctionType, 4> FunctionTypes; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 202 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 203 | // TargetObjectWriter wrappers. |
| 204 | bool is64Bit() const { return TargetObjectWriter->is64Bit(); } |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 205 | unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const { |
| 206 | return TargetObjectWriter->getRelocType(Target, Fixup); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 209 | void startSection(SectionBookkeeping &Section, unsigned SectionId, |
| 210 | const char *Name = nullptr); |
| 211 | void endSection(SectionBookkeeping &Section); |
| 212 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 213 | public: |
| 214 | WasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS) |
| 215 | : MCObjectWriter(OS, /*IsLittleEndian=*/true), TargetObjectWriter(MOTW) {} |
| 216 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 217 | private: |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 218 | ~WasmObjectWriter() override; |
| 219 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 220 | void reset() override { |
| 221 | CodeRelocations.clear(); |
| 222 | DataRelocations.clear(); |
| 223 | TypeIndices.clear(); |
| 224 | SymbolIndices.clear(); |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 225 | IndirectSymbolIndices.clear(); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 226 | FunctionTypeIndices.clear(); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 227 | FunctionTypes.clear(); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 228 | MCObjectWriter::reset(); |
| 229 | } |
| 230 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 231 | void writeHeader(const MCAssembler &Asm); |
| 232 | |
| 233 | void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 234 | const MCFragment *Fragment, const MCFixup &Fixup, |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 235 | MCValue Target, uint64_t &FixedValue) override; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 236 | |
| 237 | void executePostLayoutBinding(MCAssembler &Asm, |
| 238 | const MCAsmLayout &Layout) override; |
| 239 | |
| 240 | void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 241 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 242 | void writeString(const StringRef Str) { |
| 243 | encodeULEB128(Str.size(), getStream()); |
| 244 | writeBytes(Str); |
| 245 | } |
| 246 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 247 | void writeValueType(wasm::ValType Ty) { |
| 248 | encodeSLEB128(int32_t(Ty), getStream()); |
| 249 | } |
| 250 | |
| 251 | void writeTypeSection(const SmallVector<WasmFunctionType, 4> &FunctionTypes); |
| 252 | void writeImportSection(const SmallVector<WasmImport, 4> &Imports); |
| 253 | void writeFunctionSection(const SmallVector<WasmFunction, 4> &Functions); |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 254 | void writeTableSection(uint32_t NumElements); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 255 | void writeMemorySection(const SmallVector<char, 0> &DataBytes); |
| 256 | void writeGlobalSection(const SmallVector<WasmGlobal, 4> &Globals); |
| 257 | void writeExportSection(const SmallVector<WasmExport, 4> &Exports); |
| 258 | void writeElemSection(const SmallVector<uint32_t, 4> &TableElems); |
| 259 | void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 260 | const SmallVector<WasmFunction, 4> &Functions); |
| 261 | uint64_t |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 262 | writeDataSection(const SmallVector<char, 0> &DataBytes); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 263 | void writeNameSection(const SmallVector<WasmFunction, 4> &Functions, |
| 264 | const SmallVector<WasmImport, 4> &Imports, |
| 265 | uint32_t NumFuncImports); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 266 | void writeCodeRelocSection(); |
| 267 | void writeDataRelocSection(uint64_t DataSectionHeaderSize); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 268 | void writeLinkingMetaDataSection(uint32_t DataSize, uint32_t DataAlignment, |
| 269 | ArrayRef<StringRef> WeakSymbols, |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 270 | bool HasStackPointer, |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 271 | uint32_t StackPointerGlobal); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 272 | |
| 273 | void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations, |
| 274 | uint64_t ContentsOffset); |
| 275 | |
| 276 | void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations, |
| 277 | uint64_t HeaderSize); |
| 278 | uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 279 | uint32_t getFunctionType(const MCSymbolWasm& Symbol); |
| 280 | uint32_t registerFunctionType(const MCSymbolWasm& Symbol); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 281 | }; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 282 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 283 | } // end anonymous namespace |
| 284 | |
| 285 | WasmObjectWriter::~WasmObjectWriter() {} |
| 286 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 287 | // Return the padding size to write a 32-bit value into a 5-byte ULEB128. |
| 288 | static unsigned PaddingFor5ByteULEB128(uint32_t X) { |
| 289 | return X == 0 ? 4 : (4u - (31u - countLeadingZeros(X)) / 7u); |
| 290 | } |
| 291 | |
| 292 | // Return the padding size to write a 32-bit value into a 5-byte SLEB128. |
| 293 | static unsigned PaddingFor5ByteSLEB128(int32_t X) { |
| 294 | return 5 - getSLEB128Size(X); |
| 295 | } |
| 296 | |
| 297 | // Write out a section header and a patchable section size field. |
| 298 | void WasmObjectWriter::startSection(SectionBookkeeping &Section, |
| 299 | unsigned SectionId, |
| 300 | const char *Name) { |
| 301 | assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) && |
| 302 | "Only custom sections can have names"); |
| 303 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 304 | DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n"); |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 305 | encodeULEB128(SectionId, getStream()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 306 | |
| 307 | Section.SizeOffset = getStream().tell(); |
| 308 | |
| 309 | // The section size. We don't know the size yet, so reserve enough space |
| 310 | // for any 32-bit value; we'll patch it later. |
| 311 | encodeULEB128(UINT32_MAX, getStream()); |
| 312 | |
| 313 | // The position where the section starts, for measuring its size. |
| 314 | Section.ContentsOffset = getStream().tell(); |
| 315 | |
| 316 | // Custom sections in wasm also have a string identifier. |
| 317 | if (SectionId == wasm::WASM_SEC_CUSTOM) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 318 | assert(Name); |
| 319 | writeString(StringRef(Name)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | // Now that the section is complete and we know how big it is, patch up the |
| 324 | // section size field at the start of the section. |
| 325 | void WasmObjectWriter::endSection(SectionBookkeeping &Section) { |
| 326 | uint64_t Size = getStream().tell() - Section.ContentsOffset; |
| 327 | if (uint32_t(Size) != Size) |
| 328 | report_fatal_error("section size does not fit in a uint32_t"); |
| 329 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 330 | DEBUG(dbgs() << "endSection size=" << Size << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 331 | unsigned Padding = PaddingFor5ByteULEB128(Size); |
| 332 | |
| 333 | // Write the final section size to the payload_len field, which follows |
| 334 | // the section id byte. |
| 335 | uint8_t Buffer[16]; |
| 336 | unsigned SizeLen = encodeULEB128(Size, Buffer, Padding); |
| 337 | assert(SizeLen == 5); |
| 338 | getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset); |
| 339 | } |
| 340 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 341 | // Emit the Wasm header. |
| 342 | void WasmObjectWriter::writeHeader(const MCAssembler &Asm) { |
Dan Gohman | 7ea5adf | 2017-02-22 18:50:20 +0000 | [diff] [blame] | 343 | writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic))); |
| 344 | writeLE32(wasm::WasmVersion); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
| 348 | const MCAsmLayout &Layout) { |
| 349 | } |
| 350 | |
| 351 | void WasmObjectWriter::recordRelocation(MCAssembler &Asm, |
| 352 | const MCAsmLayout &Layout, |
| 353 | const MCFragment *Fragment, |
| 354 | const MCFixup &Fixup, MCValue Target, |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 355 | uint64_t &FixedValue) { |
| 356 | MCAsmBackend &Backend = Asm.getBackend(); |
| 357 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 358 | MCFixupKindInfo::FKF_IsPCRel; |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 359 | const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 360 | uint64_t C = Target.getConstant(); |
| 361 | uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 362 | MCContext &Ctx = Asm.getContext(); |
| 363 | |
| 364 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 365 | assert(RefB->getKind() == MCSymbolRefExpr::VK_None && |
| 366 | "Should not have constructed this"); |
| 367 | |
| 368 | // Let A, B and C being the components of Target and R be the location of |
| 369 | // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). |
| 370 | // If it is pcrel, we want to compute (A - B + C - R). |
| 371 | |
| 372 | // In general, Wasm has no relocations for -B. It can only represent (A + C) |
| 373 | // or (A + C - R). If B = R + K and the relocation is not pcrel, we can |
| 374 | // replace B to implement it: (A - R - K + C) |
| 375 | if (IsPCRel) { |
| 376 | Ctx.reportError( |
| 377 | Fixup.getLoc(), |
| 378 | "No relocation available to represent this relative expression"); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol()); |
| 383 | |
| 384 | if (SymB.isUndefined()) { |
| 385 | Ctx.reportError(Fixup.getLoc(), |
| 386 | Twine("symbol '") + SymB.getName() + |
| 387 | "' can not be undefined in a subtraction expression"); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | assert(!SymB.isAbsolute() && "Should have been folded"); |
| 392 | const MCSection &SecB = SymB.getSection(); |
| 393 | if (&SecB != &FixupSection) { |
| 394 | Ctx.reportError(Fixup.getLoc(), |
| 395 | "Cannot represent a difference across sections"); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | uint64_t SymBOffset = Layout.getSymbolOffset(SymB); |
| 400 | uint64_t K = SymBOffset - FixupOffset; |
| 401 | IsPCRel = true; |
| 402 | C -= K; |
| 403 | } |
| 404 | |
| 405 | // We either rejected the fixup or folded B into C at this point. |
| 406 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 407 | const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr; |
| 408 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 409 | if (SymA && SymA->isVariable()) { |
| 410 | const MCExpr *Expr = SymA->getVariableValue(); |
Sam Clegg | 6ad8f19 | 2017-07-11 02:21:57 +0000 | [diff] [blame] | 411 | const auto *Inner = cast<MCSymbolRefExpr>(Expr); |
| 412 | if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) |
| 413 | llvm_unreachable("weakref used in reloc not yet implemented"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Put any constant offset in an addend. Offsets can be negative, and |
| 417 | // LLVM expects wrapping, in contrast to wasm's immediates which can't |
| 418 | // be negative and don't wrap. |
| 419 | FixedValue = 0; |
| 420 | |
Sam Clegg | 6ad8f19 | 2017-07-11 02:21:57 +0000 | [diff] [blame] | 421 | if (SymA) |
| 422 | SymA->setUsedInReloc(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 423 | |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 424 | assert(!IsPCRel); |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 425 | assert(SymA); |
| 426 | |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 427 | unsigned Type = getRelocType(Target, Fixup); |
| 428 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 429 | WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 430 | DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 431 | |
| 432 | if (FixupSection.hasInstructions()) |
| 433 | CodeRelocations.push_back(Rec); |
| 434 | else |
| 435 | DataRelocations.push_back(Rec); |
| 436 | } |
| 437 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 438 | // Write X as an (unsigned) LEB value at offset Offset in Stream, padded |
| 439 | // to allow patching. |
| 440 | static void |
| 441 | WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) { |
| 442 | uint8_t Buffer[5]; |
| 443 | unsigned Padding = PaddingFor5ByteULEB128(X); |
| 444 | unsigned SizeLen = encodeULEB128(X, Buffer, Padding); |
| 445 | assert(SizeLen == 5); |
| 446 | Stream.pwrite((char *)Buffer, SizeLen, Offset); |
| 447 | } |
| 448 | |
| 449 | // Write X as an signed LEB value at offset Offset in Stream, padded |
| 450 | // to allow patching. |
| 451 | static void |
| 452 | WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) { |
| 453 | uint8_t Buffer[5]; |
| 454 | unsigned Padding = PaddingFor5ByteSLEB128(X); |
| 455 | unsigned SizeLen = encodeSLEB128(X, Buffer, Padding); |
| 456 | assert(SizeLen == 5); |
| 457 | Stream.pwrite((char *)Buffer, SizeLen, Offset); |
| 458 | } |
| 459 | |
| 460 | // Write X as a plain integer value at offset Offset in Stream. |
| 461 | static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) { |
| 462 | uint8_t Buffer[4]; |
| 463 | support::endian::write32le(Buffer, X); |
| 464 | Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); |
| 465 | } |
| 466 | |
| 467 | // Compute a value to write into the code at the location covered |
| 468 | // by RelEntry. This value isn't used by the static linker, since |
| 469 | // we have addends; it just serves to make the code more readable |
| 470 | // and to make standalone wasm modules directly usable. |
| 471 | static uint32_t ProvisionalValue(const WasmRelocationEntry &RelEntry) { |
| 472 | const MCSymbolWasm *Sym = RelEntry.Symbol; |
| 473 | |
| 474 | // For undefined symbols, use a hopefully invalid value. |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 475 | if (!Sym->isDefined(/*SetUsed=*/false)) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 476 | return UINT32_MAX; |
| 477 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 478 | const auto &Section = cast<MCSectionWasm>(RelEntry.Symbol->getSection(false)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 479 | uint64_t Address = Section.getSectionOffset() + RelEntry.Addend; |
| 480 | |
| 481 | // Ignore overflow. LLVM allows address arithmetic to silently wrap. |
| 482 | uint32_t Value = Address; |
| 483 | |
| 484 | return Value; |
| 485 | } |
| 486 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 487 | uint32_t WasmObjectWriter::getRelocationIndexValue( |
| 488 | const WasmRelocationEntry &RelEntry) { |
| 489 | switch (RelEntry.Type) { |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 490 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 491 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 492 | if (!IndirectSymbolIndices.count(RelEntry.Symbol)) |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 493 | report_fatal_error("symbol not found table index space: " + |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 494 | RelEntry.Symbol->getName()); |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 495 | return IndirectSymbolIndices[RelEntry.Symbol]; |
| 496 | case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 497 | case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 498 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 499 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 500 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 501 | if (!SymbolIndices.count(RelEntry.Symbol)) |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 502 | report_fatal_error("symbol not found function/global index space: " + |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 503 | RelEntry.Symbol->getName()); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 504 | return SymbolIndices[RelEntry.Symbol]; |
| 505 | case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 506 | if (!TypeIndices.count(RelEntry.Symbol)) |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 507 | report_fatal_error("symbol not found in type index space: " + |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 508 | RelEntry.Symbol->getName()); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 509 | return TypeIndices[RelEntry.Symbol]; |
| 510 | default: |
| 511 | llvm_unreachable("invalid relocation type"); |
| 512 | } |
| 513 | } |
| 514 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 515 | // Apply the portions of the relocation records that we can handle ourselves |
| 516 | // directly. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 517 | void WasmObjectWriter::applyRelocations( |
| 518 | ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) { |
| 519 | raw_pwrite_stream &Stream = getStream(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 520 | for (const WasmRelocationEntry &RelEntry : Relocations) { |
| 521 | uint64_t Offset = ContentsOffset + |
| 522 | RelEntry.FixupSection->getSectionOffset() + |
| 523 | RelEntry.Offset; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 524 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 525 | DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n"); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 526 | switch (RelEntry.Type) { |
| 527 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 528 | case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 529 | case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 530 | case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: { |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 531 | uint32_t Index = getRelocationIndexValue(RelEntry); |
| 532 | WritePatchableSLEB(Stream, Index, Offset); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 535 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: { |
| 536 | uint32_t Index = getRelocationIndexValue(RelEntry); |
| 537 | WriteI32(Stream, Index, Offset); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 538 | break; |
| 539 | } |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 540 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 541 | uint32_t Value = ProvisionalValue(RelEntry); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 542 | WritePatchableSLEB(Stream, Value, Offset); |
| 543 | break; |
| 544 | } |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 545 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 546 | uint32_t Value = ProvisionalValue(RelEntry); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 547 | WritePatchableLEB(Stream, Value, Offset); |
| 548 | break; |
| 549 | } |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 550 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 551 | uint32_t Value = ProvisionalValue(RelEntry); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 552 | WriteI32(Stream, Value, Offset); |
| 553 | break; |
| 554 | } |
| 555 | default: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 556 | llvm_unreachable("invalid relocation type"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 561 | // Write out the portions of the relocation records that the linker will |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 562 | // need to handle. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 563 | void WasmObjectWriter::writeRelocations( |
| 564 | ArrayRef<WasmRelocationEntry> Relocations, uint64_t HeaderSize) { |
| 565 | raw_pwrite_stream &Stream = getStream(); |
| 566 | for (const WasmRelocationEntry& RelEntry : Relocations) { |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 567 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 568 | uint64_t Offset = RelEntry.Offset + |
| 569 | RelEntry.FixupSection->getSectionOffset() + HeaderSize; |
| 570 | uint32_t Index = getRelocationIndexValue(RelEntry); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 571 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 572 | encodeULEB128(RelEntry.Type, Stream); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 573 | encodeULEB128(Offset, Stream); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 574 | encodeULEB128(Index, Stream); |
| 575 | if (RelEntry.hasAddend()) |
| 576 | encodeSLEB128(RelEntry.Addend, Stream); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 580 | void WasmObjectWriter::writeTypeSection( |
| 581 | const SmallVector<WasmFunctionType, 4> &FunctionTypes) { |
| 582 | if (FunctionTypes.empty()) |
| 583 | return; |
| 584 | |
| 585 | SectionBookkeeping Section; |
| 586 | startSection(Section, wasm::WASM_SEC_TYPE); |
| 587 | |
| 588 | encodeULEB128(FunctionTypes.size(), getStream()); |
| 589 | |
| 590 | for (const WasmFunctionType &FuncTy : FunctionTypes) { |
| 591 | encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream()); |
| 592 | encodeULEB128(FuncTy.Params.size(), getStream()); |
| 593 | for (wasm::ValType Ty : FuncTy.Params) |
| 594 | writeValueType(Ty); |
| 595 | encodeULEB128(FuncTy.Returns.size(), getStream()); |
| 596 | for (wasm::ValType Ty : FuncTy.Returns) |
| 597 | writeValueType(Ty); |
| 598 | } |
| 599 | |
| 600 | endSection(Section); |
| 601 | } |
| 602 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 603 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 604 | void WasmObjectWriter::writeImportSection( |
| 605 | const SmallVector<WasmImport, 4> &Imports) { |
| 606 | if (Imports.empty()) |
| 607 | return; |
| 608 | |
| 609 | SectionBookkeeping Section; |
| 610 | startSection(Section, wasm::WASM_SEC_IMPORT); |
| 611 | |
| 612 | encodeULEB128(Imports.size(), getStream()); |
| 613 | for (const WasmImport &Import : Imports) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 614 | writeString(Import.ModuleName); |
| 615 | writeString(Import.FieldName); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 616 | |
| 617 | encodeULEB128(Import.Kind, getStream()); |
| 618 | |
| 619 | switch (Import.Kind) { |
| 620 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 621 | encodeULEB128(Import.Type, getStream()); |
| 622 | break; |
| 623 | case wasm::WASM_EXTERNAL_GLOBAL: |
| 624 | encodeSLEB128(int32_t(Import.Type), getStream()); |
| 625 | encodeULEB128(0, getStream()); // mutability |
| 626 | break; |
| 627 | default: |
| 628 | llvm_unreachable("unsupported import kind"); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | endSection(Section); |
| 633 | } |
| 634 | |
| 635 | void WasmObjectWriter::writeFunctionSection( |
| 636 | const SmallVector<WasmFunction, 4> &Functions) { |
| 637 | if (Functions.empty()) |
| 638 | return; |
| 639 | |
| 640 | SectionBookkeeping Section; |
| 641 | startSection(Section, wasm::WASM_SEC_FUNCTION); |
| 642 | |
| 643 | encodeULEB128(Functions.size(), getStream()); |
| 644 | for (const WasmFunction &Func : Functions) |
| 645 | encodeULEB128(Func.Type, getStream()); |
| 646 | |
| 647 | endSection(Section); |
| 648 | } |
| 649 | |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 650 | void WasmObjectWriter::writeTableSection(uint32_t NumElements) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 651 | // For now, always emit the table section, since indirect calls are not |
| 652 | // valid without it. In the future, we could perhaps be more clever and omit |
| 653 | // it if there are no indirect calls. |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 654 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 655 | SectionBookkeeping Section; |
| 656 | startSection(Section, wasm::WASM_SEC_TABLE); |
| 657 | |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 658 | encodeULEB128(1, getStream()); // The number of tables. |
| 659 | // Fixed to 1 for now. |
| 660 | encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table |
| 661 | encodeULEB128(0, getStream()); // flags |
| 662 | encodeULEB128(NumElements, getStream()); // initial |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 663 | |
| 664 | endSection(Section); |
| 665 | } |
| 666 | |
| 667 | void WasmObjectWriter::writeMemorySection( |
| 668 | const SmallVector<char, 0> &DataBytes) { |
| 669 | // For now, always emit the memory section, since loads and stores are not |
| 670 | // valid without it. In the future, we could perhaps be more clever and omit |
| 671 | // it if there are no loads or stores. |
| 672 | SectionBookkeeping Section; |
| 673 | uint32_t NumPages = |
| 674 | (DataBytes.size() + wasm::WasmPageSize - 1) / wasm::WasmPageSize; |
| 675 | |
| 676 | startSection(Section, wasm::WASM_SEC_MEMORY); |
| 677 | encodeULEB128(1, getStream()); // number of memory spaces |
| 678 | |
| 679 | encodeULEB128(0, getStream()); // flags |
| 680 | encodeULEB128(NumPages, getStream()); // initial |
| 681 | |
| 682 | endSection(Section); |
| 683 | } |
| 684 | |
| 685 | void WasmObjectWriter::writeGlobalSection( |
| 686 | const SmallVector<WasmGlobal, 4> &Globals) { |
| 687 | if (Globals.empty()) |
| 688 | return; |
| 689 | |
| 690 | SectionBookkeeping Section; |
| 691 | startSection(Section, wasm::WASM_SEC_GLOBAL); |
| 692 | |
| 693 | encodeULEB128(Globals.size(), getStream()); |
| 694 | for (const WasmGlobal &Global : Globals) { |
| 695 | writeValueType(Global.Type); |
| 696 | write8(Global.IsMutable); |
| 697 | |
| 698 | if (Global.HasImport) { |
| 699 | assert(Global.InitialValue == 0); |
| 700 | write8(wasm::WASM_OPCODE_GET_GLOBAL); |
| 701 | encodeULEB128(Global.ImportIndex, getStream()); |
| 702 | } else { |
| 703 | assert(Global.ImportIndex == 0); |
| 704 | write8(wasm::WASM_OPCODE_I32_CONST); |
| 705 | encodeSLEB128(Global.InitialValue, getStream()); // offset |
| 706 | } |
| 707 | write8(wasm::WASM_OPCODE_END); |
| 708 | } |
| 709 | |
| 710 | endSection(Section); |
| 711 | } |
| 712 | |
| 713 | void WasmObjectWriter::writeExportSection( |
| 714 | const SmallVector<WasmExport, 4> &Exports) { |
| 715 | if (Exports.empty()) |
| 716 | return; |
| 717 | |
| 718 | SectionBookkeeping Section; |
| 719 | startSection(Section, wasm::WASM_SEC_EXPORT); |
| 720 | |
| 721 | encodeULEB128(Exports.size(), getStream()); |
| 722 | for (const WasmExport &Export : Exports) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 723 | writeString(Export.FieldName); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 724 | encodeSLEB128(Export.Kind, getStream()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 725 | encodeULEB128(Export.Index, getStream()); |
| 726 | } |
| 727 | |
| 728 | endSection(Section); |
| 729 | } |
| 730 | |
| 731 | void WasmObjectWriter::writeElemSection( |
| 732 | const SmallVector<uint32_t, 4> &TableElems) { |
| 733 | if (TableElems.empty()) |
| 734 | return; |
| 735 | |
| 736 | SectionBookkeeping Section; |
| 737 | startSection(Section, wasm::WASM_SEC_ELEM); |
| 738 | |
| 739 | encodeULEB128(1, getStream()); // number of "segments" |
| 740 | encodeULEB128(0, getStream()); // the table index |
| 741 | |
| 742 | // init expr for starting offset |
| 743 | write8(wasm::WASM_OPCODE_I32_CONST); |
| 744 | encodeSLEB128(0, getStream()); |
| 745 | write8(wasm::WASM_OPCODE_END); |
| 746 | |
| 747 | encodeULEB128(TableElems.size(), getStream()); |
| 748 | for (uint32_t Elem : TableElems) |
| 749 | encodeULEB128(Elem, getStream()); |
| 750 | |
| 751 | endSection(Section); |
| 752 | } |
| 753 | |
| 754 | void WasmObjectWriter::writeCodeSection( |
| 755 | const MCAssembler &Asm, const MCAsmLayout &Layout, |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 756 | const SmallVector<WasmFunction, 4> &Functions) { |
| 757 | if (Functions.empty()) |
| 758 | return; |
| 759 | |
| 760 | SectionBookkeeping Section; |
| 761 | startSection(Section, wasm::WASM_SEC_CODE); |
| 762 | |
| 763 | encodeULEB128(Functions.size(), getStream()); |
| 764 | |
| 765 | for (const WasmFunction &Func : Functions) { |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 766 | auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 767 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 768 | int64_t Size = 0; |
| 769 | if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout)) |
| 770 | report_fatal_error(".size expression must be evaluatable"); |
| 771 | |
| 772 | encodeULEB128(Size, getStream()); |
| 773 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 774 | FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 775 | |
| 776 | Asm.writeSectionData(&FuncSection, Layout); |
| 777 | } |
| 778 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 779 | // Apply fixups. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 780 | applyRelocations(CodeRelocations, Section.ContentsOffset); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 781 | |
| 782 | endSection(Section); |
| 783 | } |
| 784 | |
| 785 | uint64_t WasmObjectWriter::writeDataSection( |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 786 | const SmallVector<char, 0> &DataBytes) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 787 | if (DataBytes.empty()) |
| 788 | return 0; |
| 789 | |
| 790 | SectionBookkeeping Section; |
| 791 | startSection(Section, wasm::WASM_SEC_DATA); |
| 792 | |
| 793 | encodeULEB128(1, getStream()); // count |
| 794 | encodeULEB128(0, getStream()); // memory index |
| 795 | write8(wasm::WASM_OPCODE_I32_CONST); |
| 796 | encodeSLEB128(0, getStream()); // offset |
| 797 | write8(wasm::WASM_OPCODE_END); |
| 798 | encodeULEB128(DataBytes.size(), getStream()); // size |
| 799 | uint32_t HeaderSize = getStream().tell() - Section.ContentsOffset; |
| 800 | writeBytes(DataBytes); // data |
| 801 | |
| 802 | // Apply fixups. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 803 | applyRelocations(DataRelocations, Section.ContentsOffset + HeaderSize); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 804 | |
| 805 | endSection(Section); |
| 806 | return HeaderSize; |
| 807 | } |
| 808 | |
| 809 | void WasmObjectWriter::writeNameSection( |
| 810 | const SmallVector<WasmFunction, 4> &Functions, |
| 811 | const SmallVector<WasmImport, 4> &Imports, |
| 812 | unsigned NumFuncImports) { |
| 813 | uint32_t TotalFunctions = NumFuncImports + Functions.size(); |
| 814 | if (TotalFunctions == 0) |
| 815 | return; |
| 816 | |
| 817 | SectionBookkeeping Section; |
| 818 | startSection(Section, wasm::WASM_SEC_CUSTOM, "name"); |
| 819 | SectionBookkeeping SubSection; |
| 820 | startSection(SubSection, wasm::WASM_NAMES_FUNCTION); |
| 821 | |
| 822 | encodeULEB128(TotalFunctions, getStream()); |
| 823 | uint32_t Index = 0; |
| 824 | for (const WasmImport &Import : Imports) { |
| 825 | if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) { |
| 826 | encodeULEB128(Index, getStream()); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 827 | writeString(Import.FieldName); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 828 | ++Index; |
| 829 | } |
| 830 | } |
| 831 | for (const WasmFunction &Func : Functions) { |
| 832 | encodeULEB128(Index, getStream()); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 833 | writeString(Func.Sym->getName()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 834 | ++Index; |
| 835 | } |
| 836 | |
| 837 | endSection(SubSection); |
| 838 | endSection(Section); |
| 839 | } |
| 840 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 841 | void WasmObjectWriter::writeCodeRelocSection() { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 842 | // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md |
| 843 | // for descriptions of the reloc sections. |
| 844 | |
| 845 | if (CodeRelocations.empty()) |
| 846 | return; |
| 847 | |
| 848 | SectionBookkeeping Section; |
| 849 | startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE"); |
| 850 | |
| 851 | encodeULEB128(wasm::WASM_SEC_CODE, getStream()); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 852 | encodeULEB128(CodeRelocations.size(), getStream()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 853 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 854 | writeRelocations(CodeRelocations, 0); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 855 | |
| 856 | endSection(Section); |
| 857 | } |
| 858 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 859 | void WasmObjectWriter::writeDataRelocSection(uint64_t DataSectionHeaderSize) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 860 | // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md |
| 861 | // for descriptions of the reloc sections. |
| 862 | |
| 863 | if (DataRelocations.empty()) |
| 864 | return; |
| 865 | |
| 866 | SectionBookkeeping Section; |
| 867 | startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA"); |
| 868 | |
| 869 | encodeULEB128(wasm::WASM_SEC_DATA, getStream()); |
| 870 | encodeULEB128(DataRelocations.size(), getStream()); |
| 871 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 872 | writeRelocations(DataRelocations, DataSectionHeaderSize); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 873 | |
| 874 | endSection(Section); |
| 875 | } |
| 876 | |
| 877 | void WasmObjectWriter::writeLinkingMetaDataSection( |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 878 | uint32_t DataSize, uint32_t DataAlignment, ArrayRef<StringRef> WeakSymbols, |
| 879 | bool HasStackPointer, uint32_t StackPointerGlobal) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 880 | SectionBookkeeping Section; |
| 881 | startSection(Section, wasm::WASM_SEC_CUSTOM, "linking"); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 882 | SectionBookkeeping SubSection; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 883 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 884 | if (HasStackPointer) { |
| 885 | startSection(SubSection, wasm::WASM_STACK_POINTER); |
| 886 | encodeULEB128(StackPointerGlobal, getStream()); // id |
| 887 | endSection(SubSection); |
| 888 | } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 889 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 890 | if (WeakSymbols.size() != 0) { |
| 891 | startSection(SubSection, wasm::WASM_SYMBOL_INFO); |
| 892 | encodeULEB128(WeakSymbols.size(), getStream()); |
| 893 | for (const StringRef Export: WeakSymbols) { |
| 894 | writeString(Export); |
| 895 | encodeULEB128(wasm::WASM_SYMBOL_FLAG_WEAK, getStream()); |
| 896 | } |
| 897 | endSection(SubSection); |
| 898 | } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 899 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 900 | if (DataSize > 0) { |
| 901 | startSection(SubSection, wasm::WASM_DATA_SIZE); |
| 902 | encodeULEB128(DataSize, getStream()); |
| 903 | endSection(SubSection); |
| 904 | |
| 905 | startSection(SubSection, wasm::WASM_DATA_ALIGNMENT); |
| 906 | encodeULEB128(DataAlignment, getStream()); |
| 907 | endSection(SubSection); |
| 908 | } |
| 909 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 910 | endSection(Section); |
| 911 | } |
| 912 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 913 | uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) { |
| 914 | assert(Symbol.isFunction()); |
| 915 | assert(TypeIndices.count(&Symbol)); |
| 916 | return TypeIndices[&Symbol]; |
| 917 | } |
| 918 | |
| 919 | uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) { |
| 920 | assert(Symbol.isFunction()); |
| 921 | |
| 922 | WasmFunctionType F; |
| 923 | if (Symbol.isVariable()) { |
| 924 | const MCExpr *Expr = Symbol.getVariableValue(); |
Sam Clegg | 6ad8f19 | 2017-07-11 02:21:57 +0000 | [diff] [blame] | 925 | auto *Inner = cast<MCSymbolRefExpr>(Expr); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 926 | const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol()); |
| 927 | F.Returns = ResolvedSym->getReturns(); |
| 928 | F.Params = ResolvedSym->getParams(); |
| 929 | } else { |
| 930 | F.Returns = Symbol.getReturns(); |
| 931 | F.Params = Symbol.getParams(); |
| 932 | } |
| 933 | |
| 934 | auto Pair = |
| 935 | FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size())); |
| 936 | if (Pair.second) |
| 937 | FunctionTypes.push_back(F); |
| 938 | TypeIndices[&Symbol] = Pair.first->second; |
| 939 | |
| 940 | DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n"); |
| 941 | DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); |
| 942 | return Pair.first->second; |
| 943 | } |
| 944 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 945 | void WasmObjectWriter::writeObject(MCAssembler &Asm, |
| 946 | const MCAsmLayout &Layout) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 947 | DEBUG(dbgs() << "WasmObjectWriter::writeObject\n"); |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 948 | MCContext &Ctx = Asm.getContext(); |
Derek Schuff | b879539 | 2017-03-16 20:49:48 +0000 | [diff] [blame] | 949 | wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 950 | |
| 951 | // Collect information from the available symbols. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 952 | SmallVector<WasmFunction, 4> Functions; |
| 953 | SmallVector<uint32_t, 4> TableElems; |
| 954 | SmallVector<WasmGlobal, 4> Globals; |
| 955 | SmallVector<WasmImport, 4> Imports; |
| 956 | SmallVector<WasmExport, 4> Exports; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 957 | SmallVector<StringRef, 4> WeakSymbols; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 958 | SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken; |
| 959 | unsigned NumFuncImports = 0; |
| 960 | unsigned NumGlobalImports = 0; |
| 961 | SmallVector<char, 0> DataBytes; |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 962 | uint32_t DataAlignment = 1; |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 963 | uint32_t StackPointerGlobal = 0; |
| 964 | bool HasStackPointer = false; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 965 | |
| 966 | // Populate the IsAddressTaken set. |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 967 | for (const WasmRelocationEntry &RelEntry : CodeRelocations) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 968 | switch (RelEntry.Type) { |
| 969 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 970 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 971 | IsAddressTaken.insert(RelEntry.Symbol); |
| 972 | break; |
| 973 | default: |
| 974 | break; |
| 975 | } |
| 976 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 977 | for (const WasmRelocationEntry &RelEntry : DataRelocations) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 978 | switch (RelEntry.Type) { |
| 979 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: |
Sam Clegg | 13a2e89 | 2017-09-01 17:32:01 +0000 | [diff] [blame] | 980 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 981 | IsAddressTaken.insert(RelEntry.Symbol); |
| 982 | break; |
| 983 | default: |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // Populate the Imports set. |
| 989 | for (const MCSymbol &S : Asm.symbols()) { |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 990 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
| 991 | |
| 992 | if (WS.isTemporary()) |
Sam Clegg | 8c4baa00 | 2017-07-05 20:09:26 +0000 | [diff] [blame] | 993 | continue; |
| 994 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 995 | if (WS.isFunction()) |
| 996 | registerFunctionType(WS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 997 | |
| 998 | // If the symbol is not defined in this translation unit, import it. |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 999 | if (!WS.isDefined(/*SetUsed=*/false) || WS.isVariable()) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1000 | WasmImport Import; |
| 1001 | Import.ModuleName = WS.getModuleName(); |
| 1002 | Import.FieldName = WS.getName(); |
| 1003 | |
| 1004 | if (WS.isFunction()) { |
| 1005 | Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1006 | Import.Type = getFunctionType(WS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1007 | SymbolIndices[&WS] = NumFuncImports; |
| 1008 | ++NumFuncImports; |
| 1009 | } else { |
| 1010 | Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1011 | Import.Type = int32_t(PtrType); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1012 | SymbolIndices[&WS] = NumGlobalImports; |
| 1013 | ++NumGlobalImports; |
| 1014 | } |
| 1015 | |
| 1016 | Imports.push_back(Import); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 1020 | // In the special .global_variables section, we've encoded global |
| 1021 | // variables used by the function. Translate them into the Globals |
| 1022 | // list. |
| 1023 | MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", 0, 0); |
| 1024 | if (!GlobalVars->getFragmentList().empty()) { |
| 1025 | if (GlobalVars->getFragmentList().size() != 1) |
| 1026 | report_fatal_error("only one .global_variables fragment supported"); |
| 1027 | const MCFragment &Frag = *GlobalVars->begin(); |
| 1028 | if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) |
| 1029 | report_fatal_error("only data supported in .global_variables"); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1030 | const auto &DataFrag = cast<MCDataFragment>(Frag); |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 1031 | if (!DataFrag.getFixups().empty()) |
| 1032 | report_fatal_error("fixups not supported in .global_variables"); |
| 1033 | const SmallVectorImpl<char> &Contents = DataFrag.getContents(); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1034 | for (const uint8_t *p = (const uint8_t *)Contents.data(), |
| 1035 | *end = (const uint8_t *)Contents.data() + Contents.size(); |
| 1036 | p != end; ) { |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 1037 | WasmGlobal G; |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1038 | if (end - p < 3) |
| 1039 | report_fatal_error("truncated global variable encoding"); |
| 1040 | G.Type = wasm::ValType(int8_t(*p++)); |
| 1041 | G.IsMutable = bool(*p++); |
| 1042 | G.HasImport = bool(*p++); |
| 1043 | if (G.HasImport) { |
| 1044 | G.InitialValue = 0; |
| 1045 | |
| 1046 | WasmImport Import; |
| 1047 | Import.ModuleName = (const char *)p; |
| 1048 | const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p); |
| 1049 | if (!nul) |
| 1050 | report_fatal_error("global module name must be nul-terminated"); |
| 1051 | p = nul + 1; |
| 1052 | nul = (const uint8_t *)memchr(p, '\0', end - p); |
| 1053 | if (!nul) |
| 1054 | report_fatal_error("global base name must be nul-terminated"); |
| 1055 | Import.FieldName = (const char *)p; |
| 1056 | p = nul + 1; |
| 1057 | |
| 1058 | Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; |
| 1059 | Import.Type = int32_t(G.Type); |
| 1060 | |
| 1061 | G.ImportIndex = NumGlobalImports; |
| 1062 | ++NumGlobalImports; |
| 1063 | |
| 1064 | Imports.push_back(Import); |
| 1065 | } else { |
| 1066 | unsigned n; |
| 1067 | G.InitialValue = decodeSLEB128(p, &n); |
| 1068 | G.ImportIndex = 0; |
Simon Pilgrim | c8da0c0 | 2017-03-31 10:45:35 +0000 | [diff] [blame] | 1069 | if ((ptrdiff_t)n > end - p) |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1070 | report_fatal_error("global initial value must be valid SLEB128"); |
| 1071 | p += n; |
| 1072 | } |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 1073 | Globals.push_back(G); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1077 | // In the special .stack_pointer section, we've encoded the stack pointer |
| 1078 | // index. |
| 1079 | MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", 0, 0); |
| 1080 | if (!StackPtr->getFragmentList().empty()) { |
| 1081 | if (StackPtr->getFragmentList().size() != 1) |
| 1082 | report_fatal_error("only one .stack_pointer fragment supported"); |
| 1083 | const MCFragment &Frag = *StackPtr->begin(); |
| 1084 | if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) |
| 1085 | report_fatal_error("only data supported in .stack_pointer"); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1086 | const auto &DataFrag = cast<MCDataFragment>(Frag); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1087 | if (!DataFrag.getFixups().empty()) |
| 1088 | report_fatal_error("fixups not supported in .stack_pointer"); |
| 1089 | const SmallVectorImpl<char> &Contents = DataFrag.getContents(); |
| 1090 | if (Contents.size() != 4) |
| 1091 | report_fatal_error("only one entry supported in .stack_pointer"); |
| 1092 | HasStackPointer = true; |
| 1093 | StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data(); |
| 1094 | } |
| 1095 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1096 | // Handle regular defined and undefined symbols. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1097 | for (const MCSymbol &S : Asm.symbols()) { |
| 1098 | // Ignore unnamed temporary symbols, which aren't ever exported, imported, |
| 1099 | // or used in relocations. |
| 1100 | if (S.isTemporary() && S.getName().empty()) |
| 1101 | continue; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1102 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1103 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1104 | DEBUG(dbgs() << "MCSymbol: '" << S << "'" |
| 1105 | << " isDefined=" << S.isDefined() << " isExternal=" |
| 1106 | << S.isExternal() << " isTemporary=" << S.isTemporary() |
| 1107 | << " isFunction=" << WS.isFunction() |
| 1108 | << " isWeak=" << WS.isWeak() |
| 1109 | << " isVariable=" << WS.isVariable() << "\n"); |
| 1110 | |
| 1111 | if (WS.isWeak()) |
| 1112 | WeakSymbols.push_back(WS.getName()); |
| 1113 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1114 | if (WS.isVariable()) |
| 1115 | continue; |
| 1116 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1117 | unsigned Index; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1118 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1119 | if (WS.isFunction()) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1120 | if (WS.isDefined(/*SetUsed=*/false)) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1121 | if (WS.getOffset() != 0) |
| 1122 | report_fatal_error( |
| 1123 | "function sections must contain one function each"); |
| 1124 | |
| 1125 | if (WS.getSize() == 0) |
| 1126 | report_fatal_error( |
| 1127 | "function symbols must have a size set with .size"); |
| 1128 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1129 | // A definition. Take the next available index. |
| 1130 | Index = NumFuncImports + Functions.size(); |
| 1131 | |
| 1132 | // Prepare the function. |
| 1133 | WasmFunction Func; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1134 | Func.Type = getFunctionType(WS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1135 | Func.Sym = &WS; |
| 1136 | SymbolIndices[&WS] = Index; |
| 1137 | Functions.push_back(Func); |
| 1138 | } else { |
| 1139 | // An import; the index was assigned above. |
| 1140 | Index = SymbolIndices.find(&WS)->second; |
| 1141 | } |
| 1142 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1143 | DEBUG(dbgs() << " -> function index: " << Index << "\n"); |
| 1144 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1145 | // If needed, prepare the function to be called indirectly. |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1146 | if (IsAddressTaken.count(&WS) != 0) { |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 1147 | IndirectSymbolIndices[&WS] = TableElems.size(); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1148 | DEBUG(dbgs() << " -> adding to table: " << TableElems.size() << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1149 | TableElems.push_back(Index); |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 1150 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1151 | } else { |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1152 | if (WS.isTemporary() && !WS.getSize()) |
| 1153 | continue; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1154 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1155 | if (!WS.isDefined(/*SetUsed=*/false)) |
| 1156 | continue; |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1157 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1158 | if (WS.getOffset() != 0) |
| 1159 | report_fatal_error("data sections must contain one variable each: " + |
| 1160 | WS.getName()); |
| 1161 | if (!WS.getSize()) |
| 1162 | report_fatal_error("data symbols must have a size set with .size: " + |
| 1163 | WS.getName()); |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1164 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1165 | int64_t Size = 0; |
| 1166 | if (!WS.getSize()->evaluateAsAbsolute(Size, Layout)) |
| 1167 | report_fatal_error(".size expression must be evaluatable"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1168 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1169 | auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1170 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1171 | if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection)) |
| 1172 | report_fatal_error("data sections must contain at most one variable"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1173 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1174 | DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment())); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 1175 | DataAlignment = std::max(DataAlignment, DataSection.getAlignment()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1176 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1177 | DataSection.setSectionOffset(DataBytes.size()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1178 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1179 | for (const MCFragment &Frag : DataSection) { |
| 1180 | if (Frag.hasInstructions()) |
| 1181 | report_fatal_error("only data supported in data sections"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1182 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1183 | if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) { |
| 1184 | if (Align->getValueSize() != 1) |
| 1185 | report_fatal_error("only byte values supported for alignment"); |
| 1186 | // If nops are requested, use zeros, as this is the data section. |
| 1187 | uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); |
| 1188 | uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(), |
| 1189 | Align->getAlignment()), |
| 1190 | DataBytes.size() + |
| 1191 | Align->getMaxBytesToEmit()); |
| 1192 | DataBytes.resize(Size, Value); |
| 1193 | } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) { |
Sam Clegg | b03c2b4 | 2017-07-10 18:36:34 +0000 | [diff] [blame] | 1194 | DataBytes.insert(DataBytes.end(), Fill->getSize(), Fill->getValue()); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | const auto &DataFrag = cast<MCDataFragment>(Frag); |
| 1197 | const SmallVectorImpl<char> &Contents = DataFrag.getContents(); |
| 1198 | |
| 1199 | DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1200 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1201 | } |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1202 | |
| 1203 | // For each global, prepare a corresponding wasm global holding its |
| 1204 | // address. For externals these will also be named exports. |
| 1205 | Index = NumGlobalImports + Globals.size(); |
| 1206 | |
| 1207 | WasmGlobal Global; |
| 1208 | Global.Type = PtrType; |
| 1209 | Global.IsMutable = false; |
| 1210 | Global.HasImport = false; |
| 1211 | Global.InitialValue = DataSection.getSectionOffset(); |
| 1212 | Global.ImportIndex = 0; |
| 1213 | SymbolIndices[&WS] = Index; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1214 | DEBUG(dbgs() << " -> global index: " << Index << "\n"); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1215 | Globals.push_back(Global); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | // If the symbol is visible outside this translation unit, export it. |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1219 | if ((WS.isExternal() && WS.isDefined(/*SetUsed=*/false))) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1220 | WasmExport Export; |
| 1221 | Export.FieldName = WS.getName(); |
| 1222 | Export.Index = Index; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1223 | if (WS.isFunction()) |
| 1224 | Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; |
| 1225 | else |
| 1226 | Export.Kind = wasm::WASM_EXTERNAL_GLOBAL; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1227 | DEBUG(dbgs() << " -> export " << Exports.size() << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1228 | Exports.push_back(Export); |
| 1229 | } |
| 1230 | } |
| 1231 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1232 | // Handle weak aliases. We need to process these in a separate pass because |
| 1233 | // we need to have processed the target of the alias before the alias itself |
| 1234 | // and the symbols are not necessarily ordered in this way. |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1235 | for (const MCSymbol &S : Asm.symbols()) { |
| 1236 | if (!S.isVariable()) |
| 1237 | continue; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1238 | assert(S.isDefined(/*SetUsed=*/false)); |
| 1239 | |
| 1240 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1241 | // Find the target symbol of this weak alias and export that index |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1242 | const MCExpr *Expr = WS.getVariableValue(); |
Sam Clegg | 6ad8f19 | 2017-07-11 02:21:57 +0000 | [diff] [blame] | 1243 | auto *Inner = cast<MCSymbolRefExpr>(Expr); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1244 | const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol()); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1245 | DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n"); |
| 1246 | assert(SymbolIndices.count(ResolvedSym) > 0); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1247 | uint32_t Index = SymbolIndices.find(ResolvedSym)->second; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1248 | DEBUG(dbgs() << " -> index:" << Index << "\n"); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1249 | |
| 1250 | WasmExport Export; |
| 1251 | Export.FieldName = WS.getName(); |
| 1252 | Export.Index = Index; |
| 1253 | if (WS.isFunction()) |
| 1254 | Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; |
| 1255 | else |
| 1256 | Export.Kind = wasm::WASM_EXTERNAL_GLOBAL; |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1257 | DEBUG(dbgs() << " -> export " << Exports.size() << "\n"); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1258 | Exports.push_back(Export); |
| 1259 | } |
| 1260 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1261 | // Add types for indirect function calls. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 1262 | for (const WasmRelocationEntry &Fixup : CodeRelocations) { |
| 1263 | if (Fixup.Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) |
| 1264 | continue; |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1265 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1266 | registerFunctionType(*Fixup.Symbol); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1269 | // Write out the Wasm header. |
| 1270 | writeHeader(Asm); |
| 1271 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1272 | writeTypeSection(FunctionTypes); |
| 1273 | writeImportSection(Imports); |
| 1274 | writeFunctionSection(Functions); |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 1275 | writeTableSection(TableElems.size()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1276 | writeMemorySection(DataBytes); |
| 1277 | writeGlobalSection(Globals); |
| 1278 | writeExportSection(Exports); |
| 1279 | // TODO: Start Section |
| 1280 | writeElemSection(TableElems); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 1281 | writeCodeSection(Asm, Layout, Functions); |
| 1282 | uint64_t DataSectionHeaderSize = writeDataSection(DataBytes); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1283 | writeNameSection(Functions, Imports, NumFuncImports); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 1284 | writeCodeRelocSection(); |
| 1285 | writeDataRelocSection(DataSectionHeaderSize); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 1286 | writeLinkingMetaDataSection(DataBytes.size(), DataAlignment, WeakSymbols, HasStackPointer, StackPointerGlobal); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1287 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1288 | // TODO: Translate the .comment section to the output. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1289 | // TODO: Translate debug sections to the output. |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | MCObjectWriter *llvm::createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW, |
| 1293 | raw_pwrite_stream &OS) { |
| 1294 | return new WasmObjectWriter(MOTW, OS); |
| 1295 | } |