DWARFFormValue.h revision 344779
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; } 64314564Sdim void setForm(dwarf::Form F) { Form = F; } 65314564Sdim void setUValue(uint64_t V) { Value.uval = V; } 66314564Sdim void setSValue(int64_t V) { Value.sval = V; } 67314564Sdim void setPValue(const char *V) { Value.cstr = V; } 68321369Sdim 69321369Sdim void setBlockValue(const ArrayRef<uint8_t> &Data) { 70321369Sdim Value.data = Data.data(); 71321369Sdim setUValue(Data.size()); 72321369Sdim } 73321369Sdim 74283625Sdim bool isFormClass(FormClass FC) const; 75314564Sdim const DWARFUnit *getUnit() const { return U; } 76327952Sdim void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const; 77344779Sdim void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, 78344779Sdim SectionedAddress SA) const; 79344779Sdim static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, 80344779Sdim DIDumpOptions DumpOpts, uint64_t SectionIndex); 81283625Sdim 82327952Sdim /// Extracts a value in \p Data at offset \p *OffsetPtr. The information 83327952Sdim /// in \p FormParams is needed to interpret some forms. The optional 84341825Sdim /// \p Context and \p Unit allows extracting information if the form refers 85341825Sdim /// to other sections (e.g., .debug_str). 86321369Sdim bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, 87341825Sdim dwarf::FormParams FormParams, 88341825Sdim const DWARFContext *Context = nullptr, 89341825Sdim const DWARFUnit *Unit = nullptr); 90321369Sdim 91341825Sdim bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, 92341825Sdim dwarf::FormParams FormParams, const DWARFUnit *U) { 93341825Sdim return extractValue(Data, OffsetPtr, FormParams, nullptr, U); 94341825Sdim } 95341825Sdim 96283625Sdim bool isInlinedCStr() const { 97321369Sdim return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr; 98283625Sdim } 99283625Sdim 100283625Sdim /// getAsFoo functions below return the extracted value as Foo if only 101283625Sdim /// DWARFFormValue has form class is suitable for representing Foo. 102314564Sdim Optional<uint64_t> getAsReference() const; 103283625Sdim Optional<uint64_t> getAsUnsignedConstant() const; 104283625Sdim Optional<int64_t> getAsSignedConstant() const; 105314564Sdim Optional<const char *> getAsCString() const; 106314564Sdim Optional<uint64_t> getAsAddress() const; 107344779Sdim Optional<SectionedAddress> getAsSectionedAddress() const; 108283625Sdim Optional<uint64_t> getAsSectionOffset() const; 109283625Sdim Optional<ArrayRef<uint8_t>> getAsBlock() const; 110314564Sdim Optional<uint64_t> getAsCStringOffset() const; 111314564Sdim Optional<uint64_t> getAsReferenceUVal() const; 112321369Sdim 113321369Sdim /// Skip a form's value in \p DebugInfoData at the offset specified by 114321369Sdim /// \p OffsetPtr. 115314564Sdim /// 116321369Sdim /// Skips the bytes for the current form and updates the offset. 117314564Sdim /// 118321369Sdim /// \param DebugInfoData The data where we want to skip the value. 119321369Sdim /// \param OffsetPtr A reference to the offset that will be updated. 120321369Sdim /// \param Params DWARF parameters to help interpret forms. 121314564Sdim /// \returns true on success, false if the form was not skipped. 122321369Sdim bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, 123341825Sdim const dwarf::FormParams Params) const { 124321369Sdim return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params); 125321369Sdim } 126321369Sdim 127321369Sdim /// Skip a form's value in \p DebugInfoData at the offset specified by 128321369Sdim /// \p OffsetPtr. 129314564Sdim /// 130321369Sdim /// Skips the bytes for the specified form and updates the offset. 131314564Sdim /// 132321369Sdim /// \param Form The DW_FORM enumeration that indicates the form to skip. 133321369Sdim /// \param DebugInfoData The data where we want to skip the value. 134321369Sdim /// \param OffsetPtr A reference to the offset that will be updated. 135321369Sdim /// \param FormParams DWARF parameters to help interpret forms. 136314564Sdim /// \returns true on success, false if the form was not skipped. 137321369Sdim static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData, 138341825Sdim uint32_t *OffsetPtr, 139341825Sdim const dwarf::FormParams FormParams); 140283625Sdim 141283625Sdimprivate: 142314564Sdim void dumpString(raw_ostream &OS) const; 143283625Sdim}; 144283625Sdim 145321369Sdimnamespace dwarf { 146321369Sdim 147321369Sdim/// Take an optional DWARFFormValue and try to extract a string value from it. 148321369Sdim/// 149321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 150321369Sdim/// \returns an optional value that contains a value if the form value 151321369Sdim/// was valid and was a string. 152321369Sdiminline Optional<const char *> toString(const Optional<DWARFFormValue> &V) { 153321369Sdim if (V) 154321369Sdim return V->getAsCString(); 155321369Sdim return None; 156285181Sdim} 157283625Sdim 158321369Sdim/// Take an optional DWARFFormValue and extract a string value from it. 159321369Sdim/// 160321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 161321369Sdim/// \param Default the default value to return in case of failure. 162321369Sdim/// \returns the string value or Default if the V doesn't have a value or the 163321369Sdim/// form value's encoding wasn't a string. 164321369Sdiminline const char *toString(const Optional<DWARFFormValue> &V, 165321369Sdim const char *Default) { 166321369Sdim return toString(V).getValueOr(Default); 167321369Sdim} 168321369Sdim 169321369Sdim/// Take an optional DWARFFormValue and try to extract an unsigned constant. 170321369Sdim/// 171321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 172321369Sdim/// \returns an optional value that contains a value if the form value 173321369Sdim/// was valid and has a unsigned constant form. 174321369Sdiminline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) { 175321369Sdim if (V) 176321369Sdim return V->getAsUnsignedConstant(); 177321369Sdim return None; 178321369Sdim} 179321369Sdim 180321369Sdim/// Take an optional DWARFFormValue and extract a unsigned constant. 181321369Sdim/// 182321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 183321369Sdim/// \param Default the default value to return in case of failure. 184321369Sdim/// \returns the extracted unsigned value or Default if the V doesn't have a 185321369Sdim/// value or the form value's encoding wasn't an unsigned constant form. 186321369Sdiminline uint64_t toUnsigned(const Optional<DWARFFormValue> &V, 187321369Sdim uint64_t Default) { 188321369Sdim return toUnsigned(V).getValueOr(Default); 189321369Sdim} 190321369Sdim 191321369Sdim/// Take an optional DWARFFormValue and try to extract an reference. 192321369Sdim/// 193321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 194321369Sdim/// \returns an optional value that contains a value if the form value 195321369Sdim/// was valid and has a reference form. 196321369Sdiminline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) { 197321369Sdim if (V) 198321369Sdim return V->getAsReference(); 199321369Sdim return None; 200321369Sdim} 201321369Sdim 202321369Sdim/// Take an optional DWARFFormValue and extract a reference. 203321369Sdim/// 204321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 205321369Sdim/// \param Default the default value to return in case of failure. 206321369Sdim/// \returns the extracted reference value or Default if the V doesn't have a 207321369Sdim/// value or the form value's encoding wasn't a reference form. 208321369Sdiminline uint64_t toReference(const Optional<DWARFFormValue> &V, 209321369Sdim uint64_t Default) { 210321369Sdim return toReference(V).getValueOr(Default); 211321369Sdim} 212321369Sdim 213321369Sdim/// Take an optional DWARFFormValue and try to extract an signed constant. 214321369Sdim/// 215321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 216321369Sdim/// \returns an optional value that contains a value if the form value 217321369Sdim/// was valid and has a signed constant form. 218321369Sdiminline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) { 219321369Sdim if (V) 220321369Sdim return V->getAsSignedConstant(); 221321369Sdim return None; 222321369Sdim} 223321369Sdim 224321369Sdim/// Take an optional DWARFFormValue and extract a signed integer. 225321369Sdim/// 226321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 227321369Sdim/// \param Default the default value to return in case of failure. 228321369Sdim/// \returns the extracted signed integer value or Default if the V doesn't 229321369Sdim/// have a value or the form value's encoding wasn't a signed integer form. 230321369Sdiminline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) { 231321369Sdim return toSigned(V).getValueOr(Default); 232321369Sdim} 233321369Sdim 234321369Sdim/// Take an optional DWARFFormValue and try to extract an address. 235321369Sdim/// 236321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 237321369Sdim/// \returns an optional value that contains a value if the form value 238321369Sdim/// was valid and has a address form. 239321369Sdiminline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) { 240321369Sdim if (V) 241321369Sdim return V->getAsAddress(); 242321369Sdim return None; 243321369Sdim} 244321369Sdim 245344779Sdiminline Optional<SectionedAddress> 246344779SdimtoSectionedAddress(const Optional<DWARFFormValue> &V) { 247344779Sdim if (V) 248344779Sdim return V->getAsSectionedAddress(); 249344779Sdim return None; 250344779Sdim} 251344779Sdim 252321369Sdim/// Take an optional DWARFFormValue and extract a address. 253321369Sdim/// 254321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 255321369Sdim/// \param Default the default value to return in case of failure. 256321369Sdim/// \returns the extracted address value or Default if the V doesn't have a 257321369Sdim/// value or the form value's encoding wasn't an address form. 258321369Sdiminline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) { 259321369Sdim return toAddress(V).getValueOr(Default); 260321369Sdim} 261321369Sdim 262321369Sdim/// Take an optional DWARFFormValue and try to extract an section offset. 263321369Sdim/// 264321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 265321369Sdim/// \returns an optional value that contains a value if the form value 266321369Sdim/// was valid and has a section offset form. 267321369Sdiminline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) { 268321369Sdim if (V) 269321369Sdim return V->getAsSectionOffset(); 270321369Sdim return None; 271321369Sdim} 272321369Sdim 273321369Sdim/// Take an optional DWARFFormValue and extract a section offset. 274321369Sdim/// 275321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 276321369Sdim/// \param Default the default value to return in case of failure. 277321369Sdim/// \returns the extracted section offset value or Default if the V doesn't 278321369Sdim/// have a value or the form value's encoding wasn't a section offset form. 279321369Sdiminline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V, 280321369Sdim uint64_t Default) { 281321369Sdim return toSectionOffset(V).getValueOr(Default); 282321369Sdim} 283321369Sdim 284321369Sdim/// Take an optional DWARFFormValue and try to extract block data. 285321369Sdim/// 286321369Sdim/// \param V and optional DWARFFormValue to attempt to extract the value from. 287321369Sdim/// \returns an optional value that contains a value if the form value 288321369Sdim/// was valid and has a block form. 289321369Sdiminline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) { 290321369Sdim if (V) 291321369Sdim return V->getAsBlock(); 292321369Sdim return None; 293321369Sdim} 294321369Sdim 295321369Sdim} // end namespace dwarf 296321369Sdim 297321369Sdim} // end namespace llvm 298321369Sdim 299321369Sdim#endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H 300