1 //===- InputFiles.cpp -----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InputFiles.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputEvent.h" 13 #include "InputGlobal.h" 14 #include "SymbolTable.h" 15 #include "lld/Common/ErrorHandler.h" 16 #include "lld/Common/Memory.h" 17 #include "llvm/Object/Binary.h" 18 #include "llvm/Object/Wasm.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 #define DEBUG_TYPE "lld" 22 23 using namespace lld; 24 using namespace lld::wasm; 25 26 using namespace llvm; 27 using namespace llvm::object; 28 using namespace llvm::wasm; 29 30 Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) { 31 log("Loading: " + Path); 32 33 auto MBOrErr = MemoryBuffer::getFile(Path); 34 if (auto EC = MBOrErr.getError()) { 35 error("cannot open " + Path + ": " + EC.message()); 36 return None; 37 } 38 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 39 MemoryBufferRef MBRef = MB->getMemBufferRef(); 40 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership 41 42 return MBRef; 43 } 44 45 InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB, 46 StringRef ArchiveName) { 47 file_magic Magic = identify_magic(MB.getBuffer()); 48 if (Magic == file_magic::wasm_object) { 49 std::unique_ptr<Binary> Bin = check(createBinary(MB)); 50 auto *Obj = cast<WasmObjectFile>(Bin.get()); 51 if (Obj->isSharedObject()) 52 return make<SharedFile>(MB); 53 return make<ObjFile>(MB, ArchiveName); 54 } 55 56 if (Magic == file_magic::bitcode) 57 return make<BitcodeFile>(MB, ArchiveName); 58 59 fatal("unknown file type: " + MB.getBufferIdentifier()); 60 } 61 62 void ObjFile::dumpInfo() const { 63 log("info for: " + getName() + 64 "\n Symbols : " + Twine(Symbols.size()) + 65 "\n Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) + 66 "\n Global Imports : " + Twine(WasmObj->getNumImportedGlobals()) + 67 "\n Event Imports : " + Twine(WasmObj->getNumImportedEvents())); 68 } 69 70 // Relocations contain either symbol or type indices. This function takes a 71 // relocation and returns relocated index (i.e. translates from the input 72 // symbol/type space to the output symbol/type space). 73 uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const { 74 if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) { 75 assert(TypeIsUsed[Reloc.Index]); 76 return TypeMap[Reloc.Index]; 77 } 78 return Symbols[Reloc.Index]->getOutputSymbolIndex(); 79 } 80 81 // Relocations can contain addend for combined sections. This function takes a 82 // relocation and returns updated addend by offset in the output section. 83 uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const { 84 switch (Reloc.Type) { 85 case R_WASM_MEMORY_ADDR_LEB: 86 case R_WASM_MEMORY_ADDR_SLEB: 87 case R_WASM_MEMORY_ADDR_I32: 88 case R_WASM_FUNCTION_OFFSET_I32: 89 return Reloc.Addend; 90 case R_WASM_SECTION_OFFSET_I32: 91 return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; 92 default: 93 llvm_unreachable("unexpected relocation type"); 94 } 95 } 96 97 // Calculate the value we expect to find at the relocation location. 98 // This is used as a sanity check before applying a relocation to a given 99 // location. It is useful for catching bugs in the compiler and linker. 100 uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const { 101 switch (Reloc.Type) { 102 case R_WASM_TABLE_INDEX_I32: 103 case R_WASM_TABLE_INDEX_SLEB: 104 case R_WASM_TABLE_INDEX_REL_SLEB: { 105 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 106 return TableEntries[Sym.Info.ElementIndex]; 107 } 108 case R_WASM_MEMORY_ADDR_SLEB: 109 case R_WASM_MEMORY_ADDR_I32: 110 case R_WASM_MEMORY_ADDR_LEB: 111 case R_WASM_MEMORY_ADDR_REL_SLEB: { 112 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 113 if (Sym.isUndefined()) 114 return 0; 115 const WasmSegment &Segment = 116 WasmObj->dataSegments()[Sym.Info.DataRef.Segment]; 117 return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset + 118 Reloc.Addend; 119 } 120 case R_WASM_FUNCTION_OFFSET_I32: { 121 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 122 InputFunction *F = 123 Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; 124 return F->getFunctionInputOffset() + F->getFunctionCodeOffset() + 125 Reloc.Addend; 126 } 127 case R_WASM_SECTION_OFFSET_I32: 128 return Reloc.Addend; 129 case R_WASM_TYPE_INDEX_LEB: 130 return Reloc.Index; 131 case R_WASM_FUNCTION_INDEX_LEB: 132 case R_WASM_GLOBAL_INDEX_LEB: 133 case R_WASM_EVENT_INDEX_LEB: { 134 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 135 return Sym.Info.ElementIndex; 136 } 137 default: 138 llvm_unreachable("unknown relocation type"); 139 } 140 } 141 142 // Translate from the relocation's index into the final linked output value. 143 uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const { 144 const Symbol* Sym = nullptr; 145 if (Reloc.Type != R_WASM_TYPE_INDEX_LEB) { 146 Sym = Symbols[Reloc.Index]; 147 148 // We can end up with relocations against non-live symbols. For example 149 // in debug sections. 150 if ((isa<FunctionSymbol>(Sym) || isa<DataSymbol>(Sym)) && !Sym->isLive()) 151 return 0; 152 153 // Special handling for undefined data symbols. Most relocations against 154 // such symbols cannot be resolved. 155 if (isa<DataSymbol>(Sym) && Sym->isUndefined()) { 156 if (Sym->isWeak() || Config->Relocatable) 157 return 0; 158 // R_WASM_MEMORY_ADDR_I32 relocations in PIC code are turned into runtime 159 // fixups in __wasm_apply_relocs 160 if (Config->Pic && Reloc.Type == R_WASM_MEMORY_ADDR_I32) 161 return 0; 162 if (Reloc.Type != R_WASM_GLOBAL_INDEX_LEB) { 163 llvm_unreachable( 164 ("invalid relocation against undefined data symbol: " + toString(*Sym)) 165 .c_str()); 166 } 167 } 168 } 169 170 switch (Reloc.Type) { 171 case R_WASM_TABLE_INDEX_I32: 172 case R_WASM_TABLE_INDEX_SLEB: 173 case R_WASM_TABLE_INDEX_REL_SLEB: 174 if (Config->Pic && !getFunctionSymbol(Reloc.Index)->hasTableIndex()) 175 return 0; 176 return getFunctionSymbol(Reloc.Index)->getTableIndex(); 177 case R_WASM_MEMORY_ADDR_SLEB: 178 case R_WASM_MEMORY_ADDR_I32: 179 case R_WASM_MEMORY_ADDR_LEB: 180 case R_WASM_MEMORY_ADDR_REL_SLEB: 181 return cast<DefinedData>(Sym)->getVirtualAddress() + Reloc.Addend; 182 case R_WASM_TYPE_INDEX_LEB: 183 return TypeMap[Reloc.Index]; 184 case R_WASM_FUNCTION_INDEX_LEB: 185 return getFunctionSymbol(Reloc.Index)->getFunctionIndex(); 186 case R_WASM_GLOBAL_INDEX_LEB: 187 if (auto GS = dyn_cast<GlobalSymbol>(Sym)) 188 return GS->getGlobalIndex(); 189 return Sym->getGOTIndex(); 190 case R_WASM_EVENT_INDEX_LEB: 191 return getEventSymbol(Reloc.Index)->getEventIndex(); 192 case R_WASM_FUNCTION_OFFSET_I32: { 193 auto *F = cast<DefinedFunction>(Sym); 194 return F->Function->OutputOffset + F->Function->getFunctionCodeOffset() + 195 Reloc.Addend; 196 } 197 case R_WASM_SECTION_OFFSET_I32: 198 return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; 199 default: 200 llvm_unreachable("unknown relocation type"); 201 } 202 } 203 204 template <class T> 205 static void setRelocs(const std::vector<T *> &Chunks, 206 const WasmSection *Section) { 207 if (!Section) 208 return; 209 210 ArrayRef<WasmRelocation> Relocs = Section->Relocations; 211 assert(std::is_sorted(Relocs.begin(), Relocs.end(), 212 [](const WasmRelocation &R1, const WasmRelocation &R2) { 213 return R1.Offset < R2.Offset; 214 })); 215 assert(std::is_sorted( 216 Chunks.begin(), Chunks.end(), [](InputChunk *C1, InputChunk *C2) { 217 return C1->getInputSectionOffset() < C2->getInputSectionOffset(); 218 })); 219 220 auto RelocsNext = Relocs.begin(); 221 auto RelocsEnd = Relocs.end(); 222 auto RelocLess = [](const WasmRelocation &R, uint32_t Val) { 223 return R.Offset < Val; 224 }; 225 for (InputChunk *C : Chunks) { 226 auto RelocsStart = std::lower_bound(RelocsNext, RelocsEnd, 227 C->getInputSectionOffset(), RelocLess); 228 RelocsNext = std::lower_bound( 229 RelocsStart, RelocsEnd, C->getInputSectionOffset() + C->getInputSize(), 230 RelocLess); 231 C->setRelocations(ArrayRef<WasmRelocation>(RelocsStart, RelocsNext)); 232 } 233 } 234 235 void ObjFile::parse() { 236 // Parse a memory buffer as a wasm file. 237 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n"); 238 std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this)); 239 240 auto *Obj = dyn_cast<WasmObjectFile>(Bin.get()); 241 if (!Obj) 242 fatal(toString(this) + ": not a wasm file"); 243 if (!Obj->isRelocatableObject()) 244 fatal(toString(this) + ": not a relocatable wasm file"); 245 246 Bin.release(); 247 WasmObj.reset(Obj); 248 249 // Build up a map of function indices to table indices for use when 250 // verifying the existing table index relocations 251 uint32_t TotalFunctions = 252 WasmObj->getNumImportedFunctions() + WasmObj->functions().size(); 253 TableEntries.resize(TotalFunctions); 254 for (const WasmElemSegment &Seg : WasmObj->elements()) { 255 if (Seg.Offset.Opcode != WASM_OPCODE_I32_CONST) 256 fatal(toString(this) + ": invalid table elements"); 257 uint32_t Offset = Seg.Offset.Value.Int32; 258 for (uint32_t Index = 0; Index < Seg.Functions.size(); Index++) { 259 260 uint32_t FunctionIndex = Seg.Functions[Index]; 261 TableEntries[FunctionIndex] = Offset + Index; 262 } 263 } 264 265 // Find the code and data sections. Wasm objects can have at most one code 266 // and one data section. 267 uint32_t SectionIndex = 0; 268 for (const SectionRef &Sec : WasmObj->sections()) { 269 const WasmSection &Section = WasmObj->getWasmSection(Sec); 270 if (Section.Type == WASM_SEC_CODE) { 271 CodeSection = &Section; 272 } else if (Section.Type == WASM_SEC_DATA) { 273 DataSection = &Section; 274 } else if (Section.Type == WASM_SEC_CUSTOM) { 275 CustomSections.emplace_back(make<InputSection>(Section, this)); 276 CustomSections.back()->setRelocations(Section.Relocations); 277 CustomSectionsByIndex[SectionIndex] = CustomSections.back(); 278 } 279 SectionIndex++; 280 } 281 282 TypeMap.resize(getWasmObj()->types().size()); 283 TypeIsUsed.resize(getWasmObj()->types().size(), false); 284 285 ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats; 286 UsedComdats.resize(Comdats.size()); 287 for (unsigned I = 0; I < Comdats.size(); ++I) 288 UsedComdats[I] = Symtab->addComdat(Comdats[I]); 289 290 // Populate `Segments`. 291 for (const WasmSegment &S : WasmObj->dataSegments()) 292 Segments.emplace_back(make<InputSegment>(S, this)); 293 setRelocs(Segments, DataSection); 294 295 // Populate `Functions`. 296 ArrayRef<WasmFunction> Funcs = WasmObj->functions(); 297 ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes(); 298 ArrayRef<WasmSignature> Types = WasmObj->types(); 299 Functions.reserve(Funcs.size()); 300 301 for (size_t I = 0, E = Funcs.size(); I != E; ++I) 302 Functions.emplace_back( 303 make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this)); 304 setRelocs(Functions, CodeSection); 305 306 // Populate `Globals`. 307 for (const WasmGlobal &G : WasmObj->globals()) 308 Globals.emplace_back(make<InputGlobal>(G, this)); 309 310 // Populate `Events`. 311 for (const WasmEvent &E : WasmObj->events()) 312 Events.emplace_back(make<InputEvent>(Types[E.Type.SigIndex], E, this)); 313 314 // Populate `Symbols` based on the WasmSymbols in the object. 315 Symbols.reserve(WasmObj->getNumberOfSymbols()); 316 for (const SymbolRef &Sym : WasmObj->symbols()) { 317 const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); 318 if (Symbol *Sym = createDefined(WasmSym)) 319 Symbols.push_back(Sym); 320 else 321 Symbols.push_back(createUndefined(WasmSym)); 322 } 323 } 324 325 bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { 326 uint32_t C = Chunk->getComdat(); 327 if (C == UINT32_MAX) 328 return false; 329 return !UsedComdats[C]; 330 } 331 332 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const { 333 return cast<FunctionSymbol>(Symbols[Index]); 334 } 335 336 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const { 337 return cast<GlobalSymbol>(Symbols[Index]); 338 } 339 340 EventSymbol *ObjFile::getEventSymbol(uint32_t Index) const { 341 return cast<EventSymbol>(Symbols[Index]); 342 } 343 344 SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const { 345 return cast<SectionSymbol>(Symbols[Index]); 346 } 347 348 DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const { 349 return cast<DataSymbol>(Symbols[Index]); 350 } 351 352 Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { 353 if (!Sym.isDefined()) 354 return nullptr; 355 356 StringRef Name = Sym.Info.Name; 357 uint32_t Flags = Sym.Info.Flags; 358 359 switch (Sym.Info.Kind) { 360 case WASM_SYMBOL_TYPE_FUNCTION: { 361 InputFunction *Func = 362 Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; 363 if (isExcludedByComdat(Func)) { 364 Func->Live = false; 365 return nullptr; 366 } 367 368 if (Sym.isBindingLocal()) 369 return make<DefinedFunction>(Name, Flags, this, Func); 370 return Symtab->addDefinedFunction(Name, Flags, this, Func); 371 } 372 case WASM_SYMBOL_TYPE_DATA: { 373 InputSegment *Seg = Segments[Sym.Info.DataRef.Segment]; 374 if (isExcludedByComdat(Seg)) { 375 Seg->Live = false; 376 return nullptr; 377 } 378 379 uint32_t Offset = Sym.Info.DataRef.Offset; 380 uint32_t Size = Sym.Info.DataRef.Size; 381 382 if (Sym.isBindingLocal()) 383 return make<DefinedData>(Name, Flags, this, Seg, Offset, Size); 384 return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size); 385 } 386 case WASM_SYMBOL_TYPE_GLOBAL: { 387 InputGlobal *Global = 388 Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()]; 389 if (Sym.isBindingLocal()) 390 return make<DefinedGlobal>(Name, Flags, this, Global); 391 return Symtab->addDefinedGlobal(Name, Flags, this, Global); 392 } 393 case WASM_SYMBOL_TYPE_SECTION: { 394 InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex]; 395 assert(Sym.isBindingLocal()); 396 return make<SectionSymbol>(Name, Flags, Section, this); 397 } 398 case WASM_SYMBOL_TYPE_EVENT: { 399 InputEvent *Event = 400 Events[Sym.Info.ElementIndex - WasmObj->getNumImportedEvents()]; 401 if (Sym.isBindingLocal()) 402 return make<DefinedEvent>(Name, Flags, this, Event); 403 return Symtab->addDefinedEvent(Name, Flags, this, Event); 404 } 405 } 406 llvm_unreachable("unknown symbol kind"); 407 } 408 409 Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) { 410 StringRef Name = Sym.Info.Name; 411 uint32_t Flags = Sym.Info.Flags; 412 413 switch (Sym.Info.Kind) { 414 case WASM_SYMBOL_TYPE_FUNCTION: 415 return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName, 416 Sym.Info.ImportModule, Flags, this, 417 Sym.Signature); 418 case WASM_SYMBOL_TYPE_DATA: 419 return Symtab->addUndefinedData(Name, Flags, this); 420 case WASM_SYMBOL_TYPE_GLOBAL: 421 return Symtab->addUndefinedGlobal(Name, Sym.Info.ImportName, 422 Sym.Info.ImportModule, Flags, this, 423 Sym.GlobalType); 424 case WASM_SYMBOL_TYPE_SECTION: 425 llvm_unreachable("section symbols cannot be undefined"); 426 } 427 llvm_unreachable("unknown symbol kind"); 428 } 429 430 void ArchiveFile::parse() { 431 // Parse a MemoryBufferRef as an archive file. 432 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n"); 433 File = CHECK(Archive::create(MB), toString(this)); 434 435 // Read the symbol table to construct Lazy symbols. 436 int Count = 0; 437 for (const Archive::Symbol &Sym : File->symbols()) { 438 Symtab->addLazy(this, &Sym); 439 ++Count; 440 } 441 LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n"); 442 } 443 444 void ArchiveFile::addMember(const Archive::Symbol *Sym) { 445 const Archive::Child &C = 446 CHECK(Sym->getMember(), 447 "could not get the member for symbol " + Sym->getName()); 448 449 // Don't try to load the same member twice (this can happen when members 450 // mutually reference each other). 451 if (!Seen.insert(C.getChildOffset()).second) 452 return; 453 454 LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n"); 455 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n"); 456 457 MemoryBufferRef MB = 458 CHECK(C.getMemoryBufferRef(), 459 "could not get the buffer for the member defining symbol " + 460 Sym->getName()); 461 462 InputFile *Obj = createObjectFile(MB, getName()); 463 Symtab->addFile(Obj); 464 } 465 466 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { 467 switch (GvVisibility) { 468 case GlobalValue::DefaultVisibility: 469 return WASM_SYMBOL_VISIBILITY_DEFAULT; 470 case GlobalValue::HiddenVisibility: 471 case GlobalValue::ProtectedVisibility: 472 return WASM_SYMBOL_VISIBILITY_HIDDEN; 473 } 474 llvm_unreachable("unknown visibility"); 475 } 476 477 static Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &ObjSym, 478 BitcodeFile &F) { 479 StringRef Name = Saver.save(ObjSym.getName()); 480 481 uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; 482 Flags |= mapVisibility(ObjSym.getVisibility()); 483 484 if (ObjSym.isUndefined()) { 485 if (ObjSym.isExecutable()) 486 return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F, 487 nullptr); 488 return Symtab->addUndefinedData(Name, Flags, &F); 489 } 490 491 if (ObjSym.isExecutable()) 492 return Symtab->addDefinedFunction(Name, Flags, &F, nullptr); 493 return Symtab->addDefinedData(Name, Flags, &F, nullptr, 0, 0); 494 } 495 496 void BitcodeFile::parse() { 497 Obj = check(lto::InputFile::create(MemoryBufferRef( 498 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier())))); 499 Triple T(Obj->getTargetTriple()); 500 if (T.getArch() != Triple::wasm32) { 501 error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32"); 502 return; 503 } 504 505 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) 506 Symbols.push_back(createBitcodeSymbol(ObjSym, *this)); 507 } 508 509 // Returns a string in the format of "foo.o" or "foo.a(bar.o)". 510 std::string lld::toString(const wasm::InputFile *File) { 511 if (!File) 512 return "<internal>"; 513 514 if (File->ArchiveName.empty()) 515 return File->getName(); 516 517 return (File->ArchiveName + "(" + File->getName() + ")").str(); 518 } 519