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