libbpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <inttypes.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <asm/unistd.h>
  17. #include <linux/kernel.h>
  18. #include <linux/bpf.h>
  19. #include <libelf.h>
  20. #include <gelf.h>
  21. #include "libbpf.h"
  22. #include "bpf.h"
  23. #define __printf(a, b) __attribute__((format(printf, a, b)))
  24. __printf(1, 2)
  25. static int __base_pr(const char *format, ...)
  26. {
  27. va_list args;
  28. int err;
  29. va_start(args, format);
  30. err = vfprintf(stderr, format, args);
  31. va_end(args);
  32. return err;
  33. }
  34. static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  35. static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  36. static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  37. #define __pr(func, fmt, ...) \
  38. do { \
  39. if ((func)) \
  40. (func)("libbpf: " fmt, ##__VA_ARGS__); \
  41. } while (0)
  42. #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
  43. #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
  44. #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
  45. void libbpf_set_print(libbpf_print_fn_t warn,
  46. libbpf_print_fn_t info,
  47. libbpf_print_fn_t debug)
  48. {
  49. __pr_warning = warn;
  50. __pr_info = info;
  51. __pr_debug = debug;
  52. }
  53. /* Copied from tools/perf/util/util.h */
  54. #ifndef zfree
  55. # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  56. #endif
  57. #ifndef zclose
  58. # define zclose(fd) ({ \
  59. int ___err = 0; \
  60. if ((fd) >= 0) \
  61. ___err = close((fd)); \
  62. fd = -1; \
  63. ___err; })
  64. #endif
  65. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  66. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  67. #else
  68. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
  69. #endif
  70. /*
  71. * bpf_prog should be a better name but it has been used in
  72. * linux/filter.h.
  73. */
  74. struct bpf_program {
  75. /* Index in elf obj file, for relocation use. */
  76. int idx;
  77. char *section_name;
  78. struct bpf_insn *insns;
  79. size_t insns_cnt;
  80. struct {
  81. int insn_idx;
  82. int map_idx;
  83. } *reloc_desc;
  84. int nr_reloc;
  85. };
  86. struct bpf_object {
  87. char license[64];
  88. u32 kern_version;
  89. void *maps_buf;
  90. size_t maps_buf_sz;
  91. struct bpf_program *programs;
  92. size_t nr_programs;
  93. int *map_fds;
  94. /*
  95. * This field is required because maps_buf will be freed and
  96. * maps_buf_sz will be set to 0 after loaded.
  97. */
  98. size_t nr_map_fds;
  99. bool loaded;
  100. /*
  101. * Information when doing elf related work. Only valid if fd
  102. * is valid.
  103. */
  104. struct {
  105. int fd;
  106. void *obj_buf;
  107. size_t obj_buf_sz;
  108. Elf *elf;
  109. GElf_Ehdr ehdr;
  110. Elf_Data *symbols;
  111. struct {
  112. GElf_Shdr shdr;
  113. Elf_Data *data;
  114. } *reloc;
  115. int nr_reloc;
  116. } efile;
  117. char path[];
  118. };
  119. #define obj_elf_valid(o) ((o)->efile.elf)
  120. static void bpf_program__exit(struct bpf_program *prog)
  121. {
  122. if (!prog)
  123. return;
  124. zfree(&prog->section_name);
  125. zfree(&prog->insns);
  126. zfree(&prog->reloc_desc);
  127. prog->nr_reloc = 0;
  128. prog->insns_cnt = 0;
  129. prog->idx = -1;
  130. }
  131. static int
  132. bpf_program__init(void *data, size_t size, char *name, int idx,
  133. struct bpf_program *prog)
  134. {
  135. if (size < sizeof(struct bpf_insn)) {
  136. pr_warning("corrupted section '%s'\n", name);
  137. return -EINVAL;
  138. }
  139. bzero(prog, sizeof(*prog));
  140. prog->section_name = strdup(name);
  141. if (!prog->section_name) {
  142. pr_warning("failed to alloc name for prog %s\n",
  143. name);
  144. goto errout;
  145. }
  146. prog->insns = malloc(size);
  147. if (!prog->insns) {
  148. pr_warning("failed to alloc insns for %s\n", name);
  149. goto errout;
  150. }
  151. prog->insns_cnt = size / sizeof(struct bpf_insn);
  152. memcpy(prog->insns, data,
  153. prog->insns_cnt * sizeof(struct bpf_insn));
  154. prog->idx = idx;
  155. return 0;
  156. errout:
  157. bpf_program__exit(prog);
  158. return -ENOMEM;
  159. }
  160. static int
  161. bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
  162. char *name, int idx)
  163. {
  164. struct bpf_program prog, *progs;
  165. int nr_progs, err;
  166. err = bpf_program__init(data, size, name, idx, &prog);
  167. if (err)
  168. return err;
  169. progs = obj->programs;
  170. nr_progs = obj->nr_programs;
  171. progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
  172. if (!progs) {
  173. /*
  174. * In this case the original obj->programs
  175. * is still valid, so don't need special treat for
  176. * bpf_close_object().
  177. */
  178. pr_warning("failed to alloc a new program '%s'\n",
  179. name);
  180. bpf_program__exit(&prog);
  181. return -ENOMEM;
  182. }
  183. pr_debug("found program %s\n", prog.section_name);
  184. obj->programs = progs;
  185. obj->nr_programs = nr_progs + 1;
  186. progs[nr_progs] = prog;
  187. return 0;
  188. }
  189. static struct bpf_object *bpf_object__new(const char *path,
  190. void *obj_buf,
  191. size_t obj_buf_sz)
  192. {
  193. struct bpf_object *obj;
  194. obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
  195. if (!obj) {
  196. pr_warning("alloc memory failed for %s\n", path);
  197. return NULL;
  198. }
  199. strcpy(obj->path, path);
  200. obj->efile.fd = -1;
  201. /*
  202. * Caller of this function should also calls
  203. * bpf_object__elf_finish() after data collection to return
  204. * obj_buf to user. If not, we should duplicate the buffer to
  205. * avoid user freeing them before elf finish.
  206. */
  207. obj->efile.obj_buf = obj_buf;
  208. obj->efile.obj_buf_sz = obj_buf_sz;
  209. obj->loaded = false;
  210. return obj;
  211. }
  212. static void bpf_object__elf_finish(struct bpf_object *obj)
  213. {
  214. if (!obj_elf_valid(obj))
  215. return;
  216. if (obj->efile.elf) {
  217. elf_end(obj->efile.elf);
  218. obj->efile.elf = NULL;
  219. }
  220. obj->efile.symbols = NULL;
  221. zfree(&obj->efile.reloc);
  222. obj->efile.nr_reloc = 0;
  223. zclose(obj->efile.fd);
  224. obj->efile.obj_buf = NULL;
  225. obj->efile.obj_buf_sz = 0;
  226. }
  227. static int bpf_object__elf_init(struct bpf_object *obj)
  228. {
  229. int err = 0;
  230. GElf_Ehdr *ep;
  231. if (obj_elf_valid(obj)) {
  232. pr_warning("elf init: internal error\n");
  233. return -EEXIST;
  234. }
  235. if (obj->efile.obj_buf_sz > 0) {
  236. /*
  237. * obj_buf should have been validated by
  238. * bpf_object__open_buffer().
  239. */
  240. obj->efile.elf = elf_memory(obj->efile.obj_buf,
  241. obj->efile.obj_buf_sz);
  242. } else {
  243. obj->efile.fd = open(obj->path, O_RDONLY);
  244. if (obj->efile.fd < 0) {
  245. pr_warning("failed to open %s: %s\n", obj->path,
  246. strerror(errno));
  247. return -errno;
  248. }
  249. obj->efile.elf = elf_begin(obj->efile.fd,
  250. LIBBPF_ELF_C_READ_MMAP,
  251. NULL);
  252. }
  253. if (!obj->efile.elf) {
  254. pr_warning("failed to open %s as ELF file\n",
  255. obj->path);
  256. err = -EINVAL;
  257. goto errout;
  258. }
  259. if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
  260. pr_warning("failed to get EHDR from %s\n",
  261. obj->path);
  262. err = -EINVAL;
  263. goto errout;
  264. }
  265. ep = &obj->efile.ehdr;
  266. if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
  267. pr_warning("%s is not an eBPF object file\n",
  268. obj->path);
  269. err = -EINVAL;
  270. goto errout;
  271. }
  272. return 0;
  273. errout:
  274. bpf_object__elf_finish(obj);
  275. return err;
  276. }
  277. static int
  278. bpf_object__check_endianness(struct bpf_object *obj)
  279. {
  280. static unsigned int const endian = 1;
  281. switch (obj->efile.ehdr.e_ident[EI_DATA]) {
  282. case ELFDATA2LSB:
  283. /* We are big endian, BPF obj is little endian. */
  284. if (*(unsigned char const *)&endian != 1)
  285. goto mismatch;
  286. break;
  287. case ELFDATA2MSB:
  288. /* We are little endian, BPF obj is big endian. */
  289. if (*(unsigned char const *)&endian != 0)
  290. goto mismatch;
  291. break;
  292. default:
  293. return -EINVAL;
  294. }
  295. return 0;
  296. mismatch:
  297. pr_warning("Error: endianness mismatch.\n");
  298. return -EINVAL;
  299. }
  300. static int
  301. bpf_object__init_license(struct bpf_object *obj,
  302. void *data, size_t size)
  303. {
  304. memcpy(obj->license, data,
  305. min(size, sizeof(obj->license) - 1));
  306. pr_debug("license of %s is %s\n", obj->path, obj->license);
  307. return 0;
  308. }
  309. static int
  310. bpf_object__init_kversion(struct bpf_object *obj,
  311. void *data, size_t size)
  312. {
  313. u32 kver;
  314. if (size != sizeof(kver)) {
  315. pr_warning("invalid kver section in %s\n", obj->path);
  316. return -EINVAL;
  317. }
  318. memcpy(&kver, data, sizeof(kver));
  319. obj->kern_version = kver;
  320. pr_debug("kernel version of %s is %x\n", obj->path,
  321. obj->kern_version);
  322. return 0;
  323. }
  324. static int
  325. bpf_object__init_maps(struct bpf_object *obj, void *data,
  326. size_t size)
  327. {
  328. if (size == 0) {
  329. pr_debug("%s doesn't need map definition\n",
  330. obj->path);
  331. return 0;
  332. }
  333. obj->maps_buf = malloc(size);
  334. if (!obj->maps_buf) {
  335. pr_warning("malloc maps failed: %s\n", obj->path);
  336. return -ENOMEM;
  337. }
  338. obj->maps_buf_sz = size;
  339. memcpy(obj->maps_buf, data, size);
  340. pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
  341. return 0;
  342. }
  343. static int bpf_object__elf_collect(struct bpf_object *obj)
  344. {
  345. Elf *elf = obj->efile.elf;
  346. GElf_Ehdr *ep = &obj->efile.ehdr;
  347. Elf_Scn *scn = NULL;
  348. int idx = 0, err = 0;
  349. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  350. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
  351. pr_warning("failed to get e_shstrndx from %s\n",
  352. obj->path);
  353. return -EINVAL;
  354. }
  355. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  356. char *name;
  357. GElf_Shdr sh;
  358. Elf_Data *data;
  359. idx++;
  360. if (gelf_getshdr(scn, &sh) != &sh) {
  361. pr_warning("failed to get section header from %s\n",
  362. obj->path);
  363. err = -EINVAL;
  364. goto out;
  365. }
  366. name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
  367. if (!name) {
  368. pr_warning("failed to get section name from %s\n",
  369. obj->path);
  370. err = -EINVAL;
  371. goto out;
  372. }
  373. data = elf_getdata(scn, 0);
  374. if (!data) {
  375. pr_warning("failed to get section data from %s(%s)\n",
  376. name, obj->path);
  377. err = -EINVAL;
  378. goto out;
  379. }
  380. pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
  381. name, (unsigned long)data->d_size,
  382. (int)sh.sh_link, (unsigned long)sh.sh_flags,
  383. (int)sh.sh_type);
  384. if (strcmp(name, "license") == 0)
  385. err = bpf_object__init_license(obj,
  386. data->d_buf,
  387. data->d_size);
  388. else if (strcmp(name, "version") == 0)
  389. err = bpf_object__init_kversion(obj,
  390. data->d_buf,
  391. data->d_size);
  392. else if (strcmp(name, "maps") == 0)
  393. err = bpf_object__init_maps(obj, data->d_buf,
  394. data->d_size);
  395. else if (sh.sh_type == SHT_SYMTAB) {
  396. if (obj->efile.symbols) {
  397. pr_warning("bpf: multiple SYMTAB in %s\n",
  398. obj->path);
  399. err = -EEXIST;
  400. } else
  401. obj->efile.symbols = data;
  402. } else if ((sh.sh_type == SHT_PROGBITS) &&
  403. (sh.sh_flags & SHF_EXECINSTR) &&
  404. (data->d_size > 0)) {
  405. err = bpf_object__add_program(obj, data->d_buf,
  406. data->d_size, name, idx);
  407. if (err) {
  408. char errmsg[128];
  409. strerror_r(-err, errmsg, sizeof(errmsg));
  410. pr_warning("failed to alloc program %s (%s): %s",
  411. name, obj->path, errmsg);
  412. }
  413. } else if (sh.sh_type == SHT_REL) {
  414. void *reloc = obj->efile.reloc;
  415. int nr_reloc = obj->efile.nr_reloc + 1;
  416. reloc = realloc(reloc,
  417. sizeof(*obj->efile.reloc) * nr_reloc);
  418. if (!reloc) {
  419. pr_warning("realloc failed\n");
  420. err = -ENOMEM;
  421. } else {
  422. int n = nr_reloc - 1;
  423. obj->efile.reloc = reloc;
  424. obj->efile.nr_reloc = nr_reloc;
  425. obj->efile.reloc[n].shdr = sh;
  426. obj->efile.reloc[n].data = data;
  427. }
  428. }
  429. if (err)
  430. goto out;
  431. }
  432. out:
  433. return err;
  434. }
  435. static struct bpf_program *
  436. bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
  437. {
  438. struct bpf_program *prog;
  439. size_t i;
  440. for (i = 0; i < obj->nr_programs; i++) {
  441. prog = &obj->programs[i];
  442. if (prog->idx == idx)
  443. return prog;
  444. }
  445. return NULL;
  446. }
  447. static int
  448. bpf_program__collect_reloc(struct bpf_program *prog,
  449. size_t nr_maps, GElf_Shdr *shdr,
  450. Elf_Data *data, Elf_Data *symbols)
  451. {
  452. int i, nrels;
  453. pr_debug("collecting relocating info for: '%s'\n",
  454. prog->section_name);
  455. nrels = shdr->sh_size / shdr->sh_entsize;
  456. prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
  457. if (!prog->reloc_desc) {
  458. pr_warning("failed to alloc memory in relocation\n");
  459. return -ENOMEM;
  460. }
  461. prog->nr_reloc = nrels;
  462. for (i = 0; i < nrels; i++) {
  463. GElf_Sym sym;
  464. GElf_Rel rel;
  465. unsigned int insn_idx;
  466. struct bpf_insn *insns = prog->insns;
  467. size_t map_idx;
  468. if (!gelf_getrel(data, i, &rel)) {
  469. pr_warning("relocation: failed to get %d reloc\n", i);
  470. return -EINVAL;
  471. }
  472. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  473. pr_debug("relocation: insn_idx=%u\n", insn_idx);
  474. if (!gelf_getsym(symbols,
  475. GELF_R_SYM(rel.r_info),
  476. &sym)) {
  477. pr_warning("relocation: symbol %"PRIx64" not found\n",
  478. GELF_R_SYM(rel.r_info));
  479. return -EINVAL;
  480. }
  481. if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  482. pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
  483. insn_idx, insns[insn_idx].code);
  484. return -EINVAL;
  485. }
  486. map_idx = sym.st_value / sizeof(struct bpf_map_def);
  487. if (map_idx >= nr_maps) {
  488. pr_warning("bpf relocation: map_idx %d large than %d\n",
  489. (int)map_idx, (int)nr_maps - 1);
  490. return -EINVAL;
  491. }
  492. prog->reloc_desc[i].insn_idx = insn_idx;
  493. prog->reloc_desc[i].map_idx = map_idx;
  494. }
  495. return 0;
  496. }
  497. static int
  498. bpf_object__create_maps(struct bpf_object *obj)
  499. {
  500. unsigned int i;
  501. size_t nr_maps;
  502. int *pfd;
  503. nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def);
  504. if (!obj->maps_buf || !nr_maps) {
  505. pr_debug("don't need create maps for %s\n",
  506. obj->path);
  507. return 0;
  508. }
  509. obj->map_fds = malloc(sizeof(int) * nr_maps);
  510. if (!obj->map_fds) {
  511. pr_warning("realloc perf_bpf_map_fds failed\n");
  512. return -ENOMEM;
  513. }
  514. obj->nr_map_fds = nr_maps;
  515. /* fill all fd with -1 */
  516. memset(obj->map_fds, -1, sizeof(int) * nr_maps);
  517. pfd = obj->map_fds;
  518. for (i = 0; i < nr_maps; i++) {
  519. struct bpf_map_def def;
  520. def = *(struct bpf_map_def *)(obj->maps_buf +
  521. i * sizeof(struct bpf_map_def));
  522. *pfd = bpf_create_map(def.type,
  523. def.key_size,
  524. def.value_size,
  525. def.max_entries);
  526. if (*pfd < 0) {
  527. size_t j;
  528. int err = *pfd;
  529. pr_warning("failed to create map: %s\n",
  530. strerror(errno));
  531. for (j = 0; j < i; j++)
  532. zclose(obj->map_fds[j]);
  533. obj->nr_map_fds = 0;
  534. zfree(&obj->map_fds);
  535. return err;
  536. }
  537. pr_debug("create map: fd=%d\n", *pfd);
  538. pfd++;
  539. }
  540. zfree(&obj->maps_buf);
  541. obj->maps_buf_sz = 0;
  542. return 0;
  543. }
  544. static int bpf_object__collect_reloc(struct bpf_object *obj)
  545. {
  546. int i, err;
  547. if (!obj_elf_valid(obj)) {
  548. pr_warning("Internal error: elf object is closed\n");
  549. return -EINVAL;
  550. }
  551. for (i = 0; i < obj->efile.nr_reloc; i++) {
  552. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  553. Elf_Data *data = obj->efile.reloc[i].data;
  554. int idx = shdr->sh_info;
  555. struct bpf_program *prog;
  556. size_t nr_maps = obj->maps_buf_sz /
  557. sizeof(struct bpf_map_def);
  558. if (shdr->sh_type != SHT_REL) {
  559. pr_warning("internal error at %d\n", __LINE__);
  560. return -EINVAL;
  561. }
  562. prog = bpf_object__find_prog_by_idx(obj, idx);
  563. if (!prog) {
  564. pr_warning("relocation failed: no %d section\n",
  565. idx);
  566. return -ENOENT;
  567. }
  568. err = bpf_program__collect_reloc(prog, nr_maps,
  569. shdr, data,
  570. obj->efile.symbols);
  571. if (err)
  572. return -EINVAL;
  573. }
  574. return 0;
  575. }
  576. static int bpf_object__validate(struct bpf_object *obj)
  577. {
  578. if (obj->kern_version == 0) {
  579. pr_warning("%s doesn't provide kernel version\n",
  580. obj->path);
  581. return -EINVAL;
  582. }
  583. return 0;
  584. }
  585. static struct bpf_object *
  586. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  587. {
  588. struct bpf_object *obj;
  589. if (elf_version(EV_CURRENT) == EV_NONE) {
  590. pr_warning("failed to init libelf for %s\n", path);
  591. return NULL;
  592. }
  593. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  594. if (!obj)
  595. return NULL;
  596. if (bpf_object__elf_init(obj))
  597. goto out;
  598. if (bpf_object__check_endianness(obj))
  599. goto out;
  600. if (bpf_object__elf_collect(obj))
  601. goto out;
  602. if (bpf_object__collect_reloc(obj))
  603. goto out;
  604. if (bpf_object__validate(obj))
  605. goto out;
  606. bpf_object__elf_finish(obj);
  607. return obj;
  608. out:
  609. bpf_object__close(obj);
  610. return NULL;
  611. }
  612. struct bpf_object *bpf_object__open(const char *path)
  613. {
  614. /* param validation */
  615. if (!path)
  616. return NULL;
  617. pr_debug("loading %s\n", path);
  618. return __bpf_object__open(path, NULL, 0);
  619. }
  620. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  621. size_t obj_buf_sz)
  622. {
  623. /* param validation */
  624. if (!obj_buf || obj_buf_sz <= 0)
  625. return NULL;
  626. pr_debug("loading object from buffer\n");
  627. return __bpf_object__open("[buffer]", obj_buf, obj_buf_sz);
  628. }
  629. int bpf_object__unload(struct bpf_object *obj)
  630. {
  631. size_t i;
  632. if (!obj)
  633. return -EINVAL;
  634. for (i = 0; i < obj->nr_map_fds; i++)
  635. zclose(obj->map_fds[i]);
  636. zfree(&obj->map_fds);
  637. obj->nr_map_fds = 0;
  638. return 0;
  639. }
  640. int bpf_object__load(struct bpf_object *obj)
  641. {
  642. if (!obj)
  643. return -EINVAL;
  644. if (obj->loaded) {
  645. pr_warning("object should not be loaded twice\n");
  646. return -EINVAL;
  647. }
  648. obj->loaded = true;
  649. if (bpf_object__create_maps(obj))
  650. goto out;
  651. return 0;
  652. out:
  653. bpf_object__unload(obj);
  654. pr_warning("failed to load object '%s'\n", obj->path);
  655. return -EINVAL;
  656. }
  657. void bpf_object__close(struct bpf_object *obj)
  658. {
  659. size_t i;
  660. if (!obj)
  661. return;
  662. bpf_object__elf_finish(obj);
  663. bpf_object__unload(obj);
  664. zfree(&obj->maps_buf);
  665. if (obj->programs && obj->nr_programs) {
  666. for (i = 0; i < obj->nr_programs; i++)
  667. bpf_program__exit(&obj->programs[i]);
  668. }
  669. zfree(&obj->programs);
  670. free(obj);
  671. }