Skip to content

Commit 8c6d48b

Browse files
committed
[llvm-readobj] Construct relocation-aware DWARFDataExtractor to decode .eh_frame addresses correctly
In an object file, a "PC Begin" field in a FDE is usually relocated by a PC-relative relocation. Use a relocation-aware DWARFDataExtractor overload (with DWARFContext and a reference to its internal .eh_frame representation) to decode addresses correctly. In an object file, most sections have addresses of zero. So the displayed addresses are almost always offsets relative to the start of the associated text section. DWARFContext::create handles .eh_frame and .rela.eh_frame by itself, so if there are more than one .eh_frame (technically possible, but almost always erronerous in practice), this will only handle the first one. Supporting multiple .eh_frame is beyond the scope of this patch. Reviewed By: grimar, jhenderson Differential Revision: https://reviews.llvm.org/D84106
1 parent 741e55a commit 8c6d48b

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

llvm/test/tools/llvm-readobj/ELF/AArch64/dwarf-cfi.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
# CHECK: Program:
1111
# CHECK-NEXT: DW_CFA_def_cfa: reg31 +0
1212

13-
## FIXME Use getEHFrameSection() so that the address is decoded correctly.
1413
# CHECK: [0x14] FDE length=16 cie=[0x0]
15-
# CHECK-NEXT: initial_location: 0x1c
16-
# CHECK-NEXT: address_range: 0x4 (end : 0x20)
14+
# CHECK-NEXT: initial_location: 0x0
15+
# CHECK-NEXT: address_range: 0x4 (end : 0x4)
1716

1817
# CHECK: Program:
1918
# CHECK-NEXT: DW_CFA_nop:

llvm/test/tools/llvm-readobj/ELF/ARM/dwarf-cfi.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
# CHECK: Program:
1111
# CHECK-NEXT: DW_CFA_def_cfa: reg13 +0
1212

13-
## FIXME Use getEHFrameSection() so that the address is decoded correctly.
1413
# CHECK: [0x14] FDE length=16 cie=[0x0]
15-
# CHECK-NEXT: initial_location: 0x1c
16-
# CHECK-NEXT: address_range: 0x4 (end : 0x20)
14+
# CHECK-NEXT: initial_location: 0x0
15+
# CHECK-NEXT: address_range: 0x4 (end : 0x4)
1716

1817
# CHECK: Program:
1918
# CHECK-NEXT: DW_CFA_nop:

llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm-readobj.h"
1414
#include "llvm/ADT/STLExtras.h"
1515
#include "llvm/BinaryFormat/Dwarf.h"
16+
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
1819
#include "llvm/Object/ELF.h"
@@ -185,7 +186,10 @@ void PrinterContext<ELFT>::printEHFrame(const Elf_Shdr *EHFrameShdr) const {
185186
if (!DataOrErr)
186187
reportError(DataOrErr.takeError(), ObjF->getFileName());
187188

188-
DWARFDataExtractor DE(*DataOrErr,
189+
// Construct DWARFDataExtractor to handle relocations ("PC Begin" fields).
190+
std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*ObjF, nullptr);
191+
DWARFDataExtractor DE(DICtx->getDWARFObj(),
192+
DICtx->getDWARFObj().getEHFrameSection(),
189193
ELFT::TargetEndianness == support::endianness::little,
190194
ELFT::Is64Bits ? 8 : 4);
191195
DWARFDebugFrame EHFrame(Triple::ArchType(ObjF->getArch()), /*IsEH=*/true,

0 commit comments

Comments
 (0)