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