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