Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 1 | //===- DWARFListTable.cpp ---------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/DebugInfo/DWARF/DWARFListTable.h" |
| 10 | #include "llvm/BinaryFormat/Dwarf.h" |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Errc.h" |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Error.h" |
| 13 | #include "llvm/Support/Format.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 18 | Error DWARFListTableHeader::extract(DWARFDataExtractor Data, |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 19 | uint64_t *OffsetPtr) { |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 20 | HeaderOffset = *OffsetPtr; |
Pavel Labath | 164e2c8 | 2020-02-25 15:17:57 +0100 | [diff] [blame] | 21 | Error Err = Error::success(); |
| 22 | |
| 23 | std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err); |
| 24 | if (Err) |
| 25 | return createStringError( |
| 26 | errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s", |
| 27 | SectionName.data(), HeaderOffset, toString(std::move(Err)).c_str()); |
| 28 | |
| 29 | uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4; |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 30 | uint64_t FullLength = |
| 31 | HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
| 32 | assert(FullLength == length()); |
| 33 | if (FullLength < getHeaderSize(Format)) |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 34 | return createStringError(errc::invalid_argument, |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 35 | "%s table at offset 0x%" PRIx64 |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 36 | " has too small length (0x%" PRIx64 |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 37 | ") to contain a complete header", |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 38 | SectionName.data(), HeaderOffset, FullLength); |
| 39 | uint64_t End = HeaderOffset + FullLength; |
| 40 | if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength)) |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 41 | return createStringError(errc::invalid_argument, |
| 42 | "section is not large enough to contain a %s table " |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 43 | "of length 0x%" PRIx64 " at offset 0x%" PRIx64, |
| 44 | SectionName.data(), FullLength, HeaderOffset); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 45 | |
| 46 | HeaderData.Version = Data.getU16(OffsetPtr); |
| 47 | HeaderData.AddrSize = Data.getU8(OffsetPtr); |
| 48 | HeaderData.SegSize = Data.getU8(OffsetPtr); |
| 49 | HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr); |
| 50 | |
| 51 | // Perform basic validation of the remaining header fields. |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 52 | if (HeaderData.Version != 5) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 53 | return createStringError(errc::invalid_argument, |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 54 | "unrecognised %s table version %" PRIu16 |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 55 | " in table at offset 0x%" PRIx64, |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 56 | SectionName.data(), HeaderData.Version, HeaderOffset); |
| 57 | if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 58 | return createStringError(errc::not_supported, |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 59 | "%s table at offset 0x%" PRIx64 |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 60 | " has unsupported address size %" PRIu8, |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 61 | SectionName.data(), HeaderOffset, HeaderData.AddrSize); |
Wolfgang Pieb | f39a9bb | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 62 | if (HeaderData.SegSize != 0) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 63 | return createStringError(errc::not_supported, |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 64 | "%s table at offset 0x%" PRIx64 |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 65 | " has unsupported segment selector size %" PRIu8, |
| 66 | SectionName.data(), HeaderOffset, HeaderData.SegSize); |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 67 | if (End < HeaderOffset + getHeaderSize(Format) + |
| 68 | HeaderData.OffsetEntryCount * OffsetByteSize) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 69 | return createStringError(errc::invalid_argument, |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 70 | "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32 |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 71 | ") than there is space for", |
| 72 | SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount); |
| 73 | Data.setAddressSize(HeaderData.AddrSize); |
| 74 | for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 75 | Offsets.push_back(Data.getRelocatedValue(OffsetByteSize, OffsetPtr)); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 76 | return Error::success(); |
| 77 | } |
| 78 | |
| 79 | void DWARFListTableHeader::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { |
| 80 | if (DumpOpts.Verbose) |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 81 | OS << format("0x%8.8" PRIx64 ": ", HeaderOffset); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 82 | OS << format( |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 83 | "%s list header: length = 0x%8.8" PRIx64 ", version = 0x%4.4" PRIx16 ", " |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 84 | "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8 |
| 85 | ", offset_entry_count = " |
| 86 | "0x%8.8" PRIx32 "\n", |
| 87 | ListTypeString.data(), HeaderData.Length, HeaderData.Version, |
| 88 | HeaderData.AddrSize, HeaderData.SegSize, HeaderData.OffsetEntryCount); |
| 89 | |
| 90 | if (HeaderData.OffsetEntryCount > 0) { |
| 91 | OS << "offsets: ["; |
| 92 | for (const auto &Off : Offsets) { |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 93 | OS << format("\n0x%8.8" PRIx64, Off); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 94 | if (DumpOpts.Verbose) |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 95 | OS << format(" => 0x%8.8" PRIx64, |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 96 | Off + HeaderOffset + getHeaderSize(Format)); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 97 | } |
| 98 | OS << "\n]\n"; |
| 99 | } |
| 100 | } |
| 101 | |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 102 | uint64_t DWARFListTableHeader::length() const { |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 103 | if (HeaderData.Length == 0) |
| 104 | return 0; |
Igor Kudrin | 991f0fb | 2019-09-05 06:49:05 +0000 | [diff] [blame] | 105 | return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format); |
Wolfgang Pieb | 439801b | 2018-07-23 22:37:17 +0000 | [diff] [blame] | 106 | } |