diff options
author | Eduard Zingerman <eddyz87@gmail.com> | 2023-05-07 23:59:24 +0300 |
---|---|---|
committer | Eduard Zingerman <eddyz87@gmail.com> | 2023-09-21 21:59:10 +0300 |
commit | d15f96fe4b64e24d262fcc727dc0eede327804ba (patch) | |
tree | 8d45e939a6e69d27de9e7ccfd1fa94cea8af4655 /llvm/tools/llvm-objdump/llvm-objdump.cpp | |
parent | 4dec62f4d4a0a496a8067e283bf66897fbf6e598 (diff) | |
download | llvm-d15f96fe4b64e24d262fcc727dc0eede327804ba.zip llvm-d15f96fe4b64e24d262fcc727dc0eede327804ba.tar.gz llvm-d15f96fe4b64e24d262fcc727dc0eede327804ba.tar.bz2 |
[BPF][DebugInfo] Show CO-RE relocations in llvm-objdump
Extend llvm-objdump to show CO-RE relocations when `-r` option is
passed and object file has .BTF and .BTF.ext sections.
For example, the following C program:
#define __pai __attribute__((preserve_access_index))
struct foo { int i; int j;} __pai;
struct bar { struct foo f[7]; } __pai;
extern void sink(void *);
void root(struct bar *bar) {
sink(&bar[2].f[3].j);
}
Should lead to the following objdump output:
$ clang --target=bpf -O2 -g t.c -c -o - | \
llvm-objdump --no-addresses --no-show-raw-insn -dr -
...
r2 = 0x94
CO-RE <byte_off> [2] struct bar::[2].f[3].j (2:0:3:1)
r1 += r2
call -0x1
R_BPF_64_32 sink
exit
...
More examples could be found in unit tests, see BTFParserTest.cpp.
To achieve this:
- Move CO-RE relocation kinds definitions from BPFCORE.h to BTF.h.
- Extend BTF.h with types derived from BTF::CommonType, e.g.
BTF::IntType and BTF::StrutType, to allow dyn_cast() and access to
type additional data.
- Extend BTFParser to load BTF type and relocation data.
- Modify llvm-objdump.cpp to create instance of BTFParser when
disassembly of object file with BTF sections is processed and `-r`
flag is supplied.
Additional information about CO-RE is available at [1].
[1] https://docs.kernel.org/bpf/llvm_reloc.html
Depends on D149058
Differential Revision: https://reviews.llvm.org/D150079
Diffstat (limited to 'llvm/tools/llvm-objdump/llvm-objdump.cpp')
-rw-r--r-- | llvm/tools/llvm-objdump/llvm-objdump.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index e4e1ceb..fcfce77 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -31,6 +31,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Twine.h" +#include "llvm/DebugInfo/BTF/BTFParser.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" @@ -535,6 +536,22 @@ static void printRelocation(formatted_raw_ostream &OS, StringRef FileName, OS << Name << "\t" << Val; } +static void printBTFRelocation(formatted_raw_ostream &FOS, llvm::BTFParser &BTF, + object::SectionedAddress Address, + LiveVariablePrinter &LVP) { + const llvm::BTF::BPFFieldReloc *Reloc = BTF.findFieldReloc(Address); + if (!Reloc) + return; + + SmallString<64> Val; + BTF.symbolize(Reloc, Val); + FOS << "\t\t"; + if (LeadingAddr) + FOS << format("%016" PRIx64 ": ", Address.Address + AdjustVMA); + FOS << "CO-RE " << Val; + LVP.printAfterOtherLine(FOS, true); +} + class PrettyPrinter { public: virtual ~PrettyPrinter() = default; @@ -1626,6 +1643,16 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj, if (SymbolizeOperands && !Obj.isRelocatableObject()) ReadBBAddrMap(); + std::optional<llvm::BTFParser> BTF; + if (InlineRelocs && BTFParser::hasBTFSections(Obj)) { + BTF.emplace(); + BTFParser::ParseOptions Opts = {}; + Opts.LoadTypes = true; + Opts.LoadRelocs = true; + if (Error E = BTF->parse(Obj, Opts)) + WithColor::defaultErrorHandler(std::move(E)); + } + for (const SectionRef &Section : ToolSectionFilter(Obj)) { if (FilterSections.empty() && !DisassembleAll && (!Section.isText() || Section.isVirtual())) @@ -2163,6 +2190,9 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj, *DT->SubtargetInfo, CommentStream.str(), LVP); Comments.clear(); + if (BTF) + printBTFRelocation(FOS, *BTF, {Index, Section.getIndex()}, LVP); + // Hexagon does this in pretty printer if (Obj.getArch() != Triple::hexagon) { // Print relocation for instruction and data. |