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