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