Skip to content

Commit 393cdfb

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Support initialized global variables
Initialized global variables are no different in ELF from static variables, and don't require any extra support from libbpf. But they are matching semantics of global data (backed by BPF maps) more closely, preventing LLVM/Clang from aggressively inlining constant values and not requiring volatile incantations to prevent those. This patch enables global variables. It still disables uninitialized variables, which will be put into special COM (common) ELF section, because BPF doesn't allow uninitialized data to be accessed. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8983b73 commit 393cdfb

14 files changed

+28
-33
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,8 +1835,8 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
18351835
return -LIBBPF_ERRNO__RELOC;
18361836
}
18371837
if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
1838-
pr_warn("relocation: not yet supported relo for non-static global \'%s\' variable in special section (0x%x) found in insns[%d].code 0x%x\n",
1839-
name, shdr_idx, insn_idx, insn->code);
1838+
pr_warn("invalid relo for \'%s\' in special section 0x%x; forgot to initialize global var?..\n",
1839+
name, shdr_idx);
18401840
return -LIBBPF_ERRNO__RELOC;
18411841
}
18421842

@@ -1876,11 +1876,6 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
18761876
pr_warn("bad data relo against section %u\n", shdr_idx);
18771877
return -LIBBPF_ERRNO__RELOC;
18781878
}
1879-
if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
1880-
pr_warn("relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1881-
name, insn_idx, insn->code);
1882-
return -LIBBPF_ERRNO__RELOC;
1883-
}
18841879
if (!obj->caps.global_data) {
18851880
pr_warn("relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
18861881
name, insn_idx);

tools/testing/selftests/bpf/progs/test_core_reloc_arrays.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_arrays_output {
1717
int a2;

tools/testing/selftests/bpf/progs/test_core_reloc_bitfields_direct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_bitfields {
1717
/* unsigned bitfields */

tools/testing/selftests/bpf/progs/test_core_reloc_bitfields_probed.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_bitfields {
1717
/* unsigned bitfields */

tools/testing/selftests/bpf/progs/test_core_reloc_existence.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_existence_output {
1717
int a_exists;

tools/testing/selftests/bpf/progs/test_core_reloc_flavors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_flavors {
1717
int a;

tools/testing/selftests/bpf/progs/test_core_reloc_ints.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_ints {
1717
uint8_t u8_field;

tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_kernel_output {
1717
int valid[10];

tools/testing/selftests/bpf/progs/test_core_reloc_misc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_misc_output {
1717
int a, b, c;

tools/testing/selftests/bpf/progs/test_core_reloc_mods.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_mods_output {
1717
int a, b, c, d, e, f, g, h;

tools/testing/selftests/bpf/progs/test_core_reloc_nesting.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_nesting_substruct {
1717
int a;

tools/testing/selftests/bpf/progs/test_core_reloc_primitives.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
enum core_reloc_primitives_enum {
1717
A = 0,

tools/testing/selftests/bpf/progs/test_core_reloc_ptr_as_arr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_ptr_as_arr {
1717
int a;

tools/testing/selftests/bpf/progs/test_core_reloc_size.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11-
static volatile struct data {
11+
struct {
1212
char in[256];
1313
char out[256];
14-
} data;
14+
} data = {};
1515

1616
struct core_reloc_size_output {
1717
int int_sz;

0 commit comments

Comments
 (0)