Andrii Nakryiko 53f8dd434b libbpf: Fix global variable relocation
Similarly to a0d7da26ce86 ("libbpf: Fix call relocation offset calculation
bug"), relocations against global variables need to take into account
referenced symbol's st_value, which holds offset into a corresponding data
section (and, subsequently, offset into internal backing map). For static
variables this offset is always zero and data offset is completely described
by respective instruction's imm field.

Convert a bunch of selftests to global variables. Previously they were relying
on `static volatile` trick to ensure Clang doesn't inline static variables,
which with global variables is not necessary anymore.

Fixes: 393cdfbee809 ("libbpf: Support initialized global variables")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20191127200651.1381348-1-andriin@fb.com
2019-11-27 16:34:21 -08:00

46 lines
944 B
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Facebook
#include <linux/bpf.h>
#include <stdint.h>
#include "bpf_helpers.h"
char _license[] SEC("license") = "GPL";
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 512 * 4); /* at least 4 pages of data */
__uint(map_flags, BPF_F_MMAPABLE);
__type(key, __u32);
__type(value, __u64);
} data_map SEC(".maps");
__u64 in_val = 0;
__u64 out_val = 0;
SEC("raw_tracepoint/sys_enter")
int test_mmap(void *ctx)
{
int zero = 0, one = 1, two = 2, far = 1500;
__u64 val, *p;
out_val = in_val;
/* data_map[2] = in_val; */
bpf_map_update_elem(&data_map, &two, (const void *)&in_val, 0);
/* data_map[1] = data_map[0] * 2; */
p = bpf_map_lookup_elem(&data_map, &zero);
if (p) {
val = (*p) * 2;
bpf_map_update_elem(&data_map, &one, &val, 0);
}
/* data_map[far] = in_val * 3; */
val = in_val * 3;
bpf_map_update_elem(&data_map, &far, &val, 0);
return 0;
}