libbpf.c 20 KB

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