1 //===- DWARFDebugArangeSet.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10 #include "llvm/BinaryFormat/Dwarf.h"
11 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
12 #include "llvm/Support/Errc.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
22 void DWARFDebugArangeSet::Descriptor::dump(raw_ostream
&OS
,
23 uint32_t AddressSize
) const {
25 DWARFFormValue::dumpAddress(OS
, AddressSize
, Address
);
27 DWARFFormValue::dumpAddress(OS
, AddressSize
, getEndAddress());
31 void DWARFDebugArangeSet::clear() {
33 std::memset(&HeaderData
, 0, sizeof(Header
));
34 ArangeDescriptors
.clear();
37 Error
DWARFDebugArangeSet::extract(DWARFDataExtractor data
,
39 function_ref
<void(Error
)> WarningHandler
) {
40 assert(data
.isValidOffset(*offset_ptr
));
41 ArangeDescriptors
.clear();
44 // 7.21 Address Range Table (extract)
45 // Each set of entries in the table of address ranges contained in
46 // the .debug_aranges section begins with a header containing:
47 // 1. unit_length (initial length)
48 // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
49 // the length of the set of entries for this compilation unit,
50 // not including the length field itself.
52 // The value in this field is 2.
53 // 3. debug_info_offset (section offset)
54 // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
55 // .debug_info section of the compilation unit header.
56 // 4. address_size (ubyte)
57 // 5. segment_selector_size (ubyte)
58 // This header is followed by a series of tuples. Each tuple consists of
59 // a segment, an address and a length. The segment selector size is given by
60 // the segment_selector_size field of the header; the address and length
61 // size are each given by the address_size field of the header. Each set of
62 // tuples is terminated by a 0 for the segment, a 0 for the address and 0
63 // for the length. If the segment_selector_size field in the header is zero,
64 // the segment selectors are omitted from all tuples, including
65 // the terminating tuple.
67 Error Err
= Error::success();
68 std::tie(HeaderData
.Length
, HeaderData
.Format
) =
69 data
.getInitialLength(offset_ptr
, &Err
);
70 HeaderData
.Version
= data
.getU16(offset_ptr
, &Err
);
71 HeaderData
.CuOffset
= data
.getUnsigned(
72 offset_ptr
, dwarf::getDwarfOffsetByteSize(HeaderData
.Format
), &Err
);
73 HeaderData
.AddrSize
= data
.getU8(offset_ptr
, &Err
);
74 HeaderData
.SegSize
= data
.getU8(offset_ptr
, &Err
);
76 return createStringError(errc::invalid_argument
,
77 "parsing address ranges table at offset 0x%" PRIx64
79 Offset
, toString(std::move(Err
)).c_str());
82 // Perform basic validation of the header fields.
83 uint64_t full_length
=
84 dwarf::getUnitLengthFieldByteSize(HeaderData
.Format
) + HeaderData
.Length
;
85 if (!data
.isValidOffsetForDataOfSize(Offset
, full_length
))
86 return createStringError(errc::invalid_argument
,
87 "the length of address range table at offset "
88 "0x%" PRIx64
" exceeds section size",
90 if (HeaderData
.AddrSize
!= 4 && HeaderData
.AddrSize
!= 8)
91 return createStringError(errc::invalid_argument
,
92 "address range table at offset 0x%" PRIx64
93 " has unsupported address size: %d "
94 "(4 and 8 supported)",
95 Offset
, HeaderData
.AddrSize
);
96 if (HeaderData
.SegSize
!= 0)
97 return createStringError(errc::not_supported
,
98 "non-zero segment selector size in address range "
99 "table at offset 0x%" PRIx64
" is not supported",
102 // The first tuple following the header in each set begins at an offset that
103 // is a multiple of the size of a single tuple (that is, twice the size of
104 // an address because we do not support non-zero segment selector sizes).
105 // Therefore, the full length should also be a multiple of the tuple size.
106 const uint32_t tuple_size
= HeaderData
.AddrSize
* 2;
107 if (full_length
% tuple_size
!= 0)
108 return createStringError(
109 errc::invalid_argument
,
110 "address range table at offset 0x%" PRIx64
111 " has length that is not a multiple of the tuple size",
114 // The header is padded, if necessary, to the appropriate boundary.
115 const uint32_t header_size
= *offset_ptr
- Offset
;
116 uint32_t first_tuple_offset
= 0;
117 while (first_tuple_offset
< header_size
)
118 first_tuple_offset
+= tuple_size
;
120 // There should be space for at least one tuple.
121 if (full_length
<= first_tuple_offset
)
122 return createStringError(
123 errc::invalid_argument
,
124 "address range table at offset 0x%" PRIx64
125 " has an insufficient length to contain any entries",
128 *offset_ptr
= Offset
+ first_tuple_offset
;
130 Descriptor arangeDescriptor
;
132 static_assert(sizeof(arangeDescriptor
.Address
) ==
133 sizeof(arangeDescriptor
.Length
),
134 "Different datatypes for addresses and sizes!");
135 assert(sizeof(arangeDescriptor
.Address
) >= HeaderData
.AddrSize
);
137 uint64_t end_offset
= Offset
+ full_length
;
138 while (*offset_ptr
< end_offset
) {
139 uint64_t EntryOffset
= *offset_ptr
;
140 arangeDescriptor
.Address
= data
.getUnsigned(offset_ptr
, HeaderData
.AddrSize
);
141 arangeDescriptor
.Length
= data
.getUnsigned(offset_ptr
, HeaderData
.AddrSize
);
143 // Each set of tuples is terminated by a 0 for the address and 0
145 if (arangeDescriptor
.Length
== 0 && arangeDescriptor
.Address
== 0) {
146 if (*offset_ptr
== end_offset
)
147 return ErrorSuccess();
148 WarningHandler(createStringError(
149 errc::invalid_argument
,
150 "address range table at offset 0x%" PRIx64
151 " has a premature terminator entry at offset 0x%" PRIx64
,
152 Offset
, EntryOffset
));
155 ArangeDescriptors
.push_back(arangeDescriptor
);
158 return createStringError(errc::invalid_argument
,
159 "address range table at offset 0x%" PRIx64
160 " is not terminated by null entry",
164 void DWARFDebugArangeSet::dump(raw_ostream
&OS
) const {
165 int OffsetDumpWidth
= 2 * dwarf::getDwarfOffsetByteSize(HeaderData
.Format
);
166 OS
<< "Address Range Header: "
167 << format("length = 0x%0*" PRIx64
", ", OffsetDumpWidth
, HeaderData
.Length
)
168 << "format = " << dwarf::FormatString(HeaderData
.Format
) << ", "
169 << format("version = 0x%4.4x, ", HeaderData
.Version
)
170 << format("cu_offset = 0x%0*" PRIx64
", ", OffsetDumpWidth
,
172 << format("addr_size = 0x%2.2x, ", HeaderData
.AddrSize
)
173 << format("seg_size = 0x%2.2x\n", HeaderData
.SegSize
);
175 for (const auto &Desc
: ArangeDescriptors
) {
176 Desc
.dump(OS
, HeaderData
.AddrSize
);