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