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