]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / DWARF / DWARFDebugArangeSet.cpp
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/Support/Format.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include <cassert>
13 #include <cinttypes>
14 #include <cstdint>
15 #include <cstring>
16
17 using namespace llvm;
18
19 void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
20                                            uint32_t AddressSize) const {
21   OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, Address)
22      << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
23                getEndAddress());
24 }
25
26 void DWARFDebugArangeSet::clear() {
27   Offset = -1U;
28   std::memset(&HeaderData, 0, sizeof(Header));
29   ArangeDescriptors.clear();
30 }
31
32 bool
33 DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
34   if (data.isValidOffset(*offset_ptr)) {
35     ArangeDescriptors.clear();
36     Offset = *offset_ptr;
37
38     // 7.20 Address Range Table
39     //
40     // Each set of entries in the table of address ranges contained in
41     // the .debug_aranges section begins with a header consisting of: a
42     // 4-byte length containing the length of the set of entries for this
43     // compilation unit, not including the length field itself; a 2-byte
44     // version identifier containing the value 2 for DWARF Version 2; a
45     // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
46     // containing the size in bytes of an address (or the offset portion of
47     // an address for segmented addressing) on the target system; and a
48     // 1-byte unsigned integer containing the size in bytes of a segment
49     // descriptor on the target system. This header is followed by a series
50     // of tuples. Each tuple consists of an address and a length, each in
51     // the size appropriate for an address on the target architecture.
52     HeaderData.Length = data.getU32(offset_ptr);
53     HeaderData.Version = data.getU16(offset_ptr);
54     HeaderData.CuOffset = data.getU32(offset_ptr);
55     HeaderData.AddrSize = data.getU8(offset_ptr);
56     HeaderData.SegSize = data.getU8(offset_ptr);
57
58     // Perform basic validation of the header fields.
59     if (!data.isValidOffsetForDataOfSize(Offset, HeaderData.Length) ||
60         (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)) {
61       clear();
62       return false;
63     }
64
65     // The first tuple following the header in each set begins at an offset
66     // that is a multiple of the size of a single tuple (that is, twice the
67     // size of an address). The header is padded, if necessary, to the
68     // appropriate boundary.
69     const uint32_t header_size = *offset_ptr - Offset;
70     const uint32_t tuple_size = HeaderData.AddrSize * 2;
71     uint32_t first_tuple_offset = 0;
72     while (first_tuple_offset < header_size)
73       first_tuple_offset += tuple_size;
74
75     *offset_ptr = Offset + first_tuple_offset;
76
77     Descriptor arangeDescriptor;
78
79     static_assert(sizeof(arangeDescriptor.Address) ==
80                       sizeof(arangeDescriptor.Length),
81                   "Different datatypes for addresses and sizes!");
82     assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
83
84     while (data.isValidOffset(*offset_ptr)) {
85       arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
86       arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
87
88       // Each set of tuples is terminated by a 0 for the address and 0
89       // for the length.
90       if (arangeDescriptor.Address || arangeDescriptor.Length)
91         ArangeDescriptors.push_back(arangeDescriptor);
92       else
93         break; // We are done if we get a zero address and length
94     }
95
96     return !ArangeDescriptors.empty();
97   }
98   return false;
99 }
100
101 void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
102   OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
103                HeaderData.Length, HeaderData.Version)
104      << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
105                HeaderData.CuOffset, HeaderData.AddrSize, HeaderData.SegSize);
106
107   for (const auto &Desc : ArangeDescriptors) {
108     Desc.dump(OS, HeaderData.AddrSize);
109     OS << '\n';
110   }
111 }