Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common eBPF ELF object loading operations. |
| 3 | * |
| 4 | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
| 5 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 6 | * Copyright (C) 2015 Huawei Inc. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdarg.h> |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 12 | #include <inttypes.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 13 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | #include <errno.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 17 | #include <asm/unistd.h> |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 19 | #include <linux/bpf.h> |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 20 | #include <linux/list.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 21 | #include <libelf.h> |
| 22 | #include <gelf.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 23 | |
| 24 | #include "libbpf.h" |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 25 | #include "bpf.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 26 | |
| 27 | #define __printf(a, b) __attribute__((format(printf, a, b))) |
| 28 | |
| 29 | __printf(1, 2) |
| 30 | static int __base_pr(const char *format, ...) |
| 31 | { |
| 32 | va_list args; |
| 33 | int err; |
| 34 | |
| 35 | va_start(args, format); |
| 36 | err = vfprintf(stderr, format, args); |
| 37 | va_end(args); |
| 38 | return err; |
| 39 | } |
| 40 | |
| 41 | static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr; |
| 42 | static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr; |
| 43 | static __printf(1, 2) libbpf_print_fn_t __pr_debug; |
| 44 | |
| 45 | #define __pr(func, fmt, ...) \ |
| 46 | do { \ |
| 47 | if ((func)) \ |
| 48 | (func)("libbpf: " fmt, ##__VA_ARGS__); \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__) |
| 52 | #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__) |
| 53 | #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__) |
| 54 | |
| 55 | void libbpf_set_print(libbpf_print_fn_t warn, |
| 56 | libbpf_print_fn_t info, |
| 57 | libbpf_print_fn_t debug) |
| 58 | { |
| 59 | __pr_warning = warn; |
| 60 | __pr_info = info; |
| 61 | __pr_debug = debug; |
| 62 | } |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 63 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 64 | #define STRERR_BUFSIZE 128 |
| 65 | |
| 66 | #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) |
| 67 | #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) |
| 68 | #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) |
| 69 | |
| 70 | static const char *libbpf_strerror_table[NR_ERRNO] = { |
| 71 | [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", |
| 72 | [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", |
| 73 | [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", |
| 74 | [ERRCODE_OFFSET(ENDIAN)] = "Endian missmatch", |
| 75 | [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", |
| 76 | [ERRCODE_OFFSET(RELOC)] = "Relocation failed", |
| 77 | [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", |
| 78 | [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", |
| 79 | [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", |
| 80 | }; |
| 81 | |
| 82 | int libbpf_strerror(int err, char *buf, size_t size) |
| 83 | { |
| 84 | if (!buf || !size) |
| 85 | return -1; |
| 86 | |
| 87 | err = err > 0 ? err : -err; |
| 88 | |
| 89 | if (err < __LIBBPF_ERRNO__START) { |
| 90 | int ret; |
| 91 | |
| 92 | ret = strerror_r(err, buf, size); |
| 93 | buf[size - 1] = '\0'; |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | if (err < __LIBBPF_ERRNO__END) { |
| 98 | const char *msg; |
| 99 | |
| 100 | msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; |
| 101 | snprintf(buf, size, "%s", msg); |
| 102 | buf[size - 1] = '\0'; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | snprintf(buf, size, "Unknown libbpf error %d", err); |
| 107 | buf[size - 1] = '\0'; |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | #define CHECK_ERR(action, err, out) do { \ |
| 112 | err = action; \ |
| 113 | if (err) \ |
| 114 | goto out; \ |
| 115 | } while(0) |
| 116 | |
| 117 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 118 | /* Copied from tools/perf/util/util.h */ |
| 119 | #ifndef zfree |
| 120 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 121 | #endif |
| 122 | |
| 123 | #ifndef zclose |
| 124 | # define zclose(fd) ({ \ |
| 125 | int ___err = 0; \ |
| 126 | if ((fd) >= 0) \ |
| 127 | ___err = close((fd)); \ |
| 128 | fd = -1; \ |
| 129 | ___err; }) |
| 130 | #endif |
| 131 | |
| 132 | #ifdef HAVE_LIBELF_MMAP_SUPPORT |
| 133 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP |
| 134 | #else |
| 135 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ |
| 136 | #endif |
| 137 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 138 | /* |
| 139 | * bpf_prog should be a better name but it has been used in |
| 140 | * linux/filter.h. |
| 141 | */ |
| 142 | struct bpf_program { |
| 143 | /* Index in elf obj file, for relocation use. */ |
| 144 | int idx; |
| 145 | char *section_name; |
| 146 | struct bpf_insn *insns; |
| 147 | size_t insns_cnt; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 148 | |
| 149 | struct { |
| 150 | int insn_idx; |
| 151 | int map_idx; |
| 152 | } *reloc_desc; |
| 153 | int nr_reloc; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 154 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 155 | struct { |
| 156 | int nr; |
| 157 | int *fds; |
| 158 | } instances; |
| 159 | bpf_program_prep_t preprocessor; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 160 | |
| 161 | struct bpf_object *obj; |
| 162 | void *priv; |
| 163 | bpf_program_clear_priv_t clear_priv; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 166 | static LIST_HEAD(bpf_objects_list); |
| 167 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 168 | struct bpf_object { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 169 | char license[64]; |
| 170 | u32 kern_version; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 171 | void *maps_buf; |
| 172 | size_t maps_buf_sz; |
| 173 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 174 | struct bpf_program *programs; |
| 175 | size_t nr_programs; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 176 | int *map_fds; |
| 177 | /* |
| 178 | * This field is required because maps_buf will be freed and |
| 179 | * maps_buf_sz will be set to 0 after loaded. |
| 180 | */ |
| 181 | size_t nr_map_fds; |
| 182 | bool loaded; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 183 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Information when doing elf related work. Only valid if fd |
| 186 | * is valid. |
| 187 | */ |
| 188 | struct { |
| 189 | int fd; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 190 | void *obj_buf; |
| 191 | size_t obj_buf_sz; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 192 | Elf *elf; |
| 193 | GElf_Ehdr ehdr; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 194 | Elf_Data *symbols; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 195 | struct { |
| 196 | GElf_Shdr shdr; |
| 197 | Elf_Data *data; |
| 198 | } *reloc; |
| 199 | int nr_reloc; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 200 | } efile; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 201 | /* |
| 202 | * All loaded bpf_object is linked in a list, which is |
| 203 | * hidden to caller. bpf_objects__<func> handlers deal with |
| 204 | * all objects. |
| 205 | */ |
| 206 | struct list_head list; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 207 | char path[]; |
| 208 | }; |
| 209 | #define obj_elf_valid(o) ((o)->efile.elf) |
| 210 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 211 | static void bpf_program__unload(struct bpf_program *prog) |
| 212 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 213 | int i; |
| 214 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 215 | if (!prog) |
| 216 | return; |
| 217 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 218 | /* |
| 219 | * If the object is opened but the program was never loaded, |
| 220 | * it is possible that prog->instances.nr == -1. |
| 221 | */ |
| 222 | if (prog->instances.nr > 0) { |
| 223 | for (i = 0; i < prog->instances.nr; i++) |
| 224 | zclose(prog->instances.fds[i]); |
| 225 | } else if (prog->instances.nr != -1) { |
| 226 | pr_warning("Internal error: instances.nr is %d\n", |
| 227 | prog->instances.nr); |
| 228 | } |
| 229 | |
| 230 | prog->instances.nr = -1; |
| 231 | zfree(&prog->instances.fds); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 234 | static void bpf_program__exit(struct bpf_program *prog) |
| 235 | { |
| 236 | if (!prog) |
| 237 | return; |
| 238 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 239 | if (prog->clear_priv) |
| 240 | prog->clear_priv(prog, prog->priv); |
| 241 | |
| 242 | prog->priv = NULL; |
| 243 | prog->clear_priv = NULL; |
| 244 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 245 | bpf_program__unload(prog); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 246 | zfree(&prog->section_name); |
| 247 | zfree(&prog->insns); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 248 | zfree(&prog->reloc_desc); |
| 249 | |
| 250 | prog->nr_reloc = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 251 | prog->insns_cnt = 0; |
| 252 | prog->idx = -1; |
| 253 | } |
| 254 | |
| 255 | static int |
| 256 | bpf_program__init(void *data, size_t size, char *name, int idx, |
| 257 | struct bpf_program *prog) |
| 258 | { |
| 259 | if (size < sizeof(struct bpf_insn)) { |
| 260 | pr_warning("corrupted section '%s'\n", name); |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | |
| 264 | bzero(prog, sizeof(*prog)); |
| 265 | |
| 266 | prog->section_name = strdup(name); |
| 267 | if (!prog->section_name) { |
| 268 | pr_warning("failed to alloc name for prog %s\n", |
| 269 | name); |
| 270 | goto errout; |
| 271 | } |
| 272 | |
| 273 | prog->insns = malloc(size); |
| 274 | if (!prog->insns) { |
| 275 | pr_warning("failed to alloc insns for %s\n", name); |
| 276 | goto errout; |
| 277 | } |
| 278 | prog->insns_cnt = size / sizeof(struct bpf_insn); |
| 279 | memcpy(prog->insns, data, |
| 280 | prog->insns_cnt * sizeof(struct bpf_insn)); |
| 281 | prog->idx = idx; |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 282 | prog->instances.fds = NULL; |
| 283 | prog->instances.nr = -1; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 284 | |
| 285 | return 0; |
| 286 | errout: |
| 287 | bpf_program__exit(prog); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | bpf_object__add_program(struct bpf_object *obj, void *data, size_t size, |
| 293 | char *name, int idx) |
| 294 | { |
| 295 | struct bpf_program prog, *progs; |
| 296 | int nr_progs, err; |
| 297 | |
| 298 | err = bpf_program__init(data, size, name, idx, &prog); |
| 299 | if (err) |
| 300 | return err; |
| 301 | |
| 302 | progs = obj->programs; |
| 303 | nr_progs = obj->nr_programs; |
| 304 | |
| 305 | progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1)); |
| 306 | if (!progs) { |
| 307 | /* |
| 308 | * In this case the original obj->programs |
| 309 | * is still valid, so don't need special treat for |
| 310 | * bpf_close_object(). |
| 311 | */ |
| 312 | pr_warning("failed to alloc a new program '%s'\n", |
| 313 | name); |
| 314 | bpf_program__exit(&prog); |
| 315 | return -ENOMEM; |
| 316 | } |
| 317 | |
| 318 | pr_debug("found program %s\n", prog.section_name); |
| 319 | obj->programs = progs; |
| 320 | obj->nr_programs = nr_progs + 1; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 321 | prog.obj = obj; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 322 | progs[nr_progs] = prog; |
| 323 | return 0; |
| 324 | } |
| 325 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 326 | static struct bpf_object *bpf_object__new(const char *path, |
| 327 | void *obj_buf, |
| 328 | size_t obj_buf_sz) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 329 | { |
| 330 | struct bpf_object *obj; |
| 331 | |
| 332 | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
| 333 | if (!obj) { |
| 334 | pr_warning("alloc memory failed for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 335 | return ERR_PTR(-ENOMEM); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | strcpy(obj->path, path); |
| 339 | obj->efile.fd = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Caller of this function should also calls |
| 343 | * bpf_object__elf_finish() after data collection to return |
| 344 | * obj_buf to user. If not, we should duplicate the buffer to |
| 345 | * avoid user freeing them before elf finish. |
| 346 | */ |
| 347 | obj->efile.obj_buf = obj_buf; |
| 348 | obj->efile.obj_buf_sz = obj_buf_sz; |
| 349 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 350 | obj->loaded = false; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 351 | |
| 352 | INIT_LIST_HEAD(&obj->list); |
| 353 | list_add(&obj->list, &bpf_objects_list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 354 | return obj; |
| 355 | } |
| 356 | |
| 357 | static void bpf_object__elf_finish(struct bpf_object *obj) |
| 358 | { |
| 359 | if (!obj_elf_valid(obj)) |
| 360 | return; |
| 361 | |
| 362 | if (obj->efile.elf) { |
| 363 | elf_end(obj->efile.elf); |
| 364 | obj->efile.elf = NULL; |
| 365 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 366 | obj->efile.symbols = NULL; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 367 | |
| 368 | zfree(&obj->efile.reloc); |
| 369 | obj->efile.nr_reloc = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 370 | zclose(obj->efile.fd); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 371 | obj->efile.obj_buf = NULL; |
| 372 | obj->efile.obj_buf_sz = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int bpf_object__elf_init(struct bpf_object *obj) |
| 376 | { |
| 377 | int err = 0; |
| 378 | GElf_Ehdr *ep; |
| 379 | |
| 380 | if (obj_elf_valid(obj)) { |
| 381 | pr_warning("elf init: internal error\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 382 | return -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 385 | if (obj->efile.obj_buf_sz > 0) { |
| 386 | /* |
| 387 | * obj_buf should have been validated by |
| 388 | * bpf_object__open_buffer(). |
| 389 | */ |
| 390 | obj->efile.elf = elf_memory(obj->efile.obj_buf, |
| 391 | obj->efile.obj_buf_sz); |
| 392 | } else { |
| 393 | obj->efile.fd = open(obj->path, O_RDONLY); |
| 394 | if (obj->efile.fd < 0) { |
| 395 | pr_warning("failed to open %s: %s\n", obj->path, |
| 396 | strerror(errno)); |
| 397 | return -errno; |
| 398 | } |
| 399 | |
| 400 | obj->efile.elf = elf_begin(obj->efile.fd, |
| 401 | LIBBPF_ELF_C_READ_MMAP, |
| 402 | NULL); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 405 | if (!obj->efile.elf) { |
| 406 | pr_warning("failed to open %s as ELF file\n", |
| 407 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 408 | err = -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 409 | goto errout; |
| 410 | } |
| 411 | |
| 412 | if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { |
| 413 | pr_warning("failed to get EHDR from %s\n", |
| 414 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 415 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 416 | goto errout; |
| 417 | } |
| 418 | ep = &obj->efile.ehdr; |
| 419 | |
| 420 | if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) { |
| 421 | pr_warning("%s is not an eBPF object file\n", |
| 422 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 423 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 424 | goto errout; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | errout: |
| 429 | bpf_object__elf_finish(obj); |
| 430 | return err; |
| 431 | } |
| 432 | |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 433 | static int |
| 434 | bpf_object__check_endianness(struct bpf_object *obj) |
| 435 | { |
| 436 | static unsigned int const endian = 1; |
| 437 | |
| 438 | switch (obj->efile.ehdr.e_ident[EI_DATA]) { |
| 439 | case ELFDATA2LSB: |
| 440 | /* We are big endian, BPF obj is little endian. */ |
| 441 | if (*(unsigned char const *)&endian != 1) |
| 442 | goto mismatch; |
| 443 | break; |
| 444 | |
| 445 | case ELFDATA2MSB: |
| 446 | /* We are little endian, BPF obj is big endian. */ |
| 447 | if (*(unsigned char const *)&endian != 0) |
| 448 | goto mismatch; |
| 449 | break; |
| 450 | default: |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 451 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | |
| 456 | mismatch: |
| 457 | pr_warning("Error: endianness mismatch.\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 458 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 461 | static int |
| 462 | bpf_object__init_license(struct bpf_object *obj, |
| 463 | void *data, size_t size) |
| 464 | { |
| 465 | memcpy(obj->license, data, |
| 466 | min(size, sizeof(obj->license) - 1)); |
| 467 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int |
| 472 | bpf_object__init_kversion(struct bpf_object *obj, |
| 473 | void *data, size_t size) |
| 474 | { |
| 475 | u32 kver; |
| 476 | |
| 477 | if (size != sizeof(kver)) { |
| 478 | pr_warning("invalid kver section in %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 479 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 480 | } |
| 481 | memcpy(&kver, data, sizeof(kver)); |
| 482 | obj->kern_version = kver; |
| 483 | pr_debug("kernel version of %s is %x\n", obj->path, |
| 484 | obj->kern_version); |
| 485 | return 0; |
| 486 | } |
| 487 | |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 488 | static int |
| 489 | bpf_object__init_maps(struct bpf_object *obj, void *data, |
| 490 | size_t size) |
| 491 | { |
| 492 | if (size == 0) { |
| 493 | pr_debug("%s doesn't need map definition\n", |
| 494 | obj->path); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | obj->maps_buf = malloc(size); |
| 499 | if (!obj->maps_buf) { |
| 500 | pr_warning("malloc maps failed: %s\n", obj->path); |
| 501 | return -ENOMEM; |
| 502 | } |
| 503 | |
| 504 | obj->maps_buf_sz = size; |
| 505 | memcpy(obj->maps_buf, data, size); |
| 506 | pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size); |
| 507 | return 0; |
| 508 | } |
| 509 | |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 510 | static int bpf_object__elf_collect(struct bpf_object *obj) |
| 511 | { |
| 512 | Elf *elf = obj->efile.elf; |
| 513 | GElf_Ehdr *ep = &obj->efile.ehdr; |
| 514 | Elf_Scn *scn = NULL; |
| 515 | int idx = 0, err = 0; |
| 516 | |
| 517 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 518 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { |
| 519 | pr_warning("failed to get e_shstrndx from %s\n", |
| 520 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 521 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 525 | char *name; |
| 526 | GElf_Shdr sh; |
| 527 | Elf_Data *data; |
| 528 | |
| 529 | idx++; |
| 530 | if (gelf_getshdr(scn, &sh) != &sh) { |
| 531 | pr_warning("failed to get section header from %s\n", |
| 532 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 533 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 538 | if (!name) { |
| 539 | pr_warning("failed to get section name from %s\n", |
| 540 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 541 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 542 | goto out; |
| 543 | } |
| 544 | |
| 545 | data = elf_getdata(scn, 0); |
| 546 | if (!data) { |
| 547 | pr_warning("failed to get section data from %s(%s)\n", |
| 548 | name, obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 549 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 550 | goto out; |
| 551 | } |
| 552 | pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n", |
| 553 | name, (unsigned long)data->d_size, |
| 554 | (int)sh.sh_link, (unsigned long)sh.sh_flags, |
| 555 | (int)sh.sh_type); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 556 | |
| 557 | if (strcmp(name, "license") == 0) |
| 558 | err = bpf_object__init_license(obj, |
| 559 | data->d_buf, |
| 560 | data->d_size); |
| 561 | else if (strcmp(name, "version") == 0) |
| 562 | err = bpf_object__init_kversion(obj, |
| 563 | data->d_buf, |
| 564 | data->d_size); |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 565 | else if (strcmp(name, "maps") == 0) |
| 566 | err = bpf_object__init_maps(obj, data->d_buf, |
| 567 | data->d_size); |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 568 | else if (sh.sh_type == SHT_SYMTAB) { |
| 569 | if (obj->efile.symbols) { |
| 570 | pr_warning("bpf: multiple SYMTAB in %s\n", |
| 571 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 572 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 573 | } else |
| 574 | obj->efile.symbols = data; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 575 | } else if ((sh.sh_type == SHT_PROGBITS) && |
| 576 | (sh.sh_flags & SHF_EXECINSTR) && |
| 577 | (data->d_size > 0)) { |
| 578 | err = bpf_object__add_program(obj, data->d_buf, |
| 579 | data->d_size, name, idx); |
| 580 | if (err) { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 581 | char errmsg[STRERR_BUFSIZE]; |
| 582 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 583 | strerror_r(-err, errmsg, sizeof(errmsg)); |
| 584 | pr_warning("failed to alloc program %s (%s): %s", |
| 585 | name, obj->path, errmsg); |
| 586 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 587 | } else if (sh.sh_type == SHT_REL) { |
| 588 | void *reloc = obj->efile.reloc; |
| 589 | int nr_reloc = obj->efile.nr_reloc + 1; |
| 590 | |
| 591 | reloc = realloc(reloc, |
| 592 | sizeof(*obj->efile.reloc) * nr_reloc); |
| 593 | if (!reloc) { |
| 594 | pr_warning("realloc failed\n"); |
| 595 | err = -ENOMEM; |
| 596 | } else { |
| 597 | int n = nr_reloc - 1; |
| 598 | |
| 599 | obj->efile.reloc = reloc; |
| 600 | obj->efile.nr_reloc = nr_reloc; |
| 601 | |
| 602 | obj->efile.reloc[n].shdr = sh; |
| 603 | obj->efile.reloc[n].data = data; |
| 604 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 605 | } |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 606 | if (err) |
| 607 | goto out; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 608 | } |
| 609 | out: |
| 610 | return err; |
| 611 | } |
| 612 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 613 | static struct bpf_program * |
| 614 | bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) |
| 615 | { |
| 616 | struct bpf_program *prog; |
| 617 | size_t i; |
| 618 | |
| 619 | for (i = 0; i < obj->nr_programs; i++) { |
| 620 | prog = &obj->programs[i]; |
| 621 | if (prog->idx == idx) |
| 622 | return prog; |
| 623 | } |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | static int |
| 628 | bpf_program__collect_reloc(struct bpf_program *prog, |
| 629 | size_t nr_maps, GElf_Shdr *shdr, |
| 630 | Elf_Data *data, Elf_Data *symbols) |
| 631 | { |
| 632 | int i, nrels; |
| 633 | |
| 634 | pr_debug("collecting relocating info for: '%s'\n", |
| 635 | prog->section_name); |
| 636 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 637 | |
| 638 | prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); |
| 639 | if (!prog->reloc_desc) { |
| 640 | pr_warning("failed to alloc memory in relocation\n"); |
| 641 | return -ENOMEM; |
| 642 | } |
| 643 | prog->nr_reloc = nrels; |
| 644 | |
| 645 | for (i = 0; i < nrels; i++) { |
| 646 | GElf_Sym sym; |
| 647 | GElf_Rel rel; |
| 648 | unsigned int insn_idx; |
| 649 | struct bpf_insn *insns = prog->insns; |
| 650 | size_t map_idx; |
| 651 | |
| 652 | if (!gelf_getrel(data, i, &rel)) { |
| 653 | pr_warning("relocation: failed to get %d reloc\n", i); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 654 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 658 | pr_debug("relocation: insn_idx=%u\n", insn_idx); |
| 659 | |
| 660 | if (!gelf_getsym(symbols, |
| 661 | GELF_R_SYM(rel.r_info), |
| 662 | &sym)) { |
| 663 | pr_warning("relocation: symbol %"PRIx64" not found\n", |
| 664 | GELF_R_SYM(rel.r_info)); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 665 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 669 | pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", |
| 670 | insn_idx, insns[insn_idx].code); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 671 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | map_idx = sym.st_value / sizeof(struct bpf_map_def); |
| 675 | if (map_idx >= nr_maps) { |
| 676 | pr_warning("bpf relocation: map_idx %d large than %d\n", |
| 677 | (int)map_idx, (int)nr_maps - 1); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 678 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | prog->reloc_desc[i].insn_idx = insn_idx; |
| 682 | prog->reloc_desc[i].map_idx = map_idx; |
| 683 | } |
| 684 | return 0; |
| 685 | } |
| 686 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 687 | static int |
| 688 | bpf_object__create_maps(struct bpf_object *obj) |
| 689 | { |
| 690 | unsigned int i; |
| 691 | size_t nr_maps; |
| 692 | int *pfd; |
| 693 | |
| 694 | nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def); |
| 695 | if (!obj->maps_buf || !nr_maps) { |
| 696 | pr_debug("don't need create maps for %s\n", |
| 697 | obj->path); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | obj->map_fds = malloc(sizeof(int) * nr_maps); |
| 702 | if (!obj->map_fds) { |
| 703 | pr_warning("realloc perf_bpf_map_fds failed\n"); |
| 704 | return -ENOMEM; |
| 705 | } |
| 706 | obj->nr_map_fds = nr_maps; |
| 707 | |
| 708 | /* fill all fd with -1 */ |
| 709 | memset(obj->map_fds, -1, sizeof(int) * nr_maps); |
| 710 | |
| 711 | pfd = obj->map_fds; |
| 712 | for (i = 0; i < nr_maps; i++) { |
| 713 | struct bpf_map_def def; |
| 714 | |
| 715 | def = *(struct bpf_map_def *)(obj->maps_buf + |
| 716 | i * sizeof(struct bpf_map_def)); |
| 717 | |
| 718 | *pfd = bpf_create_map(def.type, |
| 719 | def.key_size, |
| 720 | def.value_size, |
| 721 | def.max_entries); |
| 722 | if (*pfd < 0) { |
| 723 | size_t j; |
| 724 | int err = *pfd; |
| 725 | |
| 726 | pr_warning("failed to create map: %s\n", |
| 727 | strerror(errno)); |
| 728 | for (j = 0; j < i; j++) |
| 729 | zclose(obj->map_fds[j]); |
| 730 | obj->nr_map_fds = 0; |
| 731 | zfree(&obj->map_fds); |
| 732 | return err; |
| 733 | } |
| 734 | pr_debug("create map: fd=%d\n", *pfd); |
| 735 | pfd++; |
| 736 | } |
| 737 | |
| 738 | zfree(&obj->maps_buf); |
| 739 | obj->maps_buf_sz = 0; |
| 740 | return 0; |
| 741 | } |
| 742 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 743 | static int |
| 744 | bpf_program__relocate(struct bpf_program *prog, int *map_fds) |
| 745 | { |
| 746 | int i; |
| 747 | |
| 748 | if (!prog || !prog->reloc_desc) |
| 749 | return 0; |
| 750 | |
| 751 | for (i = 0; i < prog->nr_reloc; i++) { |
| 752 | int insn_idx, map_idx; |
| 753 | struct bpf_insn *insns = prog->insns; |
| 754 | |
| 755 | insn_idx = prog->reloc_desc[i].insn_idx; |
| 756 | map_idx = prog->reloc_desc[i].map_idx; |
| 757 | |
| 758 | if (insn_idx >= (int)prog->insns_cnt) { |
| 759 | pr_warning("relocation out of range: '%s'\n", |
| 760 | prog->section_name); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 761 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 762 | } |
| 763 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
| 764 | insns[insn_idx].imm = map_fds[map_idx]; |
| 765 | } |
| 766 | |
| 767 | zfree(&prog->reloc_desc); |
| 768 | prog->nr_reloc = 0; |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | static int |
| 774 | bpf_object__relocate(struct bpf_object *obj) |
| 775 | { |
| 776 | struct bpf_program *prog; |
| 777 | size_t i; |
| 778 | int err; |
| 779 | |
| 780 | for (i = 0; i < obj->nr_programs; i++) { |
| 781 | prog = &obj->programs[i]; |
| 782 | |
| 783 | err = bpf_program__relocate(prog, obj->map_fds); |
| 784 | if (err) { |
| 785 | pr_warning("failed to relocate '%s'\n", |
| 786 | prog->section_name); |
| 787 | return err; |
| 788 | } |
| 789 | } |
| 790 | return 0; |
| 791 | } |
| 792 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 793 | static int bpf_object__collect_reloc(struct bpf_object *obj) |
| 794 | { |
| 795 | int i, err; |
| 796 | |
| 797 | if (!obj_elf_valid(obj)) { |
| 798 | pr_warning("Internal error: elf object is closed\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 799 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | for (i = 0; i < obj->efile.nr_reloc; i++) { |
| 803 | GElf_Shdr *shdr = &obj->efile.reloc[i].shdr; |
| 804 | Elf_Data *data = obj->efile.reloc[i].data; |
| 805 | int idx = shdr->sh_info; |
| 806 | struct bpf_program *prog; |
| 807 | size_t nr_maps = obj->maps_buf_sz / |
| 808 | sizeof(struct bpf_map_def); |
| 809 | |
| 810 | if (shdr->sh_type != SHT_REL) { |
| 811 | pr_warning("internal error at %d\n", __LINE__); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 812 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | prog = bpf_object__find_prog_by_idx(obj, idx); |
| 816 | if (!prog) { |
| 817 | pr_warning("relocation failed: no %d section\n", |
| 818 | idx); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 819 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | err = bpf_program__collect_reloc(prog, nr_maps, |
| 823 | shdr, data, |
| 824 | obj->efile.symbols); |
| 825 | if (err) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 826 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 827 | } |
| 828 | return 0; |
| 829 | } |
| 830 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 831 | static int |
| 832 | load_program(struct bpf_insn *insns, int insns_cnt, |
| 833 | char *license, u32 kern_version, int *pfd) |
| 834 | { |
| 835 | int ret; |
| 836 | char *log_buf; |
| 837 | |
| 838 | if (!insns || !insns_cnt) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | log_buf = malloc(BPF_LOG_BUF_SIZE); |
| 842 | if (!log_buf) |
| 843 | pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); |
| 844 | |
| 845 | ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns, |
| 846 | insns_cnt, license, kern_version, |
| 847 | log_buf, BPF_LOG_BUF_SIZE); |
| 848 | |
| 849 | if (ret >= 0) { |
| 850 | *pfd = ret; |
| 851 | ret = 0; |
| 852 | goto out; |
| 853 | } |
| 854 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 855 | ret = -LIBBPF_ERRNO__LOAD; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 856 | pr_warning("load bpf program failed: %s\n", strerror(errno)); |
| 857 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 858 | if (log_buf && log_buf[0] != '\0') { |
| 859 | ret = -LIBBPF_ERRNO__VERIFY; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 860 | pr_warning("-- BEGIN DUMP LOG ---\n"); |
| 861 | pr_warning("\n%s\n", log_buf); |
| 862 | pr_warning("-- END LOG --\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 863 | } else { |
| 864 | if (insns_cnt >= BPF_MAXINSNS) { |
| 865 | pr_warning("Program too large (%d insns), at most %d insns\n", |
| 866 | insns_cnt, BPF_MAXINSNS); |
| 867 | ret = -LIBBPF_ERRNO__PROG2BIG; |
| 868 | } else if (log_buf) { |
| 869 | pr_warning("log buffer is empty\n"); |
| 870 | ret = -LIBBPF_ERRNO__KVER; |
| 871 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | out: |
| 875 | free(log_buf); |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | static int |
| 880 | bpf_program__load(struct bpf_program *prog, |
| 881 | char *license, u32 kern_version) |
| 882 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 883 | int err = 0, fd, i; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 884 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 885 | if (prog->instances.nr < 0 || !prog->instances.fds) { |
| 886 | if (prog->preprocessor) { |
| 887 | pr_warning("Internal error: can't load program '%s'\n", |
| 888 | prog->section_name); |
| 889 | return -LIBBPF_ERRNO__INTERNAL; |
| 890 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 891 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 892 | prog->instances.fds = malloc(sizeof(int)); |
| 893 | if (!prog->instances.fds) { |
| 894 | pr_warning("Not enough memory for BPF fds\n"); |
| 895 | return -ENOMEM; |
| 896 | } |
| 897 | prog->instances.nr = 1; |
| 898 | prog->instances.fds[0] = -1; |
| 899 | } |
| 900 | |
| 901 | if (!prog->preprocessor) { |
| 902 | if (prog->instances.nr != 1) { |
| 903 | pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", |
| 904 | prog->section_name, prog->instances.nr); |
| 905 | } |
| 906 | err = load_program(prog->insns, prog->insns_cnt, |
| 907 | license, kern_version, &fd); |
| 908 | if (!err) |
| 909 | prog->instances.fds[0] = fd; |
| 910 | goto out; |
| 911 | } |
| 912 | |
| 913 | for (i = 0; i < prog->instances.nr; i++) { |
| 914 | struct bpf_prog_prep_result result; |
| 915 | bpf_program_prep_t preprocessor = prog->preprocessor; |
| 916 | |
| 917 | bzero(&result, sizeof(result)); |
| 918 | err = preprocessor(prog, i, prog->insns, |
| 919 | prog->insns_cnt, &result); |
| 920 | if (err) { |
| 921 | pr_warning("Preprocessing the %dth instance of program '%s' failed\n", |
| 922 | i, prog->section_name); |
| 923 | goto out; |
| 924 | } |
| 925 | |
| 926 | if (!result.new_insn_ptr || !result.new_insn_cnt) { |
| 927 | pr_debug("Skip loading the %dth instance of program '%s'\n", |
| 928 | i, prog->section_name); |
| 929 | prog->instances.fds[i] = -1; |
| 930 | if (result.pfd) |
| 931 | *result.pfd = -1; |
| 932 | continue; |
| 933 | } |
| 934 | |
| 935 | err = load_program(result.new_insn_ptr, |
| 936 | result.new_insn_cnt, |
| 937 | license, kern_version, &fd); |
| 938 | |
| 939 | if (err) { |
| 940 | pr_warning("Loading the %dth instance of program '%s' failed\n", |
| 941 | i, prog->section_name); |
| 942 | goto out; |
| 943 | } |
| 944 | |
| 945 | if (result.pfd) |
| 946 | *result.pfd = fd; |
| 947 | prog->instances.fds[i] = fd; |
| 948 | } |
| 949 | out: |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 950 | if (err) |
| 951 | pr_warning("failed to load program '%s'\n", |
| 952 | prog->section_name); |
| 953 | zfree(&prog->insns); |
| 954 | prog->insns_cnt = 0; |
| 955 | return err; |
| 956 | } |
| 957 | |
| 958 | static int |
| 959 | bpf_object__load_progs(struct bpf_object *obj) |
| 960 | { |
| 961 | size_t i; |
| 962 | int err; |
| 963 | |
| 964 | for (i = 0; i < obj->nr_programs; i++) { |
| 965 | err = bpf_program__load(&obj->programs[i], |
| 966 | obj->license, |
| 967 | obj->kern_version); |
| 968 | if (err) |
| 969 | return err; |
| 970 | } |
| 971 | return 0; |
| 972 | } |
| 973 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 974 | static int bpf_object__validate(struct bpf_object *obj) |
| 975 | { |
| 976 | if (obj->kern_version == 0) { |
| 977 | pr_warning("%s doesn't provide kernel version\n", |
| 978 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 979 | return -LIBBPF_ERRNO__KVERSION; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 984 | static struct bpf_object * |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 985 | __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 986 | { |
| 987 | struct bpf_object *obj; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 988 | int err; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 989 | |
| 990 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 991 | pr_warning("failed to init libelf for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 992 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 995 | obj = bpf_object__new(path, obj_buf, obj_buf_sz); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 996 | if (IS_ERR(obj)) |
| 997 | return obj; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 998 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 999 | CHECK_ERR(bpf_object__elf_init(obj), err, out); |
| 1000 | CHECK_ERR(bpf_object__check_endianness(obj), err, out); |
| 1001 | CHECK_ERR(bpf_object__elf_collect(obj), err, out); |
| 1002 | CHECK_ERR(bpf_object__collect_reloc(obj), err, out); |
| 1003 | CHECK_ERR(bpf_object__validate(obj), err, out); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1004 | |
| 1005 | bpf_object__elf_finish(obj); |
| 1006 | return obj; |
| 1007 | out: |
| 1008 | bpf_object__close(obj); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1009 | return ERR_PTR(err); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | struct bpf_object *bpf_object__open(const char *path) |
| 1013 | { |
| 1014 | /* param validation */ |
| 1015 | if (!path) |
| 1016 | return NULL; |
| 1017 | |
| 1018 | pr_debug("loading %s\n", path); |
| 1019 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1020 | return __bpf_object__open(path, NULL, 0); |
| 1021 | } |
| 1022 | |
| 1023 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1024 | size_t obj_buf_sz, |
| 1025 | const char *name) |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1026 | { |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1027 | char tmp_name[64]; |
| 1028 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1029 | /* param validation */ |
| 1030 | if (!obj_buf || obj_buf_sz <= 0) |
| 1031 | return NULL; |
| 1032 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1033 | if (!name) { |
| 1034 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
| 1035 | (unsigned long)obj_buf, |
| 1036 | (unsigned long)obj_buf_sz); |
| 1037 | tmp_name[sizeof(tmp_name) - 1] = '\0'; |
| 1038 | name = tmp_name; |
| 1039 | } |
| 1040 | pr_debug("loading object '%s' from buffer\n", |
| 1041 | name); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1042 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1043 | return __bpf_object__open(name, obj_buf, obj_buf_sz); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1046 | int bpf_object__unload(struct bpf_object *obj) |
| 1047 | { |
| 1048 | size_t i; |
| 1049 | |
| 1050 | if (!obj) |
| 1051 | return -EINVAL; |
| 1052 | |
| 1053 | for (i = 0; i < obj->nr_map_fds; i++) |
| 1054 | zclose(obj->map_fds[i]); |
| 1055 | zfree(&obj->map_fds); |
| 1056 | obj->nr_map_fds = 0; |
| 1057 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1058 | for (i = 0; i < obj->nr_programs; i++) |
| 1059 | bpf_program__unload(&obj->programs[i]); |
| 1060 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | int bpf_object__load(struct bpf_object *obj) |
| 1065 | { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1066 | int err; |
| 1067 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1068 | if (!obj) |
| 1069 | return -EINVAL; |
| 1070 | |
| 1071 | if (obj->loaded) { |
| 1072 | pr_warning("object should not be loaded twice\n"); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
| 1076 | obj->loaded = true; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1077 | |
| 1078 | CHECK_ERR(bpf_object__create_maps(obj), err, out); |
| 1079 | CHECK_ERR(bpf_object__relocate(obj), err, out); |
| 1080 | CHECK_ERR(bpf_object__load_progs(obj), err, out); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1081 | |
| 1082 | return 0; |
| 1083 | out: |
| 1084 | bpf_object__unload(obj); |
| 1085 | pr_warning("failed to load object '%s'\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1086 | return err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1089 | void bpf_object__close(struct bpf_object *obj) |
| 1090 | { |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1091 | size_t i; |
| 1092 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1093 | if (!obj) |
| 1094 | return; |
| 1095 | |
| 1096 | bpf_object__elf_finish(obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1097 | bpf_object__unload(obj); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1098 | |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 1099 | zfree(&obj->maps_buf); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (obj->programs && obj->nr_programs) { |
| 1102 | for (i = 0; i < obj->nr_programs; i++) |
| 1103 | bpf_program__exit(&obj->programs[i]); |
| 1104 | } |
| 1105 | zfree(&obj->programs); |
| 1106 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1107 | list_del(&obj->list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1108 | free(obj); |
| 1109 | } |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1110 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1111 | struct bpf_object * |
| 1112 | bpf_object__next(struct bpf_object *prev) |
| 1113 | { |
| 1114 | struct bpf_object *next; |
| 1115 | |
| 1116 | if (!prev) |
| 1117 | next = list_first_entry(&bpf_objects_list, |
| 1118 | struct bpf_object, |
| 1119 | list); |
| 1120 | else |
| 1121 | next = list_next_entry(prev, list); |
| 1122 | |
| 1123 | /* Empty list is noticed here so don't need checking on entry. */ |
| 1124 | if (&next->list == &bpf_objects_list) |
| 1125 | return NULL; |
| 1126 | |
| 1127 | return next; |
| 1128 | } |
| 1129 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1130 | const char * |
| 1131 | bpf_object__get_name(struct bpf_object *obj) |
| 1132 | { |
| 1133 | if (!obj) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1134 | return ERR_PTR(-EINVAL); |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1135 | return obj->path; |
| 1136 | } |
| 1137 | |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 1138 | unsigned int |
| 1139 | bpf_object__get_kversion(struct bpf_object *obj) |
| 1140 | { |
| 1141 | if (!obj) |
| 1142 | return 0; |
| 1143 | return obj->kern_version; |
| 1144 | } |
| 1145 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1146 | struct bpf_program * |
| 1147 | bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) |
| 1148 | { |
| 1149 | size_t idx; |
| 1150 | |
| 1151 | if (!obj->programs) |
| 1152 | return NULL; |
| 1153 | /* First handler */ |
| 1154 | if (prev == NULL) |
| 1155 | return &obj->programs[0]; |
| 1156 | |
| 1157 | if (prev->obj != obj) { |
| 1158 | pr_warning("error: program handler doesn't match object\n"); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | idx = (prev - obj->programs) + 1; |
| 1163 | if (idx >= obj->nr_programs) |
| 1164 | return NULL; |
| 1165 | return &obj->programs[idx]; |
| 1166 | } |
| 1167 | |
| 1168 | int bpf_program__set_private(struct bpf_program *prog, |
| 1169 | void *priv, |
| 1170 | bpf_program_clear_priv_t clear_priv) |
| 1171 | { |
| 1172 | if (prog->priv && prog->clear_priv) |
| 1173 | prog->clear_priv(prog, prog->priv); |
| 1174 | |
| 1175 | prog->priv = priv; |
| 1176 | prog->clear_priv = clear_priv; |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | int bpf_program__get_private(struct bpf_program *prog, void **ppriv) |
| 1181 | { |
| 1182 | *ppriv = prog->priv; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1186 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1187 | { |
| 1188 | const char *title; |
| 1189 | |
| 1190 | title = prog->section_name; |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1191 | if (needs_copy) { |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1192 | title = strdup(title); |
| 1193 | if (!title) { |
| 1194 | pr_warning("failed to strdup program title\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1195 | return ERR_PTR(-ENOMEM); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | return title; |
| 1200 | } |
| 1201 | |
| 1202 | int bpf_program__fd(struct bpf_program *prog) |
| 1203 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame^] | 1204 | return bpf_program__nth_fd(prog, 0); |
| 1205 | } |
| 1206 | |
| 1207 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, |
| 1208 | bpf_program_prep_t prep) |
| 1209 | { |
| 1210 | int *instances_fds; |
| 1211 | |
| 1212 | if (nr_instances <= 0 || !prep) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | if (prog->instances.nr > 0 || prog->instances.fds) { |
| 1216 | pr_warning("Can't set pre-processor after loading\n"); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | |
| 1220 | instances_fds = malloc(sizeof(int) * nr_instances); |
| 1221 | if (!instances_fds) { |
| 1222 | pr_warning("alloc memory failed for fds\n"); |
| 1223 | return -ENOMEM; |
| 1224 | } |
| 1225 | |
| 1226 | /* fill all fd with -1 */ |
| 1227 | memset(instances_fds, -1, sizeof(int) * nr_instances); |
| 1228 | |
| 1229 | prog->instances.nr = nr_instances; |
| 1230 | prog->instances.fds = instances_fds; |
| 1231 | prog->preprocessor = prep; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | int bpf_program__nth_fd(struct bpf_program *prog, int n) |
| 1236 | { |
| 1237 | int fd; |
| 1238 | |
| 1239 | if (n >= prog->instances.nr || n < 0) { |
| 1240 | pr_warning("Can't get the %dth fd from program %s: only %d instances\n", |
| 1241 | n, prog->section_name, prog->instances.nr); |
| 1242 | return -EINVAL; |
| 1243 | } |
| 1244 | |
| 1245 | fd = prog->instances.fds[n]; |
| 1246 | if (fd < 0) { |
| 1247 | pr_warning("%dth instance of program '%s' is invalid\n", |
| 1248 | n, prog->section_name); |
| 1249 | return -ENOENT; |
| 1250 | } |
| 1251 | |
| 1252 | return fd; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1253 | } |