Linux Perf
llvm-src-relocation.c
Go to the documentation of this file.
1 #include <tests/llvm.h>
3 "/*\n"
4 " * bpf-script-test-relocation.c\n"
5 " * Test BPF loader checking relocation\n"
6 " */\n"
7 "#ifndef LINUX_VERSION_CODE\n"
8 "# error Need LINUX_VERSION_CODE\n"
9 "# error Example: for 4.2 kernel, put 'clang-opt=\"-DLINUX_VERSION_CODE=0x40200\" into llvm section of ~/.perfconfig'\n"
10 "#endif\n"
11 "#define BPF_ANY 0\n"
12 "#define BPF_MAP_TYPE_ARRAY 2\n"
13 "#define BPF_FUNC_map_lookup_elem 1\n"
14 "#define BPF_FUNC_map_update_elem 2\n"
15 "\n"
16 "static void *(*bpf_map_lookup_elem)(void *map, void *key) =\n"
17 " (void *) BPF_FUNC_map_lookup_elem;\n"
18 "static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =\n"
19 " (void *) BPF_FUNC_map_update_elem;\n"
20 "\n"
21 "struct bpf_map_def {\n"
22 " unsigned int type;\n"
23 " unsigned int key_size;\n"
24 " unsigned int value_size;\n"
25 " unsigned int max_entries;\n"
26 "};\n"
27 "\n"
28 "#define SEC(NAME) __attribute__((section(NAME), used))\n"
29 "struct bpf_map_def SEC(\"maps\") my_table = {\n"
30 " .type = BPF_MAP_TYPE_ARRAY,\n"
31 " .key_size = sizeof(int),\n"
32 " .value_size = sizeof(int),\n"
33 " .max_entries = 1,\n"
34 "};\n"
35 "\n"
36 "int this_is_a_global_val;\n"
37 "\n"
38 "SEC(\"func=sys_write\")\n"
39 "int bpf_func__sys_write(void *ctx)\n"
40 "{\n"
41 " int key = 0;\n"
42 " int value = 0;\n"
43 "\n"
44 " /*\n"
45 " * Incorrect relocation. Should not allow this program be\n"
46 " * loaded into kernel.\n"
47 " */\n"
48 " bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);\n"
49 " return 0;\n"
50 "}\n"
51 "char _license[] SEC(\"license\") = \"GPL\";\n"
52 "int _version SEC(\"version\") = LINUX_VERSION_CODE;\n"
53 ;
const char test_llvm__bpf_test_relocation[]