LLVM  14.0.0git
DWARFDebugArangeSet.cpp
Go to the documentation of this file.
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 
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/Format.h"
16 #include <cassert>
17 #include <cinttypes>
18 #include <cstdint>
19 #include <cstring>
20 
21 using namespace llvm;
22 
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 
33  Offset = -1ULL;
34  std::memset(&HeaderData, 0, sizeof(Header));
35  ArangeDescriptors.clear();
36 }
37 
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) {
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))
88  "the length of address range table at offset "
89  "0x%" PRIx64 " exceeds section size",
90  Offset);
92  HeaderData.AddrSize, errc::invalid_argument,
93  "address range table at offset 0x%" PRIx64, Offset))
94  return SizeErr;
95  if (HeaderData.SegSize != 0)
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(
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(
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(
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 
158  "address range table at offset 0x%" PRIx64
159  " is not terminated by null entry",
160  Offset);
161 }
162 
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 }
DWARFFormValue.h
llvm::errc::invalid_argument
@ invalid_argument
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::DWARFDebugArangeSet::dump
void dump(raw_ostream &OS) const
Definition: DWARFDebugArangeSet.cpp:163
llvm::DWARFDebugArangeSet::clear
void clear()
Definition: DWARFDebugArangeSet.cpp:32
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:1029
llvm::DWARFDebugArangeSet::Descriptor::Address
uint64_t Address
Definition: DWARFDebugArangeSet.h:44
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
llvm::DWARFDebugArangeSet::Header::AddrSize
uint8_t AddrSize
The size in bytes of an address on the target architecture.
Definition: DWARFDebugArangeSet.h:37
DWARFContext.h
llvm::DWARFDebugArangeSet::Header
Definition: DWARFDebugArangeSet.h:24
Errc.h
llvm::DWARFDebugArangeSet::Descriptor::Length
uint64_t Length
Definition: DWARFDebugArangeSet.h:45
DWARFDebugArangeSet.h
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
Format.h
llvm::DWARFDebugArangeSet::Header::CuOffset
uint64_t CuOffset
The offset from the beginning of the .debug_info section of the compilation unit entry referenced by ...
Definition: DWARFDebugArangeSet.h:32
llvm::DWARFDebugArangeSet::Descriptor::getEndAddress
uint64_t getEndAddress() const
Definition: DWARFDebugArangeSet.h:47
llvm::dwarf::FormatString
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:790
llvm::DWARFContext::checkAddressSizeSupported
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
Definition: DWARFContext.h:392
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:223
uint64_t
llvm::DataExtractor::isValidOffsetForDataOfSize
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Definition: DataExtractor.h:672
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ErrorSuccess
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition: Error.h:328
llvm::dwarf::getUnitLengthFieldByteSize
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition: Dwarf.h:674
llvm::DataExtractor::getUnsigned
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:125
llvm::DataExtractor::isValidOffset
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
Definition: DataExtractor.h:665
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DWARFDataExtractor::getInitialLength
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
Definition: DWARFDataExtractor.cpp:15
llvm::DWARFDebugArangeSet::Descriptor
Definition: DWARFDebugArangeSet.h:43
llvm::DWARFFormValue::dumpAddress
void dumpAddress(raw_ostream &OS, uint64_t Address) const
Dwarf.h
llvm::errc::not_supported
@ not_supported
uint32_t
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::DataExtractor::getU8
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
Definition: DataExtractor.cpp:79
llvm::DWARFDebugArangeSet::extract
Error extract(DWARFDataExtractor data, uint64_t *offset_ptr, function_ref< void(Error)> WarningHandler)
Definition: DWARFDebugArangeSet.cpp:38
llvm::DWARFDebugArangeSet::Descriptor::dump
void dump(raw_ostream &OS, uint32_t AddressSize) const
Definition: DWARFDebugArangeSet.cpp:23
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1240
llvm::DWARFDebugArangeSet::Header::SegSize
uint8_t SegSize
The size in bytes of a segment descriptor on the target architecture.
Definition: DWARFDebugArangeSet.h:40
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: WindowsSupport.h:193
llvm::DWARFDebugArangeSet::Header::Format
dwarf::DwarfFormat Format
The DWARF format of the set.
Definition: DWARFDebugArangeSet.h:29
raw_ostream.h
llvm::dwarf::getDwarfOffsetByteSize
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:635
llvm::DWARFDebugArangeSet::Header::Length
uint64_t Length
The total length of the entries for that set, not including the length field itself.
Definition: DWARFDebugArangeSet.h:27
llvm::DWARFDebugArangeSet::Header::Version
uint16_t Version
The DWARF version number.
Definition: DWARFDebugArangeSet.h:34
llvm::DataExtractor::getU16
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Definition: DataExtractor.cpp:92