diff options
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r-- | tools/lib/bpf/libbpf.c | 204 |
1 files changed, 173 insertions, 31 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 5aa45f89da93..71ddc481f349 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -1,3 +1,5 @@ | |||
1 | // SPDX-License-Identifier: LGPL-2.1 | ||
2 | |||
1 | /* | 3 | /* |
2 | * Common eBPF ELF object loading operations. | 4 | * Common eBPF ELF object loading operations. |
3 | * | 5 | * |
@@ -106,6 +108,8 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { | |||
106 | [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", | 108 | [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", |
107 | [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", | 109 | [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", |
108 | [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", | 110 | [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", |
111 | [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", | ||
112 | [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", | ||
109 | }; | 113 | }; |
110 | 114 | ||
111 | int libbpf_strerror(int err, char *buf, size_t size) | 115 | int libbpf_strerror(int err, char *buf, size_t size) |
@@ -174,12 +178,19 @@ struct bpf_program { | |||
174 | char *name; | 178 | char *name; |
175 | char *section_name; | 179 | char *section_name; |
176 | struct bpf_insn *insns; | 180 | struct bpf_insn *insns; |
177 | size_t insns_cnt; | 181 | size_t insns_cnt, main_prog_cnt; |
178 | enum bpf_prog_type type; | 182 | enum bpf_prog_type type; |
179 | 183 | ||
180 | struct { | 184 | struct reloc_desc { |
185 | enum { | ||
186 | RELO_LD64, | ||
187 | RELO_CALL, | ||
188 | } type; | ||
181 | int insn_idx; | 189 | int insn_idx; |
182 | int map_idx; | 190 | union { |
191 | int map_idx; | ||
192 | int text_off; | ||
193 | }; | ||
183 | } *reloc_desc; | 194 | } *reloc_desc; |
184 | int nr_reloc; | 195 | int nr_reloc; |
185 | 196 | ||
@@ -234,6 +245,7 @@ struct bpf_object { | |||
234 | } *reloc; | 245 | } *reloc; |
235 | int nr_reloc; | 246 | int nr_reloc; |
236 | int maps_shndx; | 247 | int maps_shndx; |
248 | int text_shndx; | ||
237 | } efile; | 249 | } efile; |
238 | /* | 250 | /* |
239 | * All loaded bpf_object is linked in a list, which is | 251 | * All loaded bpf_object is linked in a list, which is |
@@ -375,9 +387,13 @@ bpf_object__init_prog_names(struct bpf_object *obj) | |||
375 | size_t pi, si; | 387 | size_t pi, si; |
376 | 388 | ||
377 | for (pi = 0; pi < obj->nr_programs; pi++) { | 389 | for (pi = 0; pi < obj->nr_programs; pi++) { |
378 | char *name = NULL; | 390 | const char *name = NULL; |
379 | 391 | ||
380 | prog = &obj->programs[pi]; | 392 | prog = &obj->programs[pi]; |
393 | if (prog->idx == obj->efile.text_shndx) { | ||
394 | name = ".text"; | ||
395 | goto skip_search; | ||
396 | } | ||
381 | 397 | ||
382 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name; | 398 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name; |
383 | si++) { | 399 | si++) { |
@@ -387,6 +403,8 @@ bpf_object__init_prog_names(struct bpf_object *obj) | |||
387 | continue; | 403 | continue; |
388 | if (sym.st_shndx != prog->idx) | 404 | if (sym.st_shndx != prog->idx) |
389 | continue; | 405 | continue; |
406 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL) | ||
407 | continue; | ||
390 | 408 | ||
391 | name = elf_strptr(obj->efile.elf, | 409 | name = elf_strptr(obj->efile.elf, |
392 | obj->efile.strtabidx, | 410 | obj->efile.strtabidx, |
@@ -403,7 +421,7 @@ bpf_object__init_prog_names(struct bpf_object *obj) | |||
403 | prog->section_name); | 421 | prog->section_name); |
404 | return -EINVAL; | 422 | return -EINVAL; |
405 | } | 423 | } |
406 | 424 | skip_search: | |
407 | prog->name = strdup(name); | 425 | prog->name = strdup(name); |
408 | if (!prog->name) { | 426 | if (!prog->name) { |
409 | pr_warning("failed to allocate memory for prog sym %s\n", | 427 | pr_warning("failed to allocate memory for prog sym %s\n", |
@@ -793,6 +811,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj) | |||
793 | } else if ((sh.sh_type == SHT_PROGBITS) && | 811 | } else if ((sh.sh_type == SHT_PROGBITS) && |
794 | (sh.sh_flags & SHF_EXECINSTR) && | 812 | (sh.sh_flags & SHF_EXECINSTR) && |
795 | (data->d_size > 0)) { | 813 | (data->d_size > 0)) { |
814 | if (strcmp(name, ".text") == 0) | ||
815 | obj->efile.text_shndx = idx; | ||
796 | err = bpf_object__add_program(obj, data->d_buf, | 816 | err = bpf_object__add_program(obj, data->d_buf, |
797 | data->d_size, name, idx); | 817 | data->d_size, name, idx); |
798 | if (err) { | 818 | if (err) { |
@@ -854,11 +874,14 @@ bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) | |||
854 | } | 874 | } |
855 | 875 | ||
856 | static int | 876 | static int |
857 | bpf_program__collect_reloc(struct bpf_program *prog, | 877 | bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, |
858 | size_t nr_maps, GElf_Shdr *shdr, | 878 | Elf_Data *data, struct bpf_object *obj) |
859 | Elf_Data *data, Elf_Data *symbols, | ||
860 | int maps_shndx, struct bpf_map *maps) | ||
861 | { | 879 | { |
880 | Elf_Data *symbols = obj->efile.symbols; | ||
881 | int text_shndx = obj->efile.text_shndx; | ||
882 | int maps_shndx = obj->efile.maps_shndx; | ||
883 | struct bpf_map *maps = obj->maps; | ||
884 | size_t nr_maps = obj->nr_maps; | ||
862 | int i, nrels; | 885 | int i, nrels; |
863 | 886 | ||
864 | pr_debug("collecting relocating info for: '%s'\n", | 887 | pr_debug("collecting relocating info for: '%s'\n", |
@@ -891,8 +914,11 @@ bpf_program__collect_reloc(struct bpf_program *prog, | |||
891 | GELF_R_SYM(rel.r_info)); | 914 | GELF_R_SYM(rel.r_info)); |
892 | return -LIBBPF_ERRNO__FORMAT; | 915 | return -LIBBPF_ERRNO__FORMAT; |
893 | } | 916 | } |
917 | pr_debug("relo for %lld value %lld name %d\n", | ||
918 | (long long) (rel.r_info >> 32), | ||
919 | (long long) sym.st_value, sym.st_name); | ||
894 | 920 | ||
895 | if (sym.st_shndx != maps_shndx) { | 921 | if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) { |
896 | pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n", | 922 | pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n", |
897 | prog->section_name, sym.st_shndx); | 923 | prog->section_name, sym.st_shndx); |
898 | return -LIBBPF_ERRNO__RELOC; | 924 | return -LIBBPF_ERRNO__RELOC; |
@@ -901,6 +927,17 @@ bpf_program__collect_reloc(struct bpf_program *prog, | |||
901 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); | 927 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
902 | pr_debug("relocation: insn_idx=%u\n", insn_idx); | 928 | pr_debug("relocation: insn_idx=%u\n", insn_idx); |
903 | 929 | ||
930 | if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) { | ||
931 | if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) { | ||
932 | pr_warning("incorrect bpf_call opcode\n"); | ||
933 | return -LIBBPF_ERRNO__RELOC; | ||
934 | } | ||
935 | prog->reloc_desc[i].type = RELO_CALL; | ||
936 | prog->reloc_desc[i].insn_idx = insn_idx; | ||
937 | prog->reloc_desc[i].text_off = sym.st_value; | ||
938 | continue; | ||
939 | } | ||
940 | |||
904 | if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { | 941 | if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
905 | pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", | 942 | pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", |
906 | insn_idx, insns[insn_idx].code); | 943 | insn_idx, insns[insn_idx].code); |
@@ -922,6 +959,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, | |||
922 | return -LIBBPF_ERRNO__RELOC; | 959 | return -LIBBPF_ERRNO__RELOC; |
923 | } | 960 | } |
924 | 961 | ||
962 | prog->reloc_desc[i].type = RELO_LD64; | ||
925 | prog->reloc_desc[i].insn_idx = insn_idx; | 963 | prog->reloc_desc[i].insn_idx = insn_idx; |
926 | prog->reloc_desc[i].map_idx = map_idx; | 964 | prog->reloc_desc[i].map_idx = map_idx; |
927 | } | 965 | } |
@@ -961,27 +999,76 @@ bpf_object__create_maps(struct bpf_object *obj) | |||
961 | } | 999 | } |
962 | 1000 | ||
963 | static int | 1001 | static int |
1002 | bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, | ||
1003 | struct reloc_desc *relo) | ||
1004 | { | ||
1005 | struct bpf_insn *insn, *new_insn; | ||
1006 | struct bpf_program *text; | ||
1007 | size_t new_cnt; | ||
1008 | |||
1009 | if (relo->type != RELO_CALL) | ||
1010 | return -LIBBPF_ERRNO__RELOC; | ||
1011 | |||
1012 | if (prog->idx == obj->efile.text_shndx) { | ||
1013 | pr_warning("relo in .text insn %d into off %d\n", | ||
1014 | relo->insn_idx, relo->text_off); | ||
1015 | return -LIBBPF_ERRNO__RELOC; | ||
1016 | } | ||
1017 | |||
1018 | if (prog->main_prog_cnt == 0) { | ||
1019 | text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx); | ||
1020 | if (!text) { | ||
1021 | pr_warning("no .text section found yet relo into text exist\n"); | ||
1022 | return -LIBBPF_ERRNO__RELOC; | ||
1023 | } | ||
1024 | new_cnt = prog->insns_cnt + text->insns_cnt; | ||
1025 | new_insn = realloc(prog->insns, new_cnt * sizeof(*insn)); | ||
1026 | if (!new_insn) { | ||
1027 | pr_warning("oom in prog realloc\n"); | ||
1028 | return -ENOMEM; | ||
1029 | } | ||
1030 | memcpy(new_insn + prog->insns_cnt, text->insns, | ||
1031 | text->insns_cnt * sizeof(*insn)); | ||
1032 | prog->insns = new_insn; | ||
1033 | prog->main_prog_cnt = prog->insns_cnt; | ||
1034 | prog->insns_cnt = new_cnt; | ||
1035 | } | ||
1036 | insn = &prog->insns[relo->insn_idx]; | ||
1037 | insn->imm += prog->main_prog_cnt - relo->insn_idx; | ||
1038 | pr_debug("added %zd insn from %s to prog %s\n", | ||
1039 | text->insns_cnt, text->section_name, prog->section_name); | ||
1040 | return 0; | ||
1041 | } | ||
1042 | |||
1043 | static int | ||
964 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) | 1044 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) |
965 | { | 1045 | { |
966 | int i; | 1046 | int i, err; |
967 | 1047 | ||
968 | if (!prog || !prog->reloc_desc) | 1048 | if (!prog || !prog->reloc_desc) |
969 | return 0; | 1049 | return 0; |
970 | 1050 | ||
971 | for (i = 0; i < prog->nr_reloc; i++) { | 1051 | for (i = 0; i < prog->nr_reloc; i++) { |
972 | int insn_idx, map_idx; | 1052 | if (prog->reloc_desc[i].type == RELO_LD64) { |
973 | struct bpf_insn *insns = prog->insns; | 1053 | struct bpf_insn *insns = prog->insns; |
1054 | int insn_idx, map_idx; | ||
974 | 1055 | ||
975 | insn_idx = prog->reloc_desc[i].insn_idx; | 1056 | insn_idx = prog->reloc_desc[i].insn_idx; |
976 | map_idx = prog->reloc_desc[i].map_idx; | 1057 | map_idx = prog->reloc_desc[i].map_idx; |
977 | 1058 | ||
978 | if (insn_idx >= (int)prog->insns_cnt) { | 1059 | if (insn_idx >= (int)prog->insns_cnt) { |
979 | pr_warning("relocation out of range: '%s'\n", | 1060 | pr_warning("relocation out of range: '%s'\n", |
980 | prog->section_name); | 1061 | prog->section_name); |
981 | return -LIBBPF_ERRNO__RELOC; | 1062 | return -LIBBPF_ERRNO__RELOC; |
1063 | } | ||
1064 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; | ||
1065 | insns[insn_idx].imm = obj->maps[map_idx].fd; | ||
1066 | } else { | ||
1067 | err = bpf_program__reloc_text(prog, obj, | ||
1068 | &prog->reloc_desc[i]); | ||
1069 | if (err) | ||
1070 | return err; | ||
982 | } | 1071 | } |
983 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; | ||
984 | insns[insn_idx].imm = obj->maps[map_idx].fd; | ||
985 | } | 1072 | } |
986 | 1073 | ||
987 | zfree(&prog->reloc_desc); | 1074 | zfree(&prog->reloc_desc); |
@@ -1024,7 +1111,6 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) | |||
1024 | Elf_Data *data = obj->efile.reloc[i].data; | 1111 | Elf_Data *data = obj->efile.reloc[i].data; |
1025 | int idx = shdr->sh_info; | 1112 | int idx = shdr->sh_info; |
1026 | struct bpf_program *prog; | 1113 | struct bpf_program *prog; |
1027 | size_t nr_maps = obj->nr_maps; | ||
1028 | 1114 | ||
1029 | if (shdr->sh_type != SHT_REL) { | 1115 | if (shdr->sh_type != SHT_REL) { |
1030 | pr_warning("internal error at %d\n", __LINE__); | 1116 | pr_warning("internal error at %d\n", __LINE__); |
@@ -1038,11 +1124,9 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) | |||
1038 | return -LIBBPF_ERRNO__RELOC; | 1124 | return -LIBBPF_ERRNO__RELOC; |
1039 | } | 1125 | } |
1040 | 1126 | ||
1041 | err = bpf_program__collect_reloc(prog, nr_maps, | 1127 | err = bpf_program__collect_reloc(prog, |
1042 | shdr, data, | 1128 | shdr, data, |
1043 | obj->efile.symbols, | 1129 | obj); |
1044 | obj->efile.maps_shndx, | ||
1045 | obj->maps); | ||
1046 | if (err) | 1130 | if (err) |
1047 | return err; | 1131 | return err; |
1048 | } | 1132 | } |
@@ -1195,6 +1279,8 @@ bpf_object__load_progs(struct bpf_object *obj) | |||
1195 | int err; | 1279 | int err; |
1196 | 1280 | ||
1197 | for (i = 0; i < obj->nr_programs; i++) { | 1281 | for (i = 0; i < obj->nr_programs; i++) { |
1282 | if (obj->programs[i].idx == obj->efile.text_shndx) | ||
1283 | continue; | ||
1198 | err = bpf_program__load(&obj->programs[i], | 1284 | err = bpf_program__load(&obj->programs[i], |
1199 | obj->license, | 1285 | obj->license, |
1200 | obj->kern_version); | 1286 | obj->kern_version); |
@@ -1721,6 +1807,45 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); | |||
1721 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); | 1807 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); |
1722 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); | 1808 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); |
1723 | 1809 | ||
1810 | #define BPF_PROG_SEC(string, type) { string, sizeof(string) - 1, type } | ||
1811 | static const struct { | ||
1812 | const char *sec; | ||
1813 | size_t len; | ||
1814 | enum bpf_prog_type prog_type; | ||
1815 | } section_names[] = { | ||
1816 | BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), | ||
1817 | BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), | ||
1818 | BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), | ||
1819 | BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT), | ||
1820 | BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), | ||
1821 | BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), | ||
1822 | BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), | ||
1823 | BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK), | ||
1824 | BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE), | ||
1825 | BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS), | ||
1826 | BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB), | ||
1827 | }; | ||
1828 | #undef BPF_PROG_SEC | ||
1829 | |||
1830 | static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog) | ||
1831 | { | ||
1832 | int i; | ||
1833 | |||
1834 | if (!prog->section_name) | ||
1835 | goto err; | ||
1836 | |||
1837 | for (i = 0; i < ARRAY_SIZE(section_names); i++) | ||
1838 | if (strncmp(prog->section_name, section_names[i].sec, | ||
1839 | section_names[i].len) == 0) | ||
1840 | return section_names[i].prog_type; | ||
1841 | |||
1842 | err: | ||
1843 | pr_warning("failed to guess program type based on section name %s\n", | ||
1844 | prog->section_name); | ||
1845 | |||
1846 | return BPF_PROG_TYPE_UNSPEC; | ||
1847 | } | ||
1848 | |||
1724 | int bpf_map__fd(struct bpf_map *map) | 1849 | int bpf_map__fd(struct bpf_map *map) |
1725 | { | 1850 | { |
1726 | return map ? map->fd : -EINVAL; | 1851 | return map ? map->fd : -EINVAL; |
@@ -1818,7 +1943,7 @@ long libbpf_get_error(const void *ptr) | |||
1818 | int bpf_prog_load(const char *file, enum bpf_prog_type type, | 1943 | int bpf_prog_load(const char *file, enum bpf_prog_type type, |
1819 | struct bpf_object **pobj, int *prog_fd) | 1944 | struct bpf_object **pobj, int *prog_fd) |
1820 | { | 1945 | { |
1821 | struct bpf_program *prog; | 1946 | struct bpf_program *prog, *first_prog = NULL; |
1822 | struct bpf_object *obj; | 1947 | struct bpf_object *obj; |
1823 | int err; | 1948 | int err; |
1824 | 1949 | ||
@@ -1826,13 +1951,30 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, | |||
1826 | if (IS_ERR(obj)) | 1951 | if (IS_ERR(obj)) |
1827 | return -ENOENT; | 1952 | return -ENOENT; |
1828 | 1953 | ||
1829 | prog = bpf_program__next(NULL, obj); | 1954 | bpf_object__for_each_program(prog, obj) { |
1830 | if (!prog) { | 1955 | /* |
1956 | * If type is not specified, try to guess it based on | ||
1957 | * section name. | ||
1958 | */ | ||
1959 | if (type == BPF_PROG_TYPE_UNSPEC) { | ||
1960 | type = bpf_program__guess_type(prog); | ||
1961 | if (type == BPF_PROG_TYPE_UNSPEC) { | ||
1962 | bpf_object__close(obj); | ||
1963 | return -EINVAL; | ||
1964 | } | ||
1965 | } | ||
1966 | |||
1967 | bpf_program__set_type(prog, type); | ||
1968 | if (prog->idx != obj->efile.text_shndx && !first_prog) | ||
1969 | first_prog = prog; | ||
1970 | } | ||
1971 | |||
1972 | if (!first_prog) { | ||
1973 | pr_warning("object file doesn't contain bpf program\n"); | ||
1831 | bpf_object__close(obj); | 1974 | bpf_object__close(obj); |
1832 | return -ENOENT; | 1975 | return -ENOENT; |
1833 | } | 1976 | } |
1834 | 1977 | ||
1835 | bpf_program__set_type(prog, type); | ||
1836 | err = bpf_object__load(obj); | 1978 | err = bpf_object__load(obj); |
1837 | if (err) { | 1979 | if (err) { |
1838 | bpf_object__close(obj); | 1980 | bpf_object__close(obj); |
@@ -1840,6 +1982,6 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, | |||
1840 | } | 1982 | } |
1841 | 1983 | ||
1842 | *pobj = obj; | 1984 | *pobj = obj; |
1843 | *prog_fd = bpf_program__fd(prog); | 1985 | *prog_fd = bpf_program__fd(first_prog); |
1844 | return 0; | 1986 | return 0; |
1845 | } | 1987 | } |