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