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