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(DataExtractor data, uint64_t *offset_ptr) {
35   assert(data.isValidOffset(*offset_ptr));
36   ArangeDescriptors.clear();
37   Offset = *offset_ptr;
38 
39   // 7.21 Address Range Table (extract)
40   // Each set of entries in the table of address ranges contained in
41   // the .debug_aranges section begins with a header containing:
42   // 1. unit_length (initial length)
43   //    A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
44   //    the length of the set of entries for this compilation unit,
45   //    not including the length field itself.
46   // 2. version (uhalf)
47   //    The value in this field is 2.
48   // 3. debug_info_offset (section offset)
49   //    A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
50   //    .debug_info section of the compilation unit header.
51   // 4. address_size (ubyte)
52   // 5. segment_selector_size (ubyte)
53   // This header is followed by a series of tuples. Each tuple consists of
54   // a segment, an address and a length. The segment selector size is given by
55   // the segment_selector_size field of the header; the address and length
56   // size are each given by the address_size field of the header. Each set of
57   // tuples is terminated by a 0 for the segment, a 0 for the address and 0
58   // for the length. If the segment_selector_size field in the header is zero,
59   // the segment selectors are omitted from all tuples, including
60   // the terminating tuple.
61 
62   constexpr unsigned CommonFieldsLength = 2 + // Version
63                                           1 + // Address Size
64                                           1;  // Segment Selector Size
65   static const unsigned DWARF32HeaderLength =
66       dwarf::getUnitLengthFieldByteSize(dwarf::DWARF32) + CommonFieldsLength +
67       dwarf::getDwarfOffsetByteSize(dwarf::DWARF32); // Debug Info Offset
68   static const unsigned DWARF64HeaderLength =
69       dwarf::getUnitLengthFieldByteSize(dwarf::DWARF64) + CommonFieldsLength +
70       dwarf::getDwarfOffsetByteSize(dwarf::DWARF64); // Debug Info Offset
71 
72   if (!data.isValidOffsetForDataOfSize(Offset, DWARF32HeaderLength))
73     return createStringError(errc::invalid_argument,
74                              "section is not large enough to contain "
75                              "an address range table at offset 0x%" PRIx64,
76                              Offset);
77 
78   dwarf::DwarfFormat format = dwarf::DWARF32;
79   HeaderData.Length = data.getU32(offset_ptr);
80   if (HeaderData.Length == dwarf::DW_LENGTH_DWARF64) {
81     if (!data.isValidOffsetForDataOfSize(Offset, DWARF64HeaderLength))
82       return createStringError(
83           errc::invalid_argument,
84           "section is not large enough to contain a DWARF64 "
85           "address range table at offset 0x%" PRIx64,
86           Offset);
87     HeaderData.Length = data.getU64(offset_ptr);
88     format = dwarf::DWARF64;
89   } else if (HeaderData.Length >= dwarf::DW_LENGTH_lo_reserved) {
90     return createStringError(
91         errc::invalid_argument,
92         "address range table at offset 0x%" PRIx64
93         " has unsupported reserved unit length of value 0x%8.8" PRIx64,
94         Offset, HeaderData.Length);
95   }
96   HeaderData.Version = data.getU16(offset_ptr);
97   HeaderData.CuOffset =
98       data.getUnsigned(offset_ptr, dwarf::getDwarfOffsetByteSize(format));
99   HeaderData.AddrSize = data.getU8(offset_ptr);
100   HeaderData.SegSize = data.getU8(offset_ptr);
101 
102   // Perform basic validation of the header fields.
103   uint64_t full_length =
104       dwarf::getUnitLengthFieldByteSize(format) + HeaderData.Length;
105   if (!data.isValidOffsetForDataOfSize(Offset, full_length))
106     return createStringError(errc::invalid_argument,
107                              "the length of address range table at offset "
108                              "0x%" PRIx64 " exceeds section size",
109                              Offset);
110   if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
111     return createStringError(errc::invalid_argument,
112                              "address range table at offset 0x%" PRIx64
113                              " has unsupported address size: %d "
114                              "(4 and 8 supported)",
115                              Offset, HeaderData.AddrSize);
116   if (HeaderData.SegSize != 0)
117     return createStringError(errc::not_supported,
118                              "non-zero segment selector size in address range "
119                              "table at offset 0x%" PRIx64 " is not supported",
120                              Offset);
121 
122   // The first tuple following the header in each set begins at an offset that
123   // is a multiple of the size of a single tuple (that is, twice the size of
124   // an address because we do not support non-zero segment selector sizes).
125   // Therefore, the full length should also be a multiple of the tuple size.
126   const uint32_t tuple_size = HeaderData.AddrSize * 2;
127   if (full_length % tuple_size != 0)
128     return createStringError(
129         errc::invalid_argument,
130         "address range table at offset 0x%" PRIx64
131         " has length that is not a multiple of the tuple size",
132         Offset);
133 
134   // The header is padded, if necessary, to the appropriate boundary.
135   const uint32_t header_size = *offset_ptr - Offset;
136   uint32_t first_tuple_offset = 0;
137   while (first_tuple_offset < header_size)
138     first_tuple_offset += tuple_size;
139 
140   // There should be space for at least one tuple.
141   if (full_length <= first_tuple_offset)
142     return createStringError(
143         errc::invalid_argument,
144         "address range table at offset 0x%" PRIx64
145         " has an insufficient length to contain any entries",
146         Offset);
147 
148   *offset_ptr = Offset + first_tuple_offset;
149 
150   Descriptor arangeDescriptor;
151 
152   static_assert(sizeof(arangeDescriptor.Address) ==
153                     sizeof(arangeDescriptor.Length),
154                 "Different datatypes for addresses and sizes!");
155   assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
156 
157   uint64_t end_offset = Offset + full_length;
158   while (*offset_ptr < end_offset) {
159     arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
160     arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
161 
162     if (arangeDescriptor.Length == 0) {
163       // Each set of tuples is terminated by a 0 for the address and 0
164       // for the length.
165       if (arangeDescriptor.Address == 0 && *offset_ptr == end_offset)
166         return ErrorSuccess();
167       return createStringError(
168           errc::invalid_argument,
169           "address range table at offset 0x%" PRIx64
170           " has an invalid tuple (length = 0) at offset 0x%" PRIx64,
171           Offset, *offset_ptr - tuple_size);
172     }
173 
174     ArangeDescriptors.push_back(arangeDescriptor);
175   }
176 
177   return createStringError(errc::invalid_argument,
178                            "address range table at offset 0x%" PRIx64
179                            " is not terminated by null entry",
180                            Offset);
181 }
182 
183 void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
184   OS << "Address Range Header: "
185      << format("length = 0x%8.8" PRIx64 ", ", HeaderData.Length)
186      << format("version = 0x%4.4x, ", HeaderData.Version)
187      << format("cu_offset = 0x%8.8" PRIx64 ", ", HeaderData.CuOffset)
188      << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
189      << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
190 
191   for (const auto &Desc : ArangeDescriptors) {
192     Desc.dump(OS, HeaderData.AddrSize);
193     OS << '\n';
194   }
195 }
196