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