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