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