llvm-project/llvm/lib/DebugInfo/BTF/BTFContext.cpp
Eduard Zingerman d15f96fe4b [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
2023-09-21 21:59:10 +03:00

72 lines
2.4 KiB
C++

//===- BTFContext.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implementation of the BTFContext interface, this is used by
// llvm-objdump tool to print source code alongside disassembly.
// In fact, currently it is a simple wrapper for BTFParser instance.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/BTF/BTFContext.h"
#define DEBUG_TYPE "debug-info-btf-context"
using namespace llvm;
using object::ObjectFile;
using object::SectionedAddress;
DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address,
DILineInfoSpecifier Specifier) {
const BTF::BPFLineInfo *LineInfo = BTF.findLineInfo(Address);
DILineInfo Result;
if (!LineInfo)
return Result;
Result.LineSource = BTF.findString(LineInfo->LineOff);
Result.FileName = BTF.findString(LineInfo->FileNameOff);
Result.Line = LineInfo->getLine();
Result.Column = LineInfo->getCol();
return Result;
}
DILineInfo BTFContext::getLineInfoForDataAddress(SectionedAddress Address) {
// BTF does not convey such information.
return {};
}
DILineInfoTable
BTFContext::getLineInfoForAddressRange(SectionedAddress Address, uint64_t Size,
DILineInfoSpecifier Specifier) {
// This function is used only from llvm-rtdyld utility and a few
// JITEventListener implementations. Ignore it for now.
return {};
}
DIInliningInfo
BTFContext::getInliningInfoForAddress(SectionedAddress Address,
DILineInfoSpecifier Specifier) {
// BTF does not convey such information
return {};
}
std::vector<DILocal> BTFContext::getLocalsForAddress(SectionedAddress Address) {
// BTF does not convey such information
return {};
}
std::unique_ptr<BTFContext>
BTFContext::create(const ObjectFile &Obj,
std::function<void(Error)> ErrorHandler) {
auto Ctx = std::make_unique<BTFContext>();
BTFParser::ParseOptions Opts;
Opts.LoadLines = true;
if (Error E = Ctx->BTF.parse(Obj, Opts))
ErrorHandler(std::move(E));
return Ctx;
}