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