Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- Writer.cpp ---------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Writer.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 10 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 11 | #include "InputChunks.h" |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 12 | #include "InputEvent.h" |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 13 | #include "InputGlobal.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 14 | #include "OutputSections.h" |
| 15 | #include "OutputSegment.h" |
| 16 | #include "SymbolTable.h" |
| 17 | #include "WriterUtils.h" |
| 18 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 19 | #include "lld/Common/Memory.h" |
Nicholas Wilson | 8269f37 | 2018-03-07 10:37:50 +0000 | [diff] [blame] | 20 | #include "lld/Common/Strings.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 21 | #include "lld/Common/Threads.h" |
Sam Clegg | 3141ddc | 2018-02-20 21:53:18 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseSet.h" |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 25 | #include "llvm/BinaryFormat/Wasm.h" |
Nicholas Wilson | 3e3f5fb | 2018-03-14 15:58:16 +0000 | [diff] [blame] | 26 | #include "llvm/Object/WasmTraits.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileOutputBuffer.h" |
| 28 | #include "llvm/Support/Format.h" |
| 29 | #include "llvm/Support/FormatVariadic.h" |
| 30 | #include "llvm/Support/LEB128.h" |
| 31 | |
| 32 | #include <cstdarg> |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 33 | #include <map> |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 34 | |
| 35 | #define DEBUG_TYPE "lld" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::wasm; |
| 39 | using namespace lld; |
| 40 | using namespace lld::wasm; |
| 41 | |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 42 | static constexpr int StackAlignment = 16; |
| 43 | static constexpr const char *FunctionTableName = "__indirect_function_table"; |
| 44 | const char *lld::wasm::DefaultModule = "env"; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 48 | // An init entry to be written to either the synthetic init func or the |
| 49 | // linking metadata. |
| 50 | struct WasmInitEntry { |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 51 | const FunctionSymbol *Sym; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 52 | uint32_t Priority; |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 55 | // The writer writes a SymbolTable result to a file. |
| 56 | class Writer { |
| 57 | public: |
| 58 | void run(); |
| 59 | |
| 60 | private: |
| 61 | void openFile(); |
| 62 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 63 | uint32_t lookupType(const WasmSignature &Sig); |
| 64 | uint32_t registerType(const WasmSignature &Sig); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 65 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 66 | void createCtorFunction(); |
| 67 | void calculateInitFunctions(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 68 | void assignIndexes(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 69 | void calculateImports(); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 70 | void calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 71 | void calculateCustomSections(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 72 | void assignSymtab(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 73 | void calculateTypes(); |
| 74 | void createOutputSegments(); |
| 75 | void layoutMemory(); |
| 76 | void createHeader(); |
| 77 | void createSections(); |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 78 | SyntheticSection *createSyntheticSection(uint32_t Type, StringRef Name = ""); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 79 | |
| 80 | // Builtin sections |
| 81 | void createTypeSection(); |
| 82 | void createFunctionSection(); |
| 83 | void createTableSection(); |
| 84 | void createGlobalSection(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 85 | void createEventSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 86 | void createExportSection(); |
| 87 | void createImportSection(); |
| 88 | void createMemorySection(); |
| 89 | void createElemSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 90 | void createCodeSection(); |
| 91 | void createDataSection(); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 92 | void createCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 93 | |
| 94 | // Custom sections |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 95 | void createDylinkSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 96 | void createRelocSections(); |
| 97 | void createLinkingSection(); |
| 98 | void createNameSection(); |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 99 | void createProducersSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 100 | |
| 101 | void writeHeader(); |
| 102 | void writeSections(); |
| 103 | |
| 104 | uint64_t FileSize = 0; |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 105 | uint32_t TableBase = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 106 | uint32_t NumMemoryPages = 0; |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 107 | uint32_t MaxMemoryPages = 0; |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 108 | // Memory size and aligment. Written to the "dylink" section |
| 109 | // when build with -shared or -pie. |
| 110 | uint32_t MemAlign = 0; |
| 111 | uint32_t MemSize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 112 | |
| 113 | std::vector<const WasmSignature *> Types; |
Nicholas Wilson | 3e3f5fb | 2018-03-14 15:58:16 +0000 | [diff] [blame] | 114 | DenseMap<WasmSignature, int32_t> TypeIndices; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 115 | std::vector<const Symbol *> ImportedSymbols; |
| 116 | unsigned NumImportedFunctions = 0; |
| 117 | unsigned NumImportedGlobals = 0; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 118 | unsigned NumImportedEvents = 0; |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 119 | std::vector<WasmExport> Exports; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 120 | std::vector<const DefinedData *> DefinedFakeGlobals; |
| 121 | std::vector<InputGlobal *> InputGlobals; |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 122 | std::vector<InputFunction *> InputFunctions; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 123 | std::vector<InputEvent *> InputEvents; |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 124 | std::vector<const FunctionSymbol *> IndirectFunctions; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 125 | std::vector<const Symbol *> SymtabEntries; |
| 126 | std::vector<WasmInitEntry> InitFunctions; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 127 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 128 | llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 129 | llvm::StringMap<SectionSymbol *> CustomSectionSymbols; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 130 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 131 | // Elements that are used to construct the final output |
| 132 | std::string Header; |
| 133 | std::vector<OutputSection *> OutputSections; |
| 134 | |
| 135 | std::unique_ptr<FileOutputBuffer> Buffer; |
| 136 | |
| 137 | std::vector<OutputSegment *> Segments; |
| 138 | llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; |
| 139 | }; |
| 140 | |
| 141 | } // anonymous namespace |
| 142 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 143 | void Writer::createImportSection() { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 144 | uint32_t NumImports = ImportedSymbols.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 145 | if (Config->ImportMemory) |
| 146 | ++NumImports; |
Nicholas Wilson | fc90b30 | 2018-03-28 12:53:29 +0000 | [diff] [blame] | 147 | if (Config->ImportTable) |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 148 | ++NumImports; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 149 | |
| 150 | if (NumImports == 0) |
| 151 | return; |
| 152 | |
| 153 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); |
| 154 | raw_ostream &OS = Section->getStream(); |
| 155 | |
| 156 | writeUleb128(OS, NumImports, "import count"); |
| 157 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 158 | if (Config->ImportMemory) { |
| 159 | WasmImport Import; |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 160 | Import.Module = DefaultModule; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 161 | Import.Field = "memory"; |
| 162 | Import.Kind = WASM_EXTERNAL_MEMORY; |
| 163 | Import.Memory.Flags = 0; |
| 164 | Import.Memory.Initial = NumMemoryPages; |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 165 | if (MaxMemoryPages != 0) { |
| 166 | Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX; |
| 167 | Import.Memory.Maximum = MaxMemoryPages; |
| 168 | } |
Derek Schuff | 786760a | 2018-11-06 18:02:39 +0000 | [diff] [blame] | 169 | if (Config->SharedMemory) |
Derek Schuff | 3bea8bc | 2018-11-06 17:59:32 +0000 | [diff] [blame] | 170 | Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 171 | writeImport(OS, Import); |
| 172 | } |
| 173 | |
Nicholas Wilson | fc90b30 | 2018-03-28 12:53:29 +0000 | [diff] [blame] | 174 | if (Config->ImportTable) { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 175 | uint32_t TableSize = TableBase + IndirectFunctions.size(); |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 176 | WasmImport Import; |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 177 | Import.Module = DefaultModule; |
| 178 | Import.Field = FunctionTableName; |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 179 | Import.Kind = WASM_EXTERNAL_TABLE; |
Thomas Lively | 25ff893 | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 180 | Import.Table.ElemType = WASM_TYPE_FUNCREF; |
Sam Clegg | 748f59cae | 2018-12-03 22:37:55 +0000 | [diff] [blame] | 181 | Import.Table.Limits = {0, TableSize, 0}; |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 182 | writeImport(OS, Import); |
| 183 | } |
| 184 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 185 | for (const Symbol *Sym : ImportedSymbols) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 186 | WasmImport Import; |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 187 | if (auto *F = dyn_cast<UndefinedFunction>(Sym)) { |
| 188 | Import.Field = F->ImportName; |
| 189 | Import.Module = F->ImportModule; |
| 190 | } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) { |
| 191 | Import.Field = G->ImportName; |
| 192 | Import.Module = G->ImportModule; |
| 193 | } else { |
| 194 | Import.Field = Sym->getName(); |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 195 | Import.Module = DefaultModule; |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 196 | } |
Sam Clegg | 7cc0753 | 2019-02-01 02:29:57 +0000 | [diff] [blame] | 197 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 198 | if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) { |
| 199 | Import.Kind = WASM_EXTERNAL_FUNCTION; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 200 | Import.SigIndex = lookupType(*FunctionSym->Signature); |
Sam Clegg | d425d6b | 2019-03-12 21:53:23 +0000 | [diff] [blame] | 201 | } else if (auto *DataSym = dyn_cast<UndefinedData>(Sym)) { |
| 202 | Import.Kind = WASM_EXTERNAL_GLOBAL; |
| 203 | Import.Global = {WASM_TYPE_I32, true}; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 204 | } else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 205 | Import.Kind = WASM_EXTERNAL_GLOBAL; |
| 206 | Import.Global = *GlobalSym->getGlobalType(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 207 | } else { |
| 208 | auto *EventSym = cast<EventSymbol>(Sym); |
| 209 | Import.Kind = WASM_EXTERNAL_EVENT; |
| 210 | Import.Event.Attribute = EventSym->getEventType()->Attribute; |
| 211 | Import.Event.SigIndex = lookupType(*EventSym->Signature); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 212 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 213 | writeImport(OS, Import); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void Writer::createTypeSection() { |
| 218 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE); |
| 219 | raw_ostream &OS = Section->getStream(); |
| 220 | writeUleb128(OS, Types.size(), "type count"); |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 221 | for (const WasmSignature *Sig : Types) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 222 | writeSig(OS, *Sig); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void Writer::createFunctionSection() { |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 226 | if (InputFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 227 | return; |
| 228 | |
| 229 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION); |
| 230 | raw_ostream &OS = Section->getStream(); |
| 231 | |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 232 | writeUleb128(OS, InputFunctions.size(), "function count"); |
| 233 | for (const InputFunction *Func : InputFunctions) |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 234 | writeUleb128(OS, lookupType(Func->Signature), "sig index"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void Writer::createMemorySection() { |
| 238 | if (Config->ImportMemory) |
| 239 | return; |
| 240 | |
| 241 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY); |
| 242 | raw_ostream &OS = Section->getStream(); |
| 243 | |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 244 | bool HasMax = MaxMemoryPages != 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 245 | writeUleb128(OS, 1, "memory count"); |
Derek Schuff | 786760a | 2018-11-06 18:02:39 +0000 | [diff] [blame] | 246 | unsigned Flags = 0; |
| 247 | if (HasMax) |
| 248 | Flags |= WASM_LIMITS_FLAG_HAS_MAX; |
Derek Schuff | 3bea8bc | 2018-11-06 17:59:32 +0000 | [diff] [blame] | 249 | if (Config->SharedMemory) |
| 250 | Flags |= WASM_LIMITS_FLAG_IS_SHARED; |
| 251 | writeUleb128(OS, Flags, "memory limits flags"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 252 | writeUleb128(OS, NumMemoryPages, "initial pages"); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 253 | if (HasMax) |
| 254 | writeUleb128(OS, MaxMemoryPages, "max pages"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void Writer::createGlobalSection() { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 258 | unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size(); |
| 259 | if (NumGlobals == 0) |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 260 | return; |
| 261 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 262 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL); |
| 263 | raw_ostream &OS = Section->getStream(); |
| 264 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 265 | writeUleb128(OS, NumGlobals, "global count"); |
| 266 | for (const InputGlobal *G : InputGlobals) |
| 267 | writeGlobal(OS, G->Global); |
| 268 | for (const DefinedData *Sym : DefinedFakeGlobals) { |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 269 | WasmGlobal Global; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 270 | Global.Type = {WASM_TYPE_I32, false}; |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 271 | Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 272 | Global.InitExpr.Value.Int32 = Sym->getVirtualAddress(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 273 | writeGlobal(OS, Global); |
| 274 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 277 | // The event section contains a list of declared wasm events associated with the |
| 278 | // module. Currently the only supported event kind is exceptions. A single event |
| 279 | // entry represents a single event with an event tag. All C++ exceptions are |
| 280 | // represented by a single event. An event entry in this section contains |
| 281 | // information on what kind of event it is (e.g. exception) and the type of |
| 282 | // values contained in a single event object. (In wasm, an event can contain |
| 283 | // multiple values of primitive types. But for C++ exceptions, we just throw a |
| 284 | // pointer which is an i32 value (for wasm32 architecture), so the signature of |
| 285 | // C++ exception is (i32)->(void), because all event types are assumed to have |
| 286 | // void return type to share WasmSignature with functions.) |
| 287 | void Writer::createEventSection() { |
| 288 | unsigned NumEvents = InputEvents.size(); |
| 289 | if (NumEvents == 0) |
| 290 | return; |
| 291 | |
| 292 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_EVENT); |
| 293 | raw_ostream &OS = Section->getStream(); |
| 294 | |
| 295 | writeUleb128(OS, NumEvents, "event count"); |
| 296 | for (InputEvent *E : InputEvents) { |
| 297 | E->Event.Type.SigIndex = lookupType(E->Signature); |
| 298 | writeEvent(OS, E->Event); |
| 299 | } |
| 300 | } |
| 301 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 302 | void Writer::createTableSection() { |
Nicholas Wilson | fc90b30 | 2018-03-28 12:53:29 +0000 | [diff] [blame] | 303 | if (Config->ImportTable) |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 304 | return; |
| 305 | |
| 306 | // Always output a table section (or table import), even if there are no |
| 307 | // indirect calls. There are two reasons for this: |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 308 | // 1. For executables it is useful to have an empty table slot at 0 |
| 309 | // which can be filled with a null function call handler. |
| 310 | // 2. If we don't do this, any program that contains a call_indirect but |
| 311 | // no address-taken function will fail at validation time since it is |
| 312 | // a validation error to include a call_indirect instruction if there |
| 313 | // is not table. |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 314 | uint32_t TableSize = TableBase + IndirectFunctions.size(); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 315 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 316 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE); |
| 317 | raw_ostream &OS = Section->getStream(); |
| 318 | |
| 319 | writeUleb128(OS, 1, "table count"); |
Nicholas Wilson | 874eedd | 2018-03-27 17:38:51 +0000 | [diff] [blame] | 320 | WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize}; |
Thomas Lively | 25ff893 | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 321 | writeTableType(OS, WasmTable{WASM_TYPE_FUNCREF, Limits}); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void Writer::createExportSection() { |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 325 | if (!Exports.size()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 326 | return; |
| 327 | |
| 328 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); |
| 329 | raw_ostream &OS = Section->getStream(); |
| 330 | |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 331 | writeUleb128(OS, Exports.size(), "export count"); |
| 332 | for (const WasmExport &Export : Exports) |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 333 | writeExport(OS, Export); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 336 | void Writer::calculateCustomSections() { |
| 337 | log("calculateCustomSections"); |
| 338 | bool StripDebug = Config->StripDebug || Config->StripAll; |
| 339 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 340 | for (InputSection *Section : File->CustomSections) { |
| 341 | StringRef Name = Section->getName(); |
| 342 | // These custom sections are known the linker and synthesized rather than |
| 343 | // blindly copied |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 344 | if (Name == "linking" || Name == "name" || Name == "producers" || |
| 345 | Name.startswith("reloc.")) |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 346 | continue; |
| 347 | // .. or it is a debug section |
| 348 | if (StripDebug && Name.startswith(".debug_")) |
| 349 | continue; |
| 350 | CustomSectionMapping[Name].push_back(Section); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 355 | void Writer::createCustomSections() { |
| 356 | log("createCustomSections"); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 357 | for (auto &Pair : CustomSectionMapping) { |
| 358 | StringRef Name = Pair.first(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 359 | |
| 360 | auto P = CustomSectionSymbols.find(Name); |
| 361 | if (P != CustomSectionSymbols.end()) { |
| 362 | uint32_t SectionIndex = OutputSections.size(); |
| 363 | P->second->setOutputSectionIndex(SectionIndex); |
| 364 | } |
| 365 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 366 | LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n"); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 367 | OutputSections.push_back(make<CustomSection>(Name, Pair.second)); |
| 368 | } |
| 369 | } |
| 370 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 371 | void Writer::createElemSection() { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 372 | if (IndirectFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 373 | return; |
| 374 | |
| 375 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM); |
| 376 | raw_ostream &OS = Section->getStream(); |
| 377 | |
| 378 | writeUleb128(OS, 1, "segment count"); |
| 379 | writeUleb128(OS, 0, "table index"); |
| 380 | WasmInitExpr InitExpr; |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 381 | if (Config->Pic) { |
Thomas Lively | 25ff893 | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 382 | InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET; |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 383 | InitExpr.Value.Global = WasmSym::TableBase->getGlobalIndex(); |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 384 | } else { |
| 385 | InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 386 | InitExpr.Value.Int32 = TableBase; |
| 387 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 388 | writeInitExpr(OS, InitExpr); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 389 | writeUleb128(OS, IndirectFunctions.size(), "elem count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 390 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 391 | uint32_t TableIndex = TableBase; |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 392 | for (const FunctionSymbol *Sym : IndirectFunctions) { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 393 | assert(Sym->getTableIndex() == TableIndex); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 394 | writeUleb128(OS, Sym->getFunctionIndex(), "function index"); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 395 | ++TableIndex; |
| 396 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void Writer::createCodeSection() { |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 400 | if (InputFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 401 | return; |
| 402 | |
| 403 | log("createCodeSection"); |
| 404 | |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 405 | auto Section = make<CodeSection>(InputFunctions); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 406 | OutputSections.push_back(Section); |
| 407 | } |
| 408 | |
| 409 | void Writer::createDataSection() { |
| 410 | if (!Segments.size()) |
| 411 | return; |
| 412 | |
| 413 | log("createDataSection"); |
| 414 | auto Section = make<DataSection>(Segments); |
| 415 | OutputSections.push_back(Section); |
| 416 | } |
| 417 | |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 418 | // Create relocations sections in the final output. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 419 | // These are only created when relocatable output is requested. |
| 420 | void Writer::createRelocSections() { |
| 421 | log("createRelocSections"); |
| 422 | // Don't use iterator here since we are adding to OutputSection |
| 423 | size_t OrigSize = OutputSections.size(); |
Rui Ueyama | ffa650a | 2018-04-24 23:09:57 +0000 | [diff] [blame] | 424 | for (size_t I = 0; I < OrigSize; I++) { |
| 425 | OutputSection *OSec = OutputSections[I]; |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 426 | uint32_t Count = OSec->numRelocations(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 427 | if (!Count) |
| 428 | continue; |
| 429 | |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 430 | StringRef Name; |
| 431 | if (OSec->Type == WASM_SEC_DATA) |
| 432 | Name = "reloc.DATA"; |
| 433 | else if (OSec->Type == WASM_SEC_CODE) |
| 434 | Name = "reloc.CODE"; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 435 | else if (OSec->Type == WASM_SEC_CUSTOM) |
| 436 | Name = Saver.save("reloc." + OSec->Name); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 437 | else |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 438 | llvm_unreachable( |
| 439 | "relocations only supported for code, data, or custom sections"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 440 | |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 441 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 442 | raw_ostream &OS = Section->getStream(); |
Rui Ueyama | ffa650a | 2018-04-24 23:09:57 +0000 | [diff] [blame] | 443 | writeUleb128(OS, I, "reloc section"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 444 | writeUleb128(OS, Count, "reloc count"); |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 445 | OSec->writeRelocations(OS); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Rui Ueyama | 8bfa2a6 | 2018-02-28 00:28:07 +0000 | [diff] [blame] | 449 | static uint32_t getWasmFlags(const Symbol *Sym) { |
| 450 | uint32_t Flags = 0; |
| 451 | if (Sym->isLocal()) |
| 452 | Flags |= WASM_SYMBOL_BINDING_LOCAL; |
| 453 | if (Sym->isWeak()) |
| 454 | Flags |= WASM_SYMBOL_BINDING_WEAK; |
| 455 | if (Sym->isHidden()) |
| 456 | Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN; |
| 457 | if (Sym->isUndefined()) |
| 458 | Flags |= WASM_SYMBOL_UNDEFINED; |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 459 | if (auto *F = dyn_cast<UndefinedFunction>(Sym)) { |
| 460 | if (F->getName() != F->ImportName) |
| 461 | Flags |= WASM_SYMBOL_EXPLICIT_NAME; |
| 462 | } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) { |
| 463 | if (G->getName() != G->ImportName) |
| 464 | Flags |= WASM_SYMBOL_EXPLICIT_NAME; |
| 465 | } |
Rui Ueyama | 8bfa2a6 | 2018-02-28 00:28:07 +0000 | [diff] [blame] | 466 | return Flags; |
| 467 | } |
| 468 | |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 469 | // Some synthetic sections (e.g. "name" and "linking") have subsections. |
| 470 | // Just like the synthetic sections themselves these need to be created before |
| 471 | // they can be written out (since they are preceded by their length). This |
| 472 | // class is used to create subsections and then write them into the stream |
| 473 | // of the parent section. |
| 474 | class SubSection { |
| 475 | public: |
| 476 | explicit SubSection(uint32_t Type) : Type(Type) {} |
| 477 | |
| 478 | void writeTo(raw_ostream &To) { |
| 479 | OS.flush(); |
Rui Ueyama | 6776910 | 2018-02-28 03:38:14 +0000 | [diff] [blame] | 480 | writeUleb128(To, Type, "subsection type"); |
| 481 | writeUleb128(To, Body.size(), "subsection size"); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 482 | To.write(Body.data(), Body.size()); |
| 483 | } |
| 484 | |
| 485 | private: |
| 486 | uint32_t Type; |
| 487 | std::string Body; |
| 488 | |
| 489 | public: |
| 490 | raw_string_ostream OS{Body}; |
| 491 | }; |
| 492 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 493 | // Create the custom "dylink" section containing information for the dynamic |
| 494 | // linker. |
| 495 | // See |
| 496 | // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md |
| 497 | void Writer::createDylinkSection() { |
| 498 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "dylink"); |
| 499 | raw_ostream &OS = Section->getStream(); |
| 500 | |
| 501 | writeUleb128(OS, MemSize, "MemSize"); |
Sam Clegg | 6320efb | 2019-01-16 01:43:21 +0000 | [diff] [blame] | 502 | writeUleb128(OS, MemAlign, "MemAlign"); |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 503 | writeUleb128(OS, IndirectFunctions.size(), "TableSize"); |
| 504 | writeUleb128(OS, 0, "TableAlign"); |
Sam Clegg | e01c646 | 2018-12-12 23:44:59 +0000 | [diff] [blame] | 505 | writeUleb128(OS, 0, "Needed"); // TODO: Support "needed" shared libraries |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 508 | // Create the custom "linking" section containing linker metadata. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 509 | // This is only created when relocatable output is requested. |
| 510 | void Writer::createLinkingSection() { |
| 511 | SyntheticSection *Section = |
| 512 | createSyntheticSection(WASM_SEC_CUSTOM, "linking"); |
| 513 | raw_ostream &OS = Section->getStream(); |
| 514 | |
Sam Clegg | 2b8b179 | 2018-04-26 18:17:21 +0000 | [diff] [blame] | 515 | writeUleb128(OS, WasmMetadataVersion, "Version"); |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 516 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 517 | if (!SymtabEntries.empty()) { |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 518 | SubSection Sub(WASM_SYMBOL_TABLE); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 519 | writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols"); |
| 520 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 521 | for (const Symbol *Sym : SymtabEntries) { |
| 522 | assert(Sym->isDefined() || Sym->isUndefined()); |
| 523 | WasmSymbolType Kind = Sym->getWasmType(); |
Rui Ueyama | 8bfa2a6 | 2018-02-28 00:28:07 +0000 | [diff] [blame] | 524 | uint32_t Flags = getWasmFlags(Sym); |
| 525 | |
Sam Clegg | 8518e7d | 2018-03-01 18:06:39 +0000 | [diff] [blame] | 526 | writeU8(Sub.OS, Kind, "sym kind"); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 527 | writeUleb128(Sub.OS, Flags, "sym flags"); |
Rui Ueyama | 8bfa2a6 | 2018-02-28 00:28:07 +0000 | [diff] [blame] | 528 | |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 529 | if (auto *F = dyn_cast<FunctionSymbol>(Sym)) { |
| 530 | writeUleb128(Sub.OS, F->getFunctionIndex(), "index"); |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 531 | if (Sym->isDefined() || |
| 532 | (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0) |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 533 | writeStr(Sub.OS, Sym->getName(), "sym name"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 534 | } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) { |
| 535 | writeUleb128(Sub.OS, G->getGlobalIndex(), "index"); |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 536 | if (Sym->isDefined() || |
| 537 | (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0) |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 538 | writeStr(Sub.OS, Sym->getName(), "sym name"); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 539 | } else if (auto *E = dyn_cast<EventSymbol>(Sym)) { |
| 540 | writeUleb128(Sub.OS, E->getEventIndex(), "index"); |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 541 | if (Sym->isDefined() || |
| 542 | (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0) |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 543 | writeStr(Sub.OS, Sym->getName(), "sym name"); |
Andrea Di Biagio | 25c1a2f | 2018-05-05 10:53:31 +0000 | [diff] [blame] | 544 | } else if (isa<DataSymbol>(Sym)) { |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 545 | writeStr(Sub.OS, Sym->getName(), "sym name"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 546 | if (auto *DataSym = dyn_cast<DefinedData>(Sym)) { |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 547 | writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index"); |
| 548 | writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(), |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 549 | "data offset"); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 550 | writeUleb128(Sub.OS, DataSym->getSize(), "data size"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 551 | } |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 552 | } else { |
| 553 | auto *S = cast<SectionSymbol>(Sym); |
| 554 | writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 555 | } |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 556 | } |
Rui Ueyama | 8bfa2a6 | 2018-02-28 00:28:07 +0000 | [diff] [blame] | 557 | |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 558 | Sub.writeTo(OS); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 561 | if (Segments.size()) { |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 562 | SubSection Sub(WASM_SEGMENT_INFO); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 563 | writeUleb128(Sub.OS, Segments.size(), "num data segments"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 564 | for (const OutputSegment *S : Segments) { |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 565 | writeStr(Sub.OS, S->Name, "segment name"); |
| 566 | writeUleb128(Sub.OS, S->Alignment, "alignment"); |
| 567 | writeUleb128(Sub.OS, 0, "flags"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 568 | } |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 569 | Sub.writeTo(OS); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 570 | } |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 571 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 572 | if (!InitFunctions.empty()) { |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 573 | SubSection Sub(WASM_INIT_FUNCS); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 574 | writeUleb128(Sub.OS, InitFunctions.size(), "num init functions"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 575 | for (const WasmInitEntry &F : InitFunctions) { |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 576 | writeUleb128(Sub.OS, F.Priority, "priority"); |
| 577 | writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index"); |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 578 | } |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 579 | Sub.writeTo(OS); |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 580 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 581 | |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 582 | struct ComdatEntry { |
| 583 | unsigned Kind; |
| 584 | uint32_t Index; |
| 585 | }; |
| 586 | std::map<StringRef, std::vector<ComdatEntry>> Comdats; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 587 | |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 588 | for (const InputFunction *F : InputFunctions) { |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 589 | StringRef Comdat = F->getComdatName(); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 590 | if (!Comdat.empty()) |
| 591 | Comdats[Comdat].emplace_back( |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 592 | ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()}); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 593 | } |
| 594 | for (uint32_t I = 0; I < Segments.size(); ++I) { |
Sam Clegg | f98bccf | 2018-01-13 15:57:48 +0000 | [diff] [blame] | 595 | const auto &InputSegments = Segments[I]->InputSegments; |
| 596 | if (InputSegments.empty()) |
| 597 | continue; |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 598 | StringRef Comdat = InputSegments[0]->getComdatName(); |
Sam Clegg | a697df52 | 2018-01-13 15:59:53 +0000 | [diff] [blame] | 599 | #ifndef NDEBUG |
Sam Clegg | f98bccf | 2018-01-13 15:57:48 +0000 | [diff] [blame] | 600 | for (const InputSegment *IS : InputSegments) |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 601 | assert(IS->getComdatName() == Comdat); |
Sam Clegg | a697df52 | 2018-01-13 15:59:53 +0000 | [diff] [blame] | 602 | #endif |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 603 | if (!Comdat.empty()) |
| 604 | Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I}); |
| 605 | } |
| 606 | |
| 607 | if (!Comdats.empty()) { |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 608 | SubSection Sub(WASM_COMDAT_INFO); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 609 | writeUleb128(Sub.OS, Comdats.size(), "num comdats"); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 610 | for (const auto &C : Comdats) { |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 611 | writeStr(Sub.OS, C.first, "comdat name"); |
| 612 | writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use |
| 613 | writeUleb128(Sub.OS, C.second.size(), "num entries"); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 614 | for (const ComdatEntry &Entry : C.second) { |
Sam Clegg | 8518e7d | 2018-03-01 18:06:39 +0000 | [diff] [blame] | 615 | writeU8(Sub.OS, Entry.Kind, "entry kind"); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 616 | writeUleb128(Sub.OS, Entry.Index, "entry index"); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 619 | Sub.writeTo(OS); |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 620 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Create the custom "name" section containing debug symbol names. |
| 624 | void Writer::createNameSection() { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 625 | unsigned NumNames = NumImportedFunctions; |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 626 | for (const InputFunction *F : InputFunctions) |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 627 | if (!F->getName().empty() || !F->getDebugName().empty()) |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 628 | ++NumNames; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 629 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 630 | if (NumNames == 0) |
| 631 | return; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 632 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 633 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name"); |
| 634 | |
Rui Ueyama | 19eedbf | 2018-02-28 00:39:30 +0000 | [diff] [blame] | 635 | SubSection Sub(WASM_NAMES_FUNCTION); |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 636 | writeUleb128(Sub.OS, NumNames, "name count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 637 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 638 | // Names must appear in function index order. As it happens ImportedSymbols |
| 639 | // and InputFunctions are numbered in order with imported functions coming |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 640 | // first. |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 641 | for (const Symbol *S : ImportedSymbols) { |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 642 | if (auto *F = dyn_cast<FunctionSymbol>(S)) { |
| 643 | writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); |
Sam Clegg | 37125f0 | 2018-11-09 16:57:41 +0000 | [diff] [blame] | 644 | writeStr(Sub.OS, toString(*S), "symbol name"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 645 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 646 | } |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 647 | for (const InputFunction *F : InputFunctions) { |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 648 | if (!F->getName().empty()) { |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 649 | writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 650 | if (!F->getDebugName().empty()) { |
| 651 | writeStr(Sub.OS, F->getDebugName(), "symbol name"); |
| 652 | } else { |
Sam Clegg | 37125f0 | 2018-11-09 16:57:41 +0000 | [diff] [blame] | 653 | writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name"); |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 654 | } |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 657 | |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 +0000 | [diff] [blame] | 658 | Sub.writeTo(Section->getStream()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 661 | void Writer::createProducersSection() { |
| 662 | SmallVector<std::pair<std::string, std::string>, 8> Languages; |
| 663 | SmallVector<std::pair<std::string, std::string>, 8> Tools; |
| 664 | SmallVector<std::pair<std::string, std::string>, 8> SDKs; |
| 665 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 666 | const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo(); |
| 667 | for (auto &Producers : {std::make_pair(&Info.Languages, &Languages), |
| 668 | std::make_pair(&Info.Tools, &Tools), |
| 669 | std::make_pair(&Info.SDKs, &SDKs)}) |
| 670 | for (auto &Producer : *Producers.first) |
| 671 | if (Producers.second->end() == |
| 672 | std::find_if(Producers.second->begin(), Producers.second->end(), |
| 673 | [&](std::pair<std::string, std::string> Seen) { |
| 674 | return Seen.first == Producer.first; |
| 675 | })) |
| 676 | Producers.second->push_back(Producer); |
| 677 | } |
| 678 | int FieldCount = |
| 679 | int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty()); |
| 680 | if (FieldCount == 0) |
| 681 | return; |
| 682 | SyntheticSection *Section = |
| 683 | createSyntheticSection(WASM_SEC_CUSTOM, "producers"); |
| 684 | auto &OS = Section->getStream(); |
| 685 | writeUleb128(OS, FieldCount, "field count"); |
| 686 | for (auto &Field : |
| 687 | {std::make_pair("language", Languages), |
| 688 | std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) { |
| 689 | if (Field.second.empty()) |
| 690 | continue; |
| 691 | writeStr(OS, Field.first, "field name"); |
| 692 | writeUleb128(OS, Field.second.size(), "number of entries"); |
| 693 | for (auto &Entry : Field.second) { |
| 694 | writeStr(OS, Entry.first, "producer name"); |
| 695 | writeStr(OS, Entry.second, "producer version"); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 700 | void Writer::writeHeader() { |
| 701 | memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); |
| 702 | } |
| 703 | |
| 704 | void Writer::writeSections() { |
| 705 | uint8_t *Buf = Buffer->getBufferStart(); |
| 706 | parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); }); |
| 707 | } |
| 708 | |
| 709 | // Fix the memory layout of the output binary. This assigns memory offsets |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 710 | // to each of the input data sections as well as the explicit stack region. |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 711 | // The default memory layout is as follows, from low to high. |
| 712 | // |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 713 | // - initialized data (starting at Config->GlobalBase) |
| 714 | // - BSS data (not currently implemented in llvm) |
| 715 | // - explicit stack (Config->ZStackSize) |
| 716 | // - heap start / unallocated |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 717 | // |
| 718 | // The --stack-first option means that stack is placed before any static data. |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 719 | // This can be useful since it means that stack overflow traps immediately |
| 720 | // rather than overwriting global data, but also increases code size since all |
| 721 | // static data loads and stores requires larger offsets. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 722 | void Writer::layoutMemory() { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 723 | createOutputSegments(); |
| 724 | |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 725 | uint32_t MemoryPtr = 0; |
| 726 | |
| 727 | auto PlaceStack = [&]() { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 728 | if (Config->Relocatable || Config->Shared) |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 729 | return; |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 730 | MemoryPtr = alignTo(MemoryPtr, StackAlignment); |
| 731 | if (Config->ZStackSize != alignTo(Config->ZStackSize, StackAlignment)) |
| 732 | error("stack size must be " + Twine(StackAlignment) + "-byte aligned"); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 733 | log("mem: stack size = " + Twine(Config->ZStackSize)); |
| 734 | log("mem: stack base = " + Twine(MemoryPtr)); |
| 735 | MemoryPtr += Config->ZStackSize; |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 736 | auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer); |
| 737 | SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr; |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 738 | log("mem: stack top = " + Twine(MemoryPtr)); |
| 739 | }; |
| 740 | |
| 741 | if (Config->StackFirst) { |
| 742 | PlaceStack(); |
| 743 | } else { |
| 744 | MemoryPtr = Config->GlobalBase; |
| 745 | log("mem: global base = " + Twine(Config->GlobalBase)); |
| 746 | } |
| 747 | |
| 748 | uint32_t DataStart = MemoryPtr; |
| 749 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 750 | // Arbitrarily set __dso_handle handle to point to the start of the data |
| 751 | // segments. |
| 752 | if (WasmSym::DsoHandle) |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 753 | WasmSym::DsoHandle->setVirtualAddress(DataStart); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 754 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 755 | MemAlign = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 756 | for (OutputSegment *Seg : Segments) { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 757 | MemAlign = std::max(MemAlign, Seg->Alignment); |
Sam Clegg | 622ad04 | 2019-01-17 22:09:09 +0000 | [diff] [blame] | 758 | MemoryPtr = alignTo(MemoryPtr, 1ULL << Seg->Alignment); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 759 | Seg->StartVA = MemoryPtr; |
Nicholas Wilson | a06a355 | 2018-03-14 13:50:20 +0000 | [diff] [blame] | 760 | log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name, |
| 761 | MemoryPtr, Seg->Size, Seg->Alignment)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 762 | MemoryPtr += Seg->Size; |
| 763 | } |
| 764 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 765 | // TODO: Add .bss space here. |
Sam Clegg | 37a4a8a | 2018-02-07 03:04:53 +0000 | [diff] [blame] | 766 | if (WasmSym::DataEnd) |
| 767 | WasmSym::DataEnd->setVirtualAddress(MemoryPtr); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 768 | |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 769 | log("mem: static data = " + Twine(MemoryPtr - DataStart)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 770 | |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 771 | if (Config->Shared) { |
| 772 | MemSize = MemoryPtr; |
| 773 | return; |
| 774 | } |
| 775 | |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 776 | if (!Config->StackFirst) |
| 777 | PlaceStack(); |
| 778 | |
| 779 | // Set `__heap_base` to directly follow the end of the stack or global data. |
| 780 | // The fact that this comes last means that a malloc/brk implementation |
| 781 | // can grow the heap at runtime. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 782 | if (!Config->Relocatable) { |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 783 | WasmSym::HeapBase->setVirtualAddress(MemoryPtr); |
Nicholas Wilson | a06a355 | 2018-03-14 13:50:20 +0000 | [diff] [blame] | 784 | log("mem: heap base = " + Twine(MemoryPtr)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 787 | if (Config->InitialMemory != 0) { |
| 788 | if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize)) |
| 789 | error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
| 790 | if (MemoryPtr > Config->InitialMemory) |
| 791 | error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed"); |
| 792 | else |
| 793 | MemoryPtr = Config->InitialMemory; |
| 794 | } |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 795 | MemSize = MemoryPtr; |
| 796 | NumMemoryPages = alignTo(MemoryPtr, WasmPageSize) / WasmPageSize; |
Nicholas Wilson | a06a355 | 2018-03-14 13:50:20 +0000 | [diff] [blame] | 797 | log("mem: total pages = " + Twine(NumMemoryPages)); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 798 | |
| 799 | if (Config->MaxMemory != 0) { |
| 800 | if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize)) |
| 801 | error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
| 802 | if (MemoryPtr > Config->MaxMemory) |
| 803 | error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed"); |
| 804 | MaxMemoryPages = Config->MaxMemory / WasmPageSize; |
| 805 | log("mem: max pages = " + Twine(MaxMemoryPages)); |
| 806 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | SyntheticSection *Writer::createSyntheticSection(uint32_t Type, |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 810 | StringRef Name) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 811 | auto Sec = make<SyntheticSection>(Type, Name); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 812 | log("createSection: " + toString(*Sec)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 813 | OutputSections.push_back(Sec); |
| 814 | return Sec; |
| 815 | } |
| 816 | |
| 817 | void Writer::createSections() { |
| 818 | // Known sections |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 819 | if (Config->Pic) |
| 820 | createDylinkSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 821 | createTypeSection(); |
| 822 | createImportSection(); |
| 823 | createFunctionSection(); |
| 824 | createTableSection(); |
| 825 | createMemorySection(); |
| 826 | createGlobalSection(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 827 | createEventSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 828 | createExportSection(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 829 | createElemSection(); |
| 830 | createCodeSection(); |
| 831 | createDataSection(); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 832 | createCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 833 | |
| 834 | // Custom sections |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 835 | if (Config->Relocatable) { |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 836 | createLinkingSection(); |
Nicholas Wilson | 94d3b16 | 2018-03-05 12:33:58 +0000 | [diff] [blame] | 837 | createRelocSections(); |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 838 | } |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 839 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 840 | if (!Config->StripDebug && !Config->StripAll) |
| 841 | createNameSection(); |
| 842 | |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 843 | if (!Config->StripAll) |
| 844 | createProducersSection(); |
| 845 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 846 | for (OutputSection *S : OutputSections) { |
| 847 | S->setOffset(FileSize); |
| 848 | S->finalizeContents(); |
| 849 | FileSize += S->getSize(); |
| 850 | } |
| 851 | } |
| 852 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 853 | void Writer::calculateImports() { |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 854 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 855 | if (!Sym->isUndefined()) |
| 856 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 857 | if (Sym->isWeak() && !Config->Relocatable) |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 858 | continue; |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 859 | if (!Sym->isLive()) |
| 860 | continue; |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 861 | if (!Sym->IsUsedInRegularObj) |
| 862 | continue; |
Sam Clegg | d425d6b | 2019-03-12 21:53:23 +0000 | [diff] [blame] | 863 | // In relocatable output we don't generate imports for data symbols. |
| 864 | // These live only in the symbol table. |
| 865 | if (Config->Relocatable && isa<DataSymbol>(Sym)) |
| 866 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 867 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 868 | LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 869 | ImportedSymbols.emplace_back(Sym); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 870 | if (auto *F = dyn_cast<FunctionSymbol>(Sym)) |
| 871 | F->setFunctionIndex(NumImportedFunctions++); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 872 | else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) |
| 873 | G->setGlobalIndex(NumImportedGlobals++); |
Sam Clegg | d425d6b | 2019-03-12 21:53:23 +0000 | [diff] [blame] | 874 | else if (auto *D = dyn_cast<UndefinedData>(Sym)) |
| 875 | D->setGlobalIndex(NumImportedGlobals++); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 876 | else |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 877 | cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 881 | void Writer::calculateExports() { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 882 | if (Config->Relocatable) |
| 883 | return; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 884 | |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 885 | if (!Config->Relocatable && !Config->ImportMemory) |
| 886 | Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); |
| 887 | |
| 888 | if (!Config->Relocatable && Config->ExportTable) |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +0000 | [diff] [blame] | 889 | Exports.push_back(WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0}); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 890 | |
| 891 | unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size(); |
| 892 | |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 893 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 894 | if (!Sym->isExported()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 895 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 896 | if (!Sym->isLive()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 897 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 898 | |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 899 | StringRef Name = Sym->getName(); |
| 900 | WasmExport Export; |
| 901 | if (auto *F = dyn_cast<DefinedFunction>(Sym)) { |
| 902 | Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()}; |
| 903 | } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) { |
Sam Clegg | 177b458 | 2018-06-07 01:27:07 +0000 | [diff] [blame] | 904 | // TODO(sbc): Remove this check once to mutable global proposal is |
| 905 | // implement in all major browsers. |
| 906 | // See: https://github.com/WebAssembly/mutable-global |
| 907 | if (G->getGlobalType()->Mutable) { |
| 908 | // Only the __stack_pointer should ever be create as mutable. |
| 909 | assert(G == WasmSym::StackPointer); |
| 910 | continue; |
| 911 | } |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 912 | Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()}; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 913 | } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) { |
| 914 | Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()}; |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 915 | } else { |
| 916 | auto *D = cast<DefinedData>(Sym); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 917 | DefinedFakeGlobals.emplace_back(D); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 918 | Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; |
| 919 | } |
| 920 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 921 | LLVM_DEBUG(dbgs() << "Export: " << Name << "\n"); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 922 | Exports.push_back(Export); |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 923 | } |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void Writer::assignSymtab() { |
| 927 | if (!Config->Relocatable) |
| 928 | return; |
| 929 | |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 930 | StringMap<uint32_t> SectionSymbolIndices; |
| 931 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 932 | unsigned SymbolIndex = SymtabEntries.size(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 933 | |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 934 | auto AddSymbol = [&](Symbol *Sym) { |
| 935 | if (auto *S = dyn_cast<SectionSymbol>(Sym)) { |
| 936 | StringRef Name = S->getName(); |
| 937 | if (CustomSectionMapping.count(Name) == 0) |
| 938 | return; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 939 | |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 940 | auto SSI = SectionSymbolIndices.find(Name); |
| 941 | if (SSI != SectionSymbolIndices.end()) { |
| 942 | Sym->setOutputSymbolIndex(SSI->second); |
| 943 | return; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 946 | SectionSymbolIndices[Name] = SymbolIndex; |
| 947 | CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 948 | |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 949 | Sym->markLive(); |
| 950 | } |
| 951 | |
| 952 | // (Since this is relocatable output, GC is not performed so symbols must |
| 953 | // be live.) |
| 954 | assert(Sym->isLive()); |
| 955 | Sym->setOutputSymbolIndex(SymbolIndex++); |
| 956 | SymtabEntries.emplace_back(Sym); |
| 957 | }; |
| 958 | |
| 959 | for (Symbol *Sym : Symtab->getSymbols()) |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 960 | if (Sym->IsUsedInRegularObj) |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 961 | AddSymbol(Sym); |
| 962 | |
| 963 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 964 | LLVM_DEBUG(dbgs() << "Local symtab entries: " << File->getName() << "\n"); |
| 965 | for (Symbol *Sym : File->getSymbols()) |
| 966 | if (Sym->isLocal()) |
| 967 | AddSymbol(Sym); |
| 968 | } |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 971 | uint32_t Writer::lookupType(const WasmSignature &Sig) { |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 972 | auto It = TypeIndices.find(Sig); |
| 973 | if (It == TypeIndices.end()) { |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 974 | error("type not found: " + toString(Sig)); |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 975 | return 0; |
| 976 | } |
| 977 | return It->second; |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | uint32_t Writer::registerType(const WasmSignature &Sig) { |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 981 | auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size())); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 982 | if (Pair.second) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 983 | LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n"); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 984 | Types.push_back(&Sig); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 985 | } |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 986 | return Pair.first->second; |
| 987 | } |
| 988 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 989 | void Writer::calculateTypes() { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 990 | // The output type section is the union of the following sets: |
| 991 | // 1. Any signature used in the TYPE relocation |
| 992 | // 2. The signatures of all imported functions |
| 993 | // 3. The signatures of all defined functions |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 994 | // 4. The signatures of all imported events |
| 995 | // 5. The signatures of all defined events |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 996 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 997 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 998 | ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); |
| 999 | for (uint32_t I = 0; I < Types.size(); I++) |
| 1000 | if (File->TypeIsUsed[I]) |
| 1001 | File->TypeMap[I] = registerType(Types[I]); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1002 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1003 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1004 | for (const Symbol *Sym : ImportedSymbols) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1005 | if (auto *F = dyn_cast<FunctionSymbol>(Sym)) |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1006 | registerType(*F->Signature); |
| 1007 | else if (auto *E = dyn_cast<EventSymbol>(Sym)) |
| 1008 | registerType(*E->Signature); |
| 1009 | } |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 1010 | |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 1011 | for (const InputFunction *F : InputFunctions) |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 1012 | registerType(F->Signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1013 | |
| 1014 | for (const InputEvent *E : InputEvents) |
| 1015 | registerType(E->Signature); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 1018 | void Writer::assignIndexes() { |
Heejin Ahn | aeaab99 | 2018-11-19 23:21:25 +0000 | [diff] [blame] | 1019 | assert(InputFunctions.empty()); |
| 1020 | uint32_t FunctionIndex = NumImportedFunctions; |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 1021 | auto AddDefinedFunction = [&](InputFunction *Func) { |
| 1022 | if (!Func->Live) |
| 1023 | return; |
| 1024 | InputFunctions.emplace_back(Func); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 1025 | Func->setFunctionIndex(FunctionIndex++); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 1026 | }; |
| 1027 | |
Nicholas Wilson | 5639da8 | 2018-03-12 15:44:07 +0000 | [diff] [blame] | 1028 | for (InputFunction *Func : Symtab->SyntheticFunctions) |
| 1029 | AddDefinedFunction(Func); |
| 1030 | |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 1031 | for (ObjFile *File : Symtab->ObjectFiles) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1032 | LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 1033 | for (InputFunction *Func : File->Functions) |
| 1034 | AddDefinedFunction(Func); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 1037 | uint32_t TableIndex = TableBase; |
Sam Clegg | 6c4dbfee | 2018-02-23 04:59:57 +0000 | [diff] [blame] | 1038 | auto HandleRelocs = [&](InputChunk *Chunk) { |
| 1039 | if (!Chunk->Live) |
| 1040 | return; |
| 1041 | ObjFile *File = Chunk->File; |
| 1042 | ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1043 | for (const WasmRelocation &Reloc : Chunk->getRelocations()) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 1044 | if (Reloc.Type == R_WASM_TABLE_INDEX_I32 || |
| 1045 | Reloc.Type == R_WASM_TABLE_INDEX_SLEB) { |
Sam Clegg | 6c4dbfee | 2018-02-23 04:59:57 +0000 | [diff] [blame] | 1046 | FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 1047 | if (Sym->hasTableIndex() || !Sym->hasFunctionIndex()) |
Sam Clegg | 6c4dbfee | 2018-02-23 04:59:57 +0000 | [diff] [blame] | 1048 | continue; |
| 1049 | Sym->setTableIndex(TableIndex++); |
| 1050 | IndirectFunctions.emplace_back(Sym); |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 1051 | } else if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1052 | // Mark target type as live |
Sam Clegg | 6c4dbfee | 2018-02-23 04:59:57 +0000 | [diff] [blame] | 1053 | File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]); |
| 1054 | File->TypeIsUsed[Reloc.Index] = true; |
| 1055 | } |
| 1056 | } |
| 1057 | }; |
| 1058 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 1059 | for (ObjFile *File : Symtab->ObjectFiles) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1060 | LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1061 | for (InputChunk *Chunk : File->Functions) |
| 1062 | HandleRelocs(Chunk); |
| 1063 | for (InputChunk *Chunk : File->Segments) |
| 1064 | HandleRelocs(Chunk); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 1065 | for (auto &P : File->CustomSections) |
| 1066 | HandleRelocs(P); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1067 | } |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 1068 | |
Heejin Ahn | aeaab99 | 2018-11-19 23:21:25 +0000 | [diff] [blame] | 1069 | assert(InputGlobals.empty()); |
| 1070 | uint32_t GlobalIndex = NumImportedGlobals; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1071 | auto AddDefinedGlobal = [&](InputGlobal *Global) { |
| 1072 | if (Global->Live) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1073 | LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 1074 | Global->setGlobalIndex(GlobalIndex++); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1075 | InputGlobals.push_back(Global); |
| 1076 | } |
| 1077 | }; |
| 1078 | |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 1079 | for (InputGlobal *Global : Symtab->SyntheticGlobals) |
| 1080 | AddDefinedGlobal(Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1081 | |
| 1082 | for (ObjFile *File : Symtab->ObjectFiles) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1083 | LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1084 | for (InputGlobal *Global : File->Globals) |
| 1085 | AddDefinedGlobal(Global); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1086 | } |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1087 | |
| 1088 | assert(InputEvents.empty()); |
| 1089 | uint32_t EventIndex = NumImportedEvents; |
| 1090 | auto AddDefinedEvent = [&](InputEvent *Event) { |
| 1091 | if (Event->Live) { |
| 1092 | LLVM_DEBUG(dbgs() << "AddDefinedEvent: " << EventIndex << "\n"); |
| 1093 | Event->setEventIndex(EventIndex++); |
| 1094 | InputEvents.push_back(Event); |
| 1095 | } |
| 1096 | }; |
| 1097 | |
| 1098 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 1099 | LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n"); |
| 1100 | for (InputEvent *Event : File->Events) |
| 1101 | AddDefinedEvent(Event); |
| 1102 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static StringRef getOutputDataSegmentName(StringRef Name) { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 1106 | // With PIC code we currently only support a single data segment since |
| 1107 | // we only have a single __memory_base to use as our base address. |
| 1108 | if (Config->Pic) |
| 1109 | return "data"; |
Sam Clegg | 6684476 | 2018-05-10 18:23:51 +0000 | [diff] [blame] | 1110 | if (!Config->MergeDataSegments) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1111 | return Name; |
Rui Ueyama | 4764b57 | 2018-02-28 00:57:28 +0000 | [diff] [blame] | 1112 | if (Name.startswith(".text.")) |
| 1113 | return ".text"; |
| 1114 | if (Name.startswith(".data.")) |
| 1115 | return ".data"; |
| 1116 | if (Name.startswith(".bss.")) |
| 1117 | return ".bss"; |
Sam Clegg | 57694c5 | 2018-08-08 18:02:55 +0000 | [diff] [blame] | 1118 | if (Name.startswith(".rodata.")) |
| 1119 | return ".rodata"; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1120 | return Name; |
| 1121 | } |
| 1122 | |
| 1123 | void Writer::createOutputSegments() { |
| 1124 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 1125 | for (InputSegment *Segment : File->Segments) { |
Sam Clegg | 447ae40 | 2018-02-13 20:29:38 +0000 | [diff] [blame] | 1126 | if (!Segment->Live) |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 1127 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1128 | StringRef Name = getOutputDataSegmentName(Segment->getName()); |
| 1129 | OutputSegment *&S = SegmentMap[Name]; |
| 1130 | if (S == nullptr) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1131 | LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1132 | S = make<OutputSegment>(Name, Segments.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1133 | Segments.push_back(S); |
| 1134 | } |
| 1135 | S->addInputSegment(Segment); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 1136 | LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1141 | static const int OPCODE_CALL = 0x10; |
| 1142 | static const int OPCODE_END = 0xb; |
| 1143 | |
| 1144 | // Create synthetic "__wasm_call_ctors" function based on ctor functions |
| 1145 | // in input object. |
| 1146 | void Writer::createCtorFunction() { |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 1147 | if (!WasmSym::CallCtors->isLive()) |
| 1148 | return; |
| 1149 | |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 1150 | // First write the body's contents to a string. |
| 1151 | std::string BodyContent; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1152 | { |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 1153 | raw_string_ostream OS(BodyContent); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1154 | writeUleb128(OS, 0, "num locals"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1155 | for (const WasmInitEntry &F : InitFunctions) { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1156 | writeU8(OS, OPCODE_CALL, "CALL"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 1157 | writeUleb128(OS, F.Sym->getFunctionIndex(), "function index"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1158 | } |
| 1159 | writeU8(OS, OPCODE_END, "END"); |
| 1160 | } |
| 1161 | |
| 1162 | // Once we know the size of the body we can create the final function body |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 1163 | std::string FunctionBody; |
| 1164 | { |
| 1165 | raw_string_ostream OS(FunctionBody); |
| 1166 | writeUleb128(OS, BodyContent.size(), "function size"); |
| 1167 | OS << BodyContent; |
| 1168 | } |
Rui Ueyama | 29abfe4 | 2018-02-28 17:43:15 +0000 | [diff] [blame] | 1169 | |
Sam Clegg | ea65647 | 2018-10-22 08:35:39 +0000 | [diff] [blame] | 1170 | ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 1171 | cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | // Populate InitFunctions vector with init functions from all input objects. |
| 1175 | // This is then used either when creating the output linking section or to |
| 1176 | // synthesize the "__wasm_call_ctors" function. |
| 1177 | void Writer::calculateInitFunctions() { |
Sam Clegg | 61f13b3 | 2019-03-02 04:55:02 +0000 | [diff] [blame] | 1178 | if (!Config->Relocatable && !WasmSym::CallCtors->isLive()) |
| 1179 | return; |
| 1180 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1181 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 1182 | const WasmLinkingData &L = File->getWasmObj()->linkingData(); |
Nicholas Wilson | cb81a0c | 2018-03-02 14:46:54 +0000 | [diff] [blame] | 1183 | for (const WasmInitFunc &F : L.InitFunctions) { |
| 1184 | FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol); |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 1185 | assert(Sym->isLive()); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1186 | if (*Sym->Signature != WasmSignature{{}, {}}) |
Nicholas Wilson | cb81a0c | 2018-03-02 14:46:54 +0000 | [diff] [blame] | 1187 | error("invalid signature for init func: " + toString(*Sym)); |
| 1188 | InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority}); |
| 1189 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1190 | } |
Rui Ueyama | da69b71 | 2018-02-28 00:15:59 +0000 | [diff] [blame] | 1191 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1192 | // Sort in order of priority (lowest first) so that they are called |
| 1193 | // in the correct order. |
Sam Clegg | 29b8feb | 2018-02-21 00:34:34 +0000 | [diff] [blame] | 1194 | std::stable_sort(InitFunctions.begin(), InitFunctions.end(), |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1195 | [](const WasmInitEntry &L, const WasmInitEntry &R) { |
Sam Clegg | 29b8feb | 2018-02-21 00:34:34 +0000 | [diff] [blame] | 1196 | return L.Priority < R.Priority; |
| 1197 | }); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1200 | void Writer::run() { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 1201 | if (Config->Relocatable || Config->Pic) |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 1202 | Config->GlobalBase = 0; |
| 1203 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 1204 | // For PIC code the table base is assigned dynamically by the loader. |
| 1205 | // For non-PIC, we start at 1 so that accessing table index 0 always traps. |
| 1206 | if (!Config->Pic) |
| 1207 | TableBase = 1; |
| 1208 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1209 | log("-- calculateImports"); |
| 1210 | calculateImports(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 1211 | log("-- assignIndexes"); |
| 1212 | assignIndexes(); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 1213 | log("-- calculateInitFunctions"); |
| 1214 | calculateInitFunctions(); |
| 1215 | if (!Config->Relocatable) |
| 1216 | createCtorFunction(); |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 1217 | log("-- calculateTypes"); |
| 1218 | calculateTypes(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1219 | log("-- layoutMemory"); |
| 1220 | layoutMemory(); |
| 1221 | log("-- calculateExports"); |
| 1222 | calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 1223 | log("-- calculateCustomSections"); |
| 1224 | calculateCustomSections(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1225 | log("-- assignSymtab"); |
| 1226 | assignSymtab(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (errorHandler().Verbose) { |
Sam Clegg | 9f93422 | 2018-02-21 18:29:23 +0000 | [diff] [blame] | 1229 | log("Defined Functions: " + Twine(InputFunctions.size())); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1230 | log("Defined Globals : " + Twine(InputGlobals.size())); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1231 | log("Defined Events : " + Twine(InputEvents.size())); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1232 | log("Function Imports : " + Twine(NumImportedFunctions)); |
| 1233 | log("Global Imports : " + Twine(NumImportedGlobals)); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 1234 | log("Event Imports : " + Twine(NumImportedEvents)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1235 | for (ObjFile *File : Symtab->ObjectFiles) |
| 1236 | File->dumpInfo(); |
| 1237 | } |
| 1238 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1239 | createHeader(); |
| 1240 | log("-- createSections"); |
| 1241 | createSections(); |
| 1242 | |
| 1243 | log("-- openFile"); |
| 1244 | openFile(); |
| 1245 | if (errorCount()) |
| 1246 | return; |
| 1247 | |
| 1248 | writeHeader(); |
| 1249 | |
| 1250 | log("-- writeSections"); |
| 1251 | writeSections(); |
| 1252 | if (errorCount()) |
| 1253 | return; |
| 1254 | |
| 1255 | if (Error E = Buffer->commit()) |
| 1256 | fatal("failed to write the output file: " + toString(std::move(E))); |
| 1257 | } |
| 1258 | |
| 1259 | // Open a result file. |
| 1260 | void Writer::openFile() { |
| 1261 | log("writing: " + Config->OutputFile); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1262 | |
| 1263 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 1264 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 1265 | FileOutputBuffer::F_executable); |
| 1266 | |
| 1267 | if (!BufferOrErr) |
| 1268 | error("failed to open " + Config->OutputFile + ": " + |
| 1269 | toString(BufferOrErr.takeError())); |
| 1270 | else |
| 1271 | Buffer = std::move(*BufferOrErr); |
| 1272 | } |
| 1273 | |
| 1274 | void Writer::createHeader() { |
| 1275 | raw_string_ostream OS(Header); |
| 1276 | writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); |
| 1277 | writeU32(OS, WasmVersion, "wasm version"); |
| 1278 | OS.flush(); |
| 1279 | FileSize += Header.size(); |
| 1280 | } |
| 1281 | |
| 1282 | void lld::wasm::writeResult() { Writer().run(); } |