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