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