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