109467b48Spatrick //===- DWARFDebugArangeSet.cpp --------------------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick
909467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10097a140dSpatrick #include "llvm/BinaryFormat/Dwarf.h"
11*d415bd75Srobert #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1273471bf0Spatrick #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
13097a140dSpatrick #include "llvm/Support/Errc.h"
1409467b48Spatrick #include "llvm/Support/Format.h"
1509467b48Spatrick #include "llvm/Support/raw_ostream.h"
1609467b48Spatrick #include <cassert>
1709467b48Spatrick #include <cinttypes>
1809467b48Spatrick #include <cstdint>
1909467b48Spatrick #include <cstring>
2009467b48Spatrick
2109467b48Spatrick using namespace llvm;
2209467b48Spatrick
dump(raw_ostream & OS,uint32_t AddressSize) const2309467b48Spatrick void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
2409467b48Spatrick uint32_t AddressSize) const {
2573471bf0Spatrick OS << '[';
2673471bf0Spatrick DWARFFormValue::dumpAddress(OS, AddressSize, Address);
2773471bf0Spatrick OS << ", ";
2873471bf0Spatrick DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress());
2973471bf0Spatrick OS << ')';
3009467b48Spatrick }
3109467b48Spatrick
clear()3209467b48Spatrick void DWARFDebugArangeSet::clear() {
3309467b48Spatrick Offset = -1ULL;
3409467b48Spatrick std::memset(&HeaderData, 0, sizeof(Header));
3509467b48Spatrick ArangeDescriptors.clear();
3609467b48Spatrick }
3709467b48Spatrick
extract(DWARFDataExtractor data,uint64_t * offset_ptr,function_ref<void (Error)> WarningHandler)38097a140dSpatrick Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
3973471bf0Spatrick uint64_t *offset_ptr,
4073471bf0Spatrick function_ref<void(Error)> WarningHandler) {
41097a140dSpatrick assert(data.isValidOffset(*offset_ptr));
4209467b48Spatrick ArangeDescriptors.clear();
4309467b48Spatrick Offset = *offset_ptr;
4409467b48Spatrick
45097a140dSpatrick // 7.21 Address Range Table (extract)
4609467b48Spatrick // Each set of entries in the table of address ranges contained in
47097a140dSpatrick // the .debug_aranges section begins with a header containing:
48097a140dSpatrick // 1. unit_length (initial length)
49097a140dSpatrick // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
50097a140dSpatrick // the length of the set of entries for this compilation unit,
51097a140dSpatrick // not including the length field itself.
52097a140dSpatrick // 2. version (uhalf)
53097a140dSpatrick // The value in this field is 2.
54097a140dSpatrick // 3. debug_info_offset (section offset)
55097a140dSpatrick // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
56097a140dSpatrick // .debug_info section of the compilation unit header.
57097a140dSpatrick // 4. address_size (ubyte)
58097a140dSpatrick // 5. segment_selector_size (ubyte)
59097a140dSpatrick // This header is followed by a series of tuples. Each tuple consists of
60097a140dSpatrick // a segment, an address and a length. The segment selector size is given by
61097a140dSpatrick // the segment_selector_size field of the header; the address and length
62097a140dSpatrick // size are each given by the address_size field of the header. Each set of
63097a140dSpatrick // tuples is terminated by a 0 for the segment, a 0 for the address and 0
64097a140dSpatrick // for the length. If the segment_selector_size field in the header is zero,
65097a140dSpatrick // the segment selectors are omitted from all tuples, including
66097a140dSpatrick // the terminating tuple.
6709467b48Spatrick
68097a140dSpatrick Error Err = Error::success();
69097a140dSpatrick std::tie(HeaderData.Length, HeaderData.Format) =
70097a140dSpatrick data.getInitialLength(offset_ptr, &Err);
71097a140dSpatrick HeaderData.Version = data.getU16(offset_ptr, &Err);
72097a140dSpatrick HeaderData.CuOffset = data.getUnsigned(
73097a140dSpatrick offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
74097a140dSpatrick HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
75097a140dSpatrick HeaderData.SegSize = data.getU8(offset_ptr, &Err);
76097a140dSpatrick if (Err) {
77097a140dSpatrick return createStringError(errc::invalid_argument,
78097a140dSpatrick "parsing address ranges table at offset 0x%" PRIx64
79097a140dSpatrick ": %s",
80097a140dSpatrick Offset, toString(std::move(Err)).c_str());
8109467b48Spatrick }
8209467b48Spatrick
83097a140dSpatrick // Perform basic validation of the header fields.
84097a140dSpatrick uint64_t full_length =
85097a140dSpatrick dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
86097a140dSpatrick if (!data.isValidOffsetForDataOfSize(Offset, full_length))
87097a140dSpatrick return createStringError(errc::invalid_argument,
88097a140dSpatrick "the length of address range table at offset "
89097a140dSpatrick "0x%" PRIx64 " exceeds section size",
90097a140dSpatrick Offset);
91*d415bd75Srobert if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
92*d415bd75Srobert HeaderData.AddrSize, errc::invalid_argument,
93*d415bd75Srobert "address range table at offset 0x%" PRIx64, Offset))
94*d415bd75Srobert return SizeErr;
95097a140dSpatrick if (HeaderData.SegSize != 0)
96097a140dSpatrick return createStringError(errc::not_supported,
97097a140dSpatrick "non-zero segment selector size in address range "
98097a140dSpatrick "table at offset 0x%" PRIx64 " is not supported",
99097a140dSpatrick Offset);
100097a140dSpatrick
101097a140dSpatrick // The first tuple following the header in each set begins at an offset that
102097a140dSpatrick // is a multiple of the size of a single tuple (that is, twice the size of
103097a140dSpatrick // an address because we do not support non-zero segment selector sizes).
104097a140dSpatrick // Therefore, the full length should also be a multiple of the tuple size.
10509467b48Spatrick const uint32_t tuple_size = HeaderData.AddrSize * 2;
106097a140dSpatrick if (full_length % tuple_size != 0)
107097a140dSpatrick return createStringError(
108097a140dSpatrick errc::invalid_argument,
109097a140dSpatrick "address range table at offset 0x%" PRIx64
110097a140dSpatrick " has length that is not a multiple of the tuple size",
111097a140dSpatrick Offset);
112097a140dSpatrick
113097a140dSpatrick // The header is padded, if necessary, to the appropriate boundary.
114097a140dSpatrick const uint32_t header_size = *offset_ptr - Offset;
11509467b48Spatrick uint32_t first_tuple_offset = 0;
11609467b48Spatrick while (first_tuple_offset < header_size)
11709467b48Spatrick first_tuple_offset += tuple_size;
11809467b48Spatrick
119097a140dSpatrick // There should be space for at least one tuple.
120097a140dSpatrick if (full_length <= first_tuple_offset)
121097a140dSpatrick return createStringError(
122097a140dSpatrick errc::invalid_argument,
123097a140dSpatrick "address range table at offset 0x%" PRIx64
124097a140dSpatrick " has an insufficient length to contain any entries",
125097a140dSpatrick Offset);
126097a140dSpatrick
12709467b48Spatrick *offset_ptr = Offset + first_tuple_offset;
12809467b48Spatrick
12909467b48Spatrick Descriptor arangeDescriptor;
13009467b48Spatrick
13109467b48Spatrick static_assert(sizeof(arangeDescriptor.Address) ==
13209467b48Spatrick sizeof(arangeDescriptor.Length),
13309467b48Spatrick "Different datatypes for addresses and sizes!");
13409467b48Spatrick assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
13509467b48Spatrick
136097a140dSpatrick uint64_t end_offset = Offset + full_length;
137097a140dSpatrick while (*offset_ptr < end_offset) {
138097a140dSpatrick uint64_t EntryOffset = *offset_ptr;
13909467b48Spatrick arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
14009467b48Spatrick arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
14109467b48Spatrick
14209467b48Spatrick // Each set of tuples is terminated by a 0 for the address and 0
14309467b48Spatrick // for the length.
144097a140dSpatrick if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
145097a140dSpatrick if (*offset_ptr == end_offset)
146097a140dSpatrick return ErrorSuccess();
14773471bf0Spatrick WarningHandler(createStringError(
148097a140dSpatrick errc::invalid_argument,
149097a140dSpatrick "address range table at offset 0x%" PRIx64
150097a140dSpatrick " has a premature terminator entry at offset 0x%" PRIx64,
15173471bf0Spatrick Offset, EntryOffset));
15209467b48Spatrick }
15309467b48Spatrick
154097a140dSpatrick ArangeDescriptors.push_back(arangeDescriptor);
15509467b48Spatrick }
156097a140dSpatrick
157097a140dSpatrick return createStringError(errc::invalid_argument,
158097a140dSpatrick "address range table at offset 0x%" PRIx64
159097a140dSpatrick " is not terminated by null entry",
160097a140dSpatrick Offset);
16109467b48Spatrick }
16209467b48Spatrick
dump(raw_ostream & OS) const16309467b48Spatrick void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
164097a140dSpatrick int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
165097a140dSpatrick OS << "Address Range Header: "
166097a140dSpatrick << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
167097a140dSpatrick << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
168097a140dSpatrick << format("version = 0x%4.4x, ", HeaderData.Version)
169097a140dSpatrick << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
170097a140dSpatrick HeaderData.CuOffset)
171097a140dSpatrick << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
172097a140dSpatrick << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
17309467b48Spatrick
17409467b48Spatrick for (const auto &Desc : ArangeDescriptors) {
17509467b48Spatrick Desc.dump(OS, HeaderData.AddrSize);
17609467b48Spatrick OS << '\n';
17709467b48Spatrick }
17809467b48Spatrick }
179