Lines Matching +full:ext +full:- +full:gen

1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
6 * Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]>
70 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
223 [BPF_PROG_TYPE_EXT] = "ext",
298 if (err != -EPERM || geteuid() != 0) in pr_perm_msg()
315 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", in pr_perm_msg()
331 fd = -1; \
342 /* as of v1.0 libbpf_set_strict_mode() is a no-op */ in libbpf_set_strict_mode()
388 /* stored as sec_def->cookie for all libbpf-supported SEC()s */
407 /* BPF program support non-linear XDP buffer */
445 * program. For the entry-point (main) BPF program, this is always
446 * zero. For a sub-program, this gets reset before each of main BPF
448 * whether sub-program was already appended to the main program, and
460 * entry-point BPF programs this includes the size of main program
461 * itself plus all the used sub-programs, appended at the end
510 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
619 /* BTF fd index to be patched in for insn->off, this is
620 * 0 for vmlinux BTF, index in obj->fd_array for module
707 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
711 /* vmlinux BTF override for CO-RE relocations */
756 zclose(prog->fd); in bpf_program__unload()
758 zfree(&prog->func_info); in bpf_program__unload()
759 zfree(&prog->line_info); in bpf_program__unload()
768 zfree(&prog->name); in bpf_program__exit()
769 zfree(&prog->sec_name); in bpf_program__exit()
770 zfree(&prog->insns); in bpf_program__exit()
771 zfree(&prog->reloc_desc); in bpf_program__exit()
773 prog->nr_reloc = 0; in bpf_program__exit()
774 prog->insns_cnt = 0; in bpf_program__exit()
775 prog->sec_idx = -1; in bpf_program__exit()
780 return BPF_CLASS(insn->code) == BPF_JMP && in insn_is_subprog_call()
781 BPF_OP(insn->code) == BPF_CALL && in insn_is_subprog_call()
782 BPF_SRC(insn->code) == BPF_K && in insn_is_subprog_call()
783 insn->src_reg == BPF_PSEUDO_CALL && in insn_is_subprog_call()
784 insn->dst_reg == 0 && in insn_is_subprog_call()
785 insn->off == 0; in insn_is_subprog_call()
790 return insn->code == (BPF_JMP | BPF_CALL); in is_call_insn()
795 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; in insn_is_pseudo_func()
806 return -EINVAL; in bpf_object__init_prog()
810 prog->obj = obj; in bpf_object__init_prog()
812 prog->sec_idx = sec_idx; in bpf_object__init_prog()
813 prog->sec_insn_off = sec_off / BPF_INSN_SZ; in bpf_object__init_prog()
814 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; in bpf_object__init_prog()
816 prog->insns_cnt = prog->sec_insn_cnt; in bpf_object__init_prog()
818 prog->type = BPF_PROG_TYPE_UNSPEC; in bpf_object__init_prog()
819 prog->fd = -1; in bpf_object__init_prog()
820 prog->exception_cb_idx = -1; in bpf_object__init_prog()
827 prog->autoload = false; in bpf_object__init_prog()
831 prog->autoload = true; in bpf_object__init_prog()
834 prog->autoattach = true; in bpf_object__init_prog()
837 prog->log_level = obj->log_level; in bpf_object__init_prog()
839 prog->sec_name = strdup(sec_name); in bpf_object__init_prog()
840 if (!prog->sec_name) in bpf_object__init_prog()
843 prog->name = strdup(name); in bpf_object__init_prog()
844 if (!prog->name) in bpf_object__init_prog()
847 prog->insns = malloc(insn_data_sz); in bpf_object__init_prog()
848 if (!prog->insns) in bpf_object__init_prog()
850 memcpy(prog->insns, insn_data, insn_data_sz); in bpf_object__init_prog()
856 return -ENOMEM; in bpf_object__init_prog()
863 Elf_Data *symbols = obj->efile.symbols; in bpf_object__add_programs()
865 void *data = sec_data->d_buf; in bpf_object__add_programs()
866 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; in bpf_object__add_programs()
871 progs = obj->programs; in bpf_object__add_programs()
872 nr_progs = obj->nr_programs; in bpf_object__add_programs()
873 nr_syms = symbols->d_size / sizeof(Elf64_Sym); in bpf_object__add_programs()
878 if (sym->st_shndx != sec_idx) in bpf_object__add_programs()
880 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) in bpf_object__add_programs()
883 prog_sz = sym->st_size; in bpf_object__add_programs()
884 sec_off = sym->st_value; in bpf_object__add_programs()
886 name = elf_sym_str(obj, sym->st_name); in bpf_object__add_programs()
890 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
896 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
899 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { in bpf_object__add_programs()
901 return -ENOTSUP; in bpf_object__add_programs()
910 * In this case the original obj->programs in bpf_object__add_programs()
916 return -ENOMEM; in bpf_object__add_programs()
918 obj->programs = progs; in bpf_object__add_programs()
927 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) in bpf_object__add_programs()
928 prog->sym_global = true; in bpf_object__add_programs()
935 if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in bpf_object__add_programs()
936 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) in bpf_object__add_programs()
937 prog->mark_btf_static = true; in bpf_object__add_programs()
940 obj->nr_programs = nr_progs; in bpf_object__add_programs()
948 struct bpf_program *prog = obj->programs; in bpf_object_bswap_progs()
952 for (p = 0; p < obj->nr_programs; p++, prog++) { in bpf_object_bswap_progs()
953 insn = prog->insns; in bpf_object_bswap_progs()
954 for (i = 0; i < prog->insns_cnt; i++, insn++) in bpf_object_bswap_progs()
957 pr_debug("converted %zu BPF programs to native byte order\n", obj->nr_programs); in bpf_object_bswap_progs()
982 if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) in find_member_by_name()
1045 if (kern_data_member->type == kern_type_id) in find_struct_ops_kern_types()
1051 return -EINVAL; in find_struct_ops_kern_types()
1065 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; in bpf_map__is_struct_ops()
1073 for (i = 0; i < obj->nr_programs; i++) { in is_valid_st_ops_program()
1074 if (&obj->programs[i] == prog) in is_valid_st_ops_program()
1075 return prog->type == BPF_PROG_TYPE_STRUCT_OPS; in is_valid_st_ops_program()
1092 for (i = 0; i < obj->nr_programs; ++i) { in bpf_object_adjust_struct_ops_autoload()
1096 prog = &obj->programs[i]; in bpf_object_adjust_struct_ops_autoload()
1097 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) in bpf_object_adjust_struct_ops_autoload()
1100 for (j = 0; j < obj->nr_maps; ++j) { in bpf_object_adjust_struct_ops_autoload()
1103 map = &obj->maps[j]; in bpf_object_adjust_struct_ops_autoload()
1107 type = btf__type_by_id(obj->btf, map->st_ops->type_id); in bpf_object_adjust_struct_ops_autoload()
1110 slot_prog = map->st_ops->progs[k]; in bpf_object_adjust_struct_ops_autoload()
1115 if (map->autocreate) in bpf_object_adjust_struct_ops_autoload()
1120 prog->autoload = should_load; in bpf_object_adjust_struct_ops_autoload()
1132 struct bpf_object *obj = map->obj; in bpf_map__init_kern_struct_ops()
1133 const struct btf *btf = obj->btf; in bpf_map__init_kern_struct_ops()
1141 st_ops = map->st_ops; in bpf_map__init_kern_struct_ops()
1142 type = btf__type_by_id(btf, st_ops->type_id); in bpf_map__init_kern_struct_ops()
1143 tname = btf__name_by_offset(btf, type->name_off); in bpf_map__init_kern_struct_ops()
1151 kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux; in bpf_map__init_kern_struct_ops()
1154 map->name, st_ops->type_id, kern_type_id, kern_vtype_id); in bpf_map__init_kern_struct_ops()
1156 map->mod_btf_fd = mod_btf ? mod_btf->fd : -1; in bpf_map__init_kern_struct_ops()
1157 map->def.value_size = kern_vtype->size; in bpf_map__init_kern_struct_ops()
1158 map->btf_vmlinux_value_type_id = kern_vtype_id; in bpf_map__init_kern_struct_ops()
1160 st_ops->kern_vdata = calloc(1, kern_vtype->size); in bpf_map__init_kern_struct_ops()
1161 if (!st_ops->kern_vdata) in bpf_map__init_kern_struct_ops()
1162 return -ENOMEM; in bpf_map__init_kern_struct_ops()
1164 data = st_ops->data; in bpf_map__init_kern_struct_ops()
1165 kern_data_off = kern_data_member->offset / 8; in bpf_map__init_kern_struct_ops()
1166 kern_data = st_ops->kern_vdata + kern_data_off; in bpf_map__init_kern_struct_ops()
1179 mname = btf__name_by_offset(btf, member->name_off); in bpf_map__init_kern_struct_ops()
1180 moff = member->offset / 8; in bpf_map__init_kern_struct_ops()
1182 msize = btf__resolve_size(btf, member->type); in bpf_map__init_kern_struct_ops()
1185 map->name, mname); in bpf_map__init_kern_struct_ops()
1193 map->name, mname); in bpf_map__init_kern_struct_ops()
1194 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1197 if (st_ops->progs[i]) { in bpf_map__init_kern_struct_ops()
1205 st_ops->progs[i]->autoload = false; in bpf_map__init_kern_struct_ops()
1206 st_ops->progs[i] = NULL; in bpf_map__init_kern_struct_ops()
1209 /* Skip all-zero/NULL fields if they are not present in the kernel BTF */ in bpf_map__init_kern_struct_ops()
1211 map->name, mname); in bpf_map__init_kern_struct_ops()
1215 kern_member_idx = kern_member - btf_members(kern_type); in bpf_map__init_kern_struct_ops()
1219 map->name, mname); in bpf_map__init_kern_struct_ops()
1220 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1223 kern_moff = kern_member->offset / 8; in bpf_map__init_kern_struct_ops()
1226 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); in bpf_map__init_kern_struct_ops()
1227 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, in bpf_map__init_kern_struct_ops()
1229 if (BTF_INFO_KIND(mtype->info) != in bpf_map__init_kern_struct_ops()
1230 BTF_INFO_KIND(kern_mtype->info)) { in bpf_map__init_kern_struct_ops()
1232 map->name, mname, BTF_INFO_KIND(mtype->info), in bpf_map__init_kern_struct_ops()
1233 BTF_INFO_KIND(kern_mtype->info)); in bpf_map__init_kern_struct_ops()
1234 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1243 if (st_ops->progs[i] && st_ops->progs[i] != prog) in bpf_map__init_kern_struct_ops()
1244 st_ops->progs[i]->autoload = false; in bpf_map__init_kern_struct_ops()
1247 st_ops->progs[i] = prog; in bpf_map__init_kern_struct_ops()
1253 map->name, mname); in bpf_map__init_kern_struct_ops()
1254 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1258 kern_mtype->type, in bpf_map__init_kern_struct_ops()
1261 /* mtype->type must be a func_proto which was in bpf_map__init_kern_struct_ops()
1267 map->name, mname); in bpf_map__init_kern_struct_ops()
1268 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1272 prog->attach_btf_obj_fd = mod_btf->fd; in bpf_map__init_kern_struct_ops()
1277 if (!prog->attach_btf_id) { in bpf_map__init_kern_struct_ops()
1278 prog->attach_btf_id = kern_type_id; in bpf_map__init_kern_struct_ops()
1279 prog->expected_attach_type = kern_member_idx; in bpf_map__init_kern_struct_ops()
1282 /* struct_ops BPF prog can be re-used between multiple in bpf_map__init_kern_struct_ops()
1287 if (prog->attach_btf_id != kern_type_id) { in bpf_map__init_kern_struct_ops()
1289 map->name, mname, prog->name, prog->sec_name, prog->type, in bpf_map__init_kern_struct_ops()
1290 prog->attach_btf_id, kern_type_id); in bpf_map__init_kern_struct_ops()
1291 return -EINVAL; in bpf_map__init_kern_struct_ops()
1293 if (prog->expected_attach_type != kern_member_idx) { in bpf_map__init_kern_struct_ops()
1295 map->name, mname, prog->name, prog->sec_name, prog->type, in bpf_map__init_kern_struct_ops()
1296 prog->expected_attach_type, kern_member_idx); in bpf_map__init_kern_struct_ops()
1297 return -EINVAL; in bpf_map__init_kern_struct_ops()
1300 st_ops->kern_func_off[i] = kern_data_off + kern_moff; in bpf_map__init_kern_struct_ops()
1303 map->name, mname, prog->name, moff, in bpf_map__init_kern_struct_ops()
1312 map->name, mname, (ssize_t)msize, in bpf_map__init_kern_struct_ops()
1314 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1318 map->name, mname, (unsigned int)msize, in bpf_map__init_kern_struct_ops()
1332 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__init_kern_struct_ops_maps()
1333 map = &obj->maps[i]; in bpf_object__init_kern_struct_ops_maps()
1338 if (!map->autocreate) in bpf_object__init_kern_struct_ops_maps()
1361 if (shndx == -1) in init_struct_ops_maps()
1364 btf = obj->btf; in init_struct_ops_maps()
1370 return -EINVAL; in init_struct_ops_maps()
1376 type = btf__type_by_id(obj->btf, vsi->type); in init_struct_ops_maps()
1377 var_name = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1379 type_id = btf__resolve_type(obj->btf, vsi->type); in init_struct_ops_maps()
1382 vsi->type, sec_name); in init_struct_ops_maps()
1383 return -EINVAL; in init_struct_ops_maps()
1386 type = btf__type_by_id(obj->btf, type_id); in init_struct_ops_maps()
1387 tname = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1390 return -ENOTSUP; in init_struct_ops_maps()
1394 return -EINVAL; in init_struct_ops_maps()
1401 map->sec_idx = shndx; in init_struct_ops_maps()
1402 map->sec_offset = vsi->offset; in init_struct_ops_maps()
1403 map->name = strdup(var_name); in init_struct_ops_maps()
1404 if (!map->name) in init_struct_ops_maps()
1405 return -ENOMEM; in init_struct_ops_maps()
1406 map->btf_value_type_id = type_id; in init_struct_ops_maps()
1412 map->autocreate = false; in init_struct_ops_maps()
1417 map->def.type = BPF_MAP_TYPE_STRUCT_OPS; in init_struct_ops_maps()
1418 map->def.key_size = sizeof(int); in init_struct_ops_maps()
1419 map->def.value_size = type->size; in init_struct_ops_maps()
1420 map->def.max_entries = 1; in init_struct_ops_maps()
1421 map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0; in init_struct_ops_maps()
1422 map->autoattach = true; in init_struct_ops_maps()
1424 map->st_ops = calloc(1, sizeof(*map->st_ops)); in init_struct_ops_maps()
1425 if (!map->st_ops) in init_struct_ops_maps()
1426 return -ENOMEM; in init_struct_ops_maps()
1427 st_ops = map->st_ops; in init_struct_ops_maps()
1428 st_ops->data = malloc(type->size); in init_struct_ops_maps()
1429 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); in init_struct_ops_maps()
1430 st_ops->kern_func_off = malloc(btf_vlen(type) * in init_struct_ops_maps()
1431 sizeof(*st_ops->kern_func_off)); in init_struct_ops_maps()
1432 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) in init_struct_ops_maps()
1433 return -ENOMEM; in init_struct_ops_maps()
1435 if (vsi->offset + type->size > data->d_size) { in init_struct_ops_maps()
1438 return -EINVAL; in init_struct_ops_maps()
1441 memcpy(st_ops->data, in init_struct_ops_maps()
1442 data->d_buf + vsi->offset, in init_struct_ops_maps()
1443 type->size); in init_struct_ops_maps()
1444 st_ops->type_id = type_id; in init_struct_ops_maps()
1447 tname, type_id, var_name, vsi->offset); in init_struct_ops_maps()
1458 for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) { in bpf_object_init_struct_ops()
1459 struct elf_sec_desc *desc = &obj->efile.secs[sec_idx]; in bpf_object_init_struct_ops()
1461 if (desc->sec_type != SEC_ST_OPS) in bpf_object_init_struct_ops()
1466 return -LIBBPF_ERRNO__FORMAT; in bpf_object_init_struct_ops()
1468 err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data); in bpf_object_init_struct_ops()
1487 return ERR_PTR(-ENOMEM); in bpf_object__new()
1490 strcpy(obj->path, path); in bpf_object__new()
1492 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); in bpf_object__new()
1495 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); in bpf_object__new()
1496 end = strchr(obj->name, '.'); in bpf_object__new()
1501 obj->efile.fd = -1; in bpf_object__new()
1508 obj->efile.obj_buf = obj_buf; in bpf_object__new()
1509 obj->efile.obj_buf_sz = obj_buf_sz; in bpf_object__new()
1510 obj->efile.btf_maps_shndx = -1; in bpf_object__new()
1511 obj->kconfig_map_idx = -1; in bpf_object__new()
1513 obj->kern_version = get_kernel_version(); in bpf_object__new()
1514 obj->loaded = false; in bpf_object__new()
1521 if (!obj->efile.elf) in bpf_object__elf_finish()
1524 elf_end(obj->efile.elf); in bpf_object__elf_finish()
1525 obj->efile.elf = NULL; in bpf_object__elf_finish()
1526 obj->efile.ehdr = NULL; in bpf_object__elf_finish()
1527 obj->efile.symbols = NULL; in bpf_object__elf_finish()
1528 obj->efile.arena_data = NULL; in bpf_object__elf_finish()
1530 zfree(&obj->efile.secs); in bpf_object__elf_finish()
1531 obj->efile.sec_cnt = 0; in bpf_object__elf_finish()
1532 zclose(obj->efile.fd); in bpf_object__elf_finish()
1533 obj->efile.obj_buf = NULL; in bpf_object__elf_finish()
1534 obj->efile.obj_buf_sz = 0; in bpf_object__elf_finish()
1543 if (obj->efile.elf) { in bpf_object__elf_init()
1545 return -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1548 if (obj->efile.obj_buf_sz > 0) { in bpf_object__elf_init()
1550 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); in bpf_object__elf_init()
1552 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); in bpf_object__elf_init()
1553 if (obj->efile.fd < 0) { in bpf_object__elf_init()
1554 err = -errno; in bpf_object__elf_init()
1555 pr_warn("elf: failed to open %s: %s\n", obj->path, errstr(err)); in bpf_object__elf_init()
1559 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); in bpf_object__elf_init()
1563 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1564 err = -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1568 obj->efile.elf = elf; in bpf_object__elf_init()
1571 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1572 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); in bpf_object__elf_init()
1577 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1578 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); in bpf_object__elf_init()
1582 obj->efile.ehdr = ehdr = elf64_getehdr(elf); in bpf_object__elf_init()
1583 if (!obj->efile.ehdr) { in bpf_object__elf_init()
1584 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1585 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1590 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB && in bpf_object__elf_init()
1591 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { in bpf_object__elf_init()
1592 err = -LIBBPF_ERRNO__ENDIAN; in bpf_object__elf_init()
1593 pr_warn("elf: '%s' has unknown byte order\n", obj->path); in bpf_object__elf_init()
1597 obj->byteorder = ehdr->e_ident[EI_DATA]; in bpf_object__elf_init()
1599 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { in bpf_object__elf_init()
1601 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1602 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1607 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { in bpf_object__elf_init()
1609 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1610 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1615 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { in bpf_object__elf_init()
1616 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); in bpf_object__elf_init()
1617 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1630 return obj->byteorder == ELFDATA2LSB; in is_native_endianness()
1632 return obj->byteorder == ELFDATA2MSB; in is_native_endianness()
1642 pr_warn("invalid license section in %s\n", obj->path); in bpf_object__init_license()
1643 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_license()
1645 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't in bpf_object__init_license()
1648 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); in bpf_object__init_license()
1649 pr_debug("license of %s is %s\n", obj->path, obj->license); in bpf_object__init_license()
1659 pr_warn("invalid kver section in %s\n", obj->path); in bpf_object__init_kversion()
1660 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_kversion()
1663 obj->kern_version = kver; in bpf_object__init_kversion()
1664 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); in bpf_object__init_kversion()
1682 return -EINVAL; in find_elf_sec_sz()
1687 *size = data->d_size; in find_elf_sec_sz()
1691 return -ENOENT; in find_elf_sec_sz()
1696 Elf_Data *symbols = obj->efile.symbols; in find_elf_var_sym()
1700 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { in find_elf_var_sym()
1703 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) in find_elf_var_sym()
1706 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && in find_elf_var_sym()
1707 ELF64_ST_BIND(sym->st_info) != STB_WEAK) in find_elf_var_sym()
1710 sname = elf_sym_str(obj, sym->st_name); in find_elf_var_sym()
1713 return ERR_PTR(-EIO); in find_elf_var_sym()
1719 return ERR_PTR(-ENOENT); in find_elf_var_sym()
1741 const char *name = "libbpf-placeholder-fd"; in create_placeholder_fd()
1748 return -errno; in create_placeholder_fd()
1753 return -errno; in create_placeholder_fd()
1762 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, in bpf_object__add_map()
1763 sizeof(*obj->maps), obj->nr_maps + 1); in bpf_object__add_map()
1767 map = &obj->maps[obj->nr_maps++]; in bpf_object__add_map()
1768 map->obj = obj; in bpf_object__add_map()
1781 map->fd = create_placeholder_fd(); in bpf_object__add_map()
1782 if (map->fd < 0) in bpf_object__add_map()
1783 return ERR_PTR(map->fd); in bpf_object__add_map()
1784 map->inner_map_fd = -1; in bpf_object__add_map()
1785 map->autocreate = true; in bpf_object__add_map()
1804 switch (map->def.type) { in bpf_map_mmap_sz()
1806 return array_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_map_mmap_sz()
1808 return page_sz * map->def.max_entries; in bpf_map_mmap_sz()
1818 if (!map->mmaped) in bpf_map_mmap_resize()
1819 return -EINVAL; in bpf_map_mmap_resize()
1824 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_map_mmap_resize()
1826 return -errno; in bpf_map_mmap_resize()
1828 memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); in bpf_map_mmap_resize()
1829 munmap(map->mmaped, old_sz); in bpf_map_mmap_resize()
1830 map->mmaped = mmaped; in bpf_map_mmap_resize()
1864 * '.rodata.abracad' kernel and user-visible name. in internal_map_name()
1873 sfx_len = BPF_OBJ_NAME_LEN - 1; in internal_map_name()
1879 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); in internal_map_name()
1881 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, in internal_map_name()
1906 if (!map->btf_value_type_id) in map_is_mmapable()
1909 t = btf__type_by_id(obj->btf, map->btf_value_type_id); in map_is_mmapable()
1915 vt = btf__type_by_id(obj->btf, vsi->type); in map_is_mmapable()
1919 if (btf_var(vt)->linkage != BTF_VAR_STATIC) in map_is_mmapable()
1939 map->libbpf_type = type; in bpf_object__init_internal_map()
1940 map->sec_idx = sec_idx; in bpf_object__init_internal_map()
1941 map->sec_offset = 0; in bpf_object__init_internal_map()
1942 map->real_name = strdup(real_name); in bpf_object__init_internal_map()
1943 map->name = internal_map_name(obj, real_name); in bpf_object__init_internal_map()
1944 if (!map->real_name || !map->name) { in bpf_object__init_internal_map()
1945 zfree(&map->real_name); in bpf_object__init_internal_map()
1946 zfree(&map->name); in bpf_object__init_internal_map()
1947 return -ENOMEM; in bpf_object__init_internal_map()
1950 def = &map->def; in bpf_object__init_internal_map()
1951 def->type = BPF_MAP_TYPE_ARRAY; in bpf_object__init_internal_map()
1952 def->key_size = sizeof(int); in bpf_object__init_internal_map()
1953 def->value_size = data_sz; in bpf_object__init_internal_map()
1954 def->max_entries = 1; in bpf_object__init_internal_map()
1955 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG in bpf_object__init_internal_map()
1962 def->map_flags |= BPF_F_MMAPABLE; in bpf_object__init_internal_map()
1965 map->name, map->sec_idx, map->sec_offset, def->map_flags); in bpf_object__init_internal_map()
1968 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, in bpf_object__init_internal_map()
1969 MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_object__init_internal_map()
1970 if (map->mmaped == MAP_FAILED) { in bpf_object__init_internal_map()
1971 err = -errno; in bpf_object__init_internal_map()
1972 map->mmaped = NULL; in bpf_object__init_internal_map()
1973 pr_warn("failed to alloc map '%s' content buffer: %s\n", map->name, errstr(err)); in bpf_object__init_internal_map()
1974 zfree(&map->real_name); in bpf_object__init_internal_map()
1975 zfree(&map->name); in bpf_object__init_internal_map()
1980 memcpy(map->mmaped, data, data_sz); in bpf_object__init_internal_map()
1982 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); in bpf_object__init_internal_map()
1993 * Populate obj->maps with libbpf internal maps. in bpf_object__init_global_data_maps()
1995 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { in bpf_object__init_global_data_maps()
1996 sec_desc = &obj->efile.secs[sec_idx]; in bpf_object__init_global_data_maps()
1999 if (!sec_desc->data || sec_desc->data->d_size == 0) in bpf_object__init_global_data_maps()
2002 switch (sec_desc->sec_type) { in bpf_object__init_global_data_maps()
2007 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
2008 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
2011 obj->has_rodata = true; in bpf_object__init_global_data_maps()
2015 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
2016 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
2023 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
2041 for (i = 0; i < obj->nr_extern; i++) { in find_extern_by_name()
2042 if (strcmp(obj->externs[i].name, name) == 0) in find_extern_by_name()
2043 return &obj->externs[i]; in find_extern_by_name()
2054 for (i = 0; i < obj->nr_extern; i++) { in find_extern_by_name_with_len()
2055 ext_name = obj->externs[i].name; in find_extern_by_name_with_len()
2057 return &obj->externs[i]; in find_extern_by_name_with_len()
2062 static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, in set_kcfg_value_tri() argument
2065 switch (ext->kcfg.type) { in set_kcfg_value_tri()
2069 ext->name, value); in set_kcfg_value_tri()
2070 return -EINVAL; in set_kcfg_value_tri()
2090 ext->name, value); in set_kcfg_value_tri()
2091 return -EINVAL; in set_kcfg_value_tri()
2093 ext->is_set = true; in set_kcfg_value_tri()
2097 static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, in set_kcfg_value_str() argument
2102 if (ext->kcfg.type != KCFG_CHAR_ARR) { in set_kcfg_value_str()
2104 ext->name, value); in set_kcfg_value_str()
2105 return -EINVAL; in set_kcfg_value_str()
2109 if (value[len - 1] != '"') { in set_kcfg_value_str()
2111 ext->name, value); in set_kcfg_value_str()
2112 return -EINVAL; in set_kcfg_value_str()
2116 len -= 2; in set_kcfg_value_str()
2117 if (len >= ext->kcfg.sz) { in set_kcfg_value_str()
2119 ext->name, value, len, ext->kcfg.sz - 1); in set_kcfg_value_str()
2120 len = ext->kcfg.sz - 1; in set_kcfg_value_str()
2124 ext->is_set = true; in set_kcfg_value_str()
2136 err = -errno; in parse_u64()
2142 return -EINVAL; in parse_u64()
2147 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) in is_kcfg_value_in_range() argument
2149 int bit_sz = ext->kcfg.sz * 8; in is_kcfg_value_in_range()
2151 if (ext->kcfg.sz == 8) in is_kcfg_value_in_range()
2154 /* Validate that value stored in u64 fits in integer of `ext->sz` in is_kcfg_value_in_range()
2159 * -2^(Y-1) <= X <= 2^(Y-1) - 1 in is_kcfg_value_in_range()
2160 * 0 <= X + 2^(Y-1) <= 2^Y - 1 in is_kcfg_value_in_range()
2161 * 0 <= X + 2^(Y-1) < 2^Y in is_kcfg_value_in_range()
2163 * For unsigned target integer, check that all the (64 - Y) bits are in is_kcfg_value_in_range()
2166 if (ext->kcfg.is_signed) in is_kcfg_value_in_range()
2167 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); in is_kcfg_value_in_range()
2172 static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, in set_kcfg_value_num() argument
2175 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && in set_kcfg_value_num()
2176 ext->kcfg.type != KCFG_BOOL) { in set_kcfg_value_num()
2178 ext->name, (unsigned long long)value); in set_kcfg_value_num()
2179 return -EINVAL; in set_kcfg_value_num()
2181 if (ext->kcfg.type == KCFG_BOOL && value > 1) { in set_kcfg_value_num()
2183 ext->name, (unsigned long long)value); in set_kcfg_value_num()
2184 return -EINVAL; in set_kcfg_value_num()
2187 if (!is_kcfg_value_in_range(ext, value)) { in set_kcfg_value_num()
2189 ext->name, (unsigned long long)value, ext->kcfg.sz); in set_kcfg_value_num()
2190 return -ERANGE; in set_kcfg_value_num()
2192 switch (ext->kcfg.sz) { in set_kcfg_value_num()
2206 return -EINVAL; in set_kcfg_value_num()
2208 ext->is_set = true; in set_kcfg_value_num()
2215 struct extern_desc *ext; in bpf_object__process_kconfig_line() local
2227 return -EINVAL; in bpf_object__process_kconfig_line()
2232 if (buf[len - 1] == '\n') in bpf_object__process_kconfig_line()
2233 buf[len - 1] = '\0'; in bpf_object__process_kconfig_line()
2239 return -EINVAL; in bpf_object__process_kconfig_line()
2242 ext = find_extern_by_name(obj, buf); in bpf_object__process_kconfig_line()
2243 if (!ext || ext->is_set) in bpf_object__process_kconfig_line()
2246 ext_val = data + ext->kcfg.data_off; in bpf_object__process_kconfig_line()
2251 err = set_kcfg_value_tri(ext, ext_val, *value); in bpf_object__process_kconfig_line()
2254 err = set_kcfg_value_str(ext, ext_val, value); in bpf_object__process_kconfig_line()
2260 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); in bpf_object__process_kconfig_line()
2263 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { in bpf_object__process_kconfig_line()
2264 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); in bpf_object__process_kconfig_line()
2265 return -EINVAL; in bpf_object__process_kconfig_line()
2267 err = set_kcfg_value_num(ext, ext_val, num); in bpf_object__process_kconfig_line()
2272 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); in bpf_object__process_kconfig_line()
2284 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); in bpf_object__read_kconfig_file()
2286 return -EINVAL; in bpf_object__read_kconfig_file()
2288 return -ENAMETOOLONG; in bpf_object__read_kconfig_file()
2297 return -ENOENT; in bpf_object__read_kconfig_file()
2323 err = -errno; in bpf_object__read_kconfig_mem()
2324 pr_warn("failed to open in-memory Kconfig: %s\n", errstr(err)); in bpf_object__read_kconfig_mem()
2331 pr_warn("error parsing in-memory Kconfig line '%s': %s\n", in bpf_object__read_kconfig_mem()
2343 struct extern_desc *last_ext = NULL, *ext; in bpf_object__init_kconfig_map() local
2347 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__init_kconfig_map()
2348 ext = &obj->externs[i]; in bpf_object__init_kconfig_map()
2349 if (ext->type == EXT_KCFG) in bpf_object__init_kconfig_map()
2350 last_ext = ext; in bpf_object__init_kconfig_map()
2356 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; in bpf_object__init_kconfig_map()
2358 ".kconfig", obj->efile.symbols_shndx, in bpf_object__init_kconfig_map()
2363 obj->kconfig_map_idx = obj->nr_maps - 1; in bpf_object__init_kconfig_map()
2378 *res_id = t->type; in skip_mods_and_typedefs()
2379 t = btf__type_by_id(btf, t->type); in skip_mods_and_typedefs()
2394 t = skip_mods_and_typedefs(btf, t->type, res_id); in resolve_func_ptr()
2441 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); in get_map_field_int()
2442 const char *name = btf__name_by_offset(btf, m->name_off); in get_map_field_int()
2452 arr_t = btf__type_by_id(btf, t->type); in get_map_field_int()
2455 map_name, name, t->type); in get_map_field_int()
2464 *res = arr_info->nelems; in get_map_field_int()
2471 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); in get_map_field_long()
2472 const char *name = btf__name_by_offset(btf, m->name_off); in get_map_field_long()
2499 *res = e->val; in get_map_field_long()
2514 return -EINVAL; in pathname_concat()
2516 return -ENAMETOOLONG; in pathname_concat()
2555 const char *name = btf__name_by_offset(btf, m->name_off); in parse_btf_map_def()
2559 return -EINVAL; in parse_btf_map_def()
2562 if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) in parse_btf_map_def()
2563 return -EINVAL; in parse_btf_map_def()
2564 map_def->parts |= MAP_DEF_MAP_TYPE; in parse_btf_map_def()
2566 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) in parse_btf_map_def()
2567 return -EINVAL; in parse_btf_map_def()
2568 map_def->parts |= MAP_DEF_MAX_ENTRIES; in parse_btf_map_def()
2570 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) in parse_btf_map_def()
2571 return -EINVAL; in parse_btf_map_def()
2572 map_def->parts |= MAP_DEF_MAP_FLAGS; in parse_btf_map_def()
2574 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) in parse_btf_map_def()
2575 return -EINVAL; in parse_btf_map_def()
2576 map_def->parts |= MAP_DEF_NUMA_NODE; in parse_btf_map_def()
2581 return -EINVAL; in parse_btf_map_def()
2582 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2584 map_name, map_def->key_size, sz); in parse_btf_map_def()
2585 return -EINVAL; in parse_btf_map_def()
2587 map_def->key_size = sz; in parse_btf_map_def()
2588 map_def->parts |= MAP_DEF_KEY_SIZE; in parse_btf_map_def()
2592 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2595 map_name, m->type); in parse_btf_map_def()
2596 return -EINVAL; in parse_btf_map_def()
2601 return -EINVAL; in parse_btf_map_def()
2603 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2606 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2609 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2611 map_name, map_def->key_size, (ssize_t)sz); in parse_btf_map_def()
2612 return -EINVAL; in parse_btf_map_def()
2614 map_def->key_size = sz; in parse_btf_map_def()
2615 map_def->key_type_id = t->type; in parse_btf_map_def()
2616 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; in parse_btf_map_def()
2621 return -EINVAL; in parse_btf_map_def()
2622 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2624 map_name, map_def->value_size, sz); in parse_btf_map_def()
2625 return -EINVAL; in parse_btf_map_def()
2627 map_def->value_size = sz; in parse_btf_map_def()
2628 map_def->parts |= MAP_DEF_VALUE_SIZE; in parse_btf_map_def()
2632 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2635 map_name, m->type); in parse_btf_map_def()
2636 return -EINVAL; in parse_btf_map_def()
2641 return -EINVAL; in parse_btf_map_def()
2643 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2646 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2649 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2651 map_name, map_def->value_size, (ssize_t)sz); in parse_btf_map_def()
2652 return -EINVAL; in parse_btf_map_def()
2654 map_def->value_size = sz; in parse_btf_map_def()
2655 map_def->value_type_id = t->type; in parse_btf_map_def()
2656 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; in parse_btf_map_def()
2659 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); in parse_btf_map_def()
2660 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; in parse_btf_map_def()
2661 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; in parse_btf_map_def()
2666 pr_warn("map '%s': multi-level inner maps not supported.\n", in parse_btf_map_def()
2668 return -ENOTSUP; in parse_btf_map_def()
2670 if (i != vlen - 1) { in parse_btf_map_def()
2673 return -EINVAL; in parse_btf_map_def()
2676 pr_warn("map '%s': should be map-in-map or prog-array.\n", in parse_btf_map_def()
2678 return -ENOTSUP; in parse_btf_map_def()
2680 if (map_def->value_size && map_def->value_size != 4) { in parse_btf_map_def()
2682 map_name, map_def->value_size); in parse_btf_map_def()
2683 return -EINVAL; in parse_btf_map_def()
2685 map_def->value_size = 4; in parse_btf_map_def()
2686 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2689 map_name, desc, m->type); in parse_btf_map_def()
2690 return -EINVAL; in parse_btf_map_def()
2692 if (!btf_is_array(t) || btf_array(t)->nelems) { in parse_btf_map_def()
2693 pr_warn("map '%s': %s spec is not a zero-sized array.\n", in parse_btf_map_def()
2695 return -EINVAL; in parse_btf_map_def()
2697 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); in parse_btf_map_def()
2701 return -EINVAL; in parse_btf_map_def()
2703 t = skip_mods_and_typedefs(btf, t->type, NULL); in parse_btf_map_def()
2706 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", in parse_btf_map_def()
2708 return -EINVAL; in parse_btf_map_def()
2713 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", in parse_btf_map_def()
2715 return -EINVAL; in parse_btf_map_def()
2723 map_def->parts |= MAP_DEF_INNER_MAP; in parse_btf_map_def()
2729 return -EINVAL; in parse_btf_map_def()
2732 return -EINVAL; in parse_btf_map_def()
2736 return -EINVAL; in parse_btf_map_def()
2738 map_def->pinning = val; in parse_btf_map_def()
2739 map_def->parts |= MAP_DEF_PINNING; in parse_btf_map_def()
2744 return -EINVAL; in parse_btf_map_def()
2745 map_def->map_extra = map_extra; in parse_btf_map_def()
2746 map_def->parts |= MAP_DEF_MAP_EXTRA; in parse_btf_map_def()
2750 return -ENOTSUP; in parse_btf_map_def()
2756 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { in parse_btf_map_def()
2758 return -EINVAL; in parse_btf_map_def()
2773 * a power-of-2 multiple of kernel's page size. If user diligently in adjust_ringbuf_sz()
2780 * user-set size to satisfy both user size request and kernel in adjust_ringbuf_sz()
2789 * very close to UINT_MAX but is not a power-of-2 multiple of in adjust_ringbuf_sz()
2797 return map->def.type == BPF_MAP_TYPE_RINGBUF || in map_is_ringbuf()
2798 map->def.type == BPF_MAP_TYPE_USER_RINGBUF; in map_is_ringbuf()
2803 map->def.type = def->map_type; in fill_map_from_def()
2804 map->def.key_size = def->key_size; in fill_map_from_def()
2805 map->def.value_size = def->value_size; in fill_map_from_def()
2806 map->def.max_entries = def->max_entries; in fill_map_from_def()
2807 map->def.map_flags = def->map_flags; in fill_map_from_def()
2808 map->map_extra = def->map_extra; in fill_map_from_def()
2810 map->numa_node = def->numa_node; in fill_map_from_def()
2811 map->btf_key_type_id = def->key_type_id; in fill_map_from_def()
2812 map->btf_value_type_id = def->value_type_id; in fill_map_from_def()
2814 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in fill_map_from_def()
2816 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in fill_map_from_def()
2818 if (def->parts & MAP_DEF_MAP_TYPE) in fill_map_from_def()
2819 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); in fill_map_from_def()
2821 if (def->parts & MAP_DEF_KEY_TYPE) in fill_map_from_def()
2823 map->name, def->key_type_id, def->key_size); in fill_map_from_def()
2824 else if (def->parts & MAP_DEF_KEY_SIZE) in fill_map_from_def()
2825 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); in fill_map_from_def()
2827 if (def->parts & MAP_DEF_VALUE_TYPE) in fill_map_from_def()
2829 map->name, def->value_type_id, def->value_size); in fill_map_from_def()
2830 else if (def->parts & MAP_DEF_VALUE_SIZE) in fill_map_from_def()
2831 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); in fill_map_from_def()
2833 if (def->parts & MAP_DEF_MAX_ENTRIES) in fill_map_from_def()
2834 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); in fill_map_from_def()
2835 if (def->parts & MAP_DEF_MAP_FLAGS) in fill_map_from_def()
2836 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); in fill_map_from_def()
2837 if (def->parts & MAP_DEF_MAP_EXTRA) in fill_map_from_def()
2838 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, in fill_map_from_def()
2839 (unsigned long long)def->map_extra); in fill_map_from_def()
2840 if (def->parts & MAP_DEF_PINNING) in fill_map_from_def()
2841 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); in fill_map_from_def()
2842 if (def->parts & MAP_DEF_NUMA_NODE) in fill_map_from_def()
2843 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); in fill_map_from_def()
2845 if (def->parts & MAP_DEF_INNER_MAP) in fill_map_from_def()
2846 pr_debug("map '%s': found inner map definition.\n", map->name); in fill_map_from_def()
2874 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__init_user_btf_map()
2876 map_name = btf__name_by_offset(obj->btf, var->name_off); in bpf_object__init_user_btf_map()
2880 return -EINVAL; in bpf_object__init_user_btf_map()
2882 if ((__u64)vi->offset + vi->size > data->d_size) { in bpf_object__init_user_btf_map()
2884 return -EINVAL; in bpf_object__init_user_btf_map()
2889 return -EINVAL; in bpf_object__init_user_btf_map()
2891 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { in bpf_object__init_user_btf_map()
2893 map_name, btf_var_linkage_str(var_extra->linkage)); in bpf_object__init_user_btf_map()
2894 return -EOPNOTSUPP; in bpf_object__init_user_btf_map()
2897 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__init_user_btf_map()
2901 return -EINVAL; in bpf_object__init_user_btf_map()
2903 if (def->size > vi->size) { in bpf_object__init_user_btf_map()
2905 return -EINVAL; in bpf_object__init_user_btf_map()
2911 map->name = strdup(map_name); in bpf_object__init_user_btf_map()
2912 if (!map->name) { in bpf_object__init_user_btf_map()
2914 return -ENOMEM; in bpf_object__init_user_btf_map()
2916 map->libbpf_type = LIBBPF_MAP_UNSPEC; in bpf_object__init_user_btf_map()
2917 map->def.type = BPF_MAP_TYPE_UNSPEC; in bpf_object__init_user_btf_map()
2918 map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2919 map->sec_offset = vi->offset; in bpf_object__init_user_btf_map()
2920 map->btf_var_idx = var_idx; in bpf_object__init_user_btf_map()
2922 map_name, map->sec_idx, map->sec_offset); in bpf_object__init_user_btf_map()
2924 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); in bpf_object__init_user_btf_map()
2933 pr_warn("map '%s': couldn't build pin path.\n", map->name); in bpf_object__init_user_btf_map()
2939 map->inner_map = calloc(1, sizeof(*map->inner_map)); in bpf_object__init_user_btf_map()
2940 if (!map->inner_map) in bpf_object__init_user_btf_map()
2941 return -ENOMEM; in bpf_object__init_user_btf_map()
2942 map->inner_map->fd = create_placeholder_fd(); in bpf_object__init_user_btf_map()
2943 if (map->inner_map->fd < 0) in bpf_object__init_user_btf_map()
2944 return map->inner_map->fd; in bpf_object__init_user_btf_map()
2945 map->inner_map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2946 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); in bpf_object__init_user_btf_map()
2947 if (!map->inner_map->name) in bpf_object__init_user_btf_map()
2948 return -ENOMEM; in bpf_object__init_user_btf_map()
2949 sprintf(map->inner_map->name, "%s.inner", map_name); in bpf_object__init_user_btf_map()
2951 fill_map_from_def(map->inner_map, &inner_def); in bpf_object__init_user_btf_map()
2968 mmap_sz = bpf_map_mmap_sz(obj->arena_map); in init_arena_map_data()
2972 return -E2BIG; in init_arena_map_data()
2975 obj->arena_data = malloc(data_sz); in init_arena_map_data()
2976 if (!obj->arena_data) in init_arena_map_data()
2977 return -ENOMEM; in init_arena_map_data()
2978 memcpy(obj->arena_data, data, data_sz); in init_arena_map_data()
2979 obj->arena_data_sz = data_sz; in init_arena_map_data()
2982 map->mmaped = obj->arena_data; in init_arena_map_data()
2997 if (obj->efile.btf_maps_shndx < 0) in bpf_object__init_user_btf_maps()
3000 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); in bpf_object__init_user_btf_maps()
3004 MAPS_ELF_SEC, obj->path); in bpf_object__init_user_btf_maps()
3005 return -EINVAL; in bpf_object__init_user_btf_maps()
3008 nr_types = btf__type_cnt(obj->btf); in bpf_object__init_user_btf_maps()
3010 t = btf__type_by_id(obj->btf, i); in bpf_object__init_user_btf_maps()
3013 name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__init_user_btf_maps()
3016 obj->efile.btf_maps_sec_btf_id = i; in bpf_object__init_user_btf_maps()
3023 return -ENOENT; in bpf_object__init_user_btf_maps()
3029 obj->efile.btf_maps_shndx, in bpf_object__init_user_btf_maps()
3036 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__init_user_btf_maps()
3037 struct bpf_map *map = &obj->maps[i]; in bpf_object__init_user_btf_maps()
3039 if (map->def.type != BPF_MAP_TYPE_ARENA) in bpf_object__init_user_btf_maps()
3042 if (obj->arena_map) { in bpf_object__init_user_btf_maps()
3044 map->name, obj->arena_map->name); in bpf_object__init_user_btf_maps()
3045 return -EINVAL; in bpf_object__init_user_btf_maps()
3047 obj->arena_map = map; in bpf_object__init_user_btf_maps()
3049 if (obj->efile.arena_data) { in bpf_object__init_user_btf_maps()
3050 err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx, in bpf_object__init_user_btf_maps()
3051 obj->efile.arena_data->d_buf, in bpf_object__init_user_btf_maps()
3052 obj->efile.arena_data->d_size); in bpf_object__init_user_btf_maps()
3057 if (obj->efile.arena_data && !obj->arena_map) { in bpf_object__init_user_btf_maps()
3060 return -ENOENT; in bpf_object__init_user_btf_maps()
3092 return sh->sh_flags & SHF_EXECINSTR; in section_have_execinstr()
3134 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); in bpf_object__sanitize_btf()
3140 t->size = 1; in bpf_object__sanitize_btf()
3149 name = (char *)btf__name_by_offset(btf, t->name_off); in bpf_object__sanitize_btf()
3157 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); in bpf_object__sanitize_btf()
3160 m->offset = v->offset * 8; in bpf_object__sanitize_btf()
3161 m->type = v->type; in bpf_object__sanitize_btf()
3163 vt = (void *)btf__type_by_id(btf, v->type); in bpf_object__sanitize_btf()
3164 m->name_off = vt->name_off; in bpf_object__sanitize_btf()
3167 starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { in bpf_object__sanitize_btf()
3171 name = (char *)btf__name_by_offset(btf, t->name_off); in bpf_object__sanitize_btf()
3177 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); in bpf_object__sanitize_btf()
3178 t->size = sizeof(__u32); /* kernel enforced */ in bpf_object__sanitize_btf()
3181 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); in bpf_object__sanitize_btf()
3184 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); in bpf_object__sanitize_btf()
3186 /* replace FLOAT with an equally-sized empty STRUCT; in bpf_object__sanitize_btf()
3190 t->name_off = 0; in bpf_object__sanitize_btf()
3191 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); in bpf_object__sanitize_btf()
3194 t->name_off = 0; in bpf_object__sanitize_btf()
3195 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); in bpf_object__sanitize_btf()
3198 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); in bpf_object__sanitize_btf()
3213 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); in bpf_object__sanitize_btf()
3215 m->type = enum64_placeholder_id; in bpf_object__sanitize_btf()
3216 m->offset = 0; in bpf_object__sanitize_btf()
3226 return obj->efile.btf_maps_shndx >= 0 || in libbpf_needs_btf()
3227 obj->efile.has_st_ops || in libbpf_needs_btf()
3228 obj->nr_extern > 0; in libbpf_needs_btf()
3233 return obj->efile.has_st_ops; in kernel_needs_btf()
3240 int err = -ENOENT; in bpf_object__init_btf()
3243 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); in bpf_object__init_btf()
3244 err = libbpf_get_error(obj->btf); in bpf_object__init_btf()
3246 obj->btf = NULL; in bpf_object__init_btf()
3250 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__init_btf()
3251 btf__set_pointer_size(obj->btf, 8); in bpf_object__init_btf()
3257 if (!obj->btf) { in bpf_object__init_btf()
3262 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); in bpf_object__init_btf()
3263 err = libbpf_get_error(obj->btf_ext); in bpf_object__init_btf()
3267 obj->btf_ext = NULL; in bpf_object__init_btf()
3271 /* setup .BTF.ext to ELF section mapping */ in bpf_object__init_btf()
3272 ext_segs[0] = &obj->btf_ext->func_info; in bpf_object__init_btf()
3273 ext_segs[1] = &obj->btf_ext->line_info; in bpf_object__init_btf()
3274 ext_segs[2] = &obj->btf_ext->core_relo_info; in bpf_object__init_btf()
3281 if (seg->sec_cnt == 0) in bpf_object__init_btf()
3284 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); in bpf_object__init_btf()
3285 if (!seg->sec_idxs) { in bpf_object__init_btf()
3286 err = -ENOMEM; in bpf_object__init_btf()
3297 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__init_btf()
3304 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); in bpf_object__init_btf()
3321 return a->offset - b->offset; in compare_vsi_off()
3328 const char *sec_name = btf__name_by_offset(btf, t->name_off); in btf_fixup_datasec()
3335 return -ENOENT; in btf_fixup_datasec()
3338 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and in btf_fixup_datasec()
3350 * to be optional. But the STV_HIDDEN handling is non-optional for any in btf_fixup_datasec()
3351 * non-extern DATASEC, so the variable fixup loop below handles both in btf_fixup_datasec()
3352 * functions at the same time, paying the cost of BTF VAR <-> ELF in btf_fixup_datasec()
3355 if (t->size == 0) { in btf_fixup_datasec()
3360 return -ENOENT; in btf_fixup_datasec()
3363 t->size = size; in btf_fixup_datasec()
3373 t_var = btf__type_by_id(btf, vsi->type); in btf_fixup_datasec()
3375 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); in btf_fixup_datasec()
3376 return -EINVAL; in btf_fixup_datasec()
3380 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) in btf_fixup_datasec()
3383 var_name = btf__name_by_offset(btf, t_var->name_off); in btf_fixup_datasec()
3387 return -ENOENT; in btf_fixup_datasec()
3394 return -ENOENT; in btf_fixup_datasec()
3398 vsi->offset = sym->st_value; in btf_fixup_datasec()
3407 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in btf_fixup_datasec()
3408 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) in btf_fixup_datasec()
3409 var->linkage = BTF_VAR_STATIC; in btf_fixup_datasec()
3421 if (!obj->btf) in bpf_object_fixup_btf()
3424 n = btf__type_cnt(obj->btf); in bpf_object_fixup_btf()
3426 struct btf_type *t = btf_type_by_id(obj->btf, i); in bpf_object_fixup_btf()
3434 err = btf_fixup_datasec(obj, obj->btf, t); in bpf_object_fixup_btf()
3445 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || in prog_needs_vmlinux_btf()
3446 prog->type == BPF_PROG_TYPE_LSM) in prog_needs_vmlinux_btf()
3452 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) in prog_needs_vmlinux_btf()
3469 /* CO-RE relocations need kernel BTF, only when btf_custom_path in obj_needs_vmlinux_btf()
3472 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) in obj_needs_vmlinux_btf()
3476 for (i = 0; i < obj->nr_extern; i++) { in obj_needs_vmlinux_btf()
3477 const struct extern_desc *ext; in obj_needs_vmlinux_btf() local
3479 ext = &obj->externs[i]; in obj_needs_vmlinux_btf()
3480 if (ext->type == EXT_KSYM && ext->ksym.type_id) in obj_needs_vmlinux_btf()
3485 if (!prog->autoload) in obj_needs_vmlinux_btf()
3504 if (obj->btf_vmlinux || obj->gen_loader) in bpf_object__load_vmlinux_btf()
3510 obj->btf_vmlinux = btf__load_vmlinux_btf(); in bpf_object__load_vmlinux_btf()
3511 err = libbpf_get_error(obj->btf_vmlinux); in bpf_object__load_vmlinux_btf()
3514 obj->btf_vmlinux = NULL; in bpf_object__load_vmlinux_btf()
3522 struct btf *kern_btf = obj->btf; in bpf_object__sanitize_and_load_btf()
3526 if (!obj->btf) in bpf_object__sanitize_and_load_btf()
3531 err = -EOPNOTSUPP; in bpf_object__sanitize_and_load_btf()
3546 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sanitize_and_load_btf()
3547 struct bpf_program *prog = &obj->programs[i]; in bpf_object__sanitize_and_load_btf()
3552 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) in bpf_object__sanitize_and_load_btf()
3555 n = btf__type_cnt(obj->btf); in bpf_object__sanitize_and_load_btf()
3557 t = btf_type_by_id(obj->btf, j); in bpf_object__sanitize_and_load_btf()
3561 name = btf__str_by_offset(obj->btf, t->name_off); in bpf_object__sanitize_and_load_btf()
3562 if (strcmp(name, prog->name) != 0) in bpf_object__sanitize_and_load_btf()
3565 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); in bpf_object__sanitize_and_load_btf()
3576 raw_data = btf__raw_data(obj->btf, &sz); in bpf_object__sanitize_and_load_btf()
3582 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__sanitize_and_load_btf()
3583 btf__set_pointer_size(obj->btf, 8); in bpf_object__sanitize_and_load_btf()
3589 if (obj->gen_loader) { in bpf_object__sanitize_and_load_btf()
3594 return -ENOMEM; in bpf_object__sanitize_and_load_btf()
3595 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); in bpf_object__sanitize_and_load_btf()
3597 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. in bpf_object__sanitize_and_load_btf()
3602 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, in bpf_object__sanitize_and_load_btf()
3603 obj->log_level ? 1 : 0, obj->token_fd); in bpf_object__sanitize_and_load_btf()
3608 btf__set_fd(obj->btf, btf__fd(kern_btf)); in bpf_object__sanitize_and_load_btf()
3609 btf__set_fd(kern_btf, -1); in bpf_object__sanitize_and_load_btf()
3632 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); in elf_sym_str()
3635 off, obj->path, elf_errmsg(-1)); in elf_sym_str()
3646 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); in elf_sec_str()
3649 off, obj->path, elf_errmsg(-1)); in elf_sec_str()
3660 scn = elf_getscn(obj->efile.elf, idx); in elf_sec_by_idx()
3663 idx, obj->path, elf_errmsg(-1)); in elf_sec_by_idx()
3672 Elf *elf = obj->efile.elf; in elf_sec_by_name()
3698 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_hdr()
3717 name = elf_sec_str(obj, sh->sh_name); in elf_sec_name()
3720 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_name()
3738 obj->path, elf_errmsg(-1)); in elf_sec_data()
3747 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) in elf_sym_by_idx()
3750 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; in elf_sym_by_idx()
3755 if (idx >= data->d_size / sizeof(Elf64_Rel)) in elf_rel_by_idx()
3758 return (Elf64_Rel *)data->d_buf + idx; in elf_rel_by_idx()
3770 if (hdr->sh_type == SHT_STRTAB) in ignore_elf_section()
3774 if (hdr->sh_type == SHT_LLVM_ADDRSIG) in ignore_elf_section()
3778 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && in ignore_elf_section()
3787 name += sizeof(".rel") - 1; in ignore_elf_section()
3792 /* .BTF and .BTF.ext don't need relocations */ in ignore_elf_section()
3806 if (a->sec_idx != b->sec_idx) in cmp_progs()
3807 return a->sec_idx < b->sec_idx ? -1 : 1; in cmp_progs()
3810 return a->sec_insn_off < b->sec_insn_off ? -1 : 1; in cmp_progs()
3816 Elf *elf = obj->efile.elf; in bpf_object__elf_collect()
3825 /* ELF section indices are 0-based, but sec #0 is special "invalid" in bpf_object__elf_collect()
3830 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { in bpf_object__elf_collect()
3832 obj->path, elf_errmsg(-1)); in bpf_object__elf_collect()
3833 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3835 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); in bpf_object__elf_collect()
3836 if (!obj->efile.secs) in bpf_object__elf_collect()
3837 return -ENOMEM; in bpf_object__elf_collect()
3846 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3848 if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3849 if (obj->efile.symbols) { in bpf_object__elf_collect()
3850 pr_warn("elf: multiple symbol tables in %s\n", obj->path); in bpf_object__elf_collect()
3851 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3856 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3860 obj->efile.symbols = data; in bpf_object__elf_collect()
3861 obj->efile.symbols_shndx = idx; in bpf_object__elf_collect()
3862 obj->efile.strtabidx = sh->sh_link; in bpf_object__elf_collect()
3866 if (!obj->efile.symbols) { in bpf_object__elf_collect()
3868 obj->path); in bpf_object__elf_collect()
3869 return -ENOENT; in bpf_object__elf_collect()
3875 sec_desc = &obj->efile.secs[idx]; in bpf_object__elf_collect()
3879 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3881 name = elf_sec_str(obj, sh->sh_name); in bpf_object__elf_collect()
3883 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3890 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3893 idx, name, (unsigned long)data->d_size, in bpf_object__elf_collect()
3894 (int)sh->sh_link, (unsigned long)sh->sh_flags, in bpf_object__elf_collect()
3895 (int)sh->sh_type); in bpf_object__elf_collect()
3898 err = bpf_object__init_license(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3902 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3907 return -ENOTSUP; in bpf_object__elf_collect()
3909 obj->efile.btf_maps_shndx = idx; in bpf_object__elf_collect()
3911 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3912 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3915 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3916 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3918 } else if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3920 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { in bpf_object__elf_collect()
3921 if (sh->sh_flags & SHF_EXECINSTR) { in bpf_object__elf_collect()
3923 obj->efile.text_shndx = idx; in bpf_object__elf_collect()
3929 sec_desc->sec_type = SEC_DATA; in bpf_object__elf_collect()
3930 sec_desc->shdr = sh; in bpf_object__elf_collect()
3931 sec_desc->data = data; in bpf_object__elf_collect()
3934 sec_desc->sec_type = SEC_RODATA; in bpf_object__elf_collect()
3935 sec_desc->shdr = sh; in bpf_object__elf_collect()
3936 sec_desc->data = data; in bpf_object__elf_collect()
3941 sec_desc->sec_type = SEC_ST_OPS; in bpf_object__elf_collect()
3942 sec_desc->shdr = sh; in bpf_object__elf_collect()
3943 sec_desc->data = data; in bpf_object__elf_collect()
3944 obj->efile.has_st_ops = true; in bpf_object__elf_collect()
3946 obj->efile.arena_data = data; in bpf_object__elf_collect()
3947 obj->efile.arena_data_shndx = idx; in bpf_object__elf_collect()
3952 } else if (sh->sh_type == SHT_REL) { in bpf_object__elf_collect()
3953 int targ_sec_idx = sh->sh_info; /* points to other section */ in bpf_object__elf_collect()
3955 if (sh->sh_entsize != sizeof(Elf64_Rel) || in bpf_object__elf_collect()
3956 targ_sec_idx >= obj->efile.sec_cnt) in bpf_object__elf_collect()
3957 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3972 sec_desc->sec_type = SEC_RELO; in bpf_object__elf_collect()
3973 sec_desc->shdr = sh; in bpf_object__elf_collect()
3974 sec_desc->data = data; in bpf_object__elf_collect()
3975 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || in bpf_object__elf_collect()
3977 sec_desc->sec_type = SEC_BSS; in bpf_object__elf_collect()
3978 sec_desc->shdr = sh; in bpf_object__elf_collect()
3979 sec_desc->data = data; in bpf_object__elf_collect()
3982 (size_t)sh->sh_size); in bpf_object__elf_collect()
3986 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { in bpf_object__elf_collect()
3987 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); in bpf_object__elf_collect()
3988 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3995 /* sort BPF programs by section name and in-section instruction offset in bpf_object__elf_collect()
3998 if (obj->nr_programs) in bpf_object__elf_collect()
3999 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); in bpf_object__elf_collect()
4006 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_extern()
4008 return sym->st_shndx == SHN_UNDEF && in sym_is_extern()
4010 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; in sym_is_extern()
4015 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_subprog()
4016 int type = ELF64_ST_TYPE(sym->st_info); in sym_is_subprog()
4019 if (sym->st_shndx != text_shndx) in sym_is_subprog()
4037 return -ESRCH; in find_extern_btf_id()
4046 tname = btf__name_by_offset(btf, t->name_off); in find_extern_btf_id()
4051 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) in find_extern_btf_id()
4052 return -EINVAL; in find_extern_btf_id()
4055 return -EINVAL; in find_extern_btf_id()
4060 return -ENOENT; in find_extern_btf_id()
4069 return -ESRCH; in find_extern_sec_btf_id()
4080 if (vs->type == ext_btf_id) in find_extern_sec_btf_id()
4085 return -ENOENT; in find_extern_sec_btf_id()
4095 name = btf__name_by_offset(btf, t->name_off); in find_kcfg_type()
4104 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; in find_kcfg_type()
4107 if (t->size == 1) in find_kcfg_type()
4109 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) in find_kcfg_type()
4114 if (t->size != 4) in find_kcfg_type()
4124 if (btf_array(t)->nelems == 0) in find_kcfg_type()
4126 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) in find_kcfg_type()
4139 if (a->type != b->type) in cmp_externs()
4140 return a->type < b->type ? -1 : 1; in cmp_externs()
4142 if (a->type == EXT_KCFG) { in cmp_externs()
4144 if (a->kcfg.align != b->kcfg.align) in cmp_externs()
4145 return a->kcfg.align > b->kcfg.align ? -1 : 1; in cmp_externs()
4147 if (a->kcfg.sz != b->kcfg.sz) in cmp_externs()
4148 return a->kcfg.sz < b->kcfg.sz ? -1 : 1; in cmp_externs()
4152 return strcmp(a->name, b->name); in cmp_externs()
4190 vt = btf__type_by_id(btf, vs->type); in add_dummy_ksym_var()
4214 struct extern_desc *ext; in bpf_object__collect_externs() local
4221 if (!obj->efile.symbols) in bpf_object__collect_externs()
4224 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); in bpf_object__collect_externs()
4226 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) in bpf_object__collect_externs()
4227 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
4229 dummy_var_btf_id = add_dummy_ksym_var(obj->btf); in bpf_object__collect_externs()
4233 n = sh->sh_size / sh->sh_entsize; in bpf_object__collect_externs()
4240 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
4243 ext_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_externs()
4247 ext = obj->externs; in bpf_object__collect_externs()
4248 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); in bpf_object__collect_externs()
4249 if (!ext) in bpf_object__collect_externs()
4250 return -ENOMEM; in bpf_object__collect_externs()
4251 obj->externs = ext; in bpf_object__collect_externs()
4252 ext = &ext[obj->nr_extern]; in bpf_object__collect_externs()
4253 memset(ext, 0, sizeof(*ext)); in bpf_object__collect_externs()
4254 obj->nr_extern++; in bpf_object__collect_externs()
4256 ext->btf_id = find_extern_btf_id(obj->btf, ext_name); in bpf_object__collect_externs()
4257 if (ext->btf_id <= 0) { in bpf_object__collect_externs()
4259 ext_name, ext->btf_id); in bpf_object__collect_externs()
4260 return ext->btf_id; in bpf_object__collect_externs()
4262 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
4263 ext->name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
4264 ext->sym_idx = i; in bpf_object__collect_externs()
4265 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; in bpf_object__collect_externs()
4267 ext_essent_len = bpf_core_essential_name_len(ext->name); in bpf_object__collect_externs()
4268 ext->essent_name = NULL; in bpf_object__collect_externs()
4269 if (ext_essent_len != strlen(ext->name)) { in bpf_object__collect_externs()
4270 ext->essent_name = strndup(ext->name, ext_essent_len); in bpf_object__collect_externs()
4271 if (!ext->essent_name) in bpf_object__collect_externs()
4272 return -ENOMEM; in bpf_object__collect_externs()
4275 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
4276 if (ext->sec_btf_id <= 0) { in bpf_object__collect_externs()
4278 ext_name, ext->btf_id, ext->sec_btf_id); in bpf_object__collect_externs()
4279 return ext->sec_btf_id; in bpf_object__collect_externs()
4281 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); in bpf_object__collect_externs()
4282 sec_name = btf__name_by_offset(obj->btf, sec->name_off); in bpf_object__collect_externs()
4287 ext->name, KCONFIG_SEC); in bpf_object__collect_externs()
4288 return -ENOTSUP; in bpf_object__collect_externs()
4291 ext->type = EXT_KCFG; in bpf_object__collect_externs()
4292 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); in bpf_object__collect_externs()
4293 if (ext->kcfg.sz <= 0) { in bpf_object__collect_externs()
4295 ext_name, ext->kcfg.sz); in bpf_object__collect_externs()
4296 return ext->kcfg.sz; in bpf_object__collect_externs()
4298 ext->kcfg.align = btf__align_of(obj->btf, t->type); in bpf_object__collect_externs()
4299 if (ext->kcfg.align <= 0) { in bpf_object__collect_externs()
4301 ext_name, ext->kcfg.align); in bpf_object__collect_externs()
4302 return -EINVAL; in bpf_object__collect_externs()
4304 ext->kcfg.type = find_kcfg_type(obj->btf, t->type, in bpf_object__collect_externs()
4305 &ext->kcfg.is_signed); in bpf_object__collect_externs()
4306 if (ext->kcfg.type == KCFG_UNKNOWN) { in bpf_object__collect_externs()
4308 return -ENOTSUP; in bpf_object__collect_externs()
4312 ext->type = EXT_KSYM; in bpf_object__collect_externs()
4313 skip_mods_and_typedefs(obj->btf, t->type, in bpf_object__collect_externs()
4314 &ext->ksym.type_id); in bpf_object__collect_externs()
4317 return -ENOTSUP; in bpf_object__collect_externs()
4320 pr_debug("collected %d externs total\n", obj->nr_extern); in bpf_object__collect_externs()
4322 if (!obj->nr_extern) in bpf_object__collect_externs()
4326 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); in bpf_object__collect_externs()
4330 * pretending that each extern is a 8-byte variable in bpf_object__collect_externs()
4333 /* find existing 4-byte integer type in BTF to use for fake in bpf_object__collect_externs()
4336 int int_btf_id = find_int_btf_id(obj->btf); in bpf_object__collect_externs()
4338 * will be used to replace the vs->type and in bpf_object__collect_externs()
4344 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); in bpf_object__collect_externs()
4345 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
4346 ext = &obj->externs[i]; in bpf_object__collect_externs()
4347 if (ext->type != EXT_KSYM) in bpf_object__collect_externs()
4350 i, ext->sym_idx, ext->name); in bpf_object__collect_externs()
4359 vt = (void *)btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
4360 ext_name = btf__name_by_offset(obj->btf, vt->name_off); in bpf_object__collect_externs()
4361 ext = find_extern_by_name(obj, ext_name); in bpf_object__collect_externs()
4362 if (!ext) { in bpf_object__collect_externs()
4365 return -ESRCH; in bpf_object__collect_externs()
4372 func_proto = btf__type_by_id(obj->btf, in bpf_object__collect_externs()
4373 vt->type); in bpf_object__collect_externs()
4381 dummy_var->name_off; in bpf_object__collect_externs()
4382 vs->type = dummy_var_btf_id; in bpf_object__collect_externs()
4383 vt->info &= ~0xffff; in bpf_object__collect_externs()
4384 vt->info |= BTF_FUNC_GLOBAL; in bpf_object__collect_externs()
4386 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
4387 vt->type = int_btf_id; in bpf_object__collect_externs()
4389 vs->offset = off; in bpf_object__collect_externs()
4390 vs->size = sizeof(int); in bpf_object__collect_externs()
4392 sec->size = off; in bpf_object__collect_externs()
4399 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
4400 ext = &obj->externs[i]; in bpf_object__collect_externs()
4401 if (ext->type != EXT_KCFG) in bpf_object__collect_externs()
4404 ext->kcfg.data_off = roundup(off, ext->kcfg.align); in bpf_object__collect_externs()
4405 off = ext->kcfg.data_off + ext->kcfg.sz; in bpf_object__collect_externs()
4407 i, ext->sym_idx, ext->kcfg.data_off, ext->name); in bpf_object__collect_externs()
4409 sec->size = off; in bpf_object__collect_externs()
4414 t = btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
4415 ext_name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
4416 ext = find_extern_by_name(obj, ext_name); in bpf_object__collect_externs()
4417 if (!ext) { in bpf_object__collect_externs()
4420 return -ESRCH; in bpf_object__collect_externs()
4422 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
4423 vs->offset = ext->kcfg.data_off; in bpf_object__collect_externs()
4431 return prog->sec_idx == obj->efile.text_shndx; in prog_is_subprog()
4443 if (!strcmp(prog->name, name)) in bpf_object__find_program_by_name()
4452 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__shndx_is_data()
4465 return shndx == obj->efile.btf_maps_shndx; in bpf_object__shndx_is_maps()
4471 if (shndx == obj->efile.symbols_shndx) in bpf_object__section_to_libbpf_map_type()
4474 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__section_to_libbpf_map_type()
4491 struct bpf_insn *insn = &prog->insns[insn_idx]; in bpf_program__record_reloc()
4492 size_t map_idx, nr_maps = prog->obj->nr_maps; in bpf_program__record_reloc()
4493 struct bpf_object *obj = prog->obj; in bpf_program__record_reloc()
4494 __u32 shdr_idx = sym->st_shndx; in bpf_program__record_reloc()
4501 prog->name, sym_name, insn_idx, insn->code); in bpf_program__record_reloc()
4502 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4506 int sym_idx = ELF64_R_SYM(rel->r_info); in bpf_program__record_reloc()
4507 int i, n = obj->nr_extern; in bpf_program__record_reloc()
4508 struct extern_desc *ext; in bpf_program__record_reloc() local
4511 ext = &obj->externs[i]; in bpf_program__record_reloc()
4512 if (ext->sym_idx == sym_idx) in bpf_program__record_reloc()
4517 prog->name, sym_name, sym_idx); in bpf_program__record_reloc()
4518 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4521 prog->name, i, ext->name, ext->sym_idx, insn_idx); in bpf_program__record_reloc()
4522 if (insn->code == (BPF_JMP | BPF_CALL)) in bpf_program__record_reloc()
4523 reloc_desc->type = RELO_EXTERN_CALL; in bpf_program__record_reloc()
4525 reloc_desc->type = RELO_EXTERN_LD64; in bpf_program__record_reloc()
4526 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4527 reloc_desc->ext_idx = i; in bpf_program__record_reloc()
4531 /* sub-program call relocation */ in bpf_program__record_reloc()
4533 if (insn->src_reg != BPF_PSEUDO_CALL) { in bpf_program__record_reloc()
4534 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); in bpf_program__record_reloc()
4535 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4538 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { in bpf_program__record_reloc()
4541 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4542 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4544 if (sym->st_value % BPF_INSN_SZ) { in bpf_program__record_reloc()
4546 prog->name, sym_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4547 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4549 reloc_desc->type = RELO_CALL; in bpf_program__record_reloc()
4550 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4551 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4557 prog->name, sym_name, shdr_idx); in bpf_program__record_reloc()
4558 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4562 if (sym_is_subprog(sym, obj->efile.text_shndx)) { in bpf_program__record_reloc()
4563 /* global_func: sym->st_value = offset in the section, insn->imm = 0. in bpf_program__record_reloc()
4564 * local_func: sym->st_value = 0, insn->imm = offset in the section. in bpf_program__record_reloc()
4566 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { in bpf_program__record_reloc()
4568 prog->name, sym_name, (size_t)sym->st_value, insn->imm); in bpf_program__record_reloc()
4569 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4572 reloc_desc->type = RELO_SUBPROG_ADDR; in bpf_program__record_reloc()
4573 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4574 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4582 if (shdr_idx == obj->efile.arena_data_shndx) { in bpf_program__record_reloc()
4583 reloc_desc->type = RELO_DATA; in bpf_program__record_reloc()
4584 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4585 reloc_desc->map_idx = obj->arena_map - obj->maps; in bpf_program__record_reloc()
4586 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4594 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4595 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4598 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4599 if (map->libbpf_type != type || in bpf_program__record_reloc()
4600 map->sec_idx != sym->st_shndx || in bpf_program__record_reloc()
4601 map->sec_offset != sym->st_value) in bpf_program__record_reloc()
4604 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4605 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4610 prog->name, sym_sec_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4611 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4613 reloc_desc->type = RELO_LD64; in bpf_program__record_reloc()
4614 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4615 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4616 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ in bpf_program__record_reloc()
4623 prog->name, sym_sec_name); in bpf_program__record_reloc()
4624 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4627 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4628 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) in bpf_program__record_reloc()
4631 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4632 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4637 prog->name, sym_sec_name); in bpf_program__record_reloc()
4638 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4641 reloc_desc->type = RELO_DATA; in bpf_program__record_reloc()
4642 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4643 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4644 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4650 return insn_idx >= prog->sec_insn_off && in prog_contains_insn()
4651 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; in prog_contains_insn()
4657 int l = 0, r = obj->nr_programs - 1, m; in find_prog_by_sec_insn()
4660 if (!obj->nr_programs) in find_prog_by_sec_insn()
4664 m = l + (r - l + 1) / 2; in find_prog_by_sec_insn()
4665 prog = &obj->programs[m]; in find_prog_by_sec_insn()
4667 if (prog->sec_idx < sec_idx || in find_prog_by_sec_insn()
4668 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) in find_prog_by_sec_insn()
4671 r = m - 1; in find_prog_by_sec_insn()
4676 prog = &obj->programs[l]; in find_prog_by_sec_insn()
4677 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) in find_prog_by_sec_insn()
4686 size_t sec_idx = shdr->sh_info, sym_idx; in bpf_object__collect_prog_relos()
4697 if (sec_idx >= obj->efile.sec_cnt) in bpf_object__collect_prog_relos()
4698 return -EINVAL; in bpf_object__collect_prog_relos()
4703 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4705 relo_sec_name = elf_sec_str(obj, shdr->sh_name); in bpf_object__collect_prog_relos()
4708 return -EINVAL; in bpf_object__collect_prog_relos()
4712 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_prog_relos()
4718 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4721 sym_idx = ELF64_R_SYM(rel->r_info); in bpf_object__collect_prog_relos()
4726 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4729 if (sym->st_shndx >= obj->efile.sec_cnt) { in bpf_object__collect_prog_relos()
4731 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); in bpf_object__collect_prog_relos()
4732 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4735 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { in bpf_object__collect_prog_relos()
4737 relo_sec_name, (size_t)rel->r_offset, i); in bpf_object__collect_prog_relos()
4738 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4741 insn_idx = rel->r_offset / BPF_INSN_SZ; in bpf_object__collect_prog_relos()
4748 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) in bpf_object__collect_prog_relos()
4749 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); in bpf_object__collect_prog_relos()
4751 sym_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_prog_relos()
4764 relos = libbpf_reallocarray(prog->reloc_desc, in bpf_object__collect_prog_relos()
4765 prog->nr_reloc + 1, sizeof(*relos)); in bpf_object__collect_prog_relos()
4767 return -ENOMEM; in bpf_object__collect_prog_relos()
4768 prog->reloc_desc = relos; in bpf_object__collect_prog_relos()
4771 insn_idx -= prog->sec_insn_off; in bpf_object__collect_prog_relos()
4772 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], in bpf_object__collect_prog_relos()
4777 prog->nr_reloc++; in bpf_object__collect_prog_relos()
4786 if (!obj->btf) in map_fill_btf_type_info()
4787 return -ENOENT; in map_fill_btf_type_info()
4789 /* if it's BTF-defined map, we don't need to search for type IDs. in map_fill_btf_type_info()
4793 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) in map_fill_btf_type_info()
4801 return -ENOENT; in map_fill_btf_type_info()
4803 id = btf__find_by_name(obj->btf, map->real_name); in map_fill_btf_type_info()
4807 map->btf_key_type_id = 0; in map_fill_btf_type_info()
4808 map->btf_value_type_id = id; in map_fill_btf_type_info()
4824 err = -errno; in bpf_get_map_info_from_fdinfo()
4832 info->type = val; in bpf_get_map_info_from_fdinfo()
4834 info->key_size = val; in bpf_get_map_info_from_fdinfo()
4836 info->value_size = val; in bpf_get_map_info_from_fdinfo()
4838 info->max_entries = val; in bpf_get_map_info_from_fdinfo()
4840 info->map_flags = val; in bpf_get_map_info_from_fdinfo()
4850 return map->autocreate; in bpf_map__autocreate()
4855 if (map->obj->loaded) in bpf_map__set_autocreate()
4856 return libbpf_err(-EBUSY); in bpf_map__set_autocreate()
4858 map->autocreate = autocreate; in bpf_map__set_autocreate()
4865 return libbpf_err(-EINVAL); in bpf_map__set_autoattach()
4867 map->autoattach = autoattach; in bpf_map__set_autoattach()
4873 return map->autoattach; in bpf_map__autoattach()
4891 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) in bpf_map__reuse_fd()
4892 new_name = strdup(map->name); in bpf_map__reuse_fd()
4897 return libbpf_err(-errno); in bpf_map__reuse_fd()
4906 err = -errno; in bpf_map__reuse_fd()
4910 err = reuse_fd(map->fd, new_fd); in bpf_map__reuse_fd()
4914 free(map->name); in bpf_map__reuse_fd()
4916 map->name = new_name; in bpf_map__reuse_fd()
4917 map->def.type = info.type; in bpf_map__reuse_fd()
4918 map->def.key_size = info.key_size; in bpf_map__reuse_fd()
4919 map->def.value_size = info.value_size; in bpf_map__reuse_fd()
4920 map->def.max_entries = info.max_entries; in bpf_map__reuse_fd()
4921 map->def.map_flags = info.map_flags; in bpf_map__reuse_fd()
4922 map->btf_key_type_id = info.btf_key_type_id; in bpf_map__reuse_fd()
4923 map->btf_value_type_id = info.btf_value_type_id; in bpf_map__reuse_fd()
4924 map->reused = true; in bpf_map__reuse_fd()
4925 map->map_extra = info.map_extra; in bpf_map__reuse_fd()
4936 return map->def.max_entries; in bpf_map__max_entries()
4941 if (!bpf_map_type__is_map_in_map(map->def.type)) in bpf_map__inner_map()
4944 return map->inner_map; in bpf_map__inner_map()
4949 if (map->obj->loaded) in bpf_map__set_max_entries()
4950 return libbpf_err(-EBUSY); in bpf_map__set_max_entries()
4952 map->def.max_entries = max_entries; in bpf_map__set_max_entries()
4954 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in bpf_map__set_max_entries()
4956 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in bpf_map__set_max_entries()
4964 int bpffs_fd = -1, token_fd, err; in bpf_object_prepare_token()
4969 if (obj->token_path && obj->token_path[0] == '\0') { in bpf_object_prepare_token()
4970 pr_debug("object '%s': token is prevented, skipping...\n", obj->name); in bpf_object_prepare_token()
4974 mandatory = obj->token_path != NULL; in bpf_object_prepare_token()
4977 bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH; in bpf_object_prepare_token()
4980 err = -errno; in bpf_object_prepare_token()
4982 obj->name, errstr(err), bpffs_path, in bpf_object_prepare_token()
4990 if (!mandatory && token_fd == -ENOENT) { in bpf_object_prepare_token()
4992 obj->name, bpffs_path); in bpf_object_prepare_token()
4996 obj->name, token_fd, bpffs_path, in bpf_object_prepare_token()
5001 obj->feat_cache = calloc(1, sizeof(*obj->feat_cache)); in bpf_object_prepare_token()
5002 if (!obj->feat_cache) { in bpf_object_prepare_token()
5004 return -ENOMEM; in bpf_object_prepare_token()
5007 obj->token_fd = token_fd; in bpf_object_prepare_token()
5008 obj->feat_cache->token_fd = token_fd; in bpf_object_prepare_token()
5022 .token_fd = obj->token_fd, in bpf_object__probe_loading()
5023 .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0, in bpf_object__probe_loading()
5026 if (obj->gen_loader) in bpf_object__probe_loading()
5042 return -ret; in bpf_object__probe_loading()
5051 if (obj->gen_loader) in kernel_supports()
5057 if (obj->token_fd) in kernel_supports()
5058 return feat_supported(obj->feat_cache, feat_id); in kernel_supports()
5079 return (map_info.type == map->def.type && in map_is_reuse_compat()
5080 map_info.key_size == map->def.key_size && in map_is_reuse_compat()
5081 map_info.value_size == map->def.value_size && in map_is_reuse_compat()
5082 map_info.max_entries == map->def.max_entries && in map_is_reuse_compat()
5083 map_info.map_flags == map->def.map_flags && in map_is_reuse_compat()
5084 map_info.map_extra == map->map_extra); in map_is_reuse_compat()
5092 pin_fd = bpf_obj_get(map->pin_path); in bpf_object__reuse_map()
5094 err = -errno; in bpf_object__reuse_map()
5095 if (err == -ENOENT) { in bpf_object__reuse_map()
5097 map->pin_path); in bpf_object__reuse_map()
5102 map->pin_path, errstr(err)); in bpf_object__reuse_map()
5108 map->pin_path); in bpf_object__reuse_map()
5110 return -EINVAL; in bpf_object__reuse_map()
5118 map->pinned = true; in bpf_object__reuse_map()
5119 pr_debug("reused pinned map at '%s'\n", map->pin_path); in bpf_object__reuse_map()
5127 enum libbpf_map_type map_type = map->libbpf_type; in bpf_object__populate_internal_map()
5131 if (obj->gen_loader) { in bpf_object__populate_internal_map()
5132 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, in bpf_object__populate_internal_map()
5133 map->mmaped, map->def.value_size); in bpf_object__populate_internal_map()
5135 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); in bpf_object__populate_internal_map()
5139 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); in bpf_object__populate_internal_map()
5141 err = -errno; in bpf_object__populate_internal_map()
5147 /* Freeze .rodata and .kconfig map as read-only from syscall side. */ in bpf_object__populate_internal_map()
5149 err = bpf_map_freeze(map->fd); in bpf_object__populate_internal_map()
5151 err = -errno; in bpf_object__populate_internal_map()
5152 pr_warn("map '%s': failed to freeze as read-only: %s\n", in bpf_object__populate_internal_map()
5158 /* Remap anonymous mmap()-ed "map initialization image" as in bpf_object__populate_internal_map()
5159 * a BPF map-backed mmap()-ed memory, but preserving the same in bpf_object__populate_internal_map()
5168 if (map->def.map_flags & BPF_F_MMAPABLE) { in bpf_object__populate_internal_map()
5172 if (map->def.map_flags & BPF_F_RDONLY_PROG) in bpf_object__populate_internal_map()
5176 mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map->fd, 0); in bpf_object__populate_internal_map()
5178 err = -errno; in bpf_object__populate_internal_map()
5179 pr_warn("map '%s': failed to re-mmap() contents: %s\n", in bpf_object__populate_internal_map()
5183 map->mmaped = mmaped; in bpf_object__populate_internal_map()
5184 } else if (map->mmaped) { in bpf_object__populate_internal_map()
5185 munmap(map->mmaped, mmap_sz); in bpf_object__populate_internal_map()
5186 map->mmaped = NULL; in bpf_object__populate_internal_map()
5196 return map->obj->loaded || map->reused; in map_is_created()
5202 struct bpf_map_def *def = &map->def; in bpf_object__create_map()
5207 map_name = map->name; in bpf_object__create_map()
5208 create_attr.map_ifindex = map->map_ifindex; in bpf_object__create_map()
5209 create_attr.map_flags = def->map_flags; in bpf_object__create_map()
5210 create_attr.numa_node = map->numa_node; in bpf_object__create_map()
5211 create_attr.map_extra = map->map_extra; in bpf_object__create_map()
5212 create_attr.token_fd = obj->token_fd; in bpf_object__create_map()
5213 if (obj->token_fd) in bpf_object__create_map()
5217 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; in bpf_object__create_map()
5218 if (map->mod_btf_fd >= 0) { in bpf_object__create_map()
5219 create_attr.value_type_btf_obj_fd = map->mod_btf_fd; in bpf_object__create_map()
5224 if (obj->btf && btf__fd(obj->btf) >= 0) { in bpf_object__create_map()
5225 create_attr.btf_fd = btf__fd(obj->btf); in bpf_object__create_map()
5226 create_attr.btf_key_type_id = map->btf_key_type_id; in bpf_object__create_map()
5227 create_attr.btf_value_type_id = map->btf_value_type_id; in bpf_object__create_map()
5230 if (bpf_map_type__is_map_in_map(def->type)) { in bpf_object__create_map()
5231 if (map->inner_map) { in bpf_object__create_map()
5232 err = map_set_def_max_entries(map->inner_map); in bpf_object__create_map()
5235 err = bpf_object__create_map(obj, map->inner_map, true); in bpf_object__create_map()
5238 map->name, errstr(err)); in bpf_object__create_map()
5241 map->inner_map_fd = map->inner_map->fd; in bpf_object__create_map()
5243 if (map->inner_map_fd >= 0) in bpf_object__create_map()
5244 create_attr.inner_map_fd = map->inner_map_fd; in bpf_object__create_map()
5247 switch (def->type) { in bpf_object__create_map()
5265 map->btf_key_type_id = 0; in bpf_object__create_map()
5266 map->btf_value_type_id = 0; in bpf_object__create_map()
5275 if (obj->gen_loader) { in bpf_object__create_map()
5276 bpf_gen__map_create(obj->gen_loader, def->type, map_name, in bpf_object__create_map()
5277 def->key_size, def->value_size, def->max_entries, in bpf_object__create_map()
5278 &create_attr, is_inner ? -1 : map - obj->maps); in bpf_object__create_map()
5283 * will be reset to -1 eventually. in bpf_object__create_map()
5285 map_fd = map->fd; in bpf_object__create_map()
5287 map_fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5288 def->key_size, def->value_size, in bpf_object__create_map()
5289 def->max_entries, &create_attr); in bpf_object__create_map()
5292 err = -errno; in bpf_object__create_map()
5294 map->name, errstr(err)); in bpf_object__create_map()
5298 map->btf_key_type_id = 0; in bpf_object__create_map()
5299 map->btf_value_type_id = 0; in bpf_object__create_map()
5300 map_fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5301 def->key_size, def->value_size, in bpf_object__create_map()
5302 def->max_entries, &create_attr); in bpf_object__create_map()
5305 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { in bpf_object__create_map()
5306 if (obj->gen_loader) in bpf_object__create_map()
5307 map->inner_map->fd = -1; in bpf_object__create_map()
5308 bpf_map__destroy(map->inner_map); in bpf_object__create_map()
5309 zfree(&map->inner_map); in bpf_object__create_map()
5315 /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */ in bpf_object__create_map()
5316 if (map->fd == map_fd) in bpf_object__create_map()
5322 * map->fd stays valid but now point to what map_fd points to. in bpf_object__create_map()
5324 return reuse_fd(map->fd, map_fd); in bpf_object__create_map()
5333 for (i = 0; i < map->init_slots_sz; i++) { in init_map_in_map_slots()
5334 if (!map->init_slots[i]) in init_map_in_map_slots()
5337 targ_map = map->init_slots[i]; in init_map_in_map_slots()
5338 fd = targ_map->fd; in init_map_in_map_slots()
5340 if (obj->gen_loader) { in init_map_in_map_slots()
5341 bpf_gen__populate_outer_map(obj->gen_loader, in init_map_in_map_slots()
5342 map - obj->maps, i, in init_map_in_map_slots()
5343 targ_map - obj->maps); in init_map_in_map_slots()
5345 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_map_in_map_slots()
5348 err = -errno; in init_map_in_map_slots()
5350 map->name, i, targ_map->name, fd, errstr(err)); in init_map_in_map_slots()
5354 map->name, i, targ_map->name, fd); in init_map_in_map_slots()
5357 zfree(&map->init_slots); in init_map_in_map_slots()
5358 map->init_slots_sz = 0; in init_map_in_map_slots()
5369 if (obj->gen_loader) in init_prog_array_slots()
5370 return -ENOTSUP; in init_prog_array_slots()
5372 for (i = 0; i < map->init_slots_sz; i++) { in init_prog_array_slots()
5373 if (!map->init_slots[i]) in init_prog_array_slots()
5376 targ_prog = map->init_slots[i]; in init_prog_array_slots()
5379 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_prog_array_slots()
5381 err = -errno; in init_prog_array_slots()
5383 map->name, i, targ_prog->name, fd, errstr(err)); in init_prog_array_slots()
5387 map->name, i, targ_prog->name, fd); in init_prog_array_slots()
5390 zfree(&map->init_slots); in init_prog_array_slots()
5391 map->init_slots_sz = 0; in init_prog_array_slots()
5401 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_init_prog_arrays()
5402 map = &obj->maps[i]; in bpf_object_init_prog_arrays()
5404 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) in bpf_object_init_prog_arrays()
5416 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { in map_set_def_max_entries()
5422 map->name, nr_cpus); in map_set_def_max_entries()
5425 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); in map_set_def_max_entries()
5426 map->def.max_entries = nr_cpus; in map_set_def_max_entries()
5440 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__create_maps()
5441 map = &obj->maps[i]; in bpf_object__create_maps()
5445 * loading, if we detect that at least one of the to-be-loaded in bpf_object__create_maps()
5450 * but also it allows to have CO-RE applications that use in bpf_object__create_maps()
5452 * If those global variable-using programs are not loaded at in bpf_object__create_maps()
5458 map->autocreate = false; in bpf_object__create_maps()
5460 if (!map->autocreate) { in bpf_object__create_maps()
5461 pr_debug("map '%s': skipped auto-creating...\n", map->name); in bpf_object__create_maps()
5471 if (map->pin_path) { in bpf_object__create_maps()
5475 map->name); in bpf_object__create_maps()
5478 if (retried && map->fd < 0) { in bpf_object__create_maps()
5480 map->name); in bpf_object__create_maps()
5481 err = -ENOENT; in bpf_object__create_maps()
5486 if (map->reused) { in bpf_object__create_maps()
5488 map->name, map->fd); in bpf_object__create_maps()
5495 map->name, map->fd); in bpf_object__create_maps()
5501 } else if (map->def.type == BPF_MAP_TYPE_ARENA) { in bpf_object__create_maps()
5502 map->mmaped = mmap((void *)(long)map->map_extra, in bpf_object__create_maps()
5504 map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED, in bpf_object__create_maps()
5505 map->fd, 0); in bpf_object__create_maps()
5506 if (map->mmaped == MAP_FAILED) { in bpf_object__create_maps()
5507 err = -errno; in bpf_object__create_maps()
5508 map->mmaped = NULL; in bpf_object__create_maps()
5510 map->name, errstr(err)); in bpf_object__create_maps()
5513 if (obj->arena_data) { in bpf_object__create_maps()
5514 memcpy(map->mmaped, obj->arena_data, obj->arena_data_sz); in bpf_object__create_maps()
5515 zfree(&obj->arena_data); in bpf_object__create_maps()
5518 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { in bpf_object__create_maps()
5525 if (map->pin_path && !map->pinned) { in bpf_object__create_maps()
5528 if (!retried && err == -EEXIST) { in bpf_object__create_maps()
5532 pr_warn("map '%s': failed to auto-pin at '%s': %s\n", in bpf_object__create_maps()
5533 map->name, map->pin_path, errstr(err)); in bpf_object__create_maps()
5542 pr_warn("map '%s': failed to create: %s\n", map->name, errstr(err)); in bpf_object__create_maps()
5545 zclose(obj->maps[j].fd); in bpf_object__create_maps()
5559 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5566 for (i = n - 5; i >= 0; i--) { in bpf_core_essential_name_len()
5578 free(cands->cands); in bpf_core_free_cands()
5595 local_t = btf__type_by_id(local_cand->btf, local_cand->id); in bpf_core_add_cands()
5596 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); in bpf_core_add_cands()
5604 targ_name = btf__name_by_offset(targ_btf, t->name_off); in bpf_core_add_cands()
5615 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n", in bpf_core_add_cands()
5616 local_cand->id, btf_kind_str(local_t), in bpf_core_add_cands()
5619 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, in bpf_core_add_cands()
5620 sizeof(*cands->cands)); in bpf_core_add_cands()
5622 return -ENOMEM; in bpf_core_add_cands()
5624 cand = &new_cands[cands->len]; in bpf_core_add_cands()
5625 cand->btf = targ_btf; in bpf_core_add_cands()
5626 cand->id = i; in bpf_core_add_cands()
5628 cands->cands = new_cands; in bpf_core_add_cands()
5629 cands->len++; in bpf_core_add_cands()
5643 if (obj->btf_modules_loaded) in load_module_btfs()
5646 if (obj->gen_loader) in load_module_btfs()
5650 obj->btf_modules_loaded = true; in load_module_btfs()
5665 err = -errno; in load_module_btfs()
5674 err = -errno; in load_module_btfs()
5686 err = -errno; in load_module_btfs()
5691 /* ignore non-module BTFs */ in load_module_btfs()
5697 btf = btf_get_from_fd(fd, obj->btf_vmlinux); in load_module_btfs()
5705 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, in load_module_btfs()
5706 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); in load_module_btfs()
5710 mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; in load_module_btfs()
5712 mod_btf->btf = btf; in load_module_btfs()
5713 mod_btf->id = id; in load_module_btfs()
5714 mod_btf->fd = fd; in load_module_btfs()
5715 mod_btf->name = strdup(name); in load_module_btfs()
5716 if (!mod_btf->name) { in load_module_btfs()
5717 err = -ENOMEM; in load_module_btfs()
5745 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5747 local_name = btf__name_by_offset(local_btf, local_t->name_off); in bpf_core_find_cands()
5749 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5754 return ERR_PTR(-ENOMEM); in bpf_core_find_cands()
5757 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; in bpf_core_find_cands()
5763 if (cands->len) in bpf_core_find_cands()
5767 if (obj->btf_vmlinux_override) in bpf_core_find_cands()
5775 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_core_find_cands()
5777 obj->btf_modules[i].btf, in bpf_core_find_cands()
5778 obj->btf_modules[i].name, in bpf_core_find_cands()
5779 btf__type_cnt(obj->btf_vmlinux), in bpf_core_find_cands()
5792 * type-based CO-RE relocations and follow slightly different rules than
5793 * field-based relocations. This function assumes that root types were already
5794 * checked for name match. Beyond that initial root-level name check, names
5796 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5799 * - for ENUMs, the size is ignored;
5800 * - for INT, size and signedness are ignored;
5801 * - for ARRAY, dimensionality is ignored, element types are checked for
5803 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5804 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5805 * - FUNC_PROTOs are compatible if they have compatible signature: same
5808 * more experience with using BPF CO-RE relocations.
5837 relos = libbpf_reallocarray(prog->reloc_desc, in record_relo_core()
5838 prog->nr_reloc + 1, sizeof(*relos)); in record_relo_core()
5840 return -ENOMEM; in record_relo_core()
5841 relo = &relos[prog->nr_reloc]; in record_relo_core()
5842 relo->type = RELO_CORE; in record_relo_core()
5843 relo->insn_idx = insn_idx; in record_relo_core()
5844 relo->core_relo = core_relo; in record_relo_core()
5845 prog->reloc_desc = relos; in record_relo_core()
5846 prog->nr_reloc++; in record_relo_core()
5855 for (i = 0; i < prog->nr_reloc; i++) { in find_relo_core()
5856 relo = &prog->reloc_desc[i]; in find_relo_core()
5857 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) in find_relo_core()
5860 return relo->core_relo; in find_relo_core()
5875 const char *prog_name = prog->name; in bpf_core_resolve_relo()
5878 __u32 local_id = relo->type_id; in bpf_core_resolve_relo()
5883 return -EINVAL; in bpf_core_resolve_relo()
5885 local_name = btf__name_by_offset(local_btf, local_type->name_off); in bpf_core_resolve_relo()
5887 return -EINVAL; in bpf_core_resolve_relo()
5889 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && in bpf_core_resolve_relo()
5891 cands = bpf_core_find_cands(prog->obj, local_btf, local_id); in bpf_core_resolve_relo()
5923 if (obj->btf_ext->core_relo_info.len == 0) in bpf_object__relocate_core()
5927 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); in bpf_object__relocate_core()
5928 err = libbpf_get_error(obj->btf_vmlinux_override); in bpf_object__relocate_core()
5941 seg = &obj->btf_ext->core_relo_info; in bpf_object__relocate_core()
5944 sec_idx = seg->sec_idxs[sec_num]; in bpf_object__relocate_core()
5947 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__relocate_core()
5949 err = -EINVAL; in bpf_object__relocate_core()
5953 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info); in bpf_object__relocate_core()
5956 if (rec->insn_off % BPF_INSN_SZ) in bpf_object__relocate_core()
5957 return -EINVAL; in bpf_object__relocate_core()
5958 insn_idx = rec->insn_off / BPF_INSN_SZ; in bpf_object__relocate_core()
5963 * appends all the .BTF.ext info that used to belong to that in bpf_object__relocate_core()
5965 * This is similar to what x86-64 linker does for relocations. in bpf_object__relocate_core()
5969 …pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subpro… in bpf_object__relocate_core()
5973 /* no need to apply CO-RE relocation if the program is in bpf_object__relocate_core()
5976 if (!prog->autoload) in bpf_object__relocate_core()
5980 * program's frame of reference; (sub-)program code is not yet in bpf_object__relocate_core()
5981 * relocated, so it's enough to just subtract in-section offset in bpf_object__relocate_core()
5983 insn_idx = insn_idx - prog->sec_insn_off; in bpf_object__relocate_core()
5984 if (insn_idx >= prog->insns_cnt) in bpf_object__relocate_core()
5985 return -EINVAL; in bpf_object__relocate_core()
5986 insn = &prog->insns[insn_idx]; in bpf_object__relocate_core()
5991 prog->name, i, errstr(err)); in bpf_object__relocate_core()
5995 if (prog->obj->gen_loader) in bpf_object__relocate_core()
5998 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); in bpf_object__relocate_core()
6001 prog->name, i, errstr(err)); in bpf_object__relocate_core()
6005 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); in bpf_object__relocate_core()
6008 prog->name, i, insn_idx, errstr(err)); in bpf_object__relocate_core()
6015 /* obj->btf_vmlinux and module BTFs are freed after object load */ in bpf_object__relocate_core()
6016 btf__free(obj->btf_vmlinux_override); in bpf_object__relocate_core()
6017 obj->btf_vmlinux_override = NULL; in bpf_object__relocate_core()
6021 bpf_core_free_cands(entry->pvalue); in bpf_object__relocate_core()
6039 prog->name, relo_idx, insn_idx, map_idx, map->name); in poison_map_ldimm64()
6043 insn->code = BPF_JMP | BPF_CALL; in poison_map_ldimm64()
6044 insn->dst_reg = 0; in poison_map_ldimm64()
6045 insn->src_reg = 0; in poison_map_ldimm64()
6046 insn->off = 0; in poison_map_ldimm64()
6050 * where lower 123 is map index into obj->maps[] array in poison_map_ldimm64()
6052 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; in poison_map_ldimm64()
6064 int ext_idx, const struct extern_desc *ext) in poison_kfunc_call() argument
6067 prog->name, relo_idx, insn_idx, ext->name); in poison_kfunc_call()
6070 insn->code = BPF_JMP | BPF_CALL; in poison_kfunc_call()
6071 insn->dst_reg = 0; in poison_kfunc_call()
6072 insn->src_reg = 0; in poison_kfunc_call()
6073 insn->off = 0; in poison_kfunc_call()
6077 * where lower 123 is extern index into obj->externs[] array in poison_kfunc_call()
6079 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; in poison_kfunc_call()
6083 * - map references;
6084 * - global variable references;
6085 * - extern references.
6092 for (i = 0; i < prog->nr_reloc; i++) { in bpf_object__relocate_data()
6093 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_object__relocate_data()
6094 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate_data()
6096 struct extern_desc *ext; in bpf_object__relocate_data() local
6098 switch (relo->type) { in bpf_object__relocate_data()
6100 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
6101 if (obj->gen_loader) { in bpf_object__relocate_data()
6103 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
6104 } else if (map->autocreate) { in bpf_object__relocate_data()
6106 insn[0].imm = map->fd; in bpf_object__relocate_data()
6108 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6109 relo->map_idx, map); in bpf_object__relocate_data()
6113 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
6114 insn[1].imm = insn[0].imm + relo->sym_off; in bpf_object__relocate_data()
6115 if (obj->gen_loader) { in bpf_object__relocate_data()
6117 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
6118 } else if (map->autocreate) { in bpf_object__relocate_data()
6120 insn[0].imm = map->fd; in bpf_object__relocate_data()
6122 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6123 relo->map_idx, map); in bpf_object__relocate_data()
6127 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
6128 if (ext->type == EXT_KCFG) { in bpf_object__relocate_data()
6129 if (obj->gen_loader) { in bpf_object__relocate_data()
6131 insn[0].imm = obj->kconfig_map_idx; in bpf_object__relocate_data()
6134 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; in bpf_object__relocate_data()
6136 insn[1].imm = ext->kcfg.data_off; in bpf_object__relocate_data()
6138 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ in bpf_object__relocate_data()
6140 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6141 insn[1].imm = ext->ksym.kernel_btf_obj_fd; in bpf_object__relocate_data()
6143 insn[0].imm = (__u32)ext->ksym.addr; in bpf_object__relocate_data()
6144 insn[1].imm = ext->ksym.addr >> 32; in bpf_object__relocate_data()
6149 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
6151 if (ext->is_set) { in bpf_object__relocate_data()
6152 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6153 insn[0].off = ext->ksym.btf_fd_idx; in bpf_object__relocate_data()
6155 poison_kfunc_call(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6156 relo->ext_idx, ext); in bpf_object__relocate_data()
6162 prog->name, i); in bpf_object__relocate_data()
6163 return -EINVAL; in bpf_object__relocate_data()
6175 prog->name, i, relo->type); in bpf_object__relocate_data()
6176 return -EINVAL; in bpf_object__relocate_data()
6197 sec_idx = ext_info->sec_idxs[sec_num]; in adjust_prog_btf_ext_info()
6199 if (prog->sec_idx != sec_idx) in adjust_prog_btf_ext_info()
6205 if (insn_off < prog->sec_insn_off) in adjust_prog_btf_ext_info()
6207 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) in adjust_prog_btf_ext_info()
6212 copy_end = rec + ext_info->rec_size; in adjust_prog_btf_ext_info()
6216 return -ENOENT; in adjust_prog_btf_ext_info()
6218 /* append func/line info of a given (sub-)program to the main in adjust_prog_btf_ext_info()
6221 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; in adjust_prog_btf_ext_info()
6222 new_sz = old_sz + (copy_end - copy_start); in adjust_prog_btf_ext_info()
6225 return -ENOMEM; in adjust_prog_btf_ext_info()
6227 *prog_rec_cnt = new_sz / ext_info->rec_size; in adjust_prog_btf_ext_info()
6228 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); in adjust_prog_btf_ext_info()
6230 /* Kernel instruction offsets are in units of 8-byte in adjust_prog_btf_ext_info()
6231 * instructions, while .BTF.ext instruction offsets generated in adjust_prog_btf_ext_info()
6236 off_adj = prog->sub_insn_off - prog->sec_insn_off; in adjust_prog_btf_ext_info()
6239 for (; rec < rec_end; rec += ext_info->rec_size) { in adjust_prog_btf_ext_info()
6244 *prog_rec_sz = ext_info->rec_size; in adjust_prog_btf_ext_info()
6248 return -ENOENT; in adjust_prog_btf_ext_info()
6258 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't in reloc_prog_func_and_line_info()
6261 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) in reloc_prog_func_and_line_info()
6267 if (main_prog != prog && !main_prog->func_info) in reloc_prog_func_and_line_info()
6270 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, in reloc_prog_func_and_line_info()
6271 &main_prog->func_info, in reloc_prog_func_and_line_info()
6272 &main_prog->func_info_cnt, in reloc_prog_func_and_line_info()
6273 &main_prog->func_info_rec_size); in reloc_prog_func_and_line_info()
6275 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6276 pr_warn("prog '%s': error relocating .BTF.ext function info: %s\n", in reloc_prog_func_and_line_info()
6277 prog->name, errstr(err)); in reloc_prog_func_and_line_info()
6280 if (main_prog->func_info) { in reloc_prog_func_and_line_info()
6285 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); in reloc_prog_func_and_line_info()
6289 …pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext in reloc_prog_func_and_line_info()
6290 prog->name); in reloc_prog_func_and_line_info()
6295 if (main_prog != prog && !main_prog->line_info) in reloc_prog_func_and_line_info()
6298 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, in reloc_prog_func_and_line_info()
6299 &main_prog->line_info, in reloc_prog_func_and_line_info()
6300 &main_prog->line_info_cnt, in reloc_prog_func_and_line_info()
6301 &main_prog->line_info_rec_size); in reloc_prog_func_and_line_info()
6303 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6304 pr_warn("prog '%s': error relocating .BTF.ext line info: %s\n", in reloc_prog_func_and_line_info()
6305 prog->name, errstr(err)); in reloc_prog_func_and_line_info()
6308 if (main_prog->line_info) { in reloc_prog_func_and_line_info()
6313 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); in reloc_prog_func_and_line_info()
6317 …pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line… in reloc_prog_func_and_line_info()
6318 prog->name); in reloc_prog_func_and_line_info()
6328 if (insn_idx == relo->insn_idx) in cmp_relo_by_insn_idx()
6330 return insn_idx < relo->insn_idx ? -1 : 1; in cmp_relo_by_insn_idx()
6335 if (!prog->nr_reloc) in find_prog_insn_relo()
6337 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, in find_prog_insn_relo()
6338 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); in find_prog_insn_relo()
6343 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; in append_subprog_relos()
6349 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); in append_subprog_relos()
6355 return -ENOMEM; in append_subprog_relos()
6356 if (subprog->nr_reloc) in append_subprog_relos()
6357 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, in append_subprog_relos()
6358 sizeof(*relos) * subprog->nr_reloc); in append_subprog_relos()
6360 for (i = main_prog->nr_reloc; i < new_cnt; i++) in append_subprog_relos()
6361 relos[i].insn_idx += subprog->sub_insn_off; in append_subprog_relos()
6365 main_prog->reloc_desc = relos; in append_subprog_relos()
6366 main_prog->nr_reloc = new_cnt; in append_subprog_relos()
6378 subprog->sub_insn_off = main_prog->insns_cnt; in bpf_object__append_subprog_code()
6380 new_cnt = main_prog->insns_cnt + subprog->insns_cnt; in bpf_object__append_subprog_code()
6381 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); in bpf_object__append_subprog_code()
6383 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); in bpf_object__append_subprog_code()
6384 return -ENOMEM; in bpf_object__append_subprog_code()
6386 main_prog->insns = insns; in bpf_object__append_subprog_code()
6387 main_prog->insns_cnt = new_cnt; in bpf_object__append_subprog_code()
6389 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, in bpf_object__append_subprog_code()
6390 subprog->insns_cnt * sizeof(*insns)); in bpf_object__append_subprog_code()
6392 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", in bpf_object__append_subprog_code()
6393 main_prog->name, subprog->insns_cnt, subprog->name); in bpf_object__append_subprog_code()
6416 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { in bpf_object__reloc_code()
6417 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6422 if (relo && relo->type == RELO_EXTERN_CALL) in bpf_object__reloc_code()
6427 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { in bpf_object__reloc_code()
6429 prog->name, insn_idx, relo->type); in bpf_object__reloc_code()
6430 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6433 /* sub-program instruction index is a combination of in bpf_object__reloc_code()
6436 * call always has imm = -1, but for static functions in bpf_object__reloc_code()
6437 * relocation is against STT_SECTION and insn->imm in bpf_object__reloc_code()
6440 * for subprog addr relocation, the relo->sym_off + insn->imm is in bpf_object__reloc_code()
6443 if (relo->type == RELO_CALL) in bpf_object__reloc_code()
6444 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; in bpf_object__reloc_code()
6446 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; in bpf_object__reloc_code()
6453 prog->name, insn_idx); in bpf_object__reloc_code()
6454 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6459 * offset necessary, insns->imm is relative to in bpf_object__reloc_code()
6462 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; in bpf_object__reloc_code()
6465 /* we enforce that sub-programs should be in .text section */ in bpf_object__reloc_code()
6466 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); in bpf_object__reloc_code()
6468 pr_warn("prog '%s': no .text section found yet sub-program call exists\n", in bpf_object__reloc_code()
6469 prog->name); in bpf_object__reloc_code()
6470 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6476 * - append it at the end of main program's instructions blog; in bpf_object__reloc_code()
6477 * - process is recursively, while current program is put on hold; in bpf_object__reloc_code()
6478 * - if that subprogram calls some other not yet processes in bpf_object__reloc_code()
6483 if (subprog->sub_insn_off == 0) { in bpf_object__reloc_code()
6492 /* main_prog->insns memory could have been re-allocated, so in bpf_object__reloc_code()
6495 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6502 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; in bpf_object__reloc_code()
6505 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); in bpf_object__reloc_code()
6512 * Relocate sub-program calls.
6514 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6515 * main prog) is processed separately. For each subprog (non-entry functions,
6524 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6540 * subprog->sub_insn_off as zero at all times and won't be appended to current
6549 * +--------+ +-------+
6551 * +--+---+ +--+-+-+ +---+--+
6553 * +--+---+ +------+ +---+--+
6556 * +---+-------+ +------+----+
6558 * +-----------+ +-----------+
6563 * +-----------+------+
6565 * +-----------+------+
6570 * +-----------+------+------+
6572 * +-----------+------+------+
6581 * +-----------+------+
6583 * +-----------+------+
6586 * +-----------+------+------+
6588 * +-----------+------+------+
6601 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate_calls()
6602 subprog = &obj->programs[i]; in bpf_object__relocate_calls()
6606 subprog->sub_insn_off = 0; in bpf_object__relocate_calls()
6623 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__free_relocs()
6624 prog = &obj->programs[i]; in bpf_object__free_relocs()
6625 zfree(&prog->reloc_desc); in bpf_object__free_relocs()
6626 prog->nr_reloc = 0; in bpf_object__free_relocs()
6635 if (a->insn_idx != b->insn_idx) in cmp_relocs()
6636 return a->insn_idx < b->insn_idx ? -1 : 1; in cmp_relocs()
6639 if (a->type != b->type) in cmp_relocs()
6640 return a->type < b->type ? -1 : 1; in cmp_relocs()
6649 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sort_relos()
6650 struct bpf_program *p = &obj->programs[i]; in bpf_object__sort_relos()
6652 if (!p->nr_reloc) in bpf_object__sort_relos()
6655 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); in bpf_object__sort_relos()
6665 if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG)) in bpf_prog_assign_exc_cb()
6668 n = btf__type_cnt(obj->btf); in bpf_prog_assign_exc_cb()
6673 t = btf_type_by_id(obj->btf, i); in bpf_prog_assign_exc_cb()
6674 if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1) in bpf_prog_assign_exc_cb()
6677 name = btf__str_by_offset(obj->btf, t->name_off); in bpf_prog_assign_exc_cb()
6681 t = btf_type_by_id(obj->btf, t->type); in bpf_prog_assign_exc_cb()
6684 prog->name); in bpf_prog_assign_exc_cb()
6685 return -EINVAL; in bpf_prog_assign_exc_cb()
6687 if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0) in bpf_prog_assign_exc_cb()
6693 if (prog->exception_cb_idx >= 0) { in bpf_prog_assign_exc_cb()
6694 prog->exception_cb_idx = -1; in bpf_prog_assign_exc_cb()
6701 prog->name); in bpf_prog_assign_exc_cb()
6702 return -EINVAL; in bpf_prog_assign_exc_cb()
6705 for (j = 0; j < obj->nr_programs; j++) { in bpf_prog_assign_exc_cb()
6706 struct bpf_program *subprog = &obj->programs[j]; in bpf_prog_assign_exc_cb()
6710 if (strcmp(name, subprog->name) != 0) in bpf_prog_assign_exc_cb()
6712 /* Enforce non-hidden, as from verifier point of in bpf_prog_assign_exc_cb()
6716 if (!subprog->sym_global || subprog->mark_btf_static) { in bpf_prog_assign_exc_cb()
6717 pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n", in bpf_prog_assign_exc_cb()
6718 prog->name, subprog->name); in bpf_prog_assign_exc_cb()
6719 return -EINVAL; in bpf_prog_assign_exc_cb()
6722 if (prog->exception_cb_idx >= 0) { in bpf_prog_assign_exc_cb()
6724 prog->name, subprog->name); in bpf_prog_assign_exc_cb()
6725 return -EINVAL; in bpf_prog_assign_exc_cb()
6727 prog->exception_cb_idx = j; in bpf_prog_assign_exc_cb()
6731 if (prog->exception_cb_idx >= 0) in bpf_prog_assign_exc_cb()
6734 pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name); in bpf_prog_assign_exc_cb()
6735 return -ENOENT; in bpf_prog_assign_exc_cb()
6773 /* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef,
6775 * with this approach we don't need any extra arch-specific #ifdef guards
6798 t = btf__type_by_id(btf, t->type); in need_func_arg_type_fixup()
6800 (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) { in need_func_arg_type_fixup()
6801 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; in need_func_arg_type_fixup()
6807 t = skip_mods_and_typedefs(btf, t->type, NULL); in need_func_arg_type_fixup()
6814 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; in need_func_arg_type_fixup()
6819 switch (prog->type) { in need_func_arg_type_fixup()
6839 if (btf_is_int(t) && t->size == 8) in need_func_arg_type_fixup()
6848 prog->name, subprog_name, arg_idx, ctx_name); in need_func_arg_type_fixup()
6859 /* caller already validated FUNC -> FUNC_PROTO validity */ in clone_func_btf_info()
6861 fn_proto_t = btf_type_by_id(btf, fn_t->type); in clone_func_btf_info()
6872 fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */ in clone_func_btf_info()
6874 orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */ in clone_func_btf_info()
6875 ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */ in clone_func_btf_info()
6881 return -EINVAL; in clone_func_btf_info()
6889 name_off = p->name_off; in clone_func_btf_info()
6891 err = btf__add_func_param(btf, "", p->type); in clone_func_btf_info()
6897 p->name_off = name_off; /* use remembered str offset */ in clone_func_btf_info()
6900 /* clone FUNC now, btf__add_func() enforces non-empty name, so use in clone_func_btf_info()
6904 fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id); in clone_func_btf_info()
6906 return -EINVAL; in clone_func_btf_info()
6909 fn_t->name_off = fn_name_off; /* reuse original string */ in clone_func_btf_info()
6926 struct btf *btf = obj->btf; in bpf_program_fixup_func_info()
6933 /* no .BTF.ext, no problem */ in bpf_program_fixup_func_info()
6934 if (!obj->btf_ext || !prog->func_info) in bpf_program_fixup_func_info()
6945 if (global_ctx_map[i].prog_type != prog->type) in bpf_program_fixup_func_info()
6954 orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids)); in bpf_program_fixup_func_info()
6956 return -ENOMEM; in bpf_program_fixup_func_info()
6957 for (i = 0; i < prog->func_info_cnt; i++) { in bpf_program_fixup_func_info()
6958 func_rec = prog->func_info + prog->func_info_rec_size * i; in bpf_program_fixup_func_info()
6959 orig_ids[i] = func_rec->type_id; in bpf_program_fixup_func_info()
6964 * clone and adjust FUNC -> FUNC_PROTO combo in bpf_program_fixup_func_info()
6971 if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0) in bpf_program_fixup_func_info()
6975 orig_fn_id = t->type; in bpf_program_fixup_func_info()
6980 /* sanity check FUNC -> FUNC_PROTO chain, just in case */ in bpf_program_fixup_func_info()
6981 fn_proto_t = btf_type_by_id(btf, fn_t->type); in bpf_program_fixup_func_info()
6987 for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) { in bpf_program_fixup_func_info()
6988 if (orig_ids[rec_idx] == t->type) { in bpf_program_fixup_func_info()
6989 func_rec = prog->func_info + prog->func_info_rec_size * rec_idx; in bpf_program_fixup_func_info()
6999 arg_idx = btf_decl_tag(t)->component_idx; in bpf_program_fixup_func_info()
7005 fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>"; in bpf_program_fixup_func_info()
7006 if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name)) in bpf_program_fixup_func_info()
7010 if (func_rec->type_id == orig_fn_id) { in bpf_program_fixup_func_info()
7020 func_rec->type_id = fn_id; in bpf_program_fixup_func_info()
7023 /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument; in bpf_program_fixup_func_info()
7025 * funcs share the same program type, so need only PTR -> in bpf_program_fixup_func_info()
7032 err = -EINVAL; in bpf_program_fixup_func_info()
7038 tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx); in bpf_program_fixup_func_info()
7040 err = -EINVAL; in bpf_program_fixup_func_info()
7045 fn_t = btf_type_by_id(btf, func_rec->type_id); in bpf_program_fixup_func_info()
7046 fn_proto_t = btf_type_by_id(btf, fn_t->type); in bpf_program_fixup_func_info()
7050 p->type = ptr_id; in bpf_program_fixup_func_info()
7066 if (obj->btf_ext) { in bpf_object__relocate()
7069 pr_warn("failed to perform CO-RE relocations: %s\n", in bpf_object__relocate()
7076 /* Before relocating calls pre-process relocations and mark in bpf_object__relocate()
7083 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7084 prog = &obj->programs[i]; in bpf_object__relocate()
7085 for (j = 0; j < prog->nr_reloc; j++) { in bpf_object__relocate()
7086 struct reloc_desc *relo = &prog->reloc_desc[j]; in bpf_object__relocate()
7087 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate()
7090 if (relo->type == RELO_SUBPROG_ADDR) in bpf_object__relocate()
7102 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7103 prog = &obj->programs[i]; in bpf_object__relocate()
7104 /* sub-program's sub-calls are relocated within the context of in bpf_object__relocate()
7109 if (!prog->autoload) in bpf_object__relocate()
7115 prog->name, errstr(err)); in bpf_object__relocate()
7123 if (prog->exception_cb_idx >= 0) { in bpf_object__relocate()
7124 struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx]; in bpf_object__relocate()
7131 if (subprog->sub_insn_off == 0) { in bpf_object__relocate()
7141 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7142 prog = &obj->programs[i]; in bpf_object__relocate()
7145 if (!prog->autoload) in bpf_object__relocate()
7152 prog->name, errstr(err)); in bpf_object__relocate()
7156 /* Fix up .BTF.ext information, if necessary */ in bpf_object__relocate()
7159 pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %s\n", in bpf_object__relocate()
7160 prog->name, errstr(err)); in bpf_object__relocate()
7188 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) in bpf_object__collect_map_relos()
7189 return -EINVAL; in bpf_object__collect_map_relos()
7190 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); in bpf_object__collect_map_relos()
7192 return -EINVAL; in bpf_object__collect_map_relos()
7194 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_map_relos()
7199 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
7202 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
7205 i, (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
7206 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
7208 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_map_relos()
7210 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n", in bpf_object__collect_map_relos()
7211 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, in bpf_object__collect_map_relos()
7212 (size_t)rel->r_offset, sym->st_name, name); in bpf_object__collect_map_relos()
7214 for (j = 0; j < obj->nr_maps; j++) { in bpf_object__collect_map_relos()
7215 map = &obj->maps[j]; in bpf_object__collect_map_relos()
7216 if (map->sec_idx != obj->efile.btf_maps_shndx) in bpf_object__collect_map_relos()
7219 vi = btf_var_secinfos(sec) + map->btf_var_idx; in bpf_object__collect_map_relos()
7220 if (vi->offset <= rel->r_offset && in bpf_object__collect_map_relos()
7221 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) in bpf_object__collect_map_relos()
7224 if (j == obj->nr_maps) { in bpf_object__collect_map_relos()
7225 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", in bpf_object__collect_map_relos()
7226 i, name, (size_t)rel->r_offset); in bpf_object__collect_map_relos()
7227 return -EINVAL; in bpf_object__collect_map_relos()
7230 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); in bpf_object__collect_map_relos()
7231 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; in bpf_object__collect_map_relos()
7234 if (sym->st_shndx != obj->efile.btf_maps_shndx) { in bpf_object__collect_map_relos()
7235 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", in bpf_object__collect_map_relos()
7237 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
7239 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && in bpf_object__collect_map_relos()
7240 map->def.key_size != sizeof(int)) { in bpf_object__collect_map_relos()
7241 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", in bpf_object__collect_map_relos()
7242 i, map->name, sizeof(int)); in bpf_object__collect_map_relos()
7243 return -EINVAL; in bpf_object__collect_map_relos()
7249 return -ESRCH; in bpf_object__collect_map_relos()
7256 return -ESRCH; in bpf_object__collect_map_relos()
7258 if (targ_prog->sec_idx != sym->st_shndx || in bpf_object__collect_map_relos()
7259 targ_prog->sec_insn_off * 8 != sym->st_value || in bpf_object__collect_map_relos()
7261 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", in bpf_object__collect_map_relos()
7263 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
7266 return -EINVAL; in bpf_object__collect_map_relos()
7269 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__collect_map_relos()
7270 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__collect_map_relos()
7272 return -EINVAL; in bpf_object__collect_map_relos()
7273 member = btf_members(def) + btf_vlen(def) - 1; in bpf_object__collect_map_relos()
7274 mname = btf__name_by_offset(obj->btf, member->name_off); in bpf_object__collect_map_relos()
7276 return -EINVAL; in bpf_object__collect_map_relos()
7278 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; in bpf_object__collect_map_relos()
7279 if (rel->r_offset - vi->offset < moff) in bpf_object__collect_map_relos()
7280 return -EINVAL; in bpf_object__collect_map_relos()
7282 moff = rel->r_offset - vi->offset - moff; in bpf_object__collect_map_relos()
7287 return -EINVAL; in bpf_object__collect_map_relos()
7289 if (moff >= map->init_slots_sz) { in bpf_object__collect_map_relos()
7291 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); in bpf_object__collect_map_relos()
7293 return -ENOMEM; in bpf_object__collect_map_relos()
7294 map->init_slots = tmp; in bpf_object__collect_map_relos()
7295 memset(map->init_slots + map->init_slots_sz, 0, in bpf_object__collect_map_relos()
7296 (new_sz - map->init_slots_sz) * host_ptr_sz); in bpf_object__collect_map_relos()
7297 map->init_slots_sz = new_sz; in bpf_object__collect_map_relos()
7299 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; in bpf_object__collect_map_relos()
7302 i, map->name, moff, type, name); in bpf_object__collect_map_relos()
7312 for (i = 0; i < obj->efile.sec_cnt; i++) { in bpf_object__collect_relos()
7313 struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; in bpf_object__collect_relos()
7318 if (sec_desc->sec_type != SEC_RELO) in bpf_object__collect_relos()
7321 shdr = sec_desc->shdr; in bpf_object__collect_relos()
7322 data = sec_desc->data; in bpf_object__collect_relos()
7323 idx = shdr->sh_info; in bpf_object__collect_relos()
7325 if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) { in bpf_object__collect_relos()
7327 return -LIBBPF_ERRNO__INTERNAL; in bpf_object__collect_relos()
7330 if (obj->efile.secs[idx].sec_type == SEC_ST_OPS) in bpf_object__collect_relos()
7332 else if (idx == obj->efile.btf_maps_shndx) in bpf_object__collect_relos()
7346 if (BPF_CLASS(insn->code) == BPF_JMP && in insn_is_helper_call()
7347 BPF_OP(insn->code) == BPF_CALL && in insn_is_helper_call()
7348 BPF_SRC(insn->code) == BPF_K && in insn_is_helper_call()
7349 insn->src_reg == 0 && in insn_is_helper_call()
7350 insn->dst_reg == 0) { in insn_is_helper_call()
7351 *func_id = insn->imm; in insn_is_helper_call()
7359 struct bpf_insn *insn = prog->insns; in bpf_object__sanitize_prog()
7363 if (obj->gen_loader) in bpf_object__sanitize_prog()
7366 for (i = 0; i < prog->insns_cnt; i++, insn++) { in bpf_object__sanitize_prog()
7378 insn->imm = BPF_FUNC_probe_read; in bpf_object__sanitize_prog()
7383 insn->imm = BPF_FUNC_probe_read_str; in bpf_object__sanitize_prog()
7395 /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
7402 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) in libbpf_prepare_prog_load()
7403 opts->expected_attach_type = 0; in libbpf_prepare_prog_load()
7406 opts->prog_flags |= BPF_F_SLEEPABLE; in libbpf_prepare_prog_load()
7408 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) in libbpf_prepare_prog_load()
7409 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; in libbpf_prepare_prog_load()
7412 if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) { in libbpf_prepare_prog_load()
7417 prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; in libbpf_prepare_prog_load()
7418 opts->expected_attach_type = BPF_TRACE_UPROBE_MULTI; in libbpf_prepare_prog_load()
7421 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { in libbpf_prepare_prog_load()
7425 attach_name = strchr(prog->sec_name, '/'); in libbpf_prepare_prog_load()
7436 …pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\… in libbpf_prepare_prog_load()
7437 prog->name); in libbpf_prepare_prog_load()
7438 return -EINVAL; in libbpf_prepare_prog_load()
7447 prog->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
7448 prog->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
7451 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because in libbpf_prepare_prog_load()
7455 opts->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
7456 opts->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
7472 __u32 log_level = prog->log_level; in bpf_object_load_prog()
7478 switch (prog->type) { in bpf_object_load_prog()
7485 prog->name, prog->sec_name); in bpf_object_load_prog()
7486 return -EINVAL; in bpf_object_load_prog()
7488 if (prog->attach_btf_id == 0) { in bpf_object_load_prog()
7490 prog->name); in bpf_object_load_prog()
7491 return -EINVAL; in bpf_object_load_prog()
7499 return -EINVAL; in bpf_object_load_prog()
7502 prog_name = prog->name; in bpf_object_load_prog()
7503 load_attr.attach_prog_fd = prog->attach_prog_fd; in bpf_object_load_prog()
7504 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; in bpf_object_load_prog()
7505 load_attr.attach_btf_id = prog->attach_btf_id; in bpf_object_load_prog()
7507 load_attr.prog_ifindex = prog->prog_ifindex; in bpf_object_load_prog()
7508 load_attr.expected_attach_type = prog->expected_attach_type; in bpf_object_load_prog()
7511 if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) { in bpf_object_load_prog()
7512 load_attr.prog_btf_fd = btf__fd(obj->btf); in bpf_object_load_prog()
7513 load_attr.func_info = prog->func_info; in bpf_object_load_prog()
7514 load_attr.func_info_rec_size = prog->func_info_rec_size; in bpf_object_load_prog()
7515 load_attr.func_info_cnt = prog->func_info_cnt; in bpf_object_load_prog()
7516 load_attr.line_info = prog->line_info; in bpf_object_load_prog()
7517 load_attr.line_info_rec_size = prog->line_info_rec_size; in bpf_object_load_prog()
7518 load_attr.line_info_cnt = prog->line_info_cnt; in bpf_object_load_prog()
7521 load_attr.prog_flags = prog->prog_flags; in bpf_object_load_prog()
7522 load_attr.fd_array = obj->fd_array; in bpf_object_load_prog()
7524 load_attr.token_fd = obj->token_fd; in bpf_object_load_prog()
7525 if (obj->token_fd) in bpf_object_load_prog()
7529 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { in bpf_object_load_prog()
7530 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); in bpf_object_load_prog()
7533 prog->name, errstr(err)); in bpf_object_load_prog()
7536 insns = prog->insns; in bpf_object_load_prog()
7537 insns_cnt = prog->insns_cnt; in bpf_object_load_prog()
7540 if (obj->gen_loader) { in bpf_object_load_prog()
7541 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, in bpf_object_load_prog()
7543 prog - obj->programs); in bpf_object_load_prog()
7544 *prog_fd = -1; in bpf_object_load_prog()
7555 if (prog->log_buf) { in bpf_object_load_prog()
7556 log_buf = prog->log_buf; in bpf_object_load_prog()
7557 log_buf_size = prog->log_size; in bpf_object_load_prog()
7559 } else if (obj->log_buf) { in bpf_object_load_prog()
7560 log_buf = obj->log_buf; in bpf_object_load_prog()
7561 log_buf_size = obj->log_size; in bpf_object_load_prog()
7567 ret = -ENOMEM; in bpf_object_load_prog()
7580 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); in bpf_object_load_prog()
7583 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
7584 prog->name, log_buf); in bpf_object_load_prog()
7587 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { in bpf_object_load_prog()
7591 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_load_prog()
7592 map = &prog->obj->maps[i]; in bpf_object_load_prog()
7593 if (map->libbpf_type != LIBBPF_MAP_RODATA) in bpf_object_load_prog()
7596 if (bpf_prog_bind_map(ret, map->fd, NULL)) { in bpf_object_load_prog()
7598 prog->name, map->real_name, errstr(errno)); in bpf_object_load_prog()
7618 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). in bpf_object_load_prog()
7623 ret = -errno; in bpf_object_load_prog()
7625 /* post-process verifier log to improve error descriptions */ in bpf_object_load_prog()
7628 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, errstr(errno)); in bpf_object_load_prog()
7632 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
7633 prog->name, log_buf); in bpf_object_load_prog()
7649 p = cur - 1; in find_prev_line()
7650 while (p - 1 >= buf && *(p - 1) != '\n') in find_prev_line()
7651 p--; in find_prev_line()
7659 /* size of the remaining log content to the right from the to-be-replaced part */ in patch_log()
7660 size_t rem_sz = (buf + log_sz) - (orig + orig_sz); in patch_log()
7665 * shift log contents by (patch_sz - orig_sz) bytes to the right in patch_log()
7666 * starting from after to-be-replaced part of the log. in patch_log()
7669 * shift log contents by (orig_sz - patch_sz) bytes to the left in patch_log()
7670 * starting from after to-be-replaced part of the log in patch_log()
7679 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; in patch_log()
7681 } else if (patch_sz - orig_sz > buf_sz - log_sz) { in patch_log()
7683 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); in patch_log()
7697 /* Expected log for failed and not properly guarded CO-RE relocation: in fixup_log_failed_core_relo()
7698 * line1 -> 123: (85) call unknown#195896080 in fixup_log_failed_core_relo()
7699 * line2 -> invalid func unknown#195896080 in fixup_log_failed_core_relo()
7700 * line3 -> <anything else or end of buffer> in fixup_log_failed_core_relo()
7703 * instruction index to find corresponding CO-RE relocation and in fixup_log_failed_core_relo()
7705 * failed CO-RE relocation. in fixup_log_failed_core_relo()
7719 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); in fixup_log_failed_core_relo()
7725 "%d: <invalid CO-RE relocation>\n" in fixup_log_failed_core_relo()
7726 "failed to resolve CO-RE relocation %s%s\n", in fixup_log_failed_core_relo()
7729 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_failed_core_relo()
7737 * line1 -> 123: (85) call unknown#2001000345 in fixup_log_missing_map_load()
7738 * line2 -> invalid func unknown#2001000345 in fixup_log_missing_map_load()
7739 * line3 -> <anything else or end of buffer> in fixup_log_missing_map_load()
7742 * "345" in "2001000345" is a map index in obj->maps to fetch map name. in fixup_log_missing_map_load()
7744 struct bpf_object *obj = prog->obj; in fixup_log_missing_map_load()
7752 map_idx -= POISON_LDIMM64_MAP_BASE; in fixup_log_missing_map_load()
7753 if (map_idx < 0 || map_idx >= obj->nr_maps) in fixup_log_missing_map_load()
7755 map = &obj->maps[map_idx]; in fixup_log_missing_map_load()
7760 insn_idx, map->name); in fixup_log_missing_map_load()
7762 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_map_load()
7770 * line1 -> 123: (85) call unknown#2002000345 in fixup_log_missing_kfunc_call()
7771 * line2 -> invalid func unknown#2002000345 in fixup_log_missing_kfunc_call()
7772 * line3 -> <anything else or end of buffer> in fixup_log_missing_kfunc_call()
7775 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. in fixup_log_missing_kfunc_call()
7777 struct bpf_object *obj = prog->obj; in fixup_log_missing_kfunc_call()
7778 const struct extern_desc *ext; in fixup_log_missing_kfunc_call() local
7785 ext_idx -= POISON_CALL_KFUNC_BASE; in fixup_log_missing_kfunc_call()
7786 if (ext_idx < 0 || ext_idx >= obj->nr_extern) in fixup_log_missing_kfunc_call()
7788 ext = &obj->externs[ext_idx]; in fixup_log_missing_kfunc_call()
7793 insn_idx, ext->name); in fixup_log_missing_kfunc_call()
7795 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_kfunc_call()
7810 next_line = buf + log_sz - 1; in fixup_verifier_log()
7822 /* failed CO-RE relocation case */ in fixup_verifier_log()
7850 struct bpf_object *obj = prog->obj; in bpf_program_record_relos()
7853 for (i = 0; i < prog->nr_reloc; i++) { in bpf_program_record_relos()
7854 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_program_record_relos()
7855 struct extern_desc *ext = &obj->externs[relo->ext_idx]; in bpf_program_record_relos() local
7858 switch (relo->type) { in bpf_program_record_relos()
7860 if (ext->type != EXT_KSYM) in bpf_program_record_relos()
7862 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? in bpf_program_record_relos()
7864 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7865 ext->is_weak, !ext->ksym.type_id, in bpf_program_record_relos()
7866 true, kind, relo->insn_idx); in bpf_program_record_relos()
7869 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7870 ext->is_weak, false, false, BTF_KIND_FUNC, in bpf_program_record_relos()
7871 relo->insn_idx); in bpf_program_record_relos()
7875 .insn_off = relo->insn_idx * 8, in bpf_program_record_relos()
7876 .type_id = relo->core_relo->type_id, in bpf_program_record_relos()
7877 .access_str_off = relo->core_relo->access_str_off, in bpf_program_record_relos()
7878 .kind = relo->core_relo->kind, in bpf_program_record_relos()
7881 bpf_gen__record_relo_core(obj->gen_loader, &cr); in bpf_program_record_relos()
7898 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7899 prog = &obj->programs[i]; in bpf_object__load_progs()
7905 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7906 prog = &obj->programs[i]; in bpf_object__load_progs()
7909 if (!prog->autoload) { in bpf_object__load_progs()
7910 pr_debug("prog '%s': skipped loading\n", prog->name); in bpf_object__load_progs()
7913 prog->log_level |= log_level; in bpf_object__load_progs()
7915 if (obj->gen_loader) in bpf_object__load_progs()
7918 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, in bpf_object__load_progs()
7919 obj->license, obj->kern_version, &prog->fd); in bpf_object__load_progs()
7921 pr_warn("prog '%s': failed to load: %s\n", prog->name, errstr(err)); in bpf_object__load_progs()
7938 prog->sec_def = find_sec_def(prog->sec_name); in bpf_object_init_progs()
7939 if (!prog->sec_def) { in bpf_object_init_progs()
7942 prog->name, prog->sec_name); in bpf_object_init_progs()
7946 prog->type = prog->sec_def->prog_type; in bpf_object_init_progs()
7947 prog->expected_attach_type = prog->sec_def->expected_attach_type; in bpf_object_init_progs()
7952 if (prog->sec_def->prog_setup_fn) { in bpf_object_init_progs()
7953 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); in bpf_object_init_progs()
7956 prog->name, errstr(err)); in bpf_object_init_progs()
7977 return ERR_PTR(-EINVAL); in bpf_object_open()
7982 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); in bpf_object_open()
7986 return ERR_PTR(-EINVAL); in bpf_object_open()
8000 return ERR_PTR(-EINVAL); in bpf_object_open()
8002 return ERR_PTR(-EINVAL); in bpf_object_open()
8012 return ERR_PTR(-ENAMETOOLONG); in bpf_object_open()
8018 obj->log_buf = log_buf; in bpf_object_open()
8019 obj->log_size = log_size; in bpf_object_open()
8020 obj->log_level = log_level; in bpf_object_open()
8023 obj->token_path = strdup(token_path); in bpf_object_open()
8024 if (!obj->token_path) { in bpf_object_open()
8025 err = -ENOMEM; in bpf_object_open()
8033 err = -ENAMETOOLONG; in bpf_object_open()
8036 obj->btf_custom_path = strdup(btf_tmp_path); in bpf_object_open()
8037 if (!obj->btf_custom_path) { in bpf_object_open()
8038 err = -ENOMEM; in bpf_object_open()
8045 obj->kconfig = strdup(kconfig); in bpf_object_open()
8046 if (!obj->kconfig) { in bpf_object_open()
8047 err = -ENOMEM; in bpf_object_open()
8074 return libbpf_err_ptr(-EINVAL); in bpf_object__open_file()
8091 return libbpf_err_ptr(-EINVAL); in bpf_object__open_mem()
8094 snprintf(tmp_name, sizeof(tmp_name), "%lx-%zx", (unsigned long)obj_buf, obj_buf_sz); in bpf_object__open_mem()
8104 return libbpf_err(-EINVAL); in bpf_object_unload()
8106 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_unload()
8107 zclose(obj->maps[i].fd); in bpf_object_unload()
8108 if (obj->maps[i].st_ops) in bpf_object_unload()
8109 zfree(&obj->maps[i].st_ops->kern_vdata); in bpf_object_unload()
8112 for (i = 0; i < obj->nr_programs; i++) in bpf_object_unload()
8113 bpf_program__unload(&obj->programs[i]); in bpf_object_unload()
8126 m->def.map_flags &= ~BPF_F_MMAPABLE; in bpf_object__sanitize_maps()
8144 err = -errno; in libbpf_kallsyms_parse()
8156 err = -EINVAL; in libbpf_kallsyms_parse()
8174 struct extern_desc *ext; in kallsyms_cb() local
8179 ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name); in kallsyms_cb()
8181 ext = find_extern_by_name(obj, sym_name); in kallsyms_cb()
8182 if (!ext || ext->type != EXT_KSYM) in kallsyms_cb()
8185 t = btf__type_by_id(obj->btf, ext->btf_id); in kallsyms_cb()
8189 if (ext->is_set && ext->ksym.addr != sym_addr) { in kallsyms_cb()
8191 sym_name, ext->ksym.addr, sym_addr); in kallsyms_cb()
8192 return -EINVAL; in kallsyms_cb()
8194 if (!ext->is_set) { in kallsyms_cb()
8195 ext->is_set = true; in kallsyms_cb()
8196 ext->ksym.addr = sym_addr; in kallsyms_cb()
8215 btf = obj->btf_vmlinux; in find_ksym_btf_id()
8219 if (id == -ENOENT) { in find_ksym_btf_id()
8224 for (i = 0; i < obj->btf_module_cnt; i++) { in find_ksym_btf_id()
8226 mod_btf = &obj->btf_modules[i]; in find_ksym_btf_id()
8227 btf = mod_btf->btf; in find_ksym_btf_id()
8229 if (id != -ENOENT) in find_ksym_btf_id()
8234 return -ESRCH; in find_ksym_btf_id()
8242 struct extern_desc *ext) in bpf_object__resolve_ksym_var_btf_id() argument
8251 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); in bpf_object__resolve_ksym_var_btf_id()
8253 if (id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_var_btf_id()
8256 ext->name); in bpf_object__resolve_ksym_var_btf_id()
8261 local_type_id = ext->ksym.type_id; in bpf_object__resolve_ksym_var_btf_id()
8265 targ_var_name = btf__name_by_offset(btf, targ_var->name_off); in bpf_object__resolve_ksym_var_btf_id()
8266 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); in bpf_object__resolve_ksym_var_btf_id()
8268 err = bpf_core_types_are_compat(obj->btf, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
8274 local_type = btf__type_by_id(obj->btf, local_type_id); in bpf_object__resolve_ksym_var_btf_id()
8275 local_name = btf__name_by_offset(obj->btf, local_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
8276 targ_name = btf__name_by_offset(btf, targ_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
8279 ext->name, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
8282 return -EINVAL; in bpf_object__resolve_ksym_var_btf_id()
8285 ext->is_set = true; in bpf_object__resolve_ksym_var_btf_id()
8286 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_var_btf_id()
8287 ext->ksym.kernel_btf_id = id; in bpf_object__resolve_ksym_var_btf_id()
8289 ext->name, id, btf_kind_str(targ_var), targ_var_name); in bpf_object__resolve_ksym_var_btf_id()
8295 struct extern_desc *ext) in bpf_object__resolve_ksym_func_btf_id() argument
8303 local_func_proto_id = ext->ksym.type_id; in bpf_object__resolve_ksym_func_btf_id()
8305 kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf, in bpf_object__resolve_ksym_func_btf_id()
8308 if (kfunc_id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
8311 ext->name); in bpf_object__resolve_ksym_func_btf_id()
8316 kfunc_proto_id = kern_func->type; in bpf_object__resolve_ksym_func_btf_id()
8318 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
8321 if (ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
8325 ext->name, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
8326 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); in bpf_object__resolve_ksym_func_btf_id()
8327 return -EINVAL; in bpf_object__resolve_ksym_func_btf_id()
8331 if (mod_btf && !mod_btf->fd_array_idx) { in bpf_object__resolve_ksym_func_btf_id()
8332 /* insn->off is s16 */ in bpf_object__resolve_ksym_func_btf_id()
8333 if (obj->fd_array_cnt == INT16_MAX) { in bpf_object__resolve_ksym_func_btf_id()
8335 ext->name, mod_btf->fd_array_idx); in bpf_object__resolve_ksym_func_btf_id()
8336 return -E2BIG; in bpf_object__resolve_ksym_func_btf_id()
8339 if (!obj->fd_array_cnt) in bpf_object__resolve_ksym_func_btf_id()
8340 obj->fd_array_cnt = 1; in bpf_object__resolve_ksym_func_btf_id()
8342 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), in bpf_object__resolve_ksym_func_btf_id()
8343 obj->fd_array_cnt + 1); in bpf_object__resolve_ksym_func_btf_id()
8346 mod_btf->fd_array_idx = obj->fd_array_cnt; in bpf_object__resolve_ksym_func_btf_id()
8348 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; in bpf_object__resolve_ksym_func_btf_id()
8351 ext->is_set = true; in bpf_object__resolve_ksym_func_btf_id()
8352 ext->ksym.kernel_btf_id = kfunc_id; in bpf_object__resolve_ksym_func_btf_id()
8353 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; in bpf_object__resolve_ksym_func_btf_id()
8356 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. in bpf_object__resolve_ksym_func_btf_id()
8357 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. in bpf_object__resolve_ksym_func_btf_id()
8359 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_func_btf_id()
8361 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); in bpf_object__resolve_ksym_func_btf_id()
8369 struct extern_desc *ext; in bpf_object__resolve_ksyms_btf_id() local
8372 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_ksyms_btf_id()
8373 ext = &obj->externs[i]; in bpf_object__resolve_ksyms_btf_id()
8374 if (ext->type != EXT_KSYM || !ext->ksym.type_id) in bpf_object__resolve_ksyms_btf_id()
8377 if (obj->gen_loader) { in bpf_object__resolve_ksyms_btf_id()
8378 ext->is_set = true; in bpf_object__resolve_ksyms_btf_id()
8379 ext->ksym.kernel_btf_obj_fd = 0; in bpf_object__resolve_ksyms_btf_id()
8380 ext->ksym.kernel_btf_id = 0; in bpf_object__resolve_ksyms_btf_id()
8383 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__resolve_ksyms_btf_id()
8385 err = bpf_object__resolve_ksym_var_btf_id(obj, ext); in bpf_object__resolve_ksyms_btf_id()
8387 err = bpf_object__resolve_ksym_func_btf_id(obj, ext); in bpf_object__resolve_ksyms_btf_id()
8399 struct extern_desc *ext; in bpf_object__resolve_externs() local
8403 if (obj->nr_extern == 0) in bpf_object__resolve_externs()
8406 if (obj->kconfig_map_idx >= 0) in bpf_object__resolve_externs()
8407 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; in bpf_object__resolve_externs()
8409 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8410 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8412 if (ext->type == EXT_KSYM) { in bpf_object__resolve_externs()
8413 if (ext->ksym.type_id) in bpf_object__resolve_externs()
8418 } else if (ext->type == EXT_KCFG) { in bpf_object__resolve_externs()
8419 void *ext_ptr = kcfg_data + ext->kcfg.data_off; in bpf_object__resolve_externs()
8423 if (str_has_pfx(ext->name, "CONFIG_")) { in bpf_object__resolve_externs()
8429 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { in bpf_object__resolve_externs()
8432 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); in bpf_object__resolve_externs()
8433 return -EINVAL; in bpf_object__resolve_externs()
8435 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { in bpf_object__resolve_externs()
8437 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { in bpf_object__resolve_externs()
8439 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { in bpf_object__resolve_externs()
8447 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); in bpf_object__resolve_externs()
8448 return -EINVAL; in bpf_object__resolve_externs()
8451 err = set_kcfg_value_num(ext, ext_ptr, value); in bpf_object__resolve_externs()
8455 ext->name, (long long)value); in bpf_object__resolve_externs()
8457 pr_warn("extern '%s': unrecognized extern kind\n", ext->name); in bpf_object__resolve_externs()
8458 return -EINVAL; in bpf_object__resolve_externs()
8464 return -EINVAL; in bpf_object__resolve_externs()
8466 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8467 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8468 if (ext->type == EXT_KCFG && !ext->is_set) { in bpf_object__resolve_externs()
8477 return -EINVAL; in bpf_object__resolve_externs()
8482 return -EINVAL; in bpf_object__resolve_externs()
8487 return -EINVAL; in bpf_object__resolve_externs()
8489 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8490 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8492 if (!ext->is_set && !ext->is_weak) { in bpf_object__resolve_externs()
8493 pr_warn("extern '%s' (strong): not resolved\n", ext->name); in bpf_object__resolve_externs()
8494 return -ESRCH; in bpf_object__resolve_externs()
8495 } else if (!ext->is_set) { in bpf_object__resolve_externs()
8497 ext->name); in bpf_object__resolve_externs()
8510 st_ops = map->st_ops; in bpf_map_prepare_vdata()
8511 type = btf__type_by_id(map->obj->btf, st_ops->type_id); in bpf_map_prepare_vdata()
8513 struct bpf_program *prog = st_ops->progs[i]; in bpf_map_prepare_vdata()
8521 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; in bpf_map_prepare_vdata()
8531 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_prepare_struct_ops()
8532 map = &obj->maps[i]; in bpf_object_prepare_struct_ops()
8537 if (!map->autocreate) in bpf_object_prepare_struct_ops()
8551 return libbpf_err(-EINVAL); in bpf_object_load()
8553 if (obj->loaded) { in bpf_object_load()
8554 pr_warn("object '%s': load can't be attempted twice\n", obj->name); in bpf_object_load()
8555 return libbpf_err(-EINVAL); in bpf_object_load()
8558 /* Disallow kernel loading programs of non-native endianness but in bpf_object_load()
8559 * permit cross-endian creation of "light skeleton". in bpf_object_load()
8561 if (obj->gen_loader) { in bpf_object_load()
8562 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); in bpf_object_load()
8564 pr_warn("object '%s': loading non-native endianness is unsupported\n", obj->name); in bpf_object_load()
8565 return libbpf_err(-LIBBPF_ERRNO__ENDIAN); in bpf_object_load()
8571 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); in bpf_object_load()
8575 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); in bpf_object_load()
8582 if (obj->gen_loader) { in bpf_object_load()
8584 if (obj->btf) in bpf_object_load()
8585 btf__set_fd(obj->btf, -1); in bpf_object_load()
8587 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); in bpf_object_load()
8591 zfree(&obj->fd_array); in bpf_object_load()
8594 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_object_load()
8595 close(obj->btf_modules[i].fd); in bpf_object_load()
8596 btf__free(obj->btf_modules[i].btf); in bpf_object_load()
8597 free(obj->btf_modules[i].name); in bpf_object_load()
8599 free(obj->btf_modules); in bpf_object_load()
8602 btf__free(obj->btf_vmlinux); in bpf_object_load()
8603 obj->btf_vmlinux = NULL; in bpf_object_load()
8605 obj->loaded = true; /* doesn't matter if successfully or not */ in bpf_object_load()
8612 /* unpin any maps that were auto-pinned during load */ in bpf_object_load()
8613 for (i = 0; i < obj->nr_maps; i++) in bpf_object_load()
8614 if (obj->maps[i].pinned && !obj->maps[i].reused) in bpf_object_load()
8615 bpf_map__unpin(&obj->maps[i], NULL); in bpf_object_load()
8618 pr_warn("failed to load object '%s'\n", obj->path); in bpf_object_load()
8634 return -ENOMEM; in make_parent_dir()
8638 err = -errno; in make_parent_dir()
8654 return -EINVAL; in check_path()
8658 return -ENOMEM; in check_path()
8663 err = -errno; in check_path()
8669 err = -EINVAL; in check_path()
8679 if (prog->fd < 0) { in bpf_program__pin()
8680 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); in bpf_program__pin()
8681 return libbpf_err(-EINVAL); in bpf_program__pin()
8692 if (bpf_obj_pin(prog->fd, path)) { in bpf_program__pin()
8693 err = -errno; in bpf_program__pin()
8694 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, errstr(err)); in bpf_program__pin()
8698 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); in bpf_program__pin()
8706 if (prog->fd < 0) { in bpf_program__unpin()
8707 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); in bpf_program__unpin()
8708 return libbpf_err(-EINVAL); in bpf_program__unpin()
8717 return libbpf_err(-errno); in bpf_program__unpin()
8719 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); in bpf_program__unpin()
8729 return libbpf_err(-EINVAL); in bpf_map__pin()
8732 if (map->fd < 0) { in bpf_map__pin()
8733 pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map->name); in bpf_map__pin()
8734 return libbpf_err(-EINVAL); in bpf_map__pin()
8737 if (map->pin_path) { in bpf_map__pin()
8738 if (path && strcmp(path, map->pin_path)) { in bpf_map__pin()
8740 bpf_map__name(map), map->pin_path, path); in bpf_map__pin()
8741 return libbpf_err(-EINVAL); in bpf_map__pin()
8742 } else if (map->pinned) { in bpf_map__pin()
8743 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", in bpf_map__pin()
8744 bpf_map__name(map), map->pin_path); in bpf_map__pin()
8751 return libbpf_err(-EINVAL); in bpf_map__pin()
8752 } else if (map->pinned) { in bpf_map__pin()
8754 return libbpf_err(-EEXIST); in bpf_map__pin()
8757 map->pin_path = strdup(path); in bpf_map__pin()
8758 if (!map->pin_path) { in bpf_map__pin()
8759 err = -errno; in bpf_map__pin()
8764 err = make_parent_dir(map->pin_path); in bpf_map__pin()
8768 err = check_path(map->pin_path); in bpf_map__pin()
8772 if (bpf_obj_pin(map->fd, map->pin_path)) { in bpf_map__pin()
8773 err = -errno; in bpf_map__pin()
8777 map->pinned = true; in bpf_map__pin()
8778 pr_debug("pinned map '%s'\n", map->pin_path); in bpf_map__pin()
8793 return libbpf_err(-EINVAL); in bpf_map__unpin()
8796 if (map->pin_path) { in bpf_map__unpin()
8797 if (path && strcmp(path, map->pin_path)) { in bpf_map__unpin()
8799 bpf_map__name(map), map->pin_path, path); in bpf_map__unpin()
8800 return libbpf_err(-EINVAL); in bpf_map__unpin()
8802 path = map->pin_path; in bpf_map__unpin()
8806 return libbpf_err(-EINVAL); in bpf_map__unpin()
8815 return libbpf_err(-errno); in bpf_map__unpin()
8817 map->pinned = false; in bpf_map__unpin()
8830 return libbpf_err(-errno); in bpf_map__set_pin_path()
8833 free(map->pin_path); in bpf_map__set_pin_path()
8834 map->pin_path = new; in bpf_map__set_pin_path()
8843 return map->pin_path; in bpf_map__pin_path()
8848 return map->pinned; in bpf_map__is_pinned()
8867 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8869 if (!obj->loaded) { in bpf_object__pin_maps()
8871 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8878 if (!map->autocreate) in bpf_object__pin_maps()
8887 } else if (!map->pin_path) { in bpf_object__pin_maps()
8900 if (!map->pin_path) in bpf_object__pin_maps()
8915 return libbpf_err(-ENOENT); in bpf_object__unpin_maps()
8927 } else if (!map->pin_path) { in bpf_object__unpin_maps()
8946 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8948 if (!obj->loaded) { in bpf_object__pin_programs()
8950 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8954 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__pin_programs()
8967 if (pathname_concat(buf, sizeof(buf), path, prog->name)) in bpf_object__pin_programs()
8982 return libbpf_err(-ENOENT); in bpf_object__unpin_programs()
8987 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__unpin_programs()
9033 if (map->inner_map) { in bpf_map__destroy()
9034 bpf_map__destroy(map->inner_map); in bpf_map__destroy()
9035 zfree(&map->inner_map); in bpf_map__destroy()
9038 zfree(&map->init_slots); in bpf_map__destroy()
9039 map->init_slots_sz = 0; in bpf_map__destroy()
9041 if (map->mmaped && map->mmaped != map->obj->arena_data) in bpf_map__destroy()
9042 munmap(map->mmaped, bpf_map_mmap_sz(map)); in bpf_map__destroy()
9043 map->mmaped = NULL; in bpf_map__destroy()
9045 if (map->st_ops) { in bpf_map__destroy()
9046 zfree(&map->st_ops->data); in bpf_map__destroy()
9047 zfree(&map->st_ops->progs); in bpf_map__destroy()
9048 zfree(&map->st_ops->kern_func_off); in bpf_map__destroy()
9049 zfree(&map->st_ops); in bpf_map__destroy()
9052 zfree(&map->name); in bpf_map__destroy()
9053 zfree(&map->real_name); in bpf_map__destroy()
9054 zfree(&map->pin_path); in bpf_map__destroy()
9056 if (map->fd >= 0) in bpf_map__destroy()
9057 zclose(map->fd); in bpf_map__destroy()
9067 usdt_manager_free(obj->usdt_man); in bpf_object__close()
9068 obj->usdt_man = NULL; in bpf_object__close()
9070 bpf_gen__free(obj->gen_loader); in bpf_object__close()
9073 btf__free(obj->btf); in bpf_object__close()
9074 btf__free(obj->btf_vmlinux); in bpf_object__close()
9075 btf_ext__free(obj->btf_ext); in bpf_object__close()
9077 for (i = 0; i < obj->nr_maps; i++) in bpf_object__close()
9078 bpf_map__destroy(&obj->maps[i]); in bpf_object__close()
9080 zfree(&obj->btf_custom_path); in bpf_object__close()
9081 zfree(&obj->kconfig); in bpf_object__close()
9083 for (i = 0; i < obj->nr_extern; i++) in bpf_object__close()
9084 zfree(&obj->externs[i].essent_name); in bpf_object__close()
9086 zfree(&obj->externs); in bpf_object__close()
9087 obj->nr_extern = 0; in bpf_object__close()
9089 zfree(&obj->maps); in bpf_object__close()
9090 obj->nr_maps = 0; in bpf_object__close()
9092 if (obj->programs && obj->nr_programs) { in bpf_object__close()
9093 for (i = 0; i < obj->nr_programs; i++) in bpf_object__close()
9094 bpf_program__exit(&obj->programs[i]); in bpf_object__close()
9096 zfree(&obj->programs); in bpf_object__close()
9098 zfree(&obj->feat_cache); in bpf_object__close()
9099 zfree(&obj->token_path); in bpf_object__close()
9100 if (obj->token_fd > 0) in bpf_object__close()
9101 close(obj->token_fd); in bpf_object__close()
9103 zfree(&obj->arena_data); in bpf_object__close()
9110 return obj ? obj->name : libbpf_err_ptr(-EINVAL); in bpf_object__name()
9115 return obj ? obj->kern_version : 0; in bpf_object__kversion()
9120 return obj->token_fd ?: -1; in bpf_object__token_fd()
9125 return obj ? obj->btf : NULL; in bpf_object__btf()
9130 return obj->btf ? btf__fd(obj->btf) : -1; in bpf_object__btf_fd()
9135 if (obj->loaded) in bpf_object__set_kversion()
9136 return libbpf_err(-EINVAL); in bpf_object__set_kversion()
9138 obj->kern_version = kern_version; in bpf_object__set_kversion()
9145 struct bpf_gen *gen; in bpf_object__gen_loader() local
9148 return -EFAULT; in bpf_object__gen_loader()
9150 return -EINVAL; in bpf_object__gen_loader()
9151 gen = calloc(sizeof(*gen), 1); in bpf_object__gen_loader()
9152 if (!gen) in bpf_object__gen_loader()
9153 return -ENOMEM; in bpf_object__gen_loader()
9154 gen->opts = opts; in bpf_object__gen_loader()
9155 gen->swapped_endian = !is_native_endianness(obj); in bpf_object__gen_loader()
9156 obj->gen_loader = gen; in bpf_object__gen_loader()
9164 size_t nr_programs = obj->nr_programs; in __bpf_program__iter()
9172 return forward ? &obj->programs[0] : in __bpf_program__iter()
9173 &obj->programs[nr_programs - 1]; in __bpf_program__iter()
9175 if (p->obj != obj) { in __bpf_program__iter()
9180 idx = (p - obj->programs) + (forward ? 1 : -1); in __bpf_program__iter()
9181 if (idx >= obj->nr_programs || idx < 0) in __bpf_program__iter()
9183 return &obj->programs[idx]; in __bpf_program__iter()
9212 prog->prog_ifindex = ifindex; in bpf_program__set_ifindex()
9217 return prog->name; in bpf_program__name()
9222 return prog->sec_name; in bpf_program__section_name()
9227 return prog->autoload; in bpf_program__autoload()
9232 if (prog->obj->loaded) in bpf_program__set_autoload()
9233 return libbpf_err(-EINVAL); in bpf_program__set_autoload()
9235 prog->autoload = autoload; in bpf_program__set_autoload()
9241 return prog->autoattach; in bpf_program__autoattach()
9246 prog->autoattach = autoattach; in bpf_program__set_autoattach()
9251 return prog->insns; in bpf_program__insns()
9256 return prog->insns_cnt; in bpf_program__insn_cnt()
9264 if (prog->obj->loaded) in bpf_program__set_insns()
9265 return -EBUSY; in bpf_program__set_insns()
9267 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); in bpf_program__set_insns()
9270 pr_warn("prog '%s': failed to realloc prog code\n", prog->name); in bpf_program__set_insns()
9271 return -ENOMEM; in bpf_program__set_insns()
9275 prog->insns = insns; in bpf_program__set_insns()
9276 prog->insns_cnt = new_insn_cnt; in bpf_program__set_insns()
9283 return libbpf_err(-EINVAL); in bpf_program__fd()
9285 if (prog->fd < 0) in bpf_program__fd()
9286 return libbpf_err(-ENOENT); in bpf_program__fd()
9288 return prog->fd; in bpf_program__fd()
9296 return prog->type; in bpf_program__type()
9307 if (prog->obj->loaded) in bpf_program__set_type()
9308 return libbpf_err(-EBUSY); in bpf_program__set_type()
9311 if (prog->type == type) in bpf_program__set_type()
9314 prog->type = type; in bpf_program__set_type()
9318 * fallback handler, which by definition is program type-agnostic and in bpf_program__set_type()
9319 * is a catch-all custom handler, optionally set by the application, in bpf_program__set_type()
9322 if (prog->sec_def != &custom_fallback_def) in bpf_program__set_type()
9323 prog->sec_def = NULL; in bpf_program__set_type()
9332 return prog->expected_attach_type; in bpf_program__expected_attach_type()
9338 if (prog->obj->loaded) in bpf_program__set_expected_attach_type()
9339 return libbpf_err(-EBUSY); in bpf_program__set_expected_attach_type()
9341 prog->expected_attach_type = type; in bpf_program__set_expected_attach_type()
9347 return prog->prog_flags; in bpf_program__flags()
9352 if (prog->obj->loaded) in bpf_program__set_flags()
9353 return libbpf_err(-EBUSY); in bpf_program__set_flags()
9355 prog->prog_flags = flags; in bpf_program__set_flags()
9361 return prog->log_level; in bpf_program__log_level()
9366 if (prog->obj->loaded) in bpf_program__set_log_level()
9367 return libbpf_err(-EBUSY); in bpf_program__set_log_level()
9369 prog->log_level = log_level; in bpf_program__set_log_level()
9375 *log_size = prog->log_size; in bpf_program__log_buf()
9376 return prog->log_buf; in bpf_program__log_buf()
9382 return -EINVAL; in bpf_program__set_log_buf()
9383 if (prog->log_size > UINT_MAX) in bpf_program__set_log_buf()
9384 return -EINVAL; in bpf_program__set_log_buf()
9385 if (prog->obj->loaded) in bpf_program__set_log_buf()
9386 return -EBUSY; in bpf_program__set_log_buf()
9388 prog->log_buf = log_buf; in bpf_program__set_log_buf()
9389 prog->log_size = log_size; in bpf_program__set_log_buf()
9460 SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
9529 return libbpf_err(-EINVAL); in libbpf_register_prog_handler()
9532 return libbpf_err(-E2BIG); in libbpf_register_prog_handler()
9538 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
9544 return libbpf_err(-EBUSY); in libbpf_register_prog_handler()
9549 sec_def->sec = sec ? strdup(sec) : NULL; in libbpf_register_prog_handler()
9550 if (sec && !sec_def->sec) in libbpf_register_prog_handler()
9551 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
9553 sec_def->prog_type = prog_type; in libbpf_register_prog_handler()
9554 sec_def->expected_attach_type = exp_attach_type; in libbpf_register_prog_handler()
9555 sec_def->cookie = OPTS_GET(opts, cookie, 0); in libbpf_register_prog_handler()
9557 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); in libbpf_register_prog_handler()
9558 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); in libbpf_register_prog_handler()
9559 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); in libbpf_register_prog_handler()
9561 sec_def->handler_id = ++last_custom_sec_def_handler_id; in libbpf_register_prog_handler()
9568 return sec_def->handler_id; in libbpf_register_prog_handler()
9577 return libbpf_err(-EINVAL); in libbpf_unregister_prog_handler()
9591 return libbpf_err(-ENOENT); in libbpf_unregister_prog_handler()
9595 custom_sec_defs[i - 1] = custom_sec_defs[i]; in libbpf_unregister_prog_handler()
9596 custom_sec_def_cnt--; in libbpf_unregister_prog_handler()
9612 size_t len = strlen(sec_def->sec); in sec_def_matches()
9615 if (sec_def->sec[len - 1] == '/') { in sec_def_matches()
9616 if (str_has_pfx(sec_name, sec_def->sec)) in sec_def_matches()
9622 * well-formed SEC("type/extras") with proper '/' separator in sec_def_matches()
9624 if (sec_def->sec[len - 1] == '+') { in sec_def_matches()
9625 len--; in sec_def_matches()
9627 if (strncmp(sec_name, sec_def->sec, len) != 0) in sec_def_matches()
9635 return strcmp(sec_name, sec_def->sec) == 0; in sec_def_matches()
9680 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_get_type_names()
9683 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_get_type_names()
9705 return libbpf_err(-EINVAL); in libbpf_prog_type_by_name()
9709 *prog_type = sec_def->prog_type; in libbpf_prog_type_by_name()
9710 *expected_attach_type = sec_def->expected_attach_type; in libbpf_prog_type_by_name()
9721 return libbpf_err(-ESRCH); in libbpf_prog_type_by_name()
9763 for (i = 0; i < obj->nr_maps; i++) { in find_struct_ops_map_by_offset()
9764 map = &obj->maps[i]; in find_struct_ops_map_by_offset()
9767 if (map->sec_idx == sec_idx && in find_struct_ops_map_by_offset()
9768 map->sec_offset <= offset && in find_struct_ops_map_by_offset()
9769 offset - map->sec_offset < map->def.value_size) in find_struct_ops_map_by_offset()
9776 /* Collect the reloc from ELF, populate the st_ops->progs[], and update
9777 * st_ops->data for shadow type.
9796 btf = obj->btf; in bpf_object__collect_st_ops_relos()
9797 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_st_ops_relos()
9802 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9805 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9808 (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9809 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9812 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_st_ops_relos()
9813 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); in bpf_object__collect_st_ops_relos()
9815 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", in bpf_object__collect_st_ops_relos()
9816 (size_t)rel->r_offset); in bpf_object__collect_st_ops_relos()
9817 return -EINVAL; in bpf_object__collect_st_ops_relos()
9820 moff = rel->r_offset - map->sec_offset; in bpf_object__collect_st_ops_relos()
9821 shdr_idx = sym->st_shndx; in bpf_object__collect_st_ops_relos()
9822 st_ops = map->st_ops; in bpf_object__collect_st_ops_relos()
9823 …pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %… in bpf_object__collect_st_ops_relos()
9824 map->name, in bpf_object__collect_st_ops_relos()
9825 (long long)(rel->r_info >> 32), in bpf_object__collect_st_ops_relos()
9826 (long long)sym->st_value, in bpf_object__collect_st_ops_relos()
9827 shdr_idx, (size_t)rel->r_offset, in bpf_object__collect_st_ops_relos()
9828 map->sec_offset, sym->st_name, name); in bpf_object__collect_st_ops_relos()
9831 pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n", in bpf_object__collect_st_ops_relos()
9832 map->name, (size_t)rel->r_offset, shdr_idx); in bpf_object__collect_st_ops_relos()
9833 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_st_ops_relos()
9835 if (sym->st_value % BPF_INSN_SZ) { in bpf_object__collect_st_ops_relos()
9837 map->name, (unsigned long long)sym->st_value); in bpf_object__collect_st_ops_relos()
9838 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9840 insn_idx = sym->st_value / BPF_INSN_SZ; in bpf_object__collect_st_ops_relos()
9842 type = btf__type_by_id(btf, st_ops->type_id); in bpf_object__collect_st_ops_relos()
9846 map->name, moff); in bpf_object__collect_st_ops_relos()
9847 return -EINVAL; in bpf_object__collect_st_ops_relos()
9849 member_idx = member - btf_members(type); in bpf_object__collect_st_ops_relos()
9850 name = btf__name_by_offset(btf, member->name_off); in bpf_object__collect_st_ops_relos()
9852 if (!resolve_func_ptr(btf, member->type, NULL)) { in bpf_object__collect_st_ops_relos()
9854 map->name, name); in bpf_object__collect_st_ops_relos()
9855 return -EINVAL; in bpf_object__collect_st_ops_relos()
9861 map->name, shdr_idx, name); in bpf_object__collect_st_ops_relos()
9862 return -EINVAL; in bpf_object__collect_st_ops_relos()
9866 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { in bpf_object__collect_st_ops_relos()
9868 map->name, prog->name); in bpf_object__collect_st_ops_relos()
9869 return -EINVAL; in bpf_object__collect_st_ops_relos()
9872 st_ops->progs[member_idx] = prog; in bpf_object__collect_st_ops_relos()
9874 /* st_ops->data will be exposed to users, being returned by in bpf_object__collect_st_ops_relos()
9880 *((struct bpf_program **)(st_ops->data + moff)) = prog; in bpf_object__collect_st_ops_relos()
9927 return -ENAMETOOLONG; in find_btf_by_prefix_kind()
9977 err = -EINVAL; in libbpf_find_prog_btf_id()
10008 mod_len = fn_name - mod_name; in find_kernel_btf_id()
10013 ret = find_attach_btf_id(obj->btf_vmlinux, in find_kernel_btf_id()
10021 if (ret != -ENOENT) in find_kernel_btf_id()
10029 for (i = 0; i < obj->btf_module_cnt; i++) { in find_kernel_btf_id()
10030 const struct module_btf *mod = &obj->btf_modules[i]; in find_kernel_btf_id()
10032 if (mod_name && strncmp(mod->name, mod_name, mod_len) != 0) in find_kernel_btf_id()
10035 ret = find_attach_btf_id(mod->btf, in find_kernel_btf_id()
10039 *btf_obj_fd = mod->fd; in find_kernel_btf_id()
10043 if (ret == -ENOENT) in find_kernel_btf_id()
10049 return -ESRCH; in find_kernel_btf_id()
10055 enum bpf_attach_type attach_type = prog->expected_attach_type; in libbpf_find_attach_btf_id()
10056 __u32 attach_prog_fd = prog->attach_prog_fd; in libbpf_find_attach_btf_id()
10060 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { in libbpf_find_attach_btf_id()
10062 pr_warn("prog '%s': attach program FD is not set\n", prog->name); in libbpf_find_attach_btf_id()
10063 return -EINVAL; in libbpf_find_attach_btf_id()
10068 prog->name, attach_prog_fd, attach_name, errstr(err)); in libbpf_find_attach_btf_id()
10077 if (prog->obj->gen_loader) { in libbpf_find_attach_btf_id()
10078 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); in libbpf_find_attach_btf_id()
10082 err = find_kernel_btf_id(prog->obj, attach_name, in libbpf_find_attach_btf_id()
10088 prog->name, attach_name, errstr(err)); in libbpf_find_attach_btf_id()
10101 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10112 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10115 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_attach_type_by_name()
10116 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10117 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_attach_type_by_name()
10118 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10120 *attach_type = sec_def->expected_attach_type; in libbpf_attach_type_by_name()
10127 return libbpf_err(-EINVAL); in bpf_map__fd()
10129 return -1; in bpf_map__fd()
10130 return map->fd; in bpf_map__fd()
10136 * their user-visible name differs from kernel-visible name. Users see in map_uses_real_name()
10141 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) in map_uses_real_name()
10143 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) in map_uses_real_name()
10154 return map->real_name; in bpf_map__name()
10156 return map->name; in bpf_map__name()
10161 return map->def.type; in bpf_map__type()
10167 return libbpf_err(-EBUSY); in bpf_map__set_type()
10168 map->def.type = type; in bpf_map__set_type()
10174 return map->def.map_flags; in bpf_map__map_flags()
10180 return libbpf_err(-EBUSY); in bpf_map__set_map_flags()
10181 map->def.map_flags = flags; in bpf_map__set_map_flags()
10187 return map->map_extra; in bpf_map__map_extra()
10193 return libbpf_err(-EBUSY); in bpf_map__set_map_extra()
10194 map->map_extra = map_extra; in bpf_map__set_map_extra()
10200 return map->numa_node; in bpf_map__numa_node()
10206 return libbpf_err(-EBUSY); in bpf_map__set_numa_node()
10207 map->numa_node = numa_node; in bpf_map__set_numa_node()
10213 return map->def.key_size; in bpf_map__key_size()
10219 return libbpf_err(-EBUSY); in bpf_map__set_key_size()
10220 map->def.key_size = size; in bpf_map__set_key_size()
10226 return map->def.value_size; in bpf_map__value_size()
10240 btf = bpf_object__btf(map->obj); in map_btf_datasec_resize()
10242 return -ENOENT; in map_btf_datasec_resize()
10249 return -EINVAL; in map_btf_datasec_resize()
10257 return -EINVAL; in map_btf_datasec_resize()
10261 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
10262 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
10263 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); in map_btf_datasec_resize()
10267 return -EINVAL; in map_btf_datasec_resize()
10272 element_sz = btf__resolve_size(btf, array->type); in map_btf_datasec_resize()
10273 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { in map_btf_datasec_resize()
10276 return -EINVAL; in map_btf_datasec_resize()
10280 nr_elements = (size - var->offset) / element_sz; in map_btf_datasec_resize()
10281 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); in map_btf_datasec_resize()
10288 datasec_type = btf_type_by_id(btf, map->btf_value_type_id); in map_btf_datasec_resize()
10289 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
10290 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
10293 datasec_type->size = size; in map_btf_datasec_resize()
10294 var->size = size - var->offset; in map_btf_datasec_resize()
10295 var_type->type = new_array_id; in map_btf_datasec_resize()
10302 if (map->obj->loaded || map->reused) in bpf_map__set_value_size()
10303 return libbpf_err(-EBUSY); in bpf_map__set_value_size()
10305 if (map->mmaped) { in bpf_map__set_value_size()
10309 if (map->def.type != BPF_MAP_TYPE_ARRAY) in bpf_map__set_value_size()
10310 return -EOPNOTSUPP; in bpf_map__set_value_size()
10313 mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries); in bpf_map__set_value_size()
10316 pr_warn("map '%s': failed to resize memory-mapped region: %s\n", in bpf_map__set_value_size()
10321 if (err && err != -ENOENT) { in bpf_map__set_value_size()
10324 map->btf_value_type_id = 0; in bpf_map__set_value_size()
10325 map->btf_key_type_id = 0; in bpf_map__set_value_size()
10329 map->def.value_size = size; in bpf_map__set_value_size()
10335 return map ? map->btf_key_type_id : 0; in bpf_map__btf_key_type_id()
10340 return map ? map->btf_value_type_id : 0; in bpf_map__btf_value_type_id()
10348 if (map->obj->loaded || map->reused) in bpf_map__set_initial_value()
10349 return libbpf_err(-EBUSY); in bpf_map__set_initial_value()
10351 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG) in bpf_map__set_initial_value()
10352 return libbpf_err(-EINVAL); in bpf_map__set_initial_value()
10354 if (map->def.type == BPF_MAP_TYPE_ARENA) in bpf_map__set_initial_value()
10355 actual_sz = map->obj->arena_data_sz; in bpf_map__set_initial_value()
10357 actual_sz = map->def.value_size; in bpf_map__set_initial_value()
10359 return libbpf_err(-EINVAL); in bpf_map__set_initial_value()
10361 memcpy(map->mmaped, data, size); in bpf_map__set_initial_value()
10369 *psize = map->def.value_size; in bpf_map__initial_value()
10370 return map->st_ops->data; in bpf_map__initial_value()
10373 if (!map->mmaped) in bpf_map__initial_value()
10376 if (map->def.type == BPF_MAP_TYPE_ARENA) in bpf_map__initial_value()
10377 *psize = map->obj->arena_data_sz; in bpf_map__initial_value()
10379 *psize = map->def.value_size; in bpf_map__initial_value()
10381 return map->mmaped; in bpf_map__initial_value()
10386 return map->libbpf_type != LIBBPF_MAP_UNSPEC; in bpf_map__is_internal()
10391 return map->map_ifindex; in bpf_map__ifindex()
10397 return libbpf_err(-EBUSY); in bpf_map__set_ifindex()
10398 map->map_ifindex = ifindex; in bpf_map__set_ifindex()
10404 if (!bpf_map_type__is_map_in_map(map->def.type)) { in bpf_map__set_inner_map_fd()
10406 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
10408 if (map->inner_map_fd != -1) { in bpf_map__set_inner_map_fd()
10410 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
10412 if (map->inner_map) { in bpf_map__set_inner_map_fd()
10413 bpf_map__destroy(map->inner_map); in bpf_map__set_inner_map_fd()
10414 zfree(&map->inner_map); in bpf_map__set_inner_map_fd()
10416 map->inner_map_fd = fd; in bpf_map__set_inner_map_fd()
10426 if (!obj || !obj->maps) in __bpf_map__iter()
10429 s = obj->maps; in __bpf_map__iter()
10430 e = obj->maps + obj->nr_maps; in __bpf_map__iter()
10438 idx = (m - obj->maps) + i; in __bpf_map__iter()
10439 if (idx >= obj->nr_maps || idx < 0) in __bpf_map__iter()
10441 return &obj->maps[idx]; in __bpf_map__iter()
10448 return obj->maps; in bpf_object__next_map()
10457 if (!obj->nr_maps) in bpf_object__prev_map()
10459 return obj->maps + obj->nr_maps - 1; in bpf_object__prev_map()
10462 return __bpf_map__iter(next, obj, -1); in bpf_object__prev_map()
10476 if (pos->real_name && strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
10482 if (strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
10486 if (strcmp(pos->name, name) == 0) in bpf_object__find_map_by_name()
10502 return -ENOENT; in validate_map_op()
10504 if (map->def.key_size != key_sz) { in validate_map_op()
10506 map->name, key_sz, map->def.key_size); in validate_map_op()
10507 return -EINVAL; in validate_map_op()
10510 if (map->fd < 0) { in validate_map_op()
10511 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); in validate_map_op()
10512 return -EINVAL; in validate_map_op()
10518 switch (map->def.type) { in validate_map_op()
10524 size_t elem_sz = roundup(map->def.value_size, 8); in validate_map_op()
10527 … pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n", in validate_map_op()
10528 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); in validate_map_op()
10529 return -EINVAL; in validate_map_op()
10534 if (map->def.value_size != value_sz) { in validate_map_op()
10536 map->name, value_sz, map->def.value_size); in validate_map_op()
10537 return -EINVAL; in validate_map_op()
10554 return bpf_map_lookup_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_elem()
10567 return bpf_map_update_elem(map->fd, key, value, flags); in bpf_map__update_elem()
10579 return bpf_map_delete_elem_flags(map->fd, key, flags); in bpf_map__delete_elem()
10592 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_and_delete_elem()
10604 return bpf_map_get_next_key(map->fd, cur_key, next_key); in bpf_map__get_next_key()
10613 errno = -PTR_ERR(ptr); in libbpf_get_error()
10620 return -errno; in libbpf_get_error()
10631 prog->name); in bpf_link__update_program()
10632 return libbpf_err(-EINVAL); in bpf_link__update_program()
10651 link->disconnected = true; in bpf_link__disconnect()
10661 if (!link->disconnected && link->detach) in bpf_link__destroy()
10662 err = link->detach(link); in bpf_link__destroy()
10663 if (link->pin_path) in bpf_link__destroy()
10664 free(link->pin_path); in bpf_link__destroy()
10665 if (link->dealloc) in bpf_link__destroy()
10666 link->dealloc(link); in bpf_link__destroy()
10675 return link->fd; in bpf_link__fd()
10680 return link->pin_path; in bpf_link__pin_path()
10685 return libbpf_err_errno(close(link->fd)); in bpf_link__detach_fd()
10695 fd = -errno; in bpf_link__open()
10703 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
10705 link->detach = &bpf_link__detach_fd; in bpf_link__open()
10706 link->fd = fd; in bpf_link__open()
10708 link->pin_path = strdup(path); in bpf_link__open()
10709 if (!link->pin_path) { in bpf_link__open()
10711 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
10719 return bpf_link_detach(link->fd) ? -errno : 0; in bpf_link__detach()
10726 if (link->pin_path) in bpf_link__pin()
10727 return libbpf_err(-EBUSY); in bpf_link__pin()
10735 link->pin_path = strdup(path); in bpf_link__pin()
10736 if (!link->pin_path) in bpf_link__pin()
10737 return libbpf_err(-ENOMEM); in bpf_link__pin()
10739 if (bpf_obj_pin(link->fd, link->pin_path)) { in bpf_link__pin()
10740 err = -errno; in bpf_link__pin()
10741 zfree(&link->pin_path); in bpf_link__pin()
10745 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); in bpf_link__pin()
10753 if (!link->pin_path) in bpf_link__unpin()
10754 return libbpf_err(-EINVAL); in bpf_link__unpin()
10756 err = unlink(link->pin_path); in bpf_link__unpin()
10758 return -errno; in bpf_link__unpin()
10760 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); in bpf_link__unpin()
10761 zfree(&link->pin_path); in bpf_link__unpin()
10782 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) in bpf_link_perf_detach()
10783 err = -errno; in bpf_link_perf_detach()
10785 if (perf_link->perf_event_fd != link->fd) in bpf_link_perf_detach()
10786 close(perf_link->perf_event_fd); in bpf_link_perf_detach()
10787 close(link->fd); in bpf_link_perf_detach()
10790 if (perf_link->legacy_probe_name) { in bpf_link_perf_detach()
10791 if (perf_link->legacy_is_kprobe) { in bpf_link_perf_detach()
10792 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10793 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10795 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10796 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10807 free(perf_link->legacy_probe_name); in bpf_link_perf_dealloc()
10815 int prog_fd, link_fd = -1, err; in bpf_program__attach_perf_event_opts()
10819 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10823 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10824 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10829 prog->name); in bpf_program__attach_perf_event_opts()
10830 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10835 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_perf_event_opts()
10836 link->link.detach = &bpf_link_perf_detach; in bpf_program__attach_perf_event_opts()
10837 link->link.dealloc = &bpf_link_perf_dealloc; in bpf_program__attach_perf_event_opts()
10838 link->perf_event_fd = pfd; in bpf_program__attach_perf_event_opts()
10841 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { in bpf_program__attach_perf_event_opts()
10847 err = -errno; in bpf_program__attach_perf_event_opts()
10849 prog->name, pfd, errstr(err)); in bpf_program__attach_perf_event_opts()
10852 link->link.fd = link_fd; in bpf_program__attach_perf_event_opts()
10855 pr_warn("prog '%s': user context value is not supported\n", prog->name); in bpf_program__attach_perf_event_opts()
10856 err = -EOPNOTSUPP; in bpf_program__attach_perf_event_opts()
10861 err = -errno; in bpf_program__attach_perf_event_opts()
10863 prog->name, pfd, errstr(err)); in bpf_program__attach_perf_event_opts()
10864 if (err == -EPROTO) in bpf_program__attach_perf_event_opts()
10866 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10869 link->link.fd = pfd; in bpf_program__attach_perf_event_opts()
10872 err = -errno; in bpf_program__attach_perf_event_opts()
10874 prog->name, pfd, errstr(err)); in bpf_program__attach_perf_event_opts()
10878 return &link->link; in bpf_program__attach_perf_event_opts()
10892 * this function is expected to parse integer in the range of [0, 2^31-1] from
10903 err = -errno; in parse_uint_from_file()
10909 err = err == EOF ? -EIO : -errno; in parse_uint_from_file()
10957 return -EINVAL; in perf_event_open_probe()
10989 pid < 0 ? -1 : pid /* pid */, in perf_event_open_probe()
10990 pid == -1 ? 0 : -1 /* cpu */, in perf_event_open_probe()
10991 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_probe()
10992 return pfd >= 0 ? pfd : -errno; in perf_event_open_probe()
11006 return -EINVAL; in append_to_file()
11010 return -errno; in append_to_file()
11013 err = -errno; in append_to_file()
11024 static int has_debugfs = -1; in use_debugfs()
11086 return append_to_file(tracefs_kprobe_events(), "-:%s/%s", in remove_kprobe_event_legacy()
11129 pid < 0 ? -1 : pid, /* pid */ in perf_event_kprobe_open_legacy()
11130 pid == -1 ? 0 : -1, /* cpu */ in perf_event_kprobe_open_legacy()
11131 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_kprobe_open_legacy()
11133 err = -errno; in perf_event_kprobe_open_legacy()
11218 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
11233 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
11237 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_kprobe_opts()
11238 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
11243 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
11249 -1 /* pid */, 0 /* ref_ctr_off */); in bpf_program__attach_kprobe_opts()
11258 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_kprobe_opts()
11261 offset, -1 /* pid */); in bpf_program__attach_kprobe_opts()
11264 err = -errno; in bpf_program__attach_kprobe_opts()
11266 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
11276 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
11284 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_kprobe_opts()
11285 perf_link->legacy_is_kprobe = true; in bpf_program__attach_kprobe_opts()
11286 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_kprobe_opts()
11318 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_ksyscall()
11320 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { in bpf_program__attach_ksyscall()
11387 struct kprobe_multi_resolve *res = data->res; in avail_kallsyms_cb()
11390 if (!glob_match(sym_name, res->pattern)) in avail_kallsyms_cb()
11393 if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) { in avail_kallsyms_cb()
11398 * make multi-kprobe usability a bit better: if no match is in avail_kallsyms_cb()
11412 snprintf(sym_trim, sizeof(sym_trim), "%.*s", (int)(sym_sfx - sym_name), sym_name); in avail_kallsyms_cb()
11413 if (!bsearch(&psym_trim, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) in avail_kallsyms_cb()
11417 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); in avail_kallsyms_cb()
11421 res->addrs[res->cnt++] = (unsigned long)sym_addr; in avail_kallsyms_cb()
11437 err = -errno; in libbpf_available_kallsyms_parse()
11451 err = -EINVAL; in libbpf_available_kallsyms_parse()
11455 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kallsyms_parse()
11464 err = -errno; in libbpf_available_kallsyms_parse()
11473 err = -ENOENT; in libbpf_available_kallsyms_parse()
11485 if (res->cnt == 0) in libbpf_available_kallsyms_parse()
11486 err = -ENOENT; in libbpf_available_kallsyms_parse()
11499 return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; in has_available_filter_functions_addrs()
11512 err = -errno; in libbpf_available_kprobes_parse()
11525 err = -EINVAL; in libbpf_available_kprobes_parse()
11529 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kprobes_parse()
11532 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, in libbpf_available_kprobes_parse()
11533 sizeof(*res->addrs), res->cnt + 1); in libbpf_available_kprobes_parse()
11537 res->addrs[res->cnt++] = (unsigned long)sym_addr; in libbpf_available_kprobes_parse()
11540 if (res->cnt == 0) in libbpf_available_kprobes_parse()
11541 err = -ENOENT; in libbpf_available_kprobes_parse()
11567 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11572 prog->name); in bpf_program__attach_kprobe_multi_opts()
11573 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11583 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11585 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11587 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11589 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11591 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11603 prog->name, pattern, res.cnt); in bpf_program__attach_kprobe_multi_opts()
11604 err = -EINVAL; in bpf_program__attach_kprobe_multi_opts()
11616 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11628 err = -ENOMEM; in bpf_program__attach_kprobe_multi_opts()
11631 link->detach = &bpf_link__detach_fd; in bpf_program__attach_kprobe_multi_opts()
11635 err = -errno; in bpf_program__attach_kprobe_multi_opts()
11637 prog->name, errstr(err)); in bpf_program__attach_kprobe_multi_opts()
11640 link->fd = link_fd; in bpf_program__attach_kprobe_multi_opts()
11660 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ in attach_kprobe()
11661 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) in attach_kprobe()
11664 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); in attach_kprobe()
11666 func_name = prog->sec_name + sizeof("kretprobe/") - 1; in attach_kprobe()
11668 func_name = prog->sec_name + sizeof("kprobe/") - 1; in attach_kprobe()
11670 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); in attach_kprobe()
11673 return -EINVAL; in attach_kprobe()
11678 return -EINVAL; in attach_kprobe()
11694 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ in attach_ksyscall()
11695 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) in attach_ksyscall()
11698 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); in attach_ksyscall()
11700 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; in attach_ksyscall()
11702 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; in attach_ksyscall()
11705 return *link ? 0 : -errno; in attach_ksyscall()
11717 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ in attach_kprobe_multi()
11718 if (strcmp(prog->sec_name, "kprobe.multi") == 0 || in attach_kprobe_multi()
11719 strcmp(prog->sec_name, "kretprobe.multi") == 0) in attach_kprobe_multi()
11722 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); in attach_kprobe_multi()
11724 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; in attach_kprobe_multi()
11726 spec = prog->sec_name + sizeof("kprobe.multi/") - 1; in attach_kprobe_multi()
11728 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); in attach_kprobe_multi()
11731 return -EINVAL; in attach_kprobe_multi()
11749 /* no auto-attach for SEC("kprobe.session") */ in attach_kprobe_session()
11750 if (strcmp(prog->sec_name, "kprobe.session") == 0) in attach_kprobe_session()
11753 spec = prog->sec_name + sizeof("kprobe.session/") - 1; in attach_kprobe_session()
11754 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); in attach_kprobe_session()
11757 return -EINVAL; in attach_kprobe_session()
11762 return *link ? 0 : -errno; in attach_kprobe_session()
11769 int n, ret = -EINVAL; in attach_uprobe_multi()
11773 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", in attach_uprobe_multi()
11777 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe_multi()
11784 *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); in attach_uprobe_multi()
11788 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe_multi()
11789 prog->sec_name); in attach_uprobe_multi()
11823 return append_to_file(tracefs_uprobe_events(), "-:%s/%s", in remove_uprobe_event_legacy()
11864 pid < 0 ? -1 : pid, /* pid */ in perf_event_uprobe_open_legacy()
11865 pid == -1 ? 0 : -1, /* cpu */ in perf_event_uprobe_open_legacy()
11866 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_uprobe_open_legacy()
11868 err = -errno; in perf_event_uprobe_open_legacy()
11916 ret = -LIBBPF_ERRNO__FORMAT; in elf_find_func_offset_from_archive()
11923 elf_errmsg(-1)); in elf_find_func_offset_from_archive()
11924 ret = -LIBBPF_ERRNO__LIBELF; in elf_find_func_offset_from_archive()
11951 return "/lib/x86_64-linux-gnu"; in arch_specific_lib_paths()
11953 return "/lib/i386-linux-gnu"; in arch_specific_lib_paths()
11955 return "/lib/s390x-linux-gnu"; in arch_specific_lib_paths()
11957 return "/lib/s390-linux-gnu"; in arch_specific_lib_paths()
11959 return "/lib/arm-linux-gnueabi"; in arch_specific_lib_paths()
11961 return "/lib/arm-linux-gnueabihf"; in arch_specific_lib_paths()
11963 return "/lib/aarch64-linux-gnu"; in arch_specific_lib_paths()
11965 return "/lib/mips64el-linux-gnuabi64"; in arch_specific_lib_paths()
11967 return "/lib/mipsel-linux-gnu"; in arch_specific_lib_paths()
11969 return "/lib/powerpc64le-linux-gnu"; in arch_specific_lib_paths()
11971 return "/lib/sparc64-linux-gnu"; in arch_specific_lib_paths()
11973 return "/lib/riscv64-linux-gnu"; in arch_specific_lib_paths()
12008 seg_len = next_path ? next_path - s : strlen(s); in resolve_full_path()
12019 return -ENOENT; in resolve_full_path()
12042 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12047 prog->name); in bpf_program__attach_uprobe_multi()
12048 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12067 * - syms and offsets are mutually exclusive in bpf_program__attach_uprobe_multi()
12068 * - ref_ctr_offsets and cookies are optional in bpf_program__attach_uprobe_multi()
12074 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12076 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12080 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12083 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12087 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
12094 prog->name, path, errstr(err)); in bpf_program__attach_uprobe_multi()
12128 err = -ENOMEM; in bpf_program__attach_uprobe_multi()
12131 link->detach = &bpf_link__detach_fd; in bpf_program__attach_uprobe_multi()
12135 err = -errno; in bpf_program__attach_uprobe_multi()
12136 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", in bpf_program__attach_uprobe_multi()
12137 prog->name, errstr(err)); in bpf_program__attach_uprobe_multi()
12140 link->fd = link_fd; in bpf_program__attach_uprobe_multi()
12167 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12175 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12182 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); in bpf_program__attach_uprobe_opts()
12189 prog->name, binary_path, errstr(err)); in bpf_program__attach_uprobe_opts()
12218 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
12222 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_uprobe_opts()
12223 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
12228 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12238 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12245 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_uprobe_opts()
12251 err = -errno; in bpf_program__attach_uprobe_opts()
12253 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
12264 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
12272 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_uprobe_opts()
12273 perf_link->legacy_is_kprobe = false; in bpf_program__attach_uprobe_opts()
12274 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_uprobe_opts()
12286 /* Format of u[ret]probe section definition supporting auto-attach:
12293 * specified (and auto-attach is not possible) or the above format is specified for
12294 * auto-attach.
12300 int n, c, ret = -EINVAL; in attach_uprobe()
12305 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", in attach_uprobe()
12309 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe()
12314 prog->name, prog->sec_name); in attach_uprobe()
12332 prog->name); in attach_uprobe()
12336 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); in attach_uprobe()
12340 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe()
12341 prog->sec_name); in attach_uprobe()
12367 struct bpf_object *obj = prog->obj; in bpf_program__attach_usdt()
12373 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12377 prog->name); in bpf_program__attach_usdt()
12378 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12382 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12388 prog->name, binary_path, errstr(err)); in bpf_program__attach_usdt()
12397 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
12398 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
12399 if (!obj->usdt_man) { in bpf_program__attach_usdt()
12400 obj->usdt_man = usdt_manager_new(obj); in bpf_program__attach_usdt()
12401 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
12402 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
12406 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, in bpf_program__attach_usdt()
12422 /* no auto-attach for just SEC("usdt") */ in attach_usdt()
12431 err = -EINVAL; in attach_usdt()
12433 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, in attach_usdt()
12452 return -errno; in determine_tracepoint_id()
12456 return -E2BIG; in determine_tracepoint_id()
12481 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, in perf_event_open_tracepoint()
12482 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_tracepoint()
12484 err = -errno; in perf_event_open_tracepoint()
12503 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tracepoint_opts()
12510 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
12519 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
12539 /* no auto-attach for SEC("tp") or SEC("tracepoint") */ in attach_tp()
12540 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0) in attach_tp()
12543 sec_name = strdup(prog->sec_name); in attach_tp()
12545 return -ENOMEM; in attach_tp()
12548 if (str_has_pfx(prog->sec_name, "tp/")) in attach_tp()
12549 tp_cat = sec_name + sizeof("tp/") - 1; in attach_tp()
12551 tp_cat = sec_name + sizeof("tracepoint/") - 1; in attach_tp()
12555 return -EINVAL; in attach_tp()
12575 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_raw_tracepoint_opts()
12579 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_raw_tracepoint_opts()
12580 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_raw_tracepoint_opts()
12585 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_raw_tracepoint_opts()
12586 link->detach = &bpf_link__detach_fd; in bpf_program__attach_raw_tracepoint_opts()
12592 pfd = -errno; in bpf_program__attach_raw_tracepoint_opts()
12595 prog->name, tp_name, errstr(pfd)); in bpf_program__attach_raw_tracepoint_opts()
12598 link->fd = pfd; in bpf_program__attach_raw_tracepoint_opts()
12624 if (!str_has_pfx(prog->sec_name, prefixes[i])) in attach_raw_tp()
12628 /* no auto-attach case of, e.g., SEC("raw_tp") */ in attach_raw_tp()
12629 if (prog->sec_name[pfx_len] == '\0') in attach_raw_tp()
12632 if (prog->sec_name[pfx_len] != '/') in attach_raw_tp()
12635 tp_name = prog->sec_name + pfx_len + 1; in attach_raw_tp()
12641 prog->name, prog->sec_name); in attach_raw_tp()
12642 return -EINVAL; in attach_raw_tp()
12658 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
12662 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_btf_id()
12663 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
12668 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_btf_id()
12669 link->detach = &bpf_link__detach_fd; in bpf_program__attach_btf_id()
12675 pfd = -errno; in bpf_program__attach_btf_id()
12678 prog->name, errstr(pfd)); in bpf_program__attach_btf_id()
12681 link->fd = pfd; in bpf_program__attach_btf_id()
12724 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program_attach_fd()
12725 return libbpf_err_ptr(-EINVAL); in bpf_program_attach_fd()
12730 return libbpf_err_ptr(-ENOMEM); in bpf_program_attach_fd()
12731 link->detach = &bpf_link__detach_fd; in bpf_program_attach_fd()
12736 link_fd = -errno; in bpf_program_attach_fd()
12739 prog->name, target_name, in bpf_program_attach_fd()
12743 link->fd = link_fd; in bpf_program_attach_fd()
12780 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12785 /* validate we don't have unexpected combinations of non-zero fields */ in bpf_program__attach_tcx()
12788 prog->name); in bpf_program__attach_tcx()
12789 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12793 prog->name); in bpf_program__attach_tcx()
12794 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12815 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12820 /* validate we don't have unexpected combinations of non-zero fields */ in bpf_program__attach_netkit()
12823 prog->name); in bpf_program__attach_netkit()
12824 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12828 prog->name); in bpf_program__attach_netkit()
12829 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12848 prog->name); in bpf_program__attach_freplace()
12849 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12852 if (prog->type != BPF_PROG_TYPE_EXT) { in bpf_program__attach_freplace()
12854 prog->name); in bpf_program__attach_freplace()
12855 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12887 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12894 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_iter()
12895 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12900 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_iter()
12901 link->detach = &bpf_link__detach_fd; in bpf_program__attach_iter()
12906 link_fd = -errno; in bpf_program__attach_iter()
12909 prog->name, errstr(link_fd)); in bpf_program__attach_iter()
12912 link->fd = link_fd; in bpf_program__attach_iter()
12930 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12934 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_netfilter()
12935 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12940 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_netfilter()
12942 link->detach = &bpf_link__detach_fd; in bpf_program__attach_netfilter()
12951 link_fd = -errno; in bpf_program__attach_netfilter()
12954 prog->name, errstr(link_fd)); in bpf_program__attach_netfilter()
12957 link->fd = link_fd; in bpf_program__attach_netfilter()
12967 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_program__attach()
12968 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
12972 prog->name); in bpf_program__attach()
12973 return libbpf_err_ptr(-EINVAL); in bpf_program__attach()
12976 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); in bpf_program__attach()
12980 /* When calling bpf_program__attach() explicitly, auto-attach support in bpf_program__attach()
12986 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
13003 if (st_link->map_fd < 0) in bpf_link__detach_struct_ops()
13005 return bpf_map_delete_elem(link->fd, &zero); in bpf_link__detach_struct_ops()
13007 return close(link->fd); in bpf_link__detach_struct_ops()
13017 pr_warn("map '%s': can't attach non-struct_ops map\n", map->name); in bpf_map__attach_struct_ops()
13018 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
13021 if (map->fd < 0) { in bpf_map__attach_struct_ops()
13022 pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name); in bpf_map__attach_struct_ops()
13023 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
13028 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
13031 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_map__attach_struct_ops()
13037 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { in bpf_map__attach_struct_ops()
13042 link->link.detach = bpf_link__detach_struct_ops; in bpf_map__attach_struct_ops()
13044 if (!(map->def.map_flags & BPF_F_LINK)) { in bpf_map__attach_struct_ops()
13046 link->link.fd = map->fd; in bpf_map__attach_struct_ops()
13047 link->map_fd = -1; in bpf_map__attach_struct_ops()
13048 return &link->link; in bpf_map__attach_struct_ops()
13051 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); in bpf_map__attach_struct_ops()
13057 link->link.fd = fd; in bpf_map__attach_struct_ops()
13058 link->map_fd = map->fd; in bpf_map__attach_struct_ops()
13060 return &link->link; in bpf_map__attach_struct_ops()
13073 return -EINVAL; in bpf_link__update_map()
13075 if (map->fd < 0) { in bpf_link__update_map()
13076 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); in bpf_link__update_map()
13077 return -EINVAL; in bpf_link__update_map()
13082 if (st_ops_link->map_fd < 0) in bpf_link__update_map()
13083 return -EINVAL; in bpf_link__update_map()
13085 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_link__update_map()
13091 if (err && err != -EBUSY) in bpf_link__update_map()
13094 err = bpf_link_update(link->fd, map->fd, NULL); in bpf_link__update_map()
13098 st_ops_link->map_fd = map->fd; in bpf_link__update_map()
13113 __u64 data_tail = header->data_tail; in perf_event_read_simple()
13120 ehdr = base + (data_tail & (mmap_size - 1)); in perf_event_read_simple()
13121 ehdr_size = ehdr->size; in perf_event_read_simple()
13125 size_t len_first = base + mmap_size - copy_start; in perf_event_read_simple()
13126 size_t len_secnd = ehdr_size - len_first; in perf_event_read_simple()
13160 /* sample_cb and lost_cb are higher-level common-case callbacks */
13199 if (cpu_buf->base && in perf_buffer__free_cpu_buf()
13200 munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) in perf_buffer__free_cpu_buf()
13201 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); in perf_buffer__free_cpu_buf()
13202 if (cpu_buf->fd >= 0) { in perf_buffer__free_cpu_buf()
13203 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); in perf_buffer__free_cpu_buf()
13204 close(cpu_buf->fd); in perf_buffer__free_cpu_buf()
13206 free(cpu_buf->buf); in perf_buffer__free_cpu_buf()
13216 if (pb->cpu_bufs) { in perf_buffer__free()
13217 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__free()
13218 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__free()
13223 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); in perf_buffer__free()
13226 free(pb->cpu_bufs); in perf_buffer__free()
13228 if (pb->epoll_fd >= 0) in perf_buffer__free()
13229 close(pb->epoll_fd); in perf_buffer__free()
13230 free(pb->events); in perf_buffer__free()
13243 return ERR_PTR(-ENOMEM); in perf_buffer__open_cpu_buf()
13245 cpu_buf->pb = pb; in perf_buffer__open_cpu_buf()
13246 cpu_buf->cpu = cpu; in perf_buffer__open_cpu_buf()
13247 cpu_buf->map_key = map_key; in perf_buffer__open_cpu_buf()
13249 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, in perf_buffer__open_cpu_buf()
13250 -1, PERF_FLAG_FD_CLOEXEC); in perf_buffer__open_cpu_buf()
13251 if (cpu_buf->fd < 0) { in perf_buffer__open_cpu_buf()
13252 err = -errno; in perf_buffer__open_cpu_buf()
13258 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, in perf_buffer__open_cpu_buf()
13260 cpu_buf->fd, 0); in perf_buffer__open_cpu_buf()
13261 if (cpu_buf->base == MAP_FAILED) { in perf_buffer__open_cpu_buf()
13262 cpu_buf->base = NULL; in perf_buffer__open_cpu_buf()
13263 err = -errno; in perf_buffer__open_cpu_buf()
13269 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { in perf_buffer__open_cpu_buf()
13270 err = -errno; in perf_buffer__open_cpu_buf()
13298 return libbpf_err_ptr(-EINVAL); in perf_buffer__new()
13328 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
13331 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
13353 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { in __perf_buffer__new()
13356 return ERR_PTR(-EINVAL); in __perf_buffer__new()
13359 /* best-effort sanity checks */ in __perf_buffer__new()
13364 err = -errno; in __perf_buffer__new()
13366 * -EBADFD, -EFAULT, or -E2BIG on real error in __perf_buffer__new()
13368 if (err != -EINVAL) { in __perf_buffer__new()
13379 return ERR_PTR(-EINVAL); in __perf_buffer__new()
13385 return ERR_PTR(-ENOMEM); in __perf_buffer__new()
13387 pb->event_cb = p->event_cb; in __perf_buffer__new()
13388 pb->sample_cb = p->sample_cb; in __perf_buffer__new()
13389 pb->lost_cb = p->lost_cb; in __perf_buffer__new()
13390 pb->ctx = p->ctx; in __perf_buffer__new()
13392 pb->page_size = getpagesize(); in __perf_buffer__new()
13393 pb->mmap_size = pb->page_size * page_cnt; in __perf_buffer__new()
13394 pb->map_fd = map_fd; in __perf_buffer__new()
13396 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); in __perf_buffer__new()
13397 if (pb->epoll_fd < 0) { in __perf_buffer__new()
13398 err = -errno; in __perf_buffer__new()
13404 if (p->cpu_cnt > 0) { in __perf_buffer__new()
13405 pb->cpu_cnt = p->cpu_cnt; in __perf_buffer__new()
13407 pb->cpu_cnt = libbpf_num_possible_cpus(); in __perf_buffer__new()
13408 if (pb->cpu_cnt < 0) { in __perf_buffer__new()
13409 err = pb->cpu_cnt; in __perf_buffer__new()
13412 if (map.max_entries && map.max_entries < pb->cpu_cnt) in __perf_buffer__new()
13413 pb->cpu_cnt = map.max_entries; in __perf_buffer__new()
13416 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); in __perf_buffer__new()
13417 if (!pb->events) { in __perf_buffer__new()
13418 err = -ENOMEM; in __perf_buffer__new()
13422 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); in __perf_buffer__new()
13423 if (!pb->cpu_bufs) { in __perf_buffer__new()
13424 err = -ENOMEM; in __perf_buffer__new()
13435 for (i = 0, j = 0; i < pb->cpu_cnt; i++) { in __perf_buffer__new()
13439 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; in __perf_buffer__new()
13440 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; in __perf_buffer__new()
13445 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) in __perf_buffer__new()
13448 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); in __perf_buffer__new()
13454 pb->cpu_bufs[j] = cpu_buf; in __perf_buffer__new()
13456 err = bpf_map_update_elem(pb->map_fd, &map_key, in __perf_buffer__new()
13457 &cpu_buf->fd, 0); in __perf_buffer__new()
13459 err = -errno; in __perf_buffer__new()
13460 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", in __perf_buffer__new()
13461 cpu, map_key, cpu_buf->fd, in __perf_buffer__new()
13466 pb->events[j].events = EPOLLIN; in __perf_buffer__new()
13467 pb->events[j].data.ptr = cpu_buf; in __perf_buffer__new()
13468 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, in __perf_buffer__new()
13469 &pb->events[j]) < 0) { in __perf_buffer__new()
13470 err = -errno; in __perf_buffer__new()
13472 cpu, cpu_buf->fd, in __perf_buffer__new()
13478 pb->cpu_cnt = j; in __perf_buffer__new()
13507 struct perf_buffer *pb = cpu_buf->pb; in perf_buffer__process_record()
13511 if (pb->event_cb) in perf_buffer__process_record()
13512 return pb->event_cb(pb->ctx, cpu_buf->cpu, e); in perf_buffer__process_record()
13514 switch (e->type) { in perf_buffer__process_record()
13518 if (pb->sample_cb) in perf_buffer__process_record()
13519 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); in perf_buffer__process_record()
13525 if (pb->lost_cb) in perf_buffer__process_record()
13526 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); in perf_buffer__process_record()
13530 pr_warn("unknown perf sample type %d\n", e->type); in perf_buffer__process_record()
13541 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, in perf_buffer__process_records()
13542 pb->page_size, &cpu_buf->buf, in perf_buffer__process_records()
13543 &cpu_buf->buf_size, in perf_buffer__process_records()
13552 return pb->epoll_fd; in perf_buffer__epoll_fd()
13559 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); in perf_buffer__poll()
13561 return -errno; in perf_buffer__poll()
13564 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; in perf_buffer__poll()
13580 return pb->cpu_cnt; in perf_buffer__buffer_cnt()
13592 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer_fd()
13593 return libbpf_err(-EINVAL); in perf_buffer__buffer_fd()
13595 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer_fd()
13597 return libbpf_err(-ENOENT); in perf_buffer__buffer_fd()
13599 return cpu_buf->fd; in perf_buffer__buffer_fd()
13606 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer()
13607 return libbpf_err(-EINVAL); in perf_buffer__buffer()
13609 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer()
13611 return libbpf_err(-ENOENT); in perf_buffer__buffer()
13613 *buf = cpu_buf->base; in perf_buffer__buffer()
13614 *buf_size = pb->mmap_size; in perf_buffer__buffer()
13623 * - 0 on success;
13624 * - <0 on failure.
13630 if (buf_idx >= pb->cpu_cnt) in perf_buffer__consume_buffer()
13631 return libbpf_err(-EINVAL); in perf_buffer__consume_buffer()
13633 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__consume_buffer()
13635 return libbpf_err(-ENOENT); in perf_buffer__consume_buffer()
13644 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__consume()
13645 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__consume()
13667 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13669 if (prog->obj->loaded) in bpf_program__set_attach_target()
13670 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13676 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
13687 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13690 err = bpf_object__load_vmlinux_btf(prog->obj, true); in bpf_program__set_attach_target()
13693 err = find_kernel_btf_id(prog->obj, attach_func_name, in bpf_program__set_attach_target()
13694 prog->expected_attach_type, in bpf_program__set_attach_target()
13700 prog->attach_btf_id = btf_id; in bpf_program__set_attach_target()
13701 prog->attach_btf_obj_fd = btf_obj_fd; in bpf_program__set_attach_target()
13702 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
13708 int err = 0, n, len, start, end = -1; in parse_cpu_mask_str()
13714 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ in parse_cpu_mask_str()
13720 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); in parse_cpu_mask_str()
13723 err = -EINVAL; in parse_cpu_mask_str()
13731 err = -EINVAL; in parse_cpu_mask_str()
13736 err = -ENOMEM; in parse_cpu_mask_str()
13740 memset(tmp + *mask_sz, 0, start - *mask_sz); in parse_cpu_mask_str()
13741 memset(tmp + start, 1, end - start + 1); in parse_cpu_mask_str()
13747 return -EINVAL; in parse_cpu_mask_str()
13763 err = -errno; in parse_cpu_mask_file()
13770 err = len ? -errno : -EINVAL; in parse_cpu_mask_file()
13776 return -E2BIG; in parse_cpu_mask_file()
13817 struct bpf_map **map = map_skel->map; in populate_skeleton_maps()
13818 const char *name = map_skel->name; in populate_skeleton_maps()
13819 void **mmaped = map_skel->mmaped; in populate_skeleton_maps()
13824 return -ESRCH; in populate_skeleton_maps()
13827 /* externs shouldn't be pre-setup from user code */ in populate_skeleton_maps()
13828 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) in populate_skeleton_maps()
13829 *mmaped = (*map)->mmaped; in populate_skeleton_maps()
13842 struct bpf_program **prog = prog_skel->prog; in populate_skeleton_progs()
13843 const char *name = prog_skel->name; in populate_skeleton_progs()
13848 return -ESRCH; in populate_skeleton_progs()
13860 obj = bpf_object_open(NULL, s->data, s->data_sz, s->name, opts); in bpf_object__open_skeleton()
13864 s->name, errstr(err)); in bpf_object__open_skeleton()
13868 *s->obj = obj; in bpf_object__open_skeleton()
13869 err = populate_skeleton_maps(obj, s->maps, s->map_cnt, s->map_skel_sz); in bpf_object__open_skeleton()
13871 pr_warn("failed to populate skeleton maps for '%s': %s\n", s->name, errstr(err)); in bpf_object__open_skeleton()
13875 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt, s->prog_skel_sz); in bpf_object__open_skeleton()
13877 pr_warn("failed to populate skeleton progs for '%s': %s\n", s->name, errstr(err)); in bpf_object__open_skeleton()
13895 if (!s->obj) in bpf_object__open_subskeleton()
13896 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13898 btf = bpf_object__btf(s->obj); in bpf_object__open_subskeleton()
13901 bpf_object__name(s->obj)); in bpf_object__open_subskeleton()
13902 return libbpf_err(-errno); in bpf_object__open_subskeleton()
13905 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt, s->map_skel_sz); in bpf_object__open_subskeleton()
13911 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt, s->prog_skel_sz); in bpf_object__open_subskeleton()
13917 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { in bpf_object__open_subskeleton()
13918 var_skel = (void *)s->vars + var_idx * s->var_skel_sz; in bpf_object__open_subskeleton()
13919 map = *var_skel->map; in bpf_object__open_subskeleton()
13927 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13933 var_type = btf__type_by_id(btf, var->type); in bpf_object__open_subskeleton()
13934 var_name = btf__name_by_offset(btf, var_type->name_off); in bpf_object__open_subskeleton()
13935 if (strcmp(var_name, var_skel->name) == 0) { in bpf_object__open_subskeleton()
13936 *var_skel->addr = map->mmaped + var->offset; in bpf_object__open_subskeleton()
13948 free(s->maps); in bpf_object__destroy_subskeleton()
13949 free(s->progs); in bpf_object__destroy_subskeleton()
13950 free(s->vars); in bpf_object__destroy_subskeleton()
13958 err = bpf_object__load(*s->obj); in bpf_object__load_skeleton()
13960 pr_warn("failed to load BPF skeleton '%s': %s\n", s->name, errstr(err)); in bpf_object__load_skeleton()
13964 for (i = 0; i < s->map_cnt; i++) { in bpf_object__load_skeleton()
13965 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; in bpf_object__load_skeleton()
13966 struct bpf_map *map = *map_skel->map; in bpf_object__load_skeleton()
13968 if (!map_skel->mmaped) in bpf_object__load_skeleton()
13971 *map_skel->mmaped = map->mmaped; in bpf_object__load_skeleton()
13981 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__attach_skeleton()
13982 struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz; in bpf_object__attach_skeleton()
13983 struct bpf_program *prog = *prog_skel->prog; in bpf_object__attach_skeleton()
13984 struct bpf_link **link = prog_skel->link; in bpf_object__attach_skeleton()
13986 if (!prog->autoload || !prog->autoattach) in bpf_object__attach_skeleton()
13989 /* auto-attaching not supported for this program */ in bpf_object__attach_skeleton()
13990 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_object__attach_skeleton()
13993 /* if user already set the link manually, don't attempt auto-attach */ in bpf_object__attach_skeleton()
13997 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); in bpf_object__attach_skeleton()
13999 pr_warn("prog '%s': failed to auto-attach: %s\n", in bpf_object__attach_skeleton()
14004 /* It's possible that for some SEC() definitions auto-attach in bpf_object__attach_skeleton()
14009 * auto-attached. But if not, it shouldn't trigger skeleton's in bpf_object__attach_skeleton()
14017 for (i = 0; i < s->map_cnt; i++) { in bpf_object__attach_skeleton()
14018 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; in bpf_object__attach_skeleton()
14019 struct bpf_map *map = *map_skel->map; in bpf_object__attach_skeleton()
14022 if (!map->autocreate || !map->autoattach) in bpf_object__attach_skeleton()
14030 if (s->map_skel_sz < offsetofend(struct bpf_map_skeleton, link)) { in bpf_object__attach_skeleton()
14031 pr_warn("map '%s': BPF skeleton version is old, skipping map auto-attachment...\n", in bpf_object__attach_skeleton()
14036 link = map_skel->link; in bpf_object__attach_skeleton()
14042 err = -errno; in bpf_object__attach_skeleton()
14043 pr_warn("map '%s': failed to auto-attach: %s\n", in bpf_object__attach_skeleton()
14056 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__detach_skeleton()
14057 struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz; in bpf_object__detach_skeleton()
14058 struct bpf_link **link = prog_skel->link; in bpf_object__detach_skeleton()
14064 if (s->map_skel_sz < sizeof(struct bpf_map_skeleton)) in bpf_object__detach_skeleton()
14067 for (i = 0; i < s->map_cnt; i++) { in bpf_object__detach_skeleton()
14068 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; in bpf_object__detach_skeleton()
14069 struct bpf_link **link = map_skel->link; in bpf_object__detach_skeleton()
14084 if (s->obj) in bpf_object__destroy_skeleton()
14085 bpf_object__close(*s->obj); in bpf_object__destroy_skeleton()
14086 free(s->maps); in bpf_object__destroy_skeleton()
14087 free(s->progs); in bpf_object__destroy_skeleton()