DWARFFormValue.h revision 341825
1321369Sdim//===- DWARFFormValue.h -----------------------------------------*- C++ -*-===// 2283625Sdim// 3283625Sdim// The LLVM Compiler Infrastructure 4283625Sdim// 5283625Sdim// This file is distributed under the University of Illinois Open Source 6283625Sdim// License. See LICENSE.TXT for details. 7283625Sdim// 8283625Sdim//===----------------------------------------------------------------------===// 9283625Sdim 10283625Sdim#ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H 11283625Sdim#define LLVM_DEBUGINFO_DWARFFORMVALUE_H 12283625Sdim 13321369Sdim#include "llvm/ADT/ArrayRef.h" 14321369Sdim#include "llvm/ADT/None.h" 15283625Sdim#include "llvm/ADT/Optional.h" 16321369Sdim#include "llvm/BinaryFormat/Dwarf.h" 17327952Sdim#include "llvm/DebugInfo/DIContext.h" 18321369Sdim#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 19321369Sdim#include <cstdint> 20283625Sdim 21283625Sdimnamespace llvm { 22283625Sdim 23341825Sdimclass DWARFContext; 24283625Sdimclass DWARFUnit; 25283625Sdimclass raw_ostream; 26283625Sdim 27283625Sdimclass DWARFFormValue { 28283625Sdimpublic: 29283625Sdim enum FormClass { 30283625Sdim FC_Unknown, 31283625Sdim FC_Address, 32283625Sdim FC_Block, 33283625Sdim FC_Constant, 34283625Sdim FC_String, 35283625Sdim FC_Flag, 36283625Sdim FC_Reference, 37283625Sdim FC_Indirect, 38283625Sdim FC_SectionOffset, 39283625Sdim FC_Exprloc 40283625Sdim }; 41283625Sdim 42283625Sdimprivate: 43283625Sdim struct ValueType { 44321369Sdim ValueType() { uval = 0; } 45283625Sdim 46283625Sdim union { 47283625Sdim uint64_t uval; 48283625Sdim int64_t sval; 49321369Sdim const char *cstr; 50283625Sdim }; 51321369Sdim const uint8_t *data = nullptr; 52321369Sdim uint64_t SectionIndex; /// Section index for reference forms. 53283625Sdim }; 54283625Sdim 55321369Sdim dwarf::Form Form; /// Form for this value. 56321369Sdim ValueType Value; /// Contains all data for the form. 57321369Sdim const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time. 58341825Sdim const DWARFContext *C = nullptr; /// Context for extract time. 59283625Sdimpublic: 60321369Sdim DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {} 61321369Sdim 62314564Sdim dwarf::Form getForm() const { return Form; } 63321369Sdim uint64_t getRawUValue() const { return Value.uval; } 64321369Sdim uint64_t getSectionIndex() const { return Value.SectionIndex; } 65314564Sdim void setForm(dwarf::Form F) { Form = F; } 66314564Sdim void setUValue(uint64_t V) { Value.uval = V; } 67314564Sdim void setSValue(int64_t V) { Value.sval = V; } 68314564Sdim void setPValue(const char *V) { Value.cstr = V; } 69321369Sdim 70321369Sdim void setBlockValue(const ArrayRef<uint8_t> &Data) { 71321369Sdim Value.data = Data.data(); 72321369Sdim setUValue(Data.size()); 73321369Sdim } 74321369Sdim 75283625Sdim bool isFormClass(FormClass FC) const; 76314564Sdim const DWARFUnit *getUnit() const { return U; } 77327952Sdim void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const; 78283625Sdim 79327952Sdim /// Extracts a value in \p Data at offset \p *OffsetPtr. The information 80327952Sdim /// in \p FormParams is needed to interpret some forms. The optional 81341825Sdim /// \p Context and \p Unit allows extracting information if the form refers 82341825Sdim /// to other sections (e.g., .debug_str). 83321369Sdim bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, 84341825Sdim dwarf::FormParams FormParams, 85341825Sdim const DWARFContext *Context = nullptr, 86341825Sdim const DWARFUnit *Unit = nullptr); 87321369Sdim 88341825Sdim bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, 89341825Sdim dwarf::FormParams FormParams, const DWARFUnit *U) { 90341825Sdim return extractValue(Data, OffsetPtr, FormParams, nullptr, U); 91341825Sdim } 92341825Sdim 93283625Sdim bool isInlinedCStr() const { 94321369Sdim return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr; 95283625Sdim } 96283625Sdim 97283625Sdim /// getAsFoo functions below return the extracted value as Foo if only 98283625Sdim /// DWARFFormValue has form class is suitable for representing Foo. 99314564Sdim Optional<uint64_t> getAsReference() const; 100283625Sdim Optional<uint64_t> getAsUnsignedConstant() const; 101283625Sdim Optional<int64_t> getAsSignedConstant() const; 102314564Sdim Optional<const char *> getAsCString() const; 103314564Sdim Optional<uint64_t> getAsAddress() const; 104283625Sdim Optional<uint64_t> getAsSectionOffset() const; 105283625Sdim Optional<ArrayRef<uint8_t>> getAsBlock() const; 106314564Sdim Optional<uint64_t> getAsCStringOffset() const; 107314564Sdim Optional<uint64_t> getAsReferenceUVal() const; 108321369Sdim 109321369Sdim /// Skip a form's value in \p DebugInfoData at the offset specified by 110321369Sdim /// \p OffsetPtr. 111314564Sdim /// 112321369Sdim /// Skips the bytes for the current form and updates the offset. 113314564Sdim /// 114321369Sdim /// \param DebugInfoData The data where we want to skip the value. 115321369Sdim /// \param OffsetPtr A reference to the offset that will be updated. 116321369Sdim /// \param Params DWARF parameters to help interpret forms. 117314564Sdim /// \returns true on success, false if the form was not skipped. 118321369Sdim bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, 119341825Sdim const dwarf::FormParams Params) const { 120321369Sdim return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params); 121321369Sdim } 122321369Sdim 123321369Sdim /// Skip a form's value in \p DebugInfoData at the offset specified by 124321369Sdim /// \p OffsetPtr. 125314564Sdim /// 126321369Sdim /// Skips the bytes for the specified form and updates the offset. 127314564Sdim /// 128321369Sdim /// \param Form The DW_FORM enumeration that indicates the form to skip. 129321369Sdim /// \param DebugInfoData The data where we want to skip the value. 130321369Sdim /// \param OffsetPtr A reference to the offset that will be updated. 131321369Sdim /// \param FormParams DWARF parameters to help interpret forms. 132314564Sdim /// \returns true on success, false if the form was not skipped. 133321369Sdim static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData, 134341825Sdim uint32_t *OffsetPtr, 135341825Sdim const dwarf::FormParams FormParams); 136283625Sdim 137283625Sdimprivate: 138314564Sdim void dumpString(raw_ostream &OS) const; 139283625Sdim}; 140283625Sdim 141321369Sdimnamespace dwarf { 142321369Sdim 143321369Sdim/// Take an optional DWARFFormValue and try to extract a string value from it. 144321369Sdim/// 145321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 146321369Sdim/// \returns an optional value that contains a value if the form value 147321369Sdim/// was valid and was a string. 148321369Sdiminline Optional<const char *> toString(const Optional<DWARFFormValue> &V) { 149321369Sdim if (V) 150321369Sdim return V->getAsCString(); 151321369Sdim return None; 152285181Sdim} 153283625Sdim 154321369Sdim/// Take an optional DWARFFormValue and extract a string value from it. 155321369Sdim/// 156321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 157321369Sdim/// \param Default the default value to return in case of failure. 158321369Sdim/// \returns the string value or Default if the V doesn't have a value or the 159321369Sdim/// form value's encoding wasn't a string. 160321369Sdiminline const char *toString(const Optional<DWARFFormValue> &V, 161321369Sdim const char *Default) { 162321369Sdim return toString(V).getValueOr(Default); 163321369Sdim} 164321369Sdim 165321369Sdim/// Take an optional DWARFFormValue and try to extract an unsigned constant. 166321369Sdim/// 167321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 168321369Sdim/// \returns an optional value that contains a value if the form value 169321369Sdim/// was valid and has a unsigned constant form. 170321369Sdiminline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) { 171321369Sdim if (V) 172321369Sdim return V->getAsUnsignedConstant(); 173321369Sdim return None; 174321369Sdim} 175321369Sdim 176321369Sdim/// Take an optional DWARFFormValue and extract a unsigned constant. 177321369Sdim/// 178321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 179321369Sdim/// \param Default the default value to return in case of failure. 180321369Sdim/// \returns the extracted unsigned value or Default if the V doesn't have a 181321369Sdim/// value or the form value's encoding wasn't an unsigned constant form. 182321369Sdiminline uint64_t toUnsigned(const Optional<DWARFFormValue> &V, 183321369Sdim uint64_t Default) { 184321369Sdim return toUnsigned(V).getValueOr(Default); 185321369Sdim} 186321369Sdim 187321369Sdim/// Take an optional DWARFFormValue and try to extract an reference. 188321369Sdim/// 189321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 190321369Sdim/// \returns an optional value that contains a value if the form value 191321369Sdim/// was valid and has a reference form. 192321369Sdiminline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) { 193321369Sdim if (V) 194321369Sdim return V->getAsReference(); 195321369Sdim return None; 196321369Sdim} 197321369Sdim 198321369Sdim/// Take an optional DWARFFormValue and extract a reference. 199321369Sdim/// 200321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 201321369Sdim/// \param Default the default value to return in case of failure. 202321369Sdim/// \returns the extracted reference value or Default if the V doesn't have a 203321369Sdim/// value or the form value's encoding wasn't a reference form. 204321369Sdiminline uint64_t toReference(const Optional<DWARFFormValue> &V, 205321369Sdim uint64_t Default) { 206321369Sdim return toReference(V).getValueOr(Default); 207321369Sdim} 208321369Sdim 209321369Sdim/// Take an optional DWARFFormValue and try to extract an signed constant. 210321369Sdim/// 211321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 212321369Sdim/// \returns an optional value that contains a value if the form value 213321369Sdim/// was valid and has a signed constant form. 214321369Sdiminline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) { 215321369Sdim if (V) 216321369Sdim return V->getAsSignedConstant(); 217321369Sdim return None; 218321369Sdim} 219321369Sdim 220321369Sdim/// Take an optional DWARFFormValue and extract a signed integer. 221321369Sdim/// 222321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 223321369Sdim/// \param Default the default value to return in case of failure. 224321369Sdim/// \returns the extracted signed integer value or Default if the V doesn't 225321369Sdim/// have a value or the form value's encoding wasn't a signed integer form. 226321369Sdiminline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) { 227321369Sdim return toSigned(V).getValueOr(Default); 228321369Sdim} 229321369Sdim 230321369Sdim/// Take an optional DWARFFormValue and try to extract an address. 231321369Sdim/// 232321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 233321369Sdim/// \returns an optional value that contains a value if the form value 234321369Sdim/// was valid and has a address form. 235321369Sdiminline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) { 236321369Sdim if (V) 237321369Sdim return V->getAsAddress(); 238321369Sdim return None; 239321369Sdim} 240321369Sdim 241321369Sdim/// Take an optional DWARFFormValue and extract a address. 242321369Sdim/// 243321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 244321369Sdim/// \param Default the default value to return in case of failure. 245321369Sdim/// \returns the extracted address value or Default if the V doesn't have a 246321369Sdim/// value or the form value's encoding wasn't an address form. 247321369Sdiminline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) { 248321369Sdim return toAddress(V).getValueOr(Default); 249321369Sdim} 250321369Sdim 251321369Sdim/// Take an optional DWARFFormValue and try to extract an section offset. 252321369Sdim/// 253321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 254321369Sdim/// \returns an optional value that contains a value if the form value 255321369Sdim/// was valid and has a section offset form. 256321369Sdiminline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) { 257321369Sdim if (V) 258321369Sdim return V->getAsSectionOffset(); 259321369Sdim return None; 260321369Sdim} 261321369Sdim 262321369Sdim/// Take an optional DWARFFormValue and extract a section offset. 263321369Sdim/// 264321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 265321369Sdim/// \param Default the default value to return in case of failure. 266321369Sdim/// \returns the extracted section offset value or Default if the V doesn't 267321369Sdim/// have a value or the form value's encoding wasn't a section offset form. 268321369Sdiminline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V, 269321369Sdim uint64_t Default) { 270321369Sdim return toSectionOffset(V).getValueOr(Default); 271321369Sdim} 272321369Sdim 273321369Sdim/// Take an optional DWARFFormValue and try to extract block data. 274321369Sdim/// 275321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 276321369Sdim/// \returns an optional value that contains a value if the form value 277321369Sdim/// was valid and has a block form. 278321369Sdiminline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) { 279321369Sdim if (V) 280321369Sdim return V->getAsBlock(); 281321369Sdim return None; 282321369Sdim} 283321369Sdim 284321369Sdim} // end namespace dwarf 285321369Sdim 286321369Sdim} // end namespace llvm 287321369Sdim 288321369Sdim#endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H 289