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