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