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