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