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