1283625Sdim//===-- DWARFDebugLoc.cpp -------------------------------------------------===//
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#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
11283625Sdim#include "llvm/Support/Compiler.h"
12283625Sdim#include "llvm/Support/Dwarf.h"
13283625Sdim#include "llvm/Support/Format.h"
14283625Sdim#include "llvm/Support/raw_ostream.h"
15283625Sdim
16283625Sdimusing namespace llvm;
17283625Sdim
18283625Sdimvoid DWARFDebugLoc::dump(raw_ostream &OS) const {
19283625Sdim  for (const LocationList &L : Locations) {
20283625Sdim    OS << format("0x%8.8x: ", L.Offset);
21283625Sdim    const unsigned Indent = 12;
22283625Sdim    for (const Entry &E : L.Entries) {
23283625Sdim      if (&E != L.Entries.begin())
24283625Sdim        OS.indent(Indent);
25283625Sdim      OS << "Beginning address offset: " << format("0x%016" PRIx64, E.Begin)
26283625Sdim         << '\n';
27283625Sdim      OS.indent(Indent) << "   Ending address offset: "
28283625Sdim                        << format("0x%016" PRIx64, E.End) << '\n';
29283625Sdim      OS.indent(Indent) << "    Location description: ";
30283625Sdim      for (unsigned char Loc : E.Loc) {
31283625Sdim        OS << format("%2.2x ", Loc);
32283625Sdim      }
33283625Sdim      OS << "\n\n";
34283625Sdim    }
35283625Sdim  }
36283625Sdim}
37283625Sdim
38283625Sdimvoid DWARFDebugLoc::parse(DataExtractor data, unsigned AddressSize) {
39283625Sdim  uint32_t Offset = 0;
40283625Sdim  while (data.isValidOffset(Offset+AddressSize-1)) {
41283625Sdim    Locations.resize(Locations.size() + 1);
42283625Sdim    LocationList &Loc = Locations.back();
43283625Sdim    Loc.Offset = Offset;
44283625Sdim    // 2.6.2 Location Lists
45283625Sdim    // A location list entry consists of:
46283625Sdim    while (true) {
47283625Sdim      Entry E;
48283625Sdim      RelocAddrMap::const_iterator AI = RelocMap.find(Offset);
49283625Sdim      // 1. A beginning address offset. ...
50283625Sdim      E.Begin = data.getUnsigned(&Offset, AddressSize);
51283625Sdim      if (AI != RelocMap.end())
52283625Sdim        E.Begin += AI->second.second;
53283625Sdim
54283625Sdim      AI = RelocMap.find(Offset);
55283625Sdim      // 2. An ending address offset. ...
56283625Sdim      E.End = data.getUnsigned(&Offset, AddressSize);
57283625Sdim      if (AI != RelocMap.end())
58283625Sdim        E.End += AI->second.second;
59283625Sdim
60283625Sdim      // The end of any given location list is marked by an end of list entry,
61283625Sdim      // which consists of a 0 for the beginning address offset and a 0 for the
62283625Sdim      // ending address offset.
63283625Sdim      if (E.Begin == 0 && E.End == 0)
64283625Sdim        break;
65283625Sdim
66283625Sdim      unsigned Bytes = data.getU16(&Offset);
67283625Sdim      // A single location description describing the location of the object...
68283625Sdim      StringRef str = data.getData().substr(Offset, Bytes);
69283625Sdim      Offset += Bytes;
70283625Sdim      E.Loc.append(str.begin(), str.end());
71283625Sdim      Loc.Entries.push_back(std::move(E));
72283625Sdim    }
73283625Sdim  }
74283625Sdim  if (data.isValidOffset(Offset))
75283625Sdim    llvm::errs() << "error: failed to consume entire .debug_loc section\n";
76283625Sdim}
77283625Sdim
78283625Sdimvoid DWARFDebugLocDWO::parse(DataExtractor data) {
79283625Sdim  uint32_t Offset = 0;
80283625Sdim  while (data.isValidOffset(Offset)) {
81283625Sdim    Locations.resize(Locations.size() + 1);
82283625Sdim    LocationList &Loc = Locations.back();
83283625Sdim    Loc.Offset = Offset;
84283625Sdim    dwarf::LocationListEntry Kind;
85283625Sdim    while ((Kind = static_cast<dwarf::LocationListEntry>(
86283625Sdim                data.getU8(&Offset))) != dwarf::DW_LLE_end_of_list_entry) {
87283625Sdim
88283625Sdim      if (Kind != dwarf::DW_LLE_start_length_entry) {
89283625Sdim        llvm::errs() << "error: dumping support for LLE of kind " << (int)Kind
90283625Sdim                     << " not implemented\n";
91283625Sdim        return;
92283625Sdim      }
93283625Sdim
94283625Sdim      Entry E;
95283625Sdim
96283625Sdim      E.Start = data.getULEB128(&Offset);
97283625Sdim      E.Length = data.getU32(&Offset);
98283625Sdim
99283625Sdim      unsigned Bytes = data.getU16(&Offset);
100283625Sdim      // A single location description describing the location of the object...
101283625Sdim      StringRef str = data.getData().substr(Offset, Bytes);
102283625Sdim      Offset += Bytes;
103283625Sdim      E.Loc.resize(str.size());
104283625Sdim      std::copy(str.begin(), str.end(), E.Loc.begin());
105283625Sdim
106283625Sdim      Loc.Entries.push_back(std::move(E));
107283625Sdim    }
108283625Sdim  }
109283625Sdim}
110283625Sdim
111283625Sdimvoid DWARFDebugLocDWO::dump(raw_ostream &OS) const {
112283625Sdim  for (const LocationList &L : Locations) {
113283625Sdim    OS << format("0x%8.8x: ", L.Offset);
114283625Sdim    const unsigned Indent = 12;
115283625Sdim    for (const Entry &E : L.Entries) {
116283625Sdim      if (&E != L.Entries.begin())
117283625Sdim        OS.indent(Indent);
118283625Sdim      OS << "Beginning address index: " << E.Start << '\n';
119283625Sdim      OS.indent(Indent) << "                 Length: " << E.Length << '\n';
120283625Sdim      OS.indent(Indent) << "   Location description: ";
121283625Sdim      for (unsigned char Loc : E.Loc)
122283625Sdim        OS << format("%2.2x ", Loc);
123283625Sdim      OS << "\n\n";
124283625Sdim    }
125283625Sdim  }
126283625Sdim}
127283625Sdim
128