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/MC/SubtargetFeature.h"
15 #include "llvm/Support/DataExtractor.h"
16 #include <cstddef>
17 #include <cstring>
18
19 namespace llvm {
20
21 using namespace XCOFF;
22
23 namespace object {
24
25 static const uint8_t FunctionSym = 0x20;
26 static const uint8_t SymTypeMask = 0x07;
27 static const uint16_t NoRelMask = 0x0001;
28
29 // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
30 // 'M'. Returns a pointer to the underlying object on success.
31 template <typename T>
getObject(MemoryBufferRef M,const void * Ptr,const uint64_t Size=sizeof (T))32 static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
33 const uint64_t Size = sizeof(T)) {
34 uintptr_t Addr = uintptr_t(Ptr);
35 if (Error E = Binary::checkOffset(M, Addr, Size))
36 return std::move(E);
37 return reinterpret_cast<const T *>(Addr);
38 }
39
getWithOffset(uintptr_t Base,ptrdiff_t Offset)40 static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
41 return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
42 Offset);
43 }
44
viewAs(uintptr_t in)45 template <typename T> static const T *viewAs(uintptr_t in) {
46 return reinterpret_cast<const T *>(in);
47 }
48
generateXCOFFFixedNameStringRef(const char * Name)49 static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
50 auto NulCharPtr =
51 static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
52 return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
53 : StringRef(Name, XCOFF::NameSize);
54 }
55
getName() const56 template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
57 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
58 return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
59 }
60
getSectionType() const61 template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
62 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
63 return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
64 }
65
66 template <typename T>
isReservedSectionType() const67 bool XCOFFSectionHeader<T>::isReservedSectionType() const {
68 return getSectionType() & SectionFlagsReservedMask;
69 }
70
isRelocationSigned() const71 bool XCOFFRelocation32::isRelocationSigned() const {
72 return Info & XR_SIGN_INDICATOR_MASK;
73 }
74
isFixupIndicated() const75 bool XCOFFRelocation32::isFixupIndicated() const {
76 return Info & XR_FIXUP_INDICATOR_MASK;
77 }
78
getRelocatedLength() const79 uint8_t XCOFFRelocation32::getRelocatedLength() const {
80 // The relocation encodes the bit length being relocated minus 1. Add back
81 // the 1 to get the actual length being relocated.
82 return (Info & XR_BIASED_LENGTH_MASK) + 1;
83 }
84
checkSectionAddress(uintptr_t Addr,uintptr_t TableAddress) const85 void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
86 uintptr_t TableAddress) const {
87 if (Addr < TableAddress)
88 report_fatal_error("Section header outside of section header table.");
89
90 uintptr_t Offset = Addr - TableAddress;
91 if (Offset >= getSectionHeaderSize() * getNumberOfSections())
92 report_fatal_error("Section header outside of section header table.");
93
94 if (Offset % getSectionHeaderSize() != 0)
95 report_fatal_error(
96 "Section header pointer does not point to a valid section header.");
97 }
98
99 const XCOFFSectionHeader32 *
toSection32(DataRefImpl Ref) const100 XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
101 assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
102 #ifndef NDEBUG
103 checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
104 #endif
105 return viewAs<XCOFFSectionHeader32>(Ref.p);
106 }
107
108 const XCOFFSectionHeader64 *
toSection64(DataRefImpl Ref) const109 XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
110 assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
111 #ifndef NDEBUG
112 checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
113 #endif
114 return viewAs<XCOFFSectionHeader64>(Ref.p);
115 }
116
toSymbolEntry(DataRefImpl Ref) const117 const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const {
118 assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
119 assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
120 #ifndef NDEBUG
121 checkSymbolEntryPointer(Ref.p);
122 #endif
123 auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p);
124 return SymEntPtr;
125 }
126
fileHeader32() const127 const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
128 assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
129 return static_cast<const XCOFFFileHeader32 *>(FileHeader);
130 }
131
fileHeader64() const132 const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
133 assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
134 return static_cast<const XCOFFFileHeader64 *>(FileHeader);
135 }
136
137 const XCOFFSectionHeader32 *
sectionHeaderTable32() const138 XCOFFObjectFile::sectionHeaderTable32() const {
139 assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
140 return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
141 }
142
143 const XCOFFSectionHeader64 *
sectionHeaderTable64() const144 XCOFFObjectFile::sectionHeaderTable64() const {
145 assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
146 return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
147 }
148
moveSymbolNext(DataRefImpl & Symb) const149 void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
150 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
151 SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1;
152 #ifndef NDEBUG
153 // This function is used by basic_symbol_iterator, which allows to
154 // point to the end-of-symbol-table address.
155 if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress())
156 checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr));
157 #endif
158 Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr);
159 }
160
161 Expected<StringRef>
getStringTableEntry(uint32_t Offset) const162 XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
163 // The byte offset is relative to the start of the string table.
164 // A byte offset value of 0 is a null or zero-length symbol
165 // name. A byte offset in the range 1 to 3 (inclusive) points into the length
166 // field; as a soft-error recovery mechanism, we treat such cases as having an
167 // offset of 0.
168 if (Offset < 4)
169 return StringRef(nullptr, 0);
170
171 if (StringTable.Data != nullptr && StringTable.Size > Offset)
172 return (StringTable.Data + Offset);
173
174 return make_error<GenericBinaryError>("Bad offset for string table entry",
175 object_error::parse_failed);
176 }
177
178 Expected<StringRef>
getCFileName(const XCOFFFileAuxEnt * CFileEntPtr) const179 XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
180 if (CFileEntPtr->NameInStrTbl.Magic !=
181 XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
182 return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
183 return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
184 }
185
getSymbolName(DataRefImpl Symb) const186 Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
187 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
188
189 // A storage class value with the high-order bit on indicates that the name is
190 // a symbolic debugger stabstring.
191 if (SymEntPtr->StorageClass & 0x80)
192 return StringRef("Unimplemented Debug Name");
193
194 if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
195 return generateXCOFFFixedNameStringRef(SymEntPtr->SymbolName);
196
197 return getStringTableEntry(SymEntPtr->NameInStrTbl.Offset);
198 }
199
getSymbolAddress(DataRefImpl Symb) const200 Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
201 assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
202 return toSymbolEntry(Symb)->Value;
203 }
204
getSymbolValueImpl(DataRefImpl Symb) const205 uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
206 assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
207 return toSymbolEntry(Symb)->Value;
208 }
209
getCommonSymbolSizeImpl(DataRefImpl Symb) const210 uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
211 uint64_t Result = 0;
212 llvm_unreachable("Not yet implemented!");
213 return Result;
214 }
215
216 Expected<SymbolRef::Type>
getSymbolType(DataRefImpl Symb) const217 XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
218 llvm_unreachable("Not yet implemented!");
219 return SymbolRef::ST_Other;
220 }
221
222 Expected<section_iterator>
getSymbolSection(DataRefImpl Symb) const223 XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
224 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
225 int16_t SectNum = SymEntPtr->SectionNumber;
226
227 if (isReservedSectionNumber(SectNum))
228 return section_end();
229
230 Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
231 if (!ExpSec)
232 return ExpSec.takeError();
233
234 return section_iterator(SectionRef(ExpSec.get(), this));
235 }
236
moveSectionNext(DataRefImpl & Sec) const237 void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
238 const char *Ptr = reinterpret_cast<const char *>(Sec.p);
239 Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
240 }
241
getSectionName(DataRefImpl Sec) const242 Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
243 return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
244 }
245
getSectionAddress(DataRefImpl Sec) const246 uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
247 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
248 // with MSVC.
249 if (is64Bit())
250 return toSection64(Sec)->VirtualAddress;
251
252 return toSection32(Sec)->VirtualAddress;
253 }
254
getSectionIndex(DataRefImpl Sec) const255 uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
256 // Section numbers in XCOFF are numbered beginning at 1. A section number of
257 // zero is used to indicate that a symbol is being imported or is undefined.
258 if (is64Bit())
259 return toSection64(Sec) - sectionHeaderTable64() + 1;
260 else
261 return toSection32(Sec) - sectionHeaderTable32() + 1;
262 }
263
getSectionSize(DataRefImpl Sec) const264 uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
265 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
266 // with MSVC.
267 if (is64Bit())
268 return toSection64(Sec)->SectionSize;
269
270 return toSection32(Sec)->SectionSize;
271 }
272
273 Expected<ArrayRef<uint8_t>>
getSectionContents(DataRefImpl Sec) const274 XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
275 if (isSectionVirtual(Sec))
276 return ArrayRef<uint8_t>();
277
278 uint64_t OffsetToRaw;
279 if (is64Bit())
280 OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
281 else
282 OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
283
284 const uint8_t * ContentStart = base() + OffsetToRaw;
285 uint64_t SectionSize = getSectionSize(Sec);
286 if (checkOffset(Data, uintptr_t(ContentStart), SectionSize))
287 return make_error<BinaryError>();
288
289 return makeArrayRef(ContentStart,SectionSize);
290 }
291
getSectionAlignment(DataRefImpl Sec) const292 uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
293 uint64_t Result = 0;
294 llvm_unreachable("Not yet implemented!");
295 return Result;
296 }
297
isSectionCompressed(DataRefImpl Sec) const298 bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
299 bool Result = false;
300 llvm_unreachable("Not yet implemented!");
301 return Result;
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
isSectionVirtual(DataRefImpl Sec) const318 bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
319 return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
320 : toSection32(Sec)->FileOffsetToRawData == 0;
321 }
322
section_rel_begin(DataRefImpl Sec) const323 relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
324 if (is64Bit())
325 report_fatal_error("64-bit support not implemented yet");
326 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
327 auto RelocationsOrErr = relocations(*SectionEntPtr);
328 if (Error E = RelocationsOrErr.takeError())
329 return relocation_iterator(RelocationRef());
330 DataRefImpl Ret;
331 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
332 return relocation_iterator(RelocationRef(Ret, this));
333 }
334
section_rel_end(DataRefImpl Sec) const335 relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
336 if (is64Bit())
337 report_fatal_error("64-bit support not implemented yet");
338 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
339 auto RelocationsOrErr = relocations(*SectionEntPtr);
340 if (Error E = RelocationsOrErr.takeError())
341 return relocation_iterator(RelocationRef());
342 DataRefImpl Ret;
343 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
344 return relocation_iterator(RelocationRef(Ret, this));
345 }
346
moveRelocationNext(DataRefImpl & Rel) const347 void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
348 Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
349 }
350
getRelocationOffset(DataRefImpl Rel) const351 uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
352 if (is64Bit())
353 report_fatal_error("64-bit support not implemented yet");
354 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
355 const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
356 const uint32_t RelocAddress = Reloc->VirtualAddress;
357 const uint16_t NumberOfSections = getNumberOfSections();
358 for (uint16_t i = 0; i < NumberOfSections; ++i) {
359 // Find which section this relocation is belonging to, and get the
360 // relocation offset relative to the start of the section.
361 if (Sec32->VirtualAddress <= RelocAddress &&
362 RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
363 return RelocAddress - Sec32->VirtualAddress;
364 }
365 ++Sec32;
366 }
367 return InvalidRelocOffset;
368 }
369
getRelocationSymbol(DataRefImpl Rel) const370 symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
371 if (is64Bit())
372 report_fatal_error("64-bit support not implemented yet");
373 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
374 const uint32_t Index = Reloc->SymbolIndex;
375
376 if (Index >= getLogicalNumberOfSymbolTableEntries32())
377 return symbol_end();
378
379 DataRefImpl SymDRI;
380 SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
381 return symbol_iterator(SymbolRef(SymDRI, this));
382 }
383
getRelocationType(DataRefImpl Rel) const384 uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
385 if (is64Bit())
386 report_fatal_error("64-bit support not implemented yet");
387 return viewAs<XCOFFRelocation32>(Rel.p)->Type;
388 }
389
getRelocationTypeName(DataRefImpl Rel,SmallVectorImpl<char> & Result) const390 void XCOFFObjectFile::getRelocationTypeName(
391 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
392 if (is64Bit())
393 report_fatal_error("64-bit support not implemented yet");
394 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
395 StringRef Res = XCOFF::getRelocationTypeString(Reloc->Type);
396 Result.append(Res.begin(), Res.end());
397 }
398
getSymbolFlags(DataRefImpl Symb) const399 Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
400 uint32_t Result = 0;
401 llvm_unreachable("Not yet implemented!");
402 return Result;
403 }
404
symbol_begin() const405 basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
406 if (is64Bit())
407 report_fatal_error("64-bit support not implemented yet");
408 DataRefImpl SymDRI;
409 SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
410 return basic_symbol_iterator(SymbolRef(SymDRI, this));
411 }
412
symbol_end() const413 basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
414 if (is64Bit())
415 report_fatal_error("64-bit support not implemented yet");
416 DataRefImpl SymDRI;
417 SymDRI.p = reinterpret_cast<uintptr_t>(
418 SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32());
419 return basic_symbol_iterator(SymbolRef(SymDRI, this));
420 }
421
section_begin() const422 section_iterator XCOFFObjectFile::section_begin() const {
423 DataRefImpl DRI;
424 DRI.p = getSectionHeaderTableAddress();
425 return section_iterator(SectionRef(DRI, this));
426 }
427
section_end() const428 section_iterator XCOFFObjectFile::section_end() const {
429 DataRefImpl DRI;
430 DRI.p = getWithOffset(getSectionHeaderTableAddress(),
431 getNumberOfSections() * getSectionHeaderSize());
432 return section_iterator(SectionRef(DRI, this));
433 }
434
getBytesInAddress() const435 uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
436
getFileFormatName() const437 StringRef XCOFFObjectFile::getFileFormatName() const {
438 return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
439 }
440
getArch() const441 Triple::ArchType XCOFFObjectFile::getArch() const {
442 return is64Bit() ? Triple::ppc64 : Triple::ppc;
443 }
444
getFeatures() const445 SubtargetFeatures XCOFFObjectFile::getFeatures() const {
446 return SubtargetFeatures();
447 }
448
isRelocatableObject() const449 bool XCOFFObjectFile::isRelocatableObject() const {
450 if (is64Bit())
451 report_fatal_error("64-bit support not implemented yet");
452 return !(fileHeader32()->Flags & NoRelMask);
453 }
454
getStartAddress() const455 Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
456 // TODO FIXME Should get from auxiliary_header->o_entry when support for the
457 // auxiliary_header is added.
458 return 0;
459 }
460
getFileHeaderSize() const461 size_t XCOFFObjectFile::getFileHeaderSize() const {
462 return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
463 }
464
getSectionHeaderSize() const465 size_t XCOFFObjectFile::getSectionHeaderSize() const {
466 return is64Bit() ? sizeof(XCOFFSectionHeader64) :
467 sizeof(XCOFFSectionHeader32);
468 }
469
is64Bit() const470 bool XCOFFObjectFile::is64Bit() const {
471 return Binary::ID_XCOFF64 == getType();
472 }
473
getMagic() const474 uint16_t XCOFFObjectFile::getMagic() const {
475 return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
476 }
477
getSectionByNum(int16_t Num) const478 Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
479 if (Num <= 0 || Num > getNumberOfSections())
480 return errorCodeToError(object_error::invalid_section_index);
481
482 DataRefImpl DRI;
483 DRI.p = getWithOffset(getSectionHeaderTableAddress(),
484 getSectionHeaderSize() * (Num - 1));
485 return DRI;
486 }
487
488 Expected<StringRef>
getSymbolSectionName(const XCOFFSymbolEntry * SymEntPtr) const489 XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const {
490 assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
491 int16_t SectionNum = SymEntPtr->SectionNumber;
492
493 switch (SectionNum) {
494 case XCOFF::N_DEBUG:
495 return "N_DEBUG";
496 case XCOFF::N_ABS:
497 return "N_ABS";
498 case XCOFF::N_UNDEF:
499 return "N_UNDEF";
500 default:
501 Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
502 if (SecRef)
503 return generateXCOFFFixedNameStringRef(
504 getSectionNameInternal(SecRef.get()));
505 return SecRef.takeError();
506 }
507 }
508
isReservedSectionNumber(int16_t SectionNumber)509 bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
510 return (SectionNumber <= 0 && SectionNumber >= -2);
511 }
512
getNumberOfSections() const513 uint16_t XCOFFObjectFile::getNumberOfSections() const {
514 return is64Bit() ? fileHeader64()->NumberOfSections
515 : fileHeader32()->NumberOfSections;
516 }
517
getTimeStamp() const518 int32_t XCOFFObjectFile::getTimeStamp() const {
519 return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
520 }
521
getOptionalHeaderSize() const522 uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
523 return is64Bit() ? fileHeader64()->AuxHeaderSize
524 : fileHeader32()->AuxHeaderSize;
525 }
526
getSymbolTableOffset32() const527 uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
528 return fileHeader32()->SymbolTableOffset;
529 }
530
getRawNumberOfSymbolTableEntries32() const531 int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
532 // As far as symbol table size is concerned, if this field is negative it is
533 // to be treated as a 0. However since this field is also used for printing we
534 // don't want to truncate any negative values.
535 return fileHeader32()->NumberOfSymTableEntries;
536 }
537
getLogicalNumberOfSymbolTableEntries32() const538 uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
539 return (fileHeader32()->NumberOfSymTableEntries >= 0
540 ? fileHeader32()->NumberOfSymTableEntries
541 : 0);
542 }
543
getSymbolTableOffset64() const544 uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
545 return fileHeader64()->SymbolTableOffset;
546 }
547
getNumberOfSymbolTableEntries64() const548 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
549 return fileHeader64()->NumberOfSymTableEntries;
550 }
551
getEndOfSymbolTableAddress() const552 uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
553 uint32_t NumberOfSymTableEntries =
554 is64Bit() ? getNumberOfSymbolTableEntries64()
555 : getLogicalNumberOfSymbolTableEntries32();
556 return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
557 XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
558 }
559
checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const560 void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
561 if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
562 report_fatal_error("Symbol table entry is outside of symbol table.");
563
564 if (SymbolEntPtr >= getEndOfSymbolTableAddress())
565 report_fatal_error("Symbol table entry is outside of symbol table.");
566
567 ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
568 reinterpret_cast<const char *>(SymbolTblPtr);
569
570 if (Offset % XCOFF::SymbolTableEntrySize != 0)
571 report_fatal_error(
572 "Symbol table entry position is not valid inside of symbol table.");
573 }
574
getSymbolIndex(uintptr_t SymbolEntPtr) const575 uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
576 return (reinterpret_cast<const char *>(SymbolEntPtr) -
577 reinterpret_cast<const char *>(SymbolTblPtr)) /
578 XCOFF::SymbolTableEntrySize;
579 }
580
581 Expected<StringRef>
getSymbolNameByIndex(uint32_t Index) const582 XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
583 if (is64Bit())
584 report_fatal_error("64-bit symbol table support not implemented yet.");
585
586 if (Index >= getLogicalNumberOfSymbolTableEntries32())
587 return errorCodeToError(object_error::invalid_symbol_index);
588
589 DataRefImpl SymDRI;
590 SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
591 return getSymbolName(SymDRI);
592 }
593
getFlags() const594 uint16_t XCOFFObjectFile::getFlags() const {
595 return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
596 }
597
getSectionNameInternal(DataRefImpl Sec) const598 const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
599 return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
600 }
601
getSectionHeaderTableAddress() const602 uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
603 return reinterpret_cast<uintptr_t>(SectionHeaderTable);
604 }
605
getSectionFlags(DataRefImpl Sec) const606 int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
607 return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
608 }
609
XCOFFObjectFile(unsigned int Type,MemoryBufferRef Object)610 XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
611 : ObjectFile(Type, Object) {
612 assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
613 }
614
sections64() const615 ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
616 assert(is64Bit() && "64-bit interface called for non 64-bit file.");
617 const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
618 return ArrayRef<XCOFFSectionHeader64>(TablePtr,
619 TablePtr + getNumberOfSections());
620 }
621
sections32() const622 ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
623 assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
624 const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
625 return ArrayRef<XCOFFSectionHeader32>(TablePtr,
626 TablePtr + getNumberOfSections());
627 }
628
629 // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
630 // section header contains the actual count of relocation entries in the s_paddr
631 // field. STYP_OVRFLO headers contain the section index of their corresponding
632 // sections as their raw "NumberOfRelocations" field value.
getLogicalNumberOfRelocationEntries(const XCOFFSectionHeader32 & Sec) const633 Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries(
634 const XCOFFSectionHeader32 &Sec) const {
635
636 uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1;
637
638 if (Sec.NumberOfRelocations < XCOFF::RelocOverflow)
639 return Sec.NumberOfRelocations;
640 for (const auto &Sec : sections32()) {
641 if (Sec.Flags == XCOFF::STYP_OVRFLO &&
642 Sec.NumberOfRelocations == SectionIndex)
643 return Sec.PhysicalAddress;
644 }
645 return errorCodeToError(object_error::parse_failed);
646 }
647
648 Expected<ArrayRef<XCOFFRelocation32>>
relocations(const XCOFFSectionHeader32 & Sec) const649 XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const {
650 uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
651 Sec.FileOffsetToRelocationInfo);
652 auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec);
653 if (Error E = NumRelocEntriesOrErr.takeError())
654 return std::move(E);
655
656 uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
657
658 assert(sizeof(XCOFFRelocation32) == XCOFF::RelocationSerializationSize32);
659 auto RelocationOrErr =
660 getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr),
661 NumRelocEntries * sizeof(XCOFFRelocation32));
662 if (Error E = RelocationOrErr.takeError())
663 return std::move(E);
664
665 const XCOFFRelocation32 *StartReloc = RelocationOrErr.get();
666
667 return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries);
668 }
669
670 Expected<XCOFFStringTable>
parseStringTable(const XCOFFObjectFile * Obj,uint64_t Offset)671 XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
672 // If there is a string table, then the buffer must contain at least 4 bytes
673 // for the string table's size. Not having a string table is not an error.
674 if (Error E = Binary::checkOffset(
675 Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
676 consumeError(std::move(E));
677 return XCOFFStringTable{0, nullptr};
678 }
679
680 // Read the size out of the buffer.
681 uint32_t Size = support::endian::read32be(Obj->base() + Offset);
682
683 // If the size is less then 4, then the string table is just a size and no
684 // string data.
685 if (Size <= 4)
686 return XCOFFStringTable{4, nullptr};
687
688 auto StringTableOrErr =
689 getObject<char>(Obj->Data, Obj->base() + Offset, Size);
690 if (Error E = StringTableOrErr.takeError())
691 return std::move(E);
692
693 const char *StringTablePtr = StringTableOrErr.get();
694 if (StringTablePtr[Size - 1] != '\0')
695 return errorCodeToError(object_error::string_table_non_null_end);
696
697 return XCOFFStringTable{Size, StringTablePtr};
698 }
699
700 Expected<std::unique_ptr<XCOFFObjectFile>>
create(unsigned Type,MemoryBufferRef MBR)701 XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
702 // Can't use std::make_unique because of the private constructor.
703 std::unique_ptr<XCOFFObjectFile> Obj;
704 Obj.reset(new XCOFFObjectFile(Type, MBR));
705
706 uint64_t CurOffset = 0;
707 const auto *Base = Obj->base();
708 MemoryBufferRef Data = Obj->Data;
709
710 // Parse file header.
711 auto FileHeaderOrErr =
712 getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
713 if (Error E = FileHeaderOrErr.takeError())
714 return std::move(E);
715 Obj->FileHeader = FileHeaderOrErr.get();
716
717 CurOffset += Obj->getFileHeaderSize();
718 // TODO FIXME we don't have support for an optional header yet, so just skip
719 // past it.
720 CurOffset += Obj->getOptionalHeaderSize();
721
722 // Parse the section header table if it is present.
723 if (Obj->getNumberOfSections()) {
724 auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset,
725 Obj->getNumberOfSections() *
726 Obj->getSectionHeaderSize());
727 if (Error E = SecHeadersOrErr.takeError())
728 return std::move(E);
729 Obj->SectionHeaderTable = SecHeadersOrErr.get();
730 }
731
732 // 64-bit object supports only file header and section headers for now.
733 if (Obj->is64Bit())
734 return std::move(Obj);
735
736 // If there is no symbol table we are done parsing the memory buffer.
737 if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0)
738 return std::move(Obj);
739
740 // Parse symbol table.
741 CurOffset = Obj->fileHeader32()->SymbolTableOffset;
742 uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) *
743 Obj->getLogicalNumberOfSymbolTableEntries32();
744 auto SymTableOrErr =
745 getObject<XCOFFSymbolEntry>(Data, Base + CurOffset, SymbolTableSize);
746 if (Error E = SymTableOrErr.takeError())
747 return std::move(E);
748 Obj->SymbolTblPtr = SymTableOrErr.get();
749 CurOffset += SymbolTableSize;
750
751 // Parse String table.
752 Expected<XCOFFStringTable> StringTableOrErr =
753 parseStringTable(Obj.get(), CurOffset);
754 if (Error E = StringTableOrErr.takeError())
755 return std::move(E);
756 Obj->StringTable = StringTableOrErr.get();
757
758 return std::move(Obj);
759 }
760
761 Expected<std::unique_ptr<ObjectFile>>
createXCOFFObjectFile(MemoryBufferRef MemBufRef,unsigned FileType)762 ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
763 unsigned FileType) {
764 return XCOFFObjectFile::create(FileType, MemBufRef);
765 }
766
getStorageClass() const767 XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const {
768 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass;
769 }
770
getNumberOfAuxEntries() const771 uint8_t XCOFFSymbolRef::getNumberOfAuxEntries() const {
772 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries;
773 }
774
775 // TODO: The function needs to return an error if there is no csect auxiliary
776 // entry.
getXCOFFCsectAuxEnt32() const777 const XCOFFCsectAuxEnt32 *XCOFFSymbolRef::getXCOFFCsectAuxEnt32() const {
778 assert(!OwningObjectPtr->is64Bit() &&
779 "32-bit interface called on 64-bit object file.");
780 assert(hasCsectAuxEnt() && "No Csect Auxiliary Entry is found.");
781
782 // In XCOFF32, the csect auxilliary entry is always the last auxiliary
783 // entry for the symbol.
784 uintptr_t AuxAddr = getWithOffset(
785 SymEntDataRef.p, XCOFF::SymbolTableEntrySize * getNumberOfAuxEntries());
786
787 #ifndef NDEBUG
788 OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
789 #endif
790
791 return reinterpret_cast<const XCOFFCsectAuxEnt32 *>(AuxAddr);
792 }
793
getType() const794 uint16_t XCOFFSymbolRef::getType() const {
795 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType;
796 }
797
getSectionNumber() const798 int16_t XCOFFSymbolRef::getSectionNumber() const {
799 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber;
800 }
801
802 // TODO: The function name needs to be changed to express the purpose of the
803 // function.
hasCsectAuxEnt() const804 bool XCOFFSymbolRef::hasCsectAuxEnt() const {
805 XCOFF::StorageClass SC = getStorageClass();
806 return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
807 SC == XCOFF::C_HIDEXT);
808 }
809
isFunction() const810 bool XCOFFSymbolRef::isFunction() const {
811 if (OwningObjectPtr->is64Bit())
812 report_fatal_error("64-bit support is unimplemented yet.");
813
814 if (getType() & FunctionSym)
815 return true;
816
817 if (!hasCsectAuxEnt())
818 return false;
819
820 const XCOFFCsectAuxEnt32 *CsectAuxEnt = getXCOFFCsectAuxEnt32();
821
822 // A function definition should be a label definition.
823 if ((CsectAuxEnt->SymbolAlignmentAndType & SymTypeMask) != XCOFF::XTY_LD)
824 return false;
825
826 if (CsectAuxEnt->StorageMappingClass != XCOFF::XMC_PR)
827 return false;
828
829 int16_t SectNum = getSectionNumber();
830 Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum);
831 if (!SI)
832 return false;
833
834 return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
835 }
836
837 // Explictly instantiate template classes.
838 template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
839 template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
840
doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes)841 bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) {
842 if (Bytes.size() < 4)
843 return false;
844
845 return support::endian::read32be(Bytes.data()) == 0;
846 }
847
TBVectorExt(StringRef TBvectorStrRef)848 TBVectorExt::TBVectorExt(StringRef TBvectorStrRef) {
849 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data());
850 Data = support::endian::read16be(Ptr);
851 VecParmsInfo = support::endian::read32be(Ptr + 2);
852 }
853
854 #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X))
855 #define GETVALUEWITHMASKSHIFT(X, S) \
856 ((Data & (TracebackTable::X)) >> (TracebackTable::S))
getNumberOfVRSaved() const857 uint8_t TBVectorExt::getNumberOfVRSaved() const {
858 return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift);
859 }
860
isVRSavedOnStack() const861 bool TBVectorExt::isVRSavedOnStack() const {
862 return GETVALUEWITHMASK(IsVRSavedOnStackMask);
863 }
864
hasVarArgs() const865 bool TBVectorExt::hasVarArgs() const {
866 return GETVALUEWITHMASK(HasVarArgsMask);
867 }
getNumberOfVectorParms() const868 uint8_t TBVectorExt::getNumberOfVectorParms() const {
869 return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask,
870 NumberOfVectorParmsShift);
871 }
872
hasVMXInstruction() const873 bool TBVectorExt::hasVMXInstruction() const {
874 return GETVALUEWITHMASK(HasVMXInstructionMask);
875 }
876 #undef GETVALUEWITHMASK
877 #undef GETVALUEWITHMASKSHIFT
878
getVectorParmsInfoString() const879 SmallString<32> TBVectorExt::getVectorParmsInfoString() const {
880 SmallString<32> ParmsType;
881 uint32_t Value = VecParmsInfo;
882 for (uint8_t I = 0; I < getNumberOfVectorParms(); ++I) {
883 if (I != 0)
884 ParmsType += ", ";
885 switch (Value & TracebackTable::ParmTypeMask) {
886 case TracebackTable::ParmTypeIsVectorCharBit:
887 ParmsType += "vc";
888 break;
889
890 case TracebackTable::ParmTypeIsVectorShortBit:
891 ParmsType += "vs";
892 break;
893
894 case TracebackTable::ParmTypeIsVectorIntBit:
895 ParmsType += "vi";
896 break;
897
898 case TracebackTable::ParmTypeIsVectorFloatBit:
899 ParmsType += "vf";
900 break;
901 }
902 Value <<= 2;
903 }
904 return ParmsType;
905 }
906
parseParmsTypeWithVecInfo(uint32_t Value,unsigned int ParmsNum)907 static SmallString<32> parseParmsTypeWithVecInfo(uint32_t Value,
908 unsigned int ParmsNum) {
909 SmallString<32> ParmsType;
910 unsigned I = 0;
911 bool Begin = false;
912 while (I < ParmsNum || Value) {
913 if (Begin)
914 ParmsType += ", ";
915 else
916 Begin = true;
917
918 switch (Value & TracebackTable::ParmTypeMask) {
919 case TracebackTable::ParmTypeIsFixedBits:
920 ParmsType += "i";
921 ++I;
922 break;
923 case TracebackTable::ParmTypeIsVectorBits:
924 ParmsType += "v";
925 break;
926 case TracebackTable::ParmTypeIsFloatingBits:
927 ParmsType += "f";
928 ++I;
929 break;
930 case TracebackTable::ParmTypeIsDoubleBits:
931 ParmsType += "d";
932 ++I;
933 break;
934 default:
935 assert(false && "Unrecognized bits in ParmsType.");
936 }
937 Value <<= 2;
938 }
939 assert(I == ParmsNum &&
940 "The total parameters number of fixed-point or floating-point "
941 "parameters not equal to the number in the parameter type!");
942 return ParmsType;
943 }
944
parseParmsType(uint32_t Value,unsigned ParmsNum)945 static SmallString<32> parseParmsType(uint32_t Value, unsigned ParmsNum) {
946 SmallString<32> ParmsType;
947 for (unsigned I = 0; I < ParmsNum; ++I) {
948 if (I != 0)
949 ParmsType += ", ";
950 if ((Value & TracebackTable::ParmTypeIsFloatingBit) == 0) {
951 // Fixed parameter type.
952 ParmsType += "i";
953 Value <<= 1;
954 } else {
955 if ((Value & TracebackTable::ParmTypeFloatingIsDoubleBit) == 0)
956 // Float parameter type.
957 ParmsType += "f";
958 else
959 // Double parameter type.
960 ParmsType += "d";
961
962 Value <<= 2;
963 }
964 }
965 return ParmsType;
966 }
967
create(const uint8_t * Ptr,uint64_t & Size)968 Expected<XCOFFTracebackTable> XCOFFTracebackTable::create(const uint8_t *Ptr,
969 uint64_t &Size) {
970 Error Err = Error::success();
971 XCOFFTracebackTable TBT(Ptr, Size, Err);
972 if (Err)
973 return std::move(Err);
974 return TBT;
975 }
976
XCOFFTracebackTable(const uint8_t * Ptr,uint64_t & Size,Error & Err)977 XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size,
978 Error &Err)
979 : TBPtr(Ptr) {
980 ErrorAsOutParameter EAO(&Err);
981 DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false,
982 /*AddressSize=*/0);
983 DataExtractor::Cursor Cur(/*Offset=*/0);
984
985 // Skip 8 bytes of mandatory fields.
986 DE.getU64(Cur);
987
988 // Begin to parse optional fields.
989 if (Cur) {
990 unsigned ParmNum = getNumberOfFixedParms() + getNumberOfFPParms();
991
992 // As long as there are no "fixed-point" or floating-point parameters, this
993 // field remains not present even when hasVectorInfo gives true and
994 // indicates the presence of vector parameters.
995 if (ParmNum > 0) {
996 uint32_t ParamsTypeValue = DE.getU32(Cur);
997 if (Cur)
998 ParmsType = hasVectorInfo()
999 ? parseParmsTypeWithVecInfo(ParamsTypeValue, ParmNum)
1000 : parseParmsType(ParamsTypeValue, ParmNum);
1001 }
1002 }
1003
1004 if (Cur && hasTraceBackTableOffset())
1005 TraceBackTableOffset = DE.getU32(Cur);
1006
1007 if (Cur && isInterruptHandler())
1008 HandlerMask = DE.getU32(Cur);
1009
1010 if (Cur && hasControlledStorage()) {
1011 NumOfCtlAnchors = DE.getU32(Cur);
1012 if (Cur && NumOfCtlAnchors) {
1013 SmallVector<uint32_t, 8> Disp;
1014 Disp.reserve(NumOfCtlAnchors.getValue());
1015 for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I)
1016 Disp.push_back(DE.getU32(Cur));
1017 if (Cur)
1018 ControlledStorageInfoDisp = std::move(Disp);
1019 }
1020 }
1021
1022 if (Cur && isFuncNamePresent()) {
1023 uint16_t FunctionNameLen = DE.getU16(Cur);
1024 if (Cur)
1025 FunctionName = DE.getBytes(Cur, FunctionNameLen);
1026 }
1027
1028 if (Cur && isAllocaUsed())
1029 AllocaRegister = DE.getU8(Cur);
1030
1031 if (Cur && hasVectorInfo()) {
1032 StringRef VectorExtRef = DE.getBytes(Cur, 6);
1033 if (Cur)
1034 VecExt = TBVectorExt(VectorExtRef);
1035 }
1036
1037 if (Cur && hasExtensionTable())
1038 ExtensionTable = DE.getU8(Cur);
1039
1040 if (!Cur)
1041 Err = Cur.takeError();
1042 Size = Cur.tell();
1043 }
1044
1045 #define GETBITWITHMASK(P, X) \
1046 (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X))
1047 #define GETBITWITHMASKSHIFT(P, X, S) \
1048 ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >> \
1049 (TracebackTable::S))
1050
getVersion() const1051 uint8_t XCOFFTracebackTable::getVersion() const {
1052 return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift);
1053 }
1054
getLanguageID() const1055 uint8_t XCOFFTracebackTable::getLanguageID() const {
1056 return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift);
1057 }
1058
isGlobalLinkage() const1059 bool XCOFFTracebackTable::isGlobalLinkage() const {
1060 return GETBITWITHMASK(0, IsGlobaLinkageMask);
1061 }
1062
isOutOfLineEpilogOrPrologue() const1063 bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const {
1064 return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask);
1065 }
1066
hasTraceBackTableOffset() const1067 bool XCOFFTracebackTable::hasTraceBackTableOffset() const {
1068 return GETBITWITHMASK(0, HasTraceBackTableOffsetMask);
1069 }
1070
isInternalProcedure() const1071 bool XCOFFTracebackTable::isInternalProcedure() const {
1072 return GETBITWITHMASK(0, IsInternalProcedureMask);
1073 }
1074
hasControlledStorage() const1075 bool XCOFFTracebackTable::hasControlledStorage() const {
1076 return GETBITWITHMASK(0, HasControlledStorageMask);
1077 }
1078
isTOCless() const1079 bool XCOFFTracebackTable::isTOCless() const {
1080 return GETBITWITHMASK(0, IsTOClessMask);
1081 }
1082
isFloatingPointPresent() const1083 bool XCOFFTracebackTable::isFloatingPointPresent() const {
1084 return GETBITWITHMASK(0, IsFloatingPointPresentMask);
1085 }
1086
isFloatingPointOperationLogOrAbortEnabled() const1087 bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const {
1088 return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask);
1089 }
1090
isInterruptHandler() const1091 bool XCOFFTracebackTable::isInterruptHandler() const {
1092 return GETBITWITHMASK(0, IsInterruptHandlerMask);
1093 }
1094
isFuncNamePresent() const1095 bool XCOFFTracebackTable::isFuncNamePresent() const {
1096 return GETBITWITHMASK(0, IsFunctionNamePresentMask);
1097 }
1098
isAllocaUsed() const1099 bool XCOFFTracebackTable::isAllocaUsed() const {
1100 return GETBITWITHMASK(0, IsAllocaUsedMask);
1101 }
1102
getOnConditionDirective() const1103 uint8_t XCOFFTracebackTable::getOnConditionDirective() const {
1104 return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask,
1105 OnConditionDirectiveShift);
1106 }
1107
isCRSaved() const1108 bool XCOFFTracebackTable::isCRSaved() const {
1109 return GETBITWITHMASK(0, IsCRSavedMask);
1110 }
1111
isLRSaved() const1112 bool XCOFFTracebackTable::isLRSaved() const {
1113 return GETBITWITHMASK(0, IsLRSavedMask);
1114 }
1115
isBackChainStored() const1116 bool XCOFFTracebackTable::isBackChainStored() const {
1117 return GETBITWITHMASK(4, IsBackChainStoredMask);
1118 }
1119
isFixup() const1120 bool XCOFFTracebackTable::isFixup() const {
1121 return GETBITWITHMASK(4, IsFixupMask);
1122 }
1123
getNumOfFPRsSaved() const1124 uint8_t XCOFFTracebackTable::getNumOfFPRsSaved() const {
1125 return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift);
1126 }
1127
hasExtensionTable() const1128 bool XCOFFTracebackTable::hasExtensionTable() const {
1129 return GETBITWITHMASK(4, HasExtensionTableMask);
1130 }
1131
hasVectorInfo() const1132 bool XCOFFTracebackTable::hasVectorInfo() const {
1133 return GETBITWITHMASK(4, HasVectorInfoMask);
1134 }
1135
getNumOfGPRsSaved() const1136 uint8_t XCOFFTracebackTable::getNumOfGPRsSaved() const {
1137 return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift);
1138 }
1139
getNumberOfFixedParms() const1140 uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const {
1141 return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask,
1142 NumberOfFixedParmsShift);
1143 }
1144
getNumberOfFPParms() const1145 uint8_t XCOFFTracebackTable::getNumberOfFPParms() const {
1146 return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask,
1147 NumberOfFloatingPointParmsShift);
1148 }
1149
hasParmsOnStack() const1150 bool XCOFFTracebackTable::hasParmsOnStack() const {
1151 return GETBITWITHMASK(4, HasParmsOnStackMask);
1152 }
1153
1154 #undef GETBITWITHMASK
1155 #undef GETBITWITHMASKSHIFT
1156 } // namespace object
1157 } // namespace llvm
1158