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