libbpf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 <linux/list.h>
  20. #include <libelf.h>
  21. #include <gelf.h>
  22. #include "libbpf.h"
  23. #include "bpf.h"
  24. #define __printf(a, b) __attribute__((format(printf, a, b)))
  25. __printf(1, 2)
  26. static int __base_pr(const char *format, ...)
  27. {
  28. va_list args;
  29. int err;
  30. va_start(args, format);
  31. err = vfprintf(stderr, format, args);
  32. va_end(args);
  33. return err;
  34. }
  35. static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  36. static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  37. static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  38. #define __pr(func, fmt, ...) \
  39. do { \
  40. if ((func)) \
  41. (func)("libbpf: " fmt, ##__VA_ARGS__); \
  42. } while (0)
  43. #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
  44. #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
  45. #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
  46. void libbpf_set_print(libbpf_print_fn_t warn,
  47. libbpf_print_fn_t info,
  48. libbpf_print_fn_t debug)
  49. {
  50. __pr_warning = warn;
  51. __pr_info = info;
  52. __pr_debug = debug;
  53. }
  54. /* Copied from tools/perf/util/util.h */
  55. #ifndef zfree
  56. # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  57. #endif
  58. #ifndef zclose
  59. # define zclose(fd) ({ \
  60. int ___err = 0; \
  61. if ((fd) >= 0) \
  62. ___err = close((fd)); \
  63. fd = -1; \
  64. ___err; })
  65. #endif
  66. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  67. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  68. #else
  69. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
  70. #endif
  71. /*
  72. * bpf_prog should be a better name but it has been used in
  73. * linux/filter.h.
  74. */
  75. struct bpf_program {
  76. /* Index in elf obj file, for relocation use. */
  77. int idx;
  78. char *section_name;
  79. struct bpf_insn *insns;
  80. size_t insns_cnt;
  81. struct {
  82. int insn_idx;
  83. int map_idx;
  84. } *reloc_desc;
  85. int nr_reloc;
  86. int fd;
  87. struct bpf_object *obj;
  88. void *priv;
  89. bpf_program_clear_priv_t clear_priv;
  90. };
  91. static LIST_HEAD(bpf_objects_list);
  92. struct bpf_object {
  93. char license[64];
  94. u32 kern_version;
  95. void *maps_buf;
  96. size_t maps_buf_sz;
  97. struct bpf_program *programs;
  98. size_t nr_programs;
  99. int *map_fds;
  100. /*
  101. * This field is required because maps_buf will be freed and
  102. * maps_buf_sz will be set to 0 after loaded.
  103. */
  104. size_t nr_map_fds;
  105. bool loaded;
  106. /*
  107. * Information when doing elf related work. Only valid if fd
  108. * is valid.
  109. */
  110. struct {
  111. int fd;
  112. void *obj_buf;
  113. size_t obj_buf_sz;
  114. Elf *elf;
  115. GElf_Ehdr ehdr;
  116. Elf_Data *symbols;
  117. struct {
  118. GElf_Shdr shdr;
  119. Elf_Data *data;
  120. } *reloc;
  121. int nr_reloc;
  122. } efile;
  123. /*
  124. * All loaded bpf_object is linked in a list, which is
  125. * hidden to caller. bpf_objects__<func> handlers deal with
  126. * all objects.
  127. */
  128. struct list_head list;
  129. char path[];
  130. };
  131. #define obj_elf_valid(o) ((o)->efile.elf)
  132. static void bpf_program__unload(struct bpf_program *prog)
  133. {
  134. if (!prog)
  135. return;
  136. zclose(prog->fd);
  137. }
  138. static void bpf_program__exit(struct bpf_program *prog)
  139. {
  140. if (!prog)
  141. return;
  142. if (prog->clear_priv)
  143. prog->clear_priv(prog, prog->priv);
  144. prog->priv = NULL;
  145. prog->clear_priv = NULL;
  146. bpf_program__unload(prog);
  147. zfree(&prog->section_name);
  148. zfree(&prog->insns);
  149. zfree(&prog->reloc_desc);
  150. prog->nr_reloc = 0;
  151. prog->insns_cnt = 0;
  152. prog->idx = -1;
  153. }
  154. static int
  155. bpf_program__init(void *data, size_t size, char *name, int idx,
  156. struct bpf_program *prog)
  157. {
  158. if (size < sizeof(struct bpf_insn)) {
  159. pr_warning("corrupted section '%s'\n", name);
  160. return -EINVAL;
  161. }
  162. bzero(prog, sizeof(*prog));
  163. prog->section_name = strdup(name);
  164. if (!prog->section_name) {
  165. pr_warning("failed to alloc name for prog %s\n",
  166. name);
  167. goto errout;
  168. }
  169. prog->insns = malloc(size);
  170. if (!prog->insns) {
  171. pr_warning("failed to alloc insns for %s\n", name);
  172. goto errout;
  173. }
  174. prog->insns_cnt = size / sizeof(struct bpf_insn);
  175. memcpy(prog->insns, data,
  176. prog->insns_cnt * sizeof(struct bpf_insn));
  177. prog->idx = idx;
  178. prog->fd = -1;
  179. return 0;
  180. errout:
  181. bpf_program__exit(prog);
  182. return -ENOMEM;
  183. }
  184. static int
  185. bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
  186. char *name, int idx)
  187. {
  188. struct bpf_program prog, *progs;
  189. int nr_progs, err;
  190. err = bpf_program__init(data, size, name, idx, &prog);
  191. if (err)
  192. return err;
  193. progs = obj->programs;
  194. nr_progs = obj->nr_programs;
  195. progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
  196. if (!progs) {
  197. /*
  198. * In this case the original obj->programs
  199. * is still valid, so don't need special treat for
  200. * bpf_close_object().
  201. */
  202. pr_warning("failed to alloc a new program '%s'\n",
  203. name);
  204. bpf_program__exit(&prog);
  205. return -ENOMEM;
  206. }
  207. pr_debug("found program %s\n", prog.section_name);
  208. obj->programs = progs;
  209. obj->nr_programs = nr_progs + 1;
  210. prog.obj = obj;
  211. progs[nr_progs] = prog;
  212. return 0;
  213. }
  214. static struct bpf_object *bpf_object__new(const char *path,
  215. void *obj_buf,
  216. size_t obj_buf_sz)
  217. {
  218. struct bpf_object *obj;
  219. obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
  220. if (!obj) {
  221. pr_warning("alloc memory failed for %s\n", path);
  222. return NULL;
  223. }
  224. strcpy(obj->path, path);
  225. obj->efile.fd = -1;
  226. /*
  227. * Caller of this function should also calls
  228. * bpf_object__elf_finish() after data collection to return
  229. * obj_buf to user. If not, we should duplicate the buffer to
  230. * avoid user freeing them before elf finish.
  231. */
  232. obj->efile.obj_buf = obj_buf;
  233. obj->efile.obj_buf_sz = obj_buf_sz;
  234. obj->loaded = false;
  235. INIT_LIST_HEAD(&obj->list);
  236. list_add(&obj->list, &bpf_objects_list);
  237. return obj;
  238. }
  239. static void bpf_object__elf_finish(struct bpf_object *obj)
  240. {
  241. if (!obj_elf_valid(obj))
  242. return;
  243. if (obj->efile.elf) {
  244. elf_end(obj->efile.elf);
  245. obj->efile.elf = NULL;
  246. }
  247. obj->efile.symbols = NULL;
  248. zfree(&obj->efile.reloc);
  249. obj->efile.nr_reloc = 0;
  250. zclose(obj->efile.fd);
  251. obj->efile.obj_buf = NULL;
  252. obj->efile.obj_buf_sz = 0;
  253. }
  254. static int bpf_object__elf_init(struct bpf_object *obj)
  255. {
  256. int err = 0;
  257. GElf_Ehdr *ep;
  258. if (obj_elf_valid(obj)) {
  259. pr_warning("elf init: internal error\n");
  260. return -EEXIST;
  261. }
  262. if (obj->efile.obj_buf_sz > 0) {
  263. /*
  264. * obj_buf should have been validated by
  265. * bpf_object__open_buffer().
  266. */
  267. obj->efile.elf = elf_memory(obj->efile.obj_buf,
  268. obj->efile.obj_buf_sz);
  269. } else {
  270. obj->efile.fd = open(obj->path, O_RDONLY);
  271. if (obj->efile.fd < 0) {
  272. pr_warning("failed to open %s: %s\n", obj->path,
  273. strerror(errno));
  274. return -errno;
  275. }
  276. obj->efile.elf = elf_begin(obj->efile.fd,
  277. LIBBPF_ELF_C_READ_MMAP,
  278. NULL);
  279. }
  280. if (!obj->efile.elf) {
  281. pr_warning("failed to open %s as ELF file\n",
  282. obj->path);
  283. err = -EINVAL;
  284. goto errout;
  285. }
  286. if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
  287. pr_warning("failed to get EHDR from %s\n",
  288. obj->path);
  289. err = -EINVAL;
  290. goto errout;
  291. }
  292. ep = &obj->efile.ehdr;
  293. if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
  294. pr_warning("%s is not an eBPF object file\n",
  295. obj->path);
  296. err = -EINVAL;
  297. goto errout;
  298. }
  299. return 0;
  300. errout:
  301. bpf_object__elf_finish(obj);
  302. return err;
  303. }
  304. static int
  305. bpf_object__check_endianness(struct bpf_object *obj)
  306. {
  307. static unsigned int const endian = 1;
  308. switch (obj->efile.ehdr.e_ident[EI_DATA]) {
  309. case ELFDATA2LSB:
  310. /* We are big endian, BPF obj is little endian. */
  311. if (*(unsigned char const *)&endian != 1)
  312. goto mismatch;
  313. break;
  314. case ELFDATA2MSB:
  315. /* We are little endian, BPF obj is big endian. */
  316. if (*(unsigned char const *)&endian != 0)
  317. goto mismatch;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. return 0;
  323. mismatch:
  324. pr_warning("Error: endianness mismatch.\n");
  325. return -EINVAL;
  326. }
  327. static int
  328. bpf_object__init_license(struct bpf_object *obj,
  329. void *data, size_t size)
  330. {
  331. memcpy(obj->license, data,
  332. min(size, sizeof(obj->license) - 1));
  333. pr_debug("license of %s is %s\n", obj->path, obj->license);
  334. return 0;
  335. }
  336. static int
  337. bpf_object__init_kversion(struct bpf_object *obj,
  338. void *data, size_t size)
  339. {
  340. u32 kver;
  341. if (size != sizeof(kver)) {
  342. pr_warning("invalid kver section in %s\n", obj->path);
  343. return -EINVAL;
  344. }
  345. memcpy(&kver, data, sizeof(kver));
  346. obj->kern_version = kver;
  347. pr_debug("kernel version of %s is %x\n", obj->path,
  348. obj->kern_version);
  349. return 0;
  350. }
  351. static int
  352. bpf_object__init_maps(struct bpf_object *obj, void *data,
  353. size_t size)
  354. {
  355. if (size == 0) {
  356. pr_debug("%s doesn't need map definition\n",
  357. obj->path);
  358. return 0;
  359. }
  360. obj->maps_buf = malloc(size);
  361. if (!obj->maps_buf) {
  362. pr_warning("malloc maps failed: %s\n", obj->path);
  363. return -ENOMEM;
  364. }
  365. obj->maps_buf_sz = size;
  366. memcpy(obj->maps_buf, data, size);
  367. pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
  368. return 0;
  369. }
  370. static int bpf_object__elf_collect(struct bpf_object *obj)
  371. {
  372. Elf *elf = obj->efile.elf;
  373. GElf_Ehdr *ep = &obj->efile.ehdr;
  374. Elf_Scn *scn = NULL;
  375. int idx = 0, err = 0;
  376. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  377. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
  378. pr_warning("failed to get e_shstrndx from %s\n",
  379. obj->path);
  380. return -EINVAL;
  381. }
  382. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  383. char *name;
  384. GElf_Shdr sh;
  385. Elf_Data *data;
  386. idx++;
  387. if (gelf_getshdr(scn, &sh) != &sh) {
  388. pr_warning("failed to get section header from %s\n",
  389. obj->path);
  390. err = -EINVAL;
  391. goto out;
  392. }
  393. name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
  394. if (!name) {
  395. pr_warning("failed to get section name from %s\n",
  396. obj->path);
  397. err = -EINVAL;
  398. goto out;
  399. }
  400. data = elf_getdata(scn, 0);
  401. if (!data) {
  402. pr_warning("failed to get section data from %s(%s)\n",
  403. name, obj->path);
  404. err = -EINVAL;
  405. goto out;
  406. }
  407. pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
  408. name, (unsigned long)data->d_size,
  409. (int)sh.sh_link, (unsigned long)sh.sh_flags,
  410. (int)sh.sh_type);
  411. if (strcmp(name, "license") == 0)
  412. err = bpf_object__init_license(obj,
  413. data->d_buf,
  414. data->d_size);
  415. else if (strcmp(name, "version") == 0)
  416. err = bpf_object__init_kversion(obj,
  417. data->d_buf,
  418. data->d_size);
  419. else if (strcmp(name, "maps") == 0)
  420. err = bpf_object__init_maps(obj, data->d_buf,
  421. data->d_size);
  422. else if (sh.sh_type == SHT_SYMTAB) {
  423. if (obj->efile.symbols) {
  424. pr_warning("bpf: multiple SYMTAB in %s\n",
  425. obj->path);
  426. err = -EEXIST;
  427. } else
  428. obj->efile.symbols = data;
  429. } else if ((sh.sh_type == SHT_PROGBITS) &&
  430. (sh.sh_flags & SHF_EXECINSTR) &&
  431. (data->d_size > 0)) {
  432. err = bpf_object__add_program(obj, data->d_buf,
  433. data->d_size, name, idx);
  434. if (err) {
  435. char errmsg[128];
  436. strerror_r(-err, errmsg, sizeof(errmsg));
  437. pr_warning("failed to alloc program %s (%s): %s",
  438. name, obj->path, errmsg);
  439. }
  440. } else if (sh.sh_type == SHT_REL) {
  441. void *reloc = obj->efile.reloc;
  442. int nr_reloc = obj->efile.nr_reloc + 1;
  443. reloc = realloc(reloc,
  444. sizeof(*obj->efile.reloc) * nr_reloc);
  445. if (!reloc) {
  446. pr_warning("realloc failed\n");
  447. err = -ENOMEM;
  448. } else {
  449. int n = nr_reloc - 1;
  450. obj->efile.reloc = reloc;
  451. obj->efile.nr_reloc = nr_reloc;
  452. obj->efile.reloc[n].shdr = sh;
  453. obj->efile.reloc[n].data = data;
  454. }
  455. }
  456. if (err)
  457. goto out;
  458. }
  459. out:
  460. return err;
  461. }
  462. static struct bpf_program *
  463. bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
  464. {
  465. struct bpf_program *prog;
  466. size_t i;
  467. for (i = 0; i < obj->nr_programs; i++) {
  468. prog = &obj->programs[i];
  469. if (prog->idx == idx)
  470. return prog;
  471. }
  472. return NULL;
  473. }
  474. static int
  475. bpf_program__collect_reloc(struct bpf_program *prog,
  476. size_t nr_maps, GElf_Shdr *shdr,
  477. Elf_Data *data, Elf_Data *symbols)
  478. {
  479. int i, nrels;
  480. pr_debug("collecting relocating info for: '%s'\n",
  481. prog->section_name);
  482. nrels = shdr->sh_size / shdr->sh_entsize;
  483. prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
  484. if (!prog->reloc_desc) {
  485. pr_warning("failed to alloc memory in relocation\n");
  486. return -ENOMEM;
  487. }
  488. prog->nr_reloc = nrels;
  489. for (i = 0; i < nrels; i++) {
  490. GElf_Sym sym;
  491. GElf_Rel rel;
  492. unsigned int insn_idx;
  493. struct bpf_insn *insns = prog->insns;
  494. size_t map_idx;
  495. if (!gelf_getrel(data, i, &rel)) {
  496. pr_warning("relocation: failed to get %d reloc\n", i);
  497. return -EINVAL;
  498. }
  499. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  500. pr_debug("relocation: insn_idx=%u\n", insn_idx);
  501. if (!gelf_getsym(symbols,
  502. GELF_R_SYM(rel.r_info),
  503. &sym)) {
  504. pr_warning("relocation: symbol %"PRIx64" not found\n",
  505. GELF_R_SYM(rel.r_info));
  506. return -EINVAL;
  507. }
  508. if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  509. pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
  510. insn_idx, insns[insn_idx].code);
  511. return -EINVAL;
  512. }
  513. map_idx = sym.st_value / sizeof(struct bpf_map_def);
  514. if (map_idx >= nr_maps) {
  515. pr_warning("bpf relocation: map_idx %d large than %d\n",
  516. (int)map_idx, (int)nr_maps - 1);
  517. return -EINVAL;
  518. }
  519. prog->reloc_desc[i].insn_idx = insn_idx;
  520. prog->reloc_desc[i].map_idx = map_idx;
  521. }
  522. return 0;
  523. }
  524. static int
  525. bpf_object__create_maps(struct bpf_object *obj)
  526. {
  527. unsigned int i;
  528. size_t nr_maps;
  529. int *pfd;
  530. nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def);
  531. if (!obj->maps_buf || !nr_maps) {
  532. pr_debug("don't need create maps for %s\n",
  533. obj->path);
  534. return 0;
  535. }
  536. obj->map_fds = malloc(sizeof(int) * nr_maps);
  537. if (!obj->map_fds) {
  538. pr_warning("realloc perf_bpf_map_fds failed\n");
  539. return -ENOMEM;
  540. }
  541. obj->nr_map_fds = nr_maps;
  542. /* fill all fd with -1 */
  543. memset(obj->map_fds, -1, sizeof(int) * nr_maps);
  544. pfd = obj->map_fds;
  545. for (i = 0; i < nr_maps; i++) {
  546. struct bpf_map_def def;
  547. def = *(struct bpf_map_def *)(obj->maps_buf +
  548. i * sizeof(struct bpf_map_def));
  549. *pfd = bpf_create_map(def.type,
  550. def.key_size,
  551. def.value_size,
  552. def.max_entries);
  553. if (*pfd < 0) {
  554. size_t j;
  555. int err = *pfd;
  556. pr_warning("failed to create map: %s\n",
  557. strerror(errno));
  558. for (j = 0; j < i; j++)
  559. zclose(obj->map_fds[j]);
  560. obj->nr_map_fds = 0;
  561. zfree(&obj->map_fds);
  562. return err;
  563. }
  564. pr_debug("create map: fd=%d\n", *pfd);
  565. pfd++;
  566. }
  567. zfree(&obj->maps_buf);
  568. obj->maps_buf_sz = 0;
  569. return 0;
  570. }
  571. static int
  572. bpf_program__relocate(struct bpf_program *prog, int *map_fds)
  573. {
  574. int i;
  575. if (!prog || !prog->reloc_desc)
  576. return 0;
  577. for (i = 0; i < prog->nr_reloc; i++) {
  578. int insn_idx, map_idx;
  579. struct bpf_insn *insns = prog->insns;
  580. insn_idx = prog->reloc_desc[i].insn_idx;
  581. map_idx = prog->reloc_desc[i].map_idx;
  582. if (insn_idx >= (int)prog->insns_cnt) {
  583. pr_warning("relocation out of range: '%s'\n",
  584. prog->section_name);
  585. return -ERANGE;
  586. }
  587. insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  588. insns[insn_idx].imm = map_fds[map_idx];
  589. }
  590. zfree(&prog->reloc_desc);
  591. prog->nr_reloc = 0;
  592. return 0;
  593. }
  594. static int
  595. bpf_object__relocate(struct bpf_object *obj)
  596. {
  597. struct bpf_program *prog;
  598. size_t i;
  599. int err;
  600. for (i = 0; i < obj->nr_programs; i++) {
  601. prog = &obj->programs[i];
  602. err = bpf_program__relocate(prog, obj->map_fds);
  603. if (err) {
  604. pr_warning("failed to relocate '%s'\n",
  605. prog->section_name);
  606. return err;
  607. }
  608. }
  609. return 0;
  610. }
  611. static int bpf_object__collect_reloc(struct bpf_object *obj)
  612. {
  613. int i, err;
  614. if (!obj_elf_valid(obj)) {
  615. pr_warning("Internal error: elf object is closed\n");
  616. return -EINVAL;
  617. }
  618. for (i = 0; i < obj->efile.nr_reloc; i++) {
  619. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  620. Elf_Data *data = obj->efile.reloc[i].data;
  621. int idx = shdr->sh_info;
  622. struct bpf_program *prog;
  623. size_t nr_maps = obj->maps_buf_sz /
  624. sizeof(struct bpf_map_def);
  625. if (shdr->sh_type != SHT_REL) {
  626. pr_warning("internal error at %d\n", __LINE__);
  627. return -EINVAL;
  628. }
  629. prog = bpf_object__find_prog_by_idx(obj, idx);
  630. if (!prog) {
  631. pr_warning("relocation failed: no %d section\n",
  632. idx);
  633. return -ENOENT;
  634. }
  635. err = bpf_program__collect_reloc(prog, nr_maps,
  636. shdr, data,
  637. obj->efile.symbols);
  638. if (err)
  639. return -EINVAL;
  640. }
  641. return 0;
  642. }
  643. static int
  644. load_program(struct bpf_insn *insns, int insns_cnt,
  645. char *license, u32 kern_version, int *pfd)
  646. {
  647. int ret;
  648. char *log_buf;
  649. if (!insns || !insns_cnt)
  650. return -EINVAL;
  651. log_buf = malloc(BPF_LOG_BUF_SIZE);
  652. if (!log_buf)
  653. pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
  654. ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
  655. insns_cnt, license, kern_version,
  656. log_buf, BPF_LOG_BUF_SIZE);
  657. if (ret >= 0) {
  658. *pfd = ret;
  659. ret = 0;
  660. goto out;
  661. }
  662. ret = -EINVAL;
  663. pr_warning("load bpf program failed: %s\n", strerror(errno));
  664. if (log_buf) {
  665. pr_warning("-- BEGIN DUMP LOG ---\n");
  666. pr_warning("\n%s\n", log_buf);
  667. pr_warning("-- END LOG --\n");
  668. }
  669. out:
  670. free(log_buf);
  671. return ret;
  672. }
  673. static int
  674. bpf_program__load(struct bpf_program *prog,
  675. char *license, u32 kern_version)
  676. {
  677. int err, fd;
  678. err = load_program(prog->insns, prog->insns_cnt,
  679. license, kern_version, &fd);
  680. if (!err)
  681. prog->fd = fd;
  682. if (err)
  683. pr_warning("failed to load program '%s'\n",
  684. prog->section_name);
  685. zfree(&prog->insns);
  686. prog->insns_cnt = 0;
  687. return err;
  688. }
  689. static int
  690. bpf_object__load_progs(struct bpf_object *obj)
  691. {
  692. size_t i;
  693. int err;
  694. for (i = 0; i < obj->nr_programs; i++) {
  695. err = bpf_program__load(&obj->programs[i],
  696. obj->license,
  697. obj->kern_version);
  698. if (err)
  699. return err;
  700. }
  701. return 0;
  702. }
  703. static int bpf_object__validate(struct bpf_object *obj)
  704. {
  705. if (obj->kern_version == 0) {
  706. pr_warning("%s doesn't provide kernel version\n",
  707. obj->path);
  708. return -EINVAL;
  709. }
  710. return 0;
  711. }
  712. static struct bpf_object *
  713. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  714. {
  715. struct bpf_object *obj;
  716. if (elf_version(EV_CURRENT) == EV_NONE) {
  717. pr_warning("failed to init libelf for %s\n", path);
  718. return NULL;
  719. }
  720. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  721. if (!obj)
  722. return NULL;
  723. if (bpf_object__elf_init(obj))
  724. goto out;
  725. if (bpf_object__check_endianness(obj))
  726. goto out;
  727. if (bpf_object__elf_collect(obj))
  728. goto out;
  729. if (bpf_object__collect_reloc(obj))
  730. goto out;
  731. if (bpf_object__validate(obj))
  732. goto out;
  733. bpf_object__elf_finish(obj);
  734. return obj;
  735. out:
  736. bpf_object__close(obj);
  737. return NULL;
  738. }
  739. struct bpf_object *bpf_object__open(const char *path)
  740. {
  741. /* param validation */
  742. if (!path)
  743. return NULL;
  744. pr_debug("loading %s\n", path);
  745. return __bpf_object__open(path, NULL, 0);
  746. }
  747. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  748. size_t obj_buf_sz,
  749. const char *name)
  750. {
  751. char tmp_name[64];
  752. /* param validation */
  753. if (!obj_buf || obj_buf_sz <= 0)
  754. return NULL;
  755. if (!name) {
  756. snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
  757. (unsigned long)obj_buf,
  758. (unsigned long)obj_buf_sz);
  759. tmp_name[sizeof(tmp_name) - 1] = '\0';
  760. name = tmp_name;
  761. }
  762. pr_debug("loading object '%s' from buffer\n",
  763. name);
  764. return __bpf_object__open(name, obj_buf, obj_buf_sz);
  765. }
  766. int bpf_object__unload(struct bpf_object *obj)
  767. {
  768. size_t i;
  769. if (!obj)
  770. return -EINVAL;
  771. for (i = 0; i < obj->nr_map_fds; i++)
  772. zclose(obj->map_fds[i]);
  773. zfree(&obj->map_fds);
  774. obj->nr_map_fds = 0;
  775. for (i = 0; i < obj->nr_programs; i++)
  776. bpf_program__unload(&obj->programs[i]);
  777. return 0;
  778. }
  779. int bpf_object__load(struct bpf_object *obj)
  780. {
  781. if (!obj)
  782. return -EINVAL;
  783. if (obj->loaded) {
  784. pr_warning("object should not be loaded twice\n");
  785. return -EINVAL;
  786. }
  787. obj->loaded = true;
  788. if (bpf_object__create_maps(obj))
  789. goto out;
  790. if (bpf_object__relocate(obj))
  791. goto out;
  792. if (bpf_object__load_progs(obj))
  793. goto out;
  794. return 0;
  795. out:
  796. bpf_object__unload(obj);
  797. pr_warning("failed to load object '%s'\n", obj->path);
  798. return -EINVAL;
  799. }
  800. void bpf_object__close(struct bpf_object *obj)
  801. {
  802. size_t i;
  803. if (!obj)
  804. return;
  805. bpf_object__elf_finish(obj);
  806. bpf_object__unload(obj);
  807. zfree(&obj->maps_buf);
  808. if (obj->programs && obj->nr_programs) {
  809. for (i = 0; i < obj->nr_programs; i++)
  810. bpf_program__exit(&obj->programs[i]);
  811. }
  812. zfree(&obj->programs);
  813. list_del(&obj->list);
  814. free(obj);
  815. }
  816. struct bpf_object *
  817. bpf_object__next(struct bpf_object *prev)
  818. {
  819. struct bpf_object *next;
  820. if (!prev)
  821. next = list_first_entry(&bpf_objects_list,
  822. struct bpf_object,
  823. list);
  824. else
  825. next = list_next_entry(prev, list);
  826. /* Empty list is noticed here so don't need checking on entry. */
  827. if (&next->list == &bpf_objects_list)
  828. return NULL;
  829. return next;
  830. }
  831. const char *
  832. bpf_object__get_name(struct bpf_object *obj)
  833. {
  834. if (!obj)
  835. return NULL;
  836. return obj->path;
  837. }
  838. struct bpf_program *
  839. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  840. {
  841. size_t idx;
  842. if (!obj->programs)
  843. return NULL;
  844. /* First handler */
  845. if (prev == NULL)
  846. return &obj->programs[0];
  847. if (prev->obj != obj) {
  848. pr_warning("error: program handler doesn't match object\n");
  849. return NULL;
  850. }
  851. idx = (prev - obj->programs) + 1;
  852. if (idx >= obj->nr_programs)
  853. return NULL;
  854. return &obj->programs[idx];
  855. }
  856. int bpf_program__set_private(struct bpf_program *prog,
  857. void *priv,
  858. bpf_program_clear_priv_t clear_priv)
  859. {
  860. if (prog->priv && prog->clear_priv)
  861. prog->clear_priv(prog, prog->priv);
  862. prog->priv = priv;
  863. prog->clear_priv = clear_priv;
  864. return 0;
  865. }
  866. int bpf_program__get_private(struct bpf_program *prog, void **ppriv)
  867. {
  868. *ppriv = prog->priv;
  869. return 0;
  870. }
  871. const char *bpf_program__title(struct bpf_program *prog, bool dup)
  872. {
  873. const char *title;
  874. title = prog->section_name;
  875. if (dup) {
  876. title = strdup(title);
  877. if (!title) {
  878. pr_warning("failed to strdup program title\n");
  879. return NULL;
  880. }
  881. }
  882. return title;
  883. }
  884. int bpf_program__fd(struct bpf_program *prog)
  885. {
  886. return prog->fd;
  887. }