libbpf.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Common eBPF ELF object loading operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  6. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  7. * Copyright (C) 2015 Huawei Inc.
  8. * Copyright (C) 2017 Nicira, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License (not later!)
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this program; if not, see <http://www.gnu.org/licenses>
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <libgen.h>
  27. #include <inttypes.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <asm/unistd.h>
  33. #include <linux/err.h>
  34. #include <linux/kernel.h>
  35. #include <linux/bpf.h>
  36. #include <linux/list.h>
  37. #include <linux/limits.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. #include <sys/vfs.h>
  41. #include <libelf.h>
  42. #include <gelf.h>
  43. #include "libbpf.h"
  44. #include "bpf.h"
  45. #ifndef EM_BPF
  46. #define EM_BPF 247
  47. #endif
  48. #ifndef BPF_FS_MAGIC
  49. #define BPF_FS_MAGIC 0xcafe4a11
  50. #endif
  51. #define __printf(a, b) __attribute__((format(printf, a, b)))
  52. __printf(1, 2)
  53. static int __base_pr(const char *format, ...)
  54. {
  55. va_list args;
  56. int err;
  57. va_start(args, format);
  58. err = vfprintf(stderr, format, args);
  59. va_end(args);
  60. return err;
  61. }
  62. static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  63. static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  64. static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  65. #define __pr(func, fmt, ...) \
  66. do { \
  67. if ((func)) \
  68. (func)("libbpf: " fmt, ##__VA_ARGS__); \
  69. } while (0)
  70. #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
  71. #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
  72. #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
  73. void libbpf_set_print(libbpf_print_fn_t warn,
  74. libbpf_print_fn_t info,
  75. libbpf_print_fn_t debug)
  76. {
  77. __pr_warning = warn;
  78. __pr_info = info;
  79. __pr_debug = debug;
  80. }
  81. #define STRERR_BUFSIZE 128
  82. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  83. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  84. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  85. static const char *libbpf_strerror_table[NR_ERRNO] = {
  86. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  87. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  88. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  89. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  90. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  91. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  92. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  93. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  94. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  95. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  96. [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
  97. [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
  98. };
  99. int libbpf_strerror(int err, char *buf, size_t size)
  100. {
  101. if (!buf || !size)
  102. return -1;
  103. err = err > 0 ? err : -err;
  104. if (err < __LIBBPF_ERRNO__START) {
  105. int ret;
  106. ret = strerror_r(err, buf, size);
  107. buf[size - 1] = '\0';
  108. return ret;
  109. }
  110. if (err < __LIBBPF_ERRNO__END) {
  111. const char *msg;
  112. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  113. snprintf(buf, size, "%s", msg);
  114. buf[size - 1] = '\0';
  115. return 0;
  116. }
  117. snprintf(buf, size, "Unknown libbpf error %d", err);
  118. buf[size - 1] = '\0';
  119. return -1;
  120. }
  121. #define CHECK_ERR(action, err, out) do { \
  122. err = action; \
  123. if (err) \
  124. goto out; \
  125. } while(0)
  126. /* Copied from tools/perf/util/util.h */
  127. #ifndef zfree
  128. # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  129. #endif
  130. #ifndef zclose
  131. # define zclose(fd) ({ \
  132. int ___err = 0; \
  133. if ((fd) >= 0) \
  134. ___err = close((fd)); \
  135. fd = -1; \
  136. ___err; })
  137. #endif
  138. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  139. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  140. #else
  141. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
  142. #endif
  143. /*
  144. * bpf_prog should be a better name but it has been used in
  145. * linux/filter.h.
  146. */
  147. struct bpf_program {
  148. /* Index in elf obj file, for relocation use. */
  149. int idx;
  150. char *name;
  151. char *section_name;
  152. struct bpf_insn *insns;
  153. size_t insns_cnt, main_prog_cnt;
  154. enum bpf_prog_type type;
  155. struct reloc_desc {
  156. enum {
  157. RELO_LD64,
  158. RELO_CALL,
  159. } type;
  160. int insn_idx;
  161. union {
  162. int map_idx;
  163. int text_off;
  164. };
  165. } *reloc_desc;
  166. int nr_reloc;
  167. struct {
  168. int nr;
  169. int *fds;
  170. } instances;
  171. bpf_program_prep_t preprocessor;
  172. struct bpf_object *obj;
  173. void *priv;
  174. bpf_program_clear_priv_t clear_priv;
  175. enum bpf_attach_type expected_attach_type;
  176. };
  177. struct bpf_map {
  178. int fd;
  179. char *name;
  180. size_t offset;
  181. struct bpf_map_def def;
  182. void *priv;
  183. bpf_map_clear_priv_t clear_priv;
  184. };
  185. static LIST_HEAD(bpf_objects_list);
  186. struct bpf_object {
  187. char license[64];
  188. u32 kern_version;
  189. struct bpf_program *programs;
  190. size_t nr_programs;
  191. struct bpf_map *maps;
  192. size_t nr_maps;
  193. bool loaded;
  194. /*
  195. * Information when doing elf related work. Only valid if fd
  196. * is valid.
  197. */
  198. struct {
  199. int fd;
  200. void *obj_buf;
  201. size_t obj_buf_sz;
  202. Elf *elf;
  203. GElf_Ehdr ehdr;
  204. Elf_Data *symbols;
  205. size_t strtabidx;
  206. struct {
  207. GElf_Shdr shdr;
  208. Elf_Data *data;
  209. } *reloc;
  210. int nr_reloc;
  211. int maps_shndx;
  212. int text_shndx;
  213. } efile;
  214. /*
  215. * All loaded bpf_object is linked in a list, which is
  216. * hidden to caller. bpf_objects__<func> handlers deal with
  217. * all objects.
  218. */
  219. struct list_head list;
  220. void *priv;
  221. bpf_object_clear_priv_t clear_priv;
  222. char path[];
  223. };
  224. #define obj_elf_valid(o) ((o)->efile.elf)
  225. static void bpf_program__unload(struct bpf_program *prog)
  226. {
  227. int i;
  228. if (!prog)
  229. return;
  230. /*
  231. * If the object is opened but the program was never loaded,
  232. * it is possible that prog->instances.nr == -1.
  233. */
  234. if (prog->instances.nr > 0) {
  235. for (i = 0; i < prog->instances.nr; i++)
  236. zclose(prog->instances.fds[i]);
  237. } else if (prog->instances.nr != -1) {
  238. pr_warning("Internal error: instances.nr is %d\n",
  239. prog->instances.nr);
  240. }
  241. prog->instances.nr = -1;
  242. zfree(&prog->instances.fds);
  243. }
  244. static void bpf_program__exit(struct bpf_program *prog)
  245. {
  246. if (!prog)
  247. return;
  248. if (prog->clear_priv)
  249. prog->clear_priv(prog, prog->priv);
  250. prog->priv = NULL;
  251. prog->clear_priv = NULL;
  252. bpf_program__unload(prog);
  253. zfree(&prog->name);
  254. zfree(&prog->section_name);
  255. zfree(&prog->insns);
  256. zfree(&prog->reloc_desc);
  257. prog->nr_reloc = 0;
  258. prog->insns_cnt = 0;
  259. prog->idx = -1;
  260. }
  261. static int
  262. bpf_program__init(void *data, size_t size, char *section_name, int idx,
  263. struct bpf_program *prog)
  264. {
  265. if (size < sizeof(struct bpf_insn)) {
  266. pr_warning("corrupted section '%s'\n", section_name);
  267. return -EINVAL;
  268. }
  269. bzero(prog, sizeof(*prog));
  270. prog->section_name = strdup(section_name);
  271. if (!prog->section_name) {
  272. pr_warning("failed to alloc name for prog under section(%d) %s\n",
  273. idx, section_name);
  274. goto errout;
  275. }
  276. prog->insns = malloc(size);
  277. if (!prog->insns) {
  278. pr_warning("failed to alloc insns for prog under section %s\n",
  279. section_name);
  280. goto errout;
  281. }
  282. prog->insns_cnt = size / sizeof(struct bpf_insn);
  283. memcpy(prog->insns, data,
  284. prog->insns_cnt * sizeof(struct bpf_insn));
  285. prog->idx = idx;
  286. prog->instances.fds = NULL;
  287. prog->instances.nr = -1;
  288. prog->type = BPF_PROG_TYPE_KPROBE;
  289. return 0;
  290. errout:
  291. bpf_program__exit(prog);
  292. return -ENOMEM;
  293. }
  294. static int
  295. bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
  296. char *section_name, int idx)
  297. {
  298. struct bpf_program prog, *progs;
  299. int nr_progs, err;
  300. err = bpf_program__init(data, size, section_name, idx, &prog);
  301. if (err)
  302. return err;
  303. progs = obj->programs;
  304. nr_progs = obj->nr_programs;
  305. progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
  306. if (!progs) {
  307. /*
  308. * In this case the original obj->programs
  309. * is still valid, so don't need special treat for
  310. * bpf_close_object().
  311. */
  312. pr_warning("failed to alloc a new program under section '%s'\n",
  313. section_name);
  314. bpf_program__exit(&prog);
  315. return -ENOMEM;
  316. }
  317. pr_debug("found program %s\n", prog.section_name);
  318. obj->programs = progs;
  319. obj->nr_programs = nr_progs + 1;
  320. prog.obj = obj;
  321. progs[nr_progs] = prog;
  322. return 0;
  323. }
  324. static int
  325. bpf_object__init_prog_names(struct bpf_object *obj)
  326. {
  327. Elf_Data *symbols = obj->efile.symbols;
  328. struct bpf_program *prog;
  329. size_t pi, si;
  330. for (pi = 0; pi < obj->nr_programs; pi++) {
  331. const char *name = NULL;
  332. prog = &obj->programs[pi];
  333. if (prog->idx == obj->efile.text_shndx) {
  334. name = ".text";
  335. goto skip_search;
  336. }
  337. for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
  338. si++) {
  339. GElf_Sym sym;
  340. if (!gelf_getsym(symbols, si, &sym))
  341. continue;
  342. if (sym.st_shndx != prog->idx)
  343. continue;
  344. if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
  345. continue;
  346. name = elf_strptr(obj->efile.elf,
  347. obj->efile.strtabidx,
  348. sym.st_name);
  349. if (!name) {
  350. pr_warning("failed to get sym name string for prog %s\n",
  351. prog->section_name);
  352. return -LIBBPF_ERRNO__LIBELF;
  353. }
  354. }
  355. if (!name) {
  356. pr_warning("failed to find sym for prog %s\n",
  357. prog->section_name);
  358. return -EINVAL;
  359. }
  360. skip_search:
  361. prog->name = strdup(name);
  362. if (!prog->name) {
  363. pr_warning("failed to allocate memory for prog sym %s\n",
  364. name);
  365. return -ENOMEM;
  366. }
  367. }
  368. return 0;
  369. }
  370. static struct bpf_object *bpf_object__new(const char *path,
  371. void *obj_buf,
  372. size_t obj_buf_sz)
  373. {
  374. struct bpf_object *obj;
  375. obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
  376. if (!obj) {
  377. pr_warning("alloc memory failed for %s\n", path);
  378. return ERR_PTR(-ENOMEM);
  379. }
  380. strcpy(obj->path, path);
  381. obj->efile.fd = -1;
  382. /*
  383. * Caller of this function should also calls
  384. * bpf_object__elf_finish() after data collection to return
  385. * obj_buf to user. If not, we should duplicate the buffer to
  386. * avoid user freeing them before elf finish.
  387. */
  388. obj->efile.obj_buf = obj_buf;
  389. obj->efile.obj_buf_sz = obj_buf_sz;
  390. obj->efile.maps_shndx = -1;
  391. obj->loaded = false;
  392. INIT_LIST_HEAD(&obj->list);
  393. list_add(&obj->list, &bpf_objects_list);
  394. return obj;
  395. }
  396. static void bpf_object__elf_finish(struct bpf_object *obj)
  397. {
  398. if (!obj_elf_valid(obj))
  399. return;
  400. if (obj->efile.elf) {
  401. elf_end(obj->efile.elf);
  402. obj->efile.elf = NULL;
  403. }
  404. obj->efile.symbols = NULL;
  405. zfree(&obj->efile.reloc);
  406. obj->efile.nr_reloc = 0;
  407. zclose(obj->efile.fd);
  408. obj->efile.obj_buf = NULL;
  409. obj->efile.obj_buf_sz = 0;
  410. }
  411. static int bpf_object__elf_init(struct bpf_object *obj)
  412. {
  413. int err = 0;
  414. GElf_Ehdr *ep;
  415. if (obj_elf_valid(obj)) {
  416. pr_warning("elf init: internal error\n");
  417. return -LIBBPF_ERRNO__LIBELF;
  418. }
  419. if (obj->efile.obj_buf_sz > 0) {
  420. /*
  421. * obj_buf should have been validated by
  422. * bpf_object__open_buffer().
  423. */
  424. obj->efile.elf = elf_memory(obj->efile.obj_buf,
  425. obj->efile.obj_buf_sz);
  426. } else {
  427. obj->efile.fd = open(obj->path, O_RDONLY);
  428. if (obj->efile.fd < 0) {
  429. pr_warning("failed to open %s: %s\n", obj->path,
  430. strerror(errno));
  431. return -errno;
  432. }
  433. obj->efile.elf = elf_begin(obj->efile.fd,
  434. LIBBPF_ELF_C_READ_MMAP,
  435. NULL);
  436. }
  437. if (!obj->efile.elf) {
  438. pr_warning("failed to open %s as ELF file\n",
  439. obj->path);
  440. err = -LIBBPF_ERRNO__LIBELF;
  441. goto errout;
  442. }
  443. if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
  444. pr_warning("failed to get EHDR from %s\n",
  445. obj->path);
  446. err = -LIBBPF_ERRNO__FORMAT;
  447. goto errout;
  448. }
  449. ep = &obj->efile.ehdr;
  450. /* Old LLVM set e_machine to EM_NONE */
  451. if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
  452. pr_warning("%s is not an eBPF object file\n",
  453. obj->path);
  454. err = -LIBBPF_ERRNO__FORMAT;
  455. goto errout;
  456. }
  457. return 0;
  458. errout:
  459. bpf_object__elf_finish(obj);
  460. return err;
  461. }
  462. static int
  463. bpf_object__check_endianness(struct bpf_object *obj)
  464. {
  465. static unsigned int const endian = 1;
  466. switch (obj->efile.ehdr.e_ident[EI_DATA]) {
  467. case ELFDATA2LSB:
  468. /* We are big endian, BPF obj is little endian. */
  469. if (*(unsigned char const *)&endian != 1)
  470. goto mismatch;
  471. break;
  472. case ELFDATA2MSB:
  473. /* We are little endian, BPF obj is big endian. */
  474. if (*(unsigned char const *)&endian != 0)
  475. goto mismatch;
  476. break;
  477. default:
  478. return -LIBBPF_ERRNO__ENDIAN;
  479. }
  480. return 0;
  481. mismatch:
  482. pr_warning("Error: endianness mismatch.\n");
  483. return -LIBBPF_ERRNO__ENDIAN;
  484. }
  485. static int
  486. bpf_object__init_license(struct bpf_object *obj,
  487. void *data, size_t size)
  488. {
  489. memcpy(obj->license, data,
  490. min(size, sizeof(obj->license) - 1));
  491. pr_debug("license of %s is %s\n", obj->path, obj->license);
  492. return 0;
  493. }
  494. static int
  495. bpf_object__init_kversion(struct bpf_object *obj,
  496. void *data, size_t size)
  497. {
  498. u32 kver;
  499. if (size != sizeof(kver)) {
  500. pr_warning("invalid kver section in %s\n", obj->path);
  501. return -LIBBPF_ERRNO__FORMAT;
  502. }
  503. memcpy(&kver, data, sizeof(kver));
  504. obj->kern_version = kver;
  505. pr_debug("kernel version of %s is %x\n", obj->path,
  506. obj->kern_version);
  507. return 0;
  508. }
  509. static int compare_bpf_map(const void *_a, const void *_b)
  510. {
  511. const struct bpf_map *a = _a;
  512. const struct bpf_map *b = _b;
  513. return a->offset - b->offset;
  514. }
  515. static int
  516. bpf_object__init_maps(struct bpf_object *obj)
  517. {
  518. int i, map_idx, map_def_sz, nr_maps = 0;
  519. Elf_Scn *scn;
  520. Elf_Data *data;
  521. Elf_Data *symbols = obj->efile.symbols;
  522. if (obj->efile.maps_shndx < 0)
  523. return -EINVAL;
  524. if (!symbols)
  525. return -EINVAL;
  526. scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
  527. if (scn)
  528. data = elf_getdata(scn, NULL);
  529. if (!scn || !data) {
  530. pr_warning("failed to get Elf_Data from map section %d\n",
  531. obj->efile.maps_shndx);
  532. return -EINVAL;
  533. }
  534. /*
  535. * Count number of maps. Each map has a name.
  536. * Array of maps is not supported: only the first element is
  537. * considered.
  538. *
  539. * TODO: Detect array of map and report error.
  540. */
  541. for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  542. GElf_Sym sym;
  543. if (!gelf_getsym(symbols, i, &sym))
  544. continue;
  545. if (sym.st_shndx != obj->efile.maps_shndx)
  546. continue;
  547. nr_maps++;
  548. }
  549. /* Alloc obj->maps and fill nr_maps. */
  550. pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
  551. nr_maps, data->d_size);
  552. if (!nr_maps)
  553. return 0;
  554. /* Assume equally sized map definitions */
  555. map_def_sz = data->d_size / nr_maps;
  556. if (!data->d_size || (data->d_size % nr_maps) != 0) {
  557. pr_warning("unable to determine map definition size "
  558. "section %s, %d maps in %zd bytes\n",
  559. obj->path, nr_maps, data->d_size);
  560. return -EINVAL;
  561. }
  562. obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
  563. if (!obj->maps) {
  564. pr_warning("alloc maps for object failed\n");
  565. return -ENOMEM;
  566. }
  567. obj->nr_maps = nr_maps;
  568. /*
  569. * fill all fd with -1 so won't close incorrect
  570. * fd (fd=0 is stdin) when failure (zclose won't close
  571. * negative fd)).
  572. */
  573. for (i = 0; i < nr_maps; i++)
  574. obj->maps[i].fd = -1;
  575. /*
  576. * Fill obj->maps using data in "maps" section.
  577. */
  578. for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  579. GElf_Sym sym;
  580. const char *map_name;
  581. struct bpf_map_def *def;
  582. if (!gelf_getsym(symbols, i, &sym))
  583. continue;
  584. if (sym.st_shndx != obj->efile.maps_shndx)
  585. continue;
  586. map_name = elf_strptr(obj->efile.elf,
  587. obj->efile.strtabidx,
  588. sym.st_name);
  589. obj->maps[map_idx].offset = sym.st_value;
  590. if (sym.st_value + map_def_sz > data->d_size) {
  591. pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
  592. obj->path, map_name);
  593. return -EINVAL;
  594. }
  595. obj->maps[map_idx].name = strdup(map_name);
  596. if (!obj->maps[map_idx].name) {
  597. pr_warning("failed to alloc map name\n");
  598. return -ENOMEM;
  599. }
  600. pr_debug("map %d is \"%s\"\n", map_idx,
  601. obj->maps[map_idx].name);
  602. def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
  603. /*
  604. * If the definition of the map in the object file fits in
  605. * bpf_map_def, copy it. Any extra fields in our version
  606. * of bpf_map_def will default to zero as a result of the
  607. * calloc above.
  608. */
  609. if (map_def_sz <= sizeof(struct bpf_map_def)) {
  610. memcpy(&obj->maps[map_idx].def, def, map_def_sz);
  611. } else {
  612. /*
  613. * Here the map structure being read is bigger than what
  614. * we expect, truncate if the excess bits are all zero.
  615. * If they are not zero, reject this map as
  616. * incompatible.
  617. */
  618. char *b;
  619. for (b = ((char *)def) + sizeof(struct bpf_map_def);
  620. b < ((char *)def) + map_def_sz; b++) {
  621. if (*b != 0) {
  622. pr_warning("maps section in %s: \"%s\" "
  623. "has unrecognized, non-zero "
  624. "options\n",
  625. obj->path, map_name);
  626. return -EINVAL;
  627. }
  628. }
  629. memcpy(&obj->maps[map_idx].def, def,
  630. sizeof(struct bpf_map_def));
  631. }
  632. map_idx++;
  633. }
  634. qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
  635. return 0;
  636. }
  637. static bool section_have_execinstr(struct bpf_object *obj, int idx)
  638. {
  639. Elf_Scn *scn;
  640. GElf_Shdr sh;
  641. scn = elf_getscn(obj->efile.elf, idx);
  642. if (!scn)
  643. return false;
  644. if (gelf_getshdr(scn, &sh) != &sh)
  645. return false;
  646. if (sh.sh_flags & SHF_EXECINSTR)
  647. return true;
  648. return false;
  649. }
  650. static int bpf_object__elf_collect(struct bpf_object *obj)
  651. {
  652. Elf *elf = obj->efile.elf;
  653. GElf_Ehdr *ep = &obj->efile.ehdr;
  654. Elf_Scn *scn = NULL;
  655. int idx = 0, err = 0;
  656. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  657. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
  658. pr_warning("failed to get e_shstrndx from %s\n",
  659. obj->path);
  660. return -LIBBPF_ERRNO__FORMAT;
  661. }
  662. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  663. char *name;
  664. GElf_Shdr sh;
  665. Elf_Data *data;
  666. idx++;
  667. if (gelf_getshdr(scn, &sh) != &sh) {
  668. pr_warning("failed to get section(%d) header from %s\n",
  669. idx, obj->path);
  670. err = -LIBBPF_ERRNO__FORMAT;
  671. goto out;
  672. }
  673. name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
  674. if (!name) {
  675. pr_warning("failed to get section(%d) name from %s\n",
  676. idx, obj->path);
  677. err = -LIBBPF_ERRNO__FORMAT;
  678. goto out;
  679. }
  680. data = elf_getdata(scn, 0);
  681. if (!data) {
  682. pr_warning("failed to get section(%d) data from %s(%s)\n",
  683. idx, name, obj->path);
  684. err = -LIBBPF_ERRNO__FORMAT;
  685. goto out;
  686. }
  687. pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
  688. idx, name, (unsigned long)data->d_size,
  689. (int)sh.sh_link, (unsigned long)sh.sh_flags,
  690. (int)sh.sh_type);
  691. if (strcmp(name, "license") == 0)
  692. err = bpf_object__init_license(obj,
  693. data->d_buf,
  694. data->d_size);
  695. else if (strcmp(name, "version") == 0)
  696. err = bpf_object__init_kversion(obj,
  697. data->d_buf,
  698. data->d_size);
  699. else if (strcmp(name, "maps") == 0)
  700. obj->efile.maps_shndx = idx;
  701. else if (sh.sh_type == SHT_SYMTAB) {
  702. if (obj->efile.symbols) {
  703. pr_warning("bpf: multiple SYMTAB in %s\n",
  704. obj->path);
  705. err = -LIBBPF_ERRNO__FORMAT;
  706. } else {
  707. obj->efile.symbols = data;
  708. obj->efile.strtabidx = sh.sh_link;
  709. }
  710. } else if ((sh.sh_type == SHT_PROGBITS) &&
  711. (sh.sh_flags & SHF_EXECINSTR) &&
  712. (data->d_size > 0)) {
  713. if (strcmp(name, ".text") == 0)
  714. obj->efile.text_shndx = idx;
  715. err = bpf_object__add_program(obj, data->d_buf,
  716. data->d_size, name, idx);
  717. if (err) {
  718. char errmsg[STRERR_BUFSIZE];
  719. strerror_r(-err, errmsg, sizeof(errmsg));
  720. pr_warning("failed to alloc program %s (%s): %s",
  721. name, obj->path, errmsg);
  722. }
  723. } else if (sh.sh_type == SHT_REL) {
  724. void *reloc = obj->efile.reloc;
  725. int nr_reloc = obj->efile.nr_reloc + 1;
  726. int sec = sh.sh_info; /* points to other section */
  727. /* Only do relo for section with exec instructions */
  728. if (!section_have_execinstr(obj, sec)) {
  729. pr_debug("skip relo %s(%d) for section(%d)\n",
  730. name, idx, sec);
  731. continue;
  732. }
  733. reloc = realloc(reloc,
  734. sizeof(*obj->efile.reloc) * nr_reloc);
  735. if (!reloc) {
  736. pr_warning("realloc failed\n");
  737. err = -ENOMEM;
  738. } else {
  739. int n = nr_reloc - 1;
  740. obj->efile.reloc = reloc;
  741. obj->efile.nr_reloc = nr_reloc;
  742. obj->efile.reloc[n].shdr = sh;
  743. obj->efile.reloc[n].data = data;
  744. }
  745. } else {
  746. pr_debug("skip section(%d) %s\n", idx, name);
  747. }
  748. if (err)
  749. goto out;
  750. }
  751. if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
  752. pr_warning("Corrupted ELF file: index of strtab invalid\n");
  753. return LIBBPF_ERRNO__FORMAT;
  754. }
  755. if (obj->efile.maps_shndx >= 0) {
  756. err = bpf_object__init_maps(obj);
  757. if (err)
  758. goto out;
  759. }
  760. err = bpf_object__init_prog_names(obj);
  761. out:
  762. return err;
  763. }
  764. static struct bpf_program *
  765. bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
  766. {
  767. struct bpf_program *prog;
  768. size_t i;
  769. for (i = 0; i < obj->nr_programs; i++) {
  770. prog = &obj->programs[i];
  771. if (prog->idx == idx)
  772. return prog;
  773. }
  774. return NULL;
  775. }
  776. static int
  777. bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
  778. Elf_Data *data, struct bpf_object *obj)
  779. {
  780. Elf_Data *symbols = obj->efile.symbols;
  781. int text_shndx = obj->efile.text_shndx;
  782. int maps_shndx = obj->efile.maps_shndx;
  783. struct bpf_map *maps = obj->maps;
  784. size_t nr_maps = obj->nr_maps;
  785. int i, nrels;
  786. pr_debug("collecting relocating info for: '%s'\n",
  787. prog->section_name);
  788. nrels = shdr->sh_size / shdr->sh_entsize;
  789. prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
  790. if (!prog->reloc_desc) {
  791. pr_warning("failed to alloc memory in relocation\n");
  792. return -ENOMEM;
  793. }
  794. prog->nr_reloc = nrels;
  795. for (i = 0; i < nrels; i++) {
  796. GElf_Sym sym;
  797. GElf_Rel rel;
  798. unsigned int insn_idx;
  799. struct bpf_insn *insns = prog->insns;
  800. size_t map_idx;
  801. if (!gelf_getrel(data, i, &rel)) {
  802. pr_warning("relocation: failed to get %d reloc\n", i);
  803. return -LIBBPF_ERRNO__FORMAT;
  804. }
  805. if (!gelf_getsym(symbols,
  806. GELF_R_SYM(rel.r_info),
  807. &sym)) {
  808. pr_warning("relocation: symbol %"PRIx64" not found\n",
  809. GELF_R_SYM(rel.r_info));
  810. return -LIBBPF_ERRNO__FORMAT;
  811. }
  812. pr_debug("relo for %lld value %lld name %d\n",
  813. (long long) (rel.r_info >> 32),
  814. (long long) sym.st_value, sym.st_name);
  815. if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
  816. pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
  817. prog->section_name, sym.st_shndx);
  818. return -LIBBPF_ERRNO__RELOC;
  819. }
  820. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  821. pr_debug("relocation: insn_idx=%u\n", insn_idx);
  822. if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
  823. if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
  824. pr_warning("incorrect bpf_call opcode\n");
  825. return -LIBBPF_ERRNO__RELOC;
  826. }
  827. prog->reloc_desc[i].type = RELO_CALL;
  828. prog->reloc_desc[i].insn_idx = insn_idx;
  829. prog->reloc_desc[i].text_off = sym.st_value;
  830. continue;
  831. }
  832. if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  833. pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
  834. insn_idx, insns[insn_idx].code);
  835. return -LIBBPF_ERRNO__RELOC;
  836. }
  837. /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
  838. for (map_idx = 0; map_idx < nr_maps; map_idx++) {
  839. if (maps[map_idx].offset == sym.st_value) {
  840. pr_debug("relocation: find map %zd (%s) for insn %u\n",
  841. map_idx, maps[map_idx].name, insn_idx);
  842. break;
  843. }
  844. }
  845. if (map_idx >= nr_maps) {
  846. pr_warning("bpf relocation: map_idx %d large than %d\n",
  847. (int)map_idx, (int)nr_maps - 1);
  848. return -LIBBPF_ERRNO__RELOC;
  849. }
  850. prog->reloc_desc[i].type = RELO_LD64;
  851. prog->reloc_desc[i].insn_idx = insn_idx;
  852. prog->reloc_desc[i].map_idx = map_idx;
  853. }
  854. return 0;
  855. }
  856. static int
  857. bpf_object__create_maps(struct bpf_object *obj)
  858. {
  859. unsigned int i;
  860. for (i = 0; i < obj->nr_maps; i++) {
  861. struct bpf_map_def *def = &obj->maps[i].def;
  862. int *pfd = &obj->maps[i].fd;
  863. *pfd = bpf_create_map_name(def->type,
  864. obj->maps[i].name,
  865. def->key_size,
  866. def->value_size,
  867. def->max_entries,
  868. def->map_flags);
  869. if (*pfd < 0) {
  870. size_t j;
  871. int err = *pfd;
  872. pr_warning("failed to create map (name: '%s'): %s\n",
  873. obj->maps[i].name,
  874. strerror(errno));
  875. for (j = 0; j < i; j++)
  876. zclose(obj->maps[j].fd);
  877. return err;
  878. }
  879. pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
  880. }
  881. return 0;
  882. }
  883. static int
  884. bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
  885. struct reloc_desc *relo)
  886. {
  887. struct bpf_insn *insn, *new_insn;
  888. struct bpf_program *text;
  889. size_t new_cnt;
  890. if (relo->type != RELO_CALL)
  891. return -LIBBPF_ERRNO__RELOC;
  892. if (prog->idx == obj->efile.text_shndx) {
  893. pr_warning("relo in .text insn %d into off %d\n",
  894. relo->insn_idx, relo->text_off);
  895. return -LIBBPF_ERRNO__RELOC;
  896. }
  897. if (prog->main_prog_cnt == 0) {
  898. text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
  899. if (!text) {
  900. pr_warning("no .text section found yet relo into text exist\n");
  901. return -LIBBPF_ERRNO__RELOC;
  902. }
  903. new_cnt = prog->insns_cnt + text->insns_cnt;
  904. new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
  905. if (!new_insn) {
  906. pr_warning("oom in prog realloc\n");
  907. return -ENOMEM;
  908. }
  909. memcpy(new_insn + prog->insns_cnt, text->insns,
  910. text->insns_cnt * sizeof(*insn));
  911. prog->insns = new_insn;
  912. prog->main_prog_cnt = prog->insns_cnt;
  913. prog->insns_cnt = new_cnt;
  914. pr_debug("added %zd insn from %s to prog %s\n",
  915. text->insns_cnt, text->section_name,
  916. prog->section_name);
  917. }
  918. insn = &prog->insns[relo->insn_idx];
  919. insn->imm += prog->main_prog_cnt - relo->insn_idx;
  920. return 0;
  921. }
  922. static int
  923. bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
  924. {
  925. int i, err;
  926. if (!prog || !prog->reloc_desc)
  927. return 0;
  928. for (i = 0; i < prog->nr_reloc; i++) {
  929. if (prog->reloc_desc[i].type == RELO_LD64) {
  930. struct bpf_insn *insns = prog->insns;
  931. int insn_idx, map_idx;
  932. insn_idx = prog->reloc_desc[i].insn_idx;
  933. map_idx = prog->reloc_desc[i].map_idx;
  934. if (insn_idx >= (int)prog->insns_cnt) {
  935. pr_warning("relocation out of range: '%s'\n",
  936. prog->section_name);
  937. return -LIBBPF_ERRNO__RELOC;
  938. }
  939. insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  940. insns[insn_idx].imm = obj->maps[map_idx].fd;
  941. } else {
  942. err = bpf_program__reloc_text(prog, obj,
  943. &prog->reloc_desc[i]);
  944. if (err)
  945. return err;
  946. }
  947. }
  948. zfree(&prog->reloc_desc);
  949. prog->nr_reloc = 0;
  950. return 0;
  951. }
  952. static int
  953. bpf_object__relocate(struct bpf_object *obj)
  954. {
  955. struct bpf_program *prog;
  956. size_t i;
  957. int err;
  958. for (i = 0; i < obj->nr_programs; i++) {
  959. prog = &obj->programs[i];
  960. err = bpf_program__relocate(prog, obj);
  961. if (err) {
  962. pr_warning("failed to relocate '%s'\n",
  963. prog->section_name);
  964. return err;
  965. }
  966. }
  967. return 0;
  968. }
  969. static int bpf_object__collect_reloc(struct bpf_object *obj)
  970. {
  971. int i, err;
  972. if (!obj_elf_valid(obj)) {
  973. pr_warning("Internal error: elf object is closed\n");
  974. return -LIBBPF_ERRNO__INTERNAL;
  975. }
  976. for (i = 0; i < obj->efile.nr_reloc; i++) {
  977. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  978. Elf_Data *data = obj->efile.reloc[i].data;
  979. int idx = shdr->sh_info;
  980. struct bpf_program *prog;
  981. if (shdr->sh_type != SHT_REL) {
  982. pr_warning("internal error at %d\n", __LINE__);
  983. return -LIBBPF_ERRNO__INTERNAL;
  984. }
  985. prog = bpf_object__find_prog_by_idx(obj, idx);
  986. if (!prog) {
  987. pr_warning("relocation failed: no section(%d)\n", idx);
  988. return -LIBBPF_ERRNO__RELOC;
  989. }
  990. err = bpf_program__collect_reloc(prog,
  991. shdr, data,
  992. obj);
  993. if (err)
  994. return err;
  995. }
  996. return 0;
  997. }
  998. static int
  999. load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
  1000. const char *name, struct bpf_insn *insns, int insns_cnt,
  1001. char *license, u32 kern_version, int *pfd)
  1002. {
  1003. struct bpf_load_program_attr load_attr;
  1004. char *log_buf;
  1005. int ret;
  1006. memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
  1007. load_attr.prog_type = type;
  1008. load_attr.expected_attach_type = expected_attach_type;
  1009. load_attr.name = name;
  1010. load_attr.insns = insns;
  1011. load_attr.insns_cnt = insns_cnt;
  1012. load_attr.license = license;
  1013. load_attr.kern_version = kern_version;
  1014. if (!load_attr.insns || !load_attr.insns_cnt)
  1015. return -EINVAL;
  1016. log_buf = malloc(BPF_LOG_BUF_SIZE);
  1017. if (!log_buf)
  1018. pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
  1019. ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
  1020. if (ret >= 0) {
  1021. *pfd = ret;
  1022. ret = 0;
  1023. goto out;
  1024. }
  1025. ret = -LIBBPF_ERRNO__LOAD;
  1026. pr_warning("load bpf program failed: %s\n", strerror(errno));
  1027. if (log_buf && log_buf[0] != '\0') {
  1028. ret = -LIBBPF_ERRNO__VERIFY;
  1029. pr_warning("-- BEGIN DUMP LOG ---\n");
  1030. pr_warning("\n%s\n", log_buf);
  1031. pr_warning("-- END LOG --\n");
  1032. } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
  1033. pr_warning("Program too large (%zu insns), at most %d insns\n",
  1034. load_attr.insns_cnt, BPF_MAXINSNS);
  1035. ret = -LIBBPF_ERRNO__PROG2BIG;
  1036. } else {
  1037. /* Wrong program type? */
  1038. if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
  1039. int fd;
  1040. load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
  1041. load_attr.expected_attach_type = 0;
  1042. fd = bpf_load_program_xattr(&load_attr, NULL, 0);
  1043. if (fd >= 0) {
  1044. close(fd);
  1045. ret = -LIBBPF_ERRNO__PROGTYPE;
  1046. goto out;
  1047. }
  1048. }
  1049. if (log_buf)
  1050. ret = -LIBBPF_ERRNO__KVER;
  1051. }
  1052. out:
  1053. free(log_buf);
  1054. return ret;
  1055. }
  1056. static int
  1057. bpf_program__load(struct bpf_program *prog,
  1058. char *license, u32 kern_version)
  1059. {
  1060. int err = 0, fd, i;
  1061. if (prog->instances.nr < 0 || !prog->instances.fds) {
  1062. if (prog->preprocessor) {
  1063. pr_warning("Internal error: can't load program '%s'\n",
  1064. prog->section_name);
  1065. return -LIBBPF_ERRNO__INTERNAL;
  1066. }
  1067. prog->instances.fds = malloc(sizeof(int));
  1068. if (!prog->instances.fds) {
  1069. pr_warning("Not enough memory for BPF fds\n");
  1070. return -ENOMEM;
  1071. }
  1072. prog->instances.nr = 1;
  1073. prog->instances.fds[0] = -1;
  1074. }
  1075. if (!prog->preprocessor) {
  1076. if (prog->instances.nr != 1) {
  1077. pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
  1078. prog->section_name, prog->instances.nr);
  1079. }
  1080. err = load_program(prog->type, prog->expected_attach_type,
  1081. prog->name, prog->insns, prog->insns_cnt,
  1082. license, kern_version, &fd);
  1083. if (!err)
  1084. prog->instances.fds[0] = fd;
  1085. goto out;
  1086. }
  1087. for (i = 0; i < prog->instances.nr; i++) {
  1088. struct bpf_prog_prep_result result;
  1089. bpf_program_prep_t preprocessor = prog->preprocessor;
  1090. bzero(&result, sizeof(result));
  1091. err = preprocessor(prog, i, prog->insns,
  1092. prog->insns_cnt, &result);
  1093. if (err) {
  1094. pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
  1095. i, prog->section_name);
  1096. goto out;
  1097. }
  1098. if (!result.new_insn_ptr || !result.new_insn_cnt) {
  1099. pr_debug("Skip loading the %dth instance of program '%s'\n",
  1100. i, prog->section_name);
  1101. prog->instances.fds[i] = -1;
  1102. if (result.pfd)
  1103. *result.pfd = -1;
  1104. continue;
  1105. }
  1106. err = load_program(prog->type, prog->expected_attach_type,
  1107. prog->name, result.new_insn_ptr,
  1108. result.new_insn_cnt,
  1109. license, kern_version, &fd);
  1110. if (err) {
  1111. pr_warning("Loading the %dth instance of program '%s' failed\n",
  1112. i, prog->section_name);
  1113. goto out;
  1114. }
  1115. if (result.pfd)
  1116. *result.pfd = fd;
  1117. prog->instances.fds[i] = fd;
  1118. }
  1119. out:
  1120. if (err)
  1121. pr_warning("failed to load program '%s'\n",
  1122. prog->section_name);
  1123. zfree(&prog->insns);
  1124. prog->insns_cnt = 0;
  1125. return err;
  1126. }
  1127. static int
  1128. bpf_object__load_progs(struct bpf_object *obj)
  1129. {
  1130. size_t i;
  1131. int err;
  1132. for (i = 0; i < obj->nr_programs; i++) {
  1133. if (obj->programs[i].idx == obj->efile.text_shndx)
  1134. continue;
  1135. err = bpf_program__load(&obj->programs[i],
  1136. obj->license,
  1137. obj->kern_version);
  1138. if (err)
  1139. return err;
  1140. }
  1141. return 0;
  1142. }
  1143. static int bpf_object__validate(struct bpf_object *obj)
  1144. {
  1145. if (obj->kern_version == 0) {
  1146. pr_warning("%s doesn't provide kernel version\n",
  1147. obj->path);
  1148. return -LIBBPF_ERRNO__KVERSION;
  1149. }
  1150. return 0;
  1151. }
  1152. static struct bpf_object *
  1153. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  1154. {
  1155. struct bpf_object *obj;
  1156. int err;
  1157. if (elf_version(EV_CURRENT) == EV_NONE) {
  1158. pr_warning("failed to init libelf for %s\n", path);
  1159. return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
  1160. }
  1161. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  1162. if (IS_ERR(obj))
  1163. return obj;
  1164. CHECK_ERR(bpf_object__elf_init(obj), err, out);
  1165. CHECK_ERR(bpf_object__check_endianness(obj), err, out);
  1166. CHECK_ERR(bpf_object__elf_collect(obj), err, out);
  1167. CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
  1168. CHECK_ERR(bpf_object__validate(obj), err, out);
  1169. bpf_object__elf_finish(obj);
  1170. return obj;
  1171. out:
  1172. bpf_object__close(obj);
  1173. return ERR_PTR(err);
  1174. }
  1175. struct bpf_object *bpf_object__open(const char *path)
  1176. {
  1177. /* param validation */
  1178. if (!path)
  1179. return NULL;
  1180. pr_debug("loading %s\n", path);
  1181. return __bpf_object__open(path, NULL, 0);
  1182. }
  1183. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  1184. size_t obj_buf_sz,
  1185. const char *name)
  1186. {
  1187. char tmp_name[64];
  1188. /* param validation */
  1189. if (!obj_buf || obj_buf_sz <= 0)
  1190. return NULL;
  1191. if (!name) {
  1192. snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
  1193. (unsigned long)obj_buf,
  1194. (unsigned long)obj_buf_sz);
  1195. tmp_name[sizeof(tmp_name) - 1] = '\0';
  1196. name = tmp_name;
  1197. }
  1198. pr_debug("loading object '%s' from buffer\n",
  1199. name);
  1200. return __bpf_object__open(name, obj_buf, obj_buf_sz);
  1201. }
  1202. int bpf_object__unload(struct bpf_object *obj)
  1203. {
  1204. size_t i;
  1205. if (!obj)
  1206. return -EINVAL;
  1207. for (i = 0; i < obj->nr_maps; i++)
  1208. zclose(obj->maps[i].fd);
  1209. for (i = 0; i < obj->nr_programs; i++)
  1210. bpf_program__unload(&obj->programs[i]);
  1211. return 0;
  1212. }
  1213. int bpf_object__load(struct bpf_object *obj)
  1214. {
  1215. int err;
  1216. if (!obj)
  1217. return -EINVAL;
  1218. if (obj->loaded) {
  1219. pr_warning("object should not be loaded twice\n");
  1220. return -EINVAL;
  1221. }
  1222. obj->loaded = true;
  1223. CHECK_ERR(bpf_object__create_maps(obj), err, out);
  1224. CHECK_ERR(bpf_object__relocate(obj), err, out);
  1225. CHECK_ERR(bpf_object__load_progs(obj), err, out);
  1226. return 0;
  1227. out:
  1228. bpf_object__unload(obj);
  1229. pr_warning("failed to load object '%s'\n", obj->path);
  1230. return err;
  1231. }
  1232. static int check_path(const char *path)
  1233. {
  1234. struct statfs st_fs;
  1235. char *dname, *dir;
  1236. int err = 0;
  1237. if (path == NULL)
  1238. return -EINVAL;
  1239. dname = strdup(path);
  1240. if (dname == NULL)
  1241. return -ENOMEM;
  1242. dir = dirname(dname);
  1243. if (statfs(dir, &st_fs)) {
  1244. pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
  1245. err = -errno;
  1246. }
  1247. free(dname);
  1248. if (!err && st_fs.f_type != BPF_FS_MAGIC) {
  1249. pr_warning("specified path %s is not on BPF FS\n", path);
  1250. err = -EINVAL;
  1251. }
  1252. return err;
  1253. }
  1254. int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
  1255. int instance)
  1256. {
  1257. int err;
  1258. err = check_path(path);
  1259. if (err)
  1260. return err;
  1261. if (prog == NULL) {
  1262. pr_warning("invalid program pointer\n");
  1263. return -EINVAL;
  1264. }
  1265. if (instance < 0 || instance >= prog->instances.nr) {
  1266. pr_warning("invalid prog instance %d of prog %s (max %d)\n",
  1267. instance, prog->section_name, prog->instances.nr);
  1268. return -EINVAL;
  1269. }
  1270. if (bpf_obj_pin(prog->instances.fds[instance], path)) {
  1271. pr_warning("failed to pin program: %s\n", strerror(errno));
  1272. return -errno;
  1273. }
  1274. pr_debug("pinned program '%s'\n", path);
  1275. return 0;
  1276. }
  1277. static int make_dir(const char *path)
  1278. {
  1279. int err = 0;
  1280. if (mkdir(path, 0700) && errno != EEXIST)
  1281. err = -errno;
  1282. if (err)
  1283. pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
  1284. return err;
  1285. }
  1286. int bpf_program__pin(struct bpf_program *prog, const char *path)
  1287. {
  1288. int i, err;
  1289. err = check_path(path);
  1290. if (err)
  1291. return err;
  1292. if (prog == NULL) {
  1293. pr_warning("invalid program pointer\n");
  1294. return -EINVAL;
  1295. }
  1296. if (prog->instances.nr <= 0) {
  1297. pr_warning("no instances of prog %s to pin\n",
  1298. prog->section_name);
  1299. return -EINVAL;
  1300. }
  1301. err = make_dir(path);
  1302. if (err)
  1303. return err;
  1304. for (i = 0; i < prog->instances.nr; i++) {
  1305. char buf[PATH_MAX];
  1306. int len;
  1307. len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
  1308. if (len < 0)
  1309. return -EINVAL;
  1310. else if (len >= PATH_MAX)
  1311. return -ENAMETOOLONG;
  1312. err = bpf_program__pin_instance(prog, buf, i);
  1313. if (err)
  1314. return err;
  1315. }
  1316. return 0;
  1317. }
  1318. int bpf_map__pin(struct bpf_map *map, const char *path)
  1319. {
  1320. int err;
  1321. err = check_path(path);
  1322. if (err)
  1323. return err;
  1324. if (map == NULL) {
  1325. pr_warning("invalid map pointer\n");
  1326. return -EINVAL;
  1327. }
  1328. if (bpf_obj_pin(map->fd, path)) {
  1329. pr_warning("failed to pin map: %s\n", strerror(errno));
  1330. return -errno;
  1331. }
  1332. pr_debug("pinned map '%s'\n", path);
  1333. return 0;
  1334. }
  1335. int bpf_object__pin(struct bpf_object *obj, const char *path)
  1336. {
  1337. struct bpf_program *prog;
  1338. struct bpf_map *map;
  1339. int err;
  1340. if (!obj)
  1341. return -ENOENT;
  1342. if (!obj->loaded) {
  1343. pr_warning("object not yet loaded; load it first\n");
  1344. return -ENOENT;
  1345. }
  1346. err = make_dir(path);
  1347. if (err)
  1348. return err;
  1349. bpf_map__for_each(map, obj) {
  1350. char buf[PATH_MAX];
  1351. int len;
  1352. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1353. bpf_map__name(map));
  1354. if (len < 0)
  1355. return -EINVAL;
  1356. else if (len >= PATH_MAX)
  1357. return -ENAMETOOLONG;
  1358. err = bpf_map__pin(map, buf);
  1359. if (err)
  1360. return err;
  1361. }
  1362. bpf_object__for_each_program(prog, obj) {
  1363. char buf[PATH_MAX];
  1364. int len;
  1365. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1366. prog->section_name);
  1367. if (len < 0)
  1368. return -EINVAL;
  1369. else if (len >= PATH_MAX)
  1370. return -ENAMETOOLONG;
  1371. err = bpf_program__pin(prog, buf);
  1372. if (err)
  1373. return err;
  1374. }
  1375. return 0;
  1376. }
  1377. void bpf_object__close(struct bpf_object *obj)
  1378. {
  1379. size_t i;
  1380. if (!obj)
  1381. return;
  1382. if (obj->clear_priv)
  1383. obj->clear_priv(obj, obj->priv);
  1384. bpf_object__elf_finish(obj);
  1385. bpf_object__unload(obj);
  1386. for (i = 0; i < obj->nr_maps; i++) {
  1387. zfree(&obj->maps[i].name);
  1388. if (obj->maps[i].clear_priv)
  1389. obj->maps[i].clear_priv(&obj->maps[i],
  1390. obj->maps[i].priv);
  1391. obj->maps[i].priv = NULL;
  1392. obj->maps[i].clear_priv = NULL;
  1393. }
  1394. zfree(&obj->maps);
  1395. obj->nr_maps = 0;
  1396. if (obj->programs && obj->nr_programs) {
  1397. for (i = 0; i < obj->nr_programs; i++)
  1398. bpf_program__exit(&obj->programs[i]);
  1399. }
  1400. zfree(&obj->programs);
  1401. list_del(&obj->list);
  1402. free(obj);
  1403. }
  1404. struct bpf_object *
  1405. bpf_object__next(struct bpf_object *prev)
  1406. {
  1407. struct bpf_object *next;
  1408. if (!prev)
  1409. next = list_first_entry(&bpf_objects_list,
  1410. struct bpf_object,
  1411. list);
  1412. else
  1413. next = list_next_entry(prev, list);
  1414. /* Empty list is noticed here so don't need checking on entry. */
  1415. if (&next->list == &bpf_objects_list)
  1416. return NULL;
  1417. return next;
  1418. }
  1419. const char *bpf_object__name(struct bpf_object *obj)
  1420. {
  1421. return obj ? obj->path : ERR_PTR(-EINVAL);
  1422. }
  1423. unsigned int bpf_object__kversion(struct bpf_object *obj)
  1424. {
  1425. return obj ? obj->kern_version : 0;
  1426. }
  1427. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  1428. bpf_object_clear_priv_t clear_priv)
  1429. {
  1430. if (obj->priv && obj->clear_priv)
  1431. obj->clear_priv(obj, obj->priv);
  1432. obj->priv = priv;
  1433. obj->clear_priv = clear_priv;
  1434. return 0;
  1435. }
  1436. void *bpf_object__priv(struct bpf_object *obj)
  1437. {
  1438. return obj ? obj->priv : ERR_PTR(-EINVAL);
  1439. }
  1440. struct bpf_program *
  1441. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  1442. {
  1443. size_t idx;
  1444. if (!obj->programs)
  1445. return NULL;
  1446. /* First handler */
  1447. if (prev == NULL)
  1448. return &obj->programs[0];
  1449. if (prev->obj != obj) {
  1450. pr_warning("error: program handler doesn't match object\n");
  1451. return NULL;
  1452. }
  1453. idx = (prev - obj->programs) + 1;
  1454. if (idx >= obj->nr_programs)
  1455. return NULL;
  1456. return &obj->programs[idx];
  1457. }
  1458. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  1459. bpf_program_clear_priv_t clear_priv)
  1460. {
  1461. if (prog->priv && prog->clear_priv)
  1462. prog->clear_priv(prog, prog->priv);
  1463. prog->priv = priv;
  1464. prog->clear_priv = clear_priv;
  1465. return 0;
  1466. }
  1467. void *bpf_program__priv(struct bpf_program *prog)
  1468. {
  1469. return prog ? prog->priv : ERR_PTR(-EINVAL);
  1470. }
  1471. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
  1472. {
  1473. const char *title;
  1474. title = prog->section_name;
  1475. if (needs_copy) {
  1476. title = strdup(title);
  1477. if (!title) {
  1478. pr_warning("failed to strdup program title\n");
  1479. return ERR_PTR(-ENOMEM);
  1480. }
  1481. }
  1482. return title;
  1483. }
  1484. int bpf_program__fd(struct bpf_program *prog)
  1485. {
  1486. return bpf_program__nth_fd(prog, 0);
  1487. }
  1488. int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  1489. bpf_program_prep_t prep)
  1490. {
  1491. int *instances_fds;
  1492. if (nr_instances <= 0 || !prep)
  1493. return -EINVAL;
  1494. if (prog->instances.nr > 0 || prog->instances.fds) {
  1495. pr_warning("Can't set pre-processor after loading\n");
  1496. return -EINVAL;
  1497. }
  1498. instances_fds = malloc(sizeof(int) * nr_instances);
  1499. if (!instances_fds) {
  1500. pr_warning("alloc memory failed for fds\n");
  1501. return -ENOMEM;
  1502. }
  1503. /* fill all fd with -1 */
  1504. memset(instances_fds, -1, sizeof(int) * nr_instances);
  1505. prog->instances.nr = nr_instances;
  1506. prog->instances.fds = instances_fds;
  1507. prog->preprocessor = prep;
  1508. return 0;
  1509. }
  1510. int bpf_program__nth_fd(struct bpf_program *prog, int n)
  1511. {
  1512. int fd;
  1513. if (n >= prog->instances.nr || n < 0) {
  1514. pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
  1515. n, prog->section_name, prog->instances.nr);
  1516. return -EINVAL;
  1517. }
  1518. fd = prog->instances.fds[n];
  1519. if (fd < 0) {
  1520. pr_warning("%dth instance of program '%s' is invalid\n",
  1521. n, prog->section_name);
  1522. return -ENOENT;
  1523. }
  1524. return fd;
  1525. }
  1526. void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
  1527. {
  1528. prog->type = type;
  1529. }
  1530. static bool bpf_program__is_type(struct bpf_program *prog,
  1531. enum bpf_prog_type type)
  1532. {
  1533. return prog ? (prog->type == type) : false;
  1534. }
  1535. #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
  1536. int bpf_program__set_##NAME(struct bpf_program *prog) \
  1537. { \
  1538. if (!prog) \
  1539. return -EINVAL; \
  1540. bpf_program__set_type(prog, TYPE); \
  1541. return 0; \
  1542. } \
  1543. \
  1544. bool bpf_program__is_##NAME(struct bpf_program *prog) \
  1545. { \
  1546. return bpf_program__is_type(prog, TYPE); \
  1547. } \
  1548. BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
  1549. BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
  1550. BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
  1551. BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
  1552. BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
  1553. BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
  1554. BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
  1555. static void bpf_program__set_expected_attach_type(struct bpf_program *prog,
  1556. enum bpf_attach_type type)
  1557. {
  1558. prog->expected_attach_type = type;
  1559. }
  1560. #define BPF_PROG_SEC_FULL(string, ptype, atype) \
  1561. { string, sizeof(string) - 1, ptype, atype }
  1562. #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
  1563. #define BPF_S_PROG_SEC(string, ptype) \
  1564. BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype)
  1565. #define BPF_SA_PROG_SEC(string, ptype) \
  1566. BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
  1567. static const struct {
  1568. const char *sec;
  1569. size_t len;
  1570. enum bpf_prog_type prog_type;
  1571. enum bpf_attach_type expected_attach_type;
  1572. } section_names[] = {
  1573. BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
  1574. BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
  1575. BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
  1576. BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
  1577. BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
  1578. BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
  1579. BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
  1580. BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
  1581. BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
  1582. BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
  1583. BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
  1584. BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
  1585. BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
  1586. BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
  1587. BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
  1588. BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
  1589. BPF_PROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG),
  1590. BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND),
  1591. BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND),
  1592. BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
  1593. BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
  1594. BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND),
  1595. BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND),
  1596. };
  1597. #undef BPF_PROG_SEC
  1598. #undef BPF_PROG_SEC_FULL
  1599. #undef BPF_S_PROG_SEC
  1600. #undef BPF_SA_PROG_SEC
  1601. static int bpf_program__identify_section(struct bpf_program *prog)
  1602. {
  1603. int i;
  1604. if (!prog->section_name)
  1605. goto err;
  1606. for (i = 0; i < ARRAY_SIZE(section_names); i++)
  1607. if (strncmp(prog->section_name, section_names[i].sec,
  1608. section_names[i].len) == 0)
  1609. return i;
  1610. err:
  1611. pr_warning("failed to guess program type based on section name %s\n",
  1612. prog->section_name);
  1613. return -1;
  1614. }
  1615. int bpf_map__fd(struct bpf_map *map)
  1616. {
  1617. return map ? map->fd : -EINVAL;
  1618. }
  1619. const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
  1620. {
  1621. return map ? &map->def : ERR_PTR(-EINVAL);
  1622. }
  1623. const char *bpf_map__name(struct bpf_map *map)
  1624. {
  1625. return map ? map->name : NULL;
  1626. }
  1627. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  1628. bpf_map_clear_priv_t clear_priv)
  1629. {
  1630. if (!map)
  1631. return -EINVAL;
  1632. if (map->priv) {
  1633. if (map->clear_priv)
  1634. map->clear_priv(map, map->priv);
  1635. }
  1636. map->priv = priv;
  1637. map->clear_priv = clear_priv;
  1638. return 0;
  1639. }
  1640. void *bpf_map__priv(struct bpf_map *map)
  1641. {
  1642. return map ? map->priv : ERR_PTR(-EINVAL);
  1643. }
  1644. struct bpf_map *
  1645. bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
  1646. {
  1647. size_t idx;
  1648. struct bpf_map *s, *e;
  1649. if (!obj || !obj->maps)
  1650. return NULL;
  1651. s = obj->maps;
  1652. e = obj->maps + obj->nr_maps;
  1653. if (prev == NULL)
  1654. return s;
  1655. if ((prev < s) || (prev >= e)) {
  1656. pr_warning("error in %s: map handler doesn't belong to object\n",
  1657. __func__);
  1658. return NULL;
  1659. }
  1660. idx = (prev - obj->maps) + 1;
  1661. if (idx >= obj->nr_maps)
  1662. return NULL;
  1663. return &obj->maps[idx];
  1664. }
  1665. struct bpf_map *
  1666. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
  1667. {
  1668. struct bpf_map *pos;
  1669. bpf_map__for_each(pos, obj) {
  1670. if (pos->name && !strcmp(pos->name, name))
  1671. return pos;
  1672. }
  1673. return NULL;
  1674. }
  1675. struct bpf_map *
  1676. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
  1677. {
  1678. int i;
  1679. for (i = 0; i < obj->nr_maps; i++) {
  1680. if (obj->maps[i].offset == offset)
  1681. return &obj->maps[i];
  1682. }
  1683. return ERR_PTR(-ENOENT);
  1684. }
  1685. long libbpf_get_error(const void *ptr)
  1686. {
  1687. if (IS_ERR(ptr))
  1688. return PTR_ERR(ptr);
  1689. return 0;
  1690. }
  1691. int bpf_prog_load(const char *file, enum bpf_prog_type type,
  1692. struct bpf_object **pobj, int *prog_fd)
  1693. {
  1694. struct bpf_prog_load_attr attr;
  1695. memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
  1696. attr.file = file;
  1697. attr.prog_type = type;
  1698. attr.expected_attach_type = 0;
  1699. return bpf_prog_load_xattr(&attr, pobj, prog_fd);
  1700. }
  1701. int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
  1702. struct bpf_object **pobj, int *prog_fd)
  1703. {
  1704. struct bpf_program *prog, *first_prog = NULL;
  1705. enum bpf_attach_type expected_attach_type;
  1706. enum bpf_prog_type prog_type;
  1707. struct bpf_object *obj;
  1708. int section_idx;
  1709. int err;
  1710. if (!attr)
  1711. return -EINVAL;
  1712. obj = bpf_object__open(attr->file);
  1713. if (IS_ERR(obj))
  1714. return -ENOENT;
  1715. bpf_object__for_each_program(prog, obj) {
  1716. /*
  1717. * If type is not specified, try to guess it based on
  1718. * section name.
  1719. */
  1720. prog_type = attr->prog_type;
  1721. expected_attach_type = attr->expected_attach_type;
  1722. if (prog_type == BPF_PROG_TYPE_UNSPEC) {
  1723. section_idx = bpf_program__identify_section(prog);
  1724. if (section_idx < 0) {
  1725. bpf_object__close(obj);
  1726. return -EINVAL;
  1727. }
  1728. prog_type = section_names[section_idx].prog_type;
  1729. expected_attach_type =
  1730. section_names[section_idx].expected_attach_type;
  1731. }
  1732. bpf_program__set_type(prog, prog_type);
  1733. bpf_program__set_expected_attach_type(prog,
  1734. expected_attach_type);
  1735. if (prog->idx != obj->efile.text_shndx && !first_prog)
  1736. first_prog = prog;
  1737. }
  1738. if (!first_prog) {
  1739. pr_warning("object file doesn't contain bpf program\n");
  1740. bpf_object__close(obj);
  1741. return -ENOENT;
  1742. }
  1743. err = bpf_object__load(obj);
  1744. if (err) {
  1745. bpf_object__close(obj);
  1746. return -EINVAL;
  1747. }
  1748. *pobj = obj;
  1749. *prog_fd = bpf_program__fd(first_prog);
  1750. return 0;
  1751. }