Line data Source code
1 : //===- DWARFListTable.cpp ---------------------------------------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #include "llvm/DebugInfo/DWARF/DWARFListTable.h"
11 : #include "llvm/BinaryFormat/Dwarf.h"
12 : #include "llvm/Support/Errc.h"
13 : #include "llvm/Support/Error.h"
14 : #include "llvm/Support/Format.h"
15 : #include "llvm/Support/raw_ostream.h"
16 :
17 : using namespace llvm;
18 :
19 44 : Error DWARFListTableHeader::extract(DWARFDataExtractor Data,
20 : uint32_t *OffsetPtr) {
21 44 : HeaderOffset = *OffsetPtr;
22 : // Read and verify the length field.
23 : if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, sizeof(uint32_t)))
24 : return createStringError(errc::invalid_argument,
25 : "section is not large enough to contain a "
26 : "%s table length at offset 0x%" PRIx32,
27 4 : SectionName.data(), *OffsetPtr);
28 : // TODO: Add support for DWARF64.
29 42 : HeaderData.Length = Data.getU32(OffsetPtr);
30 42 : if (HeaderData.Length == 0xffffffffu)
31 : return createStringError(errc::not_supported,
32 : "DWARF64 is not supported in %s at offset 0x%" PRIx32,
33 3 : SectionName.data(), HeaderOffset);
34 41 : Format = dwarf::DwarfFormat::DWARF32;
35 41 : if (HeaderData.Length + sizeof(uint32_t) < sizeof(Header))
36 : return createStringError(errc::invalid_argument,
37 : "%s table at offset 0x%" PRIx32
38 : " has too small length (0x%" PRIx32
39 : ") to contain a complete header",
40 1 : SectionName.data(), HeaderOffset, length());
41 40 : uint32_t End = HeaderOffset + length();
42 40 : if (!Data.isValidOffsetForDataOfSize(HeaderOffset, End - HeaderOffset))
43 : return createStringError(errc::invalid_argument,
44 : "section is not large enough to contain a %s table "
45 : "of length 0x%" PRIx32 " at offset 0x%" PRIx32,
46 1 : SectionName.data(), length(), HeaderOffset);
47 :
48 39 : HeaderData.Version = Data.getU16(OffsetPtr);
49 39 : HeaderData.AddrSize = Data.getU8(OffsetPtr);
50 39 : HeaderData.SegSize = Data.getU8(OffsetPtr);
51 39 : HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
52 :
53 : // Perform basic validation of the remaining header fields.
54 39 : if (HeaderData.Version != 5)
55 : return createStringError(errc::invalid_argument,
56 : "unrecognised %s table version %" PRIu16
57 : " in table at offset 0x%" PRIx32,
58 3 : SectionName.data(), HeaderData.Version, HeaderOffset);
59 38 : if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
60 : return createStringError(errc::not_supported,
61 : "%s table at offset 0x%" PRIx32
62 : " has unsupported address size %" PRIu8,
63 3 : SectionName.data(), HeaderOffset, HeaderData.AddrSize);
64 37 : if (HeaderData.SegSize != 0)
65 : return createStringError(errc::not_supported,
66 : "%s table at offset 0x%" PRIx32
67 : " has unsupported segment selector size %" PRIu8,
68 3 : SectionName.data(), HeaderOffset, HeaderData.SegSize);
69 72 : if (End < HeaderOffset + sizeof(HeaderData) +
70 36 : HeaderData.OffsetEntryCount * sizeof(uint32_t))
71 : return createStringError(errc::invalid_argument,
72 : "%s table at offset 0x%" PRIx32 " has more offset entries (%" PRIu32
73 : ") than there is space for",
74 3 : SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
75 : Data.setAddressSize(HeaderData.AddrSize);
76 54 : for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I)
77 19 : Offsets.push_back(Data.getU32(OffsetPtr));
78 : return Error::success();
79 : }
80 :
81 15 : void DWARFListTableHeader::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
82 15 : if (DumpOpts.Verbose)
83 16 : OS << format("0x%8.8" PRIx32 ": ", HeaderOffset);
84 15 : OS << format(
85 : "%s list header: length = 0x%8.8" PRIx32 ", version = 0x%4.4" PRIx16 ", "
86 : "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8
87 : ", offset_entry_count = "
88 : "0x%8.8" PRIx32 "\n",
89 : ListTypeString.data(), HeaderData.Length, HeaderData.Version,
90 15 : HeaderData.AddrSize, HeaderData.SegSize, HeaderData.OffsetEntryCount);
91 :
92 15 : if (HeaderData.OffsetEntryCount > 0) {
93 3 : OS << "offsets: [";
94 8 : for (const auto &Off : Offsets) {
95 10 : OS << format("\n0x%8.8" PRIx32, Off);
96 5 : if (DumpOpts.Verbose)
97 2 : OS << format(" => 0x%8.8" PRIx32,
98 4 : Off + HeaderOffset + sizeof(HeaderData));
99 : }
100 3 : OS << "\n]\n";
101 : }
102 15 : }
103 :
104 97 : uint32_t DWARFListTableHeader::length() const {
105 97 : if (HeaderData.Length == 0)
106 : return 0;
107 : // TODO: DWARF64 support.
108 95 : return HeaderData.Length + sizeof(uint32_t);
109 : }
|