Skip to content

Commit

Permalink
Change DW_LLE_baseaddr to DW_LLE_baseaddrx in .debug_loclist section
Browse files Browse the repository at this point in the history
With https://reviews.llvm.org/D154638, the ability to emit a .debug_addr
section has been added to dsymutil. With this, instead of emitting a
DW_LLE_baseaddr in the .debug_loclist section, a DW_LLE_baseaddrx can be
emitted instead, which will allow for more indirection.

Differetial Revision: https://reviews.llvm.org/D155724
  • Loading branch information
rastogishubham committed Jul 22, 2023
1 parent c5a45b2 commit ab7874e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 78 deletions.
60 changes: 29 additions & 31 deletions llvm/include/llvm/DWARFLinker/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ class AddressesMap {

using Offset2UnitMap = DenseMap<uint64_t, CompileUnit *>;

struct DebugAddrPool {
DenseMap<uint64_t, uint64_t> AddrIndexMap;
SmallVector<uint64_t> Addrs;

uint64_t getAddrIndex(uint64_t Addr) {
DenseMap<uint64_t, uint64_t>::iterator It = AddrIndexMap.find(Addr);
if (It == AddrIndexMap.end()) {
It = AddrIndexMap.insert(std::make_pair(Addr, Addrs.size())).first;
Addrs.push_back(Addr);
}
return It->second;
}

void clear() {
AddrIndexMap.clear();
Addrs.clear();
}
};

/// DwarfEmitter presents interface to generate all debug info tables.
class DwarfEmitter {
public:
Expand Down Expand Up @@ -137,7 +156,7 @@ class DwarfEmitter {
virtual void emitDwarfDebugLocListFragment(
const CompileUnit &Unit,
const DWARFLocationExpressionsVector &LinkedLocationExpression,
PatchLocation Patch) = 0;
PatchLocation Patch, DebugAddrPool &AddrPool) = 0;

/// Emit debug locations (.debug_loc, .debug_loclists) footer.
virtual void emitDwarfDebugLocListFooter(const CompileUnit &Unit,
Expand Down Expand Up @@ -636,27 +655,6 @@ class DWARFLinker {
DWARFFile &ObjFile;
OffsetsStringPool &DebugStrPool;
OffsetsStringPool &DebugLineStrPool;

struct DebugAddrPool {
DenseMap<uint64_t, uint64_t> AddrIndexMap;
SmallVector<uint64_t> Addrs;

uint64_t getAddrIndex(uint64_t Addr) {
DenseMap<uint64_t, uint64_t>::iterator It = AddrIndexMap.find(Addr);
if (It == AddrIndexMap.end()) {
It = AddrIndexMap.insert(std::make_pair(Addr, Addrs.size())).first;
Addrs.push_back(Addr);
}
return It->second;
}

void clear() {
AddrIndexMap.clear();
Addrs.clear();
}

};

DebugAddrPool AddrPool;

/// Allocator used for all the DIEValue objects.
Expand Down Expand Up @@ -705,6 +703,15 @@ class DWARFLinker {
void emitDebugAddrSection(CompileUnit &Unit,
const uint16_t DwarfVersion) const;

using ExpressionHandlerRef = function_ref<void(
SmallVectorImpl<uint8_t> &, SmallVectorImpl<uint8_t> &,
int64_t AddrRelocAdjustment)>;

/// Compute and emit debug locations (.debug_loc, .debug_loclists)
/// for \p Unit, patch the attributes referencing it.
void generateUnitLocations(CompileUnit &Unit, const DWARFFile &File,
ExpressionHandlerRef ExprHandler);

private:
using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;

Expand Down Expand Up @@ -820,15 +827,6 @@ class DWARFLinker {
/// .debug_rnglists) for \p Unit, patch the attributes referencing it.
void generateUnitRanges(CompileUnit &Unit, const DWARFFile &File) const;

using ExpressionHandlerRef =
function_ref<void(SmallVectorImpl<uint8_t> &, SmallVectorImpl<uint8_t> &,
int64_t AddrRelocAdjustment)>;

/// Compute and emit debug locations (.debug_loc, .debug_loclists)
/// for \p Unit, patch the attributes referencing it.
void generateUnitLocations(CompileUnit &Unit, const DWARFFile &File,
ExpressionHandlerRef ExprHandler) const;

/// Emit the accelerator entries for \p Unit.
void emitAcceleratorEntriesForUnit(CompileUnit &Unit);

Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/DWARFLinker/DWARFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class DwarfStreamer : public DwarfEmitter {
void emitDwarfDebugLocListFragment(
const CompileUnit &Unit,
const DWARFLocationExpressionsVector &LinkedLocationExpression,
PatchLocation Patch) override;
PatchLocation Patch, DebugAddrPool &AddrPool) override;

/// Emit debug ranges(.debug_loc, .debug_loclists) footer.
void emitDwarfDebugLocListFooter(const CompileUnit &Unit,
Expand Down Expand Up @@ -232,7 +232,7 @@ class DwarfStreamer : public DwarfEmitter {
void emitDwarfDebugLocListsTableFragment(
const CompileUnit &Unit,
const DWARFLocationExpressionsVector &LinkedLocationExpression,
PatchLocation Patch);
PatchLocation Patch, DebugAddrPool &AddrPool);

/// \defgroup Line table emission
/// @{
Expand Down
18 changes: 9 additions & 9 deletions llvm/lib/DWARFLinker/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1936,10 +1936,10 @@ void DWARFLinker::generateUnitRanges(CompileUnit &Unit,
}
}

void DWARFLinker::generateUnitLocations(
void DWARFLinker::DIECloner::generateUnitLocations(
CompileUnit &Unit, const DWARFFile &File,
ExpressionHandlerRef ExprHandler) const {
if (LLVM_UNLIKELY(Options.Update))
ExpressionHandlerRef ExprHandler) {
if (LLVM_UNLIKELY(Linker.Options.Update))
return;

const LocListAttributesTy &AllLocListAttributes =
Expand All @@ -1949,7 +1949,7 @@ void DWARFLinker::generateUnitLocations(
return;

// Emit locations list table header.
MCSymbol *EndLabel = TheDwarfEmitter->emitDwarfDebugLocListHeader(Unit);
MCSymbol *EndLabel = Emitter->emitDwarfDebugLocListHeader(Unit);

for (auto &CurLocAttr : AllLocListAttributes) {
// Get location expressions vector corresponding to the current attribute
Expand All @@ -1959,7 +1959,7 @@ void DWARFLinker::generateUnitLocations(

if (!OriginalLocations) {
llvm::consumeError(OriginalLocations.takeError());
reportWarning("Invalid location attribute ignored.", File);
Linker.reportWarning("Invalid location attribute ignored.", File);
continue;
}

Expand All @@ -1983,12 +1983,12 @@ void DWARFLinker::generateUnitLocations(
}

// Emit locations list table fragment corresponding to the CurLocAttr.
TheDwarfEmitter->emitDwarfDebugLocListFragment(
Unit, LinkedLocationExpressions, CurLocAttr);
Emitter->emitDwarfDebugLocListFragment(Unit, LinkedLocationExpressions,
CurLocAttr, AddrPool);
}

// Emit locations list table footer.
TheDwarfEmitter->emitDwarfDebugLocListFooter(Unit, EndLabel);
Emitter->emitDwarfDebugLocListFooter(Unit, EndLabel);
}

static void patchAddrBase(DIE &Die, DIEInteger Offset) {
Expand Down Expand Up @@ -2585,7 +2585,7 @@ uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(
File, *CurrentUnit, OutBytes, RelocAdjustment,
IsLittleEndian);
};
Linker.generateUnitLocations(*CurrentUnit, File, ProcessExpr);
generateUnitLocations(*CurrentUnit, File, ProcessExpr);
emitDebugAddrSection(*CurrentUnit, DwarfVersion);
}
AddrPool.clear();
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/DWARFLinker/DWARFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,14 @@ MCSymbol *DwarfStreamer::emitDwarfDebugLocListHeader(const CompileUnit &Unit) {
void DwarfStreamer::emitDwarfDebugLocListFragment(
const CompileUnit &Unit,
const DWARFLocationExpressionsVector &LinkedLocationExpression,
PatchLocation Patch) {
PatchLocation Patch, DebugAddrPool &AddrPool) {
if (Unit.getOrigUnit().getVersion() < 5) {
emitDwarfDebugLocTableFragment(Unit, LinkedLocationExpression, Patch);
return;
}

emitDwarfDebugLocListsTableFragment(Unit, LinkedLocationExpression, Patch);
emitDwarfDebugLocListsTableFragment(Unit, LinkedLocationExpression, Patch,
AddrPool);
}

/// Emit debug locations(.debug_loc, .debug_loclists) footer.
Expand Down Expand Up @@ -661,7 +662,7 @@ void DwarfStreamer::emitDwarfDebugAddrsFooter(const CompileUnit &Unit,
void DwarfStreamer::emitDwarfDebugLocListsTableFragment(
const CompileUnit &Unit,
const DWARFLocationExpressionsVector &LinkedLocationExpression,
PatchLocation Patch) {
PatchLocation Patch, DebugAddrPool &AddrPool) {
Patch.set(LocListsSectionSize);

// Make .debug_loclists the current section.
Expand All @@ -677,11 +678,10 @@ void DwarfStreamer::emitDwarfDebugLocListsTableFragment(
BaseAddress = LocExpression.Range->LowPC;

// Emit base address.
MS->emitInt8(dwarf::DW_LLE_base_address);
MS->emitInt8(dwarf::DW_LLE_base_addressx);
LocListsSectionSize += 1;
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
MS->emitIntValue(*BaseAddress, AddressSize);
LocListsSectionSize += AddressSize;
LocListsSectionSize +=
MS->emitULEB128IntValue(AddrPool.getAddrIndex(*BaseAddress));
}

// Emit type of entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ CHECK-NEXT: (0x[[#sub(LOC_PAIR_START,LOC_LOWPC)]], 0x[[#sub(LOC_PAIR_
CHECK-NEXT: (0x[[#sub(LOC_PAIR_START2,LOC_LOWPC)]], 0x[[#sub(LOC_PAIR_END2,LOC_LOWPC)]]): [[LOC_EXPR2:.*]]

CHECK: .debug_loclists contents:
CHECK-NEXT: 0x00000000: locations list header: length = 0x0000001f, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
CHECK-NEXT: 0x00000000: locations list header: length = 0x00000018, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
CHECK-NEXT: 0x[[LOCLIST_OFFSET]]:
CHECK-NEXT: DW_LLE_base_address (0x[[#LOCLIST_LOWPC]])
CHECK-NEXT: DW_LLE_base_addressx (0x0000000000000000)
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END,LOCLIST_LOWPC)]])
CHECK-NEXT: => [0x[[#LOCLIST_PAIR_START]], 0x[[#LOCLIST_PAIR_END]]): [[LOCLIST_EXPR]]
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START2,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END2,LOCLIST_LOWPC)]])
CHECK-NEXT: => [0x[[#LOCLIST_PAIR_START2]], 0x[[#LOCLIST_PAIR_END2]]): [[LOCLIST_EXPR2]]
CHECK-NEXT: DW_LLE_end_of_list ()

CHECK: .debug_line contents:
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/tools/dsymutil/ARM/dwarf5-macho.test
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START:]], 0x[[#%.16x,L
CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START2:]], 0x[[#%.16x,LOCLIST_PAIR_END2:]]): [[LOCLIST_EXPR2:.*]])

CHECK: .debug_loclists contents:
CHECK-NEXT: locations list header: length = 0x0000001f, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
CHECK-NEXT: 0x00000000: locations list header: length = 0x00000018, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
CHECK-NEXT: 0x[[LOC_OFFSET]]:
CHECK-NEXT: DW_LLE_base_address (0x[[#LOCLIST_LOWPC]])
CHECK-NEXT: DW_LLE_base_addressx (0x0000000000000000)
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END,LOCLIST_LOWPC)]])
CHECK-NEXT: => [0x[[#LOCLIST_PAIR_START]], 0x[[#LOCLIST_PAIR_END]]): [[LOCLIST_EXPR]]
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START2,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END2,LOCLIST_LOWPC)]])
CHECK-NEXT: => [0x[[#LOCLIST_PAIR_START2]], 0x[[#LOCLIST_PAIR_END2]]): [[LOCLIST_EXPR2]]
CHECK-NEXT: DW_LLE_end_of_list ()

CHECK: .debug_line contents:
Expand Down
31 changes: 12 additions & 19 deletions llvm/test/tools/dsymutil/X86/dwarf5-loclists.test
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,24 @@
#DWARF-CHECK: [0x0000000100000fa3, 0x0000000100000fbc): DW_OP_breg6 RBP-20)
#DWARF-CHECK: DW_AT_name {{.*}} "argv"
#DWARF-CHECK: DW_TAG_formal_parameter
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x00000030:
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x00000029:
#DWARF-CHECK: [0x0000000100000f70, 0x0000000100000f89): DW_OP_reg4 RSI
#DWARF-CHECK: [0x0000000100000f89, 0x0000000100000fbc): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value)
#DWARF-CHECK: DW_AT_name {{.*}} "argc"
#DWARF-CHECK: .debug_loclists contents:
#DWARF-CHECK: 0x00000000: locations list header: length = 0x00000043, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
#DWARF-CHECK: 0x00000000: locations list header: length = 0x00000035, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
#DWARF-CHECK: 0x0000000c:
#DWARF-CHECK: DW_LLE_base_address (0x0000000100000f70)
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000000, 0x0000000000000017)
#DWARF-CHECK: => [0x0000000100000f70, 0x0000000100000f87): DW_OP_reg5 RDI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000017, 0x0000000000000023)
#DWARF-CHECK: => [0x0000000100000f87, 0x0000000100000f93): DW_OP_reg3 RBX
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000023, 0x000000000000002d)
#DWARF-CHECK: => [0x0000000100000f93, 0x0000000100000f9d): DW_OP_reg4 RSI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000030, 0x0000000000000033)
#DWARF-CHECK: => [0x0000000100000fa0, 0x0000000100000fa3): DW_OP_reg3 RBX
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000033, 0x000000000000004c)
#DWARF-CHECK: => [0x0000000100000fa3, 0x0000000100000fbc): DW_OP_breg6 RBP-20
#DWARF-CHECK: DW_LLE_base_addressx (0x0000000000000000)
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000000, 0x0000000000000017): DW_OP_reg5 RDI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000017, 0x0000000000000023): DW_OP_reg3 RBX
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000023, 0x000000000000002d): DW_OP_reg4 RSI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000030, 0x0000000000000033): DW_OP_reg3 RBX
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000033, 0x000000000000004c): DW_OP_breg6 RBP-20
#DWARF-CHECK: DW_LLE_end_of_list ()
#DWARF-CHECK: 0x00000030:
#DWARF-CHECK: DW_LLE_base_address (0x0000000100000f70)
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000000, 0x0000000000000019)
#DWARF-CHECK: => [0x0000000100000f70, 0x0000000100000f89): DW_OP_reg4 RSI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000019, 0x000000000000004c)
#DWARF-CHECK: => [0x0000000100000f89, 0x0000000100000fbc): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value
#DWARF-CHECK: 0x00000029:
#DWARF-CHECK: DW_LLE_base_addressx (0x0000000000000000)
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000000, 0x0000000000000019): DW_OP_reg4 RSI
#DWARF-CHECK: DW_LLE_offset_pair (0x0000000000000019, 0x000000000000004c): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value
#DWARF-CHECK: DW_LLE_end_of_list ()


Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-loclists.test
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x0000000c:
#DWARF-CHECK: [0x0000000000001130, 0x0000000000001140): DW_OP_reg5 RDI)
#DWARF-CHECK: DW_AT_name {{.*}}"var2"
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x0000001b:
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x00000014:
#DWARF-CHECK: <default>: DW_OP_reg5 RDI)
#DWARF-CHECK: DW_TAG_variable
#DWARF-CHECK: DW_AT_name {{.*}}"var3"
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x0000001f:
#DWARF-CHECK: DW_AT_location [DW_FORM_sec_offset] (0x00000018:
#DWARF-CHECK: [0x0000000000001140, 0x0000000000001150): DW_OP_reg5 RDI
#DWARF-CHECK: [0x0000000000001160, 0x0000000000001170): DW_OP_reg6 RBP)

Expand Down

0 comments on commit ab7874e

Please sign in to comment.