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