• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/None.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/DebugInfo/DIContext.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 class DWARFContext;
24 class DWARFUnit;
25 class raw_ostream;
26 
27 class DWARFFormValue {
28 public:
29   enum FormClass {
30     FC_Unknown,
31     FC_Address,
32     FC_Block,
33     FC_Constant,
34     FC_String,
35     FC_Flag,
36     FC_Reference,
37     FC_Indirect,
38     FC_SectionOffset,
39     FC_Exprloc
40   };
41 
42 private:
43   struct ValueType {
ValueTypeValueType44     ValueType() { uval = 0; }
45 
46     union {
47       uint64_t uval;
48       int64_t sval;
49       const char *cstr;
50     };
51     const uint8_t *data = nullptr;
52     uint64_t SectionIndex;      /// Section index for reference forms.
53   };
54 
55   dwarf::Form Form;             /// Form for this value.
56   ValueType Value;              /// Contains all data for the form.
57   const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
58   const DWARFContext *C = nullptr; /// Context for extract time.
59 public:
Form(F)60   DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
61 
getForm()62   dwarf::Form getForm() const { return Form; }
getRawUValue()63   uint64_t getRawUValue() const { return Value.uval; }
getSectionIndex()64   uint64_t getSectionIndex() const { return Value.SectionIndex; }
setForm(dwarf::Form F)65   void setForm(dwarf::Form F) { Form = F; }
setUValue(uint64_t V)66   void setUValue(uint64_t V) { Value.uval = V; }
setSValue(int64_t V)67   void setSValue(int64_t V) { Value.sval = V; }
setPValue(const char * V)68   void setPValue(const char *V) { Value.cstr = V; }
69 
setBlockValue(const ArrayRef<uint8_t> & Data)70   void setBlockValue(const ArrayRef<uint8_t> &Data) {
71     Value.data = Data.data();
72     setUValue(Data.size());
73   }
74 
75   bool isFormClass(FormClass FC) const;
getUnit()76   const DWARFUnit *getUnit() const { return U; }
77   void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
78 
79   /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
80   /// in \p FormParams is needed to interpret some forms. The optional
81   /// \p Context and \p Unit allows extracting information if the form refers
82   /// to other sections (e.g., .debug_str).
83   bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
84                     dwarf::FormParams FormParams,
85                     const DWARFContext *Context = nullptr,
86                     const DWARFUnit *Unit = nullptr);
87 
extractValue(const DWARFDataExtractor & Data,uint32_t * OffsetPtr,dwarf::FormParams FormParams,const DWARFUnit * U)88   bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
89                     dwarf::FormParams FormParams, const DWARFUnit *U) {
90     return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
91   }
92 
isInlinedCStr()93   bool isInlinedCStr() const {
94     return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
95   }
96 
97   /// getAsFoo functions below return the extracted value as Foo if only
98   /// DWARFFormValue has form class is suitable for representing Foo.
99   Optional<uint64_t> getAsReference() const;
100   Optional<uint64_t> getAsUnsignedConstant() const;
101   Optional<int64_t> getAsSignedConstant() const;
102   Optional<const char *> getAsCString() const;
103   Optional<uint64_t> getAsAddress() const;
104   Optional<uint64_t> getAsSectionOffset() const;
105   Optional<ArrayRef<uint8_t>> getAsBlock() const;
106   Optional<uint64_t> getAsCStringOffset() const;
107   Optional<uint64_t> getAsReferenceUVal() const;
108 
109   /// Skip a form's value in \p DebugInfoData at the offset specified by
110   /// \p OffsetPtr.
111   ///
112   /// Skips the bytes for the current form and updates the offset.
113   ///
114   /// \param DebugInfoData The data where we want to skip the value.
115   /// \param OffsetPtr A reference to the offset that will be updated.
116   /// \param Params DWARF parameters to help interpret forms.
117   /// \returns true on success, false if the form was not skipped.
skipValue(DataExtractor DebugInfoData,uint32_t * OffsetPtr,const dwarf::FormParams Params)118   bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
119                  const dwarf::FormParams Params) const {
120     return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
121   }
122 
123   /// Skip a form's value in \p DebugInfoData at the offset specified by
124   /// \p OffsetPtr.
125   ///
126   /// Skips the bytes for the specified form and updates the offset.
127   ///
128   /// \param Form The DW_FORM enumeration that indicates the form to skip.
129   /// \param DebugInfoData The data where we want to skip the value.
130   /// \param OffsetPtr A reference to the offset that will be updated.
131   /// \param FormParams DWARF parameters to help interpret forms.
132   /// \returns true on success, false if the form was not skipped.
133   static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
134                         uint32_t *OffsetPtr,
135                         const dwarf::FormParams FormParams);
136 
137 private:
138   void dumpString(raw_ostream &OS) const;
139 };
140 
141 namespace dwarf {
142 
143 /// Take an optional DWARFFormValue and try to extract a string value from it.
144 ///
145 /// \param V and optional DWARFFormValue to attempt to extract the value from.
146 /// \returns an optional value that contains a value if the form value
147 /// was valid and was a string.
toString(const Optional<DWARFFormValue> & V)148 inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
149   if (V)
150     return V->getAsCString();
151   return None;
152 }
153 
154 /// Take an optional DWARFFormValue and extract a string value from it.
155 ///
156 /// \param V and optional DWARFFormValue to attempt to extract the value from.
157 /// \param Default the default value to return in case of failure.
158 /// \returns the string value or Default if the V doesn't have a value or the
159 /// form value's encoding wasn't a string.
toString(const Optional<DWARFFormValue> & V,const char * Default)160 inline const char *toString(const Optional<DWARFFormValue> &V,
161                             const char *Default) {
162   return toString(V).getValueOr(Default);
163 }
164 
165 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
166 ///
167 /// \param V and optional DWARFFormValue to attempt to extract the value from.
168 /// \returns an optional value that contains a value if the form value
169 /// was valid and has a unsigned constant form.
toUnsigned(const Optional<DWARFFormValue> & V)170 inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
171   if (V)
172     return V->getAsUnsignedConstant();
173   return None;
174 }
175 
176 /// Take an optional DWARFFormValue and extract a unsigned constant.
177 ///
178 /// \param V and optional DWARFFormValue to attempt to extract the value from.
179 /// \param Default the default value to return in case of failure.
180 /// \returns the extracted unsigned value or Default if the V doesn't have a
181 /// value or the form value's encoding wasn't an unsigned constant form.
toUnsigned(const Optional<DWARFFormValue> & V,uint64_t Default)182 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
183                            uint64_t Default) {
184   return toUnsigned(V).getValueOr(Default);
185 }
186 
187 /// Take an optional DWARFFormValue and try to extract an reference.
188 ///
189 /// \param V and optional DWARFFormValue to attempt to extract the value from.
190 /// \returns an optional value that contains a value if the form value
191 /// was valid and has a reference form.
toReference(const Optional<DWARFFormValue> & V)192 inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
193   if (V)
194     return V->getAsReference();
195   return None;
196 }
197 
198 /// Take an optional DWARFFormValue and extract a reference.
199 ///
200 /// \param V and optional DWARFFormValue to attempt to extract the value from.
201 /// \param Default the default value to return in case of failure.
202 /// \returns the extracted reference value or Default if the V doesn't have a
203 /// value or the form value's encoding wasn't a reference form.
toReference(const Optional<DWARFFormValue> & V,uint64_t Default)204 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
205                             uint64_t Default) {
206   return toReference(V).getValueOr(Default);
207 }
208 
209 /// Take an optional DWARFFormValue and try to extract an signed constant.
210 ///
211 /// \param V and optional DWARFFormValue to attempt to extract the value from.
212 /// \returns an optional value that contains a value if the form value
213 /// was valid and has a signed constant form.
toSigned(const Optional<DWARFFormValue> & V)214 inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
215   if (V)
216     return V->getAsSignedConstant();
217   return None;
218 }
219 
220 /// Take an optional DWARFFormValue and extract a signed integer.
221 ///
222 /// \param V and optional DWARFFormValue to attempt to extract the value from.
223 /// \param Default the default value to return in case of failure.
224 /// \returns the extracted signed integer value or Default if the V doesn't
225 /// have a value or the form value's encoding wasn't a signed integer form.
toSigned(const Optional<DWARFFormValue> & V,int64_t Default)226 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
227   return toSigned(V).getValueOr(Default);
228 }
229 
230 /// Take an optional DWARFFormValue and try to extract an address.
231 ///
232 /// \param V and optional DWARFFormValue to attempt to extract the value from.
233 /// \returns an optional value that contains a value if the form value
234 /// was valid and has a address form.
toAddress(const Optional<DWARFFormValue> & V)235 inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
236   if (V)
237     return V->getAsAddress();
238   return None;
239 }
240 
241 /// Take an optional DWARFFormValue and extract a address.
242 ///
243 /// \param V and optional DWARFFormValue to attempt to extract the value from.
244 /// \param Default the default value to return in case of failure.
245 /// \returns the extracted address value or Default if the V doesn't have a
246 /// value or the form value's encoding wasn't an address form.
toAddress(const Optional<DWARFFormValue> & V,uint64_t Default)247 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
248   return toAddress(V).getValueOr(Default);
249 }
250 
251 /// Take an optional DWARFFormValue and try to extract an section offset.
252 ///
253 /// \param V and optional DWARFFormValue to attempt to extract the value from.
254 /// \returns an optional value that contains a value if the form value
255 /// was valid and has a section offset form.
toSectionOffset(const Optional<DWARFFormValue> & V)256 inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
257   if (V)
258     return V->getAsSectionOffset();
259   return None;
260 }
261 
262 /// Take an optional DWARFFormValue and extract a section offset.
263 ///
264 /// \param V and optional DWARFFormValue to attempt to extract the value from.
265 /// \param Default the default value to return in case of failure.
266 /// \returns the extracted section offset value or Default if the V doesn't
267 /// have a value or the form value's encoding wasn't a section offset form.
toSectionOffset(const Optional<DWARFFormValue> & V,uint64_t Default)268 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
269                                 uint64_t Default) {
270   return toSectionOffset(V).getValueOr(Default);
271 }
272 
273 /// Take an optional DWARFFormValue and try to extract block data.
274 ///
275 /// \param V and optional DWARFFormValue to attempt to extract the value from.
276 /// \returns an optional value that contains a value if the form value
277 /// was valid and has a block form.
toBlock(const Optional<DWARFFormValue> & V)278 inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
279   if (V)
280     return V->getAsBlock();
281   return None;
282 }
283 
284 } // end namespace dwarf
285 
286 } // end namespace llvm
287 
288 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
289