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