Files
llvm/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugAranges.cpp

152 lines
4.9 KiB
C++
Raw Normal View History

//===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "DWARFDebugAranges.h"
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/Timer.h"
#include "DWARFCompileUnit.h"
#include "DWARFDebugInfo.h"
#include "LogChannelDWARF.h"
#include "SymbolFileDWARF.h"
using namespace lldb;
using namespace lldb_private;
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {}
//----------------------------------------------------------------------
// CountArangeDescriptors
//----------------------------------------------------------------------
class CountArangeDescriptors {
public:
CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) {
// printf("constructor CountArangeDescriptors()\n");
}
void operator()(const DWARFDebugArangeSet &set) {
count += set.NumDescriptors();
}
uint32_t &count;
};
//----------------------------------------------------------------------
// Extract
//----------------------------------------------------------------------
bool DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data) {
if (debug_aranges_data.ValidOffset(0)) {
lldb::offset_t offset = 0;
DWARFDebugArangeSet set;
Range range;
while (set.Extract(debug_aranges_data, &offset)) {
const uint32_t num_descriptors = set.NumDescriptors();
if (num_descriptors > 0) {
const dw_offset_t cu_offset = set.GetCompileUnitDIEOffset();
for (uint32_t i = 0; i < num_descriptors; ++i) {
const DWARFDebugArangeSet::Descriptor &descriptor =
set.GetDescriptorRef(i);
m_aranges.Append(RangeToDIE::Entry(descriptor.address,
descriptor.length, cu_offset));
}
}
set.Clear();
}
}
return false;
}
//----------------------------------------------------------------------
// Generate
//----------------------------------------------------------------------
bool DWARFDebugAranges::Generate(SymbolFileDWARF *dwarf2Data) {
Clear();
DWARFDebugInfo *debug_info = dwarf2Data->DebugInfo();
if (debug_info) {
uint32_t cu_idx = 0;
const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
DWARFCompileUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx);
if (cu)
cu->BuildAddressRangeTable(dwarf2Data, this);
}
}
return !IsEmpty();
}
void DWARFDebugAranges::Dump(Log *log) const {
if (log == NULL)
return;
const size_t num_entries = m_aranges.GetSize();
for (size_t i = 0; i < num_entries; ++i) {
const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
if (entry)
log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
entry->GetRangeBase(), entry->GetRangeEnd());
}
}
void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
dw_addr_t high_pc) {
if (high_pc > low_pc)
m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
Looking at some of the test suite failures in DWARF in .o files with the debug map showed that the location lists in the .o files needed some refactoring in order to work. The case that was failing was where a function that was in the "__TEXT.__textcoal_nt" in the .o file, and in the "__TEXT.__text" section in the main executable. This made symbol lookup fail due to the way we were finding a real address in the debug map which was by finding the section that the function was in in the .o file and trying to find this in the main executable. Now the section list supports finding a linked address in a section or any child sections. After fixing this, we ran into issue that were due to DWARF and how it represents locations lists. DWARF makes a list of address ranges and expressions that go along with those address ranges. The location addresses are expressed in terms of a compile unit address + offset. This works fine as long as nothing moves around. When stuff moves around and offsets change between the remapped compile unit base address and the new function address, then we can run into trouble. To deal with this, we now store supply a location list slide amount to any location list expressions that will allow us to make the location list addresses into zero based offsets from the object that owns the location list (always a function in our case). With these fixes we can now re-link random address ranges inside the debugger for use with our DWARF + debug map, incremental linking, and more. Another issue that arose when doing the DWARF in the .o files was that GCC 4.2 emits a ".debug_aranges" that only mentions functions that are externally visible. This makes .debug_aranges useless to us and we now generate a real address range lookup table in the DWARF parser at the same time as we index the name tables (that are needed because .debug_pubnames is just as useless). llvm-gcc doesn't generate a .debug_aranges section, though this could be fixed, we aren't going to rely upon it. Renamed a bunch of "UINT_MAX" to "UINT32_MAX". llvm-svn: 113829
2010-09-14 02:20:48 +00:00
}
void DWARFDebugAranges::Sort(bool minimize) {
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
static_cast<void *>(this));
Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
size_t orig_arange_size = 0;
if (log) {
orig_arange_size = m_aranges.GetSize();
log->Printf("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64
" entries",
minimize, (uint64_t)orig_arange_size);
}
m_aranges.Sort();
m_aranges.CombineConsecutiveEntriesWithEqualData();
if (log) {
if (minimize) {
const size_t new_arange_size = m_aranges.GetSize();
const size_t delta = orig_arange_size - new_arange_size;
log->Printf("DWARFDebugAranges::Sort() %" PRIu64
" entries after minimizing (%" PRIu64
" entries combined for %" PRIu64 " bytes saved)",
(uint64_t)new_arange_size, (uint64_t)delta,
(uint64_t)delta * sizeof(Range));
}
Dump(log);
}
Looking at some of the test suite failures in DWARF in .o files with the debug map showed that the location lists in the .o files needed some refactoring in order to work. The case that was failing was where a function that was in the "__TEXT.__textcoal_nt" in the .o file, and in the "__TEXT.__text" section in the main executable. This made symbol lookup fail due to the way we were finding a real address in the debug map which was by finding the section that the function was in in the .o file and trying to find this in the main executable. Now the section list supports finding a linked address in a section or any child sections. After fixing this, we ran into issue that were due to DWARF and how it represents locations lists. DWARF makes a list of address ranges and expressions that go along with those address ranges. The location addresses are expressed in terms of a compile unit address + offset. This works fine as long as nothing moves around. When stuff moves around and offsets change between the remapped compile unit base address and the new function address, then we can run into trouble. To deal with this, we now store supply a location list slide amount to any location list expressions that will allow us to make the location list addresses into zero based offsets from the object that owns the location list (always a function in our case). With these fixes we can now re-link random address ranges inside the debugger for use with our DWARF + debug map, incremental linking, and more. Another issue that arose when doing the DWARF in the .o files was that GCC 4.2 emits a ".debug_aranges" that only mentions functions that are externally visible. This makes .debug_aranges useless to us and we now generate a real address range lookup table in the DWARF parser at the same time as we index the name tables (that are needed because .debug_pubnames is just as useless). llvm-gcc doesn't generate a .debug_aranges section, though this could be fixed, we aren't going to rely upon it. Renamed a bunch of "UINT_MAX" to "UINT32_MAX". llvm-svn: 113829
2010-09-14 02:20:48 +00:00
}
//----------------------------------------------------------------------
// FindAddress
//----------------------------------------------------------------------
dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
if (entry)
return entry->data;
return DW_INVALID_OFFSET;
}