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