1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
10 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/None.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/DebugInfo/DIContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
26 class DWARFFormValue
{
43 ValueType() { uval
= 0; }
44 ValueType(int64_t V
) : sval(V
) {}
45 ValueType(uint64_t V
) : uval(V
) {}
46 ValueType(const char *V
) : cstr(V
) {}
53 const uint8_t *data
= nullptr;
54 uint64_t SectionIndex
; /// Section index for reference forms.
57 dwarf::Form Form
; /// Form for this value.
58 ValueType Value
; /// Contains all data for the form.
59 const DWARFUnit
*U
= nullptr; /// Remember the DWARFUnit at extract time.
60 const DWARFContext
*C
= nullptr; /// Context for extract time.
62 DWARFFormValue(dwarf::Form F
, ValueType V
) : Form(F
), Value(V
) {}
65 DWARFFormValue(dwarf::Form F
= dwarf::Form(0)) : Form(F
) {}
67 static DWARFFormValue
createFromSValue(dwarf::Form F
, int64_t V
);
68 static DWARFFormValue
createFromUValue(dwarf::Form F
, uint64_t V
);
69 static DWARFFormValue
createFromPValue(dwarf::Form F
, const char *V
);
70 static DWARFFormValue
createFromBlockValue(dwarf::Form F
,
72 static DWARFFormValue
createFromUnit(dwarf::Form F
, const DWARFUnit
*Unit
,
75 dwarf::Form
getForm() const { return Form
; }
76 uint64_t getRawUValue() const { return Value
.uval
; }
78 bool isFormClass(FormClass FC
) const;
79 const DWARFUnit
*getUnit() const { return U
; }
80 void dump(raw_ostream
&OS
, DIDumpOptions DumpOpts
= DIDumpOptions()) const;
81 void dumpSectionedAddress(raw_ostream
&OS
, DIDumpOptions DumpOpts
,
82 object::SectionedAddress SA
) const;
83 static void dumpAddressSection(const DWARFObject
&Obj
, raw_ostream
&OS
,
84 DIDumpOptions DumpOpts
, uint64_t SectionIndex
);
86 /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
87 /// in \p FormParams is needed to interpret some forms. The optional
88 /// \p Context and \p Unit allows extracting information if the form refers
89 /// to other sections (e.g., .debug_str).
90 bool extractValue(const DWARFDataExtractor
&Data
, uint64_t *OffsetPtr
,
91 dwarf::FormParams FormParams
,
92 const DWARFContext
*Context
= nullptr,
93 const DWARFUnit
*Unit
= nullptr);
95 bool extractValue(const DWARFDataExtractor
&Data
, uint64_t *OffsetPtr
,
96 dwarf::FormParams FormParams
, const DWARFUnit
*U
) {
97 return extractValue(Data
, OffsetPtr
, FormParams
, nullptr, U
);
100 bool isInlinedCStr() const {
101 return Value
.data
!= nullptr && Value
.data
== (const uint8_t *)Value
.cstr
;
104 /// getAsFoo functions below return the extracted value as Foo if only
105 /// DWARFFormValue has form class is suitable for representing Foo.
106 Optional
<uint64_t> getAsReference() const;
111 Optional
<UnitOffset
> getAsRelativeReference() const;
112 Optional
<uint64_t> getAsUnsignedConstant() const;
113 Optional
<int64_t> getAsSignedConstant() const;
114 Optional
<const char *> getAsCString() const;
115 Optional
<uint64_t> getAsAddress() const;
116 Optional
<object::SectionedAddress
> getAsSectionedAddress() const;
117 Optional
<uint64_t> getAsSectionOffset() const;
118 Optional
<ArrayRef
<uint8_t>> getAsBlock() const;
119 Optional
<uint64_t> getAsCStringOffset() const;
120 Optional
<uint64_t> getAsReferenceUVal() const;
122 /// Skip a form's value in \p DebugInfoData at the offset specified by
125 /// Skips the bytes for the current form and updates the offset.
127 /// \param DebugInfoData The data where we want to skip the value.
128 /// \param OffsetPtr A reference to the offset that will be updated.
129 /// \param Params DWARF parameters to help interpret forms.
130 /// \returns true on success, false if the form was not skipped.
131 bool skipValue(DataExtractor DebugInfoData
, uint64_t *OffsetPtr
,
132 const dwarf::FormParams Params
) const {
133 return DWARFFormValue::skipValue(Form
, DebugInfoData
, OffsetPtr
, Params
);
136 /// Skip a form's value in \p DebugInfoData at the offset specified by
139 /// Skips the bytes for the specified form and updates the offset.
141 /// \param Form The DW_FORM enumeration that indicates the form to skip.
142 /// \param DebugInfoData The data where we want to skip the value.
143 /// \param OffsetPtr A reference to the offset that will be updated.
144 /// \param FormParams DWARF parameters to help interpret forms.
145 /// \returns true on success, false if the form was not skipped.
146 static bool skipValue(dwarf::Form Form
, DataExtractor DebugInfoData
,
148 const dwarf::FormParams FormParams
);
151 void dumpString(raw_ostream
&OS
) const;
156 /// Take an optional DWARFFormValue and try to extract a string value from it.
158 /// \param V and optional DWARFFormValue to attempt to extract the value from.
159 /// \returns an optional value that contains a value if the form value
160 /// was valid and was a string.
161 inline Optional
<const char *> toString(const Optional
<DWARFFormValue
> &V
) {
163 return V
->getAsCString();
167 /// Take an optional DWARFFormValue and try to extract a string value from it.
169 /// \param V and optional DWARFFormValue to attempt to extract the value from.
170 /// \returns an optional value that contains a value if the form value
171 /// was valid and was a string.
172 inline StringRef
toStringRef(const Optional
<DWARFFormValue
> &V
,
173 StringRef Default
= {}) {
175 if (auto S
= V
->getAsCString())
180 /// Take an optional DWARFFormValue and extract a string value from it.
182 /// \param V and optional DWARFFormValue to attempt to extract the value from.
183 /// \param Default the default value to return in case of failure.
184 /// \returns the string value or Default if the V doesn't have a value or the
185 /// form value's encoding wasn't a string.
186 inline const char *toString(const Optional
<DWARFFormValue
> &V
,
187 const char *Default
) {
188 return toString(V
).getValueOr(Default
);
191 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
193 /// \param V and optional DWARFFormValue to attempt to extract the value from.
194 /// \returns an optional value that contains a value if the form value
195 /// was valid and has a unsigned constant form.
196 inline Optional
<uint64_t> toUnsigned(const Optional
<DWARFFormValue
> &V
) {
198 return V
->getAsUnsignedConstant();
202 /// Take an optional DWARFFormValue and extract a unsigned constant.
204 /// \param V and optional DWARFFormValue to attempt to extract the value from.
205 /// \param Default the default value to return in case of failure.
206 /// \returns the extracted unsigned value or Default if the V doesn't have a
207 /// value or the form value's encoding wasn't an unsigned constant form.
208 inline uint64_t toUnsigned(const Optional
<DWARFFormValue
> &V
,
210 return toUnsigned(V
).getValueOr(Default
);
213 /// Take an optional DWARFFormValue and try to extract an reference.
215 /// \param V and optional DWARFFormValue to attempt to extract the value from.
216 /// \returns an optional value that contains a value if the form value
217 /// was valid and has a reference form.
218 inline Optional
<uint64_t> toReference(const Optional
<DWARFFormValue
> &V
) {
220 return V
->getAsReference();
224 /// Take an optional DWARFFormValue and extract a reference.
226 /// \param V and optional DWARFFormValue to attempt to extract the value from.
227 /// \param Default the default value to return in case of failure.
228 /// \returns the extracted reference value or Default if the V doesn't have a
229 /// value or the form value's encoding wasn't a reference form.
230 inline uint64_t toReference(const Optional
<DWARFFormValue
> &V
,
232 return toReference(V
).getValueOr(Default
);
235 /// Take an optional DWARFFormValue and try to extract an signed constant.
237 /// \param V and optional DWARFFormValue to attempt to extract the value from.
238 /// \returns an optional value that contains a value if the form value
239 /// was valid and has a signed constant form.
240 inline Optional
<int64_t> toSigned(const Optional
<DWARFFormValue
> &V
) {
242 return V
->getAsSignedConstant();
246 /// Take an optional DWARFFormValue and extract a signed integer.
248 /// \param V and optional DWARFFormValue to attempt to extract the value from.
249 /// \param Default the default value to return in case of failure.
250 /// \returns the extracted signed integer value or Default if the V doesn't
251 /// have a value or the form value's encoding wasn't a signed integer form.
252 inline int64_t toSigned(const Optional
<DWARFFormValue
> &V
, int64_t Default
) {
253 return toSigned(V
).getValueOr(Default
);
256 /// Take an optional DWARFFormValue and try to extract an address.
258 /// \param V and optional DWARFFormValue to attempt to extract the value from.
259 /// \returns an optional value that contains a value if the form value
260 /// was valid and has a address form.
261 inline Optional
<uint64_t> toAddress(const Optional
<DWARFFormValue
> &V
) {
263 return V
->getAsAddress();
267 inline Optional
<object::SectionedAddress
>
268 toSectionedAddress(const Optional
<DWARFFormValue
> &V
) {
270 return V
->getAsSectionedAddress();
274 /// Take an optional DWARFFormValue and extract a address.
276 /// \param V and optional DWARFFormValue to attempt to extract the value from.
277 /// \param Default the default value to return in case of failure.
278 /// \returns the extracted address value or Default if the V doesn't have a
279 /// value or the form value's encoding wasn't an address form.
280 inline uint64_t toAddress(const Optional
<DWARFFormValue
> &V
, uint64_t Default
) {
281 return toAddress(V
).getValueOr(Default
);
284 /// Take an optional DWARFFormValue and try to extract an section offset.
286 /// \param V and optional DWARFFormValue to attempt to extract the value from.
287 /// \returns an optional value that contains a value if the form value
288 /// was valid and has a section offset form.
289 inline Optional
<uint64_t> toSectionOffset(const Optional
<DWARFFormValue
> &V
) {
291 return V
->getAsSectionOffset();
295 /// Take an optional DWARFFormValue and extract a section offset.
297 /// \param V and optional DWARFFormValue to attempt to extract the value from.
298 /// \param Default the default value to return in case of failure.
299 /// \returns the extracted section offset value or Default if the V doesn't
300 /// have a value or the form value's encoding wasn't a section offset form.
301 inline uint64_t toSectionOffset(const Optional
<DWARFFormValue
> &V
,
303 return toSectionOffset(V
).getValueOr(Default
);
306 /// Take an optional DWARFFormValue and try to extract block data.
308 /// \param V and optional DWARFFormValue to attempt to extract the value from.
309 /// \returns an optional value that contains a value if the form value
310 /// was valid and has a block form.
311 inline Optional
<ArrayRef
<uint8_t>> toBlock(const Optional
<DWARFFormValue
> &V
) {
313 return V
->getAsBlock();
317 } // end namespace dwarf
319 } // end namespace llvm
321 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H