1 //===- DWARFDebugLoc.cpp --------------------------------------------------===//
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 #include "llvm/ADT/StringRef.h"
11 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
12 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <algorithm>
17 #include <cinttypes>
18 #include <cstdint>
19 
20 using namespace llvm;
21 
22 void DWARFDebugLoc::dump(raw_ostream &OS) const {
23   for (const LocationList &L : Locations) {
24     OS << format("0x%8.8x: ", L.Offset);
25     const unsigned Indent = 12;
26     for (const Entry &E : L.Entries) {
27       if (&E != L.Entries.begin())
28         OS.indent(Indent);
29       OS << "Beginning address offset: " << format("0x%016" PRIx64, E.Begin)
30          << '\n';
31       OS.indent(Indent) << "   Ending address offset: "
32                         << format("0x%016" PRIx64, E.End) << '\n';
33       OS.indent(Indent) << "    Location description: ";
34       for (unsigned char Loc : E.Loc) {
35         OS << format("%2.2x ", Loc);
36       }
37       OS << "\n\n";
38     }
39   }
40 }
41 
42 void DWARFDebugLoc::parse(DataExtractor data, unsigned AddressSize) {
43   uint32_t Offset = 0;
44   while (data.isValidOffset(Offset+AddressSize-1)) {
45     Locations.resize(Locations.size() + 1);
46     LocationList &Loc = Locations.back();
47     Loc.Offset = Offset;
48     // 2.6.2 Location Lists
49     // A location list entry consists of:
50     while (true) {
51       Entry E;
52       RelocAddrMap::const_iterator AI = RelocMap.find(Offset);
53       // 1. A beginning address offset. ...
54       E.Begin = data.getUnsigned(&Offset, AddressSize);
55       if (AI != RelocMap.end())
56         E.Begin += AI->second.second;
57 
58       AI = RelocMap.find(Offset);
59       // 2. An ending address offset. ...
60       E.End = data.getUnsigned(&Offset, AddressSize);
61       if (AI != RelocMap.end())
62         E.End += AI->second.second;
63 
64       // The end of any given location list is marked by an end of list entry,
65       // which consists of a 0 for the beginning address offset and a 0 for the
66       // ending address offset.
67       if (E.Begin == 0 && E.End == 0)
68         break;
69 
70       unsigned Bytes = data.getU16(&Offset);
71       // A single location description describing the location of the object...
72       StringRef str = data.getData().substr(Offset, Bytes);
73       Offset += Bytes;
74       E.Loc.append(str.begin(), str.end());
75       Loc.Entries.push_back(std::move(E));
76     }
77   }
78   if (data.isValidOffset(Offset))
79     errs() << "error: failed to consume entire .debug_loc section\n";
80 }
81 
82 void DWARFDebugLocDWO::parse(DataExtractor data) {
83   uint32_t Offset = 0;
84   while (data.isValidOffset(Offset)) {
85     Locations.resize(Locations.size() + 1);
86     LocationList &Loc = Locations.back();
87     Loc.Offset = Offset;
88     dwarf::LocationListEntry Kind;
89     while ((Kind = static_cast<dwarf::LocationListEntry>(
90                 data.getU8(&Offset))) != dwarf::DW_LLE_end_of_list) {
91 
92       if (Kind != dwarf::DW_LLE_startx_length) {
93         errs() << "error: dumping support for LLE of kind " << (int)Kind
94                << " not implemented\n";
95         return;
96       }
97 
98       Entry E;
99 
100       E.Start = data.getULEB128(&Offset);
101       E.Length = data.getU32(&Offset);
102 
103       unsigned Bytes = data.getU16(&Offset);
104       // A single location description describing the location of the object...
105       StringRef str = data.getData().substr(Offset, Bytes);
106       Offset += Bytes;
107       E.Loc.resize(str.size());
108       std::copy(str.begin(), str.end(), E.Loc.begin());
109 
110       Loc.Entries.push_back(std::move(E));
111     }
112   }
113 }
114 
115 void DWARFDebugLocDWO::dump(raw_ostream &OS) const {
116   for (const LocationList &L : Locations) {
117     OS << format("0x%8.8x: ", L.Offset);
118     const unsigned Indent = 12;
119     for (const Entry &E : L.Entries) {
120       if (&E != L.Entries.begin())
121         OS.indent(Indent);
122       OS << "Beginning address index: " << E.Start << '\n';
123       OS.indent(Indent) << "                 Length: " << E.Length << '\n';
124       OS.indent(Indent) << "   Location description: ";
125       for (unsigned char Loc : E.Loc)
126         OS << format("%2.2x ", Loc);
127       OS << "\n\n";
128     }
129   }
130 }
131