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