1 //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===// 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 // This file defines the XCOFFObjectFile class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/XCOFFObjectFile.h" 14 #include "llvm/ADT/StringSwitch.h" 15 #include "llvm/MC/SubtargetFeature.h" 16 #include "llvm/Support/DataExtractor.h" 17 #include <cstddef> 18 #include <cstring> 19 20 namespace llvm { 21 22 using namespace XCOFF; 23 24 namespace object { 25 26 static const uint8_t FunctionSym = 0x20; 27 static const uint16_t NoRelMask = 0x0001; 28 static const size_t SymbolAuxTypeOffset = 17; 29 30 // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer 31 // 'M'. Returns a pointer to the underlying object on success. 32 template <typename T> 33 static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr, 34 const uint64_t Size = sizeof(T)) { 35 uintptr_t Addr = reinterpret_cast<uintptr_t>(Ptr); 36 if (Error E = Binary::checkOffset(M, Addr, Size)) 37 return std::move(E); 38 return reinterpret_cast<const T *>(Addr); 39 } 40 41 static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) { 42 return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) + 43 Offset); 44 } 45 46 template <typename T> static const T *viewAs(uintptr_t in) { 47 return reinterpret_cast<const T *>(in); 48 } 49 50 static StringRef generateXCOFFFixedNameStringRef(const char *Name) { 51 auto NulCharPtr = 52 static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize)); 53 return NulCharPtr ? StringRef(Name, NulCharPtr - Name) 54 : StringRef(Name, XCOFF::NameSize); 55 } 56 57 template <typename T> StringRef XCOFFSectionHeader<T>::getName() const { 58 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 59 return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name); 60 } 61 62 template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const { 63 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 64 return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask; 65 } 66 67 template <typename T> 68 bool XCOFFSectionHeader<T>::isReservedSectionType() const { 69 return getSectionType() & SectionFlagsReservedMask; 70 } 71 72 template <typename AddressType> 73 bool XCOFFRelocation<AddressType>::isRelocationSigned() const { 74 return Info & XR_SIGN_INDICATOR_MASK; 75 } 76 77 template <typename AddressType> 78 bool XCOFFRelocation<AddressType>::isFixupIndicated() const { 79 return Info & XR_FIXUP_INDICATOR_MASK; 80 } 81 82 template <typename AddressType> 83 uint8_t XCOFFRelocation<AddressType>::getRelocatedLength() const { 84 // The relocation encodes the bit length being relocated minus 1. Add back 85 // the 1 to get the actual length being relocated. 86 return (Info & XR_BIASED_LENGTH_MASK) + 1; 87 } 88 89 uintptr_t 90 XCOFFObjectFile::getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress, 91 uint32_t Distance) { 92 return getWithOffset(CurrentAddress, Distance * XCOFF::SymbolTableEntrySize); 93 } 94 95 const XCOFF::SymbolAuxType * 96 XCOFFObjectFile::getSymbolAuxType(uintptr_t AuxEntryAddress) const { 97 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 98 return viewAs<XCOFF::SymbolAuxType>( 99 getWithOffset(AuxEntryAddress, SymbolAuxTypeOffset)); 100 } 101 102 void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr, 103 uintptr_t TableAddress) const { 104 if (Addr < TableAddress) 105 report_fatal_error("Section header outside of section header table."); 106 107 uintptr_t Offset = Addr - TableAddress; 108 if (Offset >= getSectionHeaderSize() * getNumberOfSections()) 109 report_fatal_error("Section header outside of section header table."); 110 111 if (Offset % getSectionHeaderSize() != 0) 112 report_fatal_error( 113 "Section header pointer does not point to a valid section header."); 114 } 115 116 const XCOFFSectionHeader32 * 117 XCOFFObjectFile::toSection32(DataRefImpl Ref) const { 118 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 119 #ifndef NDEBUG 120 checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 121 #endif 122 return viewAs<XCOFFSectionHeader32>(Ref.p); 123 } 124 125 const XCOFFSectionHeader64 * 126 XCOFFObjectFile::toSection64(DataRefImpl Ref) const { 127 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 128 #ifndef NDEBUG 129 checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 130 #endif 131 return viewAs<XCOFFSectionHeader64>(Ref.p); 132 } 133 134 XCOFFSymbolRef XCOFFObjectFile::toSymbolRef(DataRefImpl Ref) const { 135 assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!"); 136 #ifndef NDEBUG 137 checkSymbolEntryPointer(Ref.p); 138 #endif 139 return XCOFFSymbolRef(Ref, this); 140 } 141 142 const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const { 143 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 144 return static_cast<const XCOFFFileHeader32 *>(FileHeader); 145 } 146 147 const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const { 148 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 149 return static_cast<const XCOFFFileHeader64 *>(FileHeader); 150 } 151 152 template <typename T> const T *XCOFFObjectFile::sectionHeaderTable() const { 153 return static_cast<const T *>(SectionHeaderTable); 154 } 155 156 const XCOFFSectionHeader32 * 157 XCOFFObjectFile::sectionHeaderTable32() const { 158 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 159 return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable); 160 } 161 162 const XCOFFSectionHeader64 * 163 XCOFFObjectFile::sectionHeaderTable64() const { 164 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 165 return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable); 166 } 167 168 void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 169 uintptr_t NextSymbolAddr = getAdvancedSymbolEntryAddress( 170 Symb.p, toSymbolRef(Symb).getNumberOfAuxEntries() + 1); 171 #ifndef NDEBUG 172 // This function is used by basic_symbol_iterator, which allows to 173 // point to the end-of-symbol-table address. 174 if (NextSymbolAddr != getEndOfSymbolTableAddress()) 175 checkSymbolEntryPointer(NextSymbolAddr); 176 #endif 177 Symb.p = NextSymbolAddr; 178 } 179 180 Expected<StringRef> 181 XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const { 182 // The byte offset is relative to the start of the string table. 183 // A byte offset value of 0 is a null or zero-length symbol 184 // name. A byte offset in the range 1 to 3 (inclusive) points into the length 185 // field; as a soft-error recovery mechanism, we treat such cases as having an 186 // offset of 0. 187 if (Offset < 4) 188 return StringRef(nullptr, 0); 189 190 if (StringTable.Data != nullptr && StringTable.Size > Offset) 191 return (StringTable.Data + Offset); 192 193 return make_error<GenericBinaryError>("Bad offset for string table entry", 194 object_error::parse_failed); 195 } 196 197 StringRef XCOFFObjectFile::getStringTable() const { 198 // If the size is less than or equal to 4, then the string table contains no 199 // string data. 200 return StringRef(StringTable.Data, 201 StringTable.Size <= 4 ? 0 : StringTable.Size); 202 } 203 204 Expected<StringRef> 205 XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const { 206 if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC) 207 return generateXCOFFFixedNameStringRef(CFileEntPtr->Name); 208 return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset); 209 } 210 211 Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const { 212 return toSymbolRef(Symb).getName(); 213 } 214 215 Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { 216 return toSymbolRef(Symb).getValue(); 217 } 218 219 uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 220 return toSymbolRef(Symb).getValue(); 221 } 222 223 uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 224 uint64_t Result = 0; 225 llvm_unreachable("Not yet implemented!"); 226 return Result; 227 } 228 229 Expected<SymbolRef::Type> 230 XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const { 231 // TODO: Return the correct symbol type. 232 return SymbolRef::ST_Other; 233 } 234 235 Expected<section_iterator> 236 XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const { 237 const int16_t SectNum = toSymbolRef(Symb).getSectionNumber(); 238 239 if (isReservedSectionNumber(SectNum)) 240 return section_end(); 241 242 Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum); 243 if (!ExpSec) 244 return ExpSec.takeError(); 245 246 return section_iterator(SectionRef(ExpSec.get(), this)); 247 } 248 249 void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const { 250 const char *Ptr = reinterpret_cast<const char *>(Sec.p); 251 Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize()); 252 } 253 254 Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const { 255 return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec)); 256 } 257 258 uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const { 259 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 260 // with MSVC. 261 if (is64Bit()) 262 return toSection64(Sec)->VirtualAddress; 263 264 return toSection32(Sec)->VirtualAddress; 265 } 266 267 uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const { 268 // Section numbers in XCOFF are numbered beginning at 1. A section number of 269 // zero is used to indicate that a symbol is being imported or is undefined. 270 if (is64Bit()) 271 return toSection64(Sec) - sectionHeaderTable64() + 1; 272 else 273 return toSection32(Sec) - sectionHeaderTable32() + 1; 274 } 275 276 uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const { 277 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 278 // with MSVC. 279 if (is64Bit()) 280 return toSection64(Sec)->SectionSize; 281 282 return toSection32(Sec)->SectionSize; 283 } 284 285 Expected<ArrayRef<uint8_t>> 286 XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const { 287 if (isSectionVirtual(Sec)) 288 return ArrayRef<uint8_t>(); 289 290 uint64_t OffsetToRaw; 291 if (is64Bit()) 292 OffsetToRaw = toSection64(Sec)->FileOffsetToRawData; 293 else 294 OffsetToRaw = toSection32(Sec)->FileOffsetToRawData; 295 296 const uint8_t * ContentStart = base() + OffsetToRaw; 297 uint64_t SectionSize = getSectionSize(Sec); 298 if (checkOffset(Data, reinterpret_cast<uintptr_t>(ContentStart), SectionSize)) 299 return make_error<BinaryError>(); 300 301 return makeArrayRef(ContentStart,SectionSize); 302 } 303 304 uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { 305 uint64_t Result = 0; 306 llvm_unreachable("Not yet implemented!"); 307 return Result; 308 } 309 310 bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 311 return false; 312 } 313 314 bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const { 315 return getSectionFlags(Sec) & XCOFF::STYP_TEXT; 316 } 317 318 bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const { 319 uint32_t Flags = getSectionFlags(Sec); 320 return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA); 321 } 322 323 bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const { 324 uint32_t Flags = getSectionFlags(Sec); 325 return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS); 326 } 327 328 bool XCOFFObjectFile::isDebugSection(DataRefImpl Sec) const { 329 uint32_t Flags = getSectionFlags(Sec); 330 return Flags & (XCOFF::STYP_DEBUG | XCOFF::STYP_DWARF); 331 } 332 333 bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const { 334 return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0 335 : toSection32(Sec)->FileOffsetToRawData == 0; 336 } 337 338 relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const { 339 DataRefImpl Ret; 340 if (is64Bit()) { 341 const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec); 342 auto RelocationsOrErr = 343 relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr); 344 if (Error E = RelocationsOrErr.takeError()) { 345 // TODO: report the error up the stack. 346 consumeError(std::move(E)); 347 return relocation_iterator(RelocationRef()); 348 } 349 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin()); 350 } else { 351 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec); 352 auto RelocationsOrErr = 353 relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr); 354 if (Error E = RelocationsOrErr.takeError()) { 355 // TODO: report the error up the stack. 356 consumeError(std::move(E)); 357 return relocation_iterator(RelocationRef()); 358 } 359 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin()); 360 } 361 return relocation_iterator(RelocationRef(Ret, this)); 362 } 363 364 relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const { 365 DataRefImpl Ret; 366 if (is64Bit()) { 367 const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec); 368 auto RelocationsOrErr = 369 relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr); 370 if (Error E = RelocationsOrErr.takeError()) { 371 // TODO: report the error up the stack. 372 consumeError(std::move(E)); 373 return relocation_iterator(RelocationRef()); 374 } 375 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end()); 376 } else { 377 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec); 378 auto RelocationsOrErr = 379 relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr); 380 if (Error E = RelocationsOrErr.takeError()) { 381 // TODO: report the error up the stack. 382 consumeError(std::move(E)); 383 return relocation_iterator(RelocationRef()); 384 } 385 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end()); 386 } 387 return relocation_iterator(RelocationRef(Ret, this)); 388 } 389 390 void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 391 if (is64Bit()) 392 Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation64>(Rel.p) + 1); 393 else 394 Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1); 395 } 396 397 uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { 398 if (is64Bit()) { 399 const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p); 400 const XCOFFSectionHeader64 *Sec64 = sectionHeaderTable64(); 401 const uint64_t RelocAddress = Reloc->VirtualAddress; 402 const uint16_t NumberOfSections = getNumberOfSections(); 403 for (uint16_t I = 0; I < NumberOfSections; ++I) { 404 // Find which section this relocation belongs to, and get the 405 // relocation offset relative to the start of the section. 406 if (Sec64->VirtualAddress <= RelocAddress && 407 RelocAddress < Sec64->VirtualAddress + Sec64->SectionSize) { 408 return RelocAddress - Sec64->VirtualAddress; 409 } 410 ++Sec64; 411 } 412 } else { 413 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 414 const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32(); 415 const uint32_t RelocAddress = Reloc->VirtualAddress; 416 const uint16_t NumberOfSections = getNumberOfSections(); 417 for (uint16_t I = 0; I < NumberOfSections; ++I) { 418 // Find which section this relocation belongs to, and get the 419 // relocation offset relative to the start of the section. 420 if (Sec32->VirtualAddress <= RelocAddress && 421 RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) { 422 return RelocAddress - Sec32->VirtualAddress; 423 } 424 ++Sec32; 425 } 426 } 427 return InvalidRelocOffset; 428 } 429 430 symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 431 uint32_t Index; 432 if (is64Bit()) { 433 const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p); 434 Index = Reloc->SymbolIndex; 435 436 if (Index >= getNumberOfSymbolTableEntries64()) 437 return symbol_end(); 438 } else { 439 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 440 Index = Reloc->SymbolIndex; 441 442 if (Index >= getLogicalNumberOfSymbolTableEntries32()) 443 return symbol_end(); 444 } 445 DataRefImpl SymDRI; 446 SymDRI.p = getSymbolEntryAddressByIndex(Index); 447 return symbol_iterator(SymbolRef(SymDRI, this)); 448 } 449 450 uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const { 451 if (is64Bit()) 452 return viewAs<XCOFFRelocation64>(Rel.p)->Type; 453 return viewAs<XCOFFRelocation32>(Rel.p)->Type; 454 } 455 456 void XCOFFObjectFile::getRelocationTypeName( 457 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 458 StringRef Res; 459 if (is64Bit()) { 460 const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p); 461 Res = XCOFF::getRelocationTypeString(Reloc->Type); 462 } else { 463 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 464 Res = XCOFF::getRelocationTypeString(Reloc->Type); 465 } 466 Result.append(Res.begin(), Res.end()); 467 } 468 469 Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { 470 uint32_t Result = 0; 471 // TODO: Return correct symbol flags. 472 return Result; 473 } 474 475 basic_symbol_iterator XCOFFObjectFile::symbol_begin() const { 476 DataRefImpl SymDRI; 477 SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr); 478 return basic_symbol_iterator(SymbolRef(SymDRI, this)); 479 } 480 481 basic_symbol_iterator XCOFFObjectFile::symbol_end() const { 482 DataRefImpl SymDRI; 483 const uint32_t NumberOfSymbolTableEntries = getNumberOfSymbolTableEntries(); 484 SymDRI.p = getSymbolEntryAddressByIndex(NumberOfSymbolTableEntries); 485 return basic_symbol_iterator(SymbolRef(SymDRI, this)); 486 } 487 488 section_iterator XCOFFObjectFile::section_begin() const { 489 DataRefImpl DRI; 490 DRI.p = getSectionHeaderTableAddress(); 491 return section_iterator(SectionRef(DRI, this)); 492 } 493 494 section_iterator XCOFFObjectFile::section_end() const { 495 DataRefImpl DRI; 496 DRI.p = getWithOffset(getSectionHeaderTableAddress(), 497 getNumberOfSections() * getSectionHeaderSize()); 498 return section_iterator(SectionRef(DRI, this)); 499 } 500 501 uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; } 502 503 StringRef XCOFFObjectFile::getFileFormatName() const { 504 return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000"; 505 } 506 507 Triple::ArchType XCOFFObjectFile::getArch() const { 508 return is64Bit() ? Triple::ppc64 : Triple::ppc; 509 } 510 511 SubtargetFeatures XCOFFObjectFile::getFeatures() const { 512 return SubtargetFeatures(); 513 } 514 515 bool XCOFFObjectFile::isRelocatableObject() const { 516 if (is64Bit()) 517 return !(fileHeader64()->Flags & NoRelMask); 518 return !(fileHeader32()->Flags & NoRelMask); 519 } 520 521 Expected<uint64_t> XCOFFObjectFile::getStartAddress() const { 522 // TODO FIXME Should get from auxiliary_header->o_entry when support for the 523 // auxiliary_header is added. 524 return 0; 525 } 526 527 StringRef XCOFFObjectFile::mapDebugSectionName(StringRef Name) const { 528 return StringSwitch<StringRef>(Name) 529 .Case("dwinfo", "debug_info") 530 .Case("dwline", "debug_line") 531 .Case("dwpbnms", "debug_pubnames") 532 .Case("dwpbtyp", "debug_pubtypes") 533 .Case("dwarnge", "debug_aranges") 534 .Case("dwabrev", "debug_abbrev") 535 .Case("dwstr", "debug_str") 536 .Case("dwrnges", "debug_ranges") 537 .Case("dwloc", "debug_loc") 538 .Case("dwframe", "debug_frame") 539 .Case("dwmac", "debug_macinfo") 540 .Default(Name); 541 } 542 543 size_t XCOFFObjectFile::getFileHeaderSize() const { 544 return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32); 545 } 546 547 size_t XCOFFObjectFile::getSectionHeaderSize() const { 548 return is64Bit() ? sizeof(XCOFFSectionHeader64) : 549 sizeof(XCOFFSectionHeader32); 550 } 551 552 bool XCOFFObjectFile::is64Bit() const { 553 return Binary::ID_XCOFF64 == getType(); 554 } 555 556 uint16_t XCOFFObjectFile::getMagic() const { 557 return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic; 558 } 559 560 Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const { 561 if (Num <= 0 || Num > getNumberOfSections()) 562 return errorCodeToError(object_error::invalid_section_index); 563 564 DataRefImpl DRI; 565 DRI.p = getWithOffset(getSectionHeaderTableAddress(), 566 getSectionHeaderSize() * (Num - 1)); 567 return DRI; 568 } 569 570 Expected<StringRef> 571 XCOFFObjectFile::getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const { 572 const int16_t SectionNum = SymEntPtr.getSectionNumber(); 573 574 switch (SectionNum) { 575 case XCOFF::N_DEBUG: 576 return "N_DEBUG"; 577 case XCOFF::N_ABS: 578 return "N_ABS"; 579 case XCOFF::N_UNDEF: 580 return "N_UNDEF"; 581 default: 582 Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum); 583 if (SecRef) 584 return generateXCOFFFixedNameStringRef( 585 getSectionNameInternal(SecRef.get())); 586 return SecRef.takeError(); 587 } 588 } 589 590 unsigned XCOFFObjectFile::getSymbolSectionID(SymbolRef Sym) const { 591 XCOFFSymbolRef XCOFFSymRef(Sym.getRawDataRefImpl(), this); 592 return XCOFFSymRef.getSectionNumber(); 593 } 594 595 bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) { 596 return (SectionNumber <= 0 && SectionNumber >= -2); 597 } 598 599 uint16_t XCOFFObjectFile::getNumberOfSections() const { 600 return is64Bit() ? fileHeader64()->NumberOfSections 601 : fileHeader32()->NumberOfSections; 602 } 603 604 int32_t XCOFFObjectFile::getTimeStamp() const { 605 return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp; 606 } 607 608 uint16_t XCOFFObjectFile::getOptionalHeaderSize() const { 609 return is64Bit() ? fileHeader64()->AuxHeaderSize 610 : fileHeader32()->AuxHeaderSize; 611 } 612 613 uint32_t XCOFFObjectFile::getSymbolTableOffset32() const { 614 return fileHeader32()->SymbolTableOffset; 615 } 616 617 int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const { 618 // As far as symbol table size is concerned, if this field is negative it is 619 // to be treated as a 0. However since this field is also used for printing we 620 // don't want to truncate any negative values. 621 return fileHeader32()->NumberOfSymTableEntries; 622 } 623 624 uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const { 625 return (fileHeader32()->NumberOfSymTableEntries >= 0 626 ? fileHeader32()->NumberOfSymTableEntries 627 : 0); 628 } 629 630 uint64_t XCOFFObjectFile::getSymbolTableOffset64() const { 631 return fileHeader64()->SymbolTableOffset; 632 } 633 634 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const { 635 return fileHeader64()->NumberOfSymTableEntries; 636 } 637 638 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries() const { 639 return is64Bit() ? getNumberOfSymbolTableEntries64() 640 : getLogicalNumberOfSymbolTableEntries32(); 641 } 642 643 uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const { 644 const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries(); 645 return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr), 646 XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries); 647 } 648 649 void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const { 650 if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr)) 651 report_fatal_error("Symbol table entry is outside of symbol table."); 652 653 if (SymbolEntPtr >= getEndOfSymbolTableAddress()) 654 report_fatal_error("Symbol table entry is outside of symbol table."); 655 656 ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) - 657 reinterpret_cast<const char *>(SymbolTblPtr); 658 659 if (Offset % XCOFF::SymbolTableEntrySize != 0) 660 report_fatal_error( 661 "Symbol table entry position is not valid inside of symbol table."); 662 } 663 664 uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const { 665 return (reinterpret_cast<const char *>(SymbolEntPtr) - 666 reinterpret_cast<const char *>(SymbolTblPtr)) / 667 XCOFF::SymbolTableEntrySize; 668 } 669 670 uintptr_t XCOFFObjectFile::getSymbolEntryAddressByIndex(uint32_t Index) const { 671 return getAdvancedSymbolEntryAddress( 672 reinterpret_cast<uintptr_t>(getPointerToSymbolTable()), Index); 673 } 674 675 Expected<StringRef> 676 XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const { 677 const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries(); 678 679 if (Index >= NumberOfSymTableEntries) 680 return errorCodeToError(object_error::invalid_symbol_index); 681 682 DataRefImpl SymDRI; 683 SymDRI.p = getSymbolEntryAddressByIndex(Index); 684 return getSymbolName(SymDRI); 685 } 686 687 uint16_t XCOFFObjectFile::getFlags() const { 688 return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags; 689 } 690 691 const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const { 692 return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name; 693 } 694 695 uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const { 696 return reinterpret_cast<uintptr_t>(SectionHeaderTable); 697 } 698 699 int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const { 700 return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags; 701 } 702 703 XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object) 704 : ObjectFile(Type, Object) { 705 assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64); 706 } 707 708 ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const { 709 assert(is64Bit() && "64-bit interface called for non 64-bit file."); 710 const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64(); 711 return ArrayRef<XCOFFSectionHeader64>(TablePtr, 712 TablePtr + getNumberOfSections()); 713 } 714 715 ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const { 716 assert(!is64Bit() && "32-bit interface called for non 32-bit file."); 717 const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32(); 718 return ArrayRef<XCOFFSectionHeader32>(TablePtr, 719 TablePtr + getNumberOfSections()); 720 } 721 722 // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO 723 // section header contains the actual count of relocation entries in the s_paddr 724 // field. STYP_OVRFLO headers contain the section index of their corresponding 725 // sections as their raw "NumberOfRelocations" field value. 726 template <typename T> 727 Expected<uint32_t> XCOFFObjectFile::getNumberOfRelocationEntries( 728 const XCOFFSectionHeader<T> &Sec) const { 729 const T &Section = static_cast<const T &>(Sec); 730 if (is64Bit()) 731 return Section.NumberOfRelocations; 732 733 uint16_t SectionIndex = &Section - sectionHeaderTable<T>() + 1; 734 if (Section.NumberOfRelocations < XCOFF::RelocOverflow) 735 return Section.NumberOfRelocations; 736 for (const auto &Sec : sections32()) { 737 if (Sec.Flags == XCOFF::STYP_OVRFLO && 738 Sec.NumberOfRelocations == SectionIndex) 739 return Sec.PhysicalAddress; 740 } 741 return errorCodeToError(object_error::parse_failed); 742 } 743 744 template <typename Shdr, typename Reloc> 745 Expected<ArrayRef<Reloc>> XCOFFObjectFile::relocations(const Shdr &Sec) const { 746 uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader), 747 Sec.FileOffsetToRelocationInfo); 748 auto NumRelocEntriesOrErr = getNumberOfRelocationEntries(Sec); 749 if (Error E = NumRelocEntriesOrErr.takeError()) 750 return std::move(E); 751 752 uint32_t NumRelocEntries = NumRelocEntriesOrErr.get(); 753 static_assert((sizeof(Reloc) == XCOFF::RelocationSerializationSize64 || 754 sizeof(Reloc) == XCOFF::RelocationSerializationSize32), 755 "Relocation structure is incorrect"); 756 auto RelocationOrErr = 757 getObject<Reloc>(Data, reinterpret_cast<void *>(RelocAddr), 758 NumRelocEntries * sizeof(Reloc)); 759 if (Error E = RelocationOrErr.takeError()) 760 return std::move(E); 761 762 const Reloc *StartReloc = RelocationOrErr.get(); 763 764 return ArrayRef<Reloc>(StartReloc, StartReloc + NumRelocEntries); 765 } 766 767 Expected<XCOFFStringTable> 768 XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) { 769 // If there is a string table, then the buffer must contain at least 4 bytes 770 // for the string table's size. Not having a string table is not an error. 771 if (Error E = Binary::checkOffset( 772 Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) { 773 consumeError(std::move(E)); 774 return XCOFFStringTable{0, nullptr}; 775 } 776 777 // Read the size out of the buffer. 778 uint32_t Size = support::endian::read32be(Obj->base() + Offset); 779 780 // If the size is less then 4, then the string table is just a size and no 781 // string data. 782 if (Size <= 4) 783 return XCOFFStringTable{4, nullptr}; 784 785 auto StringTableOrErr = 786 getObject<char>(Obj->Data, Obj->base() + Offset, Size); 787 if (Error E = StringTableOrErr.takeError()) 788 return std::move(E); 789 790 const char *StringTablePtr = StringTableOrErr.get(); 791 if (StringTablePtr[Size - 1] != '\0') 792 return errorCodeToError(object_error::string_table_non_null_end); 793 794 return XCOFFStringTable{Size, StringTablePtr}; 795 } 796 797 Expected<std::unique_ptr<XCOFFObjectFile>> 798 XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) { 799 // Can't use std::make_unique because of the private constructor. 800 std::unique_ptr<XCOFFObjectFile> Obj; 801 Obj.reset(new XCOFFObjectFile(Type, MBR)); 802 803 uint64_t CurOffset = 0; 804 const auto *Base = Obj->base(); 805 MemoryBufferRef Data = Obj->Data; 806 807 // Parse file header. 808 auto FileHeaderOrErr = 809 getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize()); 810 if (Error E = FileHeaderOrErr.takeError()) 811 return std::move(E); 812 Obj->FileHeader = FileHeaderOrErr.get(); 813 814 CurOffset += Obj->getFileHeaderSize(); 815 // TODO FIXME we don't have support for an optional header yet, so just skip 816 // past it. 817 CurOffset += Obj->getOptionalHeaderSize(); 818 819 // Parse the section header table if it is present. 820 if (Obj->getNumberOfSections()) { 821 auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset, 822 Obj->getNumberOfSections() * 823 Obj->getSectionHeaderSize()); 824 if (Error E = SecHeadersOrErr.takeError()) 825 return std::move(E); 826 Obj->SectionHeaderTable = SecHeadersOrErr.get(); 827 } 828 829 const uint32_t NumberOfSymbolTableEntries = 830 Obj->getNumberOfSymbolTableEntries(); 831 832 // If there is no symbol table we are done parsing the memory buffer. 833 if (NumberOfSymbolTableEntries == 0) 834 return std::move(Obj); 835 836 // Parse symbol table. 837 CurOffset = Obj->is64Bit() ? Obj->getSymbolTableOffset64() 838 : Obj->getSymbolTableOffset32(); 839 const uint64_t SymbolTableSize = 840 static_cast<uint64_t>(XCOFF::SymbolTableEntrySize) * 841 NumberOfSymbolTableEntries; 842 auto SymTableOrErr = 843 getObject<void *>(Data, Base + CurOffset, SymbolTableSize); 844 if (Error E = SymTableOrErr.takeError()) 845 return std::move(E); 846 Obj->SymbolTblPtr = SymTableOrErr.get(); 847 CurOffset += SymbolTableSize; 848 849 // Parse String table. 850 Expected<XCOFFStringTable> StringTableOrErr = 851 parseStringTable(Obj.get(), CurOffset); 852 if (Error E = StringTableOrErr.takeError()) 853 return std::move(E); 854 Obj->StringTable = StringTableOrErr.get(); 855 856 return std::move(Obj); 857 } 858 859 Expected<std::unique_ptr<ObjectFile>> 860 ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef, 861 unsigned FileType) { 862 return XCOFFObjectFile::create(FileType, MemBufRef); 863 } 864 865 bool XCOFFSymbolRef::isFunction() const { 866 if (!isCsectSymbol()) 867 return false; 868 869 if (getSymbolType() & FunctionSym) 870 return true; 871 872 Expected<XCOFFCsectAuxRef> ExpCsectAuxEnt = getXCOFFCsectAuxRef(); 873 if (!ExpCsectAuxEnt) 874 return false; 875 876 const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get(); 877 878 // A function definition should be a label definition. 879 // FIXME: This is not necessarily the case when -ffunction-sections is 880 // enabled. 881 if (!CsectAuxRef.isLabel()) 882 return false; 883 884 if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR) 885 return false; 886 887 const int16_t SectNum = getSectionNumber(); 888 Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum); 889 if (!SI) { 890 // If we could not get the section, then this symbol should not be 891 // a function. So consume the error and return `false` to move on. 892 consumeError(SI.takeError()); 893 return false; 894 } 895 896 return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT); 897 } 898 899 bool XCOFFSymbolRef::isCsectSymbol() const { 900 XCOFF::StorageClass SC = getStorageClass(); 901 return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT || 902 SC == XCOFF::C_HIDEXT); 903 } 904 905 Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const { 906 assert(isCsectSymbol() && 907 "Calling csect symbol interface with a non-csect symbol."); 908 909 uint8_t NumberOfAuxEntries = getNumberOfAuxEntries(); 910 911 Expected<StringRef> NameOrErr = getName(); 912 if (auto Err = NameOrErr.takeError()) 913 return std::move(Err); 914 915 if (!NumberOfAuxEntries) { 916 return createStringError(object_error::parse_failed, 917 "csect symbol \"" + *NameOrErr + 918 "\" contains no auxiliary entry"); 919 } 920 921 if (!OwningObjectPtr->is64Bit()) { 922 // In XCOFF32, the csect auxilliary entry is always the last auxiliary 923 // entry for the symbol. 924 uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress( 925 getEntryAddress(), NumberOfAuxEntries); 926 return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt32>(AuxAddr)); 927 } 928 929 // XCOFF64 uses SymbolAuxType to identify the auxiliary entry type. 930 // We need to iterate through all the auxiliary entries to find it. 931 for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) { 932 uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress( 933 getEntryAddress(), Index); 934 if (*OwningObjectPtr->getSymbolAuxType(AuxAddr) == 935 XCOFF::SymbolAuxType::AUX_CSECT) { 936 #ifndef NDEBUG 937 OwningObjectPtr->checkSymbolEntryPointer(AuxAddr); 938 #endif 939 return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(AuxAddr)); 940 } 941 } 942 943 return createStringError( 944 object_error::parse_failed, 945 "a csect auxiliary entry is not found for symbol \"" + *NameOrErr + "\""); 946 } 947 948 Expected<StringRef> XCOFFSymbolRef::getName() const { 949 // A storage class value with the high-order bit on indicates that the name is 950 // a symbolic debugger stabstring. 951 if (getStorageClass() & 0x80) 952 return StringRef("Unimplemented Debug Name"); 953 954 if (Entry32) { 955 if (Entry32->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC) 956 return generateXCOFFFixedNameStringRef(Entry32->SymbolName); 957 958 return OwningObjectPtr->getStringTableEntry(Entry32->NameInStrTbl.Offset); 959 } 960 961 return OwningObjectPtr->getStringTableEntry(Entry64->Offset); 962 } 963 964 // Explictly instantiate template classes. 965 template struct XCOFFSectionHeader<XCOFFSectionHeader32>; 966 template struct XCOFFSectionHeader<XCOFFSectionHeader64>; 967 968 template struct XCOFFRelocation<llvm::support::ubig32_t>; 969 template struct XCOFFRelocation<llvm::support::ubig64_t>; 970 971 template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation64>> 972 llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader64, 973 llvm::object::XCOFFRelocation64>( 974 llvm::object::XCOFFSectionHeader64 const &) const; 975 template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation32>> 976 llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader32, 977 llvm::object::XCOFFRelocation32>( 978 llvm::object::XCOFFSectionHeader32 const &) const; 979 980 bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) { 981 if (Bytes.size() < 4) 982 return false; 983 984 return support::endian::read32be(Bytes.data()) == 0; 985 } 986 987 #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X)) 988 #define GETVALUEWITHMASKSHIFT(X, S) \ 989 ((Data & (TracebackTable::X)) >> (TracebackTable::S)) 990 991 Expected<TBVectorExt> TBVectorExt::create(StringRef TBvectorStrRef) { 992 Error Err = Error::success(); 993 TBVectorExt TBTVecExt(TBvectorStrRef, Err); 994 if (Err) 995 return std::move(Err); 996 return TBTVecExt; 997 } 998 999 TBVectorExt::TBVectorExt(StringRef TBvectorStrRef, Error &Err) { 1000 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data()); 1001 Data = support::endian::read16be(Ptr); 1002 uint32_t VecParmsTypeValue = support::endian::read32be(Ptr + 2); 1003 unsigned ParmsNum = 1004 GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, NumberOfVectorParmsShift); 1005 1006 ErrorAsOutParameter EAO(&Err); 1007 Expected<SmallString<32>> VecParmsTypeOrError = 1008 parseVectorParmsType(VecParmsTypeValue, ParmsNum); 1009 if (!VecParmsTypeOrError) 1010 Err = VecParmsTypeOrError.takeError(); 1011 else 1012 VecParmsInfo = VecParmsTypeOrError.get(); 1013 } 1014 1015 uint8_t TBVectorExt::getNumberOfVRSaved() const { 1016 return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift); 1017 } 1018 1019 bool TBVectorExt::isVRSavedOnStack() const { 1020 return GETVALUEWITHMASK(IsVRSavedOnStackMask); 1021 } 1022 1023 bool TBVectorExt::hasVarArgs() const { 1024 return GETVALUEWITHMASK(HasVarArgsMask); 1025 } 1026 1027 uint8_t TBVectorExt::getNumberOfVectorParms() const { 1028 return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, 1029 NumberOfVectorParmsShift); 1030 } 1031 1032 bool TBVectorExt::hasVMXInstruction() const { 1033 return GETVALUEWITHMASK(HasVMXInstructionMask); 1034 } 1035 #undef GETVALUEWITHMASK 1036 #undef GETVALUEWITHMASKSHIFT 1037 1038 Expected<XCOFFTracebackTable> XCOFFTracebackTable::create(const uint8_t *Ptr, 1039 uint64_t &Size) { 1040 Error Err = Error::success(); 1041 XCOFFTracebackTable TBT(Ptr, Size, Err); 1042 if (Err) 1043 return std::move(Err); 1044 return TBT; 1045 } 1046 1047 XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size, 1048 Error &Err) 1049 : TBPtr(Ptr) { 1050 ErrorAsOutParameter EAO(&Err); 1051 DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false, 1052 /*AddressSize=*/0); 1053 DataExtractor::Cursor Cur(/*Offset=*/0); 1054 1055 // Skip 8 bytes of mandatory fields. 1056 DE.getU64(Cur); 1057 1058 unsigned FixedParmsNum = getNumberOfFixedParms(); 1059 unsigned FloatingParmsNum = getNumberOfFPParms(); 1060 uint32_t ParamsTypeValue = 0; 1061 1062 // Begin to parse optional fields. 1063 if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) 1064 ParamsTypeValue = DE.getU32(Cur); 1065 1066 if (Cur && hasTraceBackTableOffset()) 1067 TraceBackTableOffset = DE.getU32(Cur); 1068 1069 if (Cur && isInterruptHandler()) 1070 HandlerMask = DE.getU32(Cur); 1071 1072 if (Cur && hasControlledStorage()) { 1073 NumOfCtlAnchors = DE.getU32(Cur); 1074 if (Cur && NumOfCtlAnchors) { 1075 SmallVector<uint32_t, 8> Disp; 1076 Disp.reserve(NumOfCtlAnchors.getValue()); 1077 for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I) 1078 Disp.push_back(DE.getU32(Cur)); 1079 if (Cur) 1080 ControlledStorageInfoDisp = std::move(Disp); 1081 } 1082 } 1083 1084 if (Cur && isFuncNamePresent()) { 1085 uint16_t FunctionNameLen = DE.getU16(Cur); 1086 if (Cur) 1087 FunctionName = DE.getBytes(Cur, FunctionNameLen); 1088 } 1089 1090 if (Cur && isAllocaUsed()) 1091 AllocaRegister = DE.getU8(Cur); 1092 1093 unsigned VectorParmsNum = 0; 1094 if (Cur && hasVectorInfo()) { 1095 StringRef VectorExtRef = DE.getBytes(Cur, 6); 1096 if (Cur) { 1097 Expected<TBVectorExt> TBVecExtOrErr = TBVectorExt::create(VectorExtRef); 1098 if (!TBVecExtOrErr) { 1099 Err = TBVecExtOrErr.takeError(); 1100 return; 1101 } 1102 VecExt = TBVecExtOrErr.get(); 1103 VectorParmsNum = VecExt.getValue().getNumberOfVectorParms(); 1104 } 1105 } 1106 1107 // As long as there is no fixed-point or floating-point parameter, this 1108 // field remains not present even when hasVectorInfo gives true and 1109 // indicates the presence of vector parameters. 1110 if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) { 1111 Expected<SmallString<32>> ParmsTypeOrError = 1112 hasVectorInfo() 1113 ? parseParmsTypeWithVecInfo(ParamsTypeValue, FixedParmsNum, 1114 FloatingParmsNum, VectorParmsNum) 1115 : parseParmsType(ParamsTypeValue, FixedParmsNum, FloatingParmsNum); 1116 1117 if (!ParmsTypeOrError) { 1118 Err = ParmsTypeOrError.takeError(); 1119 return; 1120 } 1121 ParmsType = ParmsTypeOrError.get(); 1122 } 1123 1124 if (Cur && hasExtensionTable()) 1125 ExtensionTable = DE.getU8(Cur); 1126 1127 if (!Cur) 1128 Err = Cur.takeError(); 1129 1130 Size = Cur.tell(); 1131 } 1132 1133 #define GETBITWITHMASK(P, X) \ 1134 (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) 1135 #define GETBITWITHMASKSHIFT(P, X, S) \ 1136 ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >> \ 1137 (TracebackTable::S)) 1138 1139 uint8_t XCOFFTracebackTable::getVersion() const { 1140 return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift); 1141 } 1142 1143 uint8_t XCOFFTracebackTable::getLanguageID() const { 1144 return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift); 1145 } 1146 1147 bool XCOFFTracebackTable::isGlobalLinkage() const { 1148 return GETBITWITHMASK(0, IsGlobaLinkageMask); 1149 } 1150 1151 bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const { 1152 return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask); 1153 } 1154 1155 bool XCOFFTracebackTable::hasTraceBackTableOffset() const { 1156 return GETBITWITHMASK(0, HasTraceBackTableOffsetMask); 1157 } 1158 1159 bool XCOFFTracebackTable::isInternalProcedure() const { 1160 return GETBITWITHMASK(0, IsInternalProcedureMask); 1161 } 1162 1163 bool XCOFFTracebackTable::hasControlledStorage() const { 1164 return GETBITWITHMASK(0, HasControlledStorageMask); 1165 } 1166 1167 bool XCOFFTracebackTable::isTOCless() const { 1168 return GETBITWITHMASK(0, IsTOClessMask); 1169 } 1170 1171 bool XCOFFTracebackTable::isFloatingPointPresent() const { 1172 return GETBITWITHMASK(0, IsFloatingPointPresentMask); 1173 } 1174 1175 bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const { 1176 return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask); 1177 } 1178 1179 bool XCOFFTracebackTable::isInterruptHandler() const { 1180 return GETBITWITHMASK(0, IsInterruptHandlerMask); 1181 } 1182 1183 bool XCOFFTracebackTable::isFuncNamePresent() const { 1184 return GETBITWITHMASK(0, IsFunctionNamePresentMask); 1185 } 1186 1187 bool XCOFFTracebackTable::isAllocaUsed() const { 1188 return GETBITWITHMASK(0, IsAllocaUsedMask); 1189 } 1190 1191 uint8_t XCOFFTracebackTable::getOnConditionDirective() const { 1192 return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask, 1193 OnConditionDirectiveShift); 1194 } 1195 1196 bool XCOFFTracebackTable::isCRSaved() const { 1197 return GETBITWITHMASK(0, IsCRSavedMask); 1198 } 1199 1200 bool XCOFFTracebackTable::isLRSaved() const { 1201 return GETBITWITHMASK(0, IsLRSavedMask); 1202 } 1203 1204 bool XCOFFTracebackTable::isBackChainStored() const { 1205 return GETBITWITHMASK(4, IsBackChainStoredMask); 1206 } 1207 1208 bool XCOFFTracebackTable::isFixup() const { 1209 return GETBITWITHMASK(4, IsFixupMask); 1210 } 1211 1212 uint8_t XCOFFTracebackTable::getNumOfFPRsSaved() const { 1213 return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift); 1214 } 1215 1216 bool XCOFFTracebackTable::hasExtensionTable() const { 1217 return GETBITWITHMASK(4, HasExtensionTableMask); 1218 } 1219 1220 bool XCOFFTracebackTable::hasVectorInfo() const { 1221 return GETBITWITHMASK(4, HasVectorInfoMask); 1222 } 1223 1224 uint8_t XCOFFTracebackTable::getNumOfGPRsSaved() const { 1225 return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift); 1226 } 1227 1228 uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const { 1229 return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask, 1230 NumberOfFixedParmsShift); 1231 } 1232 1233 uint8_t XCOFFTracebackTable::getNumberOfFPParms() const { 1234 return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask, 1235 NumberOfFloatingPointParmsShift); 1236 } 1237 1238 bool XCOFFTracebackTable::hasParmsOnStack() const { 1239 return GETBITWITHMASK(4, HasParmsOnStackMask); 1240 } 1241 1242 #undef GETBITWITHMASK 1243 #undef GETBITWITHMASKSHIFT 1244 } // namespace object 1245 } // namespace llvm 1246