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/Support/DataExtractor.h" |
16 | #include "llvm/TargetParser/SubtargetFeature.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(s: Name, c: '\0', n: XCOFF::NameSize)); |
53 | return NulCharPtr ? StringRef(Name, NulCharPtr - Name) |
54 | : StringRef(Name, XCOFF::NameSize); |
55 | } |
56 | |
57 | template <typename T> StringRef XCOFFSectionHeader<T>::() const { |
58 | const T & = static_cast<const T &>(*this); |
59 | return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name); |
60 | } |
61 | |
62 | template <typename T> uint16_t XCOFFSectionHeader<T>::() const { |
63 | const T & = static_cast<const T &>(*this); |
64 | return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask; |
65 | } |
66 | |
67 | template <typename T> |
68 | uint32_t XCOFFSectionHeader<T>::() const { |
69 | const T & = static_cast<const T &>(*this); |
70 | return DerivedXCOFFSectionHeader.Flags & ~SectionFlagsTypeMask; |
71 | } |
72 | |
73 | template <typename T> |
74 | bool XCOFFSectionHeader<T>::() const { |
75 | return getSectionType() & SectionFlagsReservedMask; |
76 | } |
77 | |
78 | template <typename AddressType> |
79 | bool XCOFFRelocation<AddressType>::isRelocationSigned() const { |
80 | return Info & XR_SIGN_INDICATOR_MASK; |
81 | } |
82 | |
83 | template <typename AddressType> |
84 | bool XCOFFRelocation<AddressType>::isFixupIndicated() const { |
85 | return Info & XR_FIXUP_INDICATOR_MASK; |
86 | } |
87 | |
88 | template <typename AddressType> |
89 | uint8_t XCOFFRelocation<AddressType>::getRelocatedLength() const { |
90 | // The relocation encodes the bit length being relocated minus 1. Add back |
91 | // the 1 to get the actual length being relocated. |
92 | return (Info & XR_BIASED_LENGTH_MASK) + 1; |
93 | } |
94 | |
95 | template struct ExceptionSectionEntry<support::ubig32_t>; |
96 | template struct ExceptionSectionEntry<support::ubig64_t>; |
97 | |
98 | template <typename T> |
99 | Expected<StringRef> getLoaderSecSymNameInStrTbl(const T *, |
100 | uint64_t Offset) { |
101 | if (LoaderSecHeader->LengthOfStrTbl > Offset) |
102 | return (reinterpret_cast<const char *>(LoaderSecHeader) + |
103 | LoaderSecHeader->OffsetToStrTbl + Offset); |
104 | |
105 | return createError("entry with offset 0x" + Twine::utohexstr(Val: Offset) + |
106 | " in the loader section's string table with size 0x" + |
107 | Twine::utohexstr(Val: LoaderSecHeader->LengthOfStrTbl) + |
108 | " is invalid" ); |
109 | } |
110 | |
111 | Expected<StringRef> LoaderSectionSymbolEntry32::( |
112 | const LoaderSectionHeader32 *) const { |
113 | const NameOffsetInStrTbl *NameInStrTbl = |
114 | reinterpret_cast<const NameOffsetInStrTbl *>(SymbolName); |
115 | if (NameInStrTbl->IsNameInStrTbl != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC) |
116 | return generateXCOFFFixedNameStringRef(Name: SymbolName); |
117 | |
118 | return getLoaderSecSymNameInStrTbl(LoaderSecHeader: LoaderSecHeader32, Offset: NameInStrTbl->Offset); |
119 | } |
120 | |
121 | Expected<StringRef> LoaderSectionSymbolEntry64::( |
122 | const LoaderSectionHeader64 *) const { |
123 | return getLoaderSecSymNameInStrTbl(LoaderSecHeader: LoaderSecHeader64, Offset); |
124 | } |
125 | |
126 | uintptr_t |
127 | XCOFFObjectFile::getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress, |
128 | uint32_t Distance) { |
129 | return getWithOffset(Base: CurrentAddress, Offset: Distance * XCOFF::SymbolTableEntrySize); |
130 | } |
131 | |
132 | const XCOFF::SymbolAuxType * |
133 | XCOFFObjectFile::getSymbolAuxType(uintptr_t AuxEntryAddress) const { |
134 | assert(is64Bit() && "64-bit interface called on a 32-bit object file." ); |
135 | return viewAs<XCOFF::SymbolAuxType>( |
136 | in: getWithOffset(Base: AuxEntryAddress, Offset: SymbolAuxTypeOffset)); |
137 | } |
138 | |
139 | void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr, |
140 | uintptr_t TableAddress) const { |
141 | if (Addr < TableAddress) |
142 | report_fatal_error(reason: "Section header outside of section header table." ); |
143 | |
144 | uintptr_t Offset = Addr - TableAddress; |
145 | if (Offset >= getSectionHeaderSize() * getNumberOfSections()) |
146 | report_fatal_error(reason: "Section header outside of section header table." ); |
147 | |
148 | if (Offset % getSectionHeaderSize() != 0) |
149 | report_fatal_error( |
150 | reason: "Section header pointer does not point to a valid section header." ); |
151 | } |
152 | |
153 | const XCOFFSectionHeader32 * |
154 | XCOFFObjectFile::toSection32(DataRefImpl Ref) const { |
155 | assert(!is64Bit() && "32-bit interface called on 64-bit object file." ); |
156 | #ifndef NDEBUG |
157 | checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); |
158 | #endif |
159 | return viewAs<XCOFFSectionHeader32>(in: Ref.p); |
160 | } |
161 | |
162 | const XCOFFSectionHeader64 * |
163 | XCOFFObjectFile::toSection64(DataRefImpl Ref) const { |
164 | assert(is64Bit() && "64-bit interface called on a 32-bit object file." ); |
165 | #ifndef NDEBUG |
166 | checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); |
167 | #endif |
168 | return viewAs<XCOFFSectionHeader64>(in: Ref.p); |
169 | } |
170 | |
171 | XCOFFSymbolRef XCOFFObjectFile::toSymbolRef(DataRefImpl Ref) const { |
172 | assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!" ); |
173 | #ifndef NDEBUG |
174 | checkSymbolEntryPointer(Ref.p); |
175 | #endif |
176 | return XCOFFSymbolRef(Ref, this); |
177 | } |
178 | |
179 | const XCOFFFileHeader32 *XCOFFObjectFile::() const { |
180 | assert(!is64Bit() && "32-bit interface called on 64-bit object file." ); |
181 | return static_cast<const XCOFFFileHeader32 *>(FileHeader); |
182 | } |
183 | |
184 | const XCOFFFileHeader64 *XCOFFObjectFile::() const { |
185 | assert(is64Bit() && "64-bit interface called on a 32-bit object file." ); |
186 | return static_cast<const XCOFFFileHeader64 *>(FileHeader); |
187 | } |
188 | |
189 | const XCOFFAuxiliaryHeader32 *XCOFFObjectFile::() const { |
190 | assert(!is64Bit() && "32-bit interface called on 64-bit object file." ); |
191 | return static_cast<const XCOFFAuxiliaryHeader32 *>(AuxiliaryHeader); |
192 | } |
193 | |
194 | const XCOFFAuxiliaryHeader64 *XCOFFObjectFile::() const { |
195 | assert(is64Bit() && "64-bit interface called on a 32-bit object file." ); |
196 | return static_cast<const XCOFFAuxiliaryHeader64 *>(AuxiliaryHeader); |
197 | } |
198 | |
199 | template <typename T> const T *XCOFFObjectFile::() const { |
200 | return static_cast<const T *>(SectionHeaderTable); |
201 | } |
202 | |
203 | const XCOFFSectionHeader32 * |
204 | XCOFFObjectFile::() const { |
205 | assert(!is64Bit() && "32-bit interface called on 64-bit object file." ); |
206 | return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable); |
207 | } |
208 | |
209 | const XCOFFSectionHeader64 * |
210 | XCOFFObjectFile::() const { |
211 | assert(is64Bit() && "64-bit interface called on a 32-bit object file." ); |
212 | return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable); |
213 | } |
214 | |
215 | void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
216 | uintptr_t NextSymbolAddr = getAdvancedSymbolEntryAddress( |
217 | CurrentAddress: Symb.p, Distance: toSymbolRef(Ref: Symb).getNumberOfAuxEntries() + 1); |
218 | #ifndef NDEBUG |
219 | // This function is used by basic_symbol_iterator, which allows to |
220 | // point to the end-of-symbol-table address. |
221 | if (NextSymbolAddr != getEndOfSymbolTableAddress()) |
222 | checkSymbolEntryPointer(NextSymbolAddr); |
223 | #endif |
224 | Symb.p = NextSymbolAddr; |
225 | } |
226 | |
227 | Expected<StringRef> |
228 | XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const { |
229 | // The byte offset is relative to the start of the string table. |
230 | // A byte offset value of 0 is a null or zero-length symbol |
231 | // name. A byte offset in the range 1 to 3 (inclusive) points into the length |
232 | // field; as a soft-error recovery mechanism, we treat such cases as having an |
233 | // offset of 0. |
234 | if (Offset < 4) |
235 | return StringRef(nullptr, 0); |
236 | |
237 | if (StringTable.Data != nullptr && StringTable.Size > Offset) |
238 | return (StringTable.Data + Offset); |
239 | |
240 | return createError(Err: "entry with offset 0x" + Twine::utohexstr(Val: Offset) + |
241 | " in a string table with size 0x" + |
242 | Twine::utohexstr(Val: StringTable.Size) + " is invalid" ); |
243 | } |
244 | |
245 | StringRef XCOFFObjectFile::getStringTable() const { |
246 | // If the size is less than or equal to 4, then the string table contains no |
247 | // string data. |
248 | return StringRef(StringTable.Data, |
249 | StringTable.Size <= 4 ? 0 : StringTable.Size); |
250 | } |
251 | |
252 | Expected<StringRef> |
253 | XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const { |
254 | if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC) |
255 | return generateXCOFFFixedNameStringRef(Name: CFileEntPtr->Name); |
256 | return getStringTableEntry(Offset: CFileEntPtr->NameInStrTbl.Offset); |
257 | } |
258 | |
259 | Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const { |
260 | return toSymbolRef(Ref: Symb).getName(); |
261 | } |
262 | |
263 | Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { |
264 | return toSymbolRef(Ref: Symb).getValue(); |
265 | } |
266 | |
267 | uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { |
268 | return toSymbolRef(Ref: Symb).getValue(); |
269 | } |
270 | |
271 | uint32_t XCOFFObjectFile::getSymbolAlignment(DataRefImpl Symb) const { |
272 | uint64_t Result = 0; |
273 | XCOFFSymbolRef XCOFFSym = toSymbolRef(Ref: Symb); |
274 | if (XCOFFSym.isCsectSymbol()) { |
275 | Expected<XCOFFCsectAuxRef> CsectAuxRefOrError = |
276 | XCOFFSym.getXCOFFCsectAuxRef(); |
277 | if (!CsectAuxRefOrError) |
278 | // TODO: report the error up the stack. |
279 | consumeError(Err: CsectAuxRefOrError.takeError()); |
280 | else |
281 | Result = 1ULL << CsectAuxRefOrError.get().getAlignmentLog2(); |
282 | } |
283 | return Result; |
284 | } |
285 | |
286 | uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { |
287 | uint64_t Result = 0; |
288 | XCOFFSymbolRef XCOFFSym = toSymbolRef(Ref: Symb); |
289 | if (XCOFFSym.isCsectSymbol()) { |
290 | Expected<XCOFFCsectAuxRef> CsectAuxRefOrError = |
291 | XCOFFSym.getXCOFFCsectAuxRef(); |
292 | if (!CsectAuxRefOrError) |
293 | // TODO: report the error up the stack. |
294 | consumeError(Err: CsectAuxRefOrError.takeError()); |
295 | else { |
296 | XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get(); |
297 | assert(CsectAuxRef.getSymbolType() == XCOFF::XTY_CM); |
298 | Result = CsectAuxRef.getSectionOrLength(); |
299 | } |
300 | } |
301 | return Result; |
302 | } |
303 | |
304 | Expected<SymbolRef::Type> |
305 | XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const { |
306 | XCOFFSymbolRef XCOFFSym = toSymbolRef(Ref: Symb); |
307 | |
308 | Expected<bool> IsFunction = XCOFFSym.isFunction(); |
309 | if (!IsFunction) |
310 | return IsFunction.takeError(); |
311 | |
312 | if (*IsFunction) |
313 | return SymbolRef::ST_Function; |
314 | |
315 | if (XCOFF::C_FILE == XCOFFSym.getStorageClass()) |
316 | return SymbolRef::ST_File; |
317 | |
318 | int16_t SecNum = XCOFFSym.getSectionNumber(); |
319 | if (SecNum <= 0) |
320 | return SymbolRef::ST_Other; |
321 | |
322 | Expected<DataRefImpl> SecDRIOrErr = |
323 | getSectionByNum(Num: XCOFFSym.getSectionNumber()); |
324 | |
325 | if (!SecDRIOrErr) |
326 | return SecDRIOrErr.takeError(); |
327 | |
328 | DataRefImpl SecDRI = SecDRIOrErr.get(); |
329 | |
330 | Expected<StringRef> SymNameOrError = XCOFFSym.getName(); |
331 | if (SymNameOrError) { |
332 | // The "TOC" symbol is treated as SymbolRef::ST_Other. |
333 | if (SymNameOrError.get() == "TOC" ) |
334 | return SymbolRef::ST_Other; |
335 | |
336 | // The symbol for a section name is treated as SymbolRef::ST_Other. |
337 | StringRef SecName; |
338 | if (is64Bit()) |
339 | SecName = XCOFFObjectFile::toSection64(Ref: SecDRIOrErr.get())->getName(); |
340 | else |
341 | SecName = XCOFFObjectFile::toSection32(Ref: SecDRIOrErr.get())->getName(); |
342 | |
343 | if (SecName == SymNameOrError.get()) |
344 | return SymbolRef::ST_Other; |
345 | } else |
346 | return SymNameOrError.takeError(); |
347 | |
348 | if (isSectionData(Sec: SecDRI) || isSectionBSS(Sec: SecDRI)) |
349 | return SymbolRef::ST_Data; |
350 | |
351 | if (isDebugSection(Sec: SecDRI)) |
352 | return SymbolRef::ST_Debug; |
353 | |
354 | return SymbolRef::ST_Other; |
355 | } |
356 | |
357 | Expected<section_iterator> |
358 | XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const { |
359 | const int16_t SectNum = toSymbolRef(Ref: Symb).getSectionNumber(); |
360 | |
361 | if (isReservedSectionNumber(SectionNumber: SectNum)) |
362 | return section_end(); |
363 | |
364 | Expected<DataRefImpl> ExpSec = getSectionByNum(Num: SectNum); |
365 | if (!ExpSec) |
366 | return ExpSec.takeError(); |
367 | |
368 | return section_iterator(SectionRef(ExpSec.get(), this)); |
369 | } |
370 | |
371 | void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
372 | const char *Ptr = reinterpret_cast<const char *>(Sec.p); |
373 | Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize()); |
374 | } |
375 | |
376 | Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const { |
377 | return generateXCOFFFixedNameStringRef(Name: getSectionNameInternal(Sec)); |
378 | } |
379 | |
380 | uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const { |
381 | // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t |
382 | // with MSVC. |
383 | if (is64Bit()) |
384 | return toSection64(Ref: Sec)->VirtualAddress; |
385 | |
386 | return toSection32(Ref: Sec)->VirtualAddress; |
387 | } |
388 | |
389 | uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const { |
390 | // Section numbers in XCOFF are numbered beginning at 1. A section number of |
391 | // zero is used to indicate that a symbol is being imported or is undefined. |
392 | if (is64Bit()) |
393 | return toSection64(Ref: Sec) - sectionHeaderTable64() + 1; |
394 | else |
395 | return toSection32(Ref: Sec) - sectionHeaderTable32() + 1; |
396 | } |
397 | |
398 | uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const { |
399 | // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t |
400 | // with MSVC. |
401 | if (is64Bit()) |
402 | return toSection64(Ref: Sec)->SectionSize; |
403 | |
404 | return toSection32(Ref: Sec)->SectionSize; |
405 | } |
406 | |
407 | Expected<ArrayRef<uint8_t>> |
408 | XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const { |
409 | if (isSectionVirtual(Sec)) |
410 | return ArrayRef<uint8_t>(); |
411 | |
412 | uint64_t OffsetToRaw; |
413 | if (is64Bit()) |
414 | OffsetToRaw = toSection64(Ref: Sec)->FileOffsetToRawData; |
415 | else |
416 | OffsetToRaw = toSection32(Ref: Sec)->FileOffsetToRawData; |
417 | |
418 | const uint8_t * ContentStart = base() + OffsetToRaw; |
419 | uint64_t SectionSize = getSectionSize(Sec); |
420 | if (Error E = Binary::checkOffset( |
421 | M: Data, Addr: reinterpret_cast<uintptr_t>(ContentStart), Size: SectionSize)) |
422 | return createError( |
423 | Err: toString(E: std::move(E)) + ": section data with offset 0x" + |
424 | Twine::utohexstr(Val: OffsetToRaw) + " and size 0x" + |
425 | Twine::utohexstr(Val: SectionSize) + " goes past the end of the file" ); |
426 | |
427 | return ArrayRef(ContentStart, SectionSize); |
428 | } |
429 | |
430 | uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
431 | uint64_t Result = 0; |
432 | llvm_unreachable("Not yet implemented!" ); |
433 | return Result; |
434 | } |
435 | |
436 | uint64_t XCOFFObjectFile::getSectionFileOffsetToRawData(DataRefImpl Sec) const { |
437 | if (is64Bit()) |
438 | return toSection64(Ref: Sec)->FileOffsetToRawData; |
439 | |
440 | return toSection32(Ref: Sec)->FileOffsetToRawData; |
441 | } |
442 | |
443 | Expected<uintptr_t> XCOFFObjectFile::getSectionFileOffsetToRawData( |
444 | XCOFF::SectionTypeFlags SectType) const { |
445 | DataRefImpl DRI = getSectionByType(SectType); |
446 | |
447 | if (DRI.p == 0) // No section is not an error. |
448 | return 0; |
449 | |
450 | uint64_t SectionOffset = getSectionFileOffsetToRawData(Sec: DRI); |
451 | uint64_t SizeOfSection = getSectionSize(Sec: DRI); |
452 | |
453 | uintptr_t SectionStart = reinterpret_cast<uintptr_t>(base() + SectionOffset); |
454 | if (Error E = Binary::checkOffset(M: Data, Addr: SectionStart, Size: SizeOfSection)) { |
455 | SmallString<32> UnknownType; |
456 | Twine(("<Unknown:" ) + Twine::utohexstr(Val: SectType) + ">" ) |
457 | .toVector(Out&: UnknownType); |
458 | const char *SectionName = UnknownType.c_str(); |
459 | |
460 | switch (SectType) { |
461 | #define ECASE(Value, String) \ |
462 | case XCOFF::Value: \ |
463 | SectionName = String; \ |
464 | break |
465 | |
466 | ECASE(STYP_PAD, "pad" ); |
467 | ECASE(STYP_DWARF, "dwarf" ); |
468 | ECASE(STYP_TEXT, "text" ); |
469 | ECASE(STYP_DATA, "data" ); |
470 | ECASE(STYP_BSS, "bss" ); |
471 | ECASE(STYP_EXCEPT, "expect" ); |
472 | ECASE(STYP_INFO, "info" ); |
473 | ECASE(STYP_TDATA, "tdata" ); |
474 | ECASE(STYP_TBSS, "tbss" ); |
475 | ECASE(STYP_LOADER, "loader" ); |
476 | ECASE(STYP_DEBUG, "debug" ); |
477 | ECASE(STYP_TYPCHK, "typchk" ); |
478 | ECASE(STYP_OVRFLO, "ovrflo" ); |
479 | #undef ECASE |
480 | } |
481 | return createError(Err: toString(E: std::move(E)) + ": " + SectionName + |
482 | " section with offset 0x" + |
483 | Twine::utohexstr(Val: SectionOffset) + " and size 0x" + |
484 | Twine::utohexstr(Val: SizeOfSection) + |
485 | " goes past the end of the file" ); |
486 | } |
487 | return SectionStart; |
488 | } |
489 | |
490 | bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
491 | return false; |
492 | } |
493 | |
494 | bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const { |
495 | return getSectionFlags(Sec) & XCOFF::STYP_TEXT; |
496 | } |
497 | |
498 | bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const { |
499 | uint32_t Flags = getSectionFlags(Sec); |
500 | return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA); |
501 | } |
502 | |
503 | bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const { |
504 | uint32_t Flags = getSectionFlags(Sec); |
505 | return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS); |
506 | } |
507 | |
508 | bool XCOFFObjectFile::isDebugSection(DataRefImpl Sec) const { |
509 | uint32_t Flags = getSectionFlags(Sec); |
510 | return Flags & (XCOFF::STYP_DEBUG | XCOFF::STYP_DWARF); |
511 | } |
512 | |
513 | bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const { |
514 | return is64Bit() ? toSection64(Ref: Sec)->FileOffsetToRawData == 0 |
515 | : toSection32(Ref: Sec)->FileOffsetToRawData == 0; |
516 | } |
517 | |
518 | relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const { |
519 | DataRefImpl Ret; |
520 | if (is64Bit()) { |
521 | const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Ref: Sec); |
522 | auto RelocationsOrErr = |
523 | relocations<XCOFFSectionHeader64, XCOFFRelocation64>(Sec: *SectionEntPtr); |
524 | if (Error E = RelocationsOrErr.takeError()) { |
525 | // TODO: report the error up the stack. |
526 | consumeError(Err: std::move(E)); |
527 | return relocation_iterator(RelocationRef()); |
528 | } |
529 | Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin()); |
530 | } else { |
531 | const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Ref: Sec); |
532 | auto RelocationsOrErr = |
533 | relocations<XCOFFSectionHeader32, XCOFFRelocation32>(Sec: *SectionEntPtr); |
534 | if (Error E = RelocationsOrErr.takeError()) { |
535 | // TODO: report the error up the stack. |
536 | consumeError(Err: std::move(E)); |
537 | return relocation_iterator(RelocationRef()); |
538 | } |
539 | Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin()); |
540 | } |
541 | return relocation_iterator(RelocationRef(Ret, this)); |
542 | } |
543 | |
544 | relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const { |
545 | DataRefImpl Ret; |
546 | if (is64Bit()) { |
547 | const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Ref: Sec); |
548 | auto RelocationsOrErr = |
549 | relocations<XCOFFSectionHeader64, XCOFFRelocation64>(Sec: *SectionEntPtr); |
550 | if (Error E = RelocationsOrErr.takeError()) { |
551 | // TODO: report the error up the stack. |
552 | consumeError(Err: std::move(E)); |
553 | return relocation_iterator(RelocationRef()); |
554 | } |
555 | Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end()); |
556 | } else { |
557 | const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Ref: Sec); |
558 | auto RelocationsOrErr = |
559 | relocations<XCOFFSectionHeader32, XCOFFRelocation32>(Sec: *SectionEntPtr); |
560 | if (Error E = RelocationsOrErr.takeError()) { |
561 | // TODO: report the error up the stack. |
562 | consumeError(Err: std::move(E)); |
563 | return relocation_iterator(RelocationRef()); |
564 | } |
565 | Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end()); |
566 | } |
567 | return relocation_iterator(RelocationRef(Ret, this)); |
568 | } |
569 | |
570 | void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
571 | if (is64Bit()) |
572 | Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation64>(in: Rel.p) + 1); |
573 | else |
574 | Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(in: Rel.p) + 1); |
575 | } |
576 | |
577 | uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { |
578 | if (is64Bit()) { |
579 | const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(in: Rel.p); |
580 | const XCOFFSectionHeader64 *Sec64 = sectionHeaderTable64(); |
581 | const uint64_t RelocAddress = Reloc->VirtualAddress; |
582 | const uint16_t NumberOfSections = getNumberOfSections(); |
583 | for (uint16_t I = 0; I < NumberOfSections; ++I) { |
584 | // Find which section this relocation belongs to, and get the |
585 | // relocation offset relative to the start of the section. |
586 | if (Sec64->VirtualAddress <= RelocAddress && |
587 | RelocAddress < Sec64->VirtualAddress + Sec64->SectionSize) { |
588 | return RelocAddress - Sec64->VirtualAddress; |
589 | } |
590 | ++Sec64; |
591 | } |
592 | } else { |
593 | const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(in: Rel.p); |
594 | const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32(); |
595 | const uint32_t RelocAddress = Reloc->VirtualAddress; |
596 | const uint16_t NumberOfSections = getNumberOfSections(); |
597 | for (uint16_t I = 0; I < NumberOfSections; ++I) { |
598 | // Find which section this relocation belongs to, and get the |
599 | // relocation offset relative to the start of the section. |
600 | if (Sec32->VirtualAddress <= RelocAddress && |
601 | RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) { |
602 | return RelocAddress - Sec32->VirtualAddress; |
603 | } |
604 | ++Sec32; |
605 | } |
606 | } |
607 | return InvalidRelocOffset; |
608 | } |
609 | |
610 | symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
611 | uint32_t Index; |
612 | if (is64Bit()) { |
613 | const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(in: Rel.p); |
614 | Index = Reloc->SymbolIndex; |
615 | |
616 | if (Index >= getNumberOfSymbolTableEntries64()) |
617 | return symbol_end(); |
618 | } else { |
619 | const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(in: Rel.p); |
620 | Index = Reloc->SymbolIndex; |
621 | |
622 | if (Index >= getLogicalNumberOfSymbolTableEntries32()) |
623 | return symbol_end(); |
624 | } |
625 | DataRefImpl SymDRI; |
626 | SymDRI.p = getSymbolEntryAddressByIndex(SymbolTableIndex: Index); |
627 | return symbol_iterator(SymbolRef(SymDRI, this)); |
628 | } |
629 | |
630 | uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const { |
631 | if (is64Bit()) |
632 | return viewAs<XCOFFRelocation64>(in: Rel.p)->Type; |
633 | return viewAs<XCOFFRelocation32>(in: Rel.p)->Type; |
634 | } |
635 | |
636 | void XCOFFObjectFile::getRelocationTypeName( |
637 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
638 | StringRef Res; |
639 | if (is64Bit()) { |
640 | const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(in: Rel.p); |
641 | Res = XCOFF::getRelocationTypeString(Type: Reloc->Type); |
642 | } else { |
643 | const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(in: Rel.p); |
644 | Res = XCOFF::getRelocationTypeString(Type: Reloc->Type); |
645 | } |
646 | Result.append(in_start: Res.begin(), in_end: Res.end()); |
647 | } |
648 | |
649 | Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { |
650 | XCOFFSymbolRef XCOFFSym = toSymbolRef(Ref: Symb); |
651 | uint32_t Result = SymbolRef::SF_None; |
652 | |
653 | if (XCOFFSym.getSectionNumber() == XCOFF::N_ABS) |
654 | Result |= SymbolRef::SF_Absolute; |
655 | |
656 | XCOFF::StorageClass SC = XCOFFSym.getStorageClass(); |
657 | if (XCOFF::C_EXT == SC || XCOFF::C_WEAKEXT == SC) |
658 | Result |= SymbolRef::SF_Global; |
659 | |
660 | if (XCOFF::C_WEAKEXT == SC) |
661 | Result |= SymbolRef::SF_Weak; |
662 | |
663 | if (XCOFFSym.isCsectSymbol()) { |
664 | Expected<XCOFFCsectAuxRef> CsectAuxEntOrErr = |
665 | XCOFFSym.getXCOFFCsectAuxRef(); |
666 | if (CsectAuxEntOrErr) { |
667 | if (CsectAuxEntOrErr.get().getSymbolType() == XCOFF::XTY_CM) |
668 | Result |= SymbolRef::SF_Common; |
669 | } else |
670 | return CsectAuxEntOrErr.takeError(); |
671 | } |
672 | |
673 | if (XCOFFSym.getSectionNumber() == XCOFF::N_UNDEF) |
674 | Result |= SymbolRef::SF_Undefined; |
675 | |
676 | // There is no visibility in old 32 bit XCOFF object file interpret. |
677 | if (is64Bit() || (auxiliaryHeader32() && (auxiliaryHeader32()->getVersion() == |
678 | NEW_XCOFF_INTERPRET))) { |
679 | uint16_t SymType = XCOFFSym.getSymbolType(); |
680 | if ((SymType & VISIBILITY_MASK) == SYM_V_HIDDEN) |
681 | Result |= SymbolRef::SF_Hidden; |
682 | |
683 | if ((SymType & VISIBILITY_MASK) == SYM_V_EXPORTED) |
684 | Result |= SymbolRef::SF_Exported; |
685 | } |
686 | return Result; |
687 | } |
688 | |
689 | basic_symbol_iterator XCOFFObjectFile::symbol_begin() const { |
690 | DataRefImpl SymDRI; |
691 | SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr); |
692 | return basic_symbol_iterator(SymbolRef(SymDRI, this)); |
693 | } |
694 | |
695 | basic_symbol_iterator XCOFFObjectFile::symbol_end() const { |
696 | DataRefImpl SymDRI; |
697 | const uint32_t NumberOfSymbolTableEntries = getNumberOfSymbolTableEntries(); |
698 | SymDRI.p = getSymbolEntryAddressByIndex(SymbolTableIndex: NumberOfSymbolTableEntries); |
699 | return basic_symbol_iterator(SymbolRef(SymDRI, this)); |
700 | } |
701 | |
702 | XCOFFObjectFile::xcoff_symbol_iterator_range XCOFFObjectFile::symbols() const { |
703 | return xcoff_symbol_iterator_range(symbol_begin(), symbol_end()); |
704 | } |
705 | |
706 | section_iterator XCOFFObjectFile::section_begin() const { |
707 | DataRefImpl DRI; |
708 | DRI.p = getSectionHeaderTableAddress(); |
709 | return section_iterator(SectionRef(DRI, this)); |
710 | } |
711 | |
712 | section_iterator XCOFFObjectFile::section_end() const { |
713 | DataRefImpl DRI; |
714 | DRI.p = getWithOffset(Base: getSectionHeaderTableAddress(), |
715 | Offset: getNumberOfSections() * getSectionHeaderSize()); |
716 | return section_iterator(SectionRef(DRI, this)); |
717 | } |
718 | |
719 | uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; } |
720 | |
721 | StringRef XCOFFObjectFile::getFileFormatName() const { |
722 | return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000" ; |
723 | } |
724 | |
725 | Triple::ArchType XCOFFObjectFile::getArch() const { |
726 | return is64Bit() ? Triple::ppc64 : Triple::ppc; |
727 | } |
728 | |
729 | Expected<SubtargetFeatures> XCOFFObjectFile::getFeatures() const { |
730 | return SubtargetFeatures(); |
731 | } |
732 | |
733 | bool XCOFFObjectFile::isRelocatableObject() const { |
734 | if (is64Bit()) |
735 | return !(fileHeader64()->Flags & NoRelMask); |
736 | return !(fileHeader32()->Flags & NoRelMask); |
737 | } |
738 | |
739 | Expected<uint64_t> XCOFFObjectFile::getStartAddress() const { |
740 | if (AuxiliaryHeader == nullptr) |
741 | return 0; |
742 | |
743 | return is64Bit() ? auxiliaryHeader64()->getEntryPointAddr() |
744 | : auxiliaryHeader32()->getEntryPointAddr(); |
745 | } |
746 | |
747 | StringRef XCOFFObjectFile::mapDebugSectionName(StringRef Name) const { |
748 | return StringSwitch<StringRef>(Name) |
749 | .Case(S: "dwinfo" , Value: "debug_info" ) |
750 | .Case(S: "dwline" , Value: "debug_line" ) |
751 | .Case(S: "dwpbnms" , Value: "debug_pubnames" ) |
752 | .Case(S: "dwpbtyp" , Value: "debug_pubtypes" ) |
753 | .Case(S: "dwarnge" , Value: "debug_aranges" ) |
754 | .Case(S: "dwabrev" , Value: "debug_abbrev" ) |
755 | .Case(S: "dwstr" , Value: "debug_str" ) |
756 | .Case(S: "dwrnges" , Value: "debug_ranges" ) |
757 | .Case(S: "dwloc" , Value: "debug_loc" ) |
758 | .Case(S: "dwframe" , Value: "debug_frame" ) |
759 | .Case(S: "dwmac" , Value: "debug_macinfo" ) |
760 | .Default(Value: Name); |
761 | } |
762 | |
763 | size_t XCOFFObjectFile::() const { |
764 | return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32); |
765 | } |
766 | |
767 | size_t XCOFFObjectFile::() const { |
768 | return is64Bit() ? sizeof(XCOFFSectionHeader64) : |
769 | sizeof(XCOFFSectionHeader32); |
770 | } |
771 | |
772 | bool XCOFFObjectFile::is64Bit() const { |
773 | return Binary::ID_XCOFF64 == getType(); |
774 | } |
775 | |
776 | Expected<StringRef> XCOFFObjectFile::getRawData(const char *Start, |
777 | uint64_t Size, |
778 | StringRef Name) const { |
779 | uintptr_t StartPtr = reinterpret_cast<uintptr_t>(Start); |
780 | // TODO: this path is untested. |
781 | if (Error E = Binary::checkOffset(M: Data, Addr: StartPtr, Size)) |
782 | return createError(Err: toString(E: std::move(E)) + ": " + Name.data() + |
783 | " data with offset 0x" + Twine::utohexstr(Val: StartPtr) + |
784 | " and size 0x" + Twine::utohexstr(Val: Size) + |
785 | " goes past the end of the file" ); |
786 | return StringRef(Start, Size); |
787 | } |
788 | |
789 | uint16_t XCOFFObjectFile::getMagic() const { |
790 | return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic; |
791 | } |
792 | |
793 | Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const { |
794 | if (Num <= 0 || Num > getNumberOfSections()) |
795 | return createStringError(EC: object_error::invalid_section_index, |
796 | S: "the section index (" + Twine(Num) + |
797 | ") is invalid" ); |
798 | |
799 | DataRefImpl DRI; |
800 | DRI.p = getWithOffset(Base: getSectionHeaderTableAddress(), |
801 | Offset: getSectionHeaderSize() * (Num - 1)); |
802 | return DRI; |
803 | } |
804 | |
805 | DataRefImpl |
806 | XCOFFObjectFile::getSectionByType(XCOFF::SectionTypeFlags SectType) const { |
807 | DataRefImpl DRI; |
808 | auto GetSectionAddr = [&](const auto &Sections) -> uintptr_t { |
809 | for (const auto &Sec : Sections) |
810 | if (Sec.getSectionType() == SectType) |
811 | return reinterpret_cast<uintptr_t>(&Sec); |
812 | return uintptr_t(0); |
813 | }; |
814 | if (is64Bit()) |
815 | DRI.p = GetSectionAddr(sections64()); |
816 | else |
817 | DRI.p = GetSectionAddr(sections32()); |
818 | return DRI; |
819 | } |
820 | |
821 | Expected<StringRef> |
822 | XCOFFObjectFile::getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const { |
823 | const int16_t SectionNum = SymEntPtr.getSectionNumber(); |
824 | |
825 | switch (SectionNum) { |
826 | case XCOFF::N_DEBUG: |
827 | return "N_DEBUG" ; |
828 | case XCOFF::N_ABS: |
829 | return "N_ABS" ; |
830 | case XCOFF::N_UNDEF: |
831 | return "N_UNDEF" ; |
832 | default: |
833 | Expected<DataRefImpl> SecRef = getSectionByNum(Num: SectionNum); |
834 | if (SecRef) |
835 | return generateXCOFFFixedNameStringRef( |
836 | Name: getSectionNameInternal(Sec: SecRef.get())); |
837 | return SecRef.takeError(); |
838 | } |
839 | } |
840 | |
841 | unsigned XCOFFObjectFile::getSymbolSectionID(SymbolRef Sym) const { |
842 | XCOFFSymbolRef XCOFFSymRef(Sym.getRawDataRefImpl(), this); |
843 | return XCOFFSymRef.getSectionNumber(); |
844 | } |
845 | |
846 | bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) { |
847 | return (SectionNumber <= 0 && SectionNumber >= -2); |
848 | } |
849 | |
850 | uint16_t XCOFFObjectFile::getNumberOfSections() const { |
851 | return is64Bit() ? fileHeader64()->NumberOfSections |
852 | : fileHeader32()->NumberOfSections; |
853 | } |
854 | |
855 | int32_t XCOFFObjectFile::getTimeStamp() const { |
856 | return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp; |
857 | } |
858 | |
859 | uint16_t XCOFFObjectFile::() const { |
860 | return is64Bit() ? fileHeader64()->AuxHeaderSize |
861 | : fileHeader32()->AuxHeaderSize; |
862 | } |
863 | |
864 | uint32_t XCOFFObjectFile::getSymbolTableOffset32() const { |
865 | return fileHeader32()->SymbolTableOffset; |
866 | } |
867 | |
868 | int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const { |
869 | // As far as symbol table size is concerned, if this field is negative it is |
870 | // to be treated as a 0. However since this field is also used for printing we |
871 | // don't want to truncate any negative values. |
872 | return fileHeader32()->NumberOfSymTableEntries; |
873 | } |
874 | |
875 | uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const { |
876 | return (fileHeader32()->NumberOfSymTableEntries >= 0 |
877 | ? fileHeader32()->NumberOfSymTableEntries |
878 | : 0); |
879 | } |
880 | |
881 | uint64_t XCOFFObjectFile::getSymbolTableOffset64() const { |
882 | return fileHeader64()->SymbolTableOffset; |
883 | } |
884 | |
885 | uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const { |
886 | return fileHeader64()->NumberOfSymTableEntries; |
887 | } |
888 | |
889 | uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries() const { |
890 | return is64Bit() ? getNumberOfSymbolTableEntries64() |
891 | : getLogicalNumberOfSymbolTableEntries32(); |
892 | } |
893 | |
894 | uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const { |
895 | const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries(); |
896 | return getWithOffset(Base: reinterpret_cast<uintptr_t>(SymbolTblPtr), |
897 | Offset: XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries); |
898 | } |
899 | |
900 | void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const { |
901 | if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr)) |
902 | report_fatal_error(reason: "Symbol table entry is outside of symbol table." ); |
903 | |
904 | if (SymbolEntPtr >= getEndOfSymbolTableAddress()) |
905 | report_fatal_error(reason: "Symbol table entry is outside of symbol table." ); |
906 | |
907 | ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) - |
908 | reinterpret_cast<const char *>(SymbolTblPtr); |
909 | |
910 | if (Offset % XCOFF::SymbolTableEntrySize != 0) |
911 | report_fatal_error( |
912 | reason: "Symbol table entry position is not valid inside of symbol table." ); |
913 | } |
914 | |
915 | uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const { |
916 | return (reinterpret_cast<const char *>(SymbolEntPtr) - |
917 | reinterpret_cast<const char *>(SymbolTblPtr)) / |
918 | XCOFF::SymbolTableEntrySize; |
919 | } |
920 | |
921 | uint64_t XCOFFObjectFile::getSymbolSize(DataRefImpl Symb) const { |
922 | uint64_t Result = 0; |
923 | XCOFFSymbolRef XCOFFSym = toSymbolRef(Ref: Symb); |
924 | if (XCOFFSym.isCsectSymbol()) { |
925 | Expected<XCOFFCsectAuxRef> CsectAuxRefOrError = |
926 | XCOFFSym.getXCOFFCsectAuxRef(); |
927 | if (!CsectAuxRefOrError) |
928 | // TODO: report the error up the stack. |
929 | consumeError(Err: CsectAuxRefOrError.takeError()); |
930 | else { |
931 | XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get(); |
932 | uint8_t SymType = CsectAuxRef.getSymbolType(); |
933 | if (SymType == XCOFF::XTY_SD || SymType == XCOFF::XTY_CM) |
934 | Result = CsectAuxRef.getSectionOrLength(); |
935 | } |
936 | } |
937 | return Result; |
938 | } |
939 | |
940 | uintptr_t XCOFFObjectFile::getSymbolEntryAddressByIndex(uint32_t Index) const { |
941 | return getAdvancedSymbolEntryAddress( |
942 | CurrentAddress: reinterpret_cast<uintptr_t>(getPointerToSymbolTable()), Distance: Index); |
943 | } |
944 | |
945 | Expected<StringRef> |
946 | XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const { |
947 | const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries(); |
948 | |
949 | if (Index >= NumberOfSymTableEntries) |
950 | return createError(Err: "symbol index " + Twine(Index) + |
951 | " exceeds symbol count " + |
952 | Twine(NumberOfSymTableEntries)); |
953 | |
954 | DataRefImpl SymDRI; |
955 | SymDRI.p = getSymbolEntryAddressByIndex(Index); |
956 | return getSymbolName(Symb: SymDRI); |
957 | } |
958 | |
959 | uint16_t XCOFFObjectFile::getFlags() const { |
960 | return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags; |
961 | } |
962 | |
963 | const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const { |
964 | return is64Bit() ? toSection64(Ref: Sec)->Name : toSection32(Ref: Sec)->Name; |
965 | } |
966 | |
967 | uintptr_t XCOFFObjectFile::() const { |
968 | return reinterpret_cast<uintptr_t>(SectionHeaderTable); |
969 | } |
970 | |
971 | int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const { |
972 | return is64Bit() ? toSection64(Ref: Sec)->Flags : toSection32(Ref: Sec)->Flags; |
973 | } |
974 | |
975 | XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object) |
976 | : ObjectFile(Type, Object) { |
977 | assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64); |
978 | } |
979 | |
980 | ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const { |
981 | assert(is64Bit() && "64-bit interface called for non 64-bit file." ); |
982 | const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64(); |
983 | return ArrayRef<XCOFFSectionHeader64>(TablePtr, |
984 | TablePtr + getNumberOfSections()); |
985 | } |
986 | |
987 | ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const { |
988 | assert(!is64Bit() && "32-bit interface called for non 32-bit file." ); |
989 | const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32(); |
990 | return ArrayRef<XCOFFSectionHeader32>(TablePtr, |
991 | TablePtr + getNumberOfSections()); |
992 | } |
993 | |
994 | // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO |
995 | // section header contains the actual count of relocation entries in the s_paddr |
996 | // field. STYP_OVRFLO headers contain the section index of their corresponding |
997 | // sections as their raw "NumberOfRelocations" field value. |
998 | template <typename T> |
999 | Expected<uint32_t> XCOFFObjectFile::( |
1000 | const XCOFFSectionHeader<T> &Sec) const { |
1001 | const T &Section = static_cast<const T &>(Sec); |
1002 | if (is64Bit()) |
1003 | return Section.NumberOfRelocations; |
1004 | |
1005 | uint16_t SectionIndex = &Section - sectionHeaderTable<T>() + 1; |
1006 | if (Section.NumberOfRelocations < XCOFF::RelocOverflow) |
1007 | return Section.NumberOfRelocations; |
1008 | for (const auto &Sec : sections32()) { |
1009 | if (Sec.Flags == XCOFF::STYP_OVRFLO && |
1010 | Sec.NumberOfRelocations == SectionIndex) |
1011 | return Sec.PhysicalAddress; |
1012 | } |
1013 | return errorCodeToError(EC: object_error::parse_failed); |
1014 | } |
1015 | |
1016 | template <typename Shdr, typename Reloc> |
1017 | Expected<ArrayRef<Reloc>> XCOFFObjectFile::relocations(const Shdr &Sec) const { |
1018 | uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader), |
1019 | Sec.FileOffsetToRelocationInfo); |
1020 | auto NumRelocEntriesOrErr = getNumberOfRelocationEntries(Sec); |
1021 | if (Error E = NumRelocEntriesOrErr.takeError()) |
1022 | return std::move(E); |
1023 | |
1024 | uint32_t NumRelocEntries = NumRelocEntriesOrErr.get(); |
1025 | static_assert((sizeof(Reloc) == XCOFF::RelocationSerializationSize64 || |
1026 | sizeof(Reloc) == XCOFF::RelocationSerializationSize32), |
1027 | "Relocation structure is incorrect" ); |
1028 | auto RelocationOrErr = |
1029 | getObject<Reloc>(Data, reinterpret_cast<void *>(RelocAddr), |
1030 | NumRelocEntries * sizeof(Reloc)); |
1031 | if (!RelocationOrErr) |
1032 | return createError( |
1033 | toString(RelocationOrErr.takeError()) + ": relocations with offset 0x" + |
1034 | Twine::utohexstr(Val: Sec.FileOffsetToRelocationInfo) + " and size 0x" + |
1035 | Twine::utohexstr(Val: NumRelocEntries * sizeof(Reloc)) + |
1036 | " go past the end of the file" ); |
1037 | |
1038 | const Reloc *StartReloc = RelocationOrErr.get(); |
1039 | |
1040 | return ArrayRef<Reloc>(StartReloc, StartReloc + NumRelocEntries); |
1041 | } |
1042 | |
1043 | template <typename ExceptEnt> |
1044 | Expected<ArrayRef<ExceptEnt>> XCOFFObjectFile::getExceptionEntries() const { |
1045 | assert((is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry64)) || |
1046 | (!is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry32))); |
1047 | |
1048 | Expected<uintptr_t> ExceptionSectOrErr = |
1049 | getSectionFileOffsetToRawData(SectType: XCOFF::STYP_EXCEPT); |
1050 | if (!ExceptionSectOrErr) |
1051 | return ExceptionSectOrErr.takeError(); |
1052 | |
1053 | DataRefImpl DRI = getSectionByType(SectType: XCOFF::STYP_EXCEPT); |
1054 | if (DRI.p == 0) |
1055 | return ArrayRef<ExceptEnt>(); |
1056 | |
1057 | ExceptEnt *ExceptEntStart = |
1058 | reinterpret_cast<ExceptEnt *>(*ExceptionSectOrErr); |
1059 | return ArrayRef<ExceptEnt>( |
1060 | ExceptEntStart, ExceptEntStart + getSectionSize(Sec: DRI) / sizeof(ExceptEnt)); |
1061 | } |
1062 | |
1063 | template Expected<ArrayRef<ExceptionSectionEntry32>> |
1064 | XCOFFObjectFile::getExceptionEntries() const; |
1065 | template Expected<ArrayRef<ExceptionSectionEntry64>> |
1066 | XCOFFObjectFile::getExceptionEntries() const; |
1067 | |
1068 | Expected<XCOFFStringTable> |
1069 | XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) { |
1070 | // If there is a string table, then the buffer must contain at least 4 bytes |
1071 | // for the string table's size. Not having a string table is not an error. |
1072 | if (Error E = Binary::checkOffset( |
1073 | M: Obj->Data, Addr: reinterpret_cast<uintptr_t>(Obj->base() + Offset), Size: 4)) { |
1074 | consumeError(Err: std::move(E)); |
1075 | return XCOFFStringTable{.Size: 0, .Data: nullptr}; |
1076 | } |
1077 | |
1078 | // Read the size out of the buffer. |
1079 | uint32_t Size = support::endian::read32be(P: Obj->base() + Offset); |
1080 | |
1081 | // If the size is less then 4, then the string table is just a size and no |
1082 | // string data. |
1083 | if (Size <= 4) |
1084 | return XCOFFStringTable{.Size: 4, .Data: nullptr}; |
1085 | |
1086 | auto StringTableOrErr = |
1087 | getObject<char>(M: Obj->Data, Ptr: Obj->base() + Offset, Size); |
1088 | if (!StringTableOrErr) |
1089 | return createError(Err: toString(E: StringTableOrErr.takeError()) + |
1090 | ": string table with offset 0x" + |
1091 | Twine::utohexstr(Val: Offset) + " and size 0x" + |
1092 | Twine::utohexstr(Val: Size) + |
1093 | " goes past the end of the file" ); |
1094 | |
1095 | const char *StringTablePtr = StringTableOrErr.get(); |
1096 | if (StringTablePtr[Size - 1] != '\0') |
1097 | return errorCodeToError(EC: object_error::string_table_non_null_end); |
1098 | |
1099 | return XCOFFStringTable{.Size: Size, .Data: StringTablePtr}; |
1100 | } |
1101 | |
1102 | // This function returns the import file table. Each entry in the import file |
1103 | // table consists of: "path_name\0base_name\0archive_member_name\0". |
1104 | Expected<StringRef> XCOFFObjectFile::getImportFileTable() const { |
1105 | Expected<uintptr_t> LoaderSectionAddrOrError = |
1106 | getSectionFileOffsetToRawData(SectType: XCOFF::STYP_LOADER); |
1107 | if (!LoaderSectionAddrOrError) |
1108 | return LoaderSectionAddrOrError.takeError(); |
1109 | |
1110 | uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get(); |
1111 | if (!LoaderSectionAddr) |
1112 | return StringRef(); |
1113 | |
1114 | uint64_t OffsetToImportFileTable = 0; |
1115 | uint64_t LengthOfImportFileTable = 0; |
1116 | if (is64Bit()) { |
1117 | const LoaderSectionHeader64 *LoaderSec64 = |
1118 | viewAs<LoaderSectionHeader64>(in: LoaderSectionAddr); |
1119 | OffsetToImportFileTable = LoaderSec64->OffsetToImpid; |
1120 | LengthOfImportFileTable = LoaderSec64->LengthOfImpidStrTbl; |
1121 | } else { |
1122 | const LoaderSectionHeader32 *LoaderSec32 = |
1123 | viewAs<LoaderSectionHeader32>(in: LoaderSectionAddr); |
1124 | OffsetToImportFileTable = LoaderSec32->OffsetToImpid; |
1125 | LengthOfImportFileTable = LoaderSec32->LengthOfImpidStrTbl; |
1126 | } |
1127 | |
1128 | auto ImportTableOrErr = getObject<char>( |
1129 | M: Data, |
1130 | Ptr: reinterpret_cast<void *>(LoaderSectionAddr + OffsetToImportFileTable), |
1131 | Size: LengthOfImportFileTable); |
1132 | if (!ImportTableOrErr) |
1133 | return createError( |
1134 | Err: toString(E: ImportTableOrErr.takeError()) + |
1135 | ": import file table with offset 0x" + |
1136 | Twine::utohexstr(Val: LoaderSectionAddr + OffsetToImportFileTable) + |
1137 | " and size 0x" + Twine::utohexstr(Val: LengthOfImportFileTable) + |
1138 | " goes past the end of the file" ); |
1139 | |
1140 | const char *ImportTablePtr = ImportTableOrErr.get(); |
1141 | if (ImportTablePtr[LengthOfImportFileTable - 1] != '\0') |
1142 | return createError( |
1143 | Err: ": import file name table with offset 0x" + |
1144 | Twine::utohexstr(Val: LoaderSectionAddr + OffsetToImportFileTable) + |
1145 | " and size 0x" + Twine::utohexstr(Val: LengthOfImportFileTable) + |
1146 | " must end with a null terminator" ); |
1147 | |
1148 | return StringRef(ImportTablePtr, LengthOfImportFileTable); |
1149 | } |
1150 | |
1151 | Expected<std::unique_ptr<XCOFFObjectFile>> |
1152 | XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) { |
1153 | // Can't use std::make_unique because of the private constructor. |
1154 | std::unique_ptr<XCOFFObjectFile> Obj; |
1155 | Obj.reset(p: new XCOFFObjectFile(Type, MBR)); |
1156 | |
1157 | uint64_t CurOffset = 0; |
1158 | const auto *Base = Obj->base(); |
1159 | MemoryBufferRef Data = Obj->Data; |
1160 | |
1161 | // Parse file header. |
1162 | auto = |
1163 | getObject<void>(M: Data, Ptr: Base + CurOffset, Size: Obj->getFileHeaderSize()); |
1164 | if (Error E = FileHeaderOrErr.takeError()) |
1165 | return std::move(E); |
1166 | Obj->FileHeader = FileHeaderOrErr.get(); |
1167 | |
1168 | CurOffset += Obj->getFileHeaderSize(); |
1169 | |
1170 | if (Obj->getOptionalHeaderSize()) { |
1171 | auto = |
1172 | getObject<void>(M: Data, Ptr: Base + CurOffset, Size: Obj->getOptionalHeaderSize()); |
1173 | if (Error E = AuxiliaryHeaderOrErr.takeError()) |
1174 | return std::move(E); |
1175 | Obj->AuxiliaryHeader = AuxiliaryHeaderOrErr.get(); |
1176 | } |
1177 | |
1178 | CurOffset += Obj->getOptionalHeaderSize(); |
1179 | |
1180 | // Parse the section header table if it is present. |
1181 | if (Obj->getNumberOfSections()) { |
1182 | uint64_t = |
1183 | Obj->getNumberOfSections() * Obj->getSectionHeaderSize(); |
1184 | auto = |
1185 | getObject<void>(M: Data, Ptr: Base + CurOffset, Size: SectionHeadersSize); |
1186 | if (!SecHeadersOrErr) |
1187 | return createError(Err: toString(E: SecHeadersOrErr.takeError()) + |
1188 | ": section headers with offset 0x" + |
1189 | Twine::utohexstr(Val: CurOffset) + " and size 0x" + |
1190 | Twine::utohexstr(Val: SectionHeadersSize) + |
1191 | " go past the end of the file" ); |
1192 | |
1193 | Obj->SectionHeaderTable = SecHeadersOrErr.get(); |
1194 | } |
1195 | |
1196 | const uint32_t NumberOfSymbolTableEntries = |
1197 | Obj->getNumberOfSymbolTableEntries(); |
1198 | |
1199 | // If there is no symbol table we are done parsing the memory buffer. |
1200 | if (NumberOfSymbolTableEntries == 0) |
1201 | return std::move(Obj); |
1202 | |
1203 | // Parse symbol table. |
1204 | CurOffset = Obj->is64Bit() ? Obj->getSymbolTableOffset64() |
1205 | : Obj->getSymbolTableOffset32(); |
1206 | const uint64_t SymbolTableSize = |
1207 | static_cast<uint64_t>(XCOFF::SymbolTableEntrySize) * |
1208 | NumberOfSymbolTableEntries; |
1209 | auto SymTableOrErr = |
1210 | getObject<void *>(M: Data, Ptr: Base + CurOffset, Size: SymbolTableSize); |
1211 | if (!SymTableOrErr) |
1212 | return createError( |
1213 | Err: toString(E: SymTableOrErr.takeError()) + ": symbol table with offset 0x" + |
1214 | Twine::utohexstr(Val: CurOffset) + " and size 0x" + |
1215 | Twine::utohexstr(Val: SymbolTableSize) + " goes past the end of the file" ); |
1216 | |
1217 | Obj->SymbolTblPtr = SymTableOrErr.get(); |
1218 | CurOffset += SymbolTableSize; |
1219 | |
1220 | // Parse String table. |
1221 | Expected<XCOFFStringTable> StringTableOrErr = |
1222 | parseStringTable(Obj: Obj.get(), Offset: CurOffset); |
1223 | if (Error E = StringTableOrErr.takeError()) |
1224 | return std::move(E); |
1225 | Obj->StringTable = StringTableOrErr.get(); |
1226 | |
1227 | return std::move(Obj); |
1228 | } |
1229 | |
1230 | Expected<std::unique_ptr<ObjectFile>> |
1231 | ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef, |
1232 | unsigned FileType) { |
1233 | return XCOFFObjectFile::create(Type: FileType, MBR: MemBufRef); |
1234 | } |
1235 | |
1236 | std::optional<StringRef> XCOFFObjectFile::tryGetCPUName() const { |
1237 | return StringRef("future" ); |
1238 | } |
1239 | |
1240 | Expected<bool> XCOFFSymbolRef::isFunction() const { |
1241 | if (!isCsectSymbol()) |
1242 | return false; |
1243 | |
1244 | if (getSymbolType() & FunctionSym) |
1245 | return true; |
1246 | |
1247 | Expected<XCOFFCsectAuxRef> ExpCsectAuxEnt = getXCOFFCsectAuxRef(); |
1248 | if (!ExpCsectAuxEnt) |
1249 | return ExpCsectAuxEnt.takeError(); |
1250 | |
1251 | const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get(); |
1252 | |
1253 | if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR && |
1254 | CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_GL) |
1255 | return false; |
1256 | |
1257 | // A function definition should not be a common type symbol or an external |
1258 | // symbol. |
1259 | if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM || |
1260 | CsectAuxRef.getSymbolType() == XCOFF::XTY_ER) |
1261 | return false; |
1262 | |
1263 | // If the next symbol is an XTY_LD type symbol with the same address, this |
1264 | // XTY_SD symbol is not a function. Otherwise this is a function symbol for |
1265 | // -ffunction-sections. |
1266 | if (CsectAuxRef.getSymbolType() == XCOFF::XTY_SD) { |
1267 | // If this is a csect with size 0, it won't be a function definition. |
1268 | // This is used to work around the fact that LLVM always generates below |
1269 | // symbol for -ffunction-sections: |
1270 | // m 0x00000000 .text 1 unamex **No Symbol** |
1271 | // a4 0x00000000 0 0 SD PR 0 0 |
1272 | // FIXME: remove or replace this meaningless symbol. |
1273 | if (getSize() == 0) |
1274 | return false; |
1275 | |
1276 | xcoff_symbol_iterator NextIt(this); |
1277 | // If this is the last main symbol table entry, there won't be an XTY_LD |
1278 | // type symbol below. |
1279 | if (++NextIt == getObject()->symbol_end()) |
1280 | return true; |
1281 | |
1282 | if (cantFail(ValOrErr: getAddress()) != cantFail(ValOrErr: NextIt->getAddress())) |
1283 | return true; |
1284 | |
1285 | // Check next symbol is XTY_LD. If so, this symbol is not a function. |
1286 | Expected<XCOFFCsectAuxRef> NextCsectAuxEnt = NextIt->getXCOFFCsectAuxRef(); |
1287 | if (!NextCsectAuxEnt) |
1288 | return NextCsectAuxEnt.takeError(); |
1289 | |
1290 | if (NextCsectAuxEnt.get().getSymbolType() == XCOFF::XTY_LD) |
1291 | return false; |
1292 | |
1293 | return true; |
1294 | } |
1295 | |
1296 | if (CsectAuxRef.getSymbolType() == XCOFF::XTY_LD) |
1297 | return true; |
1298 | |
1299 | return createError( |
1300 | Err: "symbol csect aux entry with index " + |
1301 | Twine(getObject()->getSymbolIndex(SymbolEntPtr: CsectAuxRef.getEntryAddress())) + |
1302 | " has invalid symbol type " + |
1303 | Twine::utohexstr(Val: CsectAuxRef.getSymbolType())); |
1304 | } |
1305 | |
1306 | bool XCOFFSymbolRef::isCsectSymbol() const { |
1307 | XCOFF::StorageClass SC = getStorageClass(); |
1308 | return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT || |
1309 | SC == XCOFF::C_HIDEXT); |
1310 | } |
1311 | |
1312 | Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const { |
1313 | assert(isCsectSymbol() && |
1314 | "Calling csect symbol interface with a non-csect symbol." ); |
1315 | |
1316 | uint8_t NumberOfAuxEntries = getNumberOfAuxEntries(); |
1317 | |
1318 | Expected<StringRef> NameOrErr = getName(); |
1319 | if (auto Err = NameOrErr.takeError()) |
1320 | return std::move(Err); |
1321 | |
1322 | uint32_t SymbolIdx = getObject()->getSymbolIndex(SymbolEntPtr: getEntryAddress()); |
1323 | if (!NumberOfAuxEntries) { |
1324 | return createError(Err: "csect symbol \"" + *NameOrErr + "\" with index " + |
1325 | Twine(SymbolIdx) + " contains no auxiliary entry" ); |
1326 | } |
1327 | |
1328 | if (!getObject()->is64Bit()) { |
1329 | // In XCOFF32, the csect auxilliary entry is always the last auxiliary |
1330 | // entry for the symbol. |
1331 | uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress( |
1332 | CurrentAddress: getEntryAddress(), Distance: NumberOfAuxEntries); |
1333 | return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt32>(in: AuxAddr)); |
1334 | } |
1335 | |
1336 | // XCOFF64 uses SymbolAuxType to identify the auxiliary entry type. |
1337 | // We need to iterate through all the auxiliary entries to find it. |
1338 | for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) { |
1339 | uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress( |
1340 | CurrentAddress: getEntryAddress(), Distance: Index); |
1341 | if (*getObject()->getSymbolAuxType(AuxEntryAddress: AuxAddr) == |
1342 | XCOFF::SymbolAuxType::AUX_CSECT) { |
1343 | #ifndef NDEBUG |
1344 | getObject()->checkSymbolEntryPointer(AuxAddr); |
1345 | #endif |
1346 | return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(in: AuxAddr)); |
1347 | } |
1348 | } |
1349 | |
1350 | return createError( |
1351 | Err: "a csect auxiliary entry has not been found for symbol \"" + *NameOrErr + |
1352 | "\" with index " + Twine(SymbolIdx)); |
1353 | } |
1354 | |
1355 | Expected<StringRef> XCOFFSymbolRef::getName() const { |
1356 | // A storage class value with the high-order bit on indicates that the name is |
1357 | // a symbolic debugger stabstring. |
1358 | if (getStorageClass() & 0x80) |
1359 | return StringRef("Unimplemented Debug Name" ); |
1360 | |
1361 | if (!getObject()->is64Bit()) { |
1362 | if (getSymbol32()->NameInStrTbl.Magic != |
1363 | XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC) |
1364 | return generateXCOFFFixedNameStringRef(Name: getSymbol32()->SymbolName); |
1365 | |
1366 | return getObject()->getStringTableEntry(Offset: getSymbol32()->NameInStrTbl.Offset); |
1367 | } |
1368 | |
1369 | return getObject()->getStringTableEntry(Offset: getSymbol64()->Offset); |
1370 | } |
1371 | |
1372 | // Explicitly instantiate template classes. |
1373 | template struct XCOFFSectionHeader<XCOFFSectionHeader32>; |
1374 | template struct XCOFFSectionHeader<XCOFFSectionHeader64>; |
1375 | |
1376 | template struct XCOFFRelocation<llvm::support::ubig32_t>; |
1377 | template struct XCOFFRelocation<llvm::support::ubig64_t>; |
1378 | |
1379 | template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation64>> |
1380 | llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader64, |
1381 | llvm::object::XCOFFRelocation64>( |
1382 | llvm::object::XCOFFSectionHeader64 const &) const; |
1383 | template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation32>> |
1384 | llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader32, |
1385 | llvm::object::XCOFFRelocation32>( |
1386 | llvm::object::XCOFFSectionHeader32 const &) const; |
1387 | |
1388 | bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) { |
1389 | if (Bytes.size() < 4) |
1390 | return false; |
1391 | |
1392 | return support::endian::read32be(P: Bytes.data()) == 0; |
1393 | } |
1394 | |
1395 | #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X)) |
1396 | #define GETVALUEWITHMASKSHIFT(X, S) \ |
1397 | ((Data & (TracebackTable::X)) >> (TracebackTable::S)) |
1398 | |
1399 | Expected<TBVectorExt> TBVectorExt::create(StringRef TBvectorStrRef) { |
1400 | Error Err = Error::success(); |
1401 | TBVectorExt TBTVecExt(TBvectorStrRef, Err); |
1402 | if (Err) |
1403 | return std::move(Err); |
1404 | return TBTVecExt; |
1405 | } |
1406 | |
1407 | TBVectorExt::TBVectorExt(StringRef TBvectorStrRef, Error &Err) { |
1408 | const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data()); |
1409 | Data = support::endian::read16be(P: Ptr); |
1410 | uint32_t VecParmsTypeValue = support::endian::read32be(P: Ptr + 2); |
1411 | unsigned ParmsNum = |
1412 | GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, NumberOfVectorParmsShift); |
1413 | |
1414 | ErrorAsOutParameter EAO(&Err); |
1415 | Expected<SmallString<32>> VecParmsTypeOrError = |
1416 | parseVectorParmsType(Value: VecParmsTypeValue, ParmsNum); |
1417 | if (!VecParmsTypeOrError) |
1418 | Err = VecParmsTypeOrError.takeError(); |
1419 | else |
1420 | VecParmsInfo = VecParmsTypeOrError.get(); |
1421 | } |
1422 | |
1423 | uint8_t TBVectorExt::getNumberOfVRSaved() const { |
1424 | return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift); |
1425 | } |
1426 | |
1427 | bool TBVectorExt::isVRSavedOnStack() const { |
1428 | return GETVALUEWITHMASK(IsVRSavedOnStackMask); |
1429 | } |
1430 | |
1431 | bool TBVectorExt::hasVarArgs() const { |
1432 | return GETVALUEWITHMASK(HasVarArgsMask); |
1433 | } |
1434 | |
1435 | uint8_t TBVectorExt::getNumberOfVectorParms() const { |
1436 | return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, |
1437 | NumberOfVectorParmsShift); |
1438 | } |
1439 | |
1440 | bool TBVectorExt::hasVMXInstruction() const { |
1441 | return GETVALUEWITHMASK(HasVMXInstructionMask); |
1442 | } |
1443 | #undef GETVALUEWITHMASK |
1444 | #undef GETVALUEWITHMASKSHIFT |
1445 | |
1446 | Expected<XCOFFTracebackTable> |
1447 | XCOFFTracebackTable::create(const uint8_t *Ptr, uint64_t &Size, bool Is64Bit) { |
1448 | Error Err = Error::success(); |
1449 | XCOFFTracebackTable TBT(Ptr, Size, Err, Is64Bit); |
1450 | if (Err) |
1451 | return std::move(Err); |
1452 | return TBT; |
1453 | } |
1454 | |
1455 | XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size, |
1456 | Error &Err, bool Is64Bit) |
1457 | : TBPtr(Ptr), Is64BitObj(Is64Bit) { |
1458 | ErrorAsOutParameter EAO(&Err); |
1459 | DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false, |
1460 | /*AddressSize=*/0); |
1461 | DataExtractor::Cursor Cur(/*Offset=*/0); |
1462 | |
1463 | // Skip 8 bytes of mandatory fields. |
1464 | DE.getU64(C&: Cur); |
1465 | |
1466 | unsigned FixedParmsNum = getNumberOfFixedParms(); |
1467 | unsigned FloatingParmsNum = getNumberOfFPParms(); |
1468 | uint32_t ParamsTypeValue = 0; |
1469 | |
1470 | // Begin to parse optional fields. |
1471 | if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) |
1472 | ParamsTypeValue = DE.getU32(C&: Cur); |
1473 | |
1474 | if (Cur && hasTraceBackTableOffset()) |
1475 | TraceBackTableOffset = DE.getU32(C&: Cur); |
1476 | |
1477 | if (Cur && isInterruptHandler()) |
1478 | HandlerMask = DE.getU32(C&: Cur); |
1479 | |
1480 | if (Cur && hasControlledStorage()) { |
1481 | NumOfCtlAnchors = DE.getU32(C&: Cur); |
1482 | if (Cur && NumOfCtlAnchors) { |
1483 | SmallVector<uint32_t, 8> Disp; |
1484 | Disp.reserve(N: *NumOfCtlAnchors); |
1485 | for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I) |
1486 | Disp.push_back(Elt: DE.getU32(C&: Cur)); |
1487 | if (Cur) |
1488 | ControlledStorageInfoDisp = std::move(Disp); |
1489 | } |
1490 | } |
1491 | |
1492 | if (Cur && isFuncNamePresent()) { |
1493 | uint16_t FunctionNameLen = DE.getU16(C&: Cur); |
1494 | if (Cur) |
1495 | FunctionName = DE.getBytes(C&: Cur, Length: FunctionNameLen); |
1496 | } |
1497 | |
1498 | if (Cur && isAllocaUsed()) |
1499 | AllocaRegister = DE.getU8(C&: Cur); |
1500 | |
1501 | unsigned VectorParmsNum = 0; |
1502 | if (Cur && hasVectorInfo()) { |
1503 | StringRef VectorExtRef = DE.getBytes(C&: Cur, Length: 6); |
1504 | if (Cur) { |
1505 | Expected<TBVectorExt> TBVecExtOrErr = TBVectorExt::create(TBvectorStrRef: VectorExtRef); |
1506 | if (!TBVecExtOrErr) { |
1507 | Err = TBVecExtOrErr.takeError(); |
1508 | return; |
1509 | } |
1510 | VecExt = TBVecExtOrErr.get(); |
1511 | VectorParmsNum = VecExt->getNumberOfVectorParms(); |
1512 | // Skip two bytes of padding after vector info. |
1513 | DE.skip(C&: Cur, Length: 2); |
1514 | } |
1515 | } |
1516 | |
1517 | // As long as there is no fixed-point or floating-point parameter, this |
1518 | // field remains not present even when hasVectorInfo gives true and |
1519 | // indicates the presence of vector parameters. |
1520 | if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) { |
1521 | Expected<SmallString<32>> ParmsTypeOrError = |
1522 | hasVectorInfo() |
1523 | ? parseParmsTypeWithVecInfo(Value: ParamsTypeValue, FixedParmsNum, |
1524 | FloatingParmsNum, VectorParmsNum) |
1525 | : parseParmsType(Value: ParamsTypeValue, FixedParmsNum, FloatingParmsNum); |
1526 | |
1527 | if (!ParmsTypeOrError) { |
1528 | Err = ParmsTypeOrError.takeError(); |
1529 | return; |
1530 | } |
1531 | ParmsType = ParmsTypeOrError.get(); |
1532 | } |
1533 | |
1534 | if (Cur && hasExtensionTable()) { |
1535 | ExtensionTable = DE.getU8(C&: Cur); |
1536 | |
1537 | if (*ExtensionTable & ExtendedTBTableFlag::TB_EH_INFO) { |
1538 | // eh_info displacement must be 4-byte aligned. |
1539 | Cur.seek(NewOffSet: alignTo(Value: Cur.tell(), Align: 4)); |
1540 | EhInfoDisp = Is64BitObj ? DE.getU64(C&: Cur) : DE.getU32(C&: Cur); |
1541 | } |
1542 | } |
1543 | if (!Cur) |
1544 | Err = Cur.takeError(); |
1545 | |
1546 | Size = Cur.tell(); |
1547 | } |
1548 | |
1549 | #define GETBITWITHMASK(P, X) \ |
1550 | (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) |
1551 | #define GETBITWITHMASKSHIFT(P, X, S) \ |
1552 | ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >> \ |
1553 | (TracebackTable::S)) |
1554 | |
1555 | uint8_t XCOFFTracebackTable::getVersion() const { |
1556 | return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift); |
1557 | } |
1558 | |
1559 | uint8_t XCOFFTracebackTable::getLanguageID() const { |
1560 | return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift); |
1561 | } |
1562 | |
1563 | bool XCOFFTracebackTable::isGlobalLinkage() const { |
1564 | return GETBITWITHMASK(0, IsGlobaLinkageMask); |
1565 | } |
1566 | |
1567 | bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const { |
1568 | return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask); |
1569 | } |
1570 | |
1571 | bool XCOFFTracebackTable::hasTraceBackTableOffset() const { |
1572 | return GETBITWITHMASK(0, HasTraceBackTableOffsetMask); |
1573 | } |
1574 | |
1575 | bool XCOFFTracebackTable::isInternalProcedure() const { |
1576 | return GETBITWITHMASK(0, IsInternalProcedureMask); |
1577 | } |
1578 | |
1579 | bool XCOFFTracebackTable::hasControlledStorage() const { |
1580 | return GETBITWITHMASK(0, HasControlledStorageMask); |
1581 | } |
1582 | |
1583 | bool XCOFFTracebackTable::isTOCless() const { |
1584 | return GETBITWITHMASK(0, IsTOClessMask); |
1585 | } |
1586 | |
1587 | bool XCOFFTracebackTable::isFloatingPointPresent() const { |
1588 | return GETBITWITHMASK(0, IsFloatingPointPresentMask); |
1589 | } |
1590 | |
1591 | bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const { |
1592 | return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask); |
1593 | } |
1594 | |
1595 | bool XCOFFTracebackTable::isInterruptHandler() const { |
1596 | return GETBITWITHMASK(0, IsInterruptHandlerMask); |
1597 | } |
1598 | |
1599 | bool XCOFFTracebackTable::isFuncNamePresent() const { |
1600 | return GETBITWITHMASK(0, IsFunctionNamePresentMask); |
1601 | } |
1602 | |
1603 | bool XCOFFTracebackTable::isAllocaUsed() const { |
1604 | return GETBITWITHMASK(0, IsAllocaUsedMask); |
1605 | } |
1606 | |
1607 | uint8_t XCOFFTracebackTable::getOnConditionDirective() const { |
1608 | return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask, |
1609 | OnConditionDirectiveShift); |
1610 | } |
1611 | |
1612 | bool XCOFFTracebackTable::isCRSaved() const { |
1613 | return GETBITWITHMASK(0, IsCRSavedMask); |
1614 | } |
1615 | |
1616 | bool XCOFFTracebackTable::isLRSaved() const { |
1617 | return GETBITWITHMASK(0, IsLRSavedMask); |
1618 | } |
1619 | |
1620 | bool XCOFFTracebackTable::isBackChainStored() const { |
1621 | return GETBITWITHMASK(4, IsBackChainStoredMask); |
1622 | } |
1623 | |
1624 | bool XCOFFTracebackTable::isFixup() const { |
1625 | return GETBITWITHMASK(4, IsFixupMask); |
1626 | } |
1627 | |
1628 | uint8_t XCOFFTracebackTable::() const { |
1629 | return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift); |
1630 | } |
1631 | |
1632 | bool XCOFFTracebackTable::hasExtensionTable() const { |
1633 | return GETBITWITHMASK(4, HasExtensionTableMask); |
1634 | } |
1635 | |
1636 | bool XCOFFTracebackTable::hasVectorInfo() const { |
1637 | return GETBITWITHMASK(4, HasVectorInfoMask); |
1638 | } |
1639 | |
1640 | uint8_t XCOFFTracebackTable::() const { |
1641 | return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift); |
1642 | } |
1643 | |
1644 | uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const { |
1645 | return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask, |
1646 | NumberOfFixedParmsShift); |
1647 | } |
1648 | |
1649 | uint8_t XCOFFTracebackTable::getNumberOfFPParms() const { |
1650 | return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask, |
1651 | NumberOfFloatingPointParmsShift); |
1652 | } |
1653 | |
1654 | bool XCOFFTracebackTable::hasParmsOnStack() const { |
1655 | return GETBITWITHMASK(4, HasParmsOnStackMask); |
1656 | } |
1657 | |
1658 | #undef GETBITWITHMASK |
1659 | #undef GETBITWITHMASKSHIFT |
1660 | } // namespace object |
1661 | } // namespace llvm |
1662 | |