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; }
50 const uint8_t *data
= nullptr;
51 uint64_t SectionIndex
; /// Section index for reference forms.
54 dwarf::Form Form
; /// Form for this value.
55 ValueType Value
; /// Contains all data for the form.
56 const DWARFUnit
*U
= nullptr; /// Remember the DWARFUnit at extract time.
57 const DWARFContext
*C
= nullptr; /// Context for extract time.
59 DWARFFormValue(dwarf::Form F
= dwarf::Form(0)) : Form(F
) {}
61 dwarf::Form
getForm() const { return Form
; }
62 uint64_t getRawUValue() const { return Value
.uval
; }
63 void setForm(dwarf::Form F
) { Form
= F
; }
64 void setUValue(uint64_t V
) { Value
.uval
= V
; }
65 void setSValue(int64_t V
) { Value
.sval
= V
; }
66 void setPValue(const char *V
) { Value
.cstr
= V
; }
68 void setBlockValue(const ArrayRef
<uint8_t> &Data
) {
69 Value
.data
= Data
.data();
70 setUValue(Data
.size());
73 bool isFormClass(FormClass FC
) const;
74 const DWARFUnit
*getUnit() const { return U
; }
75 void dump(raw_ostream
&OS
, DIDumpOptions DumpOpts
= DIDumpOptions()) const;
76 void dumpSectionedAddress(raw_ostream
&OS
, DIDumpOptions DumpOpts
,
77 SectionedAddress SA
) const;
78 static void dumpAddressSection(const DWARFObject
&Obj
, raw_ostream
&OS
,
79 DIDumpOptions DumpOpts
, uint64_t SectionIndex
);
81 /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
82 /// in \p FormParams is needed to interpret some forms. The optional
83 /// \p Context and \p Unit allows extracting information if the form refers
84 /// to other sections (e.g., .debug_str).
85 bool extractValue(const DWARFDataExtractor
&Data
, uint32_t *OffsetPtr
,
86 dwarf::FormParams FormParams
,
87 const DWARFContext
*Context
= nullptr,
88 const DWARFUnit
*Unit
= nullptr);
90 bool extractValue(const DWARFDataExtractor
&Data
, uint32_t *OffsetPtr
,
91 dwarf::FormParams FormParams
, const DWARFUnit
*U
) {
92 return extractValue(Data
, OffsetPtr
, FormParams
, nullptr, U
);
95 bool isInlinedCStr() const {
96 return Value
.data
!= nullptr && Value
.data
== (const uint8_t *)Value
.cstr
;
99 /// getAsFoo functions below return the extracted value as Foo if only
100 /// DWARFFormValue has form class is suitable for representing Foo.
101 Optional
<uint64_t> getAsReference() const;
102 Optional
<uint64_t> getAsUnsignedConstant() const;
103 Optional
<int64_t> getAsSignedConstant() const;
104 Optional
<const char *> getAsCString() const;
105 Optional
<uint64_t> getAsAddress() const;
106 Optional
<SectionedAddress
> getAsSectionedAddress() const;
107 Optional
<uint64_t> getAsSectionOffset() const;
108 Optional
<ArrayRef
<uint8_t>> getAsBlock() const;
109 Optional
<uint64_t> getAsCStringOffset() const;
110 Optional
<uint64_t> getAsReferenceUVal() const;
112 /// Skip a form's value in \p DebugInfoData at the offset specified by
115 /// Skips the bytes for the current form and updates the offset.
117 /// \param DebugInfoData The data where we want to skip the value.
118 /// \param OffsetPtr A reference to the offset that will be updated.
119 /// \param Params DWARF parameters to help interpret forms.
120 /// \returns true on success, false if the form was not skipped.
121 bool skipValue(DataExtractor DebugInfoData
, uint32_t *OffsetPtr
,
122 const dwarf::FormParams Params
) const {
123 return DWARFFormValue::skipValue(Form
, DebugInfoData
, OffsetPtr
, Params
);
126 /// Skip a form's value in \p DebugInfoData at the offset specified by
129 /// Skips the bytes for the specified form and updates the offset.
131 /// \param Form The DW_FORM enumeration that indicates the form to skip.
132 /// \param DebugInfoData The data where we want to skip the value.
133 /// \param OffsetPtr A reference to the offset that will be updated.
134 /// \param FormParams DWARF parameters to help interpret forms.
135 /// \returns true on success, false if the form was not skipped.
136 static bool skipValue(dwarf::Form Form
, DataExtractor DebugInfoData
,
138 const dwarf::FormParams FormParams
);
141 void dumpString(raw_ostream
&OS
) const;
146 /// Take an optional DWARFFormValue and try to extract a string value from it.
148 /// \param V and optional DWARFFormValue to attempt to extract the value from.
149 /// \returns an optional value that contains a value if the form value
150 /// was valid and was a string.
151 inline Optional
<const char *> toString(const Optional
<DWARFFormValue
> &V
) {
153 return V
->getAsCString();
157 /// Take an optional DWARFFormValue and extract a string value from it.
159 /// \param V and optional DWARFFormValue to attempt to extract the value from.
160 /// \param Default the default value to return in case of failure.
161 /// \returns the string value or Default if the V doesn't have a value or the
162 /// form value's encoding wasn't a string.
163 inline const char *toString(const Optional
<DWARFFormValue
> &V
,
164 const char *Default
) {
165 return toString(V
).getValueOr(Default
);
168 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
170 /// \param V and optional DWARFFormValue to attempt to extract the value from.
171 /// \returns an optional value that contains a value if the form value
172 /// was valid and has a unsigned constant form.
173 inline Optional
<uint64_t> toUnsigned(const Optional
<DWARFFormValue
> &V
) {
175 return V
->getAsUnsignedConstant();
179 /// Take an optional DWARFFormValue and extract a unsigned constant.
181 /// \param V and optional DWARFFormValue to attempt to extract the value from.
182 /// \param Default the default value to return in case of failure.
183 /// \returns the extracted unsigned value or Default if the V doesn't have a
184 /// value or the form value's encoding wasn't an unsigned constant form.
185 inline uint64_t toUnsigned(const Optional
<DWARFFormValue
> &V
,
187 return toUnsigned(V
).getValueOr(Default
);
190 /// Take an optional DWARFFormValue and try to extract an reference.
192 /// \param V and optional DWARFFormValue to attempt to extract the value from.
193 /// \returns an optional value that contains a value if the form value
194 /// was valid and has a reference form.
195 inline Optional
<uint64_t> toReference(const Optional
<DWARFFormValue
> &V
) {
197 return V
->getAsReference();
201 /// Take an optional DWARFFormValue and extract a reference.
203 /// \param V and optional DWARFFormValue to attempt to extract the value from.
204 /// \param Default the default value to return in case of failure.
205 /// \returns the extracted reference value or Default if the V doesn't have a
206 /// value or the form value's encoding wasn't a reference form.
207 inline uint64_t toReference(const Optional
<DWARFFormValue
> &V
,
209 return toReference(V
).getValueOr(Default
);
212 /// Take an optional DWARFFormValue and try to extract an signed constant.
214 /// \param V and optional DWARFFormValue to attempt to extract the value from.
215 /// \returns an optional value that contains a value if the form value
216 /// was valid and has a signed constant form.
217 inline Optional
<int64_t> toSigned(const Optional
<DWARFFormValue
> &V
) {
219 return V
->getAsSignedConstant();
223 /// Take an optional DWARFFormValue and extract a signed integer.
225 /// \param V and optional DWARFFormValue to attempt to extract the value from.
226 /// \param Default the default value to return in case of failure.
227 /// \returns the extracted signed integer value or Default if the V doesn't
228 /// have a value or the form value's encoding wasn't a signed integer form.
229 inline int64_t toSigned(const Optional
<DWARFFormValue
> &V
, int64_t Default
) {
230 return toSigned(V
).getValueOr(Default
);
233 /// Take an optional DWARFFormValue and try to extract an address.
235 /// \param V and optional DWARFFormValue to attempt to extract the value from.
236 /// \returns an optional value that contains a value if the form value
237 /// was valid and has a address form.
238 inline Optional
<uint64_t> toAddress(const Optional
<DWARFFormValue
> &V
) {
240 return V
->getAsAddress();
244 inline Optional
<SectionedAddress
>
245 toSectionedAddress(const Optional
<DWARFFormValue
> &V
) {
247 return V
->getAsSectionedAddress();
251 /// Take an optional DWARFFormValue and extract a address.
253 /// \param V and optional DWARFFormValue to attempt to extract the value from.
254 /// \param Default the default value to return in case of failure.
255 /// \returns the extracted address value or Default if the V doesn't have a
256 /// value or the form value's encoding wasn't an address form.
257 inline uint64_t toAddress(const Optional
<DWARFFormValue
> &V
, uint64_t Default
) {
258 return toAddress(V
).getValueOr(Default
);
261 /// Take an optional DWARFFormValue and try to extract an section offset.
263 /// \param V and optional DWARFFormValue to attempt to extract the value from.
264 /// \returns an optional value that contains a value if the form value
265 /// was valid and has a section offset form.
266 inline Optional
<uint64_t> toSectionOffset(const Optional
<DWARFFormValue
> &V
) {
268 return V
->getAsSectionOffset();
272 /// Take an optional DWARFFormValue and extract a section offset.
274 /// \param V and optional DWARFFormValue to attempt to extract the value from.
275 /// \param Default the default value to return in case of failure.
276 /// \returns the extracted section offset value or Default if the V doesn't
277 /// have a value or the form value's encoding wasn't a section offset form.
278 inline uint64_t toSectionOffset(const Optional
<DWARFFormValue
> &V
,
280 return toSectionOffset(V
).getValueOr(Default
);
283 /// Take an optional DWARFFormValue and try to extract block data.
285 /// \param V and optional DWARFFormValue to attempt to extract the value from.
286 /// \returns an optional value that contains a value if the form value
287 /// was valid and has a block form.
288 inline Optional
<ArrayRef
<uint8_t>> toBlock(const Optional
<DWARFFormValue
> &V
) {
290 return V
->getAsBlock();
294 } // end namespace dwarf
296 } // end namespace llvm
298 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H