libbpf.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. {
  750. /* param validation */
  751. if (!obj_buf || obj_buf_sz <= 0)
  752. return NULL;
  753. pr_debug("loading object from buffer\n");
  754. return __bpf_object__open("[buffer]", obj_buf, obj_buf_sz);
  755. }
  756. int bpf_object__unload(struct bpf_object *obj)
  757. {
  758. size_t i;
  759. if (!obj)
  760. return -EINVAL;
  761. for (i = 0; i < obj->nr_map_fds; i++)
  762. zclose(obj->map_fds[i]);
  763. zfree(&obj->map_fds);
  764. obj->nr_map_fds = 0;
  765. for (i = 0; i < obj->nr_programs; i++)
  766. bpf_program__unload(&obj->programs[i]);
  767. return 0;
  768. }
  769. int bpf_object__load(struct bpf_object *obj)
  770. {
  771. if (!obj)
  772. return -EINVAL;
  773. if (obj->loaded) {
  774. pr_warning("object should not be loaded twice\n");
  775. return -EINVAL;
  776. }
  777. obj->loaded = true;
  778. if (bpf_object__create_maps(obj))
  779. goto out;
  780. if (bpf_object__relocate(obj))
  781. goto out;
  782. if (bpf_object__load_progs(obj))
  783. goto out;
  784. return 0;
  785. out:
  786. bpf_object__unload(obj);
  787. pr_warning("failed to load object '%s'\n", obj->path);
  788. return -EINVAL;
  789. }
  790. void bpf_object__close(struct bpf_object *obj)
  791. {
  792. size_t i;
  793. if (!obj)
  794. return;
  795. bpf_object__elf_finish(obj);
  796. bpf_object__unload(obj);
  797. zfree(&obj->maps_buf);
  798. if (obj->programs && obj->nr_programs) {
  799. for (i = 0; i < obj->nr_programs; i++)
  800. bpf_program__exit(&obj->programs[i]);
  801. }
  802. zfree(&obj->programs);
  803. list_del(&obj->list);
  804. free(obj);
  805. }
  806. struct bpf_object *
  807. bpf_object__next(struct bpf_object *prev)
  808. {
  809. struct bpf_object *next;
  810. if (!prev)
  811. next = list_first_entry(&bpf_objects_list,
  812. struct bpf_object,
  813. list);
  814. else
  815. next = list_next_entry(prev, list);
  816. /* Empty list is noticed here so don't need checking on entry. */
  817. if (&next->list == &bpf_objects_list)
  818. return NULL;
  819. return next;
  820. }
  821. struct bpf_program *
  822. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  823. {
  824. size_t idx;
  825. if (!obj->programs)
  826. return NULL;
  827. /* First handler */
  828. if (prev == NULL)
  829. return &obj->programs[0];
  830. if (prev->obj != obj) {
  831. pr_warning("error: program handler doesn't match object\n");
  832. return NULL;
  833. }
  834. idx = (prev - obj->programs) + 1;
  835. if (idx >= obj->nr_programs)
  836. return NULL;
  837. return &obj->programs[idx];
  838. }
  839. int bpf_program__set_private(struct bpf_program *prog,
  840. void *priv,
  841. bpf_program_clear_priv_t clear_priv)
  842. {
  843. if (prog->priv && prog->clear_priv)
  844. prog->clear_priv(prog, prog->priv);
  845. prog->priv = priv;
  846. prog->clear_priv = clear_priv;
  847. return 0;
  848. }
  849. int bpf_program__get_private(struct bpf_program *prog, void **ppriv)
  850. {
  851. *ppriv = prog->priv;
  852. return 0;
  853. }
  854. const char *bpf_program__title(struct bpf_program *prog, bool dup)
  855. {
  856. const char *title;
  857. title = prog->section_name;
  858. if (dup) {
  859. title = strdup(title);
  860. if (!title) {
  861. pr_warning("failed to strdup program title\n");
  862. return NULL;
  863. }
  864. }
  865. return title;
  866. }
  867. int bpf_program__fd(struct bpf_program *prog)
  868. {
  869. return prog->fd;
  870. }