diff options
author | Aiden Grossman <agrossman154@yahoo.com> | 2023-02-25 10:18:57 +0000 |
---|---|---|
committer | Aiden Grossman <agrossman154@yahoo.com> | 2023-03-13 21:29:48 +0000 |
commit | 175aa049c7c1a319289a27aca538e9ba59111383 (patch) | |
tree | e6eed1c6883f1db29d3177e31ed6c21b1489e4b9 /llvm/lib/Object/ELFObjectFile.cpp | |
parent | 60d2dbf522c0a4d7dc85e6146a830dfbb40bbc96 (diff) | |
download | llvm-175aa049c7c1a319289a27aca538e9ba59111383.zip llvm-175aa049c7c1a319289a27aca538e9ba59111383.tar.gz llvm-175aa049c7c1a319289a27aca538e9ba59111383.tar.bz2 |
[Propeller] Make decoding BBAddrMaps trace through relocations
Currently when using the LLVM tools (eg llvm-readobj, llvm-objdump) to
find information about basic block locations using the propeller tooling
in relocatable object files function addresses are not mapped properly
which causes problems. In llvm-readobj this means that incorrect
function names will be pulled. In llvm-objdum this means that most BBs
won't show up in the output if --symbolize-operands is used. This patch
changes the behavior of decodeBBAddrMap to trace through relocations
to get correct function addresses if it is going through a relocatable
object file. This fixes the behavior in both tools and also other
consumers of decodeBBAddrMap. Some helper functions have been added
in/refactoring done to aid in grabbing BB address map sections now that
in some cases both relocation and BB address map sections need to be
obtained at the same time.
Regression tests moved around/added.
Differential Revision: https://reviews.llvm.org/D143841
Diffstat (limited to 'llvm/lib/Object/ELFObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/ELFObjectFile.cpp | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp index 2cbfd8a..b9343d6 100644 --- a/llvm/lib/Object/ELFObjectFile.cpp +++ b/llvm/lib/Object/ELFObjectFile.cpp @@ -681,24 +681,39 @@ template <class ELFT> Expected<std::vector<BBAddrMap>> static readBBAddrMapImpl( const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex) { using Elf_Shdr = typename ELFT::Shdr; + bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL; std::vector<BBAddrMap> BBAddrMaps; + const auto &Sections = cantFail(EF.sections()); - for (const Elf_Shdr &Sec : Sections) { + auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> { if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP && Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0) - continue; - if (TextSectionIndex) { - Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link); - if (!TextSecOrErr) - return createError("unable to get the linked-to section for " + - describe(EF, Sec) + ": " + - toString(TextSecOrErr.takeError())); - if (*TextSectionIndex != std::distance(Sections.begin(), *TextSecOrErr)) - continue; - } - Expected<std::vector<BBAddrMap>> BBAddrMapOrErr = EF.decodeBBAddrMap(Sec); + return false; + if (!TextSectionIndex) + return true; + Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link); + if (!TextSecOrErr) + return createError("unable to get the linked-to section for " + + describe(EF, Sec) + ": " + + toString(TextSecOrErr.takeError())); + if (*TextSectionIndex != std::distance(Sections.begin(), *TextSecOrErr)) + return false; + return true; + }; + + Expected<MapVector<const Elf_Shdr *, const Elf_Shdr *>> SectionRelocMapOrErr = + EF.getSectionAndRelocations(IsMatch); + if (!SectionRelocMapOrErr) + return SectionRelocMapOrErr.takeError(); + + for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) { + if (IsRelocatable && !RelocSec) + return createError("unable to get relocation section for " + + describe(EF, *Sec)); + Expected<std::vector<BBAddrMap>> BBAddrMapOrErr = + EF.decodeBBAddrMap(*Sec, RelocSec); if (!BBAddrMapOrErr) - return createError("unable to read " + describe(EF, Sec) + ": " + + return createError("unable to read " + describe(EF, *Sec) + ": " + toString(BBAddrMapOrErr.takeError())); std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(), std::back_inserter(BBAddrMaps)); |