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