1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implementation of the MC-JIT runtime dynamic linker. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ExecutionEngine/RuntimeDyld.h" 15 #include "RuntimeDyldCOFF.h" 16 #include "RuntimeDyldCheckerImpl.h" 17 #include "RuntimeDyldELF.h" 18 #include "RuntimeDyldImpl.h" 19 #include "RuntimeDyldMachO.h" 20 #include "llvm/Object/COFF.h" 21 #include "llvm/Object/ELFObjectFile.h" 22 #include "llvm/Support/ManagedStatic.h" 23 #include "llvm/Support/MathExtras.h" 24 #include "llvm/Support/MutexGuard.h" 25 26 using namespace llvm; 27 using namespace llvm::object; 28 29 #define DEBUG_TYPE "dyld" 30 31 namespace { 32 33 enum RuntimeDyldErrorCode { 34 GenericRTDyldError = 1 35 }; 36 37 // FIXME: This class is only here to support the transition to llvm::Error. It 38 // will be removed once this transition is complete. Clients should prefer to 39 // deal with the Error value directly, rather than converting to error_code. 40 class RuntimeDyldErrorCategory : public std::error_category { 41 public: 42 const char *name() const noexcept override { return "runtimedyld"; } 43 44 std::string message(int Condition) const override { 45 switch (static_cast<RuntimeDyldErrorCode>(Condition)) { 46 case GenericRTDyldError: return "Generic RuntimeDyld error"; 47 } 48 llvm_unreachable("Unrecognized RuntimeDyldErrorCode"); 49 } 50 }; 51 52 static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory; 53 54 } 55 56 char RuntimeDyldError::ID = 0; 57 58 void RuntimeDyldError::log(raw_ostream &OS) const { 59 OS << ErrMsg << "\n"; 60 } 61 62 std::error_code RuntimeDyldError::convertToErrorCode() const { 63 return std::error_code(GenericRTDyldError, *RTDyldErrorCategory); 64 } 65 66 // Empty out-of-line virtual destructor as the key function. 67 RuntimeDyldImpl::~RuntimeDyldImpl() {} 68 69 // Pin LoadedObjectInfo's vtables to this file. 70 void RuntimeDyld::LoadedObjectInfo::anchor() {} 71 72 namespace llvm { 73 74 void RuntimeDyldImpl::registerEHFrames() {} 75 76 void RuntimeDyldImpl::deregisterEHFrames() { 77 MemMgr.deregisterEHFrames(); 78 } 79 80 #ifndef NDEBUG 81 static void dumpSectionMemory(const SectionEntry &S, StringRef State) { 82 dbgs() << "----- Contents of section " << S.getName() << " " << State 83 << " -----"; 84 85 if (S.getAddress() == nullptr) { 86 dbgs() << "\n <section not emitted>\n"; 87 return; 88 } 89 90 const unsigned ColsPerRow = 16; 91 92 uint8_t *DataAddr = S.getAddress(); 93 uint64_t LoadAddr = S.getLoadAddress(); 94 95 unsigned StartPadding = LoadAddr & (ColsPerRow - 1); 96 unsigned BytesRemaining = S.getSize(); 97 98 if (StartPadding) { 99 dbgs() << "\n" << format("0x%016" PRIx64, 100 LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":"; 101 while (StartPadding--) 102 dbgs() << " "; 103 } 104 105 while (BytesRemaining > 0) { 106 if ((LoadAddr & (ColsPerRow - 1)) == 0) 107 dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":"; 108 109 dbgs() << " " << format("%02x", *DataAddr); 110 111 ++DataAddr; 112 ++LoadAddr; 113 --BytesRemaining; 114 } 115 116 dbgs() << "\n"; 117 } 118 #endif 119 120 // Resolve the relocations for all symbols we currently know about. 121 void RuntimeDyldImpl::resolveRelocations() { 122 MutexGuard locked(lock); 123 124 // Print out the sections prior to relocation. 125 DEBUG( 126 for (int i = 0, e = Sections.size(); i != e; ++i) 127 dumpSectionMemory(Sections[i], "before relocations"); 128 ); 129 130 // First, resolve relocations associated with external symbols. 131 if (auto Err = resolveExternalSymbols()) { 132 HasError = true; 133 ErrorStr = toString(std::move(Err)); 134 } 135 136 // Iterate over all outstanding relocations 137 for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) { 138 // The Section here (Sections[i]) refers to the section in which the 139 // symbol for the relocation is located. The SectionID in the relocation 140 // entry provides the section to which the relocation will be applied. 141 int Idx = it->first; 142 uint64_t Addr = Sections[Idx].getLoadAddress(); 143 DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t" 144 << format("%p", (uintptr_t)Addr) << "\n"); 145 resolveRelocationList(it->second, Addr); 146 } 147 Relocations.clear(); 148 149 // Print out sections after relocation. 150 DEBUG( 151 for (int i = 0, e = Sections.size(); i != e; ++i) 152 dumpSectionMemory(Sections[i], "after relocations"); 153 ); 154 155 } 156 157 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, 158 uint64_t TargetAddress) { 159 MutexGuard locked(lock); 160 for (unsigned i = 0, e = Sections.size(); i != e; ++i) { 161 if (Sections[i].getAddress() == LocalAddress) { 162 reassignSectionAddress(i, TargetAddress); 163 return; 164 } 165 } 166 llvm_unreachable("Attempting to remap address of unknown section!"); 167 } 168 169 static Error getOffset(const SymbolRef &Sym, SectionRef Sec, 170 uint64_t &Result) { 171 Expected<uint64_t> AddressOrErr = Sym.getAddress(); 172 if (!AddressOrErr) 173 return AddressOrErr.takeError(); 174 Result = *AddressOrErr - Sec.getAddress(); 175 return Error::success(); 176 } 177 178 Expected<RuntimeDyldImpl::ObjSectionToIDMap> 179 RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { 180 MutexGuard locked(lock); 181 182 // Save information about our target 183 Arch = (Triple::ArchType)Obj.getArch(); 184 IsTargetLittleEndian = Obj.isLittleEndian(); 185 setMipsABI(Obj); 186 187 // Compute the memory size required to load all sections to be loaded 188 // and pass this information to the memory manager 189 if (MemMgr.needsToReserveAllocationSpace()) { 190 uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0; 191 uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1; 192 if (auto Err = computeTotalAllocSize(Obj, 193 CodeSize, CodeAlign, 194 RODataSize, RODataAlign, 195 RWDataSize, RWDataAlign)) 196 return std::move(Err); 197 MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign, 198 RWDataSize, RWDataAlign); 199 } 200 201 // Used sections from the object file 202 ObjSectionToIDMap LocalSections; 203 204 // Common symbols requiring allocation, with their sizes and alignments 205 CommonSymbolList CommonSymbols; 206 207 // Parse symbols 208 DEBUG(dbgs() << "Parse symbols:\n"); 209 for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; 210 ++I) { 211 uint32_t Flags = I->getFlags(); 212 213 // Skip undefined symbols. 214 if (Flags & SymbolRef::SF_Undefined) 215 continue; 216 217 if (Flags & SymbolRef::SF_Common) 218 CommonSymbols.push_back(*I); 219 else { 220 221 // Get the symbol type. 222 object::SymbolRef::Type SymType; 223 if (auto SymTypeOrErr = I->getType()) 224 SymType = *SymTypeOrErr; 225 else 226 return SymTypeOrErr.takeError(); 227 228 // Get symbol name. 229 StringRef Name; 230 if (auto NameOrErr = I->getName()) 231 Name = *NameOrErr; 232 else 233 return NameOrErr.takeError(); 234 235 // Compute JIT symbol flags. 236 JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(*I); 237 238 // If this is a weak definition, check to see if there's a strong one. 239 // If there is, skip this symbol (we won't be providing it: the strong 240 // definition will). If there's no strong definition, make this definition 241 // strong. 242 if (JITSymFlags.isWeak()) { 243 // First check whether there's already a definition in this instance. 244 // FIXME: Override existing weak definitions with strong ones. 245 if (GlobalSymbolTable.count(Name)) 246 continue; 247 // Then check the symbol resolver to see if there's a definition 248 // elsewhere in this logical dylib. 249 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) { 250 if (Sym.getFlags().isStrongDefinition()) 251 continue; 252 } else if (auto Err = Sym.takeError()) 253 return std::move(Err); 254 // else 255 JITSymFlags &= ~JITSymbolFlags::Weak; 256 } 257 258 if (Flags & SymbolRef::SF_Absolute && 259 SymType != object::SymbolRef::ST_File) { 260 uint64_t Addr = 0; 261 if (auto AddrOrErr = I->getAddress()) 262 Addr = *AddrOrErr; 263 else 264 return AddrOrErr.takeError(); 265 266 unsigned SectionID = AbsoluteSymbolSection; 267 268 DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name 269 << " SID: " << SectionID << " Offset: " 270 << format("%p", (uintptr_t)Addr) 271 << " flags: " << Flags << "\n"); 272 GlobalSymbolTable[Name] = 273 SymbolTableEntry(SectionID, Addr, JITSymFlags); 274 } else if (SymType == object::SymbolRef::ST_Function || 275 SymType == object::SymbolRef::ST_Data || 276 SymType == object::SymbolRef::ST_Unknown || 277 SymType == object::SymbolRef::ST_Other) { 278 279 section_iterator SI = Obj.section_end(); 280 if (auto SIOrErr = I->getSection()) 281 SI = *SIOrErr; 282 else 283 return SIOrErr.takeError(); 284 285 if (SI == Obj.section_end()) 286 continue; 287 288 // Get symbol offset. 289 uint64_t SectOffset; 290 if (auto Err = getOffset(*I, *SI, SectOffset)) 291 return std::move(Err); 292 293 bool IsCode = SI->isText(); 294 unsigned SectionID; 295 if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode, 296 LocalSections)) 297 SectionID = *SectionIDOrErr; 298 else 299 return SectionIDOrErr.takeError(); 300 301 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name 302 << " SID: " << SectionID << " Offset: " 303 << format("%p", (uintptr_t)SectOffset) 304 << " flags: " << Flags << "\n"); 305 GlobalSymbolTable[Name] = 306 SymbolTableEntry(SectionID, SectOffset, JITSymFlags); 307 } 308 } 309 } 310 311 // Allocate common symbols 312 if (auto Err = emitCommonSymbols(Obj, CommonSymbols)) 313 return std::move(Err); 314 315 // Parse and process relocations 316 DEBUG(dbgs() << "Parse relocations:\n"); 317 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 318 SI != SE; ++SI) { 319 StubMap Stubs; 320 section_iterator RelocatedSection = SI->getRelocatedSection(); 321 322 if (RelocatedSection == SE) 323 continue; 324 325 relocation_iterator I = SI->relocation_begin(); 326 relocation_iterator E = SI->relocation_end(); 327 328 if (I == E && !ProcessAllSections) 329 continue; 330 331 bool IsCode = RelocatedSection->isText(); 332 unsigned SectionID = 0; 333 if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode, 334 LocalSections)) 335 SectionID = *SectionIDOrErr; 336 else 337 return SectionIDOrErr.takeError(); 338 339 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); 340 341 for (; I != E;) 342 if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs)) 343 I = *IOrErr; 344 else 345 return IOrErr.takeError(); 346 347 // If there is an attached checker, notify it about the stubs for this 348 // section so that they can be verified. 349 if (Checker) 350 Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs); 351 } 352 353 // Give the subclasses a chance to tie-up any loose ends. 354 if (auto Err = finalizeLoad(Obj, LocalSections)) 355 return std::move(Err); 356 357 // for (auto E : LocalSections) 358 // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; 359 360 return LocalSections; 361 } 362 363 // A helper method for computeTotalAllocSize. 364 // Computes the memory size required to allocate sections with the given sizes, 365 // assuming that all sections are allocated with the given alignment 366 static uint64_t 367 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes, 368 uint64_t Alignment) { 369 uint64_t TotalSize = 0; 370 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) { 371 uint64_t AlignedSize = 372 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment; 373 TotalSize += AlignedSize; 374 } 375 return TotalSize; 376 } 377 378 static bool isRequiredForExecution(const SectionRef Section) { 379 const ObjectFile *Obj = Section.getObject(); 380 if (isa<object::ELFObjectFileBase>(Obj)) 381 return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC; 382 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) { 383 const coff_section *CoffSection = COFFObj->getCOFFSection(Section); 384 // Avoid loading zero-sized COFF sections. 385 // In PE files, VirtualSize gives the section size, and SizeOfRawData 386 // may be zero for sections with content. In Obj files, SizeOfRawData 387 // gives the section size, and VirtualSize is always zero. Hence 388 // the need to check for both cases below. 389 bool HasContent = 390 (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0); 391 bool IsDiscardable = 392 CoffSection->Characteristics & 393 (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO); 394 return HasContent && !IsDiscardable; 395 } 396 397 assert(isa<MachOObjectFile>(Obj)); 398 return true; 399 } 400 401 static bool isReadOnlyData(const SectionRef Section) { 402 const ObjectFile *Obj = Section.getObject(); 403 if (isa<object::ELFObjectFileBase>(Obj)) 404 return !(ELFSectionRef(Section).getFlags() & 405 (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); 406 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 407 return ((COFFObj->getCOFFSection(Section)->Characteristics & 408 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 409 | COFF::IMAGE_SCN_MEM_READ 410 | COFF::IMAGE_SCN_MEM_WRITE)) 411 == 412 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 413 | COFF::IMAGE_SCN_MEM_READ)); 414 415 assert(isa<MachOObjectFile>(Obj)); 416 return false; 417 } 418 419 static bool isZeroInit(const SectionRef Section) { 420 const ObjectFile *Obj = Section.getObject(); 421 if (isa<object::ELFObjectFileBase>(Obj)) 422 return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS; 423 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 424 return COFFObj->getCOFFSection(Section)->Characteristics & 425 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 426 427 auto *MachO = cast<MachOObjectFile>(Obj); 428 unsigned SectionType = MachO->getSectionType(Section); 429 return SectionType == MachO::S_ZEROFILL || 430 SectionType == MachO::S_GB_ZEROFILL; 431 } 432 433 // Compute an upper bound of the memory size that is required to load all 434 // sections 435 Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, 436 uint64_t &CodeSize, 437 uint32_t &CodeAlign, 438 uint64_t &RODataSize, 439 uint32_t &RODataAlign, 440 uint64_t &RWDataSize, 441 uint32_t &RWDataAlign) { 442 // Compute the size of all sections required for execution 443 std::vector<uint64_t> CodeSectionSizes; 444 std::vector<uint64_t> ROSectionSizes; 445 std::vector<uint64_t> RWSectionSizes; 446 447 // Collect sizes of all sections to be loaded; 448 // also determine the max alignment of all sections 449 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 450 SI != SE; ++SI) { 451 const SectionRef &Section = *SI; 452 453 bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections; 454 455 // Consider only the sections that are required to be loaded for execution 456 if (IsRequired) { 457 uint64_t DataSize = Section.getSize(); 458 uint64_t Alignment64 = Section.getAlignment(); 459 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 460 bool IsCode = Section.isText(); 461 bool IsReadOnly = isReadOnlyData(Section); 462 463 StringRef Name; 464 if (auto EC = Section.getName(Name)) 465 return errorCodeToError(EC); 466 467 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); 468 uint64_t SectionSize = DataSize + StubBufSize; 469 470 // The .eh_frame section (at least on Linux) needs an extra four bytes 471 // padded 472 // with zeroes added at the end. For MachO objects, this section has a 473 // slightly different name, so this won't have any effect for MachO 474 // objects. 475 if (Name == ".eh_frame") 476 SectionSize += 4; 477 478 if (!SectionSize) 479 SectionSize = 1; 480 481 if (IsCode) { 482 CodeAlign = std::max(CodeAlign, Alignment); 483 CodeSectionSizes.push_back(SectionSize); 484 } else if (IsReadOnly) { 485 RODataAlign = std::max(RODataAlign, Alignment); 486 ROSectionSizes.push_back(SectionSize); 487 } else { 488 RWDataAlign = std::max(RWDataAlign, Alignment); 489 RWSectionSizes.push_back(SectionSize); 490 } 491 } 492 } 493 494 // Compute Global Offset Table size. If it is not zero we 495 // also update alignment, which is equal to a size of a 496 // single GOT entry. 497 if (unsigned GotSize = computeGOTSize(Obj)) { 498 RWSectionSizes.push_back(GotSize); 499 RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize()); 500 } 501 502 // Compute the size of all common symbols 503 uint64_t CommonSize = 0; 504 uint32_t CommonAlign = 1; 505 for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; 506 ++I) { 507 uint32_t Flags = I->getFlags(); 508 if (Flags & SymbolRef::SF_Common) { 509 // Add the common symbols to a list. We'll allocate them all below. 510 uint64_t Size = I->getCommonSize(); 511 uint32_t Align = I->getAlignment(); 512 // If this is the first common symbol, use its alignment as the alignment 513 // for the common symbols section. 514 if (CommonSize == 0) 515 CommonAlign = Align; 516 CommonSize = alignTo(CommonSize, Align) + Size; 517 } 518 } 519 if (CommonSize != 0) { 520 RWSectionSizes.push_back(CommonSize); 521 RWDataAlign = std::max(RWDataAlign, CommonAlign); 522 } 523 524 // Compute the required allocation space for each different type of sections 525 // (code, read-only data, read-write data) assuming that all sections are 526 // allocated with the max alignment. Note that we cannot compute with the 527 // individual alignments of the sections, because then the required size 528 // depends on the order, in which the sections are allocated. 529 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign); 530 RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign); 531 RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign); 532 533 return Error::success(); 534 } 535 536 // compute GOT size 537 unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) { 538 size_t GotEntrySize = getGOTEntrySize(); 539 if (!GotEntrySize) 540 return 0; 541 542 size_t GotSize = 0; 543 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 544 SI != SE; ++SI) { 545 546 for (const RelocationRef &Reloc : SI->relocations()) 547 if (relocationNeedsGot(Reloc)) 548 GotSize += GotEntrySize; 549 } 550 551 return GotSize; 552 } 553 554 // compute stub buffer size for the given section 555 unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj, 556 const SectionRef &Section) { 557 unsigned StubSize = getMaxStubSize(); 558 if (StubSize == 0) { 559 return 0; 560 } 561 // FIXME: this is an inefficient way to handle this. We should computed the 562 // necessary section allocation size in loadObject by walking all the sections 563 // once. 564 unsigned StubBufSize = 0; 565 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 566 SI != SE; ++SI) { 567 section_iterator RelSecI = SI->getRelocatedSection(); 568 if (!(RelSecI == Section)) 569 continue; 570 571 for (const RelocationRef &Reloc : SI->relocations()) 572 if (relocationNeedsStub(Reloc)) 573 StubBufSize += StubSize; 574 } 575 576 // Get section data size and alignment 577 uint64_t DataSize = Section.getSize(); 578 uint64_t Alignment64 = Section.getAlignment(); 579 580 // Add stubbuf size alignment 581 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 582 unsigned StubAlignment = getStubAlignment(); 583 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); 584 if (StubAlignment > EndAlignment) 585 StubBufSize += StubAlignment - EndAlignment; 586 return StubBufSize; 587 } 588 589 uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src, 590 unsigned Size) const { 591 uint64_t Result = 0; 592 if (IsTargetLittleEndian) { 593 Src += Size - 1; 594 while (Size--) 595 Result = (Result << 8) | *Src--; 596 } else 597 while (Size--) 598 Result = (Result << 8) | *Src++; 599 600 return Result; 601 } 602 603 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst, 604 unsigned Size) const { 605 if (IsTargetLittleEndian) { 606 while (Size--) { 607 *Dst++ = Value & 0xFF; 608 Value >>= 8; 609 } 610 } else { 611 Dst += Size - 1; 612 while (Size--) { 613 *Dst-- = Value & 0xFF; 614 Value >>= 8; 615 } 616 } 617 } 618 619 Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, 620 CommonSymbolList &CommonSymbols) { 621 if (CommonSymbols.empty()) 622 return Error::success(); 623 624 uint64_t CommonSize = 0; 625 uint32_t CommonAlign = CommonSymbols.begin()->getAlignment(); 626 CommonSymbolList SymbolsToAllocate; 627 628 DEBUG(dbgs() << "Processing common symbols...\n"); 629 630 for (const auto &Sym : CommonSymbols) { 631 StringRef Name; 632 if (auto NameOrErr = Sym.getName()) 633 Name = *NameOrErr; 634 else 635 return NameOrErr.takeError(); 636 637 // Skip common symbols already elsewhere. 638 if (GlobalSymbolTable.count(Name)) { 639 DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name 640 << "'\n"); 641 continue; 642 } 643 644 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) { 645 if (!Sym.getFlags().isCommon()) { 646 DEBUG(dbgs() << "\tSkipping common symbol '" << Name 647 << "' in favor of stronger definition.\n"); 648 continue; 649 } 650 } 651 uint32_t Align = Sym.getAlignment(); 652 uint64_t Size = Sym.getCommonSize(); 653 654 CommonSize = alignTo(CommonSize, Align) + Size; 655 656 SymbolsToAllocate.push_back(Sym); 657 } 658 659 // Allocate memory for the section 660 unsigned SectionID = Sections.size(); 661 uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID, 662 "<common symbols>", false); 663 if (!Addr) 664 report_fatal_error("Unable to allocate memory for common symbols!"); 665 uint64_t Offset = 0; 666 Sections.push_back( 667 SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0)); 668 memset(Addr, 0, CommonSize); 669 670 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: " 671 << format("%p", Addr) << " DataSize: " << CommonSize << "\n"); 672 673 // Assign the address of each symbol 674 for (auto &Sym : SymbolsToAllocate) { 675 uint32_t Align = Sym.getAlignment(); 676 uint64_t Size = Sym.getCommonSize(); 677 StringRef Name; 678 if (auto NameOrErr = Sym.getName()) 679 Name = *NameOrErr; 680 else 681 return NameOrErr.takeError(); 682 if (Align) { 683 // This symbol has an alignment requirement. 684 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); 685 Addr += AlignOffset; 686 Offset += AlignOffset; 687 } 688 JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(Sym); 689 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " 690 << format("%p", Addr) << "\n"); 691 GlobalSymbolTable[Name] = 692 SymbolTableEntry(SectionID, Offset, JITSymFlags); 693 Offset += Size; 694 Addr += Size; 695 } 696 697 if (Checker) 698 Checker->registerSection(Obj.getFileName(), SectionID); 699 700 return Error::success(); 701 } 702 703 Expected<unsigned> 704 RuntimeDyldImpl::emitSection(const ObjectFile &Obj, 705 const SectionRef &Section, 706 bool IsCode) { 707 StringRef data; 708 uint64_t Alignment64 = Section.getAlignment(); 709 710 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 711 unsigned PaddingSize = 0; 712 unsigned StubBufSize = 0; 713 bool IsRequired = isRequiredForExecution(Section); 714 bool IsVirtual = Section.isVirtual(); 715 bool IsZeroInit = isZeroInit(Section); 716 bool IsReadOnly = isReadOnlyData(Section); 717 uint64_t DataSize = Section.getSize(); 718 719 StringRef Name; 720 if (auto EC = Section.getName(Name)) 721 return errorCodeToError(EC); 722 723 StubBufSize = computeSectionStubBufSize(Obj, Section); 724 725 // The .eh_frame section (at least on Linux) needs an extra four bytes padded 726 // with zeroes added at the end. For MachO objects, this section has a 727 // slightly different name, so this won't have any effect for MachO objects. 728 if (Name == ".eh_frame") 729 PaddingSize = 4; 730 731 uintptr_t Allocate; 732 unsigned SectionID = Sections.size(); 733 uint8_t *Addr; 734 const char *pData = nullptr; 735 736 // If this section contains any bits (i.e. isn't a virtual or bss section), 737 // grab a reference to them. 738 if (!IsVirtual && !IsZeroInit) { 739 // In either case, set the location of the unrelocated section in memory, 740 // since we still process relocations for it even if we're not applying them. 741 if (auto EC = Section.getContents(data)) 742 return errorCodeToError(EC); 743 pData = data.data(); 744 } 745 746 // Code section alignment needs to be at least as high as stub alignment or 747 // padding calculations may by incorrect when the section is remapped to a 748 // higher alignment. 749 if (IsCode) 750 Alignment = std::max(Alignment, getStubAlignment()); 751 752 // Some sections, such as debug info, don't need to be loaded for execution. 753 // Process those only if explicitly requested. 754 if (IsRequired || ProcessAllSections) { 755 Allocate = DataSize + PaddingSize + StubBufSize; 756 if (!Allocate) 757 Allocate = 1; 758 Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID, 759 Name) 760 : MemMgr.allocateDataSection(Allocate, Alignment, SectionID, 761 Name, IsReadOnly); 762 if (!Addr) 763 report_fatal_error("Unable to allocate section memory!"); 764 765 // Zero-initialize or copy the data from the image 766 if (IsZeroInit || IsVirtual) 767 memset(Addr, 0, DataSize); 768 else 769 memcpy(Addr, pData, DataSize); 770 771 // Fill in any extra bytes we allocated for padding 772 if (PaddingSize != 0) { 773 memset(Addr + DataSize, 0, PaddingSize); 774 // Update the DataSize variable so that the stub offset is set correctly. 775 DataSize += PaddingSize; 776 } 777 778 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name 779 << " obj addr: " << format("%p", pData) 780 << " new addr: " << format("%p", Addr) 781 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize 782 << " Allocate: " << Allocate << "\n"); 783 } else { 784 // Even if we didn't load the section, we need to record an entry for it 785 // to handle later processing (and by 'handle' I mean don't do anything 786 // with these sections). 787 Allocate = 0; 788 Addr = nullptr; 789 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name 790 << " obj addr: " << format("%p", data.data()) << " new addr: 0" 791 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize 792 << " Allocate: " << Allocate << "\n"); 793 } 794 795 Sections.push_back( 796 SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData)); 797 798 // Debug info sections are linked as if their load address was zero 799 if (!IsRequired) 800 Sections.back().setLoadAddress(0); 801 802 if (Checker) 803 Checker->registerSection(Obj.getFileName(), SectionID); 804 805 return SectionID; 806 } 807 808 Expected<unsigned> 809 RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj, 810 const SectionRef &Section, 811 bool IsCode, 812 ObjSectionToIDMap &LocalSections) { 813 814 unsigned SectionID = 0; 815 ObjSectionToIDMap::iterator i = LocalSections.find(Section); 816 if (i != LocalSections.end()) 817 SectionID = i->second; 818 else { 819 if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode)) 820 SectionID = *SectionIDOrErr; 821 else 822 return SectionIDOrErr.takeError(); 823 LocalSections[Section] = SectionID; 824 } 825 return SectionID; 826 } 827 828 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, 829 unsigned SectionID) { 830 Relocations[SectionID].push_back(RE); 831 } 832 833 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, 834 StringRef SymbolName) { 835 // Relocation by symbol. If the symbol is found in the global symbol table, 836 // create an appropriate section relocation. Otherwise, add it to 837 // ExternalSymbolRelocations. 838 RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName); 839 if (Loc == GlobalSymbolTable.end()) { 840 ExternalSymbolRelocations[SymbolName].push_back(RE); 841 } else { 842 // Copy the RE since we want to modify its addend. 843 RelocationEntry RECopy = RE; 844 const auto &SymInfo = Loc->second; 845 RECopy.Addend += SymInfo.getOffset(); 846 Relocations[SymInfo.getSectionID()].push_back(RECopy); 847 } 848 } 849 850 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr, 851 unsigned AbiVariant) { 852 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) { 853 // This stub has to be able to access the full address space, 854 // since symbol lookup won't necessarily find a handy, in-range, 855 // PLT stub for functions which could be anywhere. 856 // Stub can use ip0 (== x16) to calculate address 857 writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr> 858 writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr> 859 writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr> 860 writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr> 861 writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0 862 863 return Addr; 864 } else if (Arch == Triple::arm || Arch == Triple::armeb) { 865 // TODO: There is only ARM far stub now. We should add the Thumb stub, 866 // and stubs for branches Thumb - ARM and ARM - Thumb. 867 writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label> 868 return Addr + 4; 869 } else if (IsMipsO32ABI) { 870 // 0: 3c190000 lui t9,%hi(addr). 871 // 4: 27390000 addiu t9,t9,%lo(addr). 872 // 8: 03200008 jr t9. 873 // c: 00000000 nop. 874 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; 875 const unsigned NopInstr = 0x0; 876 unsigned JrT9Instr = 0x03200008; 877 if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6) 878 JrT9Instr = 0x03200009; 879 880 writeBytesUnaligned(LuiT9Instr, Addr, 4); 881 writeBytesUnaligned(AdduiT9Instr, Addr+4, 4); 882 writeBytesUnaligned(JrT9Instr, Addr+8, 4); 883 writeBytesUnaligned(NopInstr, Addr+12, 4); 884 return Addr; 885 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 886 // Depending on which version of the ELF ABI is in use, we need to 887 // generate one of two variants of the stub. They both start with 888 // the same sequence to load the target address into r12. 889 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) 890 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) 891 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 892 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) 893 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) 894 if (AbiVariant == 2) { 895 // PowerPC64 stub ELFv2 ABI: The address points to the function itself. 896 // The address is already in r12 as required by the ABI. Branch to it. 897 writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1) 898 writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12 899 writeInt32BE(Addr+28, 0x4E800420); // bctr 900 } else { 901 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor. 902 // Load the function address on r11 and sets it to control register. Also 903 // loads the function TOC in r2 and environment pointer to r11. 904 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) 905 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) 906 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) 907 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 908 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) 909 writeInt32BE(Addr+40, 0x4E800420); // bctr 910 } 911 return Addr; 912 } else if (Arch == Triple::systemz) { 913 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 914 writeInt16BE(Addr+2, 0x0000); 915 writeInt16BE(Addr+4, 0x0004); 916 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 917 // 8-byte address stored at Addr + 8 918 return Addr; 919 } else if (Arch == Triple::x86_64) { 920 *Addr = 0xFF; // jmp 921 *(Addr+1) = 0x25; // rip 922 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2 923 } else if (Arch == Triple::x86) { 924 *Addr = 0xE9; // 32-bit pc-relative jump. 925 } 926 return Addr; 927 } 928 929 // Assign an address to a symbol name and resolve all the relocations 930 // associated with it. 931 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, 932 uint64_t Addr) { 933 // The address to use for relocation resolution is not 934 // the address of the local section buffer. We must be doing 935 // a remote execution environment of some sort. Relocations can't 936 // be applied until all the sections have been moved. The client must 937 // trigger this with a call to MCJIT::finalize() or 938 // RuntimeDyld::resolveRelocations(). 939 // 940 // Addr is a uint64_t because we can't assume the pointer width 941 // of the target is the same as that of the host. Just use a generic 942 // "big enough" type. 943 DEBUG(dbgs() << "Reassigning address for section " << SectionID << " (" 944 << Sections[SectionID].getName() << "): " 945 << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress()) 946 << " -> " << format("0x%016" PRIx64, Addr) << "\n"); 947 Sections[SectionID].setLoadAddress(Addr); 948 } 949 950 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, 951 uint64_t Value) { 952 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 953 const RelocationEntry &RE = Relocs[i]; 954 // Ignore relocations for sections that were not loaded 955 if (Sections[RE.SectionID].getAddress() == nullptr) 956 continue; 957 resolveRelocation(RE, Value); 958 } 959 } 960 961 Error RuntimeDyldImpl::resolveExternalSymbols() { 962 while (!ExternalSymbolRelocations.empty()) { 963 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(); 964 965 StringRef Name = i->first(); 966 if (Name.size() == 0) { 967 // This is an absolute symbol, use an address of zero. 968 DEBUG(dbgs() << "Resolving absolute relocations." 969 << "\n"); 970 RelocationList &Relocs = i->second; 971 resolveRelocationList(Relocs, 0); 972 } else { 973 uint64_t Addr = 0; 974 RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name); 975 if (Loc == GlobalSymbolTable.end()) { 976 // This is an external symbol, try to get its address from the symbol 977 // resolver. 978 // First search for the symbol in this logical dylib. 979 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name.data())) { 980 if (auto AddrOrErr = Sym.getAddress()) 981 Addr = *AddrOrErr; 982 else 983 return AddrOrErr.takeError(); 984 } else if (auto Err = Sym.takeError()) 985 return Err; 986 987 // If that fails, try searching for an external symbol. 988 if (!Addr) { 989 if (auto Sym = Resolver.findSymbol(Name.data())) { 990 if (auto AddrOrErr = Sym.getAddress()) 991 Addr = *AddrOrErr; 992 else 993 return AddrOrErr.takeError(); 994 } else if (auto Err = Sym.takeError()) 995 return Err; 996 } 997 // The call to getSymbolAddress may have caused additional modules to 998 // be loaded, which may have added new entries to the 999 // ExternalSymbolRelocations map. Consquently, we need to update our 1000 // iterator. This is also why retrieval of the relocation list 1001 // associated with this symbol is deferred until below this point. 1002 // New entries may have been added to the relocation list. 1003 i = ExternalSymbolRelocations.find(Name); 1004 } else { 1005 // We found the symbol in our global table. It was probably in a 1006 // Module that we loaded previously. 1007 const auto &SymInfo = Loc->second; 1008 Addr = getSectionLoadAddress(SymInfo.getSectionID()) + 1009 SymInfo.getOffset(); 1010 } 1011 1012 // FIXME: Implement error handling that doesn't kill the host program! 1013 if (!Addr) 1014 report_fatal_error("Program used external function '" + Name + 1015 "' which could not be resolved!"); 1016 1017 // If Resolver returned UINT64_MAX, the client wants to handle this symbol 1018 // manually and we shouldn't resolve its relocations. 1019 if (Addr != UINT64_MAX) { 1020 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t" 1021 << format("0x%lx", Addr) << "\n"); 1022 // This list may have been updated when we called getSymbolAddress, so 1023 // don't change this code to get the list earlier. 1024 RelocationList &Relocs = i->second; 1025 resolveRelocationList(Relocs, Addr); 1026 } 1027 } 1028 1029 ExternalSymbolRelocations.erase(i); 1030 } 1031 1032 return Error::success(); 1033 } 1034 1035 //===----------------------------------------------------------------------===// 1036 // RuntimeDyld class implementation 1037 1038 uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( 1039 const object::SectionRef &Sec) const { 1040 1041 auto I = ObjSecToIDMap.find(Sec); 1042 if (I != ObjSecToIDMap.end()) 1043 return RTDyld.Sections[I->second].getLoadAddress(); 1044 1045 return 0; 1046 } 1047 1048 void RuntimeDyld::MemoryManager::anchor() {} 1049 void JITSymbolResolver::anchor() {} 1050 1051 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr, 1052 JITSymbolResolver &Resolver) 1053 : MemMgr(MemMgr), Resolver(Resolver) { 1054 // FIXME: There's a potential issue lurking here if a single instance of 1055 // RuntimeDyld is used to load multiple objects. The current implementation 1056 // associates a single memory manager with a RuntimeDyld instance. Even 1057 // though the public class spawns a new 'impl' instance for each load, 1058 // they share a single memory manager. This can become a problem when page 1059 // permissions are applied. 1060 Dyld = nullptr; 1061 ProcessAllSections = false; 1062 Checker = nullptr; 1063 } 1064 1065 RuntimeDyld::~RuntimeDyld() {} 1066 1067 static std::unique_ptr<RuntimeDyldCOFF> 1068 createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 1069 JITSymbolResolver &Resolver, bool ProcessAllSections, 1070 RuntimeDyldCheckerImpl *Checker) { 1071 std::unique_ptr<RuntimeDyldCOFF> Dyld = 1072 RuntimeDyldCOFF::create(Arch, MM, Resolver); 1073 Dyld->setProcessAllSections(ProcessAllSections); 1074 Dyld->setRuntimeDyldChecker(Checker); 1075 return Dyld; 1076 } 1077 1078 static std::unique_ptr<RuntimeDyldELF> 1079 createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 1080 JITSymbolResolver &Resolver, bool ProcessAllSections, 1081 RuntimeDyldCheckerImpl *Checker) { 1082 std::unique_ptr<RuntimeDyldELF> Dyld = 1083 RuntimeDyldELF::create(Arch, MM, Resolver); 1084 Dyld->setProcessAllSections(ProcessAllSections); 1085 Dyld->setRuntimeDyldChecker(Checker); 1086 return Dyld; 1087 } 1088 1089 static std::unique_ptr<RuntimeDyldMachO> 1090 createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 1091 JITSymbolResolver &Resolver, 1092 bool ProcessAllSections, 1093 RuntimeDyldCheckerImpl *Checker) { 1094 std::unique_ptr<RuntimeDyldMachO> Dyld = 1095 RuntimeDyldMachO::create(Arch, MM, Resolver); 1096 Dyld->setProcessAllSections(ProcessAllSections); 1097 Dyld->setRuntimeDyldChecker(Checker); 1098 return Dyld; 1099 } 1100 1101 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 1102 RuntimeDyld::loadObject(const ObjectFile &Obj) { 1103 if (!Dyld) { 1104 if (Obj.isELF()) 1105 Dyld = 1106 createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()), 1107 MemMgr, Resolver, ProcessAllSections, Checker); 1108 else if (Obj.isMachO()) 1109 Dyld = createRuntimeDyldMachO( 1110 static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 1111 ProcessAllSections, Checker); 1112 else if (Obj.isCOFF()) 1113 Dyld = createRuntimeDyldCOFF( 1114 static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 1115 ProcessAllSections, Checker); 1116 else 1117 report_fatal_error("Incompatible object format!"); 1118 } 1119 1120 if (!Dyld->isCompatibleFile(Obj)) 1121 report_fatal_error("Incompatible object format!"); 1122 1123 auto LoadedObjInfo = Dyld->loadObject(Obj); 1124 MemMgr.notifyObjectLoaded(*this, Obj); 1125 return LoadedObjInfo; 1126 } 1127 1128 void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const { 1129 if (!Dyld) 1130 return nullptr; 1131 return Dyld->getSymbolLocalAddress(Name); 1132 } 1133 1134 JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const { 1135 if (!Dyld) 1136 return nullptr; 1137 return Dyld->getSymbol(Name); 1138 } 1139 1140 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); } 1141 1142 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { 1143 Dyld->reassignSectionAddress(SectionID, Addr); 1144 } 1145 1146 void RuntimeDyld::mapSectionAddress(const void *LocalAddress, 1147 uint64_t TargetAddress) { 1148 Dyld->mapSectionAddress(LocalAddress, TargetAddress); 1149 } 1150 1151 bool RuntimeDyld::hasError() { return Dyld->hasError(); } 1152 1153 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); } 1154 1155 void RuntimeDyld::finalizeWithMemoryManagerLocking() { 1156 bool MemoryFinalizationLocked = MemMgr.FinalizationLocked; 1157 MemMgr.FinalizationLocked = true; 1158 resolveRelocations(); 1159 registerEHFrames(); 1160 if (!MemoryFinalizationLocked) { 1161 MemMgr.finalizeMemory(); 1162 MemMgr.FinalizationLocked = false; 1163 } 1164 } 1165 1166 void RuntimeDyld::registerEHFrames() { 1167 if (Dyld) 1168 Dyld->registerEHFrames(); 1169 } 1170 1171 void RuntimeDyld::deregisterEHFrames() { 1172 if (Dyld) 1173 Dyld->deregisterEHFrames(); 1174 } 1175 1176 } // end namespace llvm 1177