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