Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
DWARFDebugAddr.cpp
Go to the documentation of this file.
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
14using namespacellvm;
15
16Error DWARFDebugAddrTable::extractAddresses(constDWARFDataExtractor &Data,
17uint64_t *OffsetPtr,
18uint64_t EndOffset) {
19assert(EndOffset >= *OffsetPtr);
20uint64_t DataSize = EndOffset - *OffsetPtr;
21assert(Data.isValidOffsetForDataOfSize(*OffsetPtr, DataSize));
22if (Error SizeErr =DWARFContext::checkAddressSizeSupported(
23 AddrSize,errc::not_supported,"address table at offset 0x%" PRIx64,
24 Offset))
25return SizeErr;
26if (DataSize % AddrSize != 0) {
27 invalidateLength();
28returncreateStringError(errc::invalid_argument,
29"address table at offset 0x%" PRIx64
30" contains data of size 0x%" PRIx64
31" which is not a multiple of addr size %" PRIu8,
32 Offset, DataSize, AddrSize);
33 }
34 Addrs.clear();
35size_t Count = DataSize / AddrSize;
36 Addrs.reserve(Count);
37while (Count--)
38 Addrs.push_back(Data.getRelocatedValue(AddrSize, OffsetPtr));
39returnError::success();
40}
41
42ErrorDWARFDebugAddrTable::extractV5(constDWARFDataExtractor &Data,
43uint64_t *OffsetPtr,uint8_t CUAddrSize,
44 std::function<void(Error)> WarnCallback) {
45 Offset = *OffsetPtr;
46llvm::Error Err =Error::success();
47 std::tie(Length, Format) =Data.getInitialLength(OffsetPtr, &Err);
48if (Err) {
49 invalidateLength();
50returncreateStringError(errc::invalid_argument,
51"parsing address table at offset 0x%" PRIx64
52": %s",
53 Offset,toString(std::move(Err)).c_str());
54 }
55
56if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, Length)) {
57uint64_t DiagnosticLength = Length;
58 invalidateLength();
59returncreateStringError(
60errc::invalid_argument,
61"section is not large enough to contain an address table "
62"at offset 0x%" PRIx64" with a unit_length value of 0x%" PRIx64,
63 Offset, DiagnosticLength);
64 }
65uint64_t EndOffset = *OffsetPtr + Length;
66// Ensure that we can read the remaining header fields.
67if (Length < 4) {
68uint64_t DiagnosticLength =Length;
69 invalidateLength();
70returncreateStringError(
71errc::invalid_argument,
72"address table at offset 0x%" PRIx64
73" has a unit_length value of 0x%" PRIx64
74", which is too small to contain a complete header",
75 Offset, DiagnosticLength);
76 }
77
78 Version =Data.getU16(OffsetPtr);
79 AddrSize =Data.getU8(OffsetPtr);
80 SegSize =Data.getU8(OffsetPtr);
81
82// Perform a basic validation of the header fields.
83if (Version != 5)
84returncreateStringError(errc::not_supported,
85"address table at offset 0x%" PRIx64
86" has unsupported version %" PRIu16,
87 Offset, Version);
88// TODO: add support for non-zero segment selector size.
89if (SegSize != 0)
90returncreateStringError(errc::not_supported,
91"address table at offset 0x%" PRIx64
92" has unsupported segment selector size %" PRIu8,
93 Offset, SegSize);
94
95if (Error Err = extractAddresses(Data, OffsetPtr, EndOffset))
96return Err;
97if (CUAddrSize && AddrSize != CUAddrSize) {
98 WarnCallback(createStringError(
99errc::invalid_argument,
100"address table at offset 0x%" PRIx64" has address size %" PRIu8
101" which is different from CU address size %" PRIu8,
102 Offset, AddrSize, CUAddrSize));
103 }
104returnError::success();
105}
106
107ErrorDWARFDebugAddrTable::extractPreStandard(constDWARFDataExtractor &Data,
108uint64_t *OffsetPtr,
109uint16_t CUVersion,
110uint8_t CUAddrSize) {
111assert(CUVersion > 0 && CUVersion < 5);
112
113 Offset = *OffsetPtr;
114 Length = 0;
115 Version = CUVersion;
116 AddrSize = CUAddrSize;
117 SegSize = 0;
118
119return extractAddresses(Data, OffsetPtr,Data.size());
120}
121
122ErrorDWARFDebugAddrTable::extract(constDWARFDataExtractor &Data,
123uint64_t *OffsetPtr,
124uint16_t CUVersion,
125uint8_t CUAddrSize,
126 std::function<void(Error)> WarnCallback) {
127if (CUVersion > 0 && CUVersion < 5)
128returnextractPreStandard(Data, OffsetPtr, CUVersion, CUAddrSize);
129if (CUVersion == 0)
130 WarnCallback(createStringError(errc::invalid_argument,
131"DWARF version is not defined in CU,"
132" assuming version 5"));
133returnextractV5(Data, OffsetPtr, CUAddrSize, WarnCallback);
134}
135
136voidDWARFDebugAddrTable::dump(raw_ostream &OS,DIDumpOptions DumpOpts) const{
137if (DumpOpts.Verbose)
138OS <<format("0x%8.8" PRIx64": ", Offset);
139if (Length) {
140int OffsetDumpWidth = 2 *dwarf::getDwarfOffsetByteSize(Format);
141OS <<"Address table header: "
142 <<format("length = 0x%0*" PRIx64, OffsetDumpWidth, Length)
143 <<", format = " <<dwarf::FormatString(Format)
144 <<format(", version = 0x%4.4" PRIx16, Version)
145 <<format(", addr_size = 0x%2.2" PRIx8, AddrSize)
146 <<format(", seg_size = 0x%2.2" PRIx8, SegSize) <<"\n";
147 }
148
149if (Addrs.size() > 0) {
150constchar *AddrFmt;
151switch (AddrSize) {
152case 2:
153 AddrFmt ="0x%4.4" PRIx64"\n";
154break;
155case 4:
156 AddrFmt ="0x%8.8" PRIx64"\n";
157break;
158case 8:
159 AddrFmt ="0x%16.16" PRIx64"\n";
160break;
161default:
162llvm_unreachable("unsupported address size");
163 }
164OS <<"Addrs: [\n";
165for (uint64_tAddr : Addrs)
166OS <<format(AddrFmt,Addr);
167OS <<"]\n";
168 }
169}
170
171Expected<uint64_t>DWARFDebugAddrTable::getAddrEntry(uint32_t Index) const{
172if (Index < Addrs.size())
173return Addrs[Index];
174returncreateStringError(errc::invalid_argument,
175"Index %" PRIu32" is out of range of the "
176"address table at offset 0x%" PRIx64,
177 Index, Offset);
178}
179
180std::optional<uint64_t>DWARFDebugAddrTable::getFullLength() const{
181if (Length == 0)
182return std::nullopt;
183return Length +dwarf::getUnitLengthFieldByteSize(Format);
184}
DWARFContext.h
DWARFDebugAddr.h
Dwarf.h
This file contains constants used for implementing Dwarf debug support.
Addr
uint64_t Addr
Definition:ELFObjHandler.cpp:79
Errc.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
llvm::DWARFContext::checkAddressSizeSupported
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
Definition:DWARFContext.h:417
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition:DWARFDataExtractor.h:21
llvm::DWARFDebugAddrTable::dump
void dump(raw_ostream &OS, DIDumpOptions DumpOpts={}) const
Definition:DWARFDebugAddr.cpp:136
llvm::DWARFDebugAddrTable::extract
Error extract(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, uint16_t CUVersion, uint8_t CUAddrSize, std::function< void(Error)> WarnCallback)
Extract the entire table, including all addresses.
Definition:DWARFDebugAddr.cpp:122
llvm::DWARFDebugAddrTable::extractV5
Error extractV5(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, uint8_t CUAddrSize, std::function< void(Error)> WarnCallback)
Extract a DWARFv5 address table.
Definition:DWARFDebugAddr.cpp:42
llvm::DWARFDebugAddrTable::extractPreStandard
Error extractPreStandard(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, uint16_t CUVersion, uint8_t CUAddrSize)
Extract a pre-DWARFv5 address table.
Definition:DWARFDebugAddr.cpp:107
llvm::DWARFDebugAddrTable::getFullLength
std::optional< uint64_t > getFullLength() const
Return the full length of this table, including the length field.
Definition:DWARFDebugAddr.cpp:180
llvm::DWARFDebugAddrTable::getAddrEntry
Expected< uint64_t > getAddrEntry(uint32_t Index) const
Return the address based on a given index.
Definition:DWARFDebugAddr.cpp:171
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::Expected
Tagged union holding either a T or a Error.
Definition:Error.h:481
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
uint16_t
uint32_t
uint64_t
uint8_t
llvm::dwarf::FormatString
StringRef FormatString(DwarfFormat Format)
Definition:Dwarf.cpp:868
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
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:1110
llvm::dwarf::getDwarfOffsetByteSize
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition:Dwarf.h:1071
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Length
@ Length
Definition:DWP.cpp:480
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition:WindowsSupport.h:194
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition:Error.h:1291
llvm::errc::not_supported
@ not_supported
llvm::errc::invalid_argument
@ invalid_argument
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition:Format.h:125
llvm::toString
const char * toString(DWARFSectionKind Kind)
Definition:DWARFUnitIndex.h:67
llvm::Data
@ Data
Definition:SIMachineScheduler.h:55
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition:DIContext.h:196
llvm::DIDumpOptions::Verbose
bool Verbose
Definition:DIContext.h:207

Generated on Fri Jul 18 2025 11:08:57 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp