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