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