[PATCH] D150079: [BPF][DebugInfo] Show CO-RE relocations in llvm-objdump

Eduard Zingerman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 16 07:35:05 PDT 2023


eddyz87 created this revision.
Herald added subscribers: mgrang, hiraditya.
Herald added a reviewer: jhenderson.
Herald added a reviewer: MaskRay.
Herald added a project: All.
anakryiko added a comment.
eddyz87 updated this revision to Diff 537664.
eddyz87 updated this revision to Diff 540808.
eddyz87 edited reviewers, added: yonghong-song; removed: jhenderson.
eddyz87 published this revision for review.
eddyz87 added a subscriber: yonghong-song.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: jhenderson.
Herald added subscribers: llvm-commits, jplehr, sstefan1.
Herald added a project: LLVM.

Do you think it would be possible to keep output format close to libbpf's one? Here are few different CO-RE relocations as logged by libbpf, to give you an idea:

  <byte_off> [6] struct task_struct.comm (0:115 @ offset 1944)
  <field_exists> [586] struct bpf_testmod_test_read_ctx.buf (0:1 @ offset 8)
  <enumval_value> [7] enum named_enum::NAMED_ENUM_VAL2 = 2
  <enumval_value> [5] typedef anon_enum::ANON_ENUM_VAL1 = 16
  <byte_off> [5] struct core_reloc_arrays.a[2] (0:0:2 @ offset 8)
  <byte_off> [5] struct core_reloc_arrays.b[1][2][3] (0:1:1:2:3 @ offset 43)
  <byte_off> [5] struct core_reloc_ptr_as_arr[2].a (2:0 @ offset 8)
  <byte_off> [5] struct core_reloc_nesting.a.a.a (0:0:0:0 @ offset 0)
  <byte_off> [5] struct core_reloc_nesting.b.b.b (0:1:0:0 @ offset 4)

Note how libbpf outputs almost C-like access syntax, and skips unnecessary [0] for initial array reference.


eddyz87 added a comment.

Rebase, update in line with fixes suggeested for D149058 <https://reviews.llvm.org/D149058>.


eddyz87 added a comment.

- styling fixes
- test case to check formatting
- adjust-vma option support


eddyz87 added a comment.

Hi @MaskRay, @yonghong-song,

Could you please take a look at this revision?
It adds support for CO-RE relocations printing in llvm-objdump.
While current implementation works, there are two aspects that might be sub-optimal:

- ELF relocations are not unified with BTF relocations and thus there are two functions: `llvm-objdump.cpp:printRelocation()`, `llvm-objdump.cpp:printBTFRelocation()`;
- The `BTFParser` instance is not shared between `LLVMSymbolizer` (used for source line printing) and `llvm-objdump.cpp:disassembleObject()` (used for relocations printing).

Fixing both items might require some interface level changes that I opted not to do at the moment. Please let me know what you think.


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 -g t.c -c -o - | \
      llvm-objdump  --no-addresses --no-show-raw-insn -dr -
  
  ...
  	*(u64 *)(r10 - 0x8) = r1
  	r1 = *(u64 *)(r10 - 0x8)
  	r2 = 0x8c
  	CO-RE relo: FIELD_BYTE_OFFSET [3] struct bar::[2].f[3].j
  ...

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://nakryiko.com/posts/bpf-core-reference-guide/

Depends on D149058 <https://reviews.llvm.org/D149058>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150079

Files:
  llvm/include/llvm/DebugInfo/BTF/BTF.h
  llvm/include/llvm/DebugInfo/BTF/BTFParser.h
  llvm/include/llvm/DebugInfo/BTF/CoreRelo.def
  llvm/lib/DebugInfo/BTF/BTFContext.cpp
  llvm/lib/DebugInfo/BTF/BTFParser.cpp
  llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
  llvm/lib/Target/BPF/BPFCORE.h
  llvm/lib/Target/BPF/BPFPreserveDIType.cpp
  llvm/lib/Target/BPF/BTFDebug.cpp
  llvm/test/tools/llvm-objdump/BPF/core-relo-byte-offset.ll
  llvm/test/tools/llvm-objdump/BPF/core-relo-enum-value.ll
  llvm/test/tools/llvm-objdump/BPF/core-relo-field-info.ll
  llvm/test/tools/llvm-objdump/BPF/core-relo-formatting.s
  llvm/test/tools/llvm-objdump/BPF/core-relo-type-id.ll
  llvm/test/tools/llvm-objdump/BPF/core-relo-type-info.ll
  llvm/tools/llvm-objdump/llvm-objdump.cpp
  llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150079.540808.patch
Type: text/x-patch
Size: 120659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230716/7f7208e4/attachment.bin>


More information about the llvm-commits mailing list