This is an archive of the discontinued LLVM Phabricator instance.

BPF: generate .rodata BTF datasec for certain initialized local var's

Authored by yonghong-song on Jul 16 2020, 10:43 PM.



Currently, BTF datasec type for .rodata is generated only if there are
user-defined readonly global variables which have debuginfo generated.

Certain readonly global variables may be generated from initialized
local variables. For example,

void foo(const void *); 
int test() {
  const struct {
    unsigned a[4];
    char b;
  } val = { .a = {2, 3, 4, 5}, .b = 6 };
  return 0;

The clang will create a private linkage const global to store
the initialized value:

@__const.test.val = private unnamed_addr constant %struct.anon
    { [4 x i32] [i32 2, i32 3, i32 4, i32 5], i8 6 }, align 4

This global variable eventually is put in .rodata ELF section.

If there is .rodata ELF section, libbpf expects a BTF .rodata
datasec as well even though it may be empty meaning there are no
global readonly variables with proper debuginfo. Martin reported
a bug where without this empty BTF .rodata datasec, the bpftool
gen will exit with an error.

This patch fixed the issue by generating .rodata BTF datasec
if there exists local var intial data which will result in
.rodata ELF section.

Diff Detail

Event Timeline

yonghong-song created this revision.Jul 16 2020, 10:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2020, 10:43 PM
anakryiko accepted this revision.Jul 16 2020, 11:00 PM

Thanks for the fix! LGTM.

This revision is now accepted and ready to land.Jul 16 2020, 11:00 PM
ast accepted this revision.Jul 17 2020, 9:05 AM
This revision was automatically updated to reflect the committed changes.