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 <ast@kernel.org>
67 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
215 [BPF_PROG_TYPE_EXT] = "ext",
267 if (err != -EPERM || geteuid() != 0) in pr_perm_msg()
284 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", in pr_perm_msg()
300 fd = -1; \
311 /* as of v1.0 libbpf_set_strict_mode() is a no-op */ in libbpf_set_strict_mode()
357 /* stored as sec_def->cookie for all libbpf-supported SEC()s */
376 /* BPF program support non-linear XDP buffer */
414 * program. For the entry-point (main) BPF program, this is always
415 * zero. For a sub-program, this gets reset before each of main BPF
417 * whether sub-program was already appended to the main program, and
429 * entry-point BPF programs this includes the size of main program
430 * itself plus all the used sub-programs, appended at the end
481 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
587 /* BTF fd index to be patched in for insn->off, this is
588 * 0 for vmlinux BTF, index in obj->fd_array for module
673 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
677 /* vmlinux BTF override for CO-RE relocations */
714 zclose(prog->fd); in bpf_program__unload()
716 zfree(&prog->func_info); in bpf_program__unload()
717 zfree(&prog->line_info); in bpf_program__unload()
726 zfree(&prog->name); in bpf_program__exit()
727 zfree(&prog->sec_name); in bpf_program__exit()
728 zfree(&prog->insns); in bpf_program__exit()
729 zfree(&prog->reloc_desc); in bpf_program__exit()
731 prog->nr_reloc = 0; in bpf_program__exit()
732 prog->insns_cnt = 0; in bpf_program__exit()
733 prog->sec_idx = -1; in bpf_program__exit()
738 return BPF_CLASS(insn->code) == BPF_JMP && in insn_is_subprog_call()
739 BPF_OP(insn->code) == BPF_CALL && in insn_is_subprog_call()
740 BPF_SRC(insn->code) == BPF_K && in insn_is_subprog_call()
741 insn->src_reg == BPF_PSEUDO_CALL && in insn_is_subprog_call()
742 insn->dst_reg == 0 && in insn_is_subprog_call()
743 insn->off == 0; in insn_is_subprog_call()
748 return insn->code == (BPF_JMP | BPF_CALL); in is_call_insn()
753 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; in insn_is_pseudo_func()
764 return -EINVAL; in bpf_object__init_prog()
768 prog->obj = obj; in bpf_object__init_prog()
770 prog->sec_idx = sec_idx; in bpf_object__init_prog()
771 prog->sec_insn_off = sec_off / BPF_INSN_SZ; in bpf_object__init_prog()
772 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; in bpf_object__init_prog()
774 prog->insns_cnt = prog->sec_insn_cnt; in bpf_object__init_prog()
776 prog->type = BPF_PROG_TYPE_UNSPEC; in bpf_object__init_prog()
777 prog->fd = -1; in bpf_object__init_prog()
778 prog->exception_cb_idx = -1; in bpf_object__init_prog()
785 prog->autoload = false; in bpf_object__init_prog()
789 prog->autoload = true; in bpf_object__init_prog()
792 prog->autoattach = true; in bpf_object__init_prog()
795 prog->log_level = obj->log_level; in bpf_object__init_prog()
797 prog->sec_name = strdup(sec_name); in bpf_object__init_prog()
798 if (!prog->sec_name) in bpf_object__init_prog()
801 prog->name = strdup(name); in bpf_object__init_prog()
802 if (!prog->name) in bpf_object__init_prog()
805 prog->insns = malloc(insn_data_sz); in bpf_object__init_prog()
806 if (!prog->insns) in bpf_object__init_prog()
808 memcpy(prog->insns, insn_data, insn_data_sz); in bpf_object__init_prog()
814 return -ENOMEM; in bpf_object__init_prog()
821 Elf_Data *symbols = obj->efile.symbols; in bpf_object__add_programs()
823 void *data = sec_data->d_buf; in bpf_object__add_programs()
824 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; in bpf_object__add_programs()
829 progs = obj->programs; in bpf_object__add_programs()
830 nr_progs = obj->nr_programs; in bpf_object__add_programs()
831 nr_syms = symbols->d_size / sizeof(Elf64_Sym); in bpf_object__add_programs()
836 if (sym->st_shndx != sec_idx) in bpf_object__add_programs()
838 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) in bpf_object__add_programs()
841 prog_sz = sym->st_size; in bpf_object__add_programs()
842 sec_off = sym->st_value; in bpf_object__add_programs()
844 name = elf_sym_str(obj, sym->st_name); in bpf_object__add_programs()
848 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
854 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
857 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { in bpf_object__add_programs()
859 return -ENOTSUP; in bpf_object__add_programs()
868 * In this case the original obj->programs in bpf_object__add_programs()
874 return -ENOMEM; in bpf_object__add_programs()
876 obj->programs = progs; in bpf_object__add_programs()
885 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) in bpf_object__add_programs()
886 prog->sym_global = true; in bpf_object__add_programs()
893 if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in bpf_object__add_programs()
894 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) in bpf_object__add_programs()
895 prog->mark_btf_static = true; in bpf_object__add_programs()
898 obj->nr_programs = nr_progs; in bpf_object__add_programs()
926 if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) in find_member_by_name()
978 if (kern_data_member->type == kern_type_id) in find_struct_ops_kern_types()
984 return -EINVAL; in find_struct_ops_kern_types()
998 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; in bpf_map__is_struct_ops()
1014 st_ops = map->st_ops; in bpf_map__init_kern_struct_ops()
1015 type = st_ops->type; in bpf_map__init_kern_struct_ops()
1016 tname = st_ops->tname; in bpf_map__init_kern_struct_ops()
1025 map->name, st_ops->type_id, kern_type_id, kern_vtype_id); in bpf_map__init_kern_struct_ops()
1027 map->def.value_size = kern_vtype->size; in bpf_map__init_kern_struct_ops()
1028 map->btf_vmlinux_value_type_id = kern_vtype_id; in bpf_map__init_kern_struct_ops()
1030 st_ops->kern_vdata = calloc(1, kern_vtype->size); in bpf_map__init_kern_struct_ops()
1031 if (!st_ops->kern_vdata) in bpf_map__init_kern_struct_ops()
1032 return -ENOMEM; in bpf_map__init_kern_struct_ops()
1034 data = st_ops->data; in bpf_map__init_kern_struct_ops()
1035 kern_data_off = kern_data_member->offset / 8; in bpf_map__init_kern_struct_ops()
1036 kern_data = st_ops->kern_vdata + kern_data_off; in bpf_map__init_kern_struct_ops()
1048 mname = btf__name_by_offset(btf, member->name_off); in bpf_map__init_kern_struct_ops()
1052 map->name, mname); in bpf_map__init_kern_struct_ops()
1053 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1056 kern_member_idx = kern_member - btf_members(kern_type); in bpf_map__init_kern_struct_ops()
1060 map->name, mname); in bpf_map__init_kern_struct_ops()
1061 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1064 moff = member->offset / 8; in bpf_map__init_kern_struct_ops()
1065 kern_moff = kern_member->offset / 8; in bpf_map__init_kern_struct_ops()
1070 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); in bpf_map__init_kern_struct_ops()
1071 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, in bpf_map__init_kern_struct_ops()
1073 if (BTF_INFO_KIND(mtype->info) != in bpf_map__init_kern_struct_ops()
1074 BTF_INFO_KIND(kern_mtype->info)) { in bpf_map__init_kern_struct_ops()
1076 map->name, mname, BTF_INFO_KIND(mtype->info), in bpf_map__init_kern_struct_ops()
1077 BTF_INFO_KIND(kern_mtype->info)); in bpf_map__init_kern_struct_ops()
1078 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1084 prog = st_ops->progs[i]; in bpf_map__init_kern_struct_ops()
1089 kern_mtype->type, in bpf_map__init_kern_struct_ops()
1092 /* mtype->type must be a func_proto which was in bpf_map__init_kern_struct_ops()
1098 map->name, mname); in bpf_map__init_kern_struct_ops()
1099 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1102 prog->attach_btf_id = kern_type_id; in bpf_map__init_kern_struct_ops()
1103 prog->expected_attach_type = kern_member_idx; in bpf_map__init_kern_struct_ops()
1105 st_ops->kern_func_off[i] = kern_data_off + kern_moff; in bpf_map__init_kern_struct_ops()
1108 map->name, mname, prog->name, moff, in bpf_map__init_kern_struct_ops()
1118 map->name, mname, (ssize_t)msize, in bpf_map__init_kern_struct_ops()
1120 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1124 map->name, mname, (unsigned int)msize, in bpf_map__init_kern_struct_ops()
1138 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__init_kern_struct_ops_maps()
1139 map = &obj->maps[i]; in bpf_object__init_kern_struct_ops_maps()
1144 err = bpf_map__init_kern_struct_ops(map, obj->btf, in bpf_object__init_kern_struct_ops_maps()
1145 obj->btf_vmlinux); in bpf_object__init_kern_struct_ops_maps()
1165 if (shndx == -1) in init_struct_ops_maps()
1168 btf = obj->btf; in init_struct_ops_maps()
1174 return -EINVAL; in init_struct_ops_maps()
1180 type = btf__type_by_id(obj->btf, vsi->type); in init_struct_ops_maps()
1181 var_name = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1183 type_id = btf__resolve_type(obj->btf, vsi->type); in init_struct_ops_maps()
1186 vsi->type, sec_name); in init_struct_ops_maps()
1187 return -EINVAL; in init_struct_ops_maps()
1190 type = btf__type_by_id(obj->btf, type_id); in init_struct_ops_maps()
1191 tname = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1194 return -ENOTSUP; in init_struct_ops_maps()
1198 return -EINVAL; in init_struct_ops_maps()
1205 map->sec_idx = shndx; in init_struct_ops_maps()
1206 map->sec_offset = vsi->offset; in init_struct_ops_maps()
1207 map->name = strdup(var_name); in init_struct_ops_maps()
1208 if (!map->name) in init_struct_ops_maps()
1209 return -ENOMEM; in init_struct_ops_maps()
1211 map->def.type = BPF_MAP_TYPE_STRUCT_OPS; in init_struct_ops_maps()
1212 map->def.key_size = sizeof(int); in init_struct_ops_maps()
1213 map->def.value_size = type->size; in init_struct_ops_maps()
1214 map->def.max_entries = 1; in init_struct_ops_maps()
1215 map->def.map_flags = map_flags; in init_struct_ops_maps()
1217 map->st_ops = calloc(1, sizeof(*map->st_ops)); in init_struct_ops_maps()
1218 if (!map->st_ops) in init_struct_ops_maps()
1219 return -ENOMEM; in init_struct_ops_maps()
1220 st_ops = map->st_ops; in init_struct_ops_maps()
1221 st_ops->data = malloc(type->size); in init_struct_ops_maps()
1222 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); in init_struct_ops_maps()
1223 st_ops->kern_func_off = malloc(btf_vlen(type) * in init_struct_ops_maps()
1224 sizeof(*st_ops->kern_func_off)); in init_struct_ops_maps()
1225 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) in init_struct_ops_maps()
1226 return -ENOMEM; in init_struct_ops_maps()
1228 if (vsi->offset + type->size > data->d_size) { in init_struct_ops_maps()
1231 return -EINVAL; in init_struct_ops_maps()
1234 memcpy(st_ops->data, in init_struct_ops_maps()
1235 data->d_buf + vsi->offset, in init_struct_ops_maps()
1236 type->size); in init_struct_ops_maps()
1237 st_ops->tname = tname; in init_struct_ops_maps()
1238 st_ops->type = type; in init_struct_ops_maps()
1239 st_ops->type_id = type_id; in init_struct_ops_maps()
1242 tname, type_id, var_name, vsi->offset); in init_struct_ops_maps()
1252 err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx, in bpf_object_init_struct_ops()
1253 obj->efile.st_ops_data, 0); in bpf_object_init_struct_ops()
1255 obj->efile.st_ops_link_shndx, in bpf_object_init_struct_ops()
1256 obj->efile.st_ops_link_data, in bpf_object_init_struct_ops()
1272 return ERR_PTR(-ENOMEM); in bpf_object__new()
1275 strcpy(obj->path, path); in bpf_object__new()
1277 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); in bpf_object__new()
1280 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); in bpf_object__new()
1281 end = strchr(obj->name, '.'); in bpf_object__new()
1286 obj->efile.fd = -1; in bpf_object__new()
1293 obj->efile.obj_buf = obj_buf; in bpf_object__new()
1294 obj->efile.obj_buf_sz = obj_buf_sz; in bpf_object__new()
1295 obj->efile.btf_maps_shndx = -1; in bpf_object__new()
1296 obj->efile.st_ops_shndx = -1; in bpf_object__new()
1297 obj->efile.st_ops_link_shndx = -1; in bpf_object__new()
1298 obj->kconfig_map_idx = -1; in bpf_object__new()
1300 obj->kern_version = get_kernel_version(); in bpf_object__new()
1301 obj->loaded = false; in bpf_object__new()
1308 if (!obj->efile.elf) in bpf_object__elf_finish()
1311 elf_end(obj->efile.elf); in bpf_object__elf_finish()
1312 obj->efile.elf = NULL; in bpf_object__elf_finish()
1313 obj->efile.symbols = NULL; in bpf_object__elf_finish()
1314 obj->efile.st_ops_data = NULL; in bpf_object__elf_finish()
1315 obj->efile.st_ops_link_data = NULL; in bpf_object__elf_finish()
1317 zfree(&obj->efile.secs); in bpf_object__elf_finish()
1318 obj->efile.sec_cnt = 0; in bpf_object__elf_finish()
1319 zclose(obj->efile.fd); in bpf_object__elf_finish()
1320 obj->efile.obj_buf = NULL; in bpf_object__elf_finish()
1321 obj->efile.obj_buf_sz = 0; in bpf_object__elf_finish()
1330 if (obj->efile.elf) { in bpf_object__elf_init()
1332 return -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1335 if (obj->efile.obj_buf_sz > 0) { in bpf_object__elf_init()
1337 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); in bpf_object__elf_init()
1339 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); in bpf_object__elf_init()
1340 if (obj->efile.fd < 0) { in bpf_object__elf_init()
1343 err = -errno; in bpf_object__elf_init()
1345 pr_warn("elf: failed to open %s: %s\n", obj->path, cp); in bpf_object__elf_init()
1349 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); in bpf_object__elf_init()
1353 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1354 err = -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1358 obj->efile.elf = elf; in bpf_object__elf_init()
1361 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1362 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); in bpf_object__elf_init()
1367 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1368 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); in bpf_object__elf_init()
1372 obj->efile.ehdr = ehdr = elf64_getehdr(elf); in bpf_object__elf_init()
1373 if (!obj->efile.ehdr) { in bpf_object__elf_init()
1374 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1375 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1379 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { in bpf_object__elf_init()
1381 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1382 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1387 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { in bpf_object__elf_init()
1389 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1390 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1395 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { in bpf_object__elf_init()
1396 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); in bpf_object__elf_init()
1397 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1410 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) in bpf_object__check_endianness()
1413 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) in bpf_object__check_endianness()
1418 pr_warn("elf: endianness mismatch in %s.\n", obj->path); in bpf_object__check_endianness()
1419 return -LIBBPF_ERRNO__ENDIAN; in bpf_object__check_endianness()
1426 pr_warn("invalid license section in %s\n", obj->path); in bpf_object__init_license()
1427 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_license()
1429 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't in bpf_object__init_license()
1432 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); in bpf_object__init_license()
1433 pr_debug("license of %s is %s\n", obj->path, obj->license); in bpf_object__init_license()
1443 pr_warn("invalid kver section in %s\n", obj->path); in bpf_object__init_kversion()
1444 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_kversion()
1447 obj->kern_version = kver; in bpf_object__init_kversion()
1448 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); in bpf_object__init_kversion()
1466 return -EINVAL; in find_elf_sec_sz()
1471 *size = data->d_size; in find_elf_sec_sz()
1475 return -ENOENT; in find_elf_sec_sz()
1480 Elf_Data *symbols = obj->efile.symbols; in find_elf_var_sym()
1484 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { in find_elf_var_sym()
1487 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) in find_elf_var_sym()
1490 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && in find_elf_var_sym()
1491 ELF64_ST_BIND(sym->st_info) != STB_WEAK) in find_elf_var_sym()
1494 sname = elf_sym_str(obj, sym->st_name); in find_elf_var_sym()
1497 return ERR_PTR(-EIO); in find_elf_var_sym()
1503 return ERR_PTR(-ENOENT); in find_elf_var_sym()
1510 fd = ensure_good_fd(memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC)); in create_placeholder_fd()
1512 return -errno; in create_placeholder_fd()
1521 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, in bpf_object__add_map()
1522 sizeof(*obj->maps), obj->nr_maps + 1); in bpf_object__add_map()
1526 map = &obj->maps[obj->nr_maps++]; in bpf_object__add_map()
1527 map->obj = obj; in bpf_object__add_map()
1540 map->fd = create_placeholder_fd(); in bpf_object__add_map()
1541 if (map->fd < 0) in bpf_object__add_map()
1542 return ERR_PTR(map->fd); in bpf_object__add_map()
1543 map->inner_map_fd = -1; in bpf_object__add_map()
1544 map->autocreate = true; in bpf_object__add_map()
1563 if (!map->mmaped) in bpf_map_mmap_resize()
1564 return -EINVAL; in bpf_map_mmap_resize()
1569 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_map_mmap_resize()
1571 return -errno; in bpf_map_mmap_resize()
1573 memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); in bpf_map_mmap_resize()
1574 munmap(map->mmaped, old_sz); in bpf_map_mmap_resize()
1575 map->mmaped = mmaped; in bpf_map_mmap_resize()
1609 * '.rodata.abracad' kernel and user-visible name. in internal_map_name()
1618 sfx_len = BPF_OBJ_NAME_LEN - 1; in internal_map_name()
1624 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); in internal_map_name()
1626 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, in internal_map_name()
1651 if (!map->btf_value_type_id) in map_is_mmapable()
1654 t = btf__type_by_id(obj->btf, map->btf_value_type_id); in map_is_mmapable()
1660 vt = btf__type_by_id(obj->btf, vsi->type); in map_is_mmapable()
1664 if (btf_var(vt)->linkage != BTF_VAR_STATIC) in map_is_mmapable()
1684 map->libbpf_type = type; in bpf_object__init_internal_map()
1685 map->sec_idx = sec_idx; in bpf_object__init_internal_map()
1686 map->sec_offset = 0; in bpf_object__init_internal_map()
1687 map->real_name = strdup(real_name); in bpf_object__init_internal_map()
1688 map->name = internal_map_name(obj, real_name); in bpf_object__init_internal_map()
1689 if (!map->real_name || !map->name) { in bpf_object__init_internal_map()
1690 zfree(&map->real_name); in bpf_object__init_internal_map()
1691 zfree(&map->name); in bpf_object__init_internal_map()
1692 return -ENOMEM; in bpf_object__init_internal_map()
1695 def = &map->def; in bpf_object__init_internal_map()
1696 def->type = BPF_MAP_TYPE_ARRAY; in bpf_object__init_internal_map()
1697 def->key_size = sizeof(int); in bpf_object__init_internal_map()
1698 def->value_size = data_sz; in bpf_object__init_internal_map()
1699 def->max_entries = 1; in bpf_object__init_internal_map()
1700 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG in bpf_object__init_internal_map()
1707 def->map_flags |= BPF_F_MMAPABLE; in bpf_object__init_internal_map()
1710 map->name, map->sec_idx, map->sec_offset, def->map_flags); in bpf_object__init_internal_map()
1712 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_object__init_internal_map()
1713 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, in bpf_object__init_internal_map()
1714 MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_object__init_internal_map()
1715 if (map->mmaped == MAP_FAILED) { in bpf_object__init_internal_map()
1716 err = -errno; in bpf_object__init_internal_map()
1717 map->mmaped = NULL; in bpf_object__init_internal_map()
1719 map->name, err); in bpf_object__init_internal_map()
1720 zfree(&map->real_name); in bpf_object__init_internal_map()
1721 zfree(&map->name); in bpf_object__init_internal_map()
1726 memcpy(map->mmaped, data, data_sz); in bpf_object__init_internal_map()
1728 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); in bpf_object__init_internal_map()
1739 * Populate obj->maps with libbpf internal maps. in bpf_object__init_global_data_maps()
1741 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { in bpf_object__init_global_data_maps()
1742 sec_desc = &obj->efile.secs[sec_idx]; in bpf_object__init_global_data_maps()
1745 if (!sec_desc->data || sec_desc->data->d_size == 0) in bpf_object__init_global_data_maps()
1748 switch (sec_desc->sec_type) { in bpf_object__init_global_data_maps()
1753 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
1754 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1757 obj->has_rodata = true; in bpf_object__init_global_data_maps()
1761 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
1762 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1769 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1787 for (i = 0; i < obj->nr_extern; i++) { in find_extern_by_name()
1788 if (strcmp(obj->externs[i].name, name) == 0) in find_extern_by_name()
1789 return &obj->externs[i]; in find_extern_by_name()
1794 static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, in set_kcfg_value_tri() argument
1797 switch (ext->kcfg.type) { in set_kcfg_value_tri()
1801 ext->name, value); in set_kcfg_value_tri()
1802 return -EINVAL; in set_kcfg_value_tri()
1822 ext->name, value); in set_kcfg_value_tri()
1823 return -EINVAL; in set_kcfg_value_tri()
1825 ext->is_set = true; in set_kcfg_value_tri()
1829 static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, in set_kcfg_value_str() argument
1834 if (ext->kcfg.type != KCFG_CHAR_ARR) { in set_kcfg_value_str()
1836 ext->name, value); in set_kcfg_value_str()
1837 return -EINVAL; in set_kcfg_value_str()
1841 if (value[len - 1] != '"') { in set_kcfg_value_str()
1843 ext->name, value); in set_kcfg_value_str()
1844 return -EINVAL; in set_kcfg_value_str()
1848 len -= 2; in set_kcfg_value_str()
1849 if (len >= ext->kcfg.sz) { in set_kcfg_value_str()
1851 ext->name, value, len, ext->kcfg.sz - 1); in set_kcfg_value_str()
1852 len = ext->kcfg.sz - 1; in set_kcfg_value_str()
1856 ext->is_set = true; in set_kcfg_value_str()
1868 err = -errno; in parse_u64()
1874 return -EINVAL; in parse_u64()
1879 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) in is_kcfg_value_in_range() argument
1881 int bit_sz = ext->kcfg.sz * 8; in is_kcfg_value_in_range()
1883 if (ext->kcfg.sz == 8) in is_kcfg_value_in_range()
1886 /* Validate that value stored in u64 fits in integer of `ext->sz` in is_kcfg_value_in_range()
1891 * -2^(Y-1) <= X <= 2^(Y-1) - 1 in is_kcfg_value_in_range()
1892 * 0 <= X + 2^(Y-1) <= 2^Y - 1 in is_kcfg_value_in_range()
1893 * 0 <= X + 2^(Y-1) < 2^Y in is_kcfg_value_in_range()
1895 * For unsigned target integer, check that all the (64 - Y) bits are in is_kcfg_value_in_range()
1898 if (ext->kcfg.is_signed) in is_kcfg_value_in_range()
1899 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); in is_kcfg_value_in_range()
1904 static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, in set_kcfg_value_num() argument
1907 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && in set_kcfg_value_num()
1908 ext->kcfg.type != KCFG_BOOL) { in set_kcfg_value_num()
1910 ext->name, (unsigned long long)value); in set_kcfg_value_num()
1911 return -EINVAL; in set_kcfg_value_num()
1913 if (ext->kcfg.type == KCFG_BOOL && value > 1) { in set_kcfg_value_num()
1915 ext->name, (unsigned long long)value); in set_kcfg_value_num()
1916 return -EINVAL; in set_kcfg_value_num()
1919 if (!is_kcfg_value_in_range(ext, value)) { in set_kcfg_value_num()
1921 ext->name, (unsigned long long)value, ext->kcfg.sz); in set_kcfg_value_num()
1922 return -ERANGE; in set_kcfg_value_num()
1924 switch (ext->kcfg.sz) { in set_kcfg_value_num()
1938 return -EINVAL; in set_kcfg_value_num()
1940 ext->is_set = true; in set_kcfg_value_num()
1947 struct extern_desc *ext; in bpf_object__process_kconfig_line() local
1959 return -EINVAL; in bpf_object__process_kconfig_line()
1964 if (buf[len - 1] == '\n') in bpf_object__process_kconfig_line()
1965 buf[len - 1] = '\0'; in bpf_object__process_kconfig_line()
1971 return -EINVAL; in bpf_object__process_kconfig_line()
1974 ext = find_extern_by_name(obj, buf); in bpf_object__process_kconfig_line()
1975 if (!ext || ext->is_set) in bpf_object__process_kconfig_line()
1978 ext_val = data + ext->kcfg.data_off; in bpf_object__process_kconfig_line()
1983 err = set_kcfg_value_tri(ext, ext_val, *value); in bpf_object__process_kconfig_line()
1986 err = set_kcfg_value_str(ext, ext_val, value); in bpf_object__process_kconfig_line()
1992 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); in bpf_object__process_kconfig_line()
1995 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { in bpf_object__process_kconfig_line()
1996 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); in bpf_object__process_kconfig_line()
1997 return -EINVAL; in bpf_object__process_kconfig_line()
1999 err = set_kcfg_value_num(ext, ext_val, num); in bpf_object__process_kconfig_line()
2004 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); in bpf_object__process_kconfig_line()
2016 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); in bpf_object__read_kconfig_file()
2018 return -EINVAL; in bpf_object__read_kconfig_file()
2020 return -ENAMETOOLONG; in bpf_object__read_kconfig_file()
2029 return -ENOENT; in bpf_object__read_kconfig_file()
2055 err = -errno; in bpf_object__read_kconfig_mem()
2056 pr_warn("failed to open in-memory Kconfig: %d\n", err); in bpf_object__read_kconfig_mem()
2063 pr_warn("error parsing in-memory Kconfig line '%s': %d\n", in bpf_object__read_kconfig_mem()
2075 struct extern_desc *last_ext = NULL, *ext; in bpf_object__init_kconfig_map() local
2079 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__init_kconfig_map()
2080 ext = &obj->externs[i]; in bpf_object__init_kconfig_map()
2081 if (ext->type == EXT_KCFG) in bpf_object__init_kconfig_map()
2082 last_ext = ext; in bpf_object__init_kconfig_map()
2088 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; in bpf_object__init_kconfig_map()
2090 ".kconfig", obj->efile.symbols_shndx, in bpf_object__init_kconfig_map()
2095 obj->kconfig_map_idx = obj->nr_maps - 1; in bpf_object__init_kconfig_map()
2110 *res_id = t->type; in skip_mods_and_typedefs()
2111 t = btf__type_by_id(btf, t->type); in skip_mods_and_typedefs()
2126 t = skip_mods_and_typedefs(btf, t->type, res_id); in resolve_func_ptr()
2173 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); in get_map_field_int()
2174 const char *name = btf__name_by_offset(btf, m->name_off); in get_map_field_int()
2184 arr_t = btf__type_by_id(btf, t->type); in get_map_field_int()
2187 map_name, name, t->type); in get_map_field_int()
2196 *res = arr_info->nelems; in get_map_field_int()
2206 return -EINVAL; in pathname_concat()
2208 return -ENAMETOOLONG; in pathname_concat()
2247 const char *name = btf__name_by_offset(btf, m->name_off); in parse_btf_map_def()
2251 return -EINVAL; in parse_btf_map_def()
2254 if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) in parse_btf_map_def()
2255 return -EINVAL; in parse_btf_map_def()
2256 map_def->parts |= MAP_DEF_MAP_TYPE; in parse_btf_map_def()
2258 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) in parse_btf_map_def()
2259 return -EINVAL; in parse_btf_map_def()
2260 map_def->parts |= MAP_DEF_MAX_ENTRIES; in parse_btf_map_def()
2262 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) in parse_btf_map_def()
2263 return -EINVAL; in parse_btf_map_def()
2264 map_def->parts |= MAP_DEF_MAP_FLAGS; in parse_btf_map_def()
2266 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) in parse_btf_map_def()
2267 return -EINVAL; in parse_btf_map_def()
2268 map_def->parts |= MAP_DEF_NUMA_NODE; in parse_btf_map_def()
2273 return -EINVAL; in parse_btf_map_def()
2274 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2276 map_name, map_def->key_size, sz); in parse_btf_map_def()
2277 return -EINVAL; in parse_btf_map_def()
2279 map_def->key_size = sz; in parse_btf_map_def()
2280 map_def->parts |= MAP_DEF_KEY_SIZE; in parse_btf_map_def()
2284 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2287 map_name, m->type); in parse_btf_map_def()
2288 return -EINVAL; in parse_btf_map_def()
2293 return -EINVAL; in parse_btf_map_def()
2295 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2298 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2301 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2303 map_name, map_def->key_size, (ssize_t)sz); in parse_btf_map_def()
2304 return -EINVAL; in parse_btf_map_def()
2306 map_def->key_size = sz; in parse_btf_map_def()
2307 map_def->key_type_id = t->type; in parse_btf_map_def()
2308 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; in parse_btf_map_def()
2313 return -EINVAL; in parse_btf_map_def()
2314 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2316 map_name, map_def->value_size, sz); in parse_btf_map_def()
2317 return -EINVAL; in parse_btf_map_def()
2319 map_def->value_size = sz; in parse_btf_map_def()
2320 map_def->parts |= MAP_DEF_VALUE_SIZE; in parse_btf_map_def()
2324 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2327 map_name, m->type); in parse_btf_map_def()
2328 return -EINVAL; in parse_btf_map_def()
2333 return -EINVAL; in parse_btf_map_def()
2335 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2338 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2341 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2343 map_name, map_def->value_size, (ssize_t)sz); in parse_btf_map_def()
2344 return -EINVAL; in parse_btf_map_def()
2346 map_def->value_size = sz; in parse_btf_map_def()
2347 map_def->value_type_id = t->type; in parse_btf_map_def()
2348 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; in parse_btf_map_def()
2351 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); in parse_btf_map_def()
2352 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; in parse_btf_map_def()
2353 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; in parse_btf_map_def()
2358 pr_warn("map '%s': multi-level inner maps not supported.\n", in parse_btf_map_def()
2360 return -ENOTSUP; in parse_btf_map_def()
2362 if (i != vlen - 1) { in parse_btf_map_def()
2365 return -EINVAL; in parse_btf_map_def()
2368 pr_warn("map '%s': should be map-in-map or prog-array.\n", in parse_btf_map_def()
2370 return -ENOTSUP; in parse_btf_map_def()
2372 if (map_def->value_size && map_def->value_size != 4) { in parse_btf_map_def()
2374 map_name, map_def->value_size); in parse_btf_map_def()
2375 return -EINVAL; in parse_btf_map_def()
2377 map_def->value_size = 4; in parse_btf_map_def()
2378 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2381 map_name, desc, m->type); in parse_btf_map_def()
2382 return -EINVAL; in parse_btf_map_def()
2384 if (!btf_is_array(t) || btf_array(t)->nelems) { in parse_btf_map_def()
2385 pr_warn("map '%s': %s spec is not a zero-sized array.\n", in parse_btf_map_def()
2387 return -EINVAL; in parse_btf_map_def()
2389 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); in parse_btf_map_def()
2393 return -EINVAL; in parse_btf_map_def()
2395 t = skip_mods_and_typedefs(btf, t->type, NULL); in parse_btf_map_def()
2398 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", in parse_btf_map_def()
2400 return -EINVAL; in parse_btf_map_def()
2405 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", in parse_btf_map_def()
2407 return -EINVAL; in parse_btf_map_def()
2415 map_def->parts |= MAP_DEF_INNER_MAP; in parse_btf_map_def()
2421 return -EINVAL; in parse_btf_map_def()
2424 return -EINVAL; in parse_btf_map_def()
2428 return -EINVAL; in parse_btf_map_def()
2430 map_def->pinning = val; in parse_btf_map_def()
2431 map_def->parts |= MAP_DEF_PINNING; in parse_btf_map_def()
2436 return -EINVAL; in parse_btf_map_def()
2437 map_def->map_extra = map_extra; in parse_btf_map_def()
2438 map_def->parts |= MAP_DEF_MAP_EXTRA; in parse_btf_map_def()
2442 return -ENOTSUP; in parse_btf_map_def()
2448 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { in parse_btf_map_def()
2450 return -EINVAL; in parse_btf_map_def()
2465 * a power-of-2 multiple of kernel's page size. If user diligently in adjust_ringbuf_sz()
2472 * user-set size to satisfy both user size request and kernel in adjust_ringbuf_sz()
2481 * very close to UINT_MAX but is not a power-of-2 multiple of in adjust_ringbuf_sz()
2489 return map->def.type == BPF_MAP_TYPE_RINGBUF || in map_is_ringbuf()
2490 map->def.type == BPF_MAP_TYPE_USER_RINGBUF; in map_is_ringbuf()
2495 map->def.type = def->map_type; in fill_map_from_def()
2496 map->def.key_size = def->key_size; in fill_map_from_def()
2497 map->def.value_size = def->value_size; in fill_map_from_def()
2498 map->def.max_entries = def->max_entries; in fill_map_from_def()
2499 map->def.map_flags = def->map_flags; in fill_map_from_def()
2500 map->map_extra = def->map_extra; in fill_map_from_def()
2502 map->numa_node = def->numa_node; in fill_map_from_def()
2503 map->btf_key_type_id = def->key_type_id; in fill_map_from_def()
2504 map->btf_value_type_id = def->value_type_id; in fill_map_from_def()
2506 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in fill_map_from_def()
2508 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in fill_map_from_def()
2510 if (def->parts & MAP_DEF_MAP_TYPE) in fill_map_from_def()
2511 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); in fill_map_from_def()
2513 if (def->parts & MAP_DEF_KEY_TYPE) in fill_map_from_def()
2515 map->name, def->key_type_id, def->key_size); in fill_map_from_def()
2516 else if (def->parts & MAP_DEF_KEY_SIZE) in fill_map_from_def()
2517 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); in fill_map_from_def()
2519 if (def->parts & MAP_DEF_VALUE_TYPE) in fill_map_from_def()
2521 map->name, def->value_type_id, def->value_size); in fill_map_from_def()
2522 else if (def->parts & MAP_DEF_VALUE_SIZE) in fill_map_from_def()
2523 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); in fill_map_from_def()
2525 if (def->parts & MAP_DEF_MAX_ENTRIES) in fill_map_from_def()
2526 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); in fill_map_from_def()
2527 if (def->parts & MAP_DEF_MAP_FLAGS) in fill_map_from_def()
2528 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); in fill_map_from_def()
2529 if (def->parts & MAP_DEF_MAP_EXTRA) in fill_map_from_def()
2530 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, in fill_map_from_def()
2531 (unsigned long long)def->map_extra); in fill_map_from_def()
2532 if (def->parts & MAP_DEF_PINNING) in fill_map_from_def()
2533 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); in fill_map_from_def()
2534 if (def->parts & MAP_DEF_NUMA_NODE) in fill_map_from_def()
2535 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); in fill_map_from_def()
2537 if (def->parts & MAP_DEF_INNER_MAP) in fill_map_from_def()
2538 pr_debug("map '%s': found inner map definition.\n", map->name); in fill_map_from_def()
2566 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__init_user_btf_map()
2568 map_name = btf__name_by_offset(obj->btf, var->name_off); in bpf_object__init_user_btf_map()
2572 return -EINVAL; in bpf_object__init_user_btf_map()
2574 if ((__u64)vi->offset + vi->size > data->d_size) { in bpf_object__init_user_btf_map()
2576 return -EINVAL; in bpf_object__init_user_btf_map()
2581 return -EINVAL; in bpf_object__init_user_btf_map()
2583 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { in bpf_object__init_user_btf_map()
2585 map_name, btf_var_linkage_str(var_extra->linkage)); in bpf_object__init_user_btf_map()
2586 return -EOPNOTSUPP; in bpf_object__init_user_btf_map()
2589 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__init_user_btf_map()
2593 return -EINVAL; in bpf_object__init_user_btf_map()
2595 if (def->size > vi->size) { in bpf_object__init_user_btf_map()
2597 return -EINVAL; in bpf_object__init_user_btf_map()
2603 map->name = strdup(map_name); in bpf_object__init_user_btf_map()
2604 if (!map->name) { in bpf_object__init_user_btf_map()
2606 return -ENOMEM; in bpf_object__init_user_btf_map()
2608 map->libbpf_type = LIBBPF_MAP_UNSPEC; in bpf_object__init_user_btf_map()
2609 map->def.type = BPF_MAP_TYPE_UNSPEC; in bpf_object__init_user_btf_map()
2610 map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2611 map->sec_offset = vi->offset; in bpf_object__init_user_btf_map()
2612 map->btf_var_idx = var_idx; in bpf_object__init_user_btf_map()
2614 map_name, map->sec_idx, map->sec_offset); in bpf_object__init_user_btf_map()
2616 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); in bpf_object__init_user_btf_map()
2625 pr_warn("map '%s': couldn't build pin path.\n", map->name); in bpf_object__init_user_btf_map()
2631 map->inner_map = calloc(1, sizeof(*map->inner_map)); in bpf_object__init_user_btf_map()
2632 if (!map->inner_map) in bpf_object__init_user_btf_map()
2633 return -ENOMEM; in bpf_object__init_user_btf_map()
2634 map->inner_map->fd = create_placeholder_fd(); in bpf_object__init_user_btf_map()
2635 if (map->inner_map->fd < 0) in bpf_object__init_user_btf_map()
2636 return map->inner_map->fd; in bpf_object__init_user_btf_map()
2637 map->inner_map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2638 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); in bpf_object__init_user_btf_map()
2639 if (!map->inner_map->name) in bpf_object__init_user_btf_map()
2640 return -ENOMEM; in bpf_object__init_user_btf_map()
2641 sprintf(map->inner_map->name, "%s.inner", map_name); in bpf_object__init_user_btf_map()
2643 fill_map_from_def(map->inner_map, &inner_def); in bpf_object__init_user_btf_map()
2663 if (obj->efile.btf_maps_shndx < 0) in bpf_object__init_user_btf_maps()
2666 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); in bpf_object__init_user_btf_maps()
2670 MAPS_ELF_SEC, obj->path); in bpf_object__init_user_btf_maps()
2671 return -EINVAL; in bpf_object__init_user_btf_maps()
2674 nr_types = btf__type_cnt(obj->btf); in bpf_object__init_user_btf_maps()
2676 t = btf__type_by_id(obj->btf, i); in bpf_object__init_user_btf_maps()
2679 name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__init_user_btf_maps()
2682 obj->efile.btf_maps_sec_btf_id = i; in bpf_object__init_user_btf_maps()
2689 return -ENOENT; in bpf_object__init_user_btf_maps()
2695 obj->efile.btf_maps_shndx, in bpf_object__init_user_btf_maps()
2731 return sh->sh_flags & SHF_EXECINSTR; in section_have_execinstr()
2766 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); in bpf_object__sanitize_btf()
2772 t->size = 1; in bpf_object__sanitize_btf()
2781 name = (char *)btf__name_by_offset(btf, t->name_off); in bpf_object__sanitize_btf()
2789 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); in bpf_object__sanitize_btf()
2792 m->offset = v->offset * 8; in bpf_object__sanitize_btf()
2793 m->type = v->type; in bpf_object__sanitize_btf()
2795 vt = (void *)btf__type_by_id(btf, v->type); in bpf_object__sanitize_btf()
2796 m->name_off = vt->name_off; in bpf_object__sanitize_btf()
2801 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); in bpf_object__sanitize_btf()
2802 t->size = sizeof(__u32); /* kernel enforced */ in bpf_object__sanitize_btf()
2805 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); in bpf_object__sanitize_btf()
2808 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); in bpf_object__sanitize_btf()
2810 /* replace FLOAT with an equally-sized empty STRUCT; in bpf_object__sanitize_btf()
2814 t->name_off = 0; in bpf_object__sanitize_btf()
2815 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); in bpf_object__sanitize_btf()
2818 t->name_off = 0; in bpf_object__sanitize_btf()
2819 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); in bpf_object__sanitize_btf()
2822 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); in bpf_object__sanitize_btf()
2837 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); in bpf_object__sanitize_btf()
2839 m->type = enum64_placeholder_id; in bpf_object__sanitize_btf()
2840 m->offset = 0; in bpf_object__sanitize_btf()
2850 return obj->efile.btf_maps_shndx >= 0 || in libbpf_needs_btf()
2851 obj->efile.st_ops_shndx >= 0 || in libbpf_needs_btf()
2852 obj->efile.st_ops_link_shndx >= 0 || in libbpf_needs_btf()
2853 obj->nr_extern > 0; in libbpf_needs_btf()
2858 return obj->efile.st_ops_shndx >= 0 || obj->efile.st_ops_link_shndx >= 0; in kernel_needs_btf()
2865 int err = -ENOENT; in bpf_object__init_btf()
2868 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); in bpf_object__init_btf()
2869 err = libbpf_get_error(obj->btf); in bpf_object__init_btf()
2871 obj->btf = NULL; in bpf_object__init_btf()
2875 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__init_btf()
2876 btf__set_pointer_size(obj->btf, 8); in bpf_object__init_btf()
2882 if (!obj->btf) { in bpf_object__init_btf()
2887 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); in bpf_object__init_btf()
2888 err = libbpf_get_error(obj->btf_ext); in bpf_object__init_btf()
2892 obj->btf_ext = NULL; in bpf_object__init_btf()
2896 /* setup .BTF.ext to ELF section mapping */ in bpf_object__init_btf()
2897 ext_segs[0] = &obj->btf_ext->func_info; in bpf_object__init_btf()
2898 ext_segs[1] = &obj->btf_ext->line_info; in bpf_object__init_btf()
2899 ext_segs[2] = &obj->btf_ext->core_relo_info; in bpf_object__init_btf()
2906 if (seg->sec_cnt == 0) in bpf_object__init_btf()
2909 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); in bpf_object__init_btf()
2910 if (!seg->sec_idxs) { in bpf_object__init_btf()
2911 err = -ENOMEM; in bpf_object__init_btf()
2922 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__init_btf()
2929 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); in bpf_object__init_btf()
2946 return a->offset - b->offset; in compare_vsi_off()
2953 const char *sec_name = btf__name_by_offset(btf, t->name_off); in btf_fixup_datasec()
2960 return -ENOENT; in btf_fixup_datasec()
2963 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and in btf_fixup_datasec()
2975 * to be optional. But the STV_HIDDEN handling is non-optional for any in btf_fixup_datasec()
2976 * non-extern DATASEC, so the variable fixup loop below handles both in btf_fixup_datasec()
2977 * functions at the same time, paying the cost of BTF VAR <-> ELF in btf_fixup_datasec()
2980 if (t->size == 0) { in btf_fixup_datasec()
2985 return -ENOENT; in btf_fixup_datasec()
2988 t->size = size; in btf_fixup_datasec()
2998 t_var = btf__type_by_id(btf, vsi->type); in btf_fixup_datasec()
3000 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); in btf_fixup_datasec()
3001 return -EINVAL; in btf_fixup_datasec()
3005 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) in btf_fixup_datasec()
3008 var_name = btf__name_by_offset(btf, t_var->name_off); in btf_fixup_datasec()
3012 return -ENOENT; in btf_fixup_datasec()
3019 return -ENOENT; in btf_fixup_datasec()
3023 vsi->offset = sym->st_value; in btf_fixup_datasec()
3032 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in btf_fixup_datasec()
3033 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) in btf_fixup_datasec()
3034 var->linkage = BTF_VAR_STATIC; in btf_fixup_datasec()
3046 if (!obj->btf) in bpf_object_fixup_btf()
3049 n = btf__type_cnt(obj->btf); in bpf_object_fixup_btf()
3051 struct btf_type *t = btf_type_by_id(obj->btf, i); in bpf_object_fixup_btf()
3059 err = btf_fixup_datasec(obj, obj->btf, t); in bpf_object_fixup_btf()
3070 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || in prog_needs_vmlinux_btf()
3071 prog->type == BPF_PROG_TYPE_LSM) in prog_needs_vmlinux_btf()
3077 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) in prog_needs_vmlinux_btf()
3094 /* CO-RE relocations need kernel BTF, only when btf_custom_path in obj_needs_vmlinux_btf()
3097 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) in obj_needs_vmlinux_btf()
3101 for (i = 0; i < obj->nr_extern; i++) { in obj_needs_vmlinux_btf()
3102 const struct extern_desc *ext; in obj_needs_vmlinux_btf() local
3104 ext = &obj->externs[i]; in obj_needs_vmlinux_btf()
3105 if (ext->type == EXT_KSYM && ext->ksym.type_id) in obj_needs_vmlinux_btf()
3110 if (!prog->autoload) in obj_needs_vmlinux_btf()
3129 if (obj->btf_vmlinux || obj->gen_loader) in bpf_object__load_vmlinux_btf()
3135 obj->btf_vmlinux = btf__load_vmlinux_btf(); in bpf_object__load_vmlinux_btf()
3136 err = libbpf_get_error(obj->btf_vmlinux); in bpf_object__load_vmlinux_btf()
3139 obj->btf_vmlinux = NULL; in bpf_object__load_vmlinux_btf()
3147 struct btf *kern_btf = obj->btf; in bpf_object__sanitize_and_load_btf()
3151 if (!obj->btf) in bpf_object__sanitize_and_load_btf()
3156 err = -EOPNOTSUPP; in bpf_object__sanitize_and_load_btf()
3171 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sanitize_and_load_btf()
3172 struct bpf_program *prog = &obj->programs[i]; in bpf_object__sanitize_and_load_btf()
3177 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) in bpf_object__sanitize_and_load_btf()
3180 n = btf__type_cnt(obj->btf); in bpf_object__sanitize_and_load_btf()
3182 t = btf_type_by_id(obj->btf, j); in bpf_object__sanitize_and_load_btf()
3186 name = btf__str_by_offset(obj->btf, t->name_off); in bpf_object__sanitize_and_load_btf()
3187 if (strcmp(name, prog->name) != 0) in bpf_object__sanitize_and_load_btf()
3190 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); in bpf_object__sanitize_and_load_btf()
3201 raw_data = btf__raw_data(obj->btf, &sz); in bpf_object__sanitize_and_load_btf()
3207 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__sanitize_and_load_btf()
3208 btf__set_pointer_size(obj->btf, 8); in bpf_object__sanitize_and_load_btf()
3214 if (obj->gen_loader) { in bpf_object__sanitize_and_load_btf()
3219 return -ENOMEM; in bpf_object__sanitize_and_load_btf()
3220 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); in bpf_object__sanitize_and_load_btf()
3222 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. in bpf_object__sanitize_and_load_btf()
3227 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, in bpf_object__sanitize_and_load_btf()
3228 obj->log_level ? 1 : 0); in bpf_object__sanitize_and_load_btf()
3233 btf__set_fd(obj->btf, btf__fd(kern_btf)); in bpf_object__sanitize_and_load_btf()
3234 btf__set_fd(kern_btf, -1); in bpf_object__sanitize_and_load_btf()
3254 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); in elf_sym_str()
3257 off, obj->path, elf_errmsg(-1)); in elf_sym_str()
3268 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); in elf_sec_str()
3271 off, obj->path, elf_errmsg(-1)); in elf_sec_str()
3282 scn = elf_getscn(obj->efile.elf, idx); in elf_sec_by_idx()
3285 idx, obj->path, elf_errmsg(-1)); in elf_sec_by_idx()
3294 Elf *elf = obj->efile.elf; in elf_sec_by_name()
3320 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_hdr()
3339 name = elf_sec_str(obj, sh->sh_name); in elf_sec_name()
3342 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_name()
3360 obj->path, elf_errmsg(-1)); in elf_sec_data()
3369 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) in elf_sym_by_idx()
3372 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; in elf_sym_by_idx()
3377 if (idx >= data->d_size / sizeof(Elf64_Rel)) in elf_rel_by_idx()
3380 return (Elf64_Rel *)data->d_buf + idx; in elf_rel_by_idx()
3392 if (hdr->sh_type == SHT_STRTAB) in ignore_elf_section()
3396 if (hdr->sh_type == SHT_LLVM_ADDRSIG) in ignore_elf_section()
3400 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && in ignore_elf_section()
3409 name += sizeof(".rel") - 1; in ignore_elf_section()
3414 /* .BTF and .BTF.ext don't need relocations */ in ignore_elf_section()
3428 if (a->sec_idx != b->sec_idx) in cmp_progs()
3429 return a->sec_idx < b->sec_idx ? -1 : 1; in cmp_progs()
3432 return a->sec_insn_off < b->sec_insn_off ? -1 : 1; in cmp_progs()
3438 Elf *elf = obj->efile.elf; in bpf_object__elf_collect()
3447 /* ELF section indices are 0-based, but sec #0 is special "invalid" in bpf_object__elf_collect()
3452 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { in bpf_object__elf_collect()
3454 obj->path, elf_errmsg(-1)); in bpf_object__elf_collect()
3455 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3457 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); in bpf_object__elf_collect()
3458 if (!obj->efile.secs) in bpf_object__elf_collect()
3459 return -ENOMEM; in bpf_object__elf_collect()
3468 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3470 if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3471 if (obj->efile.symbols) { in bpf_object__elf_collect()
3472 pr_warn("elf: multiple symbol tables in %s\n", obj->path); in bpf_object__elf_collect()
3473 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3478 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3482 obj->efile.symbols = data; in bpf_object__elf_collect()
3483 obj->efile.symbols_shndx = idx; in bpf_object__elf_collect()
3484 obj->efile.strtabidx = sh->sh_link; in bpf_object__elf_collect()
3488 if (!obj->efile.symbols) { in bpf_object__elf_collect()
3490 obj->path); in bpf_object__elf_collect()
3491 return -ENOENT; in bpf_object__elf_collect()
3497 sec_desc = &obj->efile.secs[idx]; in bpf_object__elf_collect()
3501 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3503 name = elf_sec_str(obj, sh->sh_name); in bpf_object__elf_collect()
3505 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3512 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3515 idx, name, (unsigned long)data->d_size, in bpf_object__elf_collect()
3516 (int)sh->sh_link, (unsigned long)sh->sh_flags, in bpf_object__elf_collect()
3517 (int)sh->sh_type); in bpf_object__elf_collect()
3520 err = bpf_object__init_license(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3524 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3529 return -ENOTSUP; in bpf_object__elf_collect()
3531 obj->efile.btf_maps_shndx = idx; in bpf_object__elf_collect()
3533 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3534 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3537 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3538 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3540 } else if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3542 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { in bpf_object__elf_collect()
3543 if (sh->sh_flags & SHF_EXECINSTR) { in bpf_object__elf_collect()
3545 obj->efile.text_shndx = idx; in bpf_object__elf_collect()
3551 sec_desc->sec_type = SEC_DATA; in bpf_object__elf_collect()
3552 sec_desc->shdr = sh; in bpf_object__elf_collect()
3553 sec_desc->data = data; in bpf_object__elf_collect()
3556 sec_desc->sec_type = SEC_RODATA; in bpf_object__elf_collect()
3557 sec_desc->shdr = sh; in bpf_object__elf_collect()
3558 sec_desc->data = data; in bpf_object__elf_collect()
3560 obj->efile.st_ops_data = data; in bpf_object__elf_collect()
3561 obj->efile.st_ops_shndx = idx; in bpf_object__elf_collect()
3563 obj->efile.st_ops_link_data = data; in bpf_object__elf_collect()
3564 obj->efile.st_ops_link_shndx = idx; in bpf_object__elf_collect()
3569 } else if (sh->sh_type == SHT_REL) { in bpf_object__elf_collect()
3570 int targ_sec_idx = sh->sh_info; /* points to other section */ in bpf_object__elf_collect()
3572 if (sh->sh_entsize != sizeof(Elf64_Rel) || in bpf_object__elf_collect()
3573 targ_sec_idx >= obj->efile.sec_cnt) in bpf_object__elf_collect()
3574 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3587 sec_desc->sec_type = SEC_RELO; in bpf_object__elf_collect()
3588 sec_desc->shdr = sh; in bpf_object__elf_collect()
3589 sec_desc->data = data; in bpf_object__elf_collect()
3590 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || in bpf_object__elf_collect()
3592 sec_desc->sec_type = SEC_BSS; in bpf_object__elf_collect()
3593 sec_desc->shdr = sh; in bpf_object__elf_collect()
3594 sec_desc->data = data; in bpf_object__elf_collect()
3597 (size_t)sh->sh_size); in bpf_object__elf_collect()
3601 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { in bpf_object__elf_collect()
3602 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); in bpf_object__elf_collect()
3603 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3606 /* sort BPF programs by section name and in-section instruction offset in bpf_object__elf_collect()
3609 if (obj->nr_programs) in bpf_object__elf_collect()
3610 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); in bpf_object__elf_collect()
3617 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_extern()
3619 return sym->st_shndx == SHN_UNDEF && in sym_is_extern()
3621 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; in sym_is_extern()
3626 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_subprog()
3627 int type = ELF64_ST_TYPE(sym->st_info); in sym_is_subprog()
3630 if (sym->st_shndx != text_shndx) in sym_is_subprog()
3648 return -ESRCH; in find_extern_btf_id()
3657 tname = btf__name_by_offset(btf, t->name_off); in find_extern_btf_id()
3662 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) in find_extern_btf_id()
3663 return -EINVAL; in find_extern_btf_id()
3666 return -EINVAL; in find_extern_btf_id()
3671 return -ENOENT; in find_extern_btf_id()
3680 return -ESRCH; in find_extern_sec_btf_id()
3691 if (vs->type == ext_btf_id) in find_extern_sec_btf_id()
3696 return -ENOENT; in find_extern_sec_btf_id()
3706 name = btf__name_by_offset(btf, t->name_off); in find_kcfg_type()
3715 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; in find_kcfg_type()
3718 if (t->size == 1) in find_kcfg_type()
3720 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) in find_kcfg_type()
3725 if (t->size != 4) in find_kcfg_type()
3735 if (btf_array(t)->nelems == 0) in find_kcfg_type()
3737 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) in find_kcfg_type()
3750 if (a->type != b->type) in cmp_externs()
3751 return a->type < b->type ? -1 : 1; in cmp_externs()
3753 if (a->type == EXT_KCFG) { in cmp_externs()
3755 if (a->kcfg.align != b->kcfg.align) in cmp_externs()
3756 return a->kcfg.align > b->kcfg.align ? -1 : 1; in cmp_externs()
3758 if (a->kcfg.sz != b->kcfg.sz) in cmp_externs()
3759 return a->kcfg.sz < b->kcfg.sz ? -1 : 1; in cmp_externs()
3763 return strcmp(a->name, b->name); in cmp_externs()
3801 vt = btf__type_by_id(btf, vs->type); in add_dummy_ksym_var()
3825 struct extern_desc *ext; in bpf_object__collect_externs() local
3832 if (!obj->efile.symbols) in bpf_object__collect_externs()
3835 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); in bpf_object__collect_externs()
3837 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) in bpf_object__collect_externs()
3838 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
3840 dummy_var_btf_id = add_dummy_ksym_var(obj->btf); in bpf_object__collect_externs()
3844 n = sh->sh_size / sh->sh_entsize; in bpf_object__collect_externs()
3851 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
3854 ext_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_externs()
3858 ext = obj->externs; in bpf_object__collect_externs()
3859 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); in bpf_object__collect_externs()
3860 if (!ext) in bpf_object__collect_externs()
3861 return -ENOMEM; in bpf_object__collect_externs()
3862 obj->externs = ext; in bpf_object__collect_externs()
3863 ext = &ext[obj->nr_extern]; in bpf_object__collect_externs()
3864 memset(ext, 0, sizeof(*ext)); in bpf_object__collect_externs()
3865 obj->nr_extern++; in bpf_object__collect_externs()
3867 ext->btf_id = find_extern_btf_id(obj->btf, ext_name); in bpf_object__collect_externs()
3868 if (ext->btf_id <= 0) { in bpf_object__collect_externs()
3870 ext_name, ext->btf_id); in bpf_object__collect_externs()
3871 return ext->btf_id; in bpf_object__collect_externs()
3873 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
3874 ext->name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
3875 ext->sym_idx = i; in bpf_object__collect_externs()
3876 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; in bpf_object__collect_externs()
3878 ext_essent_len = bpf_core_essential_name_len(ext->name); in bpf_object__collect_externs()
3879 ext->essent_name = NULL; in bpf_object__collect_externs()
3880 if (ext_essent_len != strlen(ext->name)) { in bpf_object__collect_externs()
3881 ext->essent_name = strndup(ext->name, ext_essent_len); in bpf_object__collect_externs()
3882 if (!ext->essent_name) in bpf_object__collect_externs()
3883 return -ENOMEM; in bpf_object__collect_externs()
3886 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
3887 if (ext->sec_btf_id <= 0) { in bpf_object__collect_externs()
3889 ext_name, ext->btf_id, ext->sec_btf_id); in bpf_object__collect_externs()
3890 return ext->sec_btf_id; in bpf_object__collect_externs()
3892 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); in bpf_object__collect_externs()
3893 sec_name = btf__name_by_offset(obj->btf, sec->name_off); in bpf_object__collect_externs()
3898 ext->name, KCONFIG_SEC); in bpf_object__collect_externs()
3899 return -ENOTSUP; in bpf_object__collect_externs()
3902 ext->type = EXT_KCFG; in bpf_object__collect_externs()
3903 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); in bpf_object__collect_externs()
3904 if (ext->kcfg.sz <= 0) { in bpf_object__collect_externs()
3906 ext_name, ext->kcfg.sz); in bpf_object__collect_externs()
3907 return ext->kcfg.sz; in bpf_object__collect_externs()
3909 ext->kcfg.align = btf__align_of(obj->btf, t->type); in bpf_object__collect_externs()
3910 if (ext->kcfg.align <= 0) { in bpf_object__collect_externs()
3912 ext_name, ext->kcfg.align); in bpf_object__collect_externs()
3913 return -EINVAL; in bpf_object__collect_externs()
3915 ext->kcfg.type = find_kcfg_type(obj->btf, t->type, in bpf_object__collect_externs()
3916 &ext->kcfg.is_signed); in bpf_object__collect_externs()
3917 if (ext->kcfg.type == KCFG_UNKNOWN) { in bpf_object__collect_externs()
3919 return -ENOTSUP; in bpf_object__collect_externs()
3923 ext->type = EXT_KSYM; in bpf_object__collect_externs()
3924 skip_mods_and_typedefs(obj->btf, t->type, in bpf_object__collect_externs()
3925 &ext->ksym.type_id); in bpf_object__collect_externs()
3928 return -ENOTSUP; in bpf_object__collect_externs()
3931 pr_debug("collected %d externs total\n", obj->nr_extern); in bpf_object__collect_externs()
3933 if (!obj->nr_extern) in bpf_object__collect_externs()
3937 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); in bpf_object__collect_externs()
3941 * pretending that each extern is a 8-byte variable in bpf_object__collect_externs()
3944 /* find existing 4-byte integer type in BTF to use for fake in bpf_object__collect_externs()
3947 int int_btf_id = find_int_btf_id(obj->btf); in bpf_object__collect_externs()
3949 * will be used to replace the vs->type and in bpf_object__collect_externs()
3955 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); in bpf_object__collect_externs()
3956 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
3957 ext = &obj->externs[i]; in bpf_object__collect_externs()
3958 if (ext->type != EXT_KSYM) in bpf_object__collect_externs()
3961 i, ext->sym_idx, ext->name); in bpf_object__collect_externs()
3970 vt = (void *)btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
3971 ext_name = btf__name_by_offset(obj->btf, vt->name_off); in bpf_object__collect_externs()
3972 ext = find_extern_by_name(obj, ext_name); in bpf_object__collect_externs()
3973 if (!ext) { in bpf_object__collect_externs()
3976 return -ESRCH; in bpf_object__collect_externs()
3983 func_proto = btf__type_by_id(obj->btf, in bpf_object__collect_externs()
3984 vt->type); in bpf_object__collect_externs()
3992 dummy_var->name_off; in bpf_object__collect_externs()
3993 vs->type = dummy_var_btf_id; in bpf_object__collect_externs()
3994 vt->info &= ~0xffff; in bpf_object__collect_externs()
3995 vt->info |= BTF_FUNC_GLOBAL; in bpf_object__collect_externs()
3997 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
3998 vt->type = int_btf_id; in bpf_object__collect_externs()
4000 vs->offset = off; in bpf_object__collect_externs()
4001 vs->size = sizeof(int); in bpf_object__collect_externs()
4003 sec->size = off; in bpf_object__collect_externs()
4010 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
4011 ext = &obj->externs[i]; in bpf_object__collect_externs()
4012 if (ext->type != EXT_KCFG) in bpf_object__collect_externs()
4015 ext->kcfg.data_off = roundup(off, ext->kcfg.align); in bpf_object__collect_externs()
4016 off = ext->kcfg.data_off + ext->kcfg.sz; in bpf_object__collect_externs()
4018 i, ext->sym_idx, ext->kcfg.data_off, ext->name); in bpf_object__collect_externs()
4020 sec->size = off; in bpf_object__collect_externs()
4025 t = btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
4026 ext_name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
4027 ext = find_extern_by_name(obj, ext_name); in bpf_object__collect_externs()
4028 if (!ext) { in bpf_object__collect_externs()
4031 return -ESRCH; in bpf_object__collect_externs()
4033 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
4034 vs->offset = ext->kcfg.data_off; in bpf_object__collect_externs()
4042 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; in prog_is_subprog()
4054 if (!strcmp(prog->name, name)) in bpf_object__find_program_by_name()
4063 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__shndx_is_data()
4076 return shndx == obj->efile.btf_maps_shndx; in bpf_object__shndx_is_maps()
4082 if (shndx == obj->efile.symbols_shndx) in bpf_object__section_to_libbpf_map_type()
4085 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__section_to_libbpf_map_type()
4102 struct bpf_insn *insn = &prog->insns[insn_idx]; in bpf_program__record_reloc()
4103 size_t map_idx, nr_maps = prog->obj->nr_maps; in bpf_program__record_reloc()
4104 struct bpf_object *obj = prog->obj; in bpf_program__record_reloc()
4105 __u32 shdr_idx = sym->st_shndx; in bpf_program__record_reloc()
4112 prog->name, sym_name, insn_idx, insn->code); in bpf_program__record_reloc()
4113 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4117 int sym_idx = ELF64_R_SYM(rel->r_info); in bpf_program__record_reloc()
4118 int i, n = obj->nr_extern; in bpf_program__record_reloc()
4119 struct extern_desc *ext; in bpf_program__record_reloc() local
4122 ext = &obj->externs[i]; in bpf_program__record_reloc()
4123 if (ext->sym_idx == sym_idx) in bpf_program__record_reloc()
4128 prog->name, sym_name, sym_idx); in bpf_program__record_reloc()
4129 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4132 prog->name, i, ext->name, ext->sym_idx, insn_idx); in bpf_program__record_reloc()
4133 if (insn->code == (BPF_JMP | BPF_CALL)) in bpf_program__record_reloc()
4134 reloc_desc->type = RELO_EXTERN_CALL; in bpf_program__record_reloc()
4136 reloc_desc->type = RELO_EXTERN_LD64; in bpf_program__record_reloc()
4137 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4138 reloc_desc->ext_idx = i; in bpf_program__record_reloc()
4142 /* sub-program call relocation */ in bpf_program__record_reloc()
4144 if (insn->src_reg != BPF_PSEUDO_CALL) { in bpf_program__record_reloc()
4145 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); in bpf_program__record_reloc()
4146 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4149 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { in bpf_program__record_reloc()
4152 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4153 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4155 if (sym->st_value % BPF_INSN_SZ) { in bpf_program__record_reloc()
4157 prog->name, sym_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4158 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4160 reloc_desc->type = RELO_CALL; in bpf_program__record_reloc()
4161 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4162 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4168 prog->name, sym_name, shdr_idx); in bpf_program__record_reloc()
4169 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4173 if (sym_is_subprog(sym, obj->efile.text_shndx)) { in bpf_program__record_reloc()
4174 /* global_func: sym->st_value = offset in the section, insn->imm = 0. in bpf_program__record_reloc()
4175 * local_func: sym->st_value = 0, insn->imm = offset in the section. in bpf_program__record_reloc()
4177 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { in bpf_program__record_reloc()
4179 prog->name, sym_name, (size_t)sym->st_value, insn->imm); in bpf_program__record_reloc()
4180 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4183 reloc_desc->type = RELO_SUBPROG_ADDR; in bpf_program__record_reloc()
4184 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4185 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4196 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4197 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4200 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4201 if (map->libbpf_type != type || in bpf_program__record_reloc()
4202 map->sec_idx != sym->st_shndx || in bpf_program__record_reloc()
4203 map->sec_offset != sym->st_value) in bpf_program__record_reloc()
4206 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4207 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4212 prog->name, sym_sec_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4213 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4215 reloc_desc->type = RELO_LD64; in bpf_program__record_reloc()
4216 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4217 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4218 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ in bpf_program__record_reloc()
4225 prog->name, sym_sec_name); in bpf_program__record_reloc()
4226 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4229 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4230 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) in bpf_program__record_reloc()
4233 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4234 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4239 prog->name, sym_sec_name); in bpf_program__record_reloc()
4240 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4243 reloc_desc->type = RELO_DATA; in bpf_program__record_reloc()
4244 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4245 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4246 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4252 return insn_idx >= prog->sec_insn_off && in prog_contains_insn()
4253 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; in prog_contains_insn()
4259 int l = 0, r = obj->nr_programs - 1, m; in find_prog_by_sec_insn()
4262 if (!obj->nr_programs) in find_prog_by_sec_insn()
4266 m = l + (r - l + 1) / 2; in find_prog_by_sec_insn()
4267 prog = &obj->programs[m]; in find_prog_by_sec_insn()
4269 if (prog->sec_idx < sec_idx || in find_prog_by_sec_insn()
4270 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) in find_prog_by_sec_insn()
4273 r = m - 1; in find_prog_by_sec_insn()
4278 prog = &obj->programs[l]; in find_prog_by_sec_insn()
4279 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) in find_prog_by_sec_insn()
4288 size_t sec_idx = shdr->sh_info, sym_idx; in bpf_object__collect_prog_relos()
4299 if (sec_idx >= obj->efile.sec_cnt) in bpf_object__collect_prog_relos()
4300 return -EINVAL; in bpf_object__collect_prog_relos()
4305 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4307 relo_sec_name = elf_sec_str(obj, shdr->sh_name); in bpf_object__collect_prog_relos()
4310 return -EINVAL; in bpf_object__collect_prog_relos()
4314 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_prog_relos()
4320 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4323 sym_idx = ELF64_R_SYM(rel->r_info); in bpf_object__collect_prog_relos()
4328 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4331 if (sym->st_shndx >= obj->efile.sec_cnt) { in bpf_object__collect_prog_relos()
4333 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); in bpf_object__collect_prog_relos()
4334 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4337 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { in bpf_object__collect_prog_relos()
4339 relo_sec_name, (size_t)rel->r_offset, i); in bpf_object__collect_prog_relos()
4340 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4343 insn_idx = rel->r_offset / BPF_INSN_SZ; in bpf_object__collect_prog_relos()
4350 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) in bpf_object__collect_prog_relos()
4351 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); in bpf_object__collect_prog_relos()
4353 sym_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_prog_relos()
4366 relos = libbpf_reallocarray(prog->reloc_desc, in bpf_object__collect_prog_relos()
4367 prog->nr_reloc + 1, sizeof(*relos)); in bpf_object__collect_prog_relos()
4369 return -ENOMEM; in bpf_object__collect_prog_relos()
4370 prog->reloc_desc = relos; in bpf_object__collect_prog_relos()
4373 insn_idx -= prog->sec_insn_off; in bpf_object__collect_prog_relos()
4374 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], in bpf_object__collect_prog_relos()
4379 prog->nr_reloc++; in bpf_object__collect_prog_relos()
4388 if (!obj->btf) in map_fill_btf_type_info()
4389 return -ENOENT; in map_fill_btf_type_info()
4391 /* if it's BTF-defined map, we don't need to search for type IDs. in map_fill_btf_type_info()
4395 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) in map_fill_btf_type_info()
4403 return -ENOENT; in map_fill_btf_type_info()
4405 id = btf__find_by_name(obj->btf, map->real_name); in map_fill_btf_type_info()
4409 map->btf_key_type_id = 0; in map_fill_btf_type_info()
4410 map->btf_value_type_id = id; in map_fill_btf_type_info()
4426 err = -errno; in bpf_get_map_info_from_fdinfo()
4434 info->type = val; in bpf_get_map_info_from_fdinfo()
4436 info->key_size = val; in bpf_get_map_info_from_fdinfo()
4438 info->value_size = val; in bpf_get_map_info_from_fdinfo()
4440 info->max_entries = val; in bpf_get_map_info_from_fdinfo()
4442 info->map_flags = val; in bpf_get_map_info_from_fdinfo()
4452 return map->autocreate; in bpf_map__autocreate()
4457 if (map->obj->loaded) in bpf_map__set_autocreate()
4458 return libbpf_err(-EBUSY); in bpf_map__set_autocreate()
4460 map->autocreate = autocreate; in bpf_map__set_autocreate()
4479 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) in bpf_map__reuse_fd()
4480 new_name = strdup(map->name); in bpf_map__reuse_fd()
4485 return libbpf_err(-errno); in bpf_map__reuse_fd()
4494 err = -errno; in bpf_map__reuse_fd()
4498 err = reuse_fd(map->fd, new_fd); in bpf_map__reuse_fd()
4502 free(map->name); in bpf_map__reuse_fd()
4504 map->name = new_name; in bpf_map__reuse_fd()
4505 map->def.type = info.type; in bpf_map__reuse_fd()
4506 map->def.key_size = info.key_size; in bpf_map__reuse_fd()
4507 map->def.value_size = info.value_size; in bpf_map__reuse_fd()
4508 map->def.max_entries = info.max_entries; in bpf_map__reuse_fd()
4509 map->def.map_flags = info.map_flags; in bpf_map__reuse_fd()
4510 map->btf_key_type_id = info.btf_key_type_id; in bpf_map__reuse_fd()
4511 map->btf_value_type_id = info.btf_value_type_id; in bpf_map__reuse_fd()
4512 map->reused = true; in bpf_map__reuse_fd()
4513 map->map_extra = info.map_extra; in bpf_map__reuse_fd()
4524 return map->def.max_entries; in bpf_map__max_entries()
4529 if (!bpf_map_type__is_map_in_map(map->def.type)) in bpf_map__inner_map()
4532 return map->inner_map; in bpf_map__inner_map()
4537 if (map->obj->loaded) in bpf_map__set_max_entries()
4538 return libbpf_err(-EBUSY); in bpf_map__set_max_entries()
4540 map->def.max_entries = max_entries; in bpf_map__set_max_entries()
4542 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in bpf_map__set_max_entries()
4544 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in bpf_map__set_max_entries()
4559 if (obj->gen_loader) in bpf_object__probe_loading()
4577 return -ret; in bpf_object__probe_loading()
4626 ret = -errno; in probe_kern_global_data()
4629 __func__, cp, -ret); in probe_kern_global_data()
4729 BTF_TYPE_DECL_TAG_ENC(1, 2, -1), in probe_kern_btf_decl_tag()
4771 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS) in probe_kern_exp_attach_type()
4783 BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */ in probe_kern_probe_read_kernel()
4806 ret = -errno; in probe_prog_bind_map()
4809 __func__, cp, -ret); in probe_prog_bind_map()
4867 return -errno; in probe_perf_link()
4872 link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL); in probe_perf_link()
4873 err = -errno; /* close() can clobber errno */ in probe_perf_link()
4879 return link_fd < 0 && err == -EBADF; in probe_perf_link()
4898 return -errno; in probe_uprobe_multi_link()
4900 /* Creating uprobe in '/' binary should fail with -EBADF. */ in probe_uprobe_multi_link()
4905 link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts); in probe_uprobe_multi_link()
4906 err = -errno; /* close() can clobber errno */ in probe_uprobe_multi_link()
4912 return link_fd < 0 && err == -EBADF; in probe_uprobe_multi_link()
5000 "memcg-based memory accounting", probe_memcg_account,
5012 "BPF multi-uprobe link support", probe_uprobe_multi_link,
5021 if (obj && obj->gen_loader) in kernel_supports()
5027 if (READ_ONCE(feat->res) == FEAT_UNKNOWN) { in kernel_supports()
5028 ret = feat->probe(); in kernel_supports()
5030 WRITE_ONCE(feat->res, FEAT_SUPPORTED); in kernel_supports()
5032 WRITE_ONCE(feat->res, FEAT_MISSING); in kernel_supports()
5034 pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret); in kernel_supports()
5035 WRITE_ONCE(feat->res, FEAT_MISSING); in kernel_supports()
5039 return READ_ONCE(feat->res) == FEAT_SUPPORTED; in kernel_supports()
5059 return (map_info.type == map->def.type && in map_is_reuse_compat()
5060 map_info.key_size == map->def.key_size && in map_is_reuse_compat()
5061 map_info.value_size == map->def.value_size && in map_is_reuse_compat()
5062 map_info.max_entries == map->def.max_entries && in map_is_reuse_compat()
5063 map_info.map_flags == map->def.map_flags && in map_is_reuse_compat()
5064 map_info.map_extra == map->map_extra); in map_is_reuse_compat()
5073 pin_fd = bpf_obj_get(map->pin_path); in bpf_object__reuse_map()
5075 err = -errno; in bpf_object__reuse_map()
5076 if (err == -ENOENT) { in bpf_object__reuse_map()
5078 map->pin_path); in bpf_object__reuse_map()
5082 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in bpf_object__reuse_map()
5084 map->pin_path, cp); in bpf_object__reuse_map()
5090 map->pin_path); in bpf_object__reuse_map()
5092 return -EINVAL; in bpf_object__reuse_map()
5100 map->pinned = true; in bpf_object__reuse_map()
5101 pr_debug("reused pinned map at '%s'\n", map->pin_path); in bpf_object__reuse_map()
5109 enum libbpf_map_type map_type = map->libbpf_type; in bpf_object__populate_internal_map()
5113 if (obj->gen_loader) { in bpf_object__populate_internal_map()
5114 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, in bpf_object__populate_internal_map()
5115 map->mmaped, map->def.value_size); in bpf_object__populate_internal_map()
5117 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); in bpf_object__populate_internal_map()
5120 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); in bpf_object__populate_internal_map()
5122 err = -errno; in bpf_object__populate_internal_map()
5125 map->name, cp); in bpf_object__populate_internal_map()
5129 /* Freeze .rodata and .kconfig map as read-only from syscall side. */ in bpf_object__populate_internal_map()
5131 err = bpf_map_freeze(map->fd); in bpf_object__populate_internal_map()
5133 err = -errno; in bpf_object__populate_internal_map()
5135 pr_warn("Error freezing map(%s) as read-only: %s\n", in bpf_object__populate_internal_map()
5136 map->name, cp); in bpf_object__populate_internal_map()
5147 return map->obj->loaded || map->reused; in map_is_created()
5153 struct bpf_map_def *def = &map->def; in bpf_object__create_map()
5158 map_name = map->name; in bpf_object__create_map()
5159 create_attr.map_ifindex = map->map_ifindex; in bpf_object__create_map()
5160 create_attr.map_flags = def->map_flags; in bpf_object__create_map()
5161 create_attr.numa_node = map->numa_node; in bpf_object__create_map()
5162 create_attr.map_extra = map->map_extra; in bpf_object__create_map()
5165 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; in bpf_object__create_map()
5167 if (obj->btf && btf__fd(obj->btf) >= 0) { in bpf_object__create_map()
5168 create_attr.btf_fd = btf__fd(obj->btf); in bpf_object__create_map()
5169 create_attr.btf_key_type_id = map->btf_key_type_id; in bpf_object__create_map()
5170 create_attr.btf_value_type_id = map->btf_value_type_id; in bpf_object__create_map()
5173 if (bpf_map_type__is_map_in_map(def->type)) { in bpf_object__create_map()
5174 if (map->inner_map) { in bpf_object__create_map()
5175 err = bpf_object__create_map(obj, map->inner_map, true); in bpf_object__create_map()
5178 map->name, err); in bpf_object__create_map()
5181 map->inner_map_fd = map->inner_map->fd; in bpf_object__create_map()
5183 if (map->inner_map_fd >= 0) in bpf_object__create_map()
5184 create_attr.inner_map_fd = map->inner_map_fd; in bpf_object__create_map()
5187 switch (def->type) { in bpf_object__create_map()
5204 map->btf_key_type_id = 0; in bpf_object__create_map()
5205 map->btf_value_type_id = 0; in bpf_object__create_map()
5210 if (obj->gen_loader) { in bpf_object__create_map()
5211 bpf_gen__map_create(obj->gen_loader, def->type, map_name, in bpf_object__create_map()
5212 def->key_size, def->value_size, def->max_entries, in bpf_object__create_map()
5213 &create_attr, is_inner ? -1 : map - obj->maps); in bpf_object__create_map()
5218 * will be reset to -1 eventually. in bpf_object__create_map()
5220 map_fd = map->fd; in bpf_object__create_map()
5222 map_fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5223 def->key_size, def->value_size, in bpf_object__create_map()
5224 def->max_entries, &create_attr); in bpf_object__create_map()
5229 err = -errno; in bpf_object__create_map()
5232 map->name, cp, err); in bpf_object__create_map()
5236 map->btf_key_type_id = 0; in bpf_object__create_map()
5237 map->btf_value_type_id = 0; in bpf_object__create_map()
5238 map_fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5239 def->key_size, def->value_size, in bpf_object__create_map()
5240 def->max_entries, &create_attr); in bpf_object__create_map()
5243 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { in bpf_object__create_map()
5244 if (obj->gen_loader) in bpf_object__create_map()
5245 map->inner_map->fd = -1; in bpf_object__create_map()
5246 bpf_map__destroy(map->inner_map); in bpf_object__create_map()
5247 zfree(&map->inner_map); in bpf_object__create_map()
5253 /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */ in bpf_object__create_map()
5254 if (map->fd == map_fd) in bpf_object__create_map()
5260 * map->fd stays valid but now point to what map_fd points to. in bpf_object__create_map()
5262 return reuse_fd(map->fd, map_fd); in bpf_object__create_map()
5271 for (i = 0; i < map->init_slots_sz; i++) { in init_map_in_map_slots()
5272 if (!map->init_slots[i]) in init_map_in_map_slots()
5275 targ_map = map->init_slots[i]; in init_map_in_map_slots()
5276 fd = targ_map->fd; in init_map_in_map_slots()
5278 if (obj->gen_loader) { in init_map_in_map_slots()
5279 bpf_gen__populate_outer_map(obj->gen_loader, in init_map_in_map_slots()
5280 map - obj->maps, i, in init_map_in_map_slots()
5281 targ_map - obj->maps); in init_map_in_map_slots()
5283 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_map_in_map_slots()
5286 err = -errno; in init_map_in_map_slots()
5288 map->name, i, targ_map->name, fd, err); in init_map_in_map_slots()
5292 map->name, i, targ_map->name, fd); in init_map_in_map_slots()
5295 zfree(&map->init_slots); in init_map_in_map_slots()
5296 map->init_slots_sz = 0; in init_map_in_map_slots()
5307 if (obj->gen_loader) in init_prog_array_slots()
5308 return -ENOTSUP; in init_prog_array_slots()
5310 for (i = 0; i < map->init_slots_sz; i++) { in init_prog_array_slots()
5311 if (!map->init_slots[i]) in init_prog_array_slots()
5314 targ_prog = map->init_slots[i]; in init_prog_array_slots()
5317 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_prog_array_slots()
5319 err = -errno; in init_prog_array_slots()
5321 map->name, i, targ_prog->name, fd, err); in init_prog_array_slots()
5325 map->name, i, targ_prog->name, fd); in init_prog_array_slots()
5328 zfree(&map->init_slots); in init_prog_array_slots()
5329 map->init_slots_sz = 0; in init_prog_array_slots()
5339 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_init_prog_arrays()
5340 map = &obj->maps[i]; in bpf_object_init_prog_arrays()
5342 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) in bpf_object_init_prog_arrays()
5354 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { in map_set_def_max_entries()
5360 map->name, nr_cpus); in map_set_def_max_entries()
5363 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); in map_set_def_max_entries()
5364 map->def.max_entries = nr_cpus; in map_set_def_max_entries()
5379 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__create_maps()
5380 map = &obj->maps[i]; in bpf_object__create_maps()
5384 * loading, if we detect that at least one of the to-be-loaded in bpf_object__create_maps()
5389 * but also it allows to have CO-RE applications that use in bpf_object__create_maps()
5391 * If those global variable-using programs are not loaded at in bpf_object__create_maps()
5397 map->autocreate = false; in bpf_object__create_maps()
5399 if (!map->autocreate) { in bpf_object__create_maps()
5400 pr_debug("map '%s': skipped auto-creating...\n", map->name); in bpf_object__create_maps()
5410 if (map->pin_path) { in bpf_object__create_maps()
5414 map->name); in bpf_object__create_maps()
5417 if (retried && map->fd < 0) { in bpf_object__create_maps()
5419 map->name); in bpf_object__create_maps()
5420 err = -ENOENT; in bpf_object__create_maps()
5425 if (map->reused) { in bpf_object__create_maps()
5427 map->name, map->fd); in bpf_object__create_maps()
5434 map->name, map->fd); in bpf_object__create_maps()
5442 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { in bpf_object__create_maps()
5449 if (map->pin_path && !map->pinned) { in bpf_object__create_maps()
5452 if (!retried && err == -EEXIST) { in bpf_object__create_maps()
5456 pr_warn("map '%s': failed to auto-pin at '%s': %d\n", in bpf_object__create_maps()
5457 map->name, map->pin_path, err); in bpf_object__create_maps()
5467 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err); in bpf_object__create_maps()
5470 zclose(obj->maps[j].fd); in bpf_object__create_maps()
5484 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5491 for (i = n - 5; i >= 0; i--) { in bpf_core_essential_name_len()
5503 free(cands->cands); in bpf_core_free_cands()
5520 local_t = btf__type_by_id(local_cand->btf, local_cand->id); in bpf_core_add_cands()
5521 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); in bpf_core_add_cands()
5529 targ_name = btf__name_by_offset(targ_btf, t->name_off); in bpf_core_add_cands()
5540 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n", in bpf_core_add_cands()
5541 local_cand->id, btf_kind_str(local_t), in bpf_core_add_cands()
5544 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, in bpf_core_add_cands()
5545 sizeof(*cands->cands)); in bpf_core_add_cands()
5547 return -ENOMEM; in bpf_core_add_cands()
5549 cand = &new_cands[cands->len]; in bpf_core_add_cands()
5550 cand->btf = targ_btf; in bpf_core_add_cands()
5551 cand->id = i; in bpf_core_add_cands()
5553 cands->cands = new_cands; in bpf_core_add_cands()
5554 cands->len++; in bpf_core_add_cands()
5568 if (obj->btf_modules_loaded) in load_module_btfs()
5571 if (obj->gen_loader) in load_module_btfs()
5575 obj->btf_modules_loaded = true; in load_module_btfs()
5590 err = -errno; in load_module_btfs()
5599 err = -errno; in load_module_btfs()
5611 err = -errno; in load_module_btfs()
5616 /* ignore non-module BTFs */ in load_module_btfs()
5622 btf = btf_get_from_fd(fd, obj->btf_vmlinux); in load_module_btfs()
5630 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, in load_module_btfs()
5631 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); in load_module_btfs()
5635 mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; in load_module_btfs()
5637 mod_btf->btf = btf; in load_module_btfs()
5638 mod_btf->id = id; in load_module_btfs()
5639 mod_btf->fd = fd; in load_module_btfs()
5640 mod_btf->name = strdup(name); in load_module_btfs()
5641 if (!mod_btf->name) { in load_module_btfs()
5642 err = -ENOMEM; in load_module_btfs()
5670 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5672 local_name = btf__name_by_offset(local_btf, local_t->name_off); in bpf_core_find_cands()
5674 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5679 return ERR_PTR(-ENOMEM); in bpf_core_find_cands()
5682 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; in bpf_core_find_cands()
5688 if (cands->len) in bpf_core_find_cands()
5692 if (obj->btf_vmlinux_override) in bpf_core_find_cands()
5700 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_core_find_cands()
5702 obj->btf_modules[i].btf, in bpf_core_find_cands()
5703 obj->btf_modules[i].name, in bpf_core_find_cands()
5704 btf__type_cnt(obj->btf_vmlinux), in bpf_core_find_cands()
5717 * type-based CO-RE relocations and follow slightly different rules than
5718 * field-based relocations. This function assumes that root types were already
5719 * checked for name match. Beyond that initial root-level name check, names
5721 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5724 * - for ENUMs, the size is ignored;
5725 * - for INT, size and signedness are ignored;
5726 * - for ARRAY, dimensionality is ignored, element types are checked for
5728 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5729 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5730 * - FUNC_PROTOs are compatible if they have compatible signature: same
5733 * more experience with using BPF CO-RE relocations.
5762 relos = libbpf_reallocarray(prog->reloc_desc, in record_relo_core()
5763 prog->nr_reloc + 1, sizeof(*relos)); in record_relo_core()
5765 return -ENOMEM; in record_relo_core()
5766 relo = &relos[prog->nr_reloc]; in record_relo_core()
5767 relo->type = RELO_CORE; in record_relo_core()
5768 relo->insn_idx = insn_idx; in record_relo_core()
5769 relo->core_relo = core_relo; in record_relo_core()
5770 prog->reloc_desc = relos; in record_relo_core()
5771 prog->nr_reloc++; in record_relo_core()
5780 for (i = 0; i < prog->nr_reloc; i++) { in find_relo_core()
5781 relo = &prog->reloc_desc[i]; in find_relo_core()
5782 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) in find_relo_core()
5785 return relo->core_relo; in find_relo_core()
5800 const char *prog_name = prog->name; in bpf_core_resolve_relo()
5803 __u32 local_id = relo->type_id; in bpf_core_resolve_relo()
5808 return -EINVAL; in bpf_core_resolve_relo()
5810 local_name = btf__name_by_offset(local_btf, local_type->name_off); in bpf_core_resolve_relo()
5812 return -EINVAL; in bpf_core_resolve_relo()
5814 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && in bpf_core_resolve_relo()
5816 cands = bpf_core_find_cands(prog->obj, local_btf, local_id); in bpf_core_resolve_relo()
5848 if (obj->btf_ext->core_relo_info.len == 0) in bpf_object__relocate_core()
5852 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); in bpf_object__relocate_core()
5853 err = libbpf_get_error(obj->btf_vmlinux_override); in bpf_object__relocate_core()
5866 seg = &obj->btf_ext->core_relo_info; in bpf_object__relocate_core()
5869 sec_idx = seg->sec_idxs[sec_num]; in bpf_object__relocate_core()
5872 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__relocate_core()
5874 err = -EINVAL; in bpf_object__relocate_core()
5878 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info); in bpf_object__relocate_core()
5881 if (rec->insn_off % BPF_INSN_SZ) in bpf_object__relocate_core()
5882 return -EINVAL; in bpf_object__relocate_core()
5883 insn_idx = rec->insn_off / BPF_INSN_SZ; in bpf_object__relocate_core()
5888 * appends all the .BTF.ext info that used to belong to that in bpf_object__relocate_core()
5890 * This is similar to what x86-64 linker does for relocations. in bpf_object__relocate_core()
5894 …pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subpro… in bpf_object__relocate_core()
5898 /* no need to apply CO-RE relocation if the program is in bpf_object__relocate_core()
5901 if (!prog->autoload) in bpf_object__relocate_core()
5905 * program's frame of reference; (sub-)program code is not yet in bpf_object__relocate_core()
5906 * relocated, so it's enough to just subtract in-section offset in bpf_object__relocate_core()
5908 insn_idx = insn_idx - prog->sec_insn_off; in bpf_object__relocate_core()
5909 if (insn_idx >= prog->insns_cnt) in bpf_object__relocate_core()
5910 return -EINVAL; in bpf_object__relocate_core()
5911 insn = &prog->insns[insn_idx]; in bpf_object__relocate_core()
5916 prog->name, i, err); in bpf_object__relocate_core()
5920 if (prog->obj->gen_loader) in bpf_object__relocate_core()
5923 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); in bpf_object__relocate_core()
5926 prog->name, i, err); in bpf_object__relocate_core()
5930 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); in bpf_object__relocate_core()
5933 prog->name, i, insn_idx, err); in bpf_object__relocate_core()
5940 /* obj->btf_vmlinux and module BTFs are freed after object load */ in bpf_object__relocate_core()
5941 btf__free(obj->btf_vmlinux_override); in bpf_object__relocate_core()
5942 obj->btf_vmlinux_override = NULL; in bpf_object__relocate_core()
5946 bpf_core_free_cands(entry->pvalue); in bpf_object__relocate_core()
5964 prog->name, relo_idx, insn_idx, map_idx, map->name); in poison_map_ldimm64()
5968 insn->code = BPF_JMP | BPF_CALL; in poison_map_ldimm64()
5969 insn->dst_reg = 0; in poison_map_ldimm64()
5970 insn->src_reg = 0; in poison_map_ldimm64()
5971 insn->off = 0; in poison_map_ldimm64()
5975 * where lower 123 is map index into obj->maps[] array in poison_map_ldimm64()
5977 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; in poison_map_ldimm64()
5989 int ext_idx, const struct extern_desc *ext) in poison_kfunc_call() argument
5992 prog->name, relo_idx, insn_idx, ext->name); in poison_kfunc_call()
5995 insn->code = BPF_JMP | BPF_CALL; in poison_kfunc_call()
5996 insn->dst_reg = 0; in poison_kfunc_call()
5997 insn->src_reg = 0; in poison_kfunc_call()
5998 insn->off = 0; in poison_kfunc_call()
6002 * where lower 123 is extern index into obj->externs[] array in poison_kfunc_call()
6004 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; in poison_kfunc_call()
6008 * - map references;
6009 * - global variable references;
6010 * - extern references.
6017 for (i = 0; i < prog->nr_reloc; i++) { in bpf_object__relocate_data()
6018 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_object__relocate_data()
6019 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate_data()
6021 struct extern_desc *ext; in bpf_object__relocate_data() local
6023 switch (relo->type) { in bpf_object__relocate_data()
6025 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
6026 if (obj->gen_loader) { in bpf_object__relocate_data()
6028 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
6029 } else if (map->autocreate) { in bpf_object__relocate_data()
6031 insn[0].imm = map->fd; in bpf_object__relocate_data()
6033 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6034 relo->map_idx, map); in bpf_object__relocate_data()
6038 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
6039 insn[1].imm = insn[0].imm + relo->sym_off; in bpf_object__relocate_data()
6040 if (obj->gen_loader) { in bpf_object__relocate_data()
6042 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
6043 } else if (map->autocreate) { in bpf_object__relocate_data()
6045 insn[0].imm = map->fd; in bpf_object__relocate_data()
6047 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6048 relo->map_idx, map); in bpf_object__relocate_data()
6052 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
6053 if (ext->type == EXT_KCFG) { in bpf_object__relocate_data()
6054 if (obj->gen_loader) { in bpf_object__relocate_data()
6056 insn[0].imm = obj->kconfig_map_idx; in bpf_object__relocate_data()
6059 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; in bpf_object__relocate_data()
6061 insn[1].imm = ext->kcfg.data_off; in bpf_object__relocate_data()
6063 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ in bpf_object__relocate_data()
6065 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6066 insn[1].imm = ext->ksym.kernel_btf_obj_fd; in bpf_object__relocate_data()
6068 insn[0].imm = (__u32)ext->ksym.addr; in bpf_object__relocate_data()
6069 insn[1].imm = ext->ksym.addr >> 32; in bpf_object__relocate_data()
6074 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
6076 if (ext->is_set) { in bpf_object__relocate_data()
6077 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6078 insn[0].off = ext->ksym.btf_fd_idx; in bpf_object__relocate_data()
6080 poison_kfunc_call(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6081 relo->ext_idx, ext); in bpf_object__relocate_data()
6087 prog->name, i); in bpf_object__relocate_data()
6088 return -EINVAL; in bpf_object__relocate_data()
6100 prog->name, i, relo->type); in bpf_object__relocate_data()
6101 return -EINVAL; in bpf_object__relocate_data()
6122 sec_idx = ext_info->sec_idxs[sec_num]; in adjust_prog_btf_ext_info()
6124 if (prog->sec_idx != sec_idx) in adjust_prog_btf_ext_info()
6130 if (insn_off < prog->sec_insn_off) in adjust_prog_btf_ext_info()
6132 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) in adjust_prog_btf_ext_info()
6137 copy_end = rec + ext_info->rec_size; in adjust_prog_btf_ext_info()
6141 return -ENOENT; in adjust_prog_btf_ext_info()
6143 /* append func/line info of a given (sub-)program to the main in adjust_prog_btf_ext_info()
6146 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; in adjust_prog_btf_ext_info()
6147 new_sz = old_sz + (copy_end - copy_start); in adjust_prog_btf_ext_info()
6150 return -ENOMEM; in adjust_prog_btf_ext_info()
6152 *prog_rec_cnt = new_sz / ext_info->rec_size; in adjust_prog_btf_ext_info()
6153 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); in adjust_prog_btf_ext_info()
6155 /* Kernel instruction offsets are in units of 8-byte in adjust_prog_btf_ext_info()
6156 * instructions, while .BTF.ext instruction offsets generated in adjust_prog_btf_ext_info()
6161 off_adj = prog->sub_insn_off - prog->sec_insn_off; in adjust_prog_btf_ext_info()
6164 for (; rec < rec_end; rec += ext_info->rec_size) { in adjust_prog_btf_ext_info()
6169 *prog_rec_sz = ext_info->rec_size; in adjust_prog_btf_ext_info()
6173 return -ENOENT; in adjust_prog_btf_ext_info()
6183 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't in reloc_prog_func_and_line_info()
6186 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) in reloc_prog_func_and_line_info()
6192 if (main_prog != prog && !main_prog->func_info) in reloc_prog_func_and_line_info()
6195 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, in reloc_prog_func_and_line_info()
6196 &main_prog->func_info, in reloc_prog_func_and_line_info()
6197 &main_prog->func_info_cnt, in reloc_prog_func_and_line_info()
6198 &main_prog->func_info_rec_size); in reloc_prog_func_and_line_info()
6200 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6201 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n", in reloc_prog_func_and_line_info()
6202 prog->name, err); in reloc_prog_func_and_line_info()
6205 if (main_prog->func_info) { in reloc_prog_func_and_line_info()
6210 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); in reloc_prog_func_and_line_info()
6214 …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()
6215 prog->name); in reloc_prog_func_and_line_info()
6220 if (main_prog != prog && !main_prog->line_info) in reloc_prog_func_and_line_info()
6223 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, in reloc_prog_func_and_line_info()
6224 &main_prog->line_info, in reloc_prog_func_and_line_info()
6225 &main_prog->line_info_cnt, in reloc_prog_func_and_line_info()
6226 &main_prog->line_info_rec_size); in reloc_prog_func_and_line_info()
6228 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6229 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n", in reloc_prog_func_and_line_info()
6230 prog->name, err); in reloc_prog_func_and_line_info()
6233 if (main_prog->line_info) { in reloc_prog_func_and_line_info()
6238 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); in reloc_prog_func_and_line_info()
6242 …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()
6243 prog->name); in reloc_prog_func_and_line_info()
6253 if (insn_idx == relo->insn_idx) in cmp_relo_by_insn_idx()
6255 return insn_idx < relo->insn_idx ? -1 : 1; in cmp_relo_by_insn_idx()
6260 if (!prog->nr_reloc) in find_prog_insn_relo()
6262 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, in find_prog_insn_relo()
6263 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); in find_prog_insn_relo()
6268 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; in append_subprog_relos()
6274 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); in append_subprog_relos()
6280 return -ENOMEM; in append_subprog_relos()
6281 if (subprog->nr_reloc) in append_subprog_relos()
6282 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, in append_subprog_relos()
6283 sizeof(*relos) * subprog->nr_reloc); in append_subprog_relos()
6285 for (i = main_prog->nr_reloc; i < new_cnt; i++) in append_subprog_relos()
6286 relos[i].insn_idx += subprog->sub_insn_off; in append_subprog_relos()
6290 main_prog->reloc_desc = relos; in append_subprog_relos()
6291 main_prog->nr_reloc = new_cnt; in append_subprog_relos()
6303 subprog->sub_insn_off = main_prog->insns_cnt; in bpf_object__append_subprog_code()
6305 new_cnt = main_prog->insns_cnt + subprog->insns_cnt; in bpf_object__append_subprog_code()
6306 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); in bpf_object__append_subprog_code()
6308 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); in bpf_object__append_subprog_code()
6309 return -ENOMEM; in bpf_object__append_subprog_code()
6311 main_prog->insns = insns; in bpf_object__append_subprog_code()
6312 main_prog->insns_cnt = new_cnt; in bpf_object__append_subprog_code()
6314 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, in bpf_object__append_subprog_code()
6315 subprog->insns_cnt * sizeof(*insns)); in bpf_object__append_subprog_code()
6317 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", in bpf_object__append_subprog_code()
6318 main_prog->name, subprog->insns_cnt, subprog->name); in bpf_object__append_subprog_code()
6341 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { in bpf_object__reloc_code()
6342 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6347 if (relo && relo->type == RELO_EXTERN_CALL) in bpf_object__reloc_code()
6352 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { in bpf_object__reloc_code()
6354 prog->name, insn_idx, relo->type); in bpf_object__reloc_code()
6355 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6358 /* sub-program instruction index is a combination of in bpf_object__reloc_code()
6361 * call always has imm = -1, but for static functions in bpf_object__reloc_code()
6362 * relocation is against STT_SECTION and insn->imm in bpf_object__reloc_code()
6365 * for subprog addr relocation, the relo->sym_off + insn->imm is in bpf_object__reloc_code()
6368 if (relo->type == RELO_CALL) in bpf_object__reloc_code()
6369 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; in bpf_object__reloc_code()
6371 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; in bpf_object__reloc_code()
6378 prog->name, insn_idx); in bpf_object__reloc_code()
6379 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6384 * offset necessary, insns->imm is relative to in bpf_object__reloc_code()
6387 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; in bpf_object__reloc_code()
6390 /* we enforce that sub-programs should be in .text section */ in bpf_object__reloc_code()
6391 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); in bpf_object__reloc_code()
6393 pr_warn("prog '%s': no .text section found yet sub-program call exists\n", in bpf_object__reloc_code()
6394 prog->name); in bpf_object__reloc_code()
6395 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6401 * - append it at the end of main program's instructions blog; in bpf_object__reloc_code()
6402 * - process is recursively, while current program is put on hold; in bpf_object__reloc_code()
6403 * - if that subprogram calls some other not yet processes in bpf_object__reloc_code()
6408 if (subprog->sub_insn_off == 0) { in bpf_object__reloc_code()
6417 /* main_prog->insns memory could have been re-allocated, so in bpf_object__reloc_code()
6420 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6427 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; in bpf_object__reloc_code()
6430 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); in bpf_object__reloc_code()
6437 * Relocate sub-program calls.
6439 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6440 * main prog) is processed separately. For each subprog (non-entry functions,
6449 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6465 * subprog->sub_insn_off as zero at all times and won't be appended to current
6474 * +--------+ +-------+
6476 * +--+---+ +--+-+-+ +---+--+
6478 * +--+---+ +------+ +---+--+
6481 * +---+-------+ +------+----+
6483 * +-----------+ +-----------+
6488 * +-----------+------+
6490 * +-----------+------+
6495 * +-----------+------+------+
6497 * +-----------+------+------+
6506 * +-----------+------+
6508 * +-----------+------+
6511 * +-----------+------+------+
6513 * +-----------+------+------+
6526 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate_calls()
6527 subprog = &obj->programs[i]; in bpf_object__relocate_calls()
6531 subprog->sub_insn_off = 0; in bpf_object__relocate_calls()
6548 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__free_relocs()
6549 prog = &obj->programs[i]; in bpf_object__free_relocs()
6550 zfree(&prog->reloc_desc); in bpf_object__free_relocs()
6551 prog->nr_reloc = 0; in bpf_object__free_relocs()
6560 if (a->insn_idx != b->insn_idx) in cmp_relocs()
6561 return a->insn_idx < b->insn_idx ? -1 : 1; in cmp_relocs()
6564 if (a->type != b->type) in cmp_relocs()
6565 return a->type < b->type ? -1 : 1; in cmp_relocs()
6574 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sort_relos()
6575 struct bpf_program *p = &obj->programs[i]; in bpf_object__sort_relos()
6577 if (!p->nr_reloc) in bpf_object__sort_relos()
6580 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); in bpf_object__sort_relos()
6590 if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG)) in bpf_prog_assign_exc_cb()
6593 n = btf__type_cnt(obj->btf); in bpf_prog_assign_exc_cb()
6598 t = btf_type_by_id(obj->btf, i); in bpf_prog_assign_exc_cb()
6599 if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1) in bpf_prog_assign_exc_cb()
6602 name = btf__str_by_offset(obj->btf, t->name_off); in bpf_prog_assign_exc_cb()
6606 t = btf_type_by_id(obj->btf, t->type); in bpf_prog_assign_exc_cb()
6609 prog->name); in bpf_prog_assign_exc_cb()
6610 return -EINVAL; in bpf_prog_assign_exc_cb()
6612 if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0) in bpf_prog_assign_exc_cb()
6618 if (prog->exception_cb_idx >= 0) { in bpf_prog_assign_exc_cb()
6619 prog->exception_cb_idx = -1; in bpf_prog_assign_exc_cb()
6626 prog->name); in bpf_prog_assign_exc_cb()
6627 return -EINVAL; in bpf_prog_assign_exc_cb()
6630 for (j = 0; j < obj->nr_programs; j++) { in bpf_prog_assign_exc_cb()
6631 struct bpf_program *subprog = &obj->programs[j]; in bpf_prog_assign_exc_cb()
6635 if (strcmp(name, subprog->name) != 0) in bpf_prog_assign_exc_cb()
6637 /* Enforce non-hidden, as from verifier point of in bpf_prog_assign_exc_cb()
6641 if (!subprog->sym_global || subprog->mark_btf_static) { in bpf_prog_assign_exc_cb()
6642 pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n", in bpf_prog_assign_exc_cb()
6643 prog->name, subprog->name); in bpf_prog_assign_exc_cb()
6644 return -EINVAL; in bpf_prog_assign_exc_cb()
6647 if (prog->exception_cb_idx >= 0) { in bpf_prog_assign_exc_cb()
6649 prog->name, subprog->name); in bpf_prog_assign_exc_cb()
6650 return -EINVAL; in bpf_prog_assign_exc_cb()
6652 prog->exception_cb_idx = j; in bpf_prog_assign_exc_cb()
6656 if (prog->exception_cb_idx >= 0) in bpf_prog_assign_exc_cb()
6659 pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name); in bpf_prog_assign_exc_cb()
6660 return -ENOENT; in bpf_prog_assign_exc_cb()
6715 t = btf__type_by_id(btf, t->type); in need_func_arg_type_fixup()
6717 (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) { in need_func_arg_type_fixup()
6718 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; in need_func_arg_type_fixup()
6724 t = skip_mods_and_typedefs(btf, t->type, NULL); in need_func_arg_type_fixup()
6731 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; in need_func_arg_type_fixup()
6736 switch (prog->type) { in need_func_arg_type_fixup()
6746 if (btf_is_int(t) && t->size == 8) in need_func_arg_type_fixup()
6755 prog->name, subprog_name, arg_idx, ctx_name); in need_func_arg_type_fixup()
6766 /* caller already validated FUNC -> FUNC_PROTO validity */ in clone_func_btf_info()
6768 fn_proto_t = btf_type_by_id(btf, fn_t->type); in clone_func_btf_info()
6779 fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */ in clone_func_btf_info()
6781 orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */ in clone_func_btf_info()
6782 ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */ in clone_func_btf_info()
6788 return -EINVAL; in clone_func_btf_info()
6796 name_off = p->name_off; in clone_func_btf_info()
6798 err = btf__add_func_param(btf, "", p->type); in clone_func_btf_info()
6804 p->name_off = name_off; /* use remembered str offset */ in clone_func_btf_info()
6807 /* clone FUNC now, btf__add_func() enforces non-empty name, so use in clone_func_btf_info()
6811 fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id); in clone_func_btf_info()
6813 return -EINVAL; in clone_func_btf_info()
6816 fn_t->name_off = fn_name_off; /* reuse original string */ in clone_func_btf_info()
6825 * probe_kern_arg_ctx_tag() into kernel_supports() feature-detection in probe_kern_arg_ctx_tag()
6827 * This will be cleaned up a bit later when bpf/bpf-next trees settle. in probe_kern_arg_ctx_tag()
6829 static int cached_result = -1; in probe_kern_arg_ctx_tag()
6834 /* [2] PTR -> VOID */ in probe_kern_arg_ctx_tag()
6839 /* [4] FUNC 'a' -> FUNC_PROTO (main prog) */ in probe_kern_arg_ctx_tag()
6844 /* [6] FUNC 'b' -> FUNC_PROTO (subprog) */ in probe_kern_arg_ctx_tag()
6846 /* [7] DECL_TAG 'arg:ctx' -> func 'b' arg 'b' */ in probe_kern_arg_ctx_tag()
6858 { 0, 4 }, /* main prog -> FUNC 'a' */ in probe_kern_arg_ctx_tag()
6859 { 2, 6 }, /* subprog -> FUNC 'b' */ in probe_kern_arg_ctx_tag()
6896 struct btf *btf = obj->btf; in bpf_program_fixup_func_info()
6903 /* no .BTF.ext, no problem */ in bpf_program_fixup_func_info()
6904 if (!obj->btf_ext || !prog->func_info) in bpf_program_fixup_func_info()
6915 if (global_ctx_map[i].prog_type != prog->type) in bpf_program_fixup_func_info()
6924 orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids)); in bpf_program_fixup_func_info()
6926 return -ENOMEM; in bpf_program_fixup_func_info()
6927 for (i = 0; i < prog->func_info_cnt; i++) { in bpf_program_fixup_func_info()
6928 func_rec = prog->func_info + prog->func_info_rec_size * i; in bpf_program_fixup_func_info()
6929 orig_ids[i] = func_rec->type_id; in bpf_program_fixup_func_info()
6934 * clone and adjust FUNC -> FUNC_PROTO combo in bpf_program_fixup_func_info()
6941 if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0) in bpf_program_fixup_func_info()
6945 orig_fn_id = t->type; in bpf_program_fixup_func_info()
6950 /* sanity check FUNC -> FUNC_PROTO chain, just in case */ in bpf_program_fixup_func_info()
6951 fn_proto_t = btf_type_by_id(btf, fn_t->type); in bpf_program_fixup_func_info()
6957 for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) { in bpf_program_fixup_func_info()
6958 if (orig_ids[rec_idx] == t->type) { in bpf_program_fixup_func_info()
6959 func_rec = prog->func_info + prog->func_info_rec_size * rec_idx; in bpf_program_fixup_func_info()
6969 arg_idx = btf_decl_tag(t)->component_idx; in bpf_program_fixup_func_info()
6975 fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>"; in bpf_program_fixup_func_info()
6976 if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name)) in bpf_program_fixup_func_info()
6980 if (func_rec->type_id == orig_fn_id) { in bpf_program_fixup_func_info()
6990 func_rec->type_id = fn_id; in bpf_program_fixup_func_info()
6993 /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument; in bpf_program_fixup_func_info()
6995 * funcs share the same program type, so need only PTR -> in bpf_program_fixup_func_info()
7002 err = -EINVAL; in bpf_program_fixup_func_info()
7008 tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx); in bpf_program_fixup_func_info()
7010 err = -EINVAL; in bpf_program_fixup_func_info()
7015 fn_t = btf_type_by_id(btf, func_rec->type_id); in bpf_program_fixup_func_info()
7016 fn_proto_t = btf_type_by_id(btf, fn_t->type); in bpf_program_fixup_func_info()
7020 p->type = ptr_id; in bpf_program_fixup_func_info()
7036 if (obj->btf_ext) { in bpf_object__relocate()
7039 pr_warn("failed to perform CO-RE relocations: %d\n", in bpf_object__relocate()
7046 /* Before relocating calls pre-process relocations and mark in bpf_object__relocate()
7053 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7054 prog = &obj->programs[i]; in bpf_object__relocate()
7055 for (j = 0; j < prog->nr_reloc; j++) { in bpf_object__relocate()
7056 struct reloc_desc *relo = &prog->reloc_desc[j]; in bpf_object__relocate()
7057 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate()
7060 if (relo->type == RELO_SUBPROG_ADDR) in bpf_object__relocate()
7072 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7073 prog = &obj->programs[i]; in bpf_object__relocate()
7074 /* sub-program's sub-calls are relocated within the context of in bpf_object__relocate()
7079 if (!prog->autoload) in bpf_object__relocate()
7085 prog->name, err); in bpf_object__relocate()
7093 if (prog->exception_cb_idx >= 0) { in bpf_object__relocate()
7094 struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx]; in bpf_object__relocate()
7101 if (subprog->sub_insn_off == 0) { in bpf_object__relocate()
7111 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
7112 prog = &obj->programs[i]; in bpf_object__relocate()
7115 if (!prog->autoload) in bpf_object__relocate()
7122 prog->name, err); in bpf_object__relocate()
7126 /* Fix up .BTF.ext information, if necessary */ in bpf_object__relocate()
7129 pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %d\n", in bpf_object__relocate()
7130 prog->name, err); in bpf_object__relocate()
7158 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) in bpf_object__collect_map_relos()
7159 return -EINVAL; in bpf_object__collect_map_relos()
7160 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); in bpf_object__collect_map_relos()
7162 return -EINVAL; in bpf_object__collect_map_relos()
7164 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_map_relos()
7169 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
7172 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
7175 i, (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
7176 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
7178 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_map_relos()
7180 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n", in bpf_object__collect_map_relos()
7181 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, in bpf_object__collect_map_relos()
7182 (size_t)rel->r_offset, sym->st_name, name); in bpf_object__collect_map_relos()
7184 for (j = 0; j < obj->nr_maps; j++) { in bpf_object__collect_map_relos()
7185 map = &obj->maps[j]; in bpf_object__collect_map_relos()
7186 if (map->sec_idx != obj->efile.btf_maps_shndx) in bpf_object__collect_map_relos()
7189 vi = btf_var_secinfos(sec) + map->btf_var_idx; in bpf_object__collect_map_relos()
7190 if (vi->offset <= rel->r_offset && in bpf_object__collect_map_relos()
7191 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) in bpf_object__collect_map_relos()
7194 if (j == obj->nr_maps) { in bpf_object__collect_map_relos()
7195 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", in bpf_object__collect_map_relos()
7196 i, name, (size_t)rel->r_offset); in bpf_object__collect_map_relos()
7197 return -EINVAL; in bpf_object__collect_map_relos()
7200 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); in bpf_object__collect_map_relos()
7201 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; in bpf_object__collect_map_relos()
7204 if (sym->st_shndx != obj->efile.btf_maps_shndx) { in bpf_object__collect_map_relos()
7205 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", in bpf_object__collect_map_relos()
7207 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
7209 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && in bpf_object__collect_map_relos()
7210 map->def.key_size != sizeof(int)) { in bpf_object__collect_map_relos()
7211 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", in bpf_object__collect_map_relos()
7212 i, map->name, sizeof(int)); in bpf_object__collect_map_relos()
7213 return -EINVAL; in bpf_object__collect_map_relos()
7219 return -ESRCH; in bpf_object__collect_map_relos()
7226 return -ESRCH; in bpf_object__collect_map_relos()
7228 if (targ_prog->sec_idx != sym->st_shndx || in bpf_object__collect_map_relos()
7229 targ_prog->sec_insn_off * 8 != sym->st_value || in bpf_object__collect_map_relos()
7231 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", in bpf_object__collect_map_relos()
7233 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
7236 return -EINVAL; in bpf_object__collect_map_relos()
7239 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__collect_map_relos()
7240 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__collect_map_relos()
7242 return -EINVAL; in bpf_object__collect_map_relos()
7243 member = btf_members(def) + btf_vlen(def) - 1; in bpf_object__collect_map_relos()
7244 mname = btf__name_by_offset(obj->btf, member->name_off); in bpf_object__collect_map_relos()
7246 return -EINVAL; in bpf_object__collect_map_relos()
7248 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; in bpf_object__collect_map_relos()
7249 if (rel->r_offset - vi->offset < moff) in bpf_object__collect_map_relos()
7250 return -EINVAL; in bpf_object__collect_map_relos()
7252 moff = rel->r_offset - vi->offset - moff; in bpf_object__collect_map_relos()
7257 return -EINVAL; in bpf_object__collect_map_relos()
7259 if (moff >= map->init_slots_sz) { in bpf_object__collect_map_relos()
7261 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); in bpf_object__collect_map_relos()
7263 return -ENOMEM; in bpf_object__collect_map_relos()
7264 map->init_slots = tmp; in bpf_object__collect_map_relos()
7265 memset(map->init_slots + map->init_slots_sz, 0, in bpf_object__collect_map_relos()
7266 (new_sz - map->init_slots_sz) * host_ptr_sz); in bpf_object__collect_map_relos()
7267 map->init_slots_sz = new_sz; in bpf_object__collect_map_relos()
7269 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; in bpf_object__collect_map_relos()
7272 i, map->name, moff, type, name); in bpf_object__collect_map_relos()
7282 for (i = 0; i < obj->efile.sec_cnt; i++) { in bpf_object__collect_relos()
7283 struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; in bpf_object__collect_relos()
7288 if (sec_desc->sec_type != SEC_RELO) in bpf_object__collect_relos()
7291 shdr = sec_desc->shdr; in bpf_object__collect_relos()
7292 data = sec_desc->data; in bpf_object__collect_relos()
7293 idx = shdr->sh_info; in bpf_object__collect_relos()
7295 if (shdr->sh_type != SHT_REL) { in bpf_object__collect_relos()
7297 return -LIBBPF_ERRNO__INTERNAL; in bpf_object__collect_relos()
7300 if (idx == obj->efile.st_ops_shndx || idx == obj->efile.st_ops_link_shndx) in bpf_object__collect_relos()
7302 else if (idx == obj->efile.btf_maps_shndx) in bpf_object__collect_relos()
7316 if (BPF_CLASS(insn->code) == BPF_JMP && in insn_is_helper_call()
7317 BPF_OP(insn->code) == BPF_CALL && in insn_is_helper_call()
7318 BPF_SRC(insn->code) == BPF_K && in insn_is_helper_call()
7319 insn->src_reg == 0 && in insn_is_helper_call()
7320 insn->dst_reg == 0) { in insn_is_helper_call()
7321 *func_id = insn->imm; in insn_is_helper_call()
7329 struct bpf_insn *insn = prog->insns; in bpf_object__sanitize_prog()
7333 if (obj->gen_loader) in bpf_object__sanitize_prog()
7336 for (i = 0; i < prog->insns_cnt; i++, insn++) { in bpf_object__sanitize_prog()
7348 insn->imm = BPF_FUNC_probe_read; in bpf_object__sanitize_prog()
7353 insn->imm = BPF_FUNC_probe_read_str; in bpf_object__sanitize_prog()
7365 /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
7372 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) in libbpf_prepare_prog_load()
7373 opts->expected_attach_type = 0; in libbpf_prepare_prog_load()
7376 opts->prog_flags |= BPF_F_SLEEPABLE; in libbpf_prepare_prog_load()
7378 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) in libbpf_prepare_prog_load()
7379 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; in libbpf_prepare_prog_load()
7382 if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) in libbpf_prepare_prog_load()
7383 prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; in libbpf_prepare_prog_load()
7385 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { in libbpf_prepare_prog_load()
7389 attach_name = strchr(prog->sec_name, '/'); in libbpf_prepare_prog_load()
7400 …pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\… in libbpf_prepare_prog_load()
7401 prog->name); in libbpf_prepare_prog_load()
7402 return -EINVAL; in libbpf_prepare_prog_load()
7411 prog->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
7412 prog->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
7415 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because in libbpf_prepare_prog_load()
7419 opts->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
7420 opts->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
7438 __u32 log_level = prog->log_level; in bpf_object_load_prog()
7440 if (prog->type == BPF_PROG_TYPE_UNSPEC) { in bpf_object_load_prog()
7446 prog->name, prog->sec_name); in bpf_object_load_prog()
7447 return -EINVAL; in bpf_object_load_prog()
7451 return -EINVAL; in bpf_object_load_prog()
7454 prog_name = prog->name; in bpf_object_load_prog()
7455 load_attr.attach_prog_fd = prog->attach_prog_fd; in bpf_object_load_prog()
7456 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; in bpf_object_load_prog()
7457 load_attr.attach_btf_id = prog->attach_btf_id; in bpf_object_load_prog()
7459 load_attr.prog_ifindex = prog->prog_ifindex; in bpf_object_load_prog()
7462 btf_fd = btf__fd(obj->btf); in bpf_object_load_prog()
7465 load_attr.func_info = prog->func_info; in bpf_object_load_prog()
7466 load_attr.func_info_rec_size = prog->func_info_rec_size; in bpf_object_load_prog()
7467 load_attr.func_info_cnt = prog->func_info_cnt; in bpf_object_load_prog()
7468 load_attr.line_info = prog->line_info; in bpf_object_load_prog()
7469 load_attr.line_info_rec_size = prog->line_info_rec_size; in bpf_object_load_prog()
7470 load_attr.line_info_cnt = prog->line_info_cnt; in bpf_object_load_prog()
7473 load_attr.prog_flags = prog->prog_flags; in bpf_object_load_prog()
7474 load_attr.fd_array = obj->fd_array; in bpf_object_load_prog()
7477 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { in bpf_object_load_prog()
7478 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); in bpf_object_load_prog()
7481 prog->name, err); in bpf_object_load_prog()
7484 insns = prog->insns; in bpf_object_load_prog()
7485 insns_cnt = prog->insns_cnt; in bpf_object_load_prog()
7489 load_attr.expected_attach_type = prog->expected_attach_type; in bpf_object_load_prog()
7491 if (obj->gen_loader) { in bpf_object_load_prog()
7492 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, in bpf_object_load_prog()
7494 prog - obj->programs); in bpf_object_load_prog()
7495 *prog_fd = -1; in bpf_object_load_prog()
7506 if (prog->log_buf) { in bpf_object_load_prog()
7507 log_buf = prog->log_buf; in bpf_object_load_prog()
7508 log_buf_size = prog->log_size; in bpf_object_load_prog()
7510 } else if (obj->log_buf) { in bpf_object_load_prog()
7511 log_buf = obj->log_buf; in bpf_object_load_prog()
7512 log_buf_size = obj->log_size; in bpf_object_load_prog()
7518 ret = -ENOMEM; in bpf_object_load_prog()
7531 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); in bpf_object_load_prog()
7534 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
7535 prog->name, log_buf); in bpf_object_load_prog()
7538 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { in bpf_object_load_prog()
7542 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_load_prog()
7543 map = &prog->obj->maps[i]; in bpf_object_load_prog()
7544 if (map->libbpf_type != LIBBPF_MAP_RODATA) in bpf_object_load_prog()
7547 if (bpf_prog_bind_map(ret, map->fd, NULL)) { in bpf_object_load_prog()
7550 prog->name, map->real_name, cp); in bpf_object_load_prog()
7570 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). in bpf_object_load_prog()
7575 ret = -errno; in bpf_object_load_prog()
7577 /* post-process verifier log to improve error descriptions */ in bpf_object_load_prog()
7581 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp); in bpf_object_load_prog()
7585 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
7586 prog->name, log_buf); in bpf_object_load_prog()
7602 p = cur - 1; in find_prev_line()
7603 while (p - 1 >= buf && *(p - 1) != '\n') in find_prev_line()
7604 p--; in find_prev_line()
7612 /* size of the remaining log content to the right from the to-be-replaced part */ in patch_log()
7613 size_t rem_sz = (buf + log_sz) - (orig + orig_sz); in patch_log()
7618 * shift log contents by (patch_sz - orig_sz) bytes to the right in patch_log()
7619 * starting from after to-be-replaced part of the log. in patch_log()
7622 * shift log contents by (orig_sz - patch_sz) bytes to the left in patch_log()
7623 * starting from after to-be-replaced part of the log in patch_log()
7632 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; in patch_log()
7634 } else if (patch_sz - orig_sz > buf_sz - log_sz) { in patch_log()
7636 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); in patch_log()
7650 /* Expected log for failed and not properly guarded CO-RE relocation: in fixup_log_failed_core_relo()
7651 * line1 -> 123: (85) call unknown#195896080 in fixup_log_failed_core_relo()
7652 * line2 -> invalid func unknown#195896080 in fixup_log_failed_core_relo()
7653 * line3 -> <anything else or end of buffer> in fixup_log_failed_core_relo()
7656 * instruction index to find corresponding CO-RE relocation and in fixup_log_failed_core_relo()
7658 * failed CO-RE relocation. in fixup_log_failed_core_relo()
7672 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); in fixup_log_failed_core_relo()
7678 "%d: <invalid CO-RE relocation>\n" in fixup_log_failed_core_relo()
7679 "failed to resolve CO-RE relocation %s%s\n", in fixup_log_failed_core_relo()
7682 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_failed_core_relo()
7690 * line1 -> 123: (85) call unknown#2001000345 in fixup_log_missing_map_load()
7691 * line2 -> invalid func unknown#2001000345 in fixup_log_missing_map_load()
7692 * line3 -> <anything else or end of buffer> in fixup_log_missing_map_load()
7695 * "345" in "2001000345" is a map index in obj->maps to fetch map name. in fixup_log_missing_map_load()
7697 struct bpf_object *obj = prog->obj; in fixup_log_missing_map_load()
7705 map_idx -= POISON_LDIMM64_MAP_BASE; in fixup_log_missing_map_load()
7706 if (map_idx < 0 || map_idx >= obj->nr_maps) in fixup_log_missing_map_load()
7708 map = &obj->maps[map_idx]; in fixup_log_missing_map_load()
7713 insn_idx, map->name); in fixup_log_missing_map_load()
7715 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_map_load()
7723 * line1 -> 123: (85) call unknown#2002000345 in fixup_log_missing_kfunc_call()
7724 * line2 -> invalid func unknown#2002000345 in fixup_log_missing_kfunc_call()
7725 * line3 -> <anything else or end of buffer> in fixup_log_missing_kfunc_call()
7728 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. in fixup_log_missing_kfunc_call()
7730 struct bpf_object *obj = prog->obj; in fixup_log_missing_kfunc_call()
7731 const struct extern_desc *ext; in fixup_log_missing_kfunc_call() local
7738 ext_idx -= POISON_CALL_KFUNC_BASE; in fixup_log_missing_kfunc_call()
7739 if (ext_idx < 0 || ext_idx >= obj->nr_extern) in fixup_log_missing_kfunc_call()
7741 ext = &obj->externs[ext_idx]; in fixup_log_missing_kfunc_call()
7746 insn_idx, ext->name); in fixup_log_missing_kfunc_call()
7748 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_kfunc_call()
7763 next_line = buf + log_sz - 1; in fixup_verifier_log()
7775 /* failed CO-RE relocation case */ in fixup_verifier_log()
7803 struct bpf_object *obj = prog->obj; in bpf_program_record_relos()
7806 for (i = 0; i < prog->nr_reloc; i++) { in bpf_program_record_relos()
7807 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_program_record_relos()
7808 struct extern_desc *ext = &obj->externs[relo->ext_idx]; in bpf_program_record_relos() local
7811 switch (relo->type) { in bpf_program_record_relos()
7813 if (ext->type != EXT_KSYM) in bpf_program_record_relos()
7815 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? in bpf_program_record_relos()
7817 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7818 ext->is_weak, !ext->ksym.type_id, in bpf_program_record_relos()
7819 true, kind, relo->insn_idx); in bpf_program_record_relos()
7822 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7823 ext->is_weak, false, false, BTF_KIND_FUNC, in bpf_program_record_relos()
7824 relo->insn_idx); in bpf_program_record_relos()
7828 .insn_off = relo->insn_idx * 8, in bpf_program_record_relos()
7829 .type_id = relo->core_relo->type_id, in bpf_program_record_relos()
7830 .access_str_off = relo->core_relo->access_str_off, in bpf_program_record_relos()
7831 .kind = relo->core_relo->kind, in bpf_program_record_relos()
7834 bpf_gen__record_relo_core(obj->gen_loader, &cr); in bpf_program_record_relos()
7851 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7852 prog = &obj->programs[i]; in bpf_object__load_progs()
7858 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7859 prog = &obj->programs[i]; in bpf_object__load_progs()
7862 if (!prog->autoload) { in bpf_object__load_progs()
7863 pr_debug("prog '%s': skipped loading\n", prog->name); in bpf_object__load_progs()
7866 prog->log_level |= log_level; in bpf_object__load_progs()
7868 if (obj->gen_loader) in bpf_object__load_progs()
7871 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, in bpf_object__load_progs()
7872 obj->license, obj->kern_version, &prog->fd); in bpf_object__load_progs()
7874 pr_warn("prog '%s': failed to load: %d\n", prog->name, err); in bpf_object__load_progs()
7891 prog->sec_def = find_sec_def(prog->sec_name); in bpf_object_init_progs()
7892 if (!prog->sec_def) { in bpf_object_init_progs()
7895 prog->name, prog->sec_name); in bpf_object_init_progs()
7899 prog->type = prog->sec_def->prog_type; in bpf_object_init_progs()
7900 prog->expected_attach_type = prog->sec_def->expected_attach_type; in bpf_object_init_progs()
7905 if (prog->sec_def->prog_setup_fn) { in bpf_object_init_progs()
7906 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); in bpf_object_init_progs()
7909 prog->name, err); in bpf_object_init_progs()
7932 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); in bpf_object_open()
7936 return ERR_PTR(-EINVAL); in bpf_object_open()
7941 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", in bpf_object_open()
7954 return ERR_PTR(-EINVAL); in bpf_object_open()
7956 return ERR_PTR(-EINVAL); in bpf_object_open()
7962 obj->log_buf = log_buf; in bpf_object_open()
7963 obj->log_size = log_size; in bpf_object_open()
7964 obj->log_level = log_level; in bpf_object_open()
7969 err = -ENAMETOOLONG; in bpf_object_open()
7972 obj->btf_custom_path = strdup(btf_tmp_path); in bpf_object_open()
7973 if (!obj->btf_custom_path) { in bpf_object_open()
7974 err = -ENOMEM; in bpf_object_open()
7981 obj->kconfig = strdup(kconfig); in bpf_object_open()
7982 if (!obj->kconfig) { in bpf_object_open()
7983 err = -ENOMEM; in bpf_object_open()
8011 return libbpf_err_ptr(-EINVAL); in bpf_object__open_file()
8028 return libbpf_err_ptr(-EINVAL); in bpf_object__open_mem()
8038 return libbpf_err(-EINVAL); in bpf_object_unload()
8040 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_unload()
8041 zclose(obj->maps[i].fd); in bpf_object_unload()
8042 if (obj->maps[i].st_ops) in bpf_object_unload()
8043 zfree(&obj->maps[i].st_ops->kern_vdata); in bpf_object_unload()
8046 for (i = 0; i < obj->nr_programs; i++) in bpf_object_unload()
8047 bpf_program__unload(&obj->programs[i]); in bpf_object_unload()
8060 m->def.map_flags &= ~BPF_F_MMAPABLE; in bpf_object__sanitize_maps()
8075 err = -errno; in libbpf_kallsyms_parse()
8087 err = -EINVAL; in libbpf_kallsyms_parse()
8105 struct extern_desc *ext; in kallsyms_cb() local
8107 ext = find_extern_by_name(obj, sym_name); in kallsyms_cb()
8108 if (!ext || ext->type != EXT_KSYM) in kallsyms_cb()
8111 t = btf__type_by_id(obj->btf, ext->btf_id); in kallsyms_cb()
8115 if (ext->is_set && ext->ksym.addr != sym_addr) { in kallsyms_cb()
8117 sym_name, ext->ksym.addr, sym_addr); in kallsyms_cb()
8118 return -EINVAL; in kallsyms_cb()
8120 if (!ext->is_set) { in kallsyms_cb()
8121 ext->is_set = true; in kallsyms_cb()
8122 ext->ksym.addr = sym_addr; in kallsyms_cb()
8141 btf = obj->btf_vmlinux; in find_ksym_btf_id()
8145 if (id == -ENOENT) { in find_ksym_btf_id()
8150 for (i = 0; i < obj->btf_module_cnt; i++) { in find_ksym_btf_id()
8152 mod_btf = &obj->btf_modules[i]; in find_ksym_btf_id()
8153 btf = mod_btf->btf; in find_ksym_btf_id()
8155 if (id != -ENOENT) in find_ksym_btf_id()
8160 return -ESRCH; in find_ksym_btf_id()
8168 struct extern_desc *ext) in bpf_object__resolve_ksym_var_btf_id() argument
8177 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); in bpf_object__resolve_ksym_var_btf_id()
8179 if (id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_var_btf_id()
8182 ext->name); in bpf_object__resolve_ksym_var_btf_id()
8187 local_type_id = ext->ksym.type_id; in bpf_object__resolve_ksym_var_btf_id()
8191 targ_var_name = btf__name_by_offset(btf, targ_var->name_off); in bpf_object__resolve_ksym_var_btf_id()
8192 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); in bpf_object__resolve_ksym_var_btf_id()
8194 err = bpf_core_types_are_compat(obj->btf, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
8200 local_type = btf__type_by_id(obj->btf, local_type_id); in bpf_object__resolve_ksym_var_btf_id()
8201 local_name = btf__name_by_offset(obj->btf, local_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
8202 targ_name = btf__name_by_offset(btf, targ_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
8205 ext->name, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
8208 return -EINVAL; in bpf_object__resolve_ksym_var_btf_id()
8211 ext->is_set = true; in bpf_object__resolve_ksym_var_btf_id()
8212 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_var_btf_id()
8213 ext->ksym.kernel_btf_id = id; in bpf_object__resolve_ksym_var_btf_id()
8215 ext->name, id, btf_kind_str(targ_var), targ_var_name); in bpf_object__resolve_ksym_var_btf_id()
8221 struct extern_desc *ext) in bpf_object__resolve_ksym_func_btf_id() argument
8229 local_func_proto_id = ext->ksym.type_id; in bpf_object__resolve_ksym_func_btf_id()
8231 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()
8234 if (kfunc_id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
8237 ext->name); in bpf_object__resolve_ksym_func_btf_id()
8242 kfunc_proto_id = kern_func->type; in bpf_object__resolve_ksym_func_btf_id()
8244 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
8247 if (ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
8251 ext->name, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
8252 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); in bpf_object__resolve_ksym_func_btf_id()
8253 return -EINVAL; in bpf_object__resolve_ksym_func_btf_id()
8257 if (mod_btf && !mod_btf->fd_array_idx) { in bpf_object__resolve_ksym_func_btf_id()
8258 /* insn->off is s16 */ in bpf_object__resolve_ksym_func_btf_id()
8259 if (obj->fd_array_cnt == INT16_MAX) { in bpf_object__resolve_ksym_func_btf_id()
8261 ext->name, mod_btf->fd_array_idx); in bpf_object__resolve_ksym_func_btf_id()
8262 return -E2BIG; in bpf_object__resolve_ksym_func_btf_id()
8265 if (!obj->fd_array_cnt) in bpf_object__resolve_ksym_func_btf_id()
8266 obj->fd_array_cnt = 1; in bpf_object__resolve_ksym_func_btf_id()
8268 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), in bpf_object__resolve_ksym_func_btf_id()
8269 obj->fd_array_cnt + 1); in bpf_object__resolve_ksym_func_btf_id()
8272 mod_btf->fd_array_idx = obj->fd_array_cnt; in bpf_object__resolve_ksym_func_btf_id()
8274 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; in bpf_object__resolve_ksym_func_btf_id()
8277 ext->is_set = true; in bpf_object__resolve_ksym_func_btf_id()
8278 ext->ksym.kernel_btf_id = kfunc_id; in bpf_object__resolve_ksym_func_btf_id()
8279 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; in bpf_object__resolve_ksym_func_btf_id()
8282 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. in bpf_object__resolve_ksym_func_btf_id()
8283 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. in bpf_object__resolve_ksym_func_btf_id()
8285 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_func_btf_id()
8287 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); in bpf_object__resolve_ksym_func_btf_id()
8295 struct extern_desc *ext; in bpf_object__resolve_ksyms_btf_id() local
8298 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_ksyms_btf_id()
8299 ext = &obj->externs[i]; in bpf_object__resolve_ksyms_btf_id()
8300 if (ext->type != EXT_KSYM || !ext->ksym.type_id) in bpf_object__resolve_ksyms_btf_id()
8303 if (obj->gen_loader) { in bpf_object__resolve_ksyms_btf_id()
8304 ext->is_set = true; in bpf_object__resolve_ksyms_btf_id()
8305 ext->ksym.kernel_btf_obj_fd = 0; in bpf_object__resolve_ksyms_btf_id()
8306 ext->ksym.kernel_btf_id = 0; in bpf_object__resolve_ksyms_btf_id()
8309 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__resolve_ksyms_btf_id()
8311 err = bpf_object__resolve_ksym_var_btf_id(obj, ext); in bpf_object__resolve_ksyms_btf_id()
8313 err = bpf_object__resolve_ksym_func_btf_id(obj, ext); in bpf_object__resolve_ksyms_btf_id()
8325 struct extern_desc *ext; in bpf_object__resolve_externs() local
8329 if (obj->nr_extern == 0) in bpf_object__resolve_externs()
8332 if (obj->kconfig_map_idx >= 0) in bpf_object__resolve_externs()
8333 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; in bpf_object__resolve_externs()
8335 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8336 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8338 if (ext->type == EXT_KSYM) { in bpf_object__resolve_externs()
8339 if (ext->ksym.type_id) in bpf_object__resolve_externs()
8344 } else if (ext->type == EXT_KCFG) { in bpf_object__resolve_externs()
8345 void *ext_ptr = kcfg_data + ext->kcfg.data_off; in bpf_object__resolve_externs()
8349 if (str_has_pfx(ext->name, "CONFIG_")) { in bpf_object__resolve_externs()
8355 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { in bpf_object__resolve_externs()
8358 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); in bpf_object__resolve_externs()
8359 return -EINVAL; in bpf_object__resolve_externs()
8361 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { in bpf_object__resolve_externs()
8363 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { in bpf_object__resolve_externs()
8365 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { in bpf_object__resolve_externs()
8373 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); in bpf_object__resolve_externs()
8374 return -EINVAL; in bpf_object__resolve_externs()
8377 err = set_kcfg_value_num(ext, ext_ptr, value); in bpf_object__resolve_externs()
8381 ext->name, (long long)value); in bpf_object__resolve_externs()
8383 pr_warn("extern '%s': unrecognized extern kind\n", ext->name); in bpf_object__resolve_externs()
8384 return -EINVAL; in bpf_object__resolve_externs()
8390 return -EINVAL; in bpf_object__resolve_externs()
8392 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8393 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8394 if (ext->type == EXT_KCFG && !ext->is_set) { in bpf_object__resolve_externs()
8403 return -EINVAL; in bpf_object__resolve_externs()
8408 return -EINVAL; in bpf_object__resolve_externs()
8413 return -EINVAL; in bpf_object__resolve_externs()
8415 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
8416 ext = &obj->externs[i]; in bpf_object__resolve_externs()
8418 if (!ext->is_set && !ext->is_weak) { in bpf_object__resolve_externs()
8419 pr_warn("extern '%s' (strong): not resolved\n", ext->name); in bpf_object__resolve_externs()
8420 return -ESRCH; in bpf_object__resolve_externs()
8421 } else if (!ext->is_set) { in bpf_object__resolve_externs()
8423 ext->name); in bpf_object__resolve_externs()
8435 st_ops = map->st_ops; in bpf_map_prepare_vdata()
8436 for (i = 0; i < btf_vlen(st_ops->type); i++) { in bpf_map_prepare_vdata()
8437 struct bpf_program *prog = st_ops->progs[i]; in bpf_map_prepare_vdata()
8445 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; in bpf_map_prepare_vdata()
8454 for (i = 0; i < obj->nr_maps; i++) in bpf_object_prepare_struct_ops()
8455 if (bpf_map__is_struct_ops(&obj->maps[i])) in bpf_object_prepare_struct_ops()
8456 bpf_map_prepare_vdata(&obj->maps[i]); in bpf_object_prepare_struct_ops()
8466 return libbpf_err(-EINVAL); in bpf_object_load()
8468 if (obj->loaded) { in bpf_object_load()
8469 pr_warn("object '%s': load can't be attempted twice\n", obj->name); in bpf_object_load()
8470 return libbpf_err(-EINVAL); in bpf_object_load()
8473 if (obj->gen_loader) in bpf_object_load()
8474 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); in bpf_object_load()
8478 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); in bpf_object_load()
8481 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); in bpf_object_load()
8488 if (obj->gen_loader) { in bpf_object_load()
8490 if (obj->btf) in bpf_object_load()
8491 btf__set_fd(obj->btf, -1); in bpf_object_load()
8493 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); in bpf_object_load()
8497 zfree(&obj->fd_array); in bpf_object_load()
8500 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_object_load()
8501 close(obj->btf_modules[i].fd); in bpf_object_load()
8502 btf__free(obj->btf_modules[i].btf); in bpf_object_load()
8503 free(obj->btf_modules[i].name); in bpf_object_load()
8505 free(obj->btf_modules); in bpf_object_load()
8508 btf__free(obj->btf_vmlinux); in bpf_object_load()
8509 obj->btf_vmlinux = NULL; in bpf_object_load()
8511 obj->loaded = true; /* doesn't matter if successfully or not */ in bpf_object_load()
8518 /* unpin any maps that were auto-pinned during load */ in bpf_object_load()
8519 for (i = 0; i < obj->nr_maps; i++) in bpf_object_load()
8520 if (obj->maps[i].pinned && !obj->maps[i].reused) in bpf_object_load()
8521 bpf_map__unpin(&obj->maps[i], NULL); in bpf_object_load()
8524 pr_warn("failed to load object '%s'\n", obj->path); in bpf_object_load()
8541 return -ENOMEM; in make_parent_dir()
8545 err = -errno; in make_parent_dir()
8549 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in make_parent_dir()
8563 return -EINVAL; in check_path()
8567 return -ENOMEM; in check_path()
8573 err = -errno; in check_path()
8579 err = -EINVAL; in check_path()
8590 if (prog->fd < 0) { in bpf_program__pin()
8591 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); in bpf_program__pin()
8592 return libbpf_err(-EINVAL); in bpf_program__pin()
8603 if (bpf_obj_pin(prog->fd, path)) { in bpf_program__pin()
8604 err = -errno; in bpf_program__pin()
8606 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp); in bpf_program__pin()
8610 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); in bpf_program__pin()
8618 if (prog->fd < 0) { in bpf_program__unpin()
8619 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); in bpf_program__unpin()
8620 return libbpf_err(-EINVAL); in bpf_program__unpin()
8629 return libbpf_err(-errno); in bpf_program__unpin()
8631 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); in bpf_program__unpin()
8642 return libbpf_err(-EINVAL); in bpf_map__pin()
8645 if (map->pin_path) { in bpf_map__pin()
8646 if (path && strcmp(path, map->pin_path)) { in bpf_map__pin()
8648 bpf_map__name(map), map->pin_path, path); in bpf_map__pin()
8649 return libbpf_err(-EINVAL); in bpf_map__pin()
8650 } else if (map->pinned) { in bpf_map__pin()
8651 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", in bpf_map__pin()
8652 bpf_map__name(map), map->pin_path); in bpf_map__pin()
8659 return libbpf_err(-EINVAL); in bpf_map__pin()
8660 } else if (map->pinned) { in bpf_map__pin()
8662 return libbpf_err(-EEXIST); in bpf_map__pin()
8665 map->pin_path = strdup(path); in bpf_map__pin()
8666 if (!map->pin_path) { in bpf_map__pin()
8667 err = -errno; in bpf_map__pin()
8672 err = make_parent_dir(map->pin_path); in bpf_map__pin()
8676 err = check_path(map->pin_path); in bpf_map__pin()
8680 if (bpf_obj_pin(map->fd, map->pin_path)) { in bpf_map__pin()
8681 err = -errno; in bpf_map__pin()
8685 map->pinned = true; in bpf_map__pin()
8686 pr_debug("pinned map '%s'\n", map->pin_path); in bpf_map__pin()
8691 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in bpf_map__pin()
8702 return libbpf_err(-EINVAL); in bpf_map__unpin()
8705 if (map->pin_path) { in bpf_map__unpin()
8706 if (path && strcmp(path, map->pin_path)) { in bpf_map__unpin()
8708 bpf_map__name(map), map->pin_path, path); in bpf_map__unpin()
8709 return libbpf_err(-EINVAL); in bpf_map__unpin()
8711 path = map->pin_path; in bpf_map__unpin()
8715 return libbpf_err(-EINVAL); in bpf_map__unpin()
8724 return libbpf_err(-errno); in bpf_map__unpin()
8726 map->pinned = false; in bpf_map__unpin()
8739 return libbpf_err(-errno); in bpf_map__set_pin_path()
8742 free(map->pin_path); in bpf_map__set_pin_path()
8743 map->pin_path = new; in bpf_map__set_pin_path()
8752 return map->pin_path; in bpf_map__pin_path()
8757 return map->pinned; in bpf_map__is_pinned()
8776 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8778 if (!obj->loaded) { in bpf_object__pin_maps()
8780 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8787 if (!map->autocreate) in bpf_object__pin_maps()
8796 } else if (!map->pin_path) { in bpf_object__pin_maps()
8809 if (!map->pin_path) in bpf_object__pin_maps()
8824 return libbpf_err(-ENOENT); in bpf_object__unpin_maps()
8836 } else if (!map->pin_path) { in bpf_object__unpin_maps()
8855 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8857 if (!obj->loaded) { in bpf_object__pin_programs()
8859 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8863 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__pin_programs()
8876 if (pathname_concat(buf, sizeof(buf), path, prog->name)) in bpf_object__pin_programs()
8891 return libbpf_err(-ENOENT); in bpf_object__unpin_programs()
8896 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__unpin_programs()
8942 if (map->inner_map) { in bpf_map__destroy()
8943 bpf_map__destroy(map->inner_map); in bpf_map__destroy()
8944 zfree(&map->inner_map); in bpf_map__destroy()
8947 zfree(&map->init_slots); in bpf_map__destroy()
8948 map->init_slots_sz = 0; in bpf_map__destroy()
8950 if (map->mmaped) { in bpf_map__destroy()
8953 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_map__destroy()
8954 munmap(map->mmaped, mmap_sz); in bpf_map__destroy()
8955 map->mmaped = NULL; in bpf_map__destroy()
8958 if (map->st_ops) { in bpf_map__destroy()
8959 zfree(&map->st_ops->data); in bpf_map__destroy()
8960 zfree(&map->st_ops->progs); in bpf_map__destroy()
8961 zfree(&map->st_ops->kern_func_off); in bpf_map__destroy()
8962 zfree(&map->st_ops); in bpf_map__destroy()
8965 zfree(&map->name); in bpf_map__destroy()
8966 zfree(&map->real_name); in bpf_map__destroy()
8967 zfree(&map->pin_path); in bpf_map__destroy()
8969 if (map->fd >= 0) in bpf_map__destroy()
8970 zclose(map->fd); in bpf_map__destroy()
8980 usdt_manager_free(obj->usdt_man); in bpf_object__close()
8981 obj->usdt_man = NULL; in bpf_object__close()
8983 bpf_gen__free(obj->gen_loader); in bpf_object__close()
8986 btf__free(obj->btf); in bpf_object__close()
8987 btf__free(obj->btf_vmlinux); in bpf_object__close()
8988 btf_ext__free(obj->btf_ext); in bpf_object__close()
8990 for (i = 0; i < obj->nr_maps; i++) in bpf_object__close()
8991 bpf_map__destroy(&obj->maps[i]); in bpf_object__close()
8993 zfree(&obj->btf_custom_path); in bpf_object__close()
8994 zfree(&obj->kconfig); in bpf_object__close()
8996 for (i = 0; i < obj->nr_extern; i++) in bpf_object__close()
8997 zfree(&obj->externs[i].essent_name); in bpf_object__close()
8999 zfree(&obj->externs); in bpf_object__close()
9000 obj->nr_extern = 0; in bpf_object__close()
9002 zfree(&obj->maps); in bpf_object__close()
9003 obj->nr_maps = 0; in bpf_object__close()
9005 if (obj->programs && obj->nr_programs) { in bpf_object__close()
9006 for (i = 0; i < obj->nr_programs; i++) in bpf_object__close()
9007 bpf_program__exit(&obj->programs[i]); in bpf_object__close()
9009 zfree(&obj->programs); in bpf_object__close()
9016 return obj ? obj->name : libbpf_err_ptr(-EINVAL); in bpf_object__name()
9021 return obj ? obj->kern_version : 0; in bpf_object__kversion()
9026 return obj ? obj->btf : NULL; in bpf_object__btf()
9031 return obj->btf ? btf__fd(obj->btf) : -1; in bpf_object__btf_fd()
9036 if (obj->loaded) in bpf_object__set_kversion()
9037 return libbpf_err(-EINVAL); in bpf_object__set_kversion()
9039 obj->kern_version = kern_version; in bpf_object__set_kversion()
9046 struct bpf_gen *gen; in bpf_object__gen_loader() local
9049 return -EFAULT; in bpf_object__gen_loader()
9051 return -EINVAL; in bpf_object__gen_loader()
9052 gen = calloc(sizeof(*gen), 1); in bpf_object__gen_loader()
9053 if (!gen) in bpf_object__gen_loader()
9054 return -ENOMEM; in bpf_object__gen_loader()
9055 gen->opts = opts; in bpf_object__gen_loader()
9056 obj->gen_loader = gen; in bpf_object__gen_loader()
9064 size_t nr_programs = obj->nr_programs; in __bpf_program__iter()
9072 return forward ? &obj->programs[0] : in __bpf_program__iter()
9073 &obj->programs[nr_programs - 1]; in __bpf_program__iter()
9075 if (p->obj != obj) { in __bpf_program__iter()
9080 idx = (p - obj->programs) + (forward ? 1 : -1); in __bpf_program__iter()
9081 if (idx >= obj->nr_programs || idx < 0) in __bpf_program__iter()
9083 return &obj->programs[idx]; in __bpf_program__iter()
9112 prog->prog_ifindex = ifindex; in bpf_program__set_ifindex()
9117 return prog->name; in bpf_program__name()
9122 return prog->sec_name; in bpf_program__section_name()
9127 return prog->autoload; in bpf_program__autoload()
9132 if (prog->obj->loaded) in bpf_program__set_autoload()
9133 return libbpf_err(-EINVAL); in bpf_program__set_autoload()
9135 prog->autoload = autoload; in bpf_program__set_autoload()
9141 return prog->autoattach; in bpf_program__autoattach()
9146 prog->autoattach = autoattach; in bpf_program__set_autoattach()
9151 return prog->insns; in bpf_program__insns()
9156 return prog->insns_cnt; in bpf_program__insn_cnt()
9164 if (prog->obj->loaded) in bpf_program__set_insns()
9165 return -EBUSY; in bpf_program__set_insns()
9167 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); in bpf_program__set_insns()
9170 pr_warn("prog '%s': failed to realloc prog code\n", prog->name); in bpf_program__set_insns()
9171 return -ENOMEM; in bpf_program__set_insns()
9175 prog->insns = insns; in bpf_program__set_insns()
9176 prog->insns_cnt = new_insn_cnt; in bpf_program__set_insns()
9183 return libbpf_err(-EINVAL); in bpf_program__fd()
9185 if (prog->fd < 0) in bpf_program__fd()
9186 return libbpf_err(-ENOENT); in bpf_program__fd()
9188 return prog->fd; in bpf_program__fd()
9196 return prog->type; in bpf_program__type()
9207 if (prog->obj->loaded) in bpf_program__set_type()
9208 return libbpf_err(-EBUSY); in bpf_program__set_type()
9211 if (prog->type == type) in bpf_program__set_type()
9214 prog->type = type; in bpf_program__set_type()
9218 * fallback handler, which by definition is program type-agnostic and in bpf_program__set_type()
9219 * is a catch-all custom handler, optionally set by the application, in bpf_program__set_type()
9222 if (prog->sec_def != &custom_fallback_def) in bpf_program__set_type()
9223 prog->sec_def = NULL; in bpf_program__set_type()
9232 return prog->expected_attach_type; in bpf_program__expected_attach_type()
9238 if (prog->obj->loaded) in bpf_program__set_expected_attach_type()
9239 return libbpf_err(-EBUSY); in bpf_program__set_expected_attach_type()
9241 prog->expected_attach_type = type; in bpf_program__set_expected_attach_type()
9247 return prog->prog_flags; in bpf_program__flags()
9252 if (prog->obj->loaded) in bpf_program__set_flags()
9253 return libbpf_err(-EBUSY); in bpf_program__set_flags()
9255 prog->prog_flags = flags; in bpf_program__set_flags()
9261 return prog->log_level; in bpf_program__log_level()
9266 if (prog->obj->loaded) in bpf_program__set_log_level()
9267 return libbpf_err(-EBUSY); in bpf_program__set_log_level()
9269 prog->log_level = log_level; in bpf_program__set_log_level()
9275 *log_size = prog->log_size; in bpf_program__log_buf()
9276 return prog->log_buf; in bpf_program__log_buf()
9282 return -EINVAL; in bpf_program__set_log_buf()
9283 if (prog->log_size > UINT_MAX) in bpf_program__set_log_buf()
9284 return -EINVAL; in bpf_program__set_log_buf()
9285 if (prog->obj->loaded) in bpf_program__set_log_buf()
9286 return -EBUSY; in bpf_program__set_log_buf()
9288 prog->log_buf = log_buf; in bpf_program__set_log_buf()
9289 prog->log_size = log_size; in bpf_program__set_log_buf()
9356 SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
9424 return libbpf_err(-EINVAL); in libbpf_register_prog_handler()
9427 return libbpf_err(-E2BIG); in libbpf_register_prog_handler()
9433 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
9439 return libbpf_err(-EBUSY); in libbpf_register_prog_handler()
9444 sec_def->sec = sec ? strdup(sec) : NULL; in libbpf_register_prog_handler()
9445 if (sec && !sec_def->sec) in libbpf_register_prog_handler()
9446 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
9448 sec_def->prog_type = prog_type; in libbpf_register_prog_handler()
9449 sec_def->expected_attach_type = exp_attach_type; in libbpf_register_prog_handler()
9450 sec_def->cookie = OPTS_GET(opts, cookie, 0); in libbpf_register_prog_handler()
9452 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); in libbpf_register_prog_handler()
9453 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); in libbpf_register_prog_handler()
9454 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); in libbpf_register_prog_handler()
9456 sec_def->handler_id = ++last_custom_sec_def_handler_id; in libbpf_register_prog_handler()
9463 return sec_def->handler_id; in libbpf_register_prog_handler()
9472 return libbpf_err(-EINVAL); in libbpf_unregister_prog_handler()
9486 return libbpf_err(-ENOENT); in libbpf_unregister_prog_handler()
9490 custom_sec_defs[i - 1] = custom_sec_defs[i]; in libbpf_unregister_prog_handler()
9491 custom_sec_def_cnt--; in libbpf_unregister_prog_handler()
9507 size_t len = strlen(sec_def->sec); in sec_def_matches()
9510 if (sec_def->sec[len - 1] == '/') { in sec_def_matches()
9511 if (str_has_pfx(sec_name, sec_def->sec)) in sec_def_matches()
9517 * well-formed SEC("type/extras") with proper '/' separator in sec_def_matches()
9519 if (sec_def->sec[len - 1] == '+') { in sec_def_matches()
9520 len--; in sec_def_matches()
9522 if (strncmp(sec_name, sec_def->sec, len) != 0) in sec_def_matches()
9530 return strcmp(sec_name, sec_def->sec) == 0; in sec_def_matches()
9575 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_get_type_names()
9578 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_get_type_names()
9600 return libbpf_err(-EINVAL); in libbpf_prog_type_by_name()
9604 *prog_type = sec_def->prog_type; in libbpf_prog_type_by_name()
9605 *expected_attach_type = sec_def->expected_attach_type; in libbpf_prog_type_by_name()
9616 return libbpf_err(-ESRCH); in libbpf_prog_type_by_name()
9658 for (i = 0; i < obj->nr_maps; i++) { in find_struct_ops_map_by_offset()
9659 map = &obj->maps[i]; in find_struct_ops_map_by_offset()
9662 if (map->sec_idx == sec_idx && in find_struct_ops_map_by_offset()
9663 map->sec_offset <= offset && in find_struct_ops_map_by_offset()
9664 offset - map->sec_offset < map->def.value_size) in find_struct_ops_map_by_offset()
9671 /* Collect the reloc from ELF and populate the st_ops->progs[] */
9688 btf = obj->btf; in bpf_object__collect_st_ops_relos()
9689 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_st_ops_relos()
9694 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9697 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9700 (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9701 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9704 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_st_ops_relos()
9705 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); in bpf_object__collect_st_ops_relos()
9707 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", in bpf_object__collect_st_ops_relos()
9708 (size_t)rel->r_offset); in bpf_object__collect_st_ops_relos()
9709 return -EINVAL; in bpf_object__collect_st_ops_relos()
9712 moff = rel->r_offset - map->sec_offset; in bpf_object__collect_st_ops_relos()
9713 shdr_idx = sym->st_shndx; in bpf_object__collect_st_ops_relos()
9714 st_ops = map->st_ops; in bpf_object__collect_st_ops_relos()
9715 …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()
9716 map->name, in bpf_object__collect_st_ops_relos()
9717 (long long)(rel->r_info >> 32), in bpf_object__collect_st_ops_relos()
9718 (long long)sym->st_value, in bpf_object__collect_st_ops_relos()
9719 shdr_idx, (size_t)rel->r_offset, in bpf_object__collect_st_ops_relos()
9720 map->sec_offset, sym->st_name, name); in bpf_object__collect_st_ops_relos()
9723 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()
9724 map->name, (size_t)rel->r_offset, shdr_idx); in bpf_object__collect_st_ops_relos()
9725 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_st_ops_relos()
9727 if (sym->st_value % BPF_INSN_SZ) { in bpf_object__collect_st_ops_relos()
9729 map->name, (unsigned long long)sym->st_value); in bpf_object__collect_st_ops_relos()
9730 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9732 insn_idx = sym->st_value / BPF_INSN_SZ; in bpf_object__collect_st_ops_relos()
9734 member = find_member_by_offset(st_ops->type, moff * 8); in bpf_object__collect_st_ops_relos()
9737 map->name, moff); in bpf_object__collect_st_ops_relos()
9738 return -EINVAL; in bpf_object__collect_st_ops_relos()
9740 member_idx = member - btf_members(st_ops->type); in bpf_object__collect_st_ops_relos()
9741 name = btf__name_by_offset(btf, member->name_off); in bpf_object__collect_st_ops_relos()
9743 if (!resolve_func_ptr(btf, member->type, NULL)) { in bpf_object__collect_st_ops_relos()
9745 map->name, name); in bpf_object__collect_st_ops_relos()
9746 return -EINVAL; in bpf_object__collect_st_ops_relos()
9752 map->name, shdr_idx, name); in bpf_object__collect_st_ops_relos()
9753 return -EINVAL; in bpf_object__collect_st_ops_relos()
9757 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { in bpf_object__collect_st_ops_relos()
9759 map->name, prog->name); in bpf_object__collect_st_ops_relos()
9760 return -EINVAL; in bpf_object__collect_st_ops_relos()
9766 if (!prog->attach_btf_id) { in bpf_object__collect_st_ops_relos()
9767 prog->attach_btf_id = st_ops->type_id; in bpf_object__collect_st_ops_relos()
9768 prog->expected_attach_type = member_idx; in bpf_object__collect_st_ops_relos()
9771 /* struct_ops BPF prog can be re-used between multiple in bpf_object__collect_st_ops_relos()
9776 if (prog->attach_btf_id != st_ops->type_id || in bpf_object__collect_st_ops_relos()
9777 prog->expected_attach_type != member_idx) { in bpf_object__collect_st_ops_relos()
9779 map->name, prog->name, prog->sec_name, prog->type, in bpf_object__collect_st_ops_relos()
9780 prog->attach_btf_id, prog->expected_attach_type, name); in bpf_object__collect_st_ops_relos()
9781 return -EINVAL; in bpf_object__collect_st_ops_relos()
9784 st_ops->progs[member_idx] = prog; in bpf_object__collect_st_ops_relos()
9831 return -ENAMETOOLONG; in find_btf_by_prefix_kind()
9881 err = -EINVAL; in libbpf_find_prog_btf_id()
9908 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type); in find_kernel_btf_id()
9914 if (ret != -ENOENT) in find_kernel_btf_id()
9921 for (i = 0; i < obj->btf_module_cnt; i++) { in find_kernel_btf_id()
9922 const struct module_btf *mod = &obj->btf_modules[i]; in find_kernel_btf_id()
9924 ret = find_attach_btf_id(mod->btf, attach_name, attach_type); in find_kernel_btf_id()
9926 *btf_obj_fd = mod->fd; in find_kernel_btf_id()
9930 if (ret == -ENOENT) in find_kernel_btf_id()
9936 return -ESRCH; in find_kernel_btf_id()
9942 enum bpf_attach_type attach_type = prog->expected_attach_type; in libbpf_find_attach_btf_id()
9943 __u32 attach_prog_fd = prog->attach_prog_fd; in libbpf_find_attach_btf_id()
9947 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { in libbpf_find_attach_btf_id()
9949 pr_warn("prog '%s': attach program FD is not set\n", prog->name); in libbpf_find_attach_btf_id()
9950 return -EINVAL; in libbpf_find_attach_btf_id()
9955 prog->name, attach_prog_fd, attach_name, err); in libbpf_find_attach_btf_id()
9964 if (prog->obj->gen_loader) { in libbpf_find_attach_btf_id()
9965 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); in libbpf_find_attach_btf_id()
9969 err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id); in libbpf_find_attach_btf_id()
9973 prog->name, attach_name, err); in libbpf_find_attach_btf_id()
9986 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
9997 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10000 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_attach_type_by_name()
10001 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10002 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_attach_type_by_name()
10003 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
10005 *attach_type = sec_def->expected_attach_type; in libbpf_attach_type_by_name()
10012 return libbpf_err(-EINVAL); in bpf_map__fd()
10014 return -1; in bpf_map__fd()
10015 return map->fd; in bpf_map__fd()
10021 * their user-visible name differs from kernel-visible name. Users see in map_uses_real_name()
10026 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) in map_uses_real_name()
10028 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) in map_uses_real_name()
10039 return map->real_name; in bpf_map__name()
10041 return map->name; in bpf_map__name()
10046 return map->def.type; in bpf_map__type()
10052 return libbpf_err(-EBUSY); in bpf_map__set_type()
10053 map->def.type = type; in bpf_map__set_type()
10059 return map->def.map_flags; in bpf_map__map_flags()
10065 return libbpf_err(-EBUSY); in bpf_map__set_map_flags()
10066 map->def.map_flags = flags; in bpf_map__set_map_flags()
10072 return map->map_extra; in bpf_map__map_extra()
10078 return libbpf_err(-EBUSY); in bpf_map__set_map_extra()
10079 map->map_extra = map_extra; in bpf_map__set_map_extra()
10085 return map->numa_node; in bpf_map__numa_node()
10091 return libbpf_err(-EBUSY); in bpf_map__set_numa_node()
10092 map->numa_node = numa_node; in bpf_map__set_numa_node()
10098 return map->def.key_size; in bpf_map__key_size()
10104 return libbpf_err(-EBUSY); in bpf_map__set_key_size()
10105 map->def.key_size = size; in bpf_map__set_key_size()
10111 return map->def.value_size; in bpf_map__value_size()
10125 btf = bpf_object__btf(map->obj); in map_btf_datasec_resize()
10127 return -ENOENT; in map_btf_datasec_resize()
10134 return -EINVAL; in map_btf_datasec_resize()
10142 return -EINVAL; in map_btf_datasec_resize()
10146 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
10147 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
10148 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); in map_btf_datasec_resize()
10152 return -EINVAL; in map_btf_datasec_resize()
10157 element_sz = btf__resolve_size(btf, array->type); in map_btf_datasec_resize()
10158 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { in map_btf_datasec_resize()
10161 return -EINVAL; in map_btf_datasec_resize()
10165 nr_elements = (size - var->offset) / element_sz; in map_btf_datasec_resize()
10166 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); in map_btf_datasec_resize()
10173 datasec_type = btf_type_by_id(btf, map->btf_value_type_id); in map_btf_datasec_resize()
10174 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
10175 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
10178 datasec_type->size = size; in map_btf_datasec_resize()
10179 var->size = size - var->offset; in map_btf_datasec_resize()
10180 var_type->type = new_array_id; in map_btf_datasec_resize()
10187 if (map->obj->loaded || map->reused) in bpf_map__set_value_size()
10188 return libbpf_err(-EBUSY); in bpf_map__set_value_size()
10190 if (map->mmaped) { in bpf_map__set_value_size()
10194 mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_map__set_value_size()
10195 mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries); in bpf_map__set_value_size()
10198 pr_warn("map '%s': failed to resize memory-mapped region: %d\n", in bpf_map__set_value_size()
10203 if (err && err != -ENOENT) { in bpf_map__set_value_size()
10206 map->btf_value_type_id = 0; in bpf_map__set_value_size()
10207 map->btf_key_type_id = 0; in bpf_map__set_value_size()
10211 map->def.value_size = size; in bpf_map__set_value_size()
10217 return map ? map->btf_key_type_id : 0; in bpf_map__btf_key_type_id()
10222 return map ? map->btf_value_type_id : 0; in bpf_map__btf_value_type_id()
10228 if (map->obj->loaded || map->reused) in bpf_map__set_initial_value()
10229 return libbpf_err(-EBUSY); in bpf_map__set_initial_value()
10231 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG || in bpf_map__set_initial_value()
10232 size != map->def.value_size) in bpf_map__set_initial_value()
10233 return libbpf_err(-EINVAL); in bpf_map__set_initial_value()
10235 memcpy(map->mmaped, data, size); in bpf_map__set_initial_value()
10241 if (!map->mmaped) in bpf_map__initial_value()
10243 *psize = map->def.value_size; in bpf_map__initial_value()
10244 return map->mmaped; in bpf_map__initial_value()
10249 return map->libbpf_type != LIBBPF_MAP_UNSPEC; in bpf_map__is_internal()
10254 return map->map_ifindex; in bpf_map__ifindex()
10260 return libbpf_err(-EBUSY); in bpf_map__set_ifindex()
10261 map->map_ifindex = ifindex; in bpf_map__set_ifindex()
10267 if (!bpf_map_type__is_map_in_map(map->def.type)) { in bpf_map__set_inner_map_fd()
10269 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
10271 if (map->inner_map_fd != -1) { in bpf_map__set_inner_map_fd()
10273 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
10275 if (map->inner_map) { in bpf_map__set_inner_map_fd()
10276 bpf_map__destroy(map->inner_map); in bpf_map__set_inner_map_fd()
10277 zfree(&map->inner_map); in bpf_map__set_inner_map_fd()
10279 map->inner_map_fd = fd; in bpf_map__set_inner_map_fd()
10289 if (!obj || !obj->maps) in __bpf_map__iter()
10292 s = obj->maps; in __bpf_map__iter()
10293 e = obj->maps + obj->nr_maps; in __bpf_map__iter()
10301 idx = (m - obj->maps) + i; in __bpf_map__iter()
10302 if (idx >= obj->nr_maps || idx < 0) in __bpf_map__iter()
10304 return &obj->maps[idx]; in __bpf_map__iter()
10311 return obj->maps; in bpf_object__next_map()
10320 if (!obj->nr_maps) in bpf_object__prev_map()
10322 return obj->maps + obj->nr_maps - 1; in bpf_object__prev_map()
10325 return __bpf_map__iter(next, obj, -1); in bpf_object__prev_map()
10339 if (pos->real_name && strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
10345 if (strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
10349 if (strcmp(pos->name, name) == 0) in bpf_object__find_map_by_name()
10365 return -ENOENT; in validate_map_op()
10367 if (map->def.key_size != key_sz) { in validate_map_op()
10369 map->name, key_sz, map->def.key_size); in validate_map_op()
10370 return -EINVAL; in validate_map_op()
10376 switch (map->def.type) { in validate_map_op()
10382 size_t elem_sz = roundup(map->def.value_size, 8); in validate_map_op()
10385 … pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n", in validate_map_op()
10386 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); in validate_map_op()
10387 return -EINVAL; in validate_map_op()
10392 if (map->def.value_size != value_sz) { in validate_map_op()
10394 map->name, value_sz, map->def.value_size); in validate_map_op()
10395 return -EINVAL; in validate_map_op()
10412 return bpf_map_lookup_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_elem()
10425 return bpf_map_update_elem(map->fd, key, value, flags); in bpf_map__update_elem()
10437 return bpf_map_delete_elem_flags(map->fd, key, flags); in bpf_map__delete_elem()
10450 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_and_delete_elem()
10462 return bpf_map_get_next_key(map->fd, cur_key, next_key); in bpf_map__get_next_key()
10471 errno = -PTR_ERR(ptr); in libbpf_get_error()
10478 return -errno; in libbpf_get_error()
10502 link->disconnected = true; in bpf_link__disconnect()
10512 if (!link->disconnected && link->detach) in bpf_link__destroy()
10513 err = link->detach(link); in bpf_link__destroy()
10514 if (link->pin_path) in bpf_link__destroy()
10515 free(link->pin_path); in bpf_link__destroy()
10516 if (link->dealloc) in bpf_link__destroy()
10517 link->dealloc(link); in bpf_link__destroy()
10526 return link->fd; in bpf_link__fd()
10531 return link->pin_path; in bpf_link__pin_path()
10536 return libbpf_err_errno(close(link->fd)); in bpf_link__detach_fd()
10546 fd = -errno; in bpf_link__open()
10554 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
10556 link->detach = &bpf_link__detach_fd; in bpf_link__open()
10557 link->fd = fd; in bpf_link__open()
10559 link->pin_path = strdup(path); in bpf_link__open()
10560 if (!link->pin_path) { in bpf_link__open()
10562 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
10570 return bpf_link_detach(link->fd) ? -errno : 0; in bpf_link__detach()
10577 if (link->pin_path) in bpf_link__pin()
10578 return libbpf_err(-EBUSY); in bpf_link__pin()
10586 link->pin_path = strdup(path); in bpf_link__pin()
10587 if (!link->pin_path) in bpf_link__pin()
10588 return libbpf_err(-ENOMEM); in bpf_link__pin()
10590 if (bpf_obj_pin(link->fd, link->pin_path)) { in bpf_link__pin()
10591 err = -errno; in bpf_link__pin()
10592 zfree(&link->pin_path); in bpf_link__pin()
10596 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); in bpf_link__pin()
10604 if (!link->pin_path) in bpf_link__unpin()
10605 return libbpf_err(-EINVAL); in bpf_link__unpin()
10607 err = unlink(link->pin_path); in bpf_link__unpin()
10609 return -errno; in bpf_link__unpin()
10611 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); in bpf_link__unpin()
10612 zfree(&link->pin_path); in bpf_link__unpin()
10633 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) in bpf_link_perf_detach()
10634 err = -errno; in bpf_link_perf_detach()
10636 if (perf_link->perf_event_fd != link->fd) in bpf_link_perf_detach()
10637 close(perf_link->perf_event_fd); in bpf_link_perf_detach()
10638 close(link->fd); in bpf_link_perf_detach()
10641 if (perf_link->legacy_probe_name) { in bpf_link_perf_detach()
10642 if (perf_link->legacy_is_kprobe) { in bpf_link_perf_detach()
10643 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10644 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10646 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10647 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10658 free(perf_link->legacy_probe_name); in bpf_link_perf_dealloc()
10667 int prog_fd, link_fd = -1, err; in bpf_program__attach_perf_event_opts()
10671 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10675 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10676 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10681 prog->name); in bpf_program__attach_perf_event_opts()
10682 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10687 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_perf_event_opts()
10688 link->link.detach = &bpf_link_perf_detach; in bpf_program__attach_perf_event_opts()
10689 link->link.dealloc = &bpf_link_perf_dealloc; in bpf_program__attach_perf_event_opts()
10690 link->perf_event_fd = pfd; in bpf_program__attach_perf_event_opts()
10693 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { in bpf_program__attach_perf_event_opts()
10699 err = -errno; in bpf_program__attach_perf_event_opts()
10701 prog->name, pfd, in bpf_program__attach_perf_event_opts()
10705 link->link.fd = link_fd; in bpf_program__attach_perf_event_opts()
10708 pr_warn("prog '%s': user context value is not supported\n", prog->name); in bpf_program__attach_perf_event_opts()
10709 err = -EOPNOTSUPP; in bpf_program__attach_perf_event_opts()
10714 err = -errno; in bpf_program__attach_perf_event_opts()
10716 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_perf_event_opts()
10717 if (err == -EPROTO) in bpf_program__attach_perf_event_opts()
10719 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10722 link->link.fd = pfd; in bpf_program__attach_perf_event_opts()
10725 err = -errno; in bpf_program__attach_perf_event_opts()
10727 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_perf_event_opts()
10731 return &link->link; in bpf_program__attach_perf_event_opts()
10745 * this function is expected to parse integer in the range of [0, 2^31-1] from
10757 err = -errno; in parse_uint_from_file()
10764 err = err == EOF ? -EIO : -errno; in parse_uint_from_file()
10814 return -EINVAL; in perf_event_open_probe()
10846 pid < 0 ? -1 : pid /* pid */, in perf_event_open_probe()
10847 pid == -1 ? 0 : -1 /* cpu */, in perf_event_open_probe()
10848 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_probe()
10849 return pfd >= 0 ? pfd : -errno; in perf_event_open_probe()
10863 return -EINVAL; in append_to_file()
10867 return -errno; in append_to_file()
10870 err = -errno; in append_to_file()
10881 static int has_debugfs = -1; in use_debugfs()
10943 return append_to_file(tracefs_kprobe_events(), "-:%s/%s", in remove_kprobe_event_legacy()
10987 pid < 0 ? -1 : pid, /* pid */ in perf_event_kprobe_open_legacy()
10988 pid == -1 ? 0 : -1, /* cpu */ in perf_event_kprobe_open_legacy()
10989 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_kprobe_open_legacy()
10991 err = -errno; in perf_event_kprobe_open_legacy()
11077 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
11092 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
11096 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_kprobe_opts()
11097 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
11102 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
11108 -1 /* pid */, 0 /* ref_ctr_off */); in bpf_program__attach_kprobe_opts()
11117 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_kprobe_opts()
11120 offset, -1 /* pid */); in bpf_program__attach_kprobe_opts()
11123 err = -errno; in bpf_program__attach_kprobe_opts()
11125 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
11135 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
11143 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_kprobe_opts()
11144 perf_link->legacy_is_kprobe = true; in bpf_program__attach_kprobe_opts()
11145 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_kprobe_opts()
11177 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_ksyscall()
11179 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { in bpf_program__attach_ksyscall()
11246 struct kprobe_multi_resolve *res = data->res; in avail_kallsyms_cb()
11249 if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) in avail_kallsyms_cb()
11252 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); in avail_kallsyms_cb()
11256 res->addrs[res->cnt++] = (unsigned long)sym_addr; in avail_kallsyms_cb()
11272 err = -errno; in libbpf_available_kallsyms_parse()
11286 err = -EINVAL; in libbpf_available_kallsyms_parse()
11290 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kallsyms_parse()
11299 err = -errno; in libbpf_available_kallsyms_parse()
11308 err = -ENOENT; in libbpf_available_kallsyms_parse()
11320 if (res->cnt == 0) in libbpf_available_kallsyms_parse()
11321 err = -ENOENT; in libbpf_available_kallsyms_parse()
11334 return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; in has_available_filter_functions_addrs()
11347 err = -errno; in libbpf_available_kprobes_parse()
11360 err = -EINVAL; in libbpf_available_kprobes_parse()
11364 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kprobes_parse()
11367 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, in libbpf_available_kprobes_parse()
11368 sizeof(*res->addrs), res->cnt + 1); in libbpf_available_kprobes_parse()
11372 res->addrs[res->cnt++] = (unsigned long)sym_addr; in libbpf_available_kprobes_parse()
11375 if (res->cnt == 0) in libbpf_available_kprobes_parse()
11376 err = -ENOENT; in libbpf_available_kprobes_parse()
11402 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11410 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11412 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11414 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11416 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
11439 err = -ENOMEM; in bpf_program__attach_kprobe_multi_opts()
11442 link->detach = &bpf_link__detach_fd; in bpf_program__attach_kprobe_multi_opts()
11447 err = -errno; in bpf_program__attach_kprobe_multi_opts()
11449 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_kprobe_multi_opts()
11452 link->fd = link_fd; in bpf_program__attach_kprobe_multi_opts()
11472 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ in attach_kprobe()
11473 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) in attach_kprobe()
11476 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); in attach_kprobe()
11478 func_name = prog->sec_name + sizeof("kretprobe/") - 1; in attach_kprobe()
11480 func_name = prog->sec_name + sizeof("kprobe/") - 1; in attach_kprobe()
11482 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); in attach_kprobe()
11485 return -EINVAL; in attach_kprobe()
11490 return -EINVAL; in attach_kprobe()
11506 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ in attach_ksyscall()
11507 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) in attach_ksyscall()
11510 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); in attach_ksyscall()
11512 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; in attach_ksyscall()
11514 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; in attach_ksyscall()
11517 return *link ? 0 : -errno; in attach_ksyscall()
11529 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ in attach_kprobe_multi()
11530 if (strcmp(prog->sec_name, "kprobe.multi") == 0 || in attach_kprobe_multi()
11531 strcmp(prog->sec_name, "kretprobe.multi") == 0) in attach_kprobe_multi()
11534 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); in attach_kprobe_multi()
11536 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; in attach_kprobe_multi()
11538 spec = prog->sec_name + sizeof("kprobe.multi/") - 1; in attach_kprobe_multi()
11540 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); in attach_kprobe_multi()
11543 return -EINVAL; in attach_kprobe_multi()
11555 int n, ret = -EINVAL; in attach_uprobe_multi()
11559 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", in attach_uprobe_multi()
11563 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe_multi()
11568 *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); in attach_uprobe_multi()
11572 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe_multi()
11573 prog->sec_name); in attach_uprobe_multi()
11607 return append_to_file(tracefs_uprobe_events(), "-:%s/%s", in remove_uprobe_event_legacy()
11648 pid < 0 ? -1 : pid, /* pid */ in perf_event_uprobe_open_legacy()
11649 pid == -1 ? 0 : -1, /* cpu */ in perf_event_uprobe_open_legacy()
11650 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_uprobe_open_legacy()
11652 err = -errno; in perf_event_uprobe_open_legacy()
11700 ret = -LIBBPF_ERRNO__FORMAT; in elf_find_func_offset_from_archive()
11707 elf_errmsg(-1)); in elf_find_func_offset_from_archive()
11708 ret = -LIBBPF_ERRNO__LIBELF; in elf_find_func_offset_from_archive()
11735 return "/lib/x86_64-linux-gnu"; in arch_specific_lib_paths()
11737 return "/lib/i386-linux-gnu"; in arch_specific_lib_paths()
11739 return "/lib/s390x-linux-gnu"; in arch_specific_lib_paths()
11741 return "/lib/s390-linux-gnu"; in arch_specific_lib_paths()
11743 return "/lib/arm-linux-gnueabi"; in arch_specific_lib_paths()
11745 return "/lib/arm-linux-gnueabihf"; in arch_specific_lib_paths()
11747 return "/lib/aarch64-linux-gnu"; in arch_specific_lib_paths()
11749 return "/lib/mips64el-linux-gnuabi64"; in arch_specific_lib_paths()
11751 return "/lib/mipsel-linux-gnu"; in arch_specific_lib_paths()
11753 return "/lib/powerpc64le-linux-gnu"; in arch_specific_lib_paths()
11755 return "/lib/sparc64-linux-gnu"; in arch_specific_lib_paths()
11757 return "/lib/riscv64-linux-gnu"; in arch_specific_lib_paths()
11792 seg_len = next_path ? next_path - s : strlen(s); in resolve_full_path()
11803 return -ENOENT; in resolve_full_path()
11825 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11841 * - syms and offsets are mutually exclusive in bpf_program__attach_uprobe_multi()
11842 * - ref_ctr_offsets and cookies are optional in bpf_program__attach_uprobe_multi()
11848 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11850 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11854 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11857 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11865 prog->name, path, err); in bpf_program__attach_uprobe_multi()
11897 err = -ENOMEM; in bpf_program__attach_uprobe_multi()
11900 link->detach = &bpf_link__detach_fd; in bpf_program__attach_uprobe_multi()
11905 err = -errno; in bpf_program__attach_uprobe_multi()
11906 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", in bpf_program__attach_uprobe_multi()
11907 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_uprobe_multi()
11910 link->fd = link_fd; in bpf_program__attach_uprobe_multi()
11937 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11945 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11952 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); in bpf_program__attach_uprobe_opts()
11959 prog->name, binary_path, err); in bpf_program__attach_uprobe_opts()
11988 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
11992 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_uprobe_opts()
11993 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
11998 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12008 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
12015 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_uprobe_opts()
12021 err = -errno; in bpf_program__attach_uprobe_opts()
12023 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
12034 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
12042 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_uprobe_opts()
12043 perf_link->legacy_is_kprobe = false; in bpf_program__attach_uprobe_opts()
12044 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_uprobe_opts()
12056 /* Format of u[ret]probe section definition supporting auto-attach:
12063 * specified (and auto-attach is not possible) or the above format is specified for
12064 * auto-attach.
12070 int n, c, ret = -EINVAL; in attach_uprobe()
12075 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", in attach_uprobe()
12079 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe()
12084 prog->name, prog->sec_name); in attach_uprobe()
12102 prog->name); in attach_uprobe()
12106 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); in attach_uprobe()
12110 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe()
12111 prog->sec_name); in attach_uprobe()
12137 struct bpf_object *obj = prog->obj; in bpf_program__attach_usdt()
12143 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12147 prog->name); in bpf_program__attach_usdt()
12148 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12152 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
12158 prog->name, binary_path, err); in bpf_program__attach_usdt()
12167 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
12168 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
12169 if (!obj->usdt_man) { in bpf_program__attach_usdt()
12170 obj->usdt_man = usdt_manager_new(obj); in bpf_program__attach_usdt()
12171 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
12172 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
12176 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, in bpf_program__attach_usdt()
12192 /* no auto-attach for just SEC("usdt") */ in attach_usdt()
12201 err = -EINVAL; in attach_usdt()
12203 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, in attach_usdt()
12222 return -errno; in determine_tracepoint_id()
12226 return -E2BIG; in determine_tracepoint_id()
12252 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, in perf_event_open_tracepoint()
12253 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_tracepoint()
12255 err = -errno; in perf_event_open_tracepoint()
12275 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tracepoint_opts()
12282 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
12291 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
12311 /* no auto-attach for SEC("tp") or SEC("tracepoint") */ in attach_tp()
12312 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0) in attach_tp()
12315 sec_name = strdup(prog->sec_name); in attach_tp()
12317 return -ENOMEM; in attach_tp()
12320 if (str_has_pfx(prog->sec_name, "tp/")) in attach_tp()
12321 tp_cat = sec_name + sizeof("tp/") - 1; in attach_tp()
12323 tp_cat = sec_name + sizeof("tracepoint/") - 1; in attach_tp()
12327 return -EINVAL; in attach_tp()
12346 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_raw_tracepoint()
12347 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_raw_tracepoint()
12352 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_raw_tracepoint()
12353 link->detach = &bpf_link__detach_fd; in bpf_program__attach_raw_tracepoint()
12357 pfd = -errno; in bpf_program__attach_raw_tracepoint()
12360 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); in bpf_program__attach_raw_tracepoint()
12363 link->fd = pfd; in bpf_program__attach_raw_tracepoint()
12383 if (!str_has_pfx(prog->sec_name, prefixes[i])) in attach_raw_tp()
12387 /* no auto-attach case of, e.g., SEC("raw_tp") */ in attach_raw_tp()
12388 if (prog->sec_name[pfx_len] == '\0') in attach_raw_tp()
12391 if (prog->sec_name[pfx_len] != '/') in attach_raw_tp()
12394 tp_name = prog->sec_name + pfx_len + 1; in attach_raw_tp()
12400 prog->name, prog->sec_name); in attach_raw_tp()
12401 return -EINVAL; in attach_raw_tp()
12418 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
12422 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_btf_id()
12423 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
12428 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_btf_id()
12429 link->detach = &bpf_link__detach_fd; in bpf_program__attach_btf_id()
12435 pfd = -errno; in bpf_program__attach_btf_id()
12438 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); in bpf_program__attach_btf_id()
12441 link->fd = pfd; in bpf_program__attach_btf_id()
12485 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program_attach_fd()
12486 return libbpf_err_ptr(-EINVAL); in bpf_program_attach_fd()
12491 return libbpf_err_ptr(-ENOMEM); in bpf_program_attach_fd()
12492 link->detach = &bpf_link__detach_fd; in bpf_program_attach_fd()
12497 link_fd = -errno; in bpf_program_attach_fd()
12500 prog->name, target_name, in bpf_program_attach_fd()
12504 link->fd = link_fd; in bpf_program_attach_fd()
12535 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12540 /* validate we don't have unexpected combinations of non-zero fields */ in bpf_program__attach_tcx()
12543 prog->name); in bpf_program__attach_tcx()
12544 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12548 prog->name); in bpf_program__attach_tcx()
12549 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
12570 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12575 /* validate we don't have unexpected combinations of non-zero fields */ in bpf_program__attach_netkit()
12578 prog->name); in bpf_program__attach_netkit()
12579 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12583 prog->name); in bpf_program__attach_netkit()
12584 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netkit()
12603 prog->name); in bpf_program__attach_freplace()
12604 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12607 if (prog->type != BPF_PROG_TYPE_EXT) { in bpf_program__attach_freplace()
12609 prog->name); in bpf_program__attach_freplace()
12610 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12643 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12650 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_iter()
12651 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12656 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_iter()
12657 link->detach = &bpf_link__detach_fd; in bpf_program__attach_iter()
12662 link_fd = -errno; in bpf_program__attach_iter()
12665 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); in bpf_program__attach_iter()
12668 link->fd = link_fd; in bpf_program__attach_iter()
12686 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12690 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_netfilter()
12691 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12696 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_netfilter()
12698 link->detach = &bpf_link__detach_fd; in bpf_program__attach_netfilter()
12709 link_fd = -errno; in bpf_program__attach_netfilter()
12712 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); in bpf_program__attach_netfilter()
12715 link->fd = link_fd; in bpf_program__attach_netfilter()
12725 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_program__attach()
12726 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
12728 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); in bpf_program__attach()
12732 /* When calling bpf_program__attach() explicitly, auto-attach support in bpf_program__attach()
12738 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
12755 if (st_link->map_fd < 0) in bpf_link__detach_struct_ops()
12757 return bpf_map_delete_elem(link->fd, &zero); in bpf_link__detach_struct_ops()
12759 return close(link->fd); in bpf_link__detach_struct_ops()
12768 if (!bpf_map__is_struct_ops(map) || map->fd == -1) in bpf_map__attach_struct_ops()
12769 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
12773 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
12776 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_map__attach_struct_ops()
12782 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { in bpf_map__attach_struct_ops()
12787 link->link.detach = bpf_link__detach_struct_ops; in bpf_map__attach_struct_ops()
12789 if (!(map->def.map_flags & BPF_F_LINK)) { in bpf_map__attach_struct_ops()
12791 link->link.fd = map->fd; in bpf_map__attach_struct_ops()
12792 link->map_fd = -1; in bpf_map__attach_struct_ops()
12793 return &link->link; in bpf_map__attach_struct_ops()
12796 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); in bpf_map__attach_struct_ops()
12802 link->link.fd = fd; in bpf_map__attach_struct_ops()
12803 link->map_fd = map->fd; in bpf_map__attach_struct_ops()
12805 return &link->link; in bpf_map__attach_struct_ops()
12818 return -EINVAL; in bpf_link__update_map()
12822 if (st_ops_link->map_fd < 0) in bpf_link__update_map()
12823 return -EINVAL; in bpf_link__update_map()
12825 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_link__update_map()
12831 if (err && err != -EBUSY) in bpf_link__update_map()
12834 err = bpf_link_update(link->fd, map->fd, NULL); in bpf_link__update_map()
12838 st_ops_link->map_fd = map->fd; in bpf_link__update_map()
12853 __u64 data_tail = header->data_tail; in perf_event_read_simple()
12860 ehdr = base + (data_tail & (mmap_size - 1)); in perf_event_read_simple()
12861 ehdr_size = ehdr->size; in perf_event_read_simple()
12865 size_t len_first = base + mmap_size - copy_start; in perf_event_read_simple()
12866 size_t len_secnd = ehdr_size - len_first; in perf_event_read_simple()
12900 /* sample_cb and lost_cb are higher-level common-case callbacks */
12939 if (cpu_buf->base && in perf_buffer__free_cpu_buf()
12940 munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) in perf_buffer__free_cpu_buf()
12941 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); in perf_buffer__free_cpu_buf()
12942 if (cpu_buf->fd >= 0) { in perf_buffer__free_cpu_buf()
12943 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); in perf_buffer__free_cpu_buf()
12944 close(cpu_buf->fd); in perf_buffer__free_cpu_buf()
12946 free(cpu_buf->buf); in perf_buffer__free_cpu_buf()
12956 if (pb->cpu_bufs) { in perf_buffer__free()
12957 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__free()
12958 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__free()
12963 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); in perf_buffer__free()
12966 free(pb->cpu_bufs); in perf_buffer__free()
12968 if (pb->epoll_fd >= 0) in perf_buffer__free()
12969 close(pb->epoll_fd); in perf_buffer__free()
12970 free(pb->events); in perf_buffer__free()
12984 return ERR_PTR(-ENOMEM); in perf_buffer__open_cpu_buf()
12986 cpu_buf->pb = pb; in perf_buffer__open_cpu_buf()
12987 cpu_buf->cpu = cpu; in perf_buffer__open_cpu_buf()
12988 cpu_buf->map_key = map_key; in perf_buffer__open_cpu_buf()
12990 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, in perf_buffer__open_cpu_buf()
12991 -1, PERF_FLAG_FD_CLOEXEC); in perf_buffer__open_cpu_buf()
12992 if (cpu_buf->fd < 0) { in perf_buffer__open_cpu_buf()
12993 err = -errno; in perf_buffer__open_cpu_buf()
12999 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, in perf_buffer__open_cpu_buf()
13001 cpu_buf->fd, 0); in perf_buffer__open_cpu_buf()
13002 if (cpu_buf->base == MAP_FAILED) { in perf_buffer__open_cpu_buf()
13003 cpu_buf->base = NULL; in perf_buffer__open_cpu_buf()
13004 err = -errno; in perf_buffer__open_cpu_buf()
13010 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { in perf_buffer__open_cpu_buf()
13011 err = -errno; in perf_buffer__open_cpu_buf()
13039 return libbpf_err_ptr(-EINVAL); in perf_buffer__new()
13069 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
13072 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
13095 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { in __perf_buffer__new()
13098 return ERR_PTR(-EINVAL); in __perf_buffer__new()
13101 /* best-effort sanity checks */ in __perf_buffer__new()
13106 err = -errno; in __perf_buffer__new()
13108 * -EBADFD, -EFAULT, or -E2BIG on real error in __perf_buffer__new()
13110 if (err != -EINVAL) { in __perf_buffer__new()
13121 return ERR_PTR(-EINVAL); in __perf_buffer__new()
13127 return ERR_PTR(-ENOMEM); in __perf_buffer__new()
13129 pb->event_cb = p->event_cb; in __perf_buffer__new()
13130 pb->sample_cb = p->sample_cb; in __perf_buffer__new()
13131 pb->lost_cb = p->lost_cb; in __perf_buffer__new()
13132 pb->ctx = p->ctx; in __perf_buffer__new()
13134 pb->page_size = getpagesize(); in __perf_buffer__new()
13135 pb->mmap_size = pb->page_size * page_cnt; in __perf_buffer__new()
13136 pb->map_fd = map_fd; in __perf_buffer__new()
13138 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); in __perf_buffer__new()
13139 if (pb->epoll_fd < 0) { in __perf_buffer__new()
13140 err = -errno; in __perf_buffer__new()
13146 if (p->cpu_cnt > 0) { in __perf_buffer__new()
13147 pb->cpu_cnt = p->cpu_cnt; in __perf_buffer__new()
13149 pb->cpu_cnt = libbpf_num_possible_cpus(); in __perf_buffer__new()
13150 if (pb->cpu_cnt < 0) { in __perf_buffer__new()
13151 err = pb->cpu_cnt; in __perf_buffer__new()
13154 if (map.max_entries && map.max_entries < pb->cpu_cnt) in __perf_buffer__new()
13155 pb->cpu_cnt = map.max_entries; in __perf_buffer__new()
13158 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); in __perf_buffer__new()
13159 if (!pb->events) { in __perf_buffer__new()
13160 err = -ENOMEM; in __perf_buffer__new()
13164 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); in __perf_buffer__new()
13165 if (!pb->cpu_bufs) { in __perf_buffer__new()
13166 err = -ENOMEM; in __perf_buffer__new()
13177 for (i = 0, j = 0; i < pb->cpu_cnt; i++) { in __perf_buffer__new()
13181 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; in __perf_buffer__new()
13182 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; in __perf_buffer__new()
13187 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) in __perf_buffer__new()
13190 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); in __perf_buffer__new()
13196 pb->cpu_bufs[j] = cpu_buf; in __perf_buffer__new()
13198 err = bpf_map_update_elem(pb->map_fd, &map_key, in __perf_buffer__new()
13199 &cpu_buf->fd, 0); in __perf_buffer__new()
13201 err = -errno; in __perf_buffer__new()
13202 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", in __perf_buffer__new()
13203 cpu, map_key, cpu_buf->fd, in __perf_buffer__new()
13208 pb->events[j].events = EPOLLIN; in __perf_buffer__new()
13209 pb->events[j].data.ptr = cpu_buf; in __perf_buffer__new()
13210 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, in __perf_buffer__new()
13211 &pb->events[j]) < 0) { in __perf_buffer__new()
13212 err = -errno; in __perf_buffer__new()
13214 cpu, cpu_buf->fd, in __perf_buffer__new()
13220 pb->cpu_cnt = j; in __perf_buffer__new()
13249 struct perf_buffer *pb = cpu_buf->pb; in perf_buffer__process_record()
13253 if (pb->event_cb) in perf_buffer__process_record()
13254 return pb->event_cb(pb->ctx, cpu_buf->cpu, e); in perf_buffer__process_record()
13256 switch (e->type) { in perf_buffer__process_record()
13260 if (pb->sample_cb) in perf_buffer__process_record()
13261 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); in perf_buffer__process_record()
13267 if (pb->lost_cb) in perf_buffer__process_record()
13268 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); in perf_buffer__process_record()
13272 pr_warn("unknown perf sample type %d\n", e->type); in perf_buffer__process_record()
13283 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, in perf_buffer__process_records()
13284 pb->page_size, &cpu_buf->buf, in perf_buffer__process_records()
13285 &cpu_buf->buf_size, in perf_buffer__process_records()
13294 return pb->epoll_fd; in perf_buffer__epoll_fd()
13301 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); in perf_buffer__poll()
13303 return -errno; in perf_buffer__poll()
13306 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; in perf_buffer__poll()
13322 return pb->cpu_cnt; in perf_buffer__buffer_cnt()
13334 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer_fd()
13335 return libbpf_err(-EINVAL); in perf_buffer__buffer_fd()
13337 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer_fd()
13339 return libbpf_err(-ENOENT); in perf_buffer__buffer_fd()
13341 return cpu_buf->fd; in perf_buffer__buffer_fd()
13348 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer()
13349 return libbpf_err(-EINVAL); in perf_buffer__buffer()
13351 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer()
13353 return libbpf_err(-ENOENT); in perf_buffer__buffer()
13355 *buf = cpu_buf->base; in perf_buffer__buffer()
13356 *buf_size = pb->mmap_size; in perf_buffer__buffer()
13365 * - 0 on success;
13366 * - <0 on failure.
13372 if (buf_idx >= pb->cpu_cnt) in perf_buffer__consume_buffer()
13373 return libbpf_err(-EINVAL); in perf_buffer__consume_buffer()
13375 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__consume_buffer()
13377 return libbpf_err(-ENOENT); in perf_buffer__consume_buffer()
13386 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__consume()
13387 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__consume()
13408 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13410 if (prog->obj->loaded) in bpf_program__set_attach_target()
13411 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13417 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
13428 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
13431 err = bpf_object__load_vmlinux_btf(prog->obj, true); in bpf_program__set_attach_target()
13434 err = find_kernel_btf_id(prog->obj, attach_func_name, in bpf_program__set_attach_target()
13435 prog->expected_attach_type, in bpf_program__set_attach_target()
13441 prog->attach_btf_id = btf_id; in bpf_program__set_attach_target()
13442 prog->attach_btf_obj_fd = btf_obj_fd; in bpf_program__set_attach_target()
13443 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
13449 int err = 0, n, len, start, end = -1; in parse_cpu_mask_str()
13455 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ in parse_cpu_mask_str()
13461 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); in parse_cpu_mask_str()
13464 err = -EINVAL; in parse_cpu_mask_str()
13472 err = -EINVAL; in parse_cpu_mask_str()
13477 err = -ENOMEM; in parse_cpu_mask_str()
13481 memset(tmp + *mask_sz, 0, start - *mask_sz); in parse_cpu_mask_str()
13482 memset(tmp + start, 1, end - start + 1); in parse_cpu_mask_str()
13488 return -EINVAL; in parse_cpu_mask_str()
13504 err = -errno; in parse_cpu_mask_file()
13511 err = len ? -errno : -EINVAL; in parse_cpu_mask_file()
13517 return -E2BIG; in parse_cpu_mask_file()
13564 return -ESRCH; in populate_skeleton_maps()
13567 /* externs shouldn't be pre-setup from user code */ in populate_skeleton_maps()
13568 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) in populate_skeleton_maps()
13569 *mmaped = (*map)->mmaped; in populate_skeleton_maps()
13587 return -ESRCH; in populate_skeleton_progs()
13597 .object_name = s->name, in bpf_object__open_skeleton()
13602 /* Attempt to preserve opts->object_name, unless overriden by user in bpf_object__open_skeleton()
13610 if (!opts->object_name) in bpf_object__open_skeleton()
13611 skel_opts.object_name = s->name; in bpf_object__open_skeleton()
13614 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); in bpf_object__open_skeleton()
13618 s->name, err); in bpf_object__open_skeleton()
13622 *s->obj = obj; in bpf_object__open_skeleton()
13623 err = populate_skeleton_maps(obj, s->maps, s->map_cnt); in bpf_object__open_skeleton()
13625 pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); in bpf_object__open_skeleton()
13629 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); in bpf_object__open_skeleton()
13631 pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); in bpf_object__open_skeleton()
13649 if (!s->obj) in bpf_object__open_subskeleton()
13650 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13652 btf = bpf_object__btf(s->obj); in bpf_object__open_subskeleton()
13655 bpf_object__name(s->obj)); in bpf_object__open_subskeleton()
13656 return libbpf_err(-errno); in bpf_object__open_subskeleton()
13659 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); in bpf_object__open_subskeleton()
13665 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); in bpf_object__open_subskeleton()
13671 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { in bpf_object__open_subskeleton()
13672 var_skel = &s->vars[var_idx]; in bpf_object__open_subskeleton()
13673 map = *var_skel->map; in bpf_object__open_subskeleton()
13681 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13687 var_type = btf__type_by_id(btf, var->type); in bpf_object__open_subskeleton()
13688 var_name = btf__name_by_offset(btf, var_type->name_off); in bpf_object__open_subskeleton()
13689 if (strcmp(var_name, var_skel->name) == 0) { in bpf_object__open_subskeleton()
13690 *var_skel->addr = map->mmaped + var->offset; in bpf_object__open_subskeleton()
13702 free(s->maps); in bpf_object__destroy_subskeleton()
13703 free(s->progs); in bpf_object__destroy_subskeleton()
13704 free(s->vars); in bpf_object__destroy_subskeleton()
13712 err = bpf_object__load(*s->obj); in bpf_object__load_skeleton()
13714 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err); in bpf_object__load_skeleton()
13718 for (i = 0; i < s->map_cnt; i++) { in bpf_object__load_skeleton()
13719 struct bpf_map *map = *s->maps[i].map; in bpf_object__load_skeleton()
13720 size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_object__load_skeleton()
13721 int prot, map_fd = map->fd; in bpf_object__load_skeleton()
13722 void **mmaped = s->maps[i].mmaped; in bpf_object__load_skeleton()
13727 if (!(map->def.map_flags & BPF_F_MMAPABLE)) { in bpf_object__load_skeleton()
13732 if (map->def.map_flags & BPF_F_RDONLY_PROG) in bpf_object__load_skeleton()
13737 /* Remap anonymous mmap()-ed "map initialization image" as in bpf_object__load_skeleton()
13738 * a BPF map-backed mmap()-ed memory, but preserving the same in bpf_object__load_skeleton()
13747 *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0); in bpf_object__load_skeleton()
13749 err = -errno; in bpf_object__load_skeleton()
13751 pr_warn("failed to re-mmap() map '%s': %d\n", in bpf_object__load_skeleton()
13764 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__attach_skeleton()
13765 struct bpf_program *prog = *s->progs[i].prog; in bpf_object__attach_skeleton()
13766 struct bpf_link **link = s->progs[i].link; in bpf_object__attach_skeleton()
13768 if (!prog->autoload || !prog->autoattach) in bpf_object__attach_skeleton()
13771 /* auto-attaching not supported for this program */ in bpf_object__attach_skeleton()
13772 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_object__attach_skeleton()
13775 /* if user already set the link manually, don't attempt auto-attach */ in bpf_object__attach_skeleton()
13779 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); in bpf_object__attach_skeleton()
13781 pr_warn("prog '%s': failed to auto-attach: %d\n", in bpf_object__attach_skeleton()
13786 /* It's possible that for some SEC() definitions auto-attach in bpf_object__attach_skeleton()
13791 * auto-attached. But if not, it shouldn't trigger skeleton's in bpf_object__attach_skeleton()
13805 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__detach_skeleton()
13806 struct bpf_link **link = s->progs[i].link; in bpf_object__detach_skeleton()
13818 if (s->progs) in bpf_object__destroy_skeleton()
13820 if (s->obj) in bpf_object__destroy_skeleton()
13821 bpf_object__close(*s->obj); in bpf_object__destroy_skeleton()
13822 free(s->maps); in bpf_object__destroy_skeleton()
13823 free(s->progs); in bpf_object__destroy_skeleton()