[PATCH] D84106: [llvm-readobj] Construct relocation-aware DWARFDataExtractor to decode .eh_frame addresses correctly

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 18 10:05:15 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, jhenderson.
Herald added subscribers: llvm-commits, rupprecht, aprantl, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

In an object file, a "PC Begin" field in a FDE is usually relocated by a
PC-relative relocation. Use a relocation-aware DWARFDataExtractor instead of
plain DataExtractor to decode addresses correctly. In an object file, most
sections have addresses of zero. So the displayed addresses are always 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84106

Files:
  llvm/test/tools/llvm-readobj/ELF/AArch64/dwarf-cfi.s
  llvm/test/tools/llvm-readobj/ELF/ARM/dwarf-cfi.s
  llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h


Index: llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h
===================================================================
--- llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h
+++ llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h
@@ -13,16 +13,17 @@
 #include "llvm-readobj.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
+#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
 #include "llvm/Object/ELF.h"
-#include "llvm/Object/ELFTypes.h"
 #include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ELFTypes.h"
 #include "llvm/Support/Casting.h"
-#include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
-#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/type_traits.h"
 
 namespace llvm {
@@ -186,9 +187,12 @@
   if (!DataOrErr)
     reportError(DataOrErr.takeError(), ObjF->getFileName());
 
-  DWARFDataExtractor DE(*DataOrErr,
+  std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*ObjF, nullptr);
+  DWARFDataExtractor DE(DICtx->getDWARFObj(),
+                        DICtx->getDWARFObj().getEHFrameSection(),
                         ELFT::TargetEndianness == support::endianness::little,
                         ELFT::Is64Bits ? 8 : 4);
+
   DWARFDebugFrame EHFrame(Triple::ArchType(ObjF->getArch()), /*IsEH=*/true,
                           /*EHFrameAddress=*/Address);
   if (Error E = EHFrame.parse(DE))
Index: llvm/test/tools/llvm-readobj/ELF/ARM/dwarf-cfi.s
===================================================================
--- llvm/test/tools/llvm-readobj/ELF/ARM/dwarf-cfi.s
+++ llvm/test/tools/llvm-readobj/ELF/ARM/dwarf-cfi.s
@@ -10,10 +10,9 @@
 # CHECK:        Program:
 # CHECK-NEXT: DW_CFA_def_cfa: reg13 +0
 
-## FIXME Use getEHFrameSection() so that the address is decoded correctly.
 # CHECK:      [0x14] FDE length=16 cie=[0x0]
-# CHECK-NEXT:   initial_location: 0x1c
-# CHECK-NEXT:   address_range: 0x4 (end : 0x20)
+# CHECK-NEXT:   initial_location: 0x0
+# CHECK-NEXT:   address_range: 0x4 (end : 0x4)
 
 # CHECK:        Program:
 # CHECK-NEXT: DW_CFA_nop:
Index: llvm/test/tools/llvm-readobj/ELF/AArch64/dwarf-cfi.s
===================================================================
--- llvm/test/tools/llvm-readobj/ELF/AArch64/dwarf-cfi.s
+++ llvm/test/tools/llvm-readobj/ELF/AArch64/dwarf-cfi.s
@@ -10,10 +10,9 @@
 # CHECK:        Program:
 # CHECK-NEXT: DW_CFA_def_cfa: reg31 +0
 
-## FIXME Use getEHFrameSection() so that the address is decoded correctly.
 # CHECK:      [0x14] FDE length=16 cie=[0x0]
-# CHECK-NEXT:   initial_location: 0x1c
-# CHECK-NEXT:   address_range: 0x4 (end : 0x20)
+# CHECK-NEXT:   initial_location: 0x0
+# CHECK-NEXT:   address_range: 0x4 (end : 0x4)
 
 # CHECK:        Program:
 # CHECK-NEXT: DW_CFA_nop:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84106.279012.patch
Type: text/x-patch
Size: 2995 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200718/67be1c95/attachment.bin>


More information about the llvm-commits mailing list