1//===- DWARFDebugAddr.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/DWARFDebugAddr.h"
10#include "llvm/BinaryFormat/Dwarf.h"
11#include "llvm/DebugInfo/DWARF/DWARFContext.h"
12#include "llvm/Support/Errc.h"
13#include "llvm/Support/FormatAdapters.h"
14#include "llvm/Support/FormatVariadic.h"
15
16using namespace llvm;
17
18Error DWARFDebugAddrTable::extractAddresses(const DWARFDataExtractor &Data,
19 uint64_t *OffsetPtr,
20 uint64_t EndOffset) {
21 assert(EndOffset >= *OffsetPtr);
22 uint64_t DataSize = EndOffset - *OffsetPtr;
23 assert(Data.isValidOffsetForDataOfSize(*OffsetPtr, DataSize));
24 if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
25 AddressSize: AddrSize, EC: errc::not_supported, Fmt: "address table at offset 0x%" PRIx64,
26 Vals: Offset))
27 return SizeErr;
28 if (DataSize % AddrSize != 0) {
29 invalidateLength();
30 return createStringError(EC: errc::invalid_argument,
31 Fmt: "address table at offset 0x%" PRIx64
32 " contains data of size 0x%" PRIx64
33 " which is not a multiple of addr size %" PRIu8,
34 Vals: Offset, Vals: DataSize, Vals: AddrSize);
35 }
36 Addrs.clear();
37 size_t Count = DataSize / AddrSize;
38 Addrs.reserve(n: Count);
39 while (Count--)
40 Addrs.push_back(x: Data.getRelocatedValue(Size: AddrSize, Off: OffsetPtr));
41 return Error::success();
42}
43
44Error DWARFDebugAddrTable::extractV5(const DWARFDataExtractor &Data,
45 uint64_t *OffsetPtr, uint8_t CUAddrSize,
46 std::function<void(Error)> WarnCallback) {
47 Offset = *OffsetPtr;
48 llvm::Error Err = Error::success();
49 std::tie(args&: Length, args&: Format) = Data.getInitialLength(Off: OffsetPtr, Err: &Err);
50 if (Err) {
51 invalidateLength();
52 return createStringError(EC: errc::invalid_argument,
53 Fmt: "parsing address table at offset 0x%" PRIx64
54 ": %s",
55 Vals: Offset, Vals: toString(E: std::move(Err)).c_str());
56 }
57
58 if (!Data.isValidOffsetForDataOfSize(offset: *OffsetPtr, length: Length)) {
59 uint64_t DiagnosticLength = Length;
60 invalidateLength();
61 return createStringError(
62 EC: errc::invalid_argument,
63 Fmt: "section is not large enough to contain an address table "
64 "at offset 0x%" PRIx64 " with a unit_length value of 0x%" PRIx64,
65 Vals: Offset, Vals: DiagnosticLength);
66 }
67 uint64_t EndOffset = *OffsetPtr + Length;
68 // Ensure that we can read the remaining header fields.
69 if (Length < 4) {
70 uint64_t DiagnosticLength = Length;
71 invalidateLength();
72 return createStringError(
73 EC: errc::invalid_argument,
74 Fmt: "address table at offset 0x%" PRIx64
75 " has a unit_length value of 0x%" PRIx64
76 ", which is too small to contain a complete header",
77 Vals: Offset, Vals: DiagnosticLength);
78 }
79
80 Version = Data.getU16(offset_ptr: OffsetPtr);
81 AddrSize = Data.getU8(offset_ptr: OffsetPtr);
82 SegSize = Data.getU8(offset_ptr: OffsetPtr);
83
84 // Perform a basic validation of the header fields.
85 if (Version != 5)
86 return createStringError(EC: errc::not_supported,
87 Fmt: "address table at offset 0x%" PRIx64
88 " has unsupported version %" PRIu16,
89 Vals: Offset, Vals: Version);
90 // TODO: add support for non-zero segment selector size.
91 if (SegSize != 0)
92 return createStringError(EC: errc::not_supported,
93 Fmt: "address table at offset 0x%" PRIx64
94 " has unsupported segment selector size %" PRIu8,
95 Vals: Offset, Vals: SegSize);
96
97 if (Error Err = extractAddresses(Data, OffsetPtr, EndOffset))
98 return Err;
99 if (CUAddrSize && AddrSize != CUAddrSize) {
100 WarnCallback(createStringError(
101 EC: errc::invalid_argument,
102 Fmt: "address table at offset 0x%" PRIx64 " has address size %" PRIu8
103 " which is different from CU address size %" PRIu8,
104 Vals: Offset, Vals: AddrSize, Vals: CUAddrSize));
105 }
106 return Error::success();
107}
108
109Error DWARFDebugAddrTable::extractPreStandard(const DWARFDataExtractor &Data,
110 uint64_t *OffsetPtr,
111 uint16_t CUVersion,
112 uint8_t CUAddrSize) {
113 assert(CUVersion > 0 && CUVersion < 5);
114
115 Offset = *OffsetPtr;
116 Length = 0;
117 Version = CUVersion;
118 AddrSize = CUAddrSize;
119 SegSize = 0;
120
121 return extractAddresses(Data, OffsetPtr, EndOffset: Data.size());
122}
123
124Error DWARFDebugAddrTable::extract(const DWARFDataExtractor &Data,
125 uint64_t *OffsetPtr,
126 uint16_t CUVersion,
127 uint8_t CUAddrSize,
128 std::function<void(Error)> WarnCallback) {
129 if (CUVersion > 0 && CUVersion < 5)
130 return extractPreStandard(Data, OffsetPtr, CUVersion, CUAddrSize);
131 if (CUVersion == 0)
132 WarnCallback(createStringError(EC: errc::invalid_argument,
133 S: "DWARF version is not defined in CU,"
134 " assuming version 5"));
135 return extractV5(Data, OffsetPtr, CUAddrSize, WarnCallback);
136}
137
138void DWARFDebugAddrTable::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
139 if (DumpOpts.Verbose)
140 OS << formatv(Fmt: "{0:x+8}: ", Vals: Offset);
141 if (Length) {
142 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
143 OS << "Address table header: "
144 << formatv(Fmt: "length = 0x{0:x-}",
145 Vals: fmt_align(Item: Length, Where: AlignStyle::Right, Amount: OffsetDumpWidth, Fill: '0'))
146 << ", format = " << dwarf::FormatString(Format)
147 << formatv(Fmt: ", version = {0:x+4}", Vals: Version)
148 << formatv(Fmt: ", addr_size = {0:x+2}", Vals: AddrSize)
149 << formatv(Fmt: ", seg_size = {0:x+2}", Vals: SegSize) << "\n";
150 }
151
152 if (Addrs.size() > 0) {
153 const char *AddrFmt;
154 switch (AddrSize) {
155 case 2:
156 AddrFmt = "{0:x+4}\n";
157 break;
158 case 4:
159 AddrFmt = "{0:x+8}\n";
160 break;
161 case 8:
162 AddrFmt = "{0:x+16}\n";
163 break;
164 default:
165 llvm_unreachable("unsupported address size");
166 }
167 OS << "Addrs: [\n";
168 for (uint64_t Addr : Addrs)
169 OS << formatv(Fmt: AddrFmt, Vals&: Addr);
170 OS << "]\n";
171 }
172}
173
174Expected<uint64_t> DWARFDebugAddrTable::getAddrEntry(uint32_t Index) const {
175 if (Index < Addrs.size())
176 return Addrs[Index];
177 return createStringError(EC: errc::invalid_argument,
178 Fmt: "Index %" PRIu32 " is out of range of the "
179 "address table at offset 0x%" PRIx64,
180 Vals: Index, Vals: Offset);
181}
182
183std::optional<uint64_t> DWARFDebugAddrTable::getFullLength() const {
184 if (Length == 0)
185 return std::nullopt;
186 return Length + dwarf::getUnitLengthFieldByteSize(Format);
187}
188