From 496c6b022ffefc961238a423c0ccbbdadb84a82d Mon Sep 17 00:00:00 2001 From: "Arina.Naumova" Date: Thu, 26 Dec 2024 06:47:38 -0500 Subject: [PATCH] Fixed segfault when printing .eh_frame using llvm-readelf unwind Change-Id: Icf803c5c912ebca28694769f7355a1a099f56fa1 --- .../llvm/DebugInfo/DWARF/DWARFSection.h | 12 +++++++++- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 11 ++++++---- llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h | 22 +++++++++++++------ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h index e03dc21fb6f0..36e43b97af1d 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h @@ -10,7 +10,9 @@ #define LLVM_DEBUGINFO_DWARF_DWARFSECTION_H #include "llvm/ADT/StringRef.h" - +// OHOS_LOCAL begin +#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" +// OHOS_LOCAL end namespace llvm { struct DWARFSection { @@ -18,6 +20,14 @@ struct DWARFSection { uint64_t Address = 0; }; +// OHOS_LOCAL begin +// Copied from: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +// to allow DWARFSection to be modified without losing the Relocs binding. +struct DWARFSectionMap final : public DWARFSection { + RelocAddrMap Relocs; +}; +// OHOS_LOCAL end + struct SectionName { StringRef Name; bool IsNameUnique; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 19d7d659a86a..30e5ef4ae2e0 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1529,10 +1529,13 @@ static bool isRelocScattered(const object::ObjectFile &Obj, } namespace { -struct DWARFSectionMap final : public DWARFSection { - RelocAddrMap Relocs; -}; - +// OHOS_LOCAL begin +// Moved to: llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h +// to allow DWARFSection to be modified without losing the Relocs binding. +// struct DWARFSectionMap final : public DWARFSection { +// RelocAddrMap Relocs; +// }; +// OHOS_LOCAL end class DWARFObjInMemory final : public DWARFObject { bool IsLittleEndian; uint8_t AddressSize; diff --git a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h index c1c39840fec3..077dfe17423b 100644 --- a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h +++ b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h @@ -24,6 +24,9 @@ #include "llvm/Support/Format.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/type_traits.h" +// OHOS_LOCAL begin +#include "llvm/DebugInfo/DWARF/DWARFSection.h" +// OHOS_LOCAL end namespace llvm { namespace DwarfCFIEH { @@ -412,17 +415,22 @@ void PrinterContext::printEHFrame(const Elf_Shdr *EHFrameShdr) const { ObjF, DWARFContext::ProcessDebugRelocations::Process, nullptr); // OHOS_LOCAL begin - // TODO: DWARFSectionMap with relocs information must be used. - // Construct DWARFSection - DWARFSection DSection; - DSection.Address = Address; - DSection.Data = StringRef(reinterpret_cast(DataOrErr->data()), - DataOrErr->size()); + // Get original EHFrame section to bind the new section to Relocs. + const DWARFSection &EHFDSection = DICtx->getDWARFObj().getEHFrameSection(); + const DWARFSectionMap &EHFDSectionMap = + static_cast(EHFDSection); + + // Construct DWARFSectionMap + DWARFSectionMap DSectionMap; + DSectionMap.Address = Address; + DSectionMap.Data = StringRef( + reinterpret_cast(DataOrErr->data()), DataOrErr->size()); + DSectionMap.Relocs = EHFDSectionMap.Relocs; // OHOS_LOCAL end DWARFDataExtractor DE(DICtx->getDWARFObj(), // OHOS_LOCAL begin - IsAdlt ? DSection + IsAdlt ? DSectionMap : DICtx->getDWARFObj().getEHFrameSection(), // OHOS_LOCAL end ELFT::TargetEndianness == support::endianness::little, -- Gitee