• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DWARFDebugAddr.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_DWARFDEBUGADDR_H
11 #define LLVM_DEBUGINFO_DWARFDEBUGADDR_H
12 
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/DebugInfo/DIContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/Error.h"
18 #include <cstdint>
19 #include <map>
20 #include <vector>
21 
22 namespace llvm {
23 
24 class Error;
25 class raw_ostream;
26 
27 /// A class representing an address table as specified in DWARF v5.
28 /// The table consists of a header followed by an array of address values from
29 /// .debug_addr section.
30 class DWARFDebugAddrTable {
31 public:
32   struct Header {
33     /// The total length of the entries for this table, not including the length
34     /// field itself.
35     uint32_t Length = 0;
36     /// The DWARF version number.
37     uint16_t Version = 5;
38     /// The size in bytes of an address on the target architecture. For
39     /// segmented addressing, this is the size of the offset portion of the
40     /// address.
41     uint8_t AddrSize;
42     /// The size in bytes of a segment selector on the target architecture.
43     /// If the target system uses a flat address space, this value is 0.
44     uint8_t SegSize = 0;
45   };
46 
47 private:
48   dwarf::DwarfFormat Format;
49   uint32_t HeaderOffset;
50   Header HeaderData;
51   uint32_t DataSize = 0;
52   std::vector<uint64_t> Addrs;
53 
54 public:
55   void clear();
56 
57   /// Extract an entire table, including all addresses.
58   Error extract(DWARFDataExtractor Data, uint32_t *OffsetPtr,
59                 uint16_t Version, uint8_t AddrSize,
60                 std::function<void(Error)> WarnCallback);
61 
getHeaderOffset()62   uint32_t getHeaderOffset() const { return HeaderOffset; }
getAddrSize()63   uint8_t getAddrSize() const { return HeaderData.AddrSize; }
64   void dump(raw_ostream &OS, DIDumpOptions DumpOpts = {}) const;
65 
66   /// Return the address based on a given index.
67   Expected<uint64_t> getAddrEntry(uint32_t Index) const;
68 
69   /// Return the size of the table header including the length
70   /// but not including the addresses.
getHeaderSize()71   uint8_t getHeaderSize() const {
72     switch (Format) {
73     case dwarf::DwarfFormat::DWARF32:
74       return 8; // 4 + 2 + 1 + 1
75     case dwarf::DwarfFormat::DWARF64:
76       return 16; // 12 + 2 + 1 + 1
77     }
78     llvm_unreachable("Invalid DWARF format (expected DWARF32 or DWARF64)");
79   }
80 
81   /// Returns the length of this table, including the length field, or 0 if the
82   /// length has not been determined (e.g. because the table has not yet been
83   /// parsed, or there was a problem in parsing).
84   uint32_t getLength() const;
85 
86   /// Verify that the given length is valid for this table.
hasValidLength()87   bool hasValidLength() const { return getLength() != 0; }
88 
89   /// Invalidate Length field to stop further processing.
invalidateLength()90   void invalidateLength() { HeaderData.Length = 0; }
91 
92   /// Returns the length of the array of addresses.
93   uint32_t getDataSize() const;
94 };
95 
96 } // end namespace llvm
97 
98 #endif // LLVM_DEBUGINFO_DWARFDEBUGADDR_H
99