xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFListTable.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- DWARFListTable.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFListTable.h"
100b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
11*349cc55cSDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
120b57cec5SDimitry Andric #include "llvm/Support/Errc.h"
130b57cec5SDimitry Andric #include "llvm/Support/Error.h"
140b57cec5SDimitry Andric #include "llvm/Support/Format.h"
150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric 
extract(DWARFDataExtractor Data,uint64_t * OffsetPtr)190b57cec5SDimitry Andric Error DWARFListTableHeader::extract(DWARFDataExtractor Data,
208bcb0991SDimitry Andric                                     uint64_t *OffsetPtr) {
210b57cec5SDimitry Andric   HeaderOffset = *OffsetPtr;
225ffd83dbSDimitry Andric   Error Err = Error::success();
235ffd83dbSDimitry Andric 
245ffd83dbSDimitry Andric   std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err);
255ffd83dbSDimitry Andric   if (Err)
265ffd83dbSDimitry Andric     return createStringError(
275ffd83dbSDimitry Andric         errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s",
285ffd83dbSDimitry Andric         SectionName.data(), HeaderOffset, toString(std::move(Err)).c_str());
295ffd83dbSDimitry Andric 
305ffd83dbSDimitry Andric   uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
318bcb0991SDimitry Andric   uint64_t FullLength =
328bcb0991SDimitry Andric       HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
338bcb0991SDimitry Andric   if (FullLength < getHeaderSize(Format))
348bcb0991SDimitry Andric     return createStringError(errc::invalid_argument,
358bcb0991SDimitry Andric                        "%s table at offset 0x%" PRIx64
368bcb0991SDimitry Andric                        " has too small length (0x%" PRIx64
370b57cec5SDimitry Andric                        ") to contain a complete header",
388bcb0991SDimitry Andric                        SectionName.data(), HeaderOffset, FullLength);
395ffd83dbSDimitry Andric   assert(FullLength == length() && "Inconsistent calculation of length.");
408bcb0991SDimitry Andric   uint64_t End = HeaderOffset + FullLength;
418bcb0991SDimitry Andric   if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength))
420b57cec5SDimitry Andric     return createStringError(errc::invalid_argument,
430b57cec5SDimitry Andric                        "section is not large enough to contain a %s table "
448bcb0991SDimitry Andric                        "of length 0x%" PRIx64 " at offset 0x%" PRIx64,
458bcb0991SDimitry Andric                        SectionName.data(), FullLength, HeaderOffset);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   HeaderData.Version = Data.getU16(OffsetPtr);
480b57cec5SDimitry Andric   HeaderData.AddrSize = Data.getU8(OffsetPtr);
490b57cec5SDimitry Andric   HeaderData.SegSize = Data.getU8(OffsetPtr);
500b57cec5SDimitry Andric   HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // Perform basic validation of the remaining header fields.
530b57cec5SDimitry Andric   if (HeaderData.Version != 5)
540b57cec5SDimitry Andric     return createStringError(errc::invalid_argument,
550b57cec5SDimitry Andric                        "unrecognised %s table version %" PRIu16
568bcb0991SDimitry Andric                        " in table at offset 0x%" PRIx64,
570b57cec5SDimitry Andric                        SectionName.data(), HeaderData.Version, HeaderOffset);
58*349cc55cSDimitry Andric   if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
59*349cc55cSDimitry Andric           HeaderData.AddrSize, errc::not_supported,
60*349cc55cSDimitry Andric           "%s table at offset 0x%" PRIx64, SectionName.data(), HeaderOffset))
61*349cc55cSDimitry Andric     return SizeErr;
620b57cec5SDimitry Andric   if (HeaderData.SegSize != 0)
630b57cec5SDimitry Andric     return createStringError(errc::not_supported,
648bcb0991SDimitry Andric                        "%s table at offset 0x%" PRIx64
650b57cec5SDimitry Andric                        " has unsupported segment selector size %" PRIu8,
660b57cec5SDimitry Andric                        SectionName.data(), HeaderOffset, HeaderData.SegSize);
678bcb0991SDimitry Andric   if (End < HeaderOffset + getHeaderSize(Format) +
688bcb0991SDimitry Andric                 HeaderData.OffsetEntryCount * OffsetByteSize)
690b57cec5SDimitry Andric     return createStringError(errc::invalid_argument,
708bcb0991SDimitry Andric         "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32
710b57cec5SDimitry Andric         ") than there is space for",
720b57cec5SDimitry Andric         SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
730b57cec5SDimitry Andric   Data.setAddressSize(HeaderData.AddrSize);
74e8d8bef9SDimitry Andric   *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize;
750b57cec5SDimitry Andric   return Error::success();
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
dump(DataExtractor Data,raw_ostream & OS,DIDumpOptions DumpOpts) const78e8d8bef9SDimitry Andric void DWARFListTableHeader::dump(DataExtractor Data, raw_ostream &OS,
79e8d8bef9SDimitry Andric                                 DIDumpOptions DumpOpts) const {
800b57cec5SDimitry Andric   if (DumpOpts.Verbose)
818bcb0991SDimitry Andric     OS << format("0x%8.8" PRIx64 ": ", HeaderOffset);
825ffd83dbSDimitry Andric   int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
835ffd83dbSDimitry Andric   OS << format("%s list header: length = 0x%0*" PRIx64, ListTypeString.data(),
845ffd83dbSDimitry Andric                OffsetDumpWidth, HeaderData.Length)
855ffd83dbSDimitry Andric      << ", format = " << dwarf::FormatString(Format)
865ffd83dbSDimitry Andric      << format(", version = 0x%4.4" PRIx16 ", addr_size = 0x%2.2" PRIx8
875ffd83dbSDimitry Andric                ", seg_size = 0x%2.2" PRIx8
885ffd83dbSDimitry Andric                ", offset_entry_count = 0x%8.8" PRIx32 "\n",
895ffd83dbSDimitry Andric                HeaderData.Version, HeaderData.AddrSize, HeaderData.SegSize,
905ffd83dbSDimitry Andric                HeaderData.OffsetEntryCount);
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   if (HeaderData.OffsetEntryCount > 0) {
930b57cec5SDimitry Andric     OS << "offsets: [";
94e8d8bef9SDimitry Andric     for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) {
95e8d8bef9SDimitry Andric       auto Off = *getOffsetEntry(Data, I);
965ffd83dbSDimitry Andric       OS << format("\n0x%0*" PRIx64, OffsetDumpWidth, Off);
970b57cec5SDimitry Andric       if (DumpOpts.Verbose)
985ffd83dbSDimitry Andric         OS << format(" => 0x%08" PRIx64,
998bcb0991SDimitry Andric                      Off + HeaderOffset + getHeaderSize(Format));
1000b57cec5SDimitry Andric     }
1010b57cec5SDimitry Andric     OS << "\n]\n";
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
length() const1058bcb0991SDimitry Andric uint64_t DWARFListTableHeader::length() const {
1060b57cec5SDimitry Andric   if (HeaderData.Length == 0)
1070b57cec5SDimitry Andric     return 0;
1088bcb0991SDimitry Andric   return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
1090b57cec5SDimitry Andric }
110