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" |
| 11 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 12 | #include "llvm/ADT/DenseSet.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 13 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 14 | #include "InputChunks.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "OutputSections.h" |
| 16 | #include "OutputSegment.h" |
| 17 | #include "SymbolTable.h" |
| 18 | #include "WriterUtils.h" |
| 19 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 20 | #include "lld/Common/Memory.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 21 | #include "lld/Common/Threads.h" |
| 22 | #include "llvm/Support/FileOutputBuffer.h" |
| 23 | #include "llvm/Support/Format.h" |
| 24 | #include "llvm/Support/FormatVariadic.h" |
| 25 | #include "llvm/Support/LEB128.h" |
| 26 | |
| 27 | #include <cstdarg> |
| 28 | |
| 29 | #define DEBUG_TYPE "lld" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | using namespace llvm::wasm; |
| 33 | using namespace lld; |
| 34 | using namespace lld::wasm; |
| 35 | |
| 36 | static constexpr int kStackAlignment = 16; |
| 37 | |
| 38 | namespace { |
| 39 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 40 | // Traits for using WasmSignature in a DenseMap. |
| 41 | struct WasmSignatureDenseMapInfo { |
| 42 | static WasmSignature getEmptyKey() { |
| 43 | WasmSignature Sig; |
| 44 | Sig.ReturnType = 1; |
| 45 | return Sig; |
| 46 | } |
| 47 | static WasmSignature getTombstoneKey() { |
| 48 | WasmSignature Sig; |
| 49 | Sig.ReturnType = 2; |
| 50 | return Sig; |
| 51 | } |
| 52 | static unsigned getHashValue(const WasmSignature &Sig) { |
| 53 | uintptr_t Value = 0; |
| 54 | Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType); |
| 55 | for (int32_t Param : Sig.ParamTypes) |
| 56 | Value += DenseMapInfo<int32_t>::getHashValue(Param); |
| 57 | return Value; |
| 58 | } |
| 59 | static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { |
| 60 | return LHS == RHS; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | // The writer writes a SymbolTable result to a file. |
| 65 | class Writer { |
| 66 | public: |
| 67 | void run(); |
| 68 | |
| 69 | private: |
| 70 | void openFile(); |
| 71 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 72 | uint32_t lookupType(const WasmSignature &Sig); |
| 73 | uint32_t registerType(const WasmSignature &Sig); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 74 | void createCtorFunction(); |
| 75 | void calculateInitFunctions(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 76 | void assignIndexes(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 77 | void calculateImports(); |
| 78 | void calculateOffsets(); |
| 79 | void calculateTypes(); |
| 80 | void createOutputSegments(); |
| 81 | void layoutMemory(); |
| 82 | void createHeader(); |
| 83 | void createSections(); |
| 84 | SyntheticSection *createSyntheticSection(uint32_t Type, |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 85 | StringRef Name = ""); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 86 | |
| 87 | // Builtin sections |
| 88 | void createTypeSection(); |
| 89 | void createFunctionSection(); |
| 90 | void createTableSection(); |
| 91 | void createGlobalSection(); |
| 92 | void createExportSection(); |
| 93 | void createImportSection(); |
| 94 | void createMemorySection(); |
| 95 | void createElemSection(); |
| 96 | void createStartSection(); |
| 97 | void createCodeSection(); |
| 98 | void createDataSection(); |
| 99 | |
| 100 | // Custom sections |
| 101 | void createRelocSections(); |
| 102 | void createLinkingSection(); |
| 103 | void createNameSection(); |
| 104 | |
| 105 | void writeHeader(); |
| 106 | void writeSections(); |
| 107 | |
| 108 | uint64_t FileSize = 0; |
| 109 | uint32_t DataSize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 110 | uint32_t NumMemoryPages = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 111 | uint32_t InitialTableOffset = 0; |
| 112 | |
| 113 | std::vector<const WasmSignature *> Types; |
| 114 | DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices; |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 115 | std::vector<const Symbol *> ImportedFunctions; |
| 116 | std::vector<const Symbol *> ImportedGlobals; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 117 | std::vector<const Symbol *> DefinedGlobals; |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 118 | std::vector<InputFunction *> DefinedFunctions; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 119 | std::vector<const Symbol *> IndirectFunctions; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 120 | std::vector<WasmInitFunc> InitFunctions; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 121 | |
| 122 | // Elements that are used to construct the final output |
| 123 | std::string Header; |
| 124 | std::vector<OutputSection *> OutputSections; |
| 125 | |
| 126 | std::unique_ptr<FileOutputBuffer> Buffer; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 127 | std::unique_ptr<SyntheticFunction> CtorFunction; |
| 128 | std::string CtorFunctionBody; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 129 | |
| 130 | std::vector<OutputSegment *> Segments; |
| 131 | llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; |
| 132 | }; |
| 133 | |
| 134 | } // anonymous namespace |
| 135 | |
| 136 | static void debugPrint(const char *fmt, ...) { |
| 137 | if (!errorHandler().Verbose) |
| 138 | return; |
| 139 | fprintf(stderr, "lld: "); |
| 140 | va_list ap; |
| 141 | va_start(ap, fmt); |
| 142 | vfprintf(stderr, fmt, ap); |
| 143 | va_end(ap); |
| 144 | } |
| 145 | |
| 146 | void Writer::createImportSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 147 | uint32_t NumImports = ImportedFunctions.size() + ImportedGlobals.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 148 | if (Config->ImportMemory) |
| 149 | ++NumImports; |
| 150 | |
| 151 | if (NumImports == 0) |
| 152 | return; |
| 153 | |
| 154 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); |
| 155 | raw_ostream &OS = Section->getStream(); |
| 156 | |
| 157 | writeUleb128(OS, NumImports, "import count"); |
| 158 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 159 | for (const Symbol *Sym : ImportedFunctions) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 160 | WasmImport Import; |
| 161 | Import.Module = "env"; |
| 162 | Import.Field = Sym->getName(); |
| 163 | Import.Kind = WASM_EXTERNAL_FUNCTION; |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 164 | Import.SigIndex = lookupType(Sym->getFunctionType()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 165 | writeImport(OS, Import); |
| 166 | } |
| 167 | |
| 168 | if (Config->ImportMemory) { |
| 169 | WasmImport Import; |
| 170 | Import.Module = "env"; |
| 171 | Import.Field = "memory"; |
| 172 | Import.Kind = WASM_EXTERNAL_MEMORY; |
| 173 | Import.Memory.Flags = 0; |
| 174 | Import.Memory.Initial = NumMemoryPages; |
| 175 | writeImport(OS, Import); |
| 176 | } |
| 177 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 178 | for (const Symbol *Sym : ImportedGlobals) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 179 | WasmImport Import; |
| 180 | Import.Module = "env"; |
| 181 | Import.Field = Sym->getName(); |
| 182 | Import.Kind = WASM_EXTERNAL_GLOBAL; |
| 183 | Import.Global.Mutable = false; |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 184 | Import.Global.Type = WASM_TYPE_I32; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 185 | writeImport(OS, Import); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void Writer::createTypeSection() { |
| 190 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE); |
| 191 | raw_ostream &OS = Section->getStream(); |
| 192 | writeUleb128(OS, Types.size(), "type count"); |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 193 | for (const WasmSignature *Sig : Types) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 194 | writeSig(OS, *Sig); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void Writer::createFunctionSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 198 | if (DefinedFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 199 | return; |
| 200 | |
| 201 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION); |
| 202 | raw_ostream &OS = Section->getStream(); |
| 203 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 204 | writeUleb128(OS, DefinedFunctions.size(), "function count"); |
| 205 | for (const InputFunction *Func : DefinedFunctions) |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 206 | writeUleb128(OS, lookupType(Func->Signature), "sig index"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void Writer::createMemorySection() { |
| 210 | if (Config->ImportMemory) |
| 211 | return; |
| 212 | |
| 213 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY); |
| 214 | raw_ostream &OS = Section->getStream(); |
| 215 | |
| 216 | writeUleb128(OS, 1, "memory count"); |
| 217 | writeUleb128(OS, 0, "memory limits flags"); |
| 218 | writeUleb128(OS, NumMemoryPages, "initial pages"); |
| 219 | } |
| 220 | |
| 221 | void Writer::createGlobalSection() { |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 222 | if (DefinedGlobals.empty()) |
| 223 | return; |
| 224 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 225 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL); |
| 226 | raw_ostream &OS = Section->getStream(); |
| 227 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 228 | writeUleb128(OS, DefinedGlobals.size(), "global count"); |
| 229 | for (const Symbol *Sym : DefinedGlobals) { |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 230 | WasmGlobal Global; |
| 231 | Global.Type = WASM_TYPE_I32; |
| 232 | Global.Mutable = Sym == Config->StackPointerSymbol; |
| 233 | Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 234 | Global.InitExpr.Value.Int32 = Sym->getVirtualAddress(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 235 | writeGlobal(OS, Global); |
| 236 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void Writer::createTableSection() { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 240 | // Always output a table section, even if there are no indirect calls. |
| 241 | // There are two reasons for this: |
| 242 | // 1. For executables it is useful to have an empty table slot at 0 |
| 243 | // which can be filled with a null function call handler. |
| 244 | // 2. If we don't do this, any program that contains a call_indirect but |
| 245 | // no address-taken function will fail at validation time since it is |
| 246 | // a validation error to include a call_indirect instruction if there |
| 247 | // is not table. |
| 248 | uint32_t TableSize = InitialTableOffset + IndirectFunctions.size(); |
| 249 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 250 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE); |
| 251 | raw_ostream &OS = Section->getStream(); |
| 252 | |
| 253 | writeUleb128(OS, 1, "table count"); |
| 254 | writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type"); |
| 255 | writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags"); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 256 | writeUleb128(OS, TableSize, "table initial size"); |
| 257 | writeUleb128(OS, TableSize, "table max size"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void Writer::createExportSection() { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 261 | bool ExportMemory = !Config->Relocatable && !Config->ImportMemory; |
Sam Clegg | 4b27c05 | 2017-12-03 02:38:04 +0000 | [diff] [blame] | 262 | Symbol *EntrySym = Symtab->find(Config->Entry); |
| 263 | bool ExportEntry = !Config->Relocatable && EntrySym && EntrySym->isDefined(); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 264 | bool ExportHidden = Config->EmitRelocs; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 265 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 266 | uint32_t NumExports = ExportMemory ? 1 : 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 267 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 268 | std::vector<const Symbol *> SymbolExports; |
Sam Clegg | 4b27c05 | 2017-12-03 02:38:04 +0000 | [diff] [blame] | 269 | if (ExportEntry) |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 270 | SymbolExports.emplace_back(EntrySym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 271 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 272 | for (const Symbol *Sym : Symtab->getSymbols()) { |
| 273 | if (Sym->isUndefined() || Sym->isGlobal()) |
| 274 | continue; |
| 275 | if (Sym->isHidden() && !ExportHidden) |
| 276 | continue; |
| 277 | if (ExportEntry && Sym == EntrySym) |
| 278 | continue; |
| 279 | SymbolExports.emplace_back(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 282 | for (const Symbol *Sym : DefinedGlobals) { |
| 283 | // Can't export the SP right now because it mutable and mutable globals |
| 284 | // connot be exported. |
| 285 | if (Sym == Config->StackPointerSymbol) |
| 286 | continue; |
| 287 | SymbolExports.emplace_back(Sym); |
| 288 | } |
| 289 | |
| 290 | NumExports += SymbolExports.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 291 | if (!NumExports) |
| 292 | return; |
| 293 | |
| 294 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); |
| 295 | raw_ostream &OS = Section->getStream(); |
| 296 | |
| 297 | writeUleb128(OS, NumExports, "export count"); |
| 298 | |
| 299 | if (ExportMemory) { |
| 300 | WasmExport MemoryExport; |
| 301 | MemoryExport.Name = "memory"; |
| 302 | MemoryExport.Kind = WASM_EXTERNAL_MEMORY; |
| 303 | MemoryExport.Index = 0; |
| 304 | writeExport(OS, MemoryExport); |
| 305 | } |
| 306 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 307 | for (const Symbol *Sym : SymbolExports) { |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 308 | DEBUG(dbgs() << "Export: " << Sym->getName() << "\n"); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 309 | WasmExport Export; |
| 310 | Export.Name = Sym->getName(); |
| 311 | Export.Index = Sym->getOutputIndex(); |
| 312 | if (Sym->isFunction()) |
| 313 | Export.Kind = WASM_EXTERNAL_FUNCTION; |
| 314 | else |
| 315 | Export.Kind = WASM_EXTERNAL_GLOBAL; |
| 316 | writeExport(OS, Export); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | void Writer::createStartSection() {} |
| 321 | |
| 322 | void Writer::createElemSection() { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 323 | if (IndirectFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 324 | return; |
| 325 | |
| 326 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM); |
| 327 | raw_ostream &OS = Section->getStream(); |
| 328 | |
| 329 | writeUleb128(OS, 1, "segment count"); |
| 330 | writeUleb128(OS, 0, "table index"); |
| 331 | WasmInitExpr InitExpr; |
| 332 | InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 333 | InitExpr.Value.Int32 = InitialTableOffset; |
| 334 | writeInitExpr(OS, InitExpr); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 335 | writeUleb128(OS, IndirectFunctions.size(), "elem count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 336 | |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 337 | uint32_t TableIndex = InitialTableOffset; |
| 338 | for (const Symbol *Sym : IndirectFunctions) { |
| 339 | assert(Sym->getTableIndex() == TableIndex); |
| 340 | writeUleb128(OS, Sym->getOutputIndex(), "function index"); |
| 341 | ++TableIndex; |
| 342 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void Writer::createCodeSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 346 | if (DefinedFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 347 | return; |
| 348 | |
| 349 | log("createCodeSection"); |
| 350 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 351 | auto Section = make<CodeSection>(DefinedFunctions); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 352 | OutputSections.push_back(Section); |
| 353 | } |
| 354 | |
| 355 | void Writer::createDataSection() { |
| 356 | if (!Segments.size()) |
| 357 | return; |
| 358 | |
| 359 | log("createDataSection"); |
| 360 | auto Section = make<DataSection>(Segments); |
| 361 | OutputSections.push_back(Section); |
| 362 | } |
| 363 | |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 364 | // Create relocations sections in the final output. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 365 | // These are only created when relocatable output is requested. |
| 366 | void Writer::createRelocSections() { |
| 367 | log("createRelocSections"); |
| 368 | // Don't use iterator here since we are adding to OutputSection |
| 369 | size_t OrigSize = OutputSections.size(); |
| 370 | for (size_t i = 0; i < OrigSize; i++) { |
| 371 | OutputSection *S = OutputSections[i]; |
| 372 | const char *name; |
| 373 | uint32_t Count = S->numRelocations(); |
| 374 | if (!Count) |
| 375 | continue; |
| 376 | |
| 377 | if (S->Type == WASM_SEC_DATA) |
| 378 | name = "reloc.DATA"; |
| 379 | else if (S->Type == WASM_SEC_CODE) |
| 380 | name = "reloc.CODE"; |
| 381 | else |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 382 | llvm_unreachable("relocations only supported for code and data"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 383 | |
| 384 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name); |
| 385 | raw_ostream &OS = Section->getStream(); |
| 386 | writeUleb128(OS, S->Type, "reloc section"); |
| 387 | writeUleb128(OS, Count, "reloc count"); |
| 388 | S->writeRelocations(OS); |
| 389 | } |
| 390 | } |
| 391 | |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 392 | // Create the custom "linking" section containing linker metadata. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 393 | // This is only created when relocatable output is requested. |
| 394 | void Writer::createLinkingSection() { |
| 395 | SyntheticSection *Section = |
| 396 | createSyntheticSection(WASM_SEC_CUSTOM, "linking"); |
| 397 | raw_ostream &OS = Section->getStream(); |
| 398 | |
| 399 | SubSection DataSizeSubSection(WASM_DATA_SIZE); |
| 400 | writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size"); |
| 401 | DataSizeSubSection.finalizeContents(); |
| 402 | DataSizeSubSection.writeToStream(OS); |
| 403 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 404 | if (!Config->Relocatable) |
| 405 | return; |
| 406 | |
| 407 | if (Segments.size()) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 408 | SubSection SubSection(WASM_SEGMENT_INFO); |
| 409 | writeUleb128(SubSection.getStream(), Segments.size(), "num data segments"); |
| 410 | for (const OutputSegment *S : Segments) { |
| 411 | writeStr(SubSection.getStream(), S->Name, "segment name"); |
| 412 | writeUleb128(SubSection.getStream(), S->Alignment, "alignment"); |
| 413 | writeUleb128(SubSection.getStream(), 0, "flags"); |
| 414 | } |
| 415 | SubSection.finalizeContents(); |
| 416 | SubSection.writeToStream(OS); |
| 417 | } |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 418 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 419 | if (!InitFunctions.empty()) { |
| 420 | SubSection SubSection(WASM_INIT_FUNCS); |
| 421 | writeUleb128(SubSection.getStream(), InitFunctions.size(), |
| 422 | "num init functionsw"); |
| 423 | for (const WasmInitFunc &F : InitFunctions) { |
| 424 | writeUleb128(SubSection.getStream(), F.Priority, "priority"); |
| 425 | writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index"); |
| 426 | } |
| 427 | SubSection.finalizeContents(); |
| 428 | SubSection.writeToStream(OS); |
| 429 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Create the custom "name" section containing debug symbol names. |
| 433 | void Writer::createNameSection() { |
| 434 | // Create an array of all function sorted by function index space |
| 435 | std::vector<const Symbol *> Names; |
| 436 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 437 | auto AddToNames = [&](Symbol* S) { |
| 438 | if (!S->isFunction() || S->WrittenToNameSec) |
| 439 | return; |
| 440 | // We also need to guard against two different symbols (two different |
| 441 | // names) for the same wasm function. While this is possible (aliases) |
| 442 | // it is not legal in the "name" section. |
| 443 | InputFunction *Function = S->getFunction(); |
| 444 | if (Function) { |
| 445 | if (Function->WrittenToNameSec) |
| 446 | return; |
| 447 | Function->WrittenToNameSec = true; |
| 448 | } |
| 449 | S->WrittenToNameSec = true; |
| 450 | Names.emplace_back(S); |
| 451 | }; |
| 452 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 453 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 454 | Names.reserve(Names.size() + File->getSymbols().size()); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 455 | DEBUG(dbgs() << "adding names from: " << File->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 456 | for (Symbol *S : File->getSymbols()) { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 457 | if (S->isWeak()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 458 | continue; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 459 | AddToNames(S); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 463 | DEBUG(dbgs() << "adding symtab names\n"); |
| 464 | for (Symbol *S : Symtab->getSymbols()) { |
| 465 | DEBUG(dbgs() << "sym: " << S->getName() << "\n"); |
| 466 | if (S->getFile()) |
| 467 | continue; |
| 468 | AddToNames(S); |
| 469 | } |
| 470 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 471 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name"); |
| 472 | |
| 473 | std::sort(Names.begin(), Names.end(), [](const Symbol *A, const Symbol *B) { |
| 474 | return A->getOutputIndex() < B->getOutputIndex(); |
| 475 | }); |
| 476 | |
| 477 | SubSection FunctionSubsection(WASM_NAMES_FUNCTION); |
| 478 | raw_ostream &OS = FunctionSubsection.getStream(); |
| 479 | writeUleb128(OS, Names.size(), "name count"); |
| 480 | |
| 481 | // We have to iterate through the inputs twice so that all the imports |
| 482 | // appear first before any of the local function names. |
| 483 | for (const Symbol *S : Names) { |
| 484 | writeUleb128(OS, S->getOutputIndex(), "func index"); |
| 485 | writeStr(OS, S->getName(), "symbol name"); |
| 486 | } |
| 487 | |
| 488 | FunctionSubsection.finalizeContents(); |
| 489 | FunctionSubsection.writeToStream(Section->getStream()); |
| 490 | } |
| 491 | |
| 492 | void Writer::writeHeader() { |
| 493 | memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); |
| 494 | } |
| 495 | |
| 496 | void Writer::writeSections() { |
| 497 | uint8_t *Buf = Buffer->getBufferStart(); |
| 498 | parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); }); |
| 499 | } |
| 500 | |
| 501 | // Fix the memory layout of the output binary. This assigns memory offsets |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 502 | // to each of the input data sections as well as the explicit stack region. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 503 | void Writer::layoutMemory() { |
| 504 | uint32_t MemoryPtr = 0; |
| 505 | if (!Config->Relocatable) { |
| 506 | MemoryPtr = Config->GlobalBase; |
| 507 | debugPrint("mem: global base = %d\n", Config->GlobalBase); |
| 508 | } |
| 509 | |
| 510 | createOutputSegments(); |
| 511 | |
| 512 | // Static data comes first |
| 513 | for (OutputSegment *Seg : Segments) { |
| 514 | MemoryPtr = alignTo(MemoryPtr, Seg->Alignment); |
| 515 | Seg->StartVA = MemoryPtr; |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 516 | debugPrint("mem: %-15s offset=%-8d size=%-8d align=%d\n", |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 517 | Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment); |
| 518 | MemoryPtr += Seg->Size; |
| 519 | } |
| 520 | |
| 521 | DataSize = MemoryPtr; |
| 522 | if (!Config->Relocatable) |
| 523 | DataSize -= Config->GlobalBase; |
| 524 | debugPrint("mem: static data = %d\n", DataSize); |
| 525 | |
| 526 | // Stack comes after static data |
| 527 | if (!Config->Relocatable) { |
| 528 | MemoryPtr = alignTo(MemoryPtr, kStackAlignment); |
| 529 | if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment)) |
| 530 | error("stack size must be " + Twine(kStackAlignment) + "-byte aligned"); |
| 531 | debugPrint("mem: stack size = %d\n", Config->ZStackSize); |
| 532 | debugPrint("mem: stack base = %d\n", MemoryPtr); |
| 533 | MemoryPtr += Config->ZStackSize; |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 534 | Config->StackPointerSymbol->setVirtualAddress(MemoryPtr); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 535 | debugPrint("mem: stack top = %d\n", MemoryPtr); |
| 536 | } |
| 537 | |
| 538 | uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize); |
| 539 | NumMemoryPages = MemSize / WasmPageSize; |
| 540 | debugPrint("mem: total pages = %d\n", NumMemoryPages); |
| 541 | } |
| 542 | |
| 543 | SyntheticSection *Writer::createSyntheticSection(uint32_t Type, |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 544 | StringRef Name) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 545 | auto Sec = make<SyntheticSection>(Type, Name); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 546 | log("createSection: " + toString(*Sec)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 547 | OutputSections.push_back(Sec); |
| 548 | return Sec; |
| 549 | } |
| 550 | |
| 551 | void Writer::createSections() { |
| 552 | // Known sections |
| 553 | createTypeSection(); |
| 554 | createImportSection(); |
| 555 | createFunctionSection(); |
| 556 | createTableSection(); |
| 557 | createMemorySection(); |
| 558 | createGlobalSection(); |
| 559 | createExportSection(); |
| 560 | createStartSection(); |
| 561 | createElemSection(); |
| 562 | createCodeSection(); |
| 563 | createDataSection(); |
| 564 | |
| 565 | // Custom sections |
Sam Clegg | 22cfe52 | 2017-12-05 16:53:25 +0000 | [diff] [blame] | 566 | if (Config->EmitRelocs) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 567 | createRelocSections(); |
| 568 | createLinkingSection(); |
| 569 | if (!Config->StripDebug && !Config->StripAll) |
| 570 | createNameSection(); |
| 571 | |
| 572 | for (OutputSection *S : OutputSections) { |
| 573 | S->setOffset(FileSize); |
| 574 | S->finalizeContents(); |
| 575 | FileSize += S->getSize(); |
| 576 | } |
| 577 | } |
| 578 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 579 | void Writer::calculateImports() { |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 580 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | c018115 | 2017-12-15 22:17:15 +0000 | [diff] [blame] | 581 | if (!Sym->isUndefined() || Sym->isWeak()) |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 582 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 583 | |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 584 | if (Sym->isFunction()) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 585 | Sym->setOutputIndex(ImportedFunctions.size()); |
| 586 | ImportedFunctions.push_back(Sym); |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 587 | } else { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 588 | Sym->setOutputIndex(ImportedGlobals.size()); |
| 589 | ImportedGlobals.push_back(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 594 | uint32_t Writer::lookupType(const WasmSignature &Sig) { |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 595 | auto It = TypeIndices.find(Sig); |
| 596 | if (It == TypeIndices.end()) { |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 597 | error("type not found: " + toString(Sig)); |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | return It->second; |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | uint32_t Writer::registerType(const WasmSignature &Sig) { |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 604 | auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size())); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 605 | if (Pair.second) { |
| 606 | DEBUG(dbgs() << "type " << toString(Sig) << "\n"); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 607 | Types.push_back(&Sig); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 608 | } |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 609 | return Pair.first->second; |
| 610 | } |
| 611 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 612 | void Writer::calculateTypes() { |
| 613 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 614 | File->TypeMap.reserve(File->getWasmObj()->types().size()); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 615 | for (const WasmSignature &Sig : File->getWasmObj()->types()) |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 616 | File->TypeMap.push_back(registerType(Sig)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 617 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 618 | |
| 619 | for (Symbol *Sym : Symtab->getSymbols()) |
| 620 | if (Sym->isFunction()) |
| 621 | registerType(Sym->getFunctionType()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 624 | void Writer::assignIndexes() { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 625 | uint32_t GlobalIndex = ImportedGlobals.size() + DefinedGlobals.size(); |
| 626 | uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 627 | |
| 628 | if (Config->StackPointerSymbol) { |
| 629 | DefinedGlobals.emplace_back(Config->StackPointerSymbol); |
| 630 | Config->StackPointerSymbol->setOutputIndex(GlobalIndex++); |
| 631 | } |
| 632 | |
| 633 | if (Config->EmitRelocs) |
| 634 | DefinedGlobals.reserve(Symtab->getSymbols().size()); |
| 635 | |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 636 | uint32_t TableIndex = InitialTableOffset; |
| 637 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 638 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 639 | if (Config->EmitRelocs) { |
| 640 | DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); |
| 641 | for (Symbol *Sym : File->getSymbols()) { |
| 642 | // Create wasm globals for data symbols defined in this file |
| 643 | if (!Sym->isDefined() || File != Sym->getFile()) |
| 644 | continue; |
| 645 | if (Sym->isFunction()) |
| 646 | continue; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 647 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 648 | DefinedGlobals.emplace_back(Sym); |
| 649 | Sym->setOutputIndex(GlobalIndex++); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 652 | } |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 653 | |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 654 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 655 | DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); |
| 656 | for (InputFunction *Func : File->Functions) { |
| 657 | DefinedFunctions.emplace_back(Func); |
| 658 | Func->setOutputIndex(FunctionIndex++); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 663 | DEBUG(dbgs() << "Table Indexes: " << File->getName() << "\n"); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 664 | for (Symbol *Sym : File->getTableSymbols()) { |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 665 | if (Sym->hasTableIndex() || !Sym->hasOutputIndex()) |
| 666 | continue; |
| 667 | Sym->setTableIndex(TableIndex++); |
| 668 | IndirectFunctions.emplace_back(Sym); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 669 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | static StringRef getOutputDataSegmentName(StringRef Name) { |
| 674 | if (Config->Relocatable) |
| 675 | return Name; |
| 676 | |
| 677 | for (StringRef V : |
| 678 | {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", |
| 679 | ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", |
| 680 | ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) { |
| 681 | StringRef Prefix = V.drop_back(); |
| 682 | if (Name.startswith(V) || Name == Prefix) |
| 683 | return Prefix; |
| 684 | } |
| 685 | |
| 686 | return Name; |
| 687 | } |
| 688 | |
| 689 | void Writer::createOutputSegments() { |
| 690 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 691 | for (InputSegment *Segment : File->Segments) { |
| 692 | StringRef Name = getOutputDataSegmentName(Segment->getName()); |
| 693 | OutputSegment *&S = SegmentMap[Name]; |
| 694 | if (S == nullptr) { |
| 695 | DEBUG(dbgs() << "new segment: " << Name << "\n"); |
| 696 | S = make<OutputSegment>(Name); |
| 697 | Segments.push_back(S); |
| 698 | } |
| 699 | S->addInputSegment(Segment); |
| 700 | DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 705 | static const int OPCODE_CALL = 0x10; |
| 706 | static const int OPCODE_END = 0xb; |
| 707 | |
| 708 | // Create synthetic "__wasm_call_ctors" function based on ctor functions |
| 709 | // in input object. |
| 710 | void Writer::createCtorFunction() { |
| 711 | uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); |
| 712 | Config->CtorSymbol->setOutputIndex(FunctionIndex); |
| 713 | |
| 714 | // First write the body bytes to a string. |
| 715 | std::string FunctionBody; |
| 716 | static WasmSignature Signature = {{}, WASM_TYPE_NORESULT}; |
| 717 | { |
| 718 | raw_string_ostream OS(FunctionBody); |
| 719 | writeUleb128(OS, 0, "num locals"); |
| 720 | for (const WasmInitFunc &F : InitFunctions) { |
| 721 | writeU8(OS, OPCODE_CALL, "CALL"); |
| 722 | writeUleb128(OS, F.FunctionIndex, "function index"); |
| 723 | } |
| 724 | writeU8(OS, OPCODE_END, "END"); |
| 725 | } |
| 726 | |
| 727 | // Once we know the size of the body we can create the final function body |
| 728 | raw_string_ostream OS(CtorFunctionBody); |
| 729 | writeUleb128(OS, FunctionBody.size(), "function size"); |
| 730 | OS.flush(); |
| 731 | CtorFunctionBody += FunctionBody; |
| 732 | CtorFunction = |
| 733 | llvm::make_unique<SyntheticFunction>(Signature, CtorFunctionBody); |
| 734 | DefinedFunctions.emplace_back(CtorFunction.get()); |
| 735 | } |
| 736 | |
| 737 | // Populate InitFunctions vector with init functions from all input objects. |
| 738 | // This is then used either when creating the output linking section or to |
| 739 | // synthesize the "__wasm_call_ctors" function. |
| 740 | void Writer::calculateInitFunctions() { |
| 741 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 742 | const WasmLinkingData &L = File->getWasmObj()->linkingData(); |
| 743 | InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size()); |
| 744 | for (const WasmInitFunc &F : L.InitFunctions) |
| 745 | InitFunctions.emplace_back(WasmInitFunc{ |
| 746 | F.Priority, File->relocateFunctionIndex(F.FunctionIndex)}); |
| 747 | } |
| 748 | // Sort in order of priority (lowest first) so that they are called |
| 749 | // in the correct order. |
| 750 | std::sort(InitFunctions.begin(), InitFunctions.end(), |
| 751 | [](const WasmInitFunc &L, const WasmInitFunc &R) { |
| 752 | return L.Priority < R.Priority; |
| 753 | }); |
| 754 | } |
| 755 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 756 | void Writer::run() { |
| 757 | if (!Config->Relocatable) |
| 758 | InitialTableOffset = 1; |
| 759 | |
| 760 | log("-- calculateTypes"); |
| 761 | calculateTypes(); |
| 762 | log("-- calculateImports"); |
| 763 | calculateImports(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 764 | log("-- assignIndexes"); |
| 765 | assignIndexes(); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame^] | 766 | log("-- calculateInitFunctions"); |
| 767 | calculateInitFunctions(); |
| 768 | if (!Config->Relocatable) |
| 769 | createCtorFunction(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 770 | |
| 771 | if (errorHandler().Verbose) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 772 | log("Defined Functions: " + Twine(DefinedFunctions.size())); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 773 | log("Defined Globals : " + Twine(DefinedGlobals.size())); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 774 | log("Function Imports : " + Twine(ImportedFunctions.size())); |
| 775 | log("Global Imports : " + Twine(ImportedGlobals.size())); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 776 | log("Total Imports : " + |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 777 | Twine(ImportedFunctions.size() + ImportedGlobals.size())); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 778 | for (ObjFile *File : Symtab->ObjectFiles) |
| 779 | File->dumpInfo(); |
| 780 | } |
| 781 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 782 | log("-- layoutMemory"); |
| 783 | layoutMemory(); |
| 784 | |
| 785 | createHeader(); |
| 786 | log("-- createSections"); |
| 787 | createSections(); |
| 788 | |
| 789 | log("-- openFile"); |
| 790 | openFile(); |
| 791 | if (errorCount()) |
| 792 | return; |
| 793 | |
| 794 | writeHeader(); |
| 795 | |
| 796 | log("-- writeSections"); |
| 797 | writeSections(); |
| 798 | if (errorCount()) |
| 799 | return; |
| 800 | |
| 801 | if (Error E = Buffer->commit()) |
| 802 | fatal("failed to write the output file: " + toString(std::move(E))); |
| 803 | } |
| 804 | |
| 805 | // Open a result file. |
| 806 | void Writer::openFile() { |
| 807 | log("writing: " + Config->OutputFile); |
| 808 | ::remove(Config->OutputFile.str().c_str()); |
| 809 | |
| 810 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 811 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 812 | FileOutputBuffer::F_executable); |
| 813 | |
| 814 | if (!BufferOrErr) |
| 815 | error("failed to open " + Config->OutputFile + ": " + |
| 816 | toString(BufferOrErr.takeError())); |
| 817 | else |
| 818 | Buffer = std::move(*BufferOrErr); |
| 819 | } |
| 820 | |
| 821 | void Writer::createHeader() { |
| 822 | raw_string_ostream OS(Header); |
| 823 | writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); |
| 824 | writeU32(OS, WasmVersion, "wasm version"); |
| 825 | OS.flush(); |
| 826 | FileSize += Header.size(); |
| 827 | } |
| 828 | |
| 829 | void lld::wasm::writeResult() { Writer().run(); } |