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