Lines Matching +full:riscv +full:- +full:v +full:- +full:spec

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"
259 if (err != -EPERM || geteuid() != 0) in pr_perm_msg()
276 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", in pr_perm_msg()
292 fd = -1; \
303 /* as of v1.0 libbpf_set_strict_mode() is a no-op */ in libbpf_set_strict_mode()
321 return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION); in libbpf_version_string()
349 /* stored as sec_def->cookie for all libbpf-supported SEC()s */
368 /* BPF program support non-linear XDP buffer */
406 * program. For the entry-point (main) BPF program, this is always
407 * zero. For a sub-program, this gets reset before each of main BPF
409 * whether sub-program was already appended to the main program, and
421 * entry-point BPF programs this includes the size of main program
422 * itself plus all the used sub-programs, appended at the end
471 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
577 /* BTF fd index to be patched in for insn->off, this is
578 * 0 for vmlinux BTF, index in obj->fd_array for module
663 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
667 /* vmlinux BTF override for CO-RE relocations */
704 zclose(prog->fd); in bpf_program__unload()
706 zfree(&prog->func_info); in bpf_program__unload()
707 zfree(&prog->line_info); in bpf_program__unload()
716 zfree(&prog->name); in bpf_program__exit()
717 zfree(&prog->sec_name); in bpf_program__exit()
718 zfree(&prog->insns); in bpf_program__exit()
719 zfree(&prog->reloc_desc); in bpf_program__exit()
721 prog->nr_reloc = 0; in bpf_program__exit()
722 prog->insns_cnt = 0; in bpf_program__exit()
723 prog->sec_idx = -1; in bpf_program__exit()
728 return BPF_CLASS(insn->code) == BPF_JMP && in insn_is_subprog_call()
729 BPF_OP(insn->code) == BPF_CALL && in insn_is_subprog_call()
730 BPF_SRC(insn->code) == BPF_K && in insn_is_subprog_call()
731 insn->src_reg == BPF_PSEUDO_CALL && in insn_is_subprog_call()
732 insn->dst_reg == 0 && in insn_is_subprog_call()
733 insn->off == 0; in insn_is_subprog_call()
738 return insn->code == (BPF_JMP | BPF_CALL); in is_call_insn()
743 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; in insn_is_pseudo_func()
754 return -EINVAL; in bpf_object__init_prog()
758 prog->obj = obj; in bpf_object__init_prog()
760 prog->sec_idx = sec_idx; in bpf_object__init_prog()
761 prog->sec_insn_off = sec_off / BPF_INSN_SZ; in bpf_object__init_prog()
762 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; in bpf_object__init_prog()
764 prog->insns_cnt = prog->sec_insn_cnt; in bpf_object__init_prog()
766 prog->type = BPF_PROG_TYPE_UNSPEC; in bpf_object__init_prog()
767 prog->fd = -1; in bpf_object__init_prog()
774 prog->autoload = false; in bpf_object__init_prog()
778 prog->autoload = true; in bpf_object__init_prog()
781 prog->autoattach = true; in bpf_object__init_prog()
784 prog->log_level = obj->log_level; in bpf_object__init_prog()
786 prog->sec_name = strdup(sec_name); in bpf_object__init_prog()
787 if (!prog->sec_name) in bpf_object__init_prog()
790 prog->name = strdup(name); in bpf_object__init_prog()
791 if (!prog->name) in bpf_object__init_prog()
794 prog->insns = malloc(insn_data_sz); in bpf_object__init_prog()
795 if (!prog->insns) in bpf_object__init_prog()
797 memcpy(prog->insns, insn_data, insn_data_sz); in bpf_object__init_prog()
803 return -ENOMEM; in bpf_object__init_prog()
810 Elf_Data *symbols = obj->efile.symbols; in bpf_object__add_programs()
812 void *data = sec_data->d_buf; in bpf_object__add_programs()
813 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; in bpf_object__add_programs()
818 progs = obj->programs; in bpf_object__add_programs()
819 nr_progs = obj->nr_programs; in bpf_object__add_programs()
820 nr_syms = symbols->d_size / sizeof(Elf64_Sym); in bpf_object__add_programs()
825 if (sym->st_shndx != sec_idx) in bpf_object__add_programs()
827 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) in bpf_object__add_programs()
830 prog_sz = sym->st_size; in bpf_object__add_programs()
831 sec_off = sym->st_value; in bpf_object__add_programs()
833 name = elf_sym_str(obj, sym->st_name); in bpf_object__add_programs()
837 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
843 return -LIBBPF_ERRNO__FORMAT; in bpf_object__add_programs()
846 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { in bpf_object__add_programs()
848 return -ENOTSUP; in bpf_object__add_programs()
857 * In this case the original obj->programs in bpf_object__add_programs()
863 return -ENOMEM; in bpf_object__add_programs()
865 obj->programs = progs; in bpf_object__add_programs()
879 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL in bpf_object__add_programs()
880 && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in bpf_object__add_programs()
881 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) in bpf_object__add_programs()
882 prog->mark_btf_static = true; in bpf_object__add_programs()
885 obj->nr_programs = nr_progs; in bpf_object__add_programs()
913 if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) in find_member_by_name()
965 if (kern_data_member->type == kern_type_id) in find_struct_ops_kern_types()
971 return -EINVAL; in find_struct_ops_kern_types()
985 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; in bpf_map__is_struct_ops()
1001 st_ops = map->st_ops; in bpf_map__init_kern_struct_ops()
1002 type = st_ops->type; in bpf_map__init_kern_struct_ops()
1003 tname = st_ops->tname; in bpf_map__init_kern_struct_ops()
1012 map->name, st_ops->type_id, kern_type_id, kern_vtype_id); in bpf_map__init_kern_struct_ops()
1014 map->def.value_size = kern_vtype->size; in bpf_map__init_kern_struct_ops()
1015 map->btf_vmlinux_value_type_id = kern_vtype_id; in bpf_map__init_kern_struct_ops()
1017 st_ops->kern_vdata = calloc(1, kern_vtype->size); in bpf_map__init_kern_struct_ops()
1018 if (!st_ops->kern_vdata) in bpf_map__init_kern_struct_ops()
1019 return -ENOMEM; in bpf_map__init_kern_struct_ops()
1021 data = st_ops->data; in bpf_map__init_kern_struct_ops()
1022 kern_data_off = kern_data_member->offset / 8; in bpf_map__init_kern_struct_ops()
1023 kern_data = st_ops->kern_vdata + kern_data_off; in bpf_map__init_kern_struct_ops()
1035 mname = btf__name_by_offset(btf, member->name_off); in bpf_map__init_kern_struct_ops()
1039 map->name, mname); in bpf_map__init_kern_struct_ops()
1040 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1043 kern_member_idx = kern_member - btf_members(kern_type); in bpf_map__init_kern_struct_ops()
1047 map->name, mname); in bpf_map__init_kern_struct_ops()
1048 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1051 moff = member->offset / 8; in bpf_map__init_kern_struct_ops()
1052 kern_moff = kern_member->offset / 8; in bpf_map__init_kern_struct_ops()
1057 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); in bpf_map__init_kern_struct_ops()
1058 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, in bpf_map__init_kern_struct_ops()
1060 if (BTF_INFO_KIND(mtype->info) != in bpf_map__init_kern_struct_ops()
1061 BTF_INFO_KIND(kern_mtype->info)) { in bpf_map__init_kern_struct_ops()
1063 map->name, mname, BTF_INFO_KIND(mtype->info), in bpf_map__init_kern_struct_ops()
1064 BTF_INFO_KIND(kern_mtype->info)); in bpf_map__init_kern_struct_ops()
1065 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1071 prog = st_ops->progs[i]; in bpf_map__init_kern_struct_ops()
1076 kern_mtype->type, in bpf_map__init_kern_struct_ops()
1079 /* mtype->type must be a func_proto which was in bpf_map__init_kern_struct_ops()
1085 map->name, mname); in bpf_map__init_kern_struct_ops()
1086 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1089 prog->attach_btf_id = kern_type_id; in bpf_map__init_kern_struct_ops()
1090 prog->expected_attach_type = kern_member_idx; in bpf_map__init_kern_struct_ops()
1092 st_ops->kern_func_off[i] = kern_data_off + kern_moff; in bpf_map__init_kern_struct_ops()
1095 map->name, mname, prog->name, moff, in bpf_map__init_kern_struct_ops()
1105 map->name, mname, (ssize_t)msize, in bpf_map__init_kern_struct_ops()
1107 return -ENOTSUP; in bpf_map__init_kern_struct_ops()
1111 map->name, mname, (unsigned int)msize, in bpf_map__init_kern_struct_ops()
1125 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__init_kern_struct_ops_maps()
1126 map = &obj->maps[i]; in bpf_object__init_kern_struct_ops_maps()
1131 err = bpf_map__init_kern_struct_ops(map, obj->btf, in bpf_object__init_kern_struct_ops_maps()
1132 obj->btf_vmlinux); in bpf_object__init_kern_struct_ops_maps()
1152 if (shndx == -1) in init_struct_ops_maps()
1155 btf = obj->btf; in init_struct_ops_maps()
1161 return -EINVAL; in init_struct_ops_maps()
1167 type = btf__type_by_id(obj->btf, vsi->type); in init_struct_ops_maps()
1168 var_name = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1170 type_id = btf__resolve_type(obj->btf, vsi->type); in init_struct_ops_maps()
1173 vsi->type, sec_name); in init_struct_ops_maps()
1174 return -EINVAL; in init_struct_ops_maps()
1177 type = btf__type_by_id(obj->btf, type_id); in init_struct_ops_maps()
1178 tname = btf__name_by_offset(obj->btf, type->name_off); in init_struct_ops_maps()
1181 return -ENOTSUP; in init_struct_ops_maps()
1185 return -EINVAL; in init_struct_ops_maps()
1192 map->sec_idx = shndx; in init_struct_ops_maps()
1193 map->sec_offset = vsi->offset; in init_struct_ops_maps()
1194 map->name = strdup(var_name); in init_struct_ops_maps()
1195 if (!map->name) in init_struct_ops_maps()
1196 return -ENOMEM; in init_struct_ops_maps()
1198 map->def.type = BPF_MAP_TYPE_STRUCT_OPS; in init_struct_ops_maps()
1199 map->def.key_size = sizeof(int); in init_struct_ops_maps()
1200 map->def.value_size = type->size; in init_struct_ops_maps()
1201 map->def.max_entries = 1; in init_struct_ops_maps()
1202 map->def.map_flags = map_flags; in init_struct_ops_maps()
1204 map->st_ops = calloc(1, sizeof(*map->st_ops)); in init_struct_ops_maps()
1205 if (!map->st_ops) in init_struct_ops_maps()
1206 return -ENOMEM; in init_struct_ops_maps()
1207 st_ops = map->st_ops; in init_struct_ops_maps()
1208 st_ops->data = malloc(type->size); in init_struct_ops_maps()
1209 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); in init_struct_ops_maps()
1210 st_ops->kern_func_off = malloc(btf_vlen(type) * in init_struct_ops_maps()
1211 sizeof(*st_ops->kern_func_off)); in init_struct_ops_maps()
1212 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) in init_struct_ops_maps()
1213 return -ENOMEM; in init_struct_ops_maps()
1215 if (vsi->offset + type->size > data->d_size) { in init_struct_ops_maps()
1218 return -EINVAL; in init_struct_ops_maps()
1221 memcpy(st_ops->data, in init_struct_ops_maps()
1222 data->d_buf + vsi->offset, in init_struct_ops_maps()
1223 type->size); in init_struct_ops_maps()
1224 st_ops->tname = tname; in init_struct_ops_maps()
1225 st_ops->type = type; in init_struct_ops_maps()
1226 st_ops->type_id = type_id; in init_struct_ops_maps()
1229 tname, type_id, var_name, vsi->offset); in init_struct_ops_maps()
1239 err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx, in bpf_object_init_struct_ops()
1240 obj->efile.st_ops_data, 0); in bpf_object_init_struct_ops()
1242 obj->efile.st_ops_link_shndx, in bpf_object_init_struct_ops()
1243 obj->efile.st_ops_link_data, in bpf_object_init_struct_ops()
1259 return ERR_PTR(-ENOMEM); in bpf_object__new()
1262 strcpy(obj->path, path); in bpf_object__new()
1264 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); in bpf_object__new()
1267 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); in bpf_object__new()
1268 end = strchr(obj->name, '.'); in bpf_object__new()
1273 obj->efile.fd = -1; in bpf_object__new()
1280 obj->efile.obj_buf = obj_buf; in bpf_object__new()
1281 obj->efile.obj_buf_sz = obj_buf_sz; in bpf_object__new()
1282 obj->efile.btf_maps_shndx = -1; in bpf_object__new()
1283 obj->efile.st_ops_shndx = -1; in bpf_object__new()
1284 obj->efile.st_ops_link_shndx = -1; in bpf_object__new()
1285 obj->kconfig_map_idx = -1; in bpf_object__new()
1287 obj->kern_version = get_kernel_version(); in bpf_object__new()
1288 obj->loaded = false; in bpf_object__new()
1295 if (!obj->efile.elf) in bpf_object__elf_finish()
1298 elf_end(obj->efile.elf); in bpf_object__elf_finish()
1299 obj->efile.elf = NULL; in bpf_object__elf_finish()
1300 obj->efile.symbols = NULL; in bpf_object__elf_finish()
1301 obj->efile.st_ops_data = NULL; in bpf_object__elf_finish()
1302 obj->efile.st_ops_link_data = NULL; in bpf_object__elf_finish()
1304 zfree(&obj->efile.secs); in bpf_object__elf_finish()
1305 obj->efile.sec_cnt = 0; in bpf_object__elf_finish()
1306 zclose(obj->efile.fd); in bpf_object__elf_finish()
1307 obj->efile.obj_buf = NULL; in bpf_object__elf_finish()
1308 obj->efile.obj_buf_sz = 0; in bpf_object__elf_finish()
1317 if (obj->efile.elf) { in bpf_object__elf_init()
1319 return -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1322 if (obj->efile.obj_buf_sz > 0) { in bpf_object__elf_init()
1324 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); in bpf_object__elf_init()
1326 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); in bpf_object__elf_init()
1327 if (obj->efile.fd < 0) { in bpf_object__elf_init()
1330 err = -errno; in bpf_object__elf_init()
1332 pr_warn("elf: failed to open %s: %s\n", obj->path, cp); in bpf_object__elf_init()
1336 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); in bpf_object__elf_init()
1340 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1341 err = -LIBBPF_ERRNO__LIBELF; in bpf_object__elf_init()
1345 obj->efile.elf = elf; in bpf_object__elf_init()
1348 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1349 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); in bpf_object__elf_init()
1354 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1355 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); in bpf_object__elf_init()
1359 obj->efile.ehdr = ehdr = elf64_getehdr(elf); in bpf_object__elf_init()
1360 if (!obj->efile.ehdr) { in bpf_object__elf_init()
1361 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1362 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1366 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { in bpf_object__elf_init()
1368 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1369 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1374 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { in bpf_object__elf_init()
1376 obj->path, elf_errmsg(-1)); in bpf_object__elf_init()
1377 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1382 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { in bpf_object__elf_init()
1383 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); in bpf_object__elf_init()
1384 err = -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_init()
1397 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) in bpf_object__check_endianness()
1400 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) in bpf_object__check_endianness()
1405 pr_warn("elf: endianness mismatch in %s.\n", obj->path); in bpf_object__check_endianness()
1406 return -LIBBPF_ERRNO__ENDIAN; in bpf_object__check_endianness()
1413 pr_warn("invalid license section in %s\n", obj->path); in bpf_object__init_license()
1414 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_license()
1416 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't in bpf_object__init_license()
1419 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); in bpf_object__init_license()
1420 pr_debug("license of %s is %s\n", obj->path, obj->license); in bpf_object__init_license()
1430 pr_warn("invalid kver section in %s\n", obj->path); in bpf_object__init_kversion()
1431 return -LIBBPF_ERRNO__FORMAT; in bpf_object__init_kversion()
1434 obj->kern_version = kver; in bpf_object__init_kversion()
1435 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); in bpf_object__init_kversion()
1453 return -EINVAL; in find_elf_sec_sz()
1458 *size = data->d_size; in find_elf_sec_sz()
1462 return -ENOENT; in find_elf_sec_sz()
1467 Elf_Data *symbols = obj->efile.symbols; in find_elf_var_sym()
1471 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { in find_elf_var_sym()
1474 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) in find_elf_var_sym()
1477 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && in find_elf_var_sym()
1478 ELF64_ST_BIND(sym->st_info) != STB_WEAK) in find_elf_var_sym()
1481 sname = elf_sym_str(obj, sym->st_name); in find_elf_var_sym()
1484 return ERR_PTR(-EIO); in find_elf_var_sym()
1490 return ERR_PTR(-ENOENT); in find_elf_var_sym()
1498 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, in bpf_object__add_map()
1499 sizeof(*obj->maps), obj->nr_maps + 1); in bpf_object__add_map()
1503 map = &obj->maps[obj->nr_maps++]; in bpf_object__add_map()
1504 map->obj = obj; in bpf_object__add_map()
1505 map->fd = -1; in bpf_object__add_map()
1506 map->inner_map_fd = -1; in bpf_object__add_map()
1507 map->autocreate = true; in bpf_object__add_map()
1526 if (!map->mmaped) in bpf_map_mmap_resize()
1527 return -EINVAL; in bpf_map_mmap_resize()
1532 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_map_mmap_resize()
1534 return -errno; in bpf_map_mmap_resize()
1536 memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); in bpf_map_mmap_resize()
1537 munmap(map->mmaped, old_sz); in bpf_map_mmap_resize()
1538 map->mmaped = mmaped; in bpf_map_mmap_resize()
1572 * '.rodata.abracad' kernel and user-visible name. in internal_map_name()
1581 sfx_len = BPF_OBJ_NAME_LEN - 1; in internal_map_name()
1587 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); in internal_map_name()
1589 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, in internal_map_name()
1614 if (!map->btf_value_type_id) in map_is_mmapable()
1617 t = btf__type_by_id(obj->btf, map->btf_value_type_id); in map_is_mmapable()
1623 vt = btf__type_by_id(obj->btf, vsi->type); in map_is_mmapable()
1627 if (btf_var(vt)->linkage != BTF_VAR_STATIC) in map_is_mmapable()
1647 map->libbpf_type = type; in bpf_object__init_internal_map()
1648 map->sec_idx = sec_idx; in bpf_object__init_internal_map()
1649 map->sec_offset = 0; in bpf_object__init_internal_map()
1650 map->real_name = strdup(real_name); in bpf_object__init_internal_map()
1651 map->name = internal_map_name(obj, real_name); in bpf_object__init_internal_map()
1652 if (!map->real_name || !map->name) { in bpf_object__init_internal_map()
1653 zfree(&map->real_name); in bpf_object__init_internal_map()
1654 zfree(&map->name); in bpf_object__init_internal_map()
1655 return -ENOMEM; in bpf_object__init_internal_map()
1658 def = &map->def; in bpf_object__init_internal_map()
1659 def->type = BPF_MAP_TYPE_ARRAY; in bpf_object__init_internal_map()
1660 def->key_size = sizeof(int); in bpf_object__init_internal_map()
1661 def->value_size = data_sz; in bpf_object__init_internal_map()
1662 def->max_entries = 1; in bpf_object__init_internal_map()
1663 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG in bpf_object__init_internal_map()
1670 def->map_flags |= BPF_F_MMAPABLE; in bpf_object__init_internal_map()
1673 map->name, map->sec_idx, map->sec_offset, def->map_flags); in bpf_object__init_internal_map()
1675 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_object__init_internal_map()
1676 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, in bpf_object__init_internal_map()
1677 MAP_SHARED | MAP_ANONYMOUS, -1, 0); in bpf_object__init_internal_map()
1678 if (map->mmaped == MAP_FAILED) { in bpf_object__init_internal_map()
1679 err = -errno; in bpf_object__init_internal_map()
1680 map->mmaped = NULL; in bpf_object__init_internal_map()
1682 map->name, err); in bpf_object__init_internal_map()
1683 zfree(&map->real_name); in bpf_object__init_internal_map()
1684 zfree(&map->name); in bpf_object__init_internal_map()
1689 memcpy(map->mmaped, data, data_sz); in bpf_object__init_internal_map()
1691 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); in bpf_object__init_internal_map()
1702 * Populate obj->maps with libbpf internal maps. in bpf_object__init_global_data_maps()
1704 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { in bpf_object__init_global_data_maps()
1705 sec_desc = &obj->efile.secs[sec_idx]; in bpf_object__init_global_data_maps()
1708 if (!sec_desc->data || sec_desc->data->d_size == 0) in bpf_object__init_global_data_maps()
1711 switch (sec_desc->sec_type) { in bpf_object__init_global_data_maps()
1716 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
1717 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1720 obj->has_rodata = true; in bpf_object__init_global_data_maps()
1724 sec_desc->data->d_buf, in bpf_object__init_global_data_maps()
1725 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1732 sec_desc->data->d_size); in bpf_object__init_global_data_maps()
1750 for (i = 0; i < obj->nr_extern; i++) { in find_extern_by_name()
1751 if (strcmp(obj->externs[i].name, name) == 0) in find_extern_by_name()
1752 return &obj->externs[i]; in find_extern_by_name()
1760 switch (ext->kcfg.type) { in set_kcfg_value_tri()
1764 ext->name, value); in set_kcfg_value_tri()
1765 return -EINVAL; in set_kcfg_value_tri()
1785 ext->name, value); in set_kcfg_value_tri()
1786 return -EINVAL; in set_kcfg_value_tri()
1788 ext->is_set = true; in set_kcfg_value_tri()
1797 if (ext->kcfg.type != KCFG_CHAR_ARR) { in set_kcfg_value_str()
1799 ext->name, value); in set_kcfg_value_str()
1800 return -EINVAL; in set_kcfg_value_str()
1804 if (value[len - 1] != '"') { in set_kcfg_value_str()
1806 ext->name, value); in set_kcfg_value_str()
1807 return -EINVAL; in set_kcfg_value_str()
1811 len -= 2; in set_kcfg_value_str()
1812 if (len >= ext->kcfg.sz) { in set_kcfg_value_str()
1814 ext->name, value, len, ext->kcfg.sz - 1); in set_kcfg_value_str()
1815 len = ext->kcfg.sz - 1; in set_kcfg_value_str()
1819 ext->is_set = true; in set_kcfg_value_str()
1831 err = -errno; in parse_u64()
1837 return -EINVAL; in parse_u64()
1842 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) in is_kcfg_value_in_range() argument
1844 int bit_sz = ext->kcfg.sz * 8; in is_kcfg_value_in_range()
1846 if (ext->kcfg.sz == 8) in is_kcfg_value_in_range()
1849 /* Validate that value stored in u64 fits in integer of `ext->sz` in is_kcfg_value_in_range()
1854 * -2^(Y-1) <= X <= 2^(Y-1) - 1 in is_kcfg_value_in_range()
1855 * 0 <= X + 2^(Y-1) <= 2^Y - 1 in is_kcfg_value_in_range()
1856 * 0 <= X + 2^(Y-1) < 2^Y in is_kcfg_value_in_range()
1858 * For unsigned target integer, check that all the (64 - Y) bits are in is_kcfg_value_in_range()
1861 if (ext->kcfg.is_signed) in is_kcfg_value_in_range()
1862 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); in is_kcfg_value_in_range()
1864 return (v >> bit_sz) == 0; in is_kcfg_value_in_range()
1870 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && in set_kcfg_value_num()
1871 ext->kcfg.type != KCFG_BOOL) { in set_kcfg_value_num()
1873 ext->name, (unsigned long long)value); in set_kcfg_value_num()
1874 return -EINVAL; in set_kcfg_value_num()
1876 if (ext->kcfg.type == KCFG_BOOL && value > 1) { in set_kcfg_value_num()
1878 ext->name, (unsigned long long)value); in set_kcfg_value_num()
1879 return -EINVAL; in set_kcfg_value_num()
1884 ext->name, (unsigned long long)value, ext->kcfg.sz); in set_kcfg_value_num()
1885 return -ERANGE; in set_kcfg_value_num()
1887 switch (ext->kcfg.sz) { in set_kcfg_value_num()
1901 return -EINVAL; in set_kcfg_value_num()
1903 ext->is_set = true; in set_kcfg_value_num()
1922 return -EINVAL; in bpf_object__process_kconfig_line()
1927 if (buf[len - 1] == '\n') in bpf_object__process_kconfig_line()
1928 buf[len - 1] = '\0'; in bpf_object__process_kconfig_line()
1934 return -EINVAL; in bpf_object__process_kconfig_line()
1938 if (!ext || ext->is_set) in bpf_object__process_kconfig_line()
1941 ext_val = data + ext->kcfg.data_off; in bpf_object__process_kconfig_line()
1955 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); in bpf_object__process_kconfig_line()
1958 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { in bpf_object__process_kconfig_line()
1959 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); in bpf_object__process_kconfig_line()
1960 return -EINVAL; in bpf_object__process_kconfig_line()
1967 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); in bpf_object__process_kconfig_line()
1979 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); in bpf_object__read_kconfig_file()
1981 return -EINVAL; in bpf_object__read_kconfig_file()
1983 return -ENAMETOOLONG; in bpf_object__read_kconfig_file()
1992 return -ENOENT; in bpf_object__read_kconfig_file()
2018 err = -errno; in bpf_object__read_kconfig_mem()
2019 pr_warn("failed to open in-memory Kconfig: %d\n", err); in bpf_object__read_kconfig_mem()
2026 pr_warn("error parsing in-memory Kconfig line '%s': %d\n", in bpf_object__read_kconfig_mem()
2042 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__init_kconfig_map()
2043 ext = &obj->externs[i]; in bpf_object__init_kconfig_map()
2044 if (ext->type == EXT_KCFG) in bpf_object__init_kconfig_map()
2051 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; in bpf_object__init_kconfig_map()
2053 ".kconfig", obj->efile.symbols_shndx, in bpf_object__init_kconfig_map()
2058 obj->kconfig_map_idx = obj->nr_maps - 1; in bpf_object__init_kconfig_map()
2073 *res_id = t->type; in skip_mods_and_typedefs()
2074 t = btf__type_by_id(btf, t->type); in skip_mods_and_typedefs()
2089 t = skip_mods_and_typedefs(btf, t->type, res_id); in resolve_func_ptr()
2136 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); in get_map_field_int()
2137 const char *name = btf__name_by_offset(btf, m->name_off); in get_map_field_int()
2147 arr_t = btf__type_by_id(btf, t->type); in get_map_field_int()
2150 map_name, name, t->type); in get_map_field_int()
2159 *res = arr_info->nelems; in get_map_field_int()
2169 return -EINVAL; in pathname_concat()
2171 return -ENAMETOOLONG; in pathname_concat()
2210 const char *name = btf__name_by_offset(btf, m->name_off); in parse_btf_map_def()
2214 return -EINVAL; in parse_btf_map_def()
2217 if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) in parse_btf_map_def()
2218 return -EINVAL; in parse_btf_map_def()
2219 map_def->parts |= MAP_DEF_MAP_TYPE; in parse_btf_map_def()
2221 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) in parse_btf_map_def()
2222 return -EINVAL; in parse_btf_map_def()
2223 map_def->parts |= MAP_DEF_MAX_ENTRIES; in parse_btf_map_def()
2225 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) in parse_btf_map_def()
2226 return -EINVAL; in parse_btf_map_def()
2227 map_def->parts |= MAP_DEF_MAP_FLAGS; in parse_btf_map_def()
2229 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) in parse_btf_map_def()
2230 return -EINVAL; in parse_btf_map_def()
2231 map_def->parts |= MAP_DEF_NUMA_NODE; in parse_btf_map_def()
2236 return -EINVAL; in parse_btf_map_def()
2237 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2239 map_name, map_def->key_size, sz); in parse_btf_map_def()
2240 return -EINVAL; in parse_btf_map_def()
2242 map_def->key_size = sz; in parse_btf_map_def()
2243 map_def->parts |= MAP_DEF_KEY_SIZE; in parse_btf_map_def()
2247 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2250 map_name, m->type); in parse_btf_map_def()
2251 return -EINVAL; in parse_btf_map_def()
2254 pr_warn("map '%s': key spec is not PTR: %s.\n", in parse_btf_map_def()
2256 return -EINVAL; in parse_btf_map_def()
2258 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2261 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2264 if (map_def->key_size && map_def->key_size != sz) { in parse_btf_map_def()
2266 map_name, map_def->key_size, (ssize_t)sz); in parse_btf_map_def()
2267 return -EINVAL; in parse_btf_map_def()
2269 map_def->key_size = sz; in parse_btf_map_def()
2270 map_def->key_type_id = t->type; in parse_btf_map_def()
2271 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; in parse_btf_map_def()
2276 return -EINVAL; in parse_btf_map_def()
2277 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2279 map_name, map_def->value_size, sz); in parse_btf_map_def()
2280 return -EINVAL; in parse_btf_map_def()
2282 map_def->value_size = sz; in parse_btf_map_def()
2283 map_def->parts |= MAP_DEF_VALUE_SIZE; in parse_btf_map_def()
2287 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2290 map_name, m->type); in parse_btf_map_def()
2291 return -EINVAL; in parse_btf_map_def()
2294 pr_warn("map '%s': value spec is not PTR: %s.\n", in parse_btf_map_def()
2296 return -EINVAL; in parse_btf_map_def()
2298 sz = btf__resolve_size(btf, t->type); in parse_btf_map_def()
2301 map_name, t->type, (ssize_t)sz); in parse_btf_map_def()
2304 if (map_def->value_size && map_def->value_size != sz) { in parse_btf_map_def()
2306 map_name, map_def->value_size, (ssize_t)sz); in parse_btf_map_def()
2307 return -EINVAL; in parse_btf_map_def()
2309 map_def->value_size = sz; in parse_btf_map_def()
2310 map_def->value_type_id = t->type; in parse_btf_map_def()
2311 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; in parse_btf_map_def()
2314 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); in parse_btf_map_def()
2315 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; in parse_btf_map_def()
2316 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; in parse_btf_map_def()
2321 pr_warn("map '%s': multi-level inner maps not supported.\n", in parse_btf_map_def()
2323 return -ENOTSUP; in parse_btf_map_def()
2325 if (i != vlen - 1) { in parse_btf_map_def()
2328 return -EINVAL; in parse_btf_map_def()
2331 pr_warn("map '%s': should be map-in-map or prog-array.\n", in parse_btf_map_def()
2333 return -ENOTSUP; in parse_btf_map_def()
2335 if (map_def->value_size && map_def->value_size != 4) { in parse_btf_map_def()
2337 map_name, map_def->value_size); in parse_btf_map_def()
2338 return -EINVAL; in parse_btf_map_def()
2340 map_def->value_size = 4; in parse_btf_map_def()
2341 t = btf__type_by_id(btf, m->type); in parse_btf_map_def()
2344 map_name, desc, m->type); in parse_btf_map_def()
2345 return -EINVAL; in parse_btf_map_def()
2347 if (!btf_is_array(t) || btf_array(t)->nelems) { in parse_btf_map_def()
2348 pr_warn("map '%s': %s spec is not a zero-sized array.\n", in parse_btf_map_def()
2350 return -EINVAL; in parse_btf_map_def()
2352 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); in parse_btf_map_def()
2356 return -EINVAL; in parse_btf_map_def()
2358 t = skip_mods_and_typedefs(btf, t->type, NULL); in parse_btf_map_def()
2361 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", in parse_btf_map_def()
2363 return -EINVAL; in parse_btf_map_def()
2368 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", in parse_btf_map_def()
2370 return -EINVAL; in parse_btf_map_def()
2378 map_def->parts |= MAP_DEF_INNER_MAP; in parse_btf_map_def()
2384 return -EINVAL; in parse_btf_map_def()
2387 return -EINVAL; in parse_btf_map_def()
2391 return -EINVAL; in parse_btf_map_def()
2393 map_def->pinning = val; in parse_btf_map_def()
2394 map_def->parts |= MAP_DEF_PINNING; in parse_btf_map_def()
2399 return -EINVAL; in parse_btf_map_def()
2400 map_def->map_extra = map_extra; in parse_btf_map_def()
2401 map_def->parts |= MAP_DEF_MAP_EXTRA; in parse_btf_map_def()
2405 return -ENOTSUP; in parse_btf_map_def()
2411 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { in parse_btf_map_def()
2413 return -EINVAL; in parse_btf_map_def()
2428 * a power-of-2 multiple of kernel's page size. If user diligently in adjust_ringbuf_sz()
2435 * user-set size to satisfy both user size request and kernel in adjust_ringbuf_sz()
2444 * very close to UINT_MAX but is not a power-of-2 multiple of in adjust_ringbuf_sz()
2452 return map->def.type == BPF_MAP_TYPE_RINGBUF || in map_is_ringbuf()
2453 map->def.type == BPF_MAP_TYPE_USER_RINGBUF; in map_is_ringbuf()
2458 map->def.type = def->map_type; in fill_map_from_def()
2459 map->def.key_size = def->key_size; in fill_map_from_def()
2460 map->def.value_size = def->value_size; in fill_map_from_def()
2461 map->def.max_entries = def->max_entries; in fill_map_from_def()
2462 map->def.map_flags = def->map_flags; in fill_map_from_def()
2463 map->map_extra = def->map_extra; in fill_map_from_def()
2465 map->numa_node = def->numa_node; in fill_map_from_def()
2466 map->btf_key_type_id = def->key_type_id; in fill_map_from_def()
2467 map->btf_value_type_id = def->value_type_id; in fill_map_from_def()
2469 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in fill_map_from_def()
2471 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in fill_map_from_def()
2473 if (def->parts & MAP_DEF_MAP_TYPE) in fill_map_from_def()
2474 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); in fill_map_from_def()
2476 if (def->parts & MAP_DEF_KEY_TYPE) in fill_map_from_def()
2478 map->name, def->key_type_id, def->key_size); in fill_map_from_def()
2479 else if (def->parts & MAP_DEF_KEY_SIZE) in fill_map_from_def()
2480 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); in fill_map_from_def()
2482 if (def->parts & MAP_DEF_VALUE_TYPE) in fill_map_from_def()
2484 map->name, def->value_type_id, def->value_size); in fill_map_from_def()
2485 else if (def->parts & MAP_DEF_VALUE_SIZE) in fill_map_from_def()
2486 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); in fill_map_from_def()
2488 if (def->parts & MAP_DEF_MAX_ENTRIES) in fill_map_from_def()
2489 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); in fill_map_from_def()
2490 if (def->parts & MAP_DEF_MAP_FLAGS) in fill_map_from_def()
2491 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); in fill_map_from_def()
2492 if (def->parts & MAP_DEF_MAP_EXTRA) in fill_map_from_def()
2493 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, in fill_map_from_def()
2494 (unsigned long long)def->map_extra); in fill_map_from_def()
2495 if (def->parts & MAP_DEF_PINNING) in fill_map_from_def()
2496 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); in fill_map_from_def()
2497 if (def->parts & MAP_DEF_NUMA_NODE) in fill_map_from_def()
2498 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); in fill_map_from_def()
2500 if (def->parts & MAP_DEF_INNER_MAP) in fill_map_from_def()
2501 pr_debug("map '%s': found inner map definition.\n", map->name); in fill_map_from_def()
2529 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__init_user_btf_map()
2531 map_name = btf__name_by_offset(obj->btf, var->name_off); in bpf_object__init_user_btf_map()
2535 return -EINVAL; in bpf_object__init_user_btf_map()
2537 if ((__u64)vi->offset + vi->size > data->d_size) { in bpf_object__init_user_btf_map()
2539 return -EINVAL; in bpf_object__init_user_btf_map()
2544 return -EINVAL; in bpf_object__init_user_btf_map()
2546 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { in bpf_object__init_user_btf_map()
2548 map_name, btf_var_linkage_str(var_extra->linkage)); in bpf_object__init_user_btf_map()
2549 return -EOPNOTSUPP; in bpf_object__init_user_btf_map()
2552 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__init_user_btf_map()
2556 return -EINVAL; in bpf_object__init_user_btf_map()
2558 if (def->size > vi->size) { in bpf_object__init_user_btf_map()
2560 return -EINVAL; in bpf_object__init_user_btf_map()
2566 map->name = strdup(map_name); in bpf_object__init_user_btf_map()
2567 if (!map->name) { in bpf_object__init_user_btf_map()
2569 return -ENOMEM; in bpf_object__init_user_btf_map()
2571 map->libbpf_type = LIBBPF_MAP_UNSPEC; in bpf_object__init_user_btf_map()
2572 map->def.type = BPF_MAP_TYPE_UNSPEC; in bpf_object__init_user_btf_map()
2573 map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2574 map->sec_offset = vi->offset; in bpf_object__init_user_btf_map()
2575 map->btf_var_idx = var_idx; in bpf_object__init_user_btf_map()
2577 map_name, map->sec_idx, map->sec_offset); in bpf_object__init_user_btf_map()
2579 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); in bpf_object__init_user_btf_map()
2588 pr_warn("map '%s': couldn't build pin path.\n", map->name); in bpf_object__init_user_btf_map()
2594 map->inner_map = calloc(1, sizeof(*map->inner_map)); in bpf_object__init_user_btf_map()
2595 if (!map->inner_map) in bpf_object__init_user_btf_map()
2596 return -ENOMEM; in bpf_object__init_user_btf_map()
2597 map->inner_map->fd = -1; in bpf_object__init_user_btf_map()
2598 map->inner_map->sec_idx = sec_idx; in bpf_object__init_user_btf_map()
2599 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); in bpf_object__init_user_btf_map()
2600 if (!map->inner_map->name) in bpf_object__init_user_btf_map()
2601 return -ENOMEM; in bpf_object__init_user_btf_map()
2602 sprintf(map->inner_map->name, "%s.inner", map_name); in bpf_object__init_user_btf_map()
2604 fill_map_from_def(map->inner_map, &inner_def); in bpf_object__init_user_btf_map()
2624 if (obj->efile.btf_maps_shndx < 0) in bpf_object__init_user_btf_maps()
2627 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); in bpf_object__init_user_btf_maps()
2631 MAPS_ELF_SEC, obj->path); in bpf_object__init_user_btf_maps()
2632 return -EINVAL; in bpf_object__init_user_btf_maps()
2635 nr_types = btf__type_cnt(obj->btf); in bpf_object__init_user_btf_maps()
2637 t = btf__type_by_id(obj->btf, i); in bpf_object__init_user_btf_maps()
2640 name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__init_user_btf_maps()
2643 obj->efile.btf_maps_sec_btf_id = i; in bpf_object__init_user_btf_maps()
2650 return -ENOENT; in bpf_object__init_user_btf_maps()
2656 obj->efile.btf_maps_shndx, in bpf_object__init_user_btf_maps()
2692 return sh->sh_flags & SHF_EXECINSTR; in section_have_execinstr()
2727 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); in bpf_object__sanitize_btf()
2733 t->size = 1; in bpf_object__sanitize_btf()
2737 const struct btf_var_secinfo *v = btf_var_secinfos(t); in bpf_object__sanitize_btf() local
2742 name = (char *)btf__name_by_offset(btf, t->name_off); in bpf_object__sanitize_btf()
2750 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); in bpf_object__sanitize_btf()
2751 for (j = 0; j < vlen; j++, v++, m++) { in bpf_object__sanitize_btf()
2753 m->offset = v->offset * 8; in bpf_object__sanitize_btf()
2754 m->type = v->type; in bpf_object__sanitize_btf()
2756 vt = (void *)btf__type_by_id(btf, v->type); in bpf_object__sanitize_btf()
2757 m->name_off = vt->name_off; in bpf_object__sanitize_btf()
2762 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); in bpf_object__sanitize_btf()
2763 t->size = sizeof(__u32); /* kernel enforced */ in bpf_object__sanitize_btf()
2766 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); in bpf_object__sanitize_btf()
2769 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); in bpf_object__sanitize_btf()
2771 /* replace FLOAT with an equally-sized empty STRUCT; in bpf_object__sanitize_btf()
2775 t->name_off = 0; in bpf_object__sanitize_btf()
2776 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); in bpf_object__sanitize_btf()
2779 t->name_off = 0; in bpf_object__sanitize_btf()
2780 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); in bpf_object__sanitize_btf()
2783 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); in bpf_object__sanitize_btf()
2798 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); in bpf_object__sanitize_btf()
2800 m->type = enum64_placeholder_id; in bpf_object__sanitize_btf()
2801 m->offset = 0; in bpf_object__sanitize_btf()
2811 return obj->efile.btf_maps_shndx >= 0 || in libbpf_needs_btf()
2812 obj->efile.st_ops_shndx >= 0 || in libbpf_needs_btf()
2813 obj->efile.st_ops_link_shndx >= 0 || in libbpf_needs_btf()
2814 obj->nr_extern > 0; in libbpf_needs_btf()
2819 return obj->efile.st_ops_shndx >= 0 || obj->efile.st_ops_link_shndx >= 0; in kernel_needs_btf()
2826 int err = -ENOENT; in bpf_object__init_btf()
2829 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); in bpf_object__init_btf()
2830 err = libbpf_get_error(obj->btf); in bpf_object__init_btf()
2832 obj->btf = NULL; in bpf_object__init_btf()
2836 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__init_btf()
2837 btf__set_pointer_size(obj->btf, 8); in bpf_object__init_btf()
2843 if (!obj->btf) { in bpf_object__init_btf()
2848 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); in bpf_object__init_btf()
2849 err = libbpf_get_error(obj->btf_ext); in bpf_object__init_btf()
2853 obj->btf_ext = NULL; in bpf_object__init_btf()
2858 ext_segs[0] = &obj->btf_ext->func_info; in bpf_object__init_btf()
2859 ext_segs[1] = &obj->btf_ext->line_info; in bpf_object__init_btf()
2860 ext_segs[2] = &obj->btf_ext->core_relo_info; in bpf_object__init_btf()
2867 if (seg->sec_cnt == 0) in bpf_object__init_btf()
2870 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); in bpf_object__init_btf()
2871 if (!seg->sec_idxs) { in bpf_object__init_btf()
2872 err = -ENOMEM; in bpf_object__init_btf()
2883 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__init_btf()
2890 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); in bpf_object__init_btf()
2907 return a->offset - b->offset; in compare_vsi_off()
2914 const char *sec_name = btf__name_by_offset(btf, t->name_off); in btf_fixup_datasec()
2921 return -ENOENT; in btf_fixup_datasec()
2924 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and in btf_fixup_datasec()
2936 * to be optional. But the STV_HIDDEN handling is non-optional for any in btf_fixup_datasec()
2937 * non-extern DATASEC, so the variable fixup loop below handles both in btf_fixup_datasec()
2938 * functions at the same time, paying the cost of BTF VAR <-> ELF in btf_fixup_datasec()
2941 if (t->size == 0) { in btf_fixup_datasec()
2946 return -ENOENT; in btf_fixup_datasec()
2949 t->size = size; in btf_fixup_datasec()
2959 t_var = btf__type_by_id(btf, vsi->type); in btf_fixup_datasec()
2961 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); in btf_fixup_datasec()
2962 return -EINVAL; in btf_fixup_datasec()
2966 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) in btf_fixup_datasec()
2969 var_name = btf__name_by_offset(btf, t_var->name_off); in btf_fixup_datasec()
2973 return -ENOENT; in btf_fixup_datasec()
2980 return -ENOENT; in btf_fixup_datasec()
2984 vsi->offset = sym->st_value; in btf_fixup_datasec()
2993 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN in btf_fixup_datasec()
2994 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) in btf_fixup_datasec()
2995 var->linkage = BTF_VAR_STATIC; in btf_fixup_datasec()
3007 if (!obj->btf) in bpf_object_fixup_btf()
3010 n = btf__type_cnt(obj->btf); in bpf_object_fixup_btf()
3012 struct btf_type *t = btf_type_by_id(obj->btf, i); in bpf_object_fixup_btf()
3020 err = btf_fixup_datasec(obj, obj->btf, t); in bpf_object_fixup_btf()
3031 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || in prog_needs_vmlinux_btf()
3032 prog->type == BPF_PROG_TYPE_LSM) in prog_needs_vmlinux_btf()
3038 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) in prog_needs_vmlinux_btf()
3049 /* CO-RE relocations need kernel BTF, only when btf_custom_path in obj_needs_vmlinux_btf()
3052 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) in obj_needs_vmlinux_btf()
3056 for (i = 0; i < obj->nr_extern; i++) { in obj_needs_vmlinux_btf()
3059 ext = &obj->externs[i]; in obj_needs_vmlinux_btf()
3060 if (ext->type == EXT_KSYM && ext->ksym.type_id) in obj_needs_vmlinux_btf()
3065 if (!prog->autoload) in obj_needs_vmlinux_btf()
3079 if (obj->btf_vmlinux || obj->gen_loader) in bpf_object__load_vmlinux_btf()
3085 obj->btf_vmlinux = btf__load_vmlinux_btf(); in bpf_object__load_vmlinux_btf()
3086 err = libbpf_get_error(obj->btf_vmlinux); in bpf_object__load_vmlinux_btf()
3089 obj->btf_vmlinux = NULL; in bpf_object__load_vmlinux_btf()
3097 struct btf *kern_btf = obj->btf; in bpf_object__sanitize_and_load_btf()
3101 if (!obj->btf) in bpf_object__sanitize_and_load_btf()
3106 err = -EOPNOTSUPP; in bpf_object__sanitize_and_load_btf()
3121 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sanitize_and_load_btf()
3122 struct bpf_program *prog = &obj->programs[i]; in bpf_object__sanitize_and_load_btf()
3127 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) in bpf_object__sanitize_and_load_btf()
3130 n = btf__type_cnt(obj->btf); in bpf_object__sanitize_and_load_btf()
3132 t = btf_type_by_id(obj->btf, j); in bpf_object__sanitize_and_load_btf()
3136 name = btf__str_by_offset(obj->btf, t->name_off); in bpf_object__sanitize_and_load_btf()
3137 if (strcmp(name, prog->name) != 0) in bpf_object__sanitize_and_load_btf()
3140 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); in bpf_object__sanitize_and_load_btf()
3151 raw_data = btf__raw_data(obj->btf, &sz); in bpf_object__sanitize_and_load_btf()
3157 /* enforce 8-byte pointers for BPF-targeted BTFs */ in bpf_object__sanitize_and_load_btf()
3158 btf__set_pointer_size(obj->btf, 8); in bpf_object__sanitize_and_load_btf()
3164 if (obj->gen_loader) { in bpf_object__sanitize_and_load_btf()
3169 return -ENOMEM; in bpf_object__sanitize_and_load_btf()
3170 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); in bpf_object__sanitize_and_load_btf()
3172 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. in bpf_object__sanitize_and_load_btf()
3177 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, in bpf_object__sanitize_and_load_btf()
3178 obj->log_level ? 1 : 0); in bpf_object__sanitize_and_load_btf()
3183 btf__set_fd(obj->btf, btf__fd(kern_btf)); in bpf_object__sanitize_and_load_btf()
3184 btf__set_fd(kern_btf, -1); in bpf_object__sanitize_and_load_btf()
3204 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); in elf_sym_str()
3207 off, obj->path, elf_errmsg(-1)); in elf_sym_str()
3218 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); in elf_sec_str()
3221 off, obj->path, elf_errmsg(-1)); in elf_sec_str()
3232 scn = elf_getscn(obj->efile.elf, idx); in elf_sec_by_idx()
3235 idx, obj->path, elf_errmsg(-1)); in elf_sec_by_idx()
3244 Elf *elf = obj->efile.elf; in elf_sec_by_name()
3270 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_hdr()
3289 name = elf_sec_str(obj, sh->sh_name); in elf_sec_name()
3292 elf_ndxscn(scn), obj->path, elf_errmsg(-1)); in elf_sec_name()
3310 obj->path, elf_errmsg(-1)); in elf_sec_data()
3319 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) in elf_sym_by_idx()
3322 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; in elf_sym_by_idx()
3327 if (idx >= data->d_size / sizeof(Elf64_Rel)) in elf_rel_by_idx()
3330 return (Elf64_Rel *)data->d_buf + idx; in elf_rel_by_idx()
3342 if (hdr->sh_type == SHT_STRTAB) in ignore_elf_section()
3346 if (hdr->sh_type == SHT_LLVM_ADDRSIG) in ignore_elf_section()
3350 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && in ignore_elf_section()
3359 name += sizeof(".rel") - 1; in ignore_elf_section()
3378 if (a->sec_idx != b->sec_idx) in cmp_progs()
3379 return a->sec_idx < b->sec_idx ? -1 : 1; in cmp_progs()
3382 return a->sec_insn_off < b->sec_insn_off ? -1 : 1; in cmp_progs()
3388 Elf *elf = obj->efile.elf; in bpf_object__elf_collect()
3397 /* ELF section indices are 0-based, but sec #0 is special "invalid" in bpf_object__elf_collect()
3402 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { in bpf_object__elf_collect()
3404 obj->path, elf_errmsg(-1)); in bpf_object__elf_collect()
3405 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3407 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); in bpf_object__elf_collect()
3408 if (!obj->efile.secs) in bpf_object__elf_collect()
3409 return -ENOMEM; in bpf_object__elf_collect()
3418 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3420 if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3421 if (obj->efile.symbols) { in bpf_object__elf_collect()
3422 pr_warn("elf: multiple symbol tables in %s\n", obj->path); in bpf_object__elf_collect()
3423 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3428 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3432 obj->efile.symbols = data; in bpf_object__elf_collect()
3433 obj->efile.symbols_shndx = idx; in bpf_object__elf_collect()
3434 obj->efile.strtabidx = sh->sh_link; in bpf_object__elf_collect()
3438 if (!obj->efile.symbols) { in bpf_object__elf_collect()
3440 obj->path); in bpf_object__elf_collect()
3441 return -ENOENT; in bpf_object__elf_collect()
3447 sec_desc = &obj->efile.secs[idx]; in bpf_object__elf_collect()
3451 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3453 name = elf_sec_str(obj, sh->sh_name); in bpf_object__elf_collect()
3455 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3462 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3465 idx, name, (unsigned long)data->d_size, in bpf_object__elf_collect()
3466 (int)sh->sh_link, (unsigned long)sh->sh_flags, in bpf_object__elf_collect()
3467 (int)sh->sh_type); in bpf_object__elf_collect()
3470 err = bpf_object__init_license(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3474 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); in bpf_object__elf_collect()
3479 return -ENOTSUP; in bpf_object__elf_collect()
3481 obj->efile.btf_maps_shndx = idx; in bpf_object__elf_collect()
3483 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3484 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3487 if (sh->sh_type != SHT_PROGBITS) in bpf_object__elf_collect()
3488 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3490 } else if (sh->sh_type == SHT_SYMTAB) { in bpf_object__elf_collect()
3492 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { in bpf_object__elf_collect()
3493 if (sh->sh_flags & SHF_EXECINSTR) { in bpf_object__elf_collect()
3495 obj->efile.text_shndx = idx; in bpf_object__elf_collect()
3501 sec_desc->sec_type = SEC_DATA; in bpf_object__elf_collect()
3502 sec_desc->shdr = sh; in bpf_object__elf_collect()
3503 sec_desc->data = data; in bpf_object__elf_collect()
3506 sec_desc->sec_type = SEC_RODATA; in bpf_object__elf_collect()
3507 sec_desc->shdr = sh; in bpf_object__elf_collect()
3508 sec_desc->data = data; in bpf_object__elf_collect()
3510 obj->efile.st_ops_data = data; in bpf_object__elf_collect()
3511 obj->efile.st_ops_shndx = idx; in bpf_object__elf_collect()
3513 obj->efile.st_ops_link_data = data; in bpf_object__elf_collect()
3514 obj->efile.st_ops_link_shndx = idx; in bpf_object__elf_collect()
3519 } else if (sh->sh_type == SHT_REL) { in bpf_object__elf_collect()
3520 int targ_sec_idx = sh->sh_info; /* points to other section */ in bpf_object__elf_collect()
3522 if (sh->sh_entsize != sizeof(Elf64_Rel) || in bpf_object__elf_collect()
3523 targ_sec_idx >= obj->efile.sec_cnt) in bpf_object__elf_collect()
3524 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3537 sec_desc->sec_type = SEC_RELO; in bpf_object__elf_collect()
3538 sec_desc->shdr = sh; in bpf_object__elf_collect()
3539 sec_desc->data = data; in bpf_object__elf_collect()
3540 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || in bpf_object__elf_collect()
3542 sec_desc->sec_type = SEC_BSS; in bpf_object__elf_collect()
3543 sec_desc->shdr = sh; in bpf_object__elf_collect()
3544 sec_desc->data = data; in bpf_object__elf_collect()
3547 (size_t)sh->sh_size); in bpf_object__elf_collect()
3551 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { in bpf_object__elf_collect()
3552 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); in bpf_object__elf_collect()
3553 return -LIBBPF_ERRNO__FORMAT; in bpf_object__elf_collect()
3556 /* sort BPF programs by section name and in-section instruction offset in bpf_object__elf_collect()
3559 if (obj->nr_programs) in bpf_object__elf_collect()
3560 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); in bpf_object__elf_collect()
3567 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_extern()
3569 return sym->st_shndx == SHN_UNDEF && in sym_is_extern()
3571 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; in sym_is_extern()
3576 int bind = ELF64_ST_BIND(sym->st_info); in sym_is_subprog()
3577 int type = ELF64_ST_TYPE(sym->st_info); in sym_is_subprog()
3580 if (sym->st_shndx != text_shndx) in sym_is_subprog()
3598 return -ESRCH; in find_extern_btf_id()
3607 tname = btf__name_by_offset(btf, t->name_off); in find_extern_btf_id()
3612 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) in find_extern_btf_id()
3613 return -EINVAL; in find_extern_btf_id()
3616 return -EINVAL; in find_extern_btf_id()
3621 return -ENOENT; in find_extern_btf_id()
3630 return -ESRCH; in find_extern_sec_btf_id()
3641 if (vs->type == ext_btf_id) in find_extern_sec_btf_id()
3646 return -ENOENT; in find_extern_sec_btf_id()
3656 name = btf__name_by_offset(btf, t->name_off); in find_kcfg_type()
3665 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; in find_kcfg_type()
3668 if (t->size == 1) in find_kcfg_type()
3670 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) in find_kcfg_type()
3675 if (t->size != 4) in find_kcfg_type()
3685 if (btf_array(t)->nelems == 0) in find_kcfg_type()
3687 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) in find_kcfg_type()
3700 if (a->type != b->type) in cmp_externs()
3701 return a->type < b->type ? -1 : 1; in cmp_externs()
3703 if (a->type == EXT_KCFG) { in cmp_externs()
3705 if (a->kcfg.align != b->kcfg.align) in cmp_externs()
3706 return a->kcfg.align > b->kcfg.align ? -1 : 1; in cmp_externs()
3708 if (a->kcfg.sz != b->kcfg.sz) in cmp_externs()
3709 return a->kcfg.sz < b->kcfg.sz ? -1 : 1; in cmp_externs()
3713 return strcmp(a->name, b->name); in cmp_externs()
3751 vt = btf__type_by_id(btf, vs->type); in add_dummy_ksym_var()
3782 if (!obj->efile.symbols) in bpf_object__collect_externs()
3785 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); in bpf_object__collect_externs()
3787 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) in bpf_object__collect_externs()
3788 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
3790 dummy_var_btf_id = add_dummy_ksym_var(obj->btf); in bpf_object__collect_externs()
3794 n = sh->sh_size / sh->sh_entsize; in bpf_object__collect_externs()
3801 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_externs()
3804 ext_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_externs()
3808 ext = obj->externs; in bpf_object__collect_externs()
3809 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); in bpf_object__collect_externs()
3811 return -ENOMEM; in bpf_object__collect_externs()
3812 obj->externs = ext; in bpf_object__collect_externs()
3813 ext = &ext[obj->nr_extern]; in bpf_object__collect_externs()
3815 obj->nr_extern++; in bpf_object__collect_externs()
3817 ext->btf_id = find_extern_btf_id(obj->btf, ext_name); in bpf_object__collect_externs()
3818 if (ext->btf_id <= 0) { in bpf_object__collect_externs()
3820 ext_name, ext->btf_id); in bpf_object__collect_externs()
3821 return ext->btf_id; in bpf_object__collect_externs()
3823 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
3824 ext->name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
3825 ext->sym_idx = i; in bpf_object__collect_externs()
3826 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; in bpf_object__collect_externs()
3828 ext_essent_len = bpf_core_essential_name_len(ext->name); in bpf_object__collect_externs()
3829 ext->essent_name = NULL; in bpf_object__collect_externs()
3830 if (ext_essent_len != strlen(ext->name)) { in bpf_object__collect_externs()
3831 ext->essent_name = strndup(ext->name, ext_essent_len); in bpf_object__collect_externs()
3832 if (!ext->essent_name) in bpf_object__collect_externs()
3833 return -ENOMEM; in bpf_object__collect_externs()
3836 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); in bpf_object__collect_externs()
3837 if (ext->sec_btf_id <= 0) { in bpf_object__collect_externs()
3839 ext_name, ext->btf_id, ext->sec_btf_id); in bpf_object__collect_externs()
3840 return ext->sec_btf_id; in bpf_object__collect_externs()
3842 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); in bpf_object__collect_externs()
3843 sec_name = btf__name_by_offset(obj->btf, sec->name_off); in bpf_object__collect_externs()
3848 ext->name, KCONFIG_SEC); in bpf_object__collect_externs()
3849 return -ENOTSUP; in bpf_object__collect_externs()
3852 ext->type = EXT_KCFG; in bpf_object__collect_externs()
3853 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); in bpf_object__collect_externs()
3854 if (ext->kcfg.sz <= 0) { in bpf_object__collect_externs()
3856 ext_name, ext->kcfg.sz); in bpf_object__collect_externs()
3857 return ext->kcfg.sz; in bpf_object__collect_externs()
3859 ext->kcfg.align = btf__align_of(obj->btf, t->type); in bpf_object__collect_externs()
3860 if (ext->kcfg.align <= 0) { in bpf_object__collect_externs()
3862 ext_name, ext->kcfg.align); in bpf_object__collect_externs()
3863 return -EINVAL; in bpf_object__collect_externs()
3865 ext->kcfg.type = find_kcfg_type(obj->btf, t->type, in bpf_object__collect_externs()
3866 &ext->kcfg.is_signed); in bpf_object__collect_externs()
3867 if (ext->kcfg.type == KCFG_UNKNOWN) { in bpf_object__collect_externs()
3869 return -ENOTSUP; in bpf_object__collect_externs()
3873 ext->type = EXT_KSYM; in bpf_object__collect_externs()
3874 skip_mods_and_typedefs(obj->btf, t->type, in bpf_object__collect_externs()
3875 &ext->ksym.type_id); in bpf_object__collect_externs()
3878 return -ENOTSUP; in bpf_object__collect_externs()
3881 pr_debug("collected %d externs total\n", obj->nr_extern); in bpf_object__collect_externs()
3883 if (!obj->nr_extern) in bpf_object__collect_externs()
3887 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); in bpf_object__collect_externs()
3891 * pretending that each extern is a 8-byte variable in bpf_object__collect_externs()
3894 /* find existing 4-byte integer type in BTF to use for fake in bpf_object__collect_externs()
3897 int int_btf_id = find_int_btf_id(obj->btf); in bpf_object__collect_externs()
3899 * will be used to replace the vs->type and in bpf_object__collect_externs()
3905 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); in bpf_object__collect_externs()
3906 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
3907 ext = &obj->externs[i]; in bpf_object__collect_externs()
3908 if (ext->type != EXT_KSYM) in bpf_object__collect_externs()
3911 i, ext->sym_idx, ext->name); in bpf_object__collect_externs()
3920 vt = (void *)btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
3921 ext_name = btf__name_by_offset(obj->btf, vt->name_off); in bpf_object__collect_externs()
3926 return -ESRCH; in bpf_object__collect_externs()
3933 func_proto = btf__type_by_id(obj->btf, in bpf_object__collect_externs()
3934 vt->type); in bpf_object__collect_externs()
3942 dummy_var->name_off; in bpf_object__collect_externs()
3943 vs->type = dummy_var_btf_id; in bpf_object__collect_externs()
3944 vt->info &= ~0xffff; in bpf_object__collect_externs()
3945 vt->info |= BTF_FUNC_GLOBAL; in bpf_object__collect_externs()
3947 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
3948 vt->type = int_btf_id; in bpf_object__collect_externs()
3950 vs->offset = off; in bpf_object__collect_externs()
3951 vs->size = sizeof(int); in bpf_object__collect_externs()
3953 sec->size = off; in bpf_object__collect_externs()
3960 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__collect_externs()
3961 ext = &obj->externs[i]; in bpf_object__collect_externs()
3962 if (ext->type != EXT_KCFG) in bpf_object__collect_externs()
3965 ext->kcfg.data_off = roundup(off, ext->kcfg.align); in bpf_object__collect_externs()
3966 off = ext->kcfg.data_off + ext->kcfg.sz; in bpf_object__collect_externs()
3968 i, ext->sym_idx, ext->kcfg.data_off, ext->name); in bpf_object__collect_externs()
3970 sec->size = off; in bpf_object__collect_externs()
3975 t = btf__type_by_id(obj->btf, vs->type); in bpf_object__collect_externs()
3976 ext_name = btf__name_by_offset(obj->btf, t->name_off); in bpf_object__collect_externs()
3981 return -ESRCH; in bpf_object__collect_externs()
3983 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; in bpf_object__collect_externs()
3984 vs->offset = ext->kcfg.data_off; in bpf_object__collect_externs()
3992 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; in prog_is_subprog()
4004 if (!strcmp(prog->name, name)) in bpf_object__find_program_by_name()
4013 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__shndx_is_data()
4026 return shndx == obj->efile.btf_maps_shndx; in bpf_object__shndx_is_maps()
4032 if (shndx == obj->efile.symbols_shndx) in bpf_object__section_to_libbpf_map_type()
4035 switch (obj->efile.secs[shndx].sec_type) { in bpf_object__section_to_libbpf_map_type()
4052 struct bpf_insn *insn = &prog->insns[insn_idx]; in bpf_program__record_reloc()
4053 size_t map_idx, nr_maps = prog->obj->nr_maps; in bpf_program__record_reloc()
4054 struct bpf_object *obj = prog->obj; in bpf_program__record_reloc()
4055 __u32 shdr_idx = sym->st_shndx; in bpf_program__record_reloc()
4062 prog->name, sym_name, insn_idx, insn->code); in bpf_program__record_reloc()
4063 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4067 int sym_idx = ELF64_R_SYM(rel->r_info); in bpf_program__record_reloc()
4068 int i, n = obj->nr_extern; in bpf_program__record_reloc()
4072 ext = &obj->externs[i]; in bpf_program__record_reloc()
4073 if (ext->sym_idx == sym_idx) in bpf_program__record_reloc()
4078 prog->name, sym_name, sym_idx); in bpf_program__record_reloc()
4079 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4082 prog->name, i, ext->name, ext->sym_idx, insn_idx); in bpf_program__record_reloc()
4083 if (insn->code == (BPF_JMP | BPF_CALL)) in bpf_program__record_reloc()
4084 reloc_desc->type = RELO_EXTERN_CALL; in bpf_program__record_reloc()
4086 reloc_desc->type = RELO_EXTERN_LD64; in bpf_program__record_reloc()
4087 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4088 reloc_desc->ext_idx = i; in bpf_program__record_reloc()
4092 /* sub-program call relocation */ in bpf_program__record_reloc()
4094 if (insn->src_reg != BPF_PSEUDO_CALL) { in bpf_program__record_reloc()
4095 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); in bpf_program__record_reloc()
4096 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4099 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { in bpf_program__record_reloc()
4102 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4103 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4105 if (sym->st_value % BPF_INSN_SZ) { in bpf_program__record_reloc()
4107 prog->name, sym_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4108 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4110 reloc_desc->type = RELO_CALL; in bpf_program__record_reloc()
4111 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4112 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4118 prog->name, sym_name, shdr_idx); in bpf_program__record_reloc()
4119 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4123 if (sym_is_subprog(sym, obj->efile.text_shndx)) { in bpf_program__record_reloc()
4124 /* global_func: sym->st_value = offset in the section, insn->imm = 0. in bpf_program__record_reloc()
4125 * local_func: sym->st_value = 0, insn->imm = offset in the section. in bpf_program__record_reloc()
4127 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { in bpf_program__record_reloc()
4129 prog->name, sym_name, (size_t)sym->st_value, insn->imm); in bpf_program__record_reloc()
4130 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4133 reloc_desc->type = RELO_SUBPROG_ADDR; in bpf_program__record_reloc()
4134 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4135 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4146 prog->name, sym_name, sym_sec_name); in bpf_program__record_reloc()
4147 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4150 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4151 if (map->libbpf_type != type || in bpf_program__record_reloc()
4152 map->sec_idx != sym->st_shndx || in bpf_program__record_reloc()
4153 map->sec_offset != sym->st_value) in bpf_program__record_reloc()
4156 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4157 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4162 prog->name, sym_sec_name, (size_t)sym->st_value); in bpf_program__record_reloc()
4163 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4165 reloc_desc->type = RELO_LD64; in bpf_program__record_reloc()
4166 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4167 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4168 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ in bpf_program__record_reloc()
4175 prog->name, sym_sec_name); in bpf_program__record_reloc()
4176 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4179 map = &obj->maps[map_idx]; in bpf_program__record_reloc()
4180 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) in bpf_program__record_reloc()
4183 prog->name, map_idx, map->name, map->sec_idx, in bpf_program__record_reloc()
4184 map->sec_offset, insn_idx); in bpf_program__record_reloc()
4189 prog->name, sym_sec_name); in bpf_program__record_reloc()
4190 return -LIBBPF_ERRNO__RELOC; in bpf_program__record_reloc()
4193 reloc_desc->type = RELO_DATA; in bpf_program__record_reloc()
4194 reloc_desc->insn_idx = insn_idx; in bpf_program__record_reloc()
4195 reloc_desc->map_idx = map_idx; in bpf_program__record_reloc()
4196 reloc_desc->sym_off = sym->st_value; in bpf_program__record_reloc()
4202 return insn_idx >= prog->sec_insn_off && in prog_contains_insn()
4203 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; in prog_contains_insn()
4209 int l = 0, r = obj->nr_programs - 1, m; in find_prog_by_sec_insn()
4212 if (!obj->nr_programs) in find_prog_by_sec_insn()
4216 m = l + (r - l + 1) / 2; in find_prog_by_sec_insn()
4217 prog = &obj->programs[m]; in find_prog_by_sec_insn()
4219 if (prog->sec_idx < sec_idx || in find_prog_by_sec_insn()
4220 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) in find_prog_by_sec_insn()
4223 r = m - 1; in find_prog_by_sec_insn()
4228 prog = &obj->programs[l]; in find_prog_by_sec_insn()
4229 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) in find_prog_by_sec_insn()
4238 size_t sec_idx = shdr->sh_info, sym_idx; in bpf_object__collect_prog_relos()
4249 if (sec_idx >= obj->efile.sec_cnt) in bpf_object__collect_prog_relos()
4250 return -EINVAL; in bpf_object__collect_prog_relos()
4255 relo_sec_name = elf_sec_str(obj, shdr->sh_name); in bpf_object__collect_prog_relos()
4258 return -EINVAL; in bpf_object__collect_prog_relos()
4262 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_prog_relos()
4268 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4271 sym_idx = ELF64_R_SYM(rel->r_info); in bpf_object__collect_prog_relos()
4276 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4279 if (sym->st_shndx >= obj->efile.sec_cnt) { in bpf_object__collect_prog_relos()
4281 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); in bpf_object__collect_prog_relos()
4282 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4285 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { in bpf_object__collect_prog_relos()
4287 relo_sec_name, (size_t)rel->r_offset, i); in bpf_object__collect_prog_relos()
4288 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_prog_relos()
4291 insn_idx = rel->r_offset / BPF_INSN_SZ; in bpf_object__collect_prog_relos()
4298 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) in bpf_object__collect_prog_relos()
4299 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); in bpf_object__collect_prog_relos()
4301 sym_name = elf_sym_str(obj, sym->st_name); in bpf_object__collect_prog_relos()
4314 relos = libbpf_reallocarray(prog->reloc_desc, in bpf_object__collect_prog_relos()
4315 prog->nr_reloc + 1, sizeof(*relos)); in bpf_object__collect_prog_relos()
4317 return -ENOMEM; in bpf_object__collect_prog_relos()
4318 prog->reloc_desc = relos; in bpf_object__collect_prog_relos()
4321 insn_idx -= prog->sec_insn_off; in bpf_object__collect_prog_relos()
4322 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], in bpf_object__collect_prog_relos()
4327 prog->nr_reloc++; in bpf_object__collect_prog_relos()
4336 if (!obj->btf) in map_fill_btf_type_info()
4337 return -ENOENT; in map_fill_btf_type_info()
4339 /* if it's BTF-defined map, we don't need to search for type IDs. in map_fill_btf_type_info()
4343 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) in map_fill_btf_type_info()
4351 return -ENOENT; in map_fill_btf_type_info()
4353 id = btf__find_by_name(obj->btf, map->real_name); in map_fill_btf_type_info()
4357 map->btf_key_type_id = 0; in map_fill_btf_type_info()
4358 map->btf_value_type_id = id; in map_fill_btf_type_info()
4374 err = -errno; in bpf_get_map_info_from_fdinfo()
4382 info->type = val; in bpf_get_map_info_from_fdinfo()
4384 info->key_size = val; in bpf_get_map_info_from_fdinfo()
4386 info->value_size = val; in bpf_get_map_info_from_fdinfo()
4388 info->max_entries = val; in bpf_get_map_info_from_fdinfo()
4390 info->map_flags = val; in bpf_get_map_info_from_fdinfo()
4400 return map->autocreate; in bpf_map__autocreate()
4405 if (map->obj->loaded) in bpf_map__set_autocreate()
4406 return libbpf_err(-EBUSY); in bpf_map__set_autocreate()
4408 map->autocreate = autocreate; in bpf_map__set_autocreate()
4427 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) in bpf_map__reuse_fd()
4428 new_name = strdup(map->name); in bpf_map__reuse_fd()
4433 return libbpf_err(-errno); in bpf_map__reuse_fd()
4442 err = -errno; in bpf_map__reuse_fd()
4446 err = zclose(map->fd); in bpf_map__reuse_fd()
4448 err = -errno; in bpf_map__reuse_fd()
4451 free(map->name); in bpf_map__reuse_fd()
4453 map->fd = new_fd; in bpf_map__reuse_fd()
4454 map->name = new_name; in bpf_map__reuse_fd()
4455 map->def.type = info.type; in bpf_map__reuse_fd()
4456 map->def.key_size = info.key_size; in bpf_map__reuse_fd()
4457 map->def.value_size = info.value_size; in bpf_map__reuse_fd()
4458 map->def.max_entries = info.max_entries; in bpf_map__reuse_fd()
4459 map->def.map_flags = info.map_flags; in bpf_map__reuse_fd()
4460 map->btf_key_type_id = info.btf_key_type_id; in bpf_map__reuse_fd()
4461 map->btf_value_type_id = info.btf_value_type_id; in bpf_map__reuse_fd()
4462 map->reused = true; in bpf_map__reuse_fd()
4463 map->map_extra = info.map_extra; in bpf_map__reuse_fd()
4476 return map->def.max_entries; in bpf_map__max_entries()
4481 if (!bpf_map_type__is_map_in_map(map->def.type)) in bpf_map__inner_map()
4484 return map->inner_map; in bpf_map__inner_map()
4489 if (map->obj->loaded) in bpf_map__set_max_entries()
4490 return libbpf_err(-EBUSY); in bpf_map__set_max_entries()
4492 map->def.max_entries = max_entries; in bpf_map__set_max_entries()
4494 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ in bpf_map__set_max_entries()
4496 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); in bpf_map__set_max_entries()
4511 if (obj->gen_loader) in bpf_object__probe_loading()
4529 return -ret; in bpf_object__probe_loading()
4578 ret = -errno; in probe_kern_global_data()
4581 __func__, cp, -ret); in probe_kern_global_data()
4681 BTF_TYPE_DECL_TAG_ENC(1, 2, -1), in probe_kern_btf_decl_tag()
4723 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS) in probe_kern_exp_attach_type()
4735 BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */ in probe_kern_probe_read_kernel()
4758 ret = -errno; in probe_prog_bind_map()
4761 __func__, cp, -ret); in probe_prog_bind_map()
4819 return -errno; in probe_perf_link()
4824 link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL); in probe_perf_link()
4825 err = -errno; /* close() can clobber errno */ in probe_perf_link()
4831 return link_fd < 0 && err == -EBADF; in probe_perf_link()
4850 return -errno; in probe_uprobe_multi_link()
4852 /* Creating uprobe in '/' binary should fail with -EBADF. */ in probe_uprobe_multi_link()
4857 link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts); in probe_uprobe_multi_link()
4858 err = -errno; /* close() can clobber errno */ in probe_uprobe_multi_link()
4864 return link_fd < 0 && err == -EBADF; in probe_uprobe_multi_link()
4952 "memcg-based memory accounting", probe_memcg_account,
4964 "BPF multi-uprobe link support", probe_uprobe_multi_link,
4973 if (obj && obj->gen_loader) in kernel_supports()
4979 if (READ_ONCE(feat->res) == FEAT_UNKNOWN) { in kernel_supports()
4980 ret = feat->probe(); in kernel_supports()
4982 WRITE_ONCE(feat->res, FEAT_SUPPORTED); in kernel_supports()
4984 WRITE_ONCE(feat->res, FEAT_MISSING); in kernel_supports()
4986 pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret); in kernel_supports()
4987 WRITE_ONCE(feat->res, FEAT_MISSING); in kernel_supports()
4991 return READ_ONCE(feat->res) == FEAT_SUPPORTED; in kernel_supports()
5011 return (map_info.type == map->def.type && in map_is_reuse_compat()
5012 map_info.key_size == map->def.key_size && in map_is_reuse_compat()
5013 map_info.value_size == map->def.value_size && in map_is_reuse_compat()
5014 map_info.max_entries == map->def.max_entries && in map_is_reuse_compat()
5015 map_info.map_flags == map->def.map_flags && in map_is_reuse_compat()
5016 map_info.map_extra == map->map_extra); in map_is_reuse_compat()
5025 pin_fd = bpf_obj_get(map->pin_path); in bpf_object__reuse_map()
5027 err = -errno; in bpf_object__reuse_map()
5028 if (err == -ENOENT) { in bpf_object__reuse_map()
5030 map->pin_path); in bpf_object__reuse_map()
5034 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in bpf_object__reuse_map()
5036 map->pin_path, cp); in bpf_object__reuse_map()
5042 map->pin_path); in bpf_object__reuse_map()
5044 return -EINVAL; in bpf_object__reuse_map()
5052 map->pinned = true; in bpf_object__reuse_map()
5053 pr_debug("reused pinned map at '%s'\n", map->pin_path); in bpf_object__reuse_map()
5061 enum libbpf_map_type map_type = map->libbpf_type; in bpf_object__populate_internal_map()
5065 if (obj->gen_loader) { in bpf_object__populate_internal_map()
5066 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, in bpf_object__populate_internal_map()
5067 map->mmaped, map->def.value_size); in bpf_object__populate_internal_map()
5069 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); in bpf_object__populate_internal_map()
5072 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); in bpf_object__populate_internal_map()
5074 err = -errno; in bpf_object__populate_internal_map()
5077 map->name, cp); in bpf_object__populate_internal_map()
5081 /* Freeze .rodata and .kconfig map as read-only from syscall side. */ in bpf_object__populate_internal_map()
5083 err = bpf_map_freeze(map->fd); in bpf_object__populate_internal_map()
5085 err = -errno; in bpf_object__populate_internal_map()
5087 pr_warn("Error freezing map(%s) as read-only: %s\n", in bpf_object__populate_internal_map()
5088 map->name, cp); in bpf_object__populate_internal_map()
5100 struct bpf_map_def *def = &map->def; in bpf_object__create_map()
5105 map_name = map->name; in bpf_object__create_map()
5106 create_attr.map_ifindex = map->map_ifindex; in bpf_object__create_map()
5107 create_attr.map_flags = def->map_flags; in bpf_object__create_map()
5108 create_attr.numa_node = map->numa_node; in bpf_object__create_map()
5109 create_attr.map_extra = map->map_extra; in bpf_object__create_map()
5112 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; in bpf_object__create_map()
5114 if (obj->btf && btf__fd(obj->btf) >= 0) { in bpf_object__create_map()
5115 create_attr.btf_fd = btf__fd(obj->btf); in bpf_object__create_map()
5116 create_attr.btf_key_type_id = map->btf_key_type_id; in bpf_object__create_map()
5117 create_attr.btf_value_type_id = map->btf_value_type_id; in bpf_object__create_map()
5120 if (bpf_map_type__is_map_in_map(def->type)) { in bpf_object__create_map()
5121 if (map->inner_map) { in bpf_object__create_map()
5122 err = bpf_object__create_map(obj, map->inner_map, true); in bpf_object__create_map()
5125 map->name, err); in bpf_object__create_map()
5128 map->inner_map_fd = bpf_map__fd(map->inner_map); in bpf_object__create_map()
5130 if (map->inner_map_fd >= 0) in bpf_object__create_map()
5131 create_attr.inner_map_fd = map->inner_map_fd; in bpf_object__create_map()
5134 switch (def->type) { in bpf_object__create_map()
5151 map->btf_key_type_id = 0; in bpf_object__create_map()
5152 map->btf_value_type_id = 0; in bpf_object__create_map()
5157 if (obj->gen_loader) { in bpf_object__create_map()
5158 bpf_gen__map_create(obj->gen_loader, def->type, map_name, in bpf_object__create_map()
5159 def->key_size, def->value_size, def->max_entries, in bpf_object__create_map()
5160 &create_attr, is_inner ? -1 : map - obj->maps); in bpf_object__create_map()
5162 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. in bpf_object__create_map()
5164 map->fd = 0; in bpf_object__create_map()
5166 map->fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5167 def->key_size, def->value_size, in bpf_object__create_map()
5168 def->max_entries, &create_attr); in bpf_object__create_map()
5170 if (map->fd < 0 && (create_attr.btf_key_type_id || in bpf_object__create_map()
5174 err = -errno; in bpf_object__create_map()
5177 map->name, cp, err); in bpf_object__create_map()
5181 map->btf_key_type_id = 0; in bpf_object__create_map()
5182 map->btf_value_type_id = 0; in bpf_object__create_map()
5183 map->fd = bpf_map_create(def->type, map_name, in bpf_object__create_map()
5184 def->key_size, def->value_size, in bpf_object__create_map()
5185 def->max_entries, &create_attr); in bpf_object__create_map()
5188 err = map->fd < 0 ? -errno : 0; in bpf_object__create_map()
5190 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { in bpf_object__create_map()
5191 if (obj->gen_loader) in bpf_object__create_map()
5192 map->inner_map->fd = -1; in bpf_object__create_map()
5193 bpf_map__destroy(map->inner_map); in bpf_object__create_map()
5194 zfree(&map->inner_map); in bpf_object__create_map()
5206 for (i = 0; i < map->init_slots_sz; i++) { in init_map_in_map_slots()
5207 if (!map->init_slots[i]) in init_map_in_map_slots()
5210 targ_map = map->init_slots[i]; in init_map_in_map_slots()
5213 if (obj->gen_loader) { in init_map_in_map_slots()
5214 bpf_gen__populate_outer_map(obj->gen_loader, in init_map_in_map_slots()
5215 map - obj->maps, i, in init_map_in_map_slots()
5216 targ_map - obj->maps); in init_map_in_map_slots()
5218 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_map_in_map_slots()
5221 err = -errno; in init_map_in_map_slots()
5223 map->name, i, targ_map->name, fd, err); in init_map_in_map_slots()
5227 map->name, i, targ_map->name, fd); in init_map_in_map_slots()
5230 zfree(&map->init_slots); in init_map_in_map_slots()
5231 map->init_slots_sz = 0; in init_map_in_map_slots()
5242 if (obj->gen_loader) in init_prog_array_slots()
5243 return -ENOTSUP; in init_prog_array_slots()
5245 for (i = 0; i < map->init_slots_sz; i++) { in init_prog_array_slots()
5246 if (!map->init_slots[i]) in init_prog_array_slots()
5249 targ_prog = map->init_slots[i]; in init_prog_array_slots()
5252 err = bpf_map_update_elem(map->fd, &i, &fd, 0); in init_prog_array_slots()
5254 err = -errno; in init_prog_array_slots()
5256 map->name, i, targ_prog->name, fd, err); in init_prog_array_slots()
5260 map->name, i, targ_prog->name, fd); in init_prog_array_slots()
5263 zfree(&map->init_slots); in init_prog_array_slots()
5264 map->init_slots_sz = 0; in init_prog_array_slots()
5274 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_init_prog_arrays()
5275 map = &obj->maps[i]; in bpf_object_init_prog_arrays()
5277 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) in bpf_object_init_prog_arrays()
5282 zclose(map->fd); in bpf_object_init_prog_arrays()
5291 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { in map_set_def_max_entries()
5297 map->name, nr_cpus); in map_set_def_max_entries()
5300 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); in map_set_def_max_entries()
5301 map->def.max_entries = nr_cpus; in map_set_def_max_entries()
5316 for (i = 0; i < obj->nr_maps; i++) { in bpf_object__create_maps()
5317 map = &obj->maps[i]; in bpf_object__create_maps()
5321 * loading, if we detect that at least one of the to-be-loaded in bpf_object__create_maps()
5326 * but also it allows to have CO-RE applications that use in bpf_object__create_maps()
5328 * If those global variable-using programs are not loaded at in bpf_object__create_maps()
5334 map->autocreate = false; in bpf_object__create_maps()
5336 if (!map->autocreate) { in bpf_object__create_maps()
5337 pr_debug("map '%s': skipped auto-creating...\n", map->name); in bpf_object__create_maps()
5347 if (map->pin_path) { in bpf_object__create_maps()
5351 map->name); in bpf_object__create_maps()
5354 if (retried && map->fd < 0) { in bpf_object__create_maps()
5356 map->name); in bpf_object__create_maps()
5357 err = -ENOENT; in bpf_object__create_maps()
5362 if (map->fd >= 0) { in bpf_object__create_maps()
5364 map->name, map->fd); in bpf_object__create_maps()
5371 map->name, map->fd); in bpf_object__create_maps()
5376 zclose(map->fd); in bpf_object__create_maps()
5381 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { in bpf_object__create_maps()
5384 zclose(map->fd); in bpf_object__create_maps()
5390 if (map->pin_path && !map->pinned) { in bpf_object__create_maps()
5393 zclose(map->fd); in bpf_object__create_maps()
5394 if (!retried && err == -EEXIST) { in bpf_object__create_maps()
5398 pr_warn("map '%s': failed to auto-pin at '%s': %d\n", in bpf_object__create_maps()
5399 map->name, map->pin_path, err); in bpf_object__create_maps()
5409 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err); in bpf_object__create_maps()
5412 zclose(obj->maps[j].fd); in bpf_object__create_maps()
5426 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5433 for (i = n - 5; i >= 0; i--) { in bpf_core_essential_name_len()
5445 free(cands->cands); in bpf_core_free_cands()
5462 local_t = btf__type_by_id(local_cand->btf, local_cand->id); in bpf_core_add_cands()
5463 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); in bpf_core_add_cands()
5471 targ_name = btf__name_by_offset(targ_btf, t->name_off); in bpf_core_add_cands()
5482 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n", in bpf_core_add_cands()
5483 local_cand->id, btf_kind_str(local_t), in bpf_core_add_cands()
5486 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, in bpf_core_add_cands()
5487 sizeof(*cands->cands)); in bpf_core_add_cands()
5489 return -ENOMEM; in bpf_core_add_cands()
5491 cand = &new_cands[cands->len]; in bpf_core_add_cands()
5492 cand->btf = targ_btf; in bpf_core_add_cands()
5493 cand->id = i; in bpf_core_add_cands()
5495 cands->cands = new_cands; in bpf_core_add_cands()
5496 cands->len++; in bpf_core_add_cands()
5510 if (obj->btf_modules_loaded) in load_module_btfs()
5513 if (obj->gen_loader) in load_module_btfs()
5517 obj->btf_modules_loaded = true; in load_module_btfs()
5532 err = -errno; in load_module_btfs()
5541 err = -errno; in load_module_btfs()
5553 err = -errno; in load_module_btfs()
5558 /* ignore non-module BTFs */ in load_module_btfs()
5564 btf = btf_get_from_fd(fd, obj->btf_vmlinux); in load_module_btfs()
5572 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, in load_module_btfs()
5573 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); in load_module_btfs()
5577 mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; in load_module_btfs()
5579 mod_btf->btf = btf; in load_module_btfs()
5580 mod_btf->id = id; in load_module_btfs()
5581 mod_btf->fd = fd; in load_module_btfs()
5582 mod_btf->name = strdup(name); in load_module_btfs()
5583 if (!mod_btf->name) { in load_module_btfs()
5584 err = -ENOMEM; in load_module_btfs()
5612 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5614 local_name = btf__name_by_offset(local_btf, local_t->name_off); in bpf_core_find_cands()
5616 return ERR_PTR(-EINVAL); in bpf_core_find_cands()
5621 return ERR_PTR(-ENOMEM); in bpf_core_find_cands()
5624 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; in bpf_core_find_cands()
5630 if (cands->len) in bpf_core_find_cands()
5634 if (obj->btf_vmlinux_override) in bpf_core_find_cands()
5642 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_core_find_cands()
5644 obj->btf_modules[i].btf, in bpf_core_find_cands()
5645 obj->btf_modules[i].name, in bpf_core_find_cands()
5646 btf__type_cnt(obj->btf_vmlinux), in bpf_core_find_cands()
5659 * type-based CO-RE relocations and follow slightly different rules than
5660 * field-based relocations. This function assumes that root types were already
5661 * checked for name match. Beyond that initial root-level name check, names
5663 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5666 * - for ENUMs, the size is ignored;
5667 * - for INT, size and signedness are ignored;
5668 * - for ARRAY, dimensionality is ignored, element types are checked for
5670 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5671 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5672 * - FUNC_PROTOs are compatible if they have compatible signature: same
5675 * more experience with using BPF CO-RE relocations.
5704 relos = libbpf_reallocarray(prog->reloc_desc, in record_relo_core()
5705 prog->nr_reloc + 1, sizeof(*relos)); in record_relo_core()
5707 return -ENOMEM; in record_relo_core()
5708 relo = &relos[prog->nr_reloc]; in record_relo_core()
5709 relo->type = RELO_CORE; in record_relo_core()
5710 relo->insn_idx = insn_idx; in record_relo_core()
5711 relo->core_relo = core_relo; in record_relo_core()
5712 prog->reloc_desc = relos; in record_relo_core()
5713 prog->nr_reloc++; in record_relo_core()
5722 for (i = 0; i < prog->nr_reloc; i++) { in find_relo_core()
5723 relo = &prog->reloc_desc[i]; in find_relo_core()
5724 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) in find_relo_core()
5727 return relo->core_relo; in find_relo_core()
5742 const char *prog_name = prog->name; in bpf_core_resolve_relo()
5745 __u32 local_id = relo->type_id; in bpf_core_resolve_relo()
5750 return -EINVAL; in bpf_core_resolve_relo()
5752 local_name = btf__name_by_offset(local_btf, local_type->name_off); in bpf_core_resolve_relo()
5754 return -EINVAL; in bpf_core_resolve_relo()
5756 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && in bpf_core_resolve_relo()
5758 cands = bpf_core_find_cands(prog->obj, local_btf, local_id); in bpf_core_resolve_relo()
5790 if (obj->btf_ext->core_relo_info.len == 0) in bpf_object__relocate_core()
5794 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); in bpf_object__relocate_core()
5795 err = libbpf_get_error(obj->btf_vmlinux_override); in bpf_object__relocate_core()
5808 seg = &obj->btf_ext->core_relo_info; in bpf_object__relocate_core()
5811 sec_idx = seg->sec_idxs[sec_num]; in bpf_object__relocate_core()
5814 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); in bpf_object__relocate_core()
5816 err = -EINVAL; in bpf_object__relocate_core()
5820 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info); in bpf_object__relocate_core()
5823 if (rec->insn_off % BPF_INSN_SZ) in bpf_object__relocate_core()
5824 return -EINVAL; in bpf_object__relocate_core()
5825 insn_idx = rec->insn_off / BPF_INSN_SZ; in bpf_object__relocate_core()
5832 * This is similar to what x86-64 linker does for relocations. in bpf_object__relocate_core()
5836 …pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subpro… in bpf_object__relocate_core()
5840 /* no need to apply CO-RE relocation if the program is in bpf_object__relocate_core()
5843 if (!prog->autoload) in bpf_object__relocate_core()
5847 * program's frame of reference; (sub-)program code is not yet in bpf_object__relocate_core()
5848 * relocated, so it's enough to just subtract in-section offset in bpf_object__relocate_core()
5850 insn_idx = insn_idx - prog->sec_insn_off; in bpf_object__relocate_core()
5851 if (insn_idx >= prog->insns_cnt) in bpf_object__relocate_core()
5852 return -EINVAL; in bpf_object__relocate_core()
5853 insn = &prog->insns[insn_idx]; in bpf_object__relocate_core()
5858 prog->name, i, err); in bpf_object__relocate_core()
5862 if (prog->obj->gen_loader) in bpf_object__relocate_core()
5865 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); in bpf_object__relocate_core()
5868 prog->name, i, err); in bpf_object__relocate_core()
5872 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); in bpf_object__relocate_core()
5875 prog->name, i, insn_idx, err); in bpf_object__relocate_core()
5882 /* obj->btf_vmlinux and module BTFs are freed after object load */ in bpf_object__relocate_core()
5883 btf__free(obj->btf_vmlinux_override); in bpf_object__relocate_core()
5884 obj->btf_vmlinux_override = NULL; in bpf_object__relocate_core()
5888 bpf_core_free_cands(entry->pvalue); in bpf_object__relocate_core()
5906 prog->name, relo_idx, insn_idx, map_idx, map->name); in poison_map_ldimm64()
5910 insn->code = BPF_JMP | BPF_CALL; in poison_map_ldimm64()
5911 insn->dst_reg = 0; in poison_map_ldimm64()
5912 insn->src_reg = 0; in poison_map_ldimm64()
5913 insn->off = 0; in poison_map_ldimm64()
5917 * where lower 123 is map index into obj->maps[] array in poison_map_ldimm64()
5919 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; in poison_map_ldimm64()
5934 prog->name, relo_idx, insn_idx, ext->name); in poison_kfunc_call()
5937 insn->code = BPF_JMP | BPF_CALL; in poison_kfunc_call()
5938 insn->dst_reg = 0; in poison_kfunc_call()
5939 insn->src_reg = 0; in poison_kfunc_call()
5940 insn->off = 0; in poison_kfunc_call()
5944 * where lower 123 is extern index into obj->externs[] array in poison_kfunc_call()
5946 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; in poison_kfunc_call()
5950 * - map references;
5951 * - global variable references;
5952 * - extern references.
5959 for (i = 0; i < prog->nr_reloc; i++) { in bpf_object__relocate_data()
5960 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_object__relocate_data()
5961 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate_data()
5965 switch (relo->type) { in bpf_object__relocate_data()
5967 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
5968 if (obj->gen_loader) { in bpf_object__relocate_data()
5970 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
5971 } else if (map->autocreate) { in bpf_object__relocate_data()
5973 insn[0].imm = map->fd; in bpf_object__relocate_data()
5975 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
5976 relo->map_idx, map); in bpf_object__relocate_data()
5980 map = &obj->maps[relo->map_idx]; in bpf_object__relocate_data()
5981 insn[1].imm = insn[0].imm + relo->sym_off; in bpf_object__relocate_data()
5982 if (obj->gen_loader) { in bpf_object__relocate_data()
5984 insn[0].imm = relo->map_idx; in bpf_object__relocate_data()
5985 } else if (map->autocreate) { in bpf_object__relocate_data()
5987 insn[0].imm = map->fd; in bpf_object__relocate_data()
5989 poison_map_ldimm64(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
5990 relo->map_idx, map); in bpf_object__relocate_data()
5994 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
5995 if (ext->type == EXT_KCFG) { in bpf_object__relocate_data()
5996 if (obj->gen_loader) { in bpf_object__relocate_data()
5998 insn[0].imm = obj->kconfig_map_idx; in bpf_object__relocate_data()
6001 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; in bpf_object__relocate_data()
6003 insn[1].imm = ext->kcfg.data_off; in bpf_object__relocate_data()
6005 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ in bpf_object__relocate_data()
6007 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6008 insn[1].imm = ext->ksym.kernel_btf_obj_fd; in bpf_object__relocate_data()
6010 insn[0].imm = (__u32)ext->ksym.addr; in bpf_object__relocate_data()
6011 insn[1].imm = ext->ksym.addr >> 32; in bpf_object__relocate_data()
6016 ext = &obj->externs[relo->ext_idx]; in bpf_object__relocate_data()
6018 if (ext->is_set) { in bpf_object__relocate_data()
6019 insn[0].imm = ext->ksym.kernel_btf_id; in bpf_object__relocate_data()
6020 insn[0].off = ext->ksym.btf_fd_idx; in bpf_object__relocate_data()
6022 poison_kfunc_call(prog, i, relo->insn_idx, insn, in bpf_object__relocate_data()
6023 relo->ext_idx, ext); in bpf_object__relocate_data()
6029 prog->name, i); in bpf_object__relocate_data()
6030 return -EINVAL; in bpf_object__relocate_data()
6042 prog->name, i, relo->type); in bpf_object__relocate_data()
6043 return -EINVAL; in bpf_object__relocate_data()
6064 sec_idx = ext_info->sec_idxs[sec_num]; in adjust_prog_btf_ext_info()
6066 if (prog->sec_idx != sec_idx) in adjust_prog_btf_ext_info()
6072 if (insn_off < prog->sec_insn_off) in adjust_prog_btf_ext_info()
6074 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) in adjust_prog_btf_ext_info()
6079 copy_end = rec + ext_info->rec_size; in adjust_prog_btf_ext_info()
6083 return -ENOENT; in adjust_prog_btf_ext_info()
6085 /* append func/line info of a given (sub-)program to the main in adjust_prog_btf_ext_info()
6088 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; in adjust_prog_btf_ext_info()
6089 new_sz = old_sz + (copy_end - copy_start); in adjust_prog_btf_ext_info()
6092 return -ENOMEM; in adjust_prog_btf_ext_info()
6094 *prog_rec_cnt = new_sz / ext_info->rec_size; in adjust_prog_btf_ext_info()
6095 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); in adjust_prog_btf_ext_info()
6097 /* Kernel instruction offsets are in units of 8-byte in adjust_prog_btf_ext_info()
6103 off_adj = prog->sub_insn_off - prog->sec_insn_off; in adjust_prog_btf_ext_info()
6106 for (; rec < rec_end; rec += ext_info->rec_size) { in adjust_prog_btf_ext_info()
6111 *prog_rec_sz = ext_info->rec_size; in adjust_prog_btf_ext_info()
6115 return -ENOENT; in adjust_prog_btf_ext_info()
6128 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) in reloc_prog_func_and_line_info()
6134 if (main_prog != prog && !main_prog->func_info) in reloc_prog_func_and_line_info()
6137 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, in reloc_prog_func_and_line_info()
6138 &main_prog->func_info, in reloc_prog_func_and_line_info()
6139 &main_prog->func_info_cnt, in reloc_prog_func_and_line_info()
6140 &main_prog->func_info_rec_size); in reloc_prog_func_and_line_info()
6142 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6144 prog->name, err); in reloc_prog_func_and_line_info()
6147 if (main_prog->func_info) { in reloc_prog_func_and_line_info()
6152 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); in reloc_prog_func_and_line_info()
6157 prog->name); in reloc_prog_func_and_line_info()
6162 if (main_prog != prog && !main_prog->line_info) in reloc_prog_func_and_line_info()
6165 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, in reloc_prog_func_and_line_info()
6166 &main_prog->line_info, in reloc_prog_func_and_line_info()
6167 &main_prog->line_info_cnt, in reloc_prog_func_and_line_info()
6168 &main_prog->line_info_rec_size); in reloc_prog_func_and_line_info()
6170 if (err != -ENOENT) { in reloc_prog_func_and_line_info()
6172 prog->name, err); in reloc_prog_func_and_line_info()
6175 if (main_prog->line_info) { in reloc_prog_func_and_line_info()
6180 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); in reloc_prog_func_and_line_info()
6185 prog->name); in reloc_prog_func_and_line_info()
6195 if (insn_idx == relo->insn_idx) in cmp_relo_by_insn_idx()
6197 return insn_idx < relo->insn_idx ? -1 : 1; in cmp_relo_by_insn_idx()
6202 if (!prog->nr_reloc) in find_prog_insn_relo()
6204 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, in find_prog_insn_relo()
6205 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); in find_prog_insn_relo()
6210 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; in append_subprog_relos()
6216 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); in append_subprog_relos()
6222 return -ENOMEM; in append_subprog_relos()
6223 if (subprog->nr_reloc) in append_subprog_relos()
6224 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, in append_subprog_relos()
6225 sizeof(*relos) * subprog->nr_reloc); in append_subprog_relos()
6227 for (i = main_prog->nr_reloc; i < new_cnt; i++) in append_subprog_relos()
6228 relos[i].insn_idx += subprog->sub_insn_off; in append_subprog_relos()
6232 main_prog->reloc_desc = relos; in append_subprog_relos()
6233 main_prog->nr_reloc = new_cnt; in append_subprog_relos()
6251 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { in bpf_object__reloc_code()
6252 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6257 if (relo && relo->type == RELO_EXTERN_CALL) in bpf_object__reloc_code()
6262 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { in bpf_object__reloc_code()
6264 prog->name, insn_idx, relo->type); in bpf_object__reloc_code()
6265 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6268 /* sub-program instruction index is a combination of in bpf_object__reloc_code()
6271 * call always has imm = -1, but for static functions in bpf_object__reloc_code()
6272 * relocation is against STT_SECTION and insn->imm in bpf_object__reloc_code()
6275 * for subprog addr relocation, the relo->sym_off + insn->imm is in bpf_object__reloc_code()
6278 if (relo->type == RELO_CALL) in bpf_object__reloc_code()
6279 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; in bpf_object__reloc_code()
6281 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; in bpf_object__reloc_code()
6288 prog->name, insn_idx); in bpf_object__reloc_code()
6289 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6294 * offset necessary, insns->imm is relative to in bpf_object__reloc_code()
6297 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; in bpf_object__reloc_code()
6300 /* we enforce that sub-programs should be in .text section */ in bpf_object__reloc_code()
6301 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); in bpf_object__reloc_code()
6303 pr_warn("prog '%s': no .text section found yet sub-program call exists\n", in bpf_object__reloc_code()
6304 prog->name); in bpf_object__reloc_code()
6305 return -LIBBPF_ERRNO__RELOC; in bpf_object__reloc_code()
6311 * - append it at the end of main program's instructions blog; in bpf_object__reloc_code()
6312 * - process is recursively, while current program is put on hold; in bpf_object__reloc_code()
6313 * - if that subprogram calls some other not yet processes in bpf_object__reloc_code()
6318 if (subprog->sub_insn_off == 0) { in bpf_object__reloc_code()
6319 subprog->sub_insn_off = main_prog->insns_cnt; in bpf_object__reloc_code()
6321 new_cnt = main_prog->insns_cnt + subprog->insns_cnt; in bpf_object__reloc_code()
6322 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); in bpf_object__reloc_code()
6324 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); in bpf_object__reloc_code()
6325 return -ENOMEM; in bpf_object__reloc_code()
6327 main_prog->insns = insns; in bpf_object__reloc_code()
6328 main_prog->insns_cnt = new_cnt; in bpf_object__reloc_code()
6330 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, in bpf_object__reloc_code()
6331 subprog->insns_cnt * sizeof(*insns)); in bpf_object__reloc_code()
6333 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", in bpf_object__reloc_code()
6334 main_prog->name, subprog->insns_cnt, subprog->name); in bpf_object__reloc_code()
6345 /* main_prog->insns memory could have been re-allocated, so in bpf_object__reloc_code()
6348 insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; in bpf_object__reloc_code()
6355 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; in bpf_object__reloc_code()
6358 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); in bpf_object__reloc_code()
6365 * Relocate sub-program calls.
6367 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6368 * main prog) is processed separately. For each subprog (non-entry functions,
6377 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6393 * subprog->sub_insn_off as zero at all times and won't be appended to current
6402 * +--------+ +-------+
6403 * | v v |
6404 * +--+---+ +--+-+-+ +---+--+
6406 * +--+---+ +------+ +---+--+
6409 * +---+-------+ +------+----+
6411 * +-----------+ +-----------+
6416 * +-----------+------+
6418 * +-----------+------+
6423 * +-----------+------+------+
6425 * +-----------+------+------+
6434 * +-----------+------+
6436 * +-----------+------+
6439 * +-----------+------+------+
6441 * +-----------+------+------+
6454 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate_calls()
6455 subprog = &obj->programs[i]; in bpf_object__relocate_calls()
6459 subprog->sub_insn_off = 0; in bpf_object__relocate_calls()
6476 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__free_relocs()
6477 prog = &obj->programs[i]; in bpf_object__free_relocs()
6478 zfree(&prog->reloc_desc); in bpf_object__free_relocs()
6479 prog->nr_reloc = 0; in bpf_object__free_relocs()
6488 if (a->insn_idx != b->insn_idx) in cmp_relocs()
6489 return a->insn_idx < b->insn_idx ? -1 : 1; in cmp_relocs()
6492 if (a->type != b->type) in cmp_relocs()
6493 return a->type < b->type ? -1 : 1; in cmp_relocs()
6502 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__sort_relos()
6503 struct bpf_program *p = &obj->programs[i]; in bpf_object__sort_relos()
6505 if (!p->nr_reloc) in bpf_object__sort_relos()
6508 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); in bpf_object__sort_relos()
6519 if (obj->btf_ext) { in bpf_object__relocate()
6522 pr_warn("failed to perform CO-RE relocations: %d\n", in bpf_object__relocate()
6529 /* Before relocating calls pre-process relocations and mark in bpf_object__relocate()
6536 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
6537 prog = &obj->programs[i]; in bpf_object__relocate()
6538 for (j = 0; j < prog->nr_reloc; j++) { in bpf_object__relocate()
6539 struct reloc_desc *relo = &prog->reloc_desc[j]; in bpf_object__relocate()
6540 struct bpf_insn *insn = &prog->insns[relo->insn_idx]; in bpf_object__relocate()
6543 if (relo->type == RELO_SUBPROG_ADDR) in bpf_object__relocate()
6555 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
6556 prog = &obj->programs[i]; in bpf_object__relocate()
6557 /* sub-program's sub-calls are relocated within the context of in bpf_object__relocate()
6562 if (!prog->autoload) in bpf_object__relocate()
6568 prog->name, err); in bpf_object__relocate()
6573 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__relocate()
6574 prog = &obj->programs[i]; in bpf_object__relocate()
6577 if (!prog->autoload) in bpf_object__relocate()
6582 prog->name, err); in bpf_object__relocate()
6610 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) in bpf_object__collect_map_relos()
6611 return -EINVAL; in bpf_object__collect_map_relos()
6612 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); in bpf_object__collect_map_relos()
6614 return -EINVAL; in bpf_object__collect_map_relos()
6616 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_map_relos()
6621 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
6624 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
6627 i, (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_map_relos()
6628 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_map_relos()
6630 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_map_relos()
6632 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n", in bpf_object__collect_map_relos()
6633 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, in bpf_object__collect_map_relos()
6634 (size_t)rel->r_offset, sym->st_name, name); in bpf_object__collect_map_relos()
6636 for (j = 0; j < obj->nr_maps; j++) { in bpf_object__collect_map_relos()
6637 map = &obj->maps[j]; in bpf_object__collect_map_relos()
6638 if (map->sec_idx != obj->efile.btf_maps_shndx) in bpf_object__collect_map_relos()
6641 vi = btf_var_secinfos(sec) + map->btf_var_idx; in bpf_object__collect_map_relos()
6642 if (vi->offset <= rel->r_offset && in bpf_object__collect_map_relos()
6643 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) in bpf_object__collect_map_relos()
6646 if (j == obj->nr_maps) { in bpf_object__collect_map_relos()
6647 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", in bpf_object__collect_map_relos()
6648 i, name, (size_t)rel->r_offset); in bpf_object__collect_map_relos()
6649 return -EINVAL; in bpf_object__collect_map_relos()
6652 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); in bpf_object__collect_map_relos()
6653 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; in bpf_object__collect_map_relos()
6656 if (sym->st_shndx != obj->efile.btf_maps_shndx) { in bpf_object__collect_map_relos()
6657 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", in bpf_object__collect_map_relos()
6659 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
6661 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && in bpf_object__collect_map_relos()
6662 map->def.key_size != sizeof(int)) { in bpf_object__collect_map_relos()
6663 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", in bpf_object__collect_map_relos()
6664 i, map->name, sizeof(int)); in bpf_object__collect_map_relos()
6665 return -EINVAL; in bpf_object__collect_map_relos()
6671 return -ESRCH; in bpf_object__collect_map_relos()
6678 return -ESRCH; in bpf_object__collect_map_relos()
6680 if (targ_prog->sec_idx != sym->st_shndx || in bpf_object__collect_map_relos()
6681 targ_prog->sec_insn_off * 8 != sym->st_value || in bpf_object__collect_map_relos()
6683 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", in bpf_object__collect_map_relos()
6685 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_map_relos()
6688 return -EINVAL; in bpf_object__collect_map_relos()
6691 var = btf__type_by_id(obj->btf, vi->type); in bpf_object__collect_map_relos()
6692 def = skip_mods_and_typedefs(obj->btf, var->type, NULL); in bpf_object__collect_map_relos()
6694 return -EINVAL; in bpf_object__collect_map_relos()
6695 member = btf_members(def) + btf_vlen(def) - 1; in bpf_object__collect_map_relos()
6696 mname = btf__name_by_offset(obj->btf, member->name_off); in bpf_object__collect_map_relos()
6698 return -EINVAL; in bpf_object__collect_map_relos()
6700 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; in bpf_object__collect_map_relos()
6701 if (rel->r_offset - vi->offset < moff) in bpf_object__collect_map_relos()
6702 return -EINVAL; in bpf_object__collect_map_relos()
6704 moff = rel->r_offset - vi->offset - moff; in bpf_object__collect_map_relos()
6709 return -EINVAL; in bpf_object__collect_map_relos()
6711 if (moff >= map->init_slots_sz) { in bpf_object__collect_map_relos()
6713 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); in bpf_object__collect_map_relos()
6715 return -ENOMEM; in bpf_object__collect_map_relos()
6716 map->init_slots = tmp; in bpf_object__collect_map_relos()
6717 memset(map->init_slots + map->init_slots_sz, 0, in bpf_object__collect_map_relos()
6718 (new_sz - map->init_slots_sz) * host_ptr_sz); in bpf_object__collect_map_relos()
6719 map->init_slots_sz = new_sz; in bpf_object__collect_map_relos()
6721 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; in bpf_object__collect_map_relos()
6724 i, map->name, moff, type, name); in bpf_object__collect_map_relos()
6734 for (i = 0; i < obj->efile.sec_cnt; i++) { in bpf_object__collect_relos()
6735 struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; in bpf_object__collect_relos()
6740 if (sec_desc->sec_type != SEC_RELO) in bpf_object__collect_relos()
6743 shdr = sec_desc->shdr; in bpf_object__collect_relos()
6744 data = sec_desc->data; in bpf_object__collect_relos()
6745 idx = shdr->sh_info; in bpf_object__collect_relos()
6747 if (shdr->sh_type != SHT_REL) { in bpf_object__collect_relos()
6749 return -LIBBPF_ERRNO__INTERNAL; in bpf_object__collect_relos()
6752 if (idx == obj->efile.st_ops_shndx || idx == obj->efile.st_ops_link_shndx) in bpf_object__collect_relos()
6754 else if (idx == obj->efile.btf_maps_shndx) in bpf_object__collect_relos()
6768 if (BPF_CLASS(insn->code) == BPF_JMP && in insn_is_helper_call()
6769 BPF_OP(insn->code) == BPF_CALL && in insn_is_helper_call()
6770 BPF_SRC(insn->code) == BPF_K && in insn_is_helper_call()
6771 insn->src_reg == 0 && in insn_is_helper_call()
6772 insn->dst_reg == 0) { in insn_is_helper_call()
6773 *func_id = insn->imm; in insn_is_helper_call()
6781 struct bpf_insn *insn = prog->insns; in bpf_object__sanitize_prog()
6785 if (obj->gen_loader) in bpf_object__sanitize_prog()
6788 for (i = 0; i < prog->insns_cnt; i++, insn++) { in bpf_object__sanitize_prog()
6800 insn->imm = BPF_FUNC_probe_read; in bpf_object__sanitize_prog()
6805 insn->imm = BPF_FUNC_probe_read_str; in bpf_object__sanitize_prog()
6817 /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
6824 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) in libbpf_prepare_prog_load()
6825 opts->expected_attach_type = 0; in libbpf_prepare_prog_load()
6828 opts->prog_flags |= BPF_F_SLEEPABLE; in libbpf_prepare_prog_load()
6830 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) in libbpf_prepare_prog_load()
6831 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; in libbpf_prepare_prog_load()
6834 if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) in libbpf_prepare_prog_load()
6835 prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; in libbpf_prepare_prog_load()
6837 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { in libbpf_prepare_prog_load()
6841 attach_name = strchr(prog->sec_name, '/'); in libbpf_prepare_prog_load()
6852 …pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\… in libbpf_prepare_prog_load()
6853 prog->name); in libbpf_prepare_prog_load()
6854 return -EINVAL; in libbpf_prepare_prog_load()
6863 prog->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
6864 prog->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
6867 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because in libbpf_prepare_prog_load()
6871 opts->attach_btf_obj_fd = btf_obj_fd; in libbpf_prepare_prog_load()
6872 opts->attach_btf_id = btf_type_id; in libbpf_prepare_prog_load()
6890 __u32 log_level = prog->log_level; in bpf_object_load_prog()
6892 if (prog->type == BPF_PROG_TYPE_UNSPEC) { in bpf_object_load_prog()
6898 prog->name, prog->sec_name); in bpf_object_load_prog()
6899 return -EINVAL; in bpf_object_load_prog()
6903 return -EINVAL; in bpf_object_load_prog()
6906 prog_name = prog->name; in bpf_object_load_prog()
6907 load_attr.attach_prog_fd = prog->attach_prog_fd; in bpf_object_load_prog()
6908 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; in bpf_object_load_prog()
6909 load_attr.attach_btf_id = prog->attach_btf_id; in bpf_object_load_prog()
6911 load_attr.prog_ifindex = prog->prog_ifindex; in bpf_object_load_prog()
6917 load_attr.func_info = prog->func_info; in bpf_object_load_prog()
6918 load_attr.func_info_rec_size = prog->func_info_rec_size; in bpf_object_load_prog()
6919 load_attr.func_info_cnt = prog->func_info_cnt; in bpf_object_load_prog()
6920 load_attr.line_info = prog->line_info; in bpf_object_load_prog()
6921 load_attr.line_info_rec_size = prog->line_info_rec_size; in bpf_object_load_prog()
6922 load_attr.line_info_cnt = prog->line_info_cnt; in bpf_object_load_prog()
6925 load_attr.prog_flags = prog->prog_flags; in bpf_object_load_prog()
6926 load_attr.fd_array = obj->fd_array; in bpf_object_load_prog()
6929 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { in bpf_object_load_prog()
6930 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); in bpf_object_load_prog()
6933 prog->name, err); in bpf_object_load_prog()
6936 insns = prog->insns; in bpf_object_load_prog()
6937 insns_cnt = prog->insns_cnt; in bpf_object_load_prog()
6941 load_attr.expected_attach_type = prog->expected_attach_type; in bpf_object_load_prog()
6943 if (obj->gen_loader) { in bpf_object_load_prog()
6944 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, in bpf_object_load_prog()
6946 prog - obj->programs); in bpf_object_load_prog()
6947 *prog_fd = -1; in bpf_object_load_prog()
6958 if (prog->log_buf) { in bpf_object_load_prog()
6959 log_buf = prog->log_buf; in bpf_object_load_prog()
6960 log_buf_size = prog->log_size; in bpf_object_load_prog()
6962 } else if (obj->log_buf) { in bpf_object_load_prog()
6963 log_buf = obj->log_buf; in bpf_object_load_prog()
6964 log_buf_size = obj->log_size; in bpf_object_load_prog()
6970 ret = -ENOMEM; in bpf_object_load_prog()
6983 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); in bpf_object_load_prog()
6986 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
6987 prog->name, log_buf); in bpf_object_load_prog()
6990 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { in bpf_object_load_prog()
6994 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_load_prog()
6995 map = &prog->obj->maps[i]; in bpf_object_load_prog()
6996 if (map->libbpf_type != LIBBPF_MAP_RODATA) in bpf_object_load_prog()
7002 prog->name, map->real_name, cp); in bpf_object_load_prog()
7022 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). in bpf_object_load_prog()
7027 ret = -errno; in bpf_object_load_prog()
7029 /* post-process verifier log to improve error descriptions */ in bpf_object_load_prog()
7033 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp); in bpf_object_load_prog()
7037 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", in bpf_object_load_prog()
7038 prog->name, log_buf); in bpf_object_load_prog()
7054 p = cur - 1; in find_prev_line()
7055 while (p - 1 >= buf && *(p - 1) != '\n') in find_prev_line()
7056 p--; in find_prev_line()
7064 /* size of the remaining log content to the right from the to-be-replaced part */ in patch_log()
7065 size_t rem_sz = (buf + log_sz) - (orig + orig_sz); in patch_log()
7070 * shift log contents by (patch_sz - orig_sz) bytes to the right in patch_log()
7071 * starting from after to-be-replaced part of the log. in patch_log()
7074 * shift log contents by (orig_sz - patch_sz) bytes to the left in patch_log()
7075 * starting from after to-be-replaced part of the log in patch_log()
7084 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; in patch_log()
7086 } else if (patch_sz - orig_sz > buf_sz - log_sz) { in patch_log()
7088 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); in patch_log()
7102 /* Expected log for failed and not properly guarded CO-RE relocation: in fixup_log_failed_core_relo()
7103 * line1 -> 123: (85) call unknown#195896080 in fixup_log_failed_core_relo()
7104 * line2 -> invalid func unknown#195896080 in fixup_log_failed_core_relo()
7105 * line3 -> <anything else or end of buffer> in fixup_log_failed_core_relo()
7108 * instruction index to find corresponding CO-RE relocation and in fixup_log_failed_core_relo()
7110 * failed CO-RE relocation. in fixup_log_failed_core_relo()
7113 struct bpf_core_spec spec; in fixup_log_failed_core_relo() local
7124 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); in fixup_log_failed_core_relo()
7128 spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec); in fixup_log_failed_core_relo()
7130 "%d: <invalid CO-RE relocation>\n" in fixup_log_failed_core_relo()
7131 "failed to resolve CO-RE relocation %s%s\n", in fixup_log_failed_core_relo()
7134 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_failed_core_relo()
7142 * line1 -> 123: (85) call unknown#2001000345 in fixup_log_missing_map_load()
7143 * line2 -> invalid func unknown#2001000345 in fixup_log_missing_map_load()
7144 * line3 -> <anything else or end of buffer> in fixup_log_missing_map_load()
7147 * "345" in "2001000345" is a map index in obj->maps to fetch map name. in fixup_log_missing_map_load()
7149 struct bpf_object *obj = prog->obj; in fixup_log_missing_map_load()
7157 map_idx -= POISON_LDIMM64_MAP_BASE; in fixup_log_missing_map_load()
7158 if (map_idx < 0 || map_idx >= obj->nr_maps) in fixup_log_missing_map_load()
7160 map = &obj->maps[map_idx]; in fixup_log_missing_map_load()
7165 insn_idx, map->name); in fixup_log_missing_map_load()
7167 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_map_load()
7175 * line1 -> 123: (85) call unknown#2002000345 in fixup_log_missing_kfunc_call()
7176 * line2 -> invalid func unknown#2002000345 in fixup_log_missing_kfunc_call()
7177 * line3 -> <anything else or end of buffer> in fixup_log_missing_kfunc_call()
7180 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. in fixup_log_missing_kfunc_call()
7182 struct bpf_object *obj = prog->obj; in fixup_log_missing_kfunc_call()
7190 ext_idx -= POISON_CALL_KFUNC_BASE; in fixup_log_missing_kfunc_call()
7191 if (ext_idx < 0 || ext_idx >= obj->nr_extern) in fixup_log_missing_kfunc_call()
7193 ext = &obj->externs[ext_idx]; in fixup_log_missing_kfunc_call()
7198 insn_idx, ext->name); in fixup_log_missing_kfunc_call()
7200 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); in fixup_log_missing_kfunc_call()
7215 next_line = buf + log_sz - 1; in fixup_verifier_log()
7227 /* failed CO-RE relocation case */ in fixup_verifier_log()
7255 struct bpf_object *obj = prog->obj; in bpf_program_record_relos()
7258 for (i = 0; i < prog->nr_reloc; i++) { in bpf_program_record_relos()
7259 struct reloc_desc *relo = &prog->reloc_desc[i]; in bpf_program_record_relos()
7260 struct extern_desc *ext = &obj->externs[relo->ext_idx]; in bpf_program_record_relos()
7263 switch (relo->type) { in bpf_program_record_relos()
7265 if (ext->type != EXT_KSYM) in bpf_program_record_relos()
7267 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? in bpf_program_record_relos()
7269 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7270 ext->is_weak, !ext->ksym.type_id, in bpf_program_record_relos()
7271 true, kind, relo->insn_idx); in bpf_program_record_relos()
7274 bpf_gen__record_extern(obj->gen_loader, ext->name, in bpf_program_record_relos()
7275 ext->is_weak, false, false, BTF_KIND_FUNC, in bpf_program_record_relos()
7276 relo->insn_idx); in bpf_program_record_relos()
7280 .insn_off = relo->insn_idx * 8, in bpf_program_record_relos()
7281 .type_id = relo->core_relo->type_id, in bpf_program_record_relos()
7282 .access_str_off = relo->core_relo->access_str_off, in bpf_program_record_relos()
7283 .kind = relo->core_relo->kind, in bpf_program_record_relos()
7286 bpf_gen__record_relo_core(obj->gen_loader, &cr); in bpf_program_record_relos()
7303 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7304 prog = &obj->programs[i]; in bpf_object__load_progs()
7310 for (i = 0; i < obj->nr_programs; i++) { in bpf_object__load_progs()
7311 prog = &obj->programs[i]; in bpf_object__load_progs()
7314 if (!prog->autoload) { in bpf_object__load_progs()
7315 pr_debug("prog '%s': skipped loading\n", prog->name); in bpf_object__load_progs()
7318 prog->log_level |= log_level; in bpf_object__load_progs()
7320 if (obj->gen_loader) in bpf_object__load_progs()
7323 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, in bpf_object__load_progs()
7324 obj->license, obj->kern_version, &prog->fd); in bpf_object__load_progs()
7326 pr_warn("prog '%s': failed to load: %d\n", prog->name, err); in bpf_object__load_progs()
7343 prog->sec_def = find_sec_def(prog->sec_name); in bpf_object_init_progs()
7344 if (!prog->sec_def) { in bpf_object_init_progs()
7347 prog->name, prog->sec_name); in bpf_object_init_progs()
7351 prog->type = prog->sec_def->prog_type; in bpf_object_init_progs()
7352 prog->expected_attach_type = prog->sec_def->expected_attach_type; in bpf_object_init_progs()
7357 if (prog->sec_def->prog_setup_fn) { in bpf_object_init_progs()
7358 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); in bpf_object_init_progs()
7361 prog->name, err); in bpf_object_init_progs()
7384 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); in bpf_object_open()
7388 return ERR_PTR(-EINVAL); in bpf_object_open()
7393 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", in bpf_object_open()
7406 return ERR_PTR(-EINVAL); in bpf_object_open()
7408 return ERR_PTR(-EINVAL); in bpf_object_open()
7414 obj->log_buf = log_buf; in bpf_object_open()
7415 obj->log_size = log_size; in bpf_object_open()
7416 obj->log_level = log_level; in bpf_object_open()
7421 err = -ENAMETOOLONG; in bpf_object_open()
7424 obj->btf_custom_path = strdup(btf_tmp_path); in bpf_object_open()
7425 if (!obj->btf_custom_path) { in bpf_object_open()
7426 err = -ENOMEM; in bpf_object_open()
7433 obj->kconfig = strdup(kconfig); in bpf_object_open()
7434 if (!obj->kconfig) { in bpf_object_open()
7435 err = -ENOMEM; in bpf_object_open()
7463 return libbpf_err_ptr(-EINVAL); in bpf_object__open_file()
7480 return libbpf_err_ptr(-EINVAL); in bpf_object__open_mem()
7490 return libbpf_err(-EINVAL); in bpf_object_unload()
7492 for (i = 0; i < obj->nr_maps; i++) { in bpf_object_unload()
7493 zclose(obj->maps[i].fd); in bpf_object_unload()
7494 if (obj->maps[i].st_ops) in bpf_object_unload()
7495 zfree(&obj->maps[i].st_ops->kern_vdata); in bpf_object_unload()
7498 for (i = 0; i < obj->nr_programs; i++) in bpf_object_unload()
7499 bpf_program__unload(&obj->programs[i]); in bpf_object_unload()
7512 m->def.map_flags &= ~BPF_F_MMAPABLE; in bpf_object__sanitize_maps()
7527 err = -errno; in libbpf_kallsyms_parse()
7539 err = -EINVAL; in libbpf_kallsyms_parse()
7560 if (!ext || ext->type != EXT_KSYM) in kallsyms_cb()
7563 t = btf__type_by_id(obj->btf, ext->btf_id); in kallsyms_cb()
7567 if (ext->is_set && ext->ksym.addr != sym_addr) { in kallsyms_cb()
7569 sym_name, ext->ksym.addr, sym_addr); in kallsyms_cb()
7570 return -EINVAL; in kallsyms_cb()
7572 if (!ext->is_set) { in kallsyms_cb()
7573 ext->is_set = true; in kallsyms_cb()
7574 ext->ksym.addr = sym_addr; in kallsyms_cb()
7593 btf = obj->btf_vmlinux; in find_ksym_btf_id()
7597 if (id == -ENOENT) { in find_ksym_btf_id()
7602 for (i = 0; i < obj->btf_module_cnt; i++) { in find_ksym_btf_id()
7604 mod_btf = &obj->btf_modules[i]; in find_ksym_btf_id()
7605 btf = mod_btf->btf; in find_ksym_btf_id()
7607 if (id != -ENOENT) in find_ksym_btf_id()
7612 return -ESRCH; in find_ksym_btf_id()
7629 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); in bpf_object__resolve_ksym_var_btf_id()
7631 if (id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_var_btf_id()
7634 ext->name); in bpf_object__resolve_ksym_var_btf_id()
7639 local_type_id = ext->ksym.type_id; in bpf_object__resolve_ksym_var_btf_id()
7643 targ_var_name = btf__name_by_offset(btf, targ_var->name_off); in bpf_object__resolve_ksym_var_btf_id()
7644 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); in bpf_object__resolve_ksym_var_btf_id()
7646 err = bpf_core_types_are_compat(obj->btf, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
7652 local_type = btf__type_by_id(obj->btf, local_type_id); in bpf_object__resolve_ksym_var_btf_id()
7653 local_name = btf__name_by_offset(obj->btf, local_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
7654 targ_name = btf__name_by_offset(btf, targ_type->name_off); in bpf_object__resolve_ksym_var_btf_id()
7657 ext->name, local_type_id, in bpf_object__resolve_ksym_var_btf_id()
7660 return -EINVAL; in bpf_object__resolve_ksym_var_btf_id()
7663 ext->is_set = true; in bpf_object__resolve_ksym_var_btf_id()
7664 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_var_btf_id()
7665 ext->ksym.kernel_btf_id = id; in bpf_object__resolve_ksym_var_btf_id()
7667 ext->name, id, btf_kind_str(targ_var), targ_var_name); in bpf_object__resolve_ksym_var_btf_id()
7681 local_func_proto_id = ext->ksym.type_id; in bpf_object__resolve_ksym_func_btf_id()
7683 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()
7686 if (kfunc_id == -ESRCH && ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
7689 ext->name); in bpf_object__resolve_ksym_func_btf_id()
7694 kfunc_proto_id = kern_func->type; in bpf_object__resolve_ksym_func_btf_id()
7696 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
7699 if (ext->is_weak) in bpf_object__resolve_ksym_func_btf_id()
7703 ext->name, local_func_proto_id, in bpf_object__resolve_ksym_func_btf_id()
7704 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); in bpf_object__resolve_ksym_func_btf_id()
7705 return -EINVAL; in bpf_object__resolve_ksym_func_btf_id()
7709 if (mod_btf && !mod_btf->fd_array_idx) { in bpf_object__resolve_ksym_func_btf_id()
7710 /* insn->off is s16 */ in bpf_object__resolve_ksym_func_btf_id()
7711 if (obj->fd_array_cnt == INT16_MAX) { in bpf_object__resolve_ksym_func_btf_id()
7713 ext->name, mod_btf->fd_array_idx); in bpf_object__resolve_ksym_func_btf_id()
7714 return -E2BIG; in bpf_object__resolve_ksym_func_btf_id()
7717 if (!obj->fd_array_cnt) in bpf_object__resolve_ksym_func_btf_id()
7718 obj->fd_array_cnt = 1; in bpf_object__resolve_ksym_func_btf_id()
7720 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), in bpf_object__resolve_ksym_func_btf_id()
7721 obj->fd_array_cnt + 1); in bpf_object__resolve_ksym_func_btf_id()
7724 mod_btf->fd_array_idx = obj->fd_array_cnt; in bpf_object__resolve_ksym_func_btf_id()
7726 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; in bpf_object__resolve_ksym_func_btf_id()
7729 ext->is_set = true; in bpf_object__resolve_ksym_func_btf_id()
7730 ext->ksym.kernel_btf_id = kfunc_id; in bpf_object__resolve_ksym_func_btf_id()
7731 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; in bpf_object__resolve_ksym_func_btf_id()
7734 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. in bpf_object__resolve_ksym_func_btf_id()
7735 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. in bpf_object__resolve_ksym_func_btf_id()
7737 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; in bpf_object__resolve_ksym_func_btf_id()
7739 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); in bpf_object__resolve_ksym_func_btf_id()
7750 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_ksyms_btf_id()
7751 ext = &obj->externs[i]; in bpf_object__resolve_ksyms_btf_id()
7752 if (ext->type != EXT_KSYM || !ext->ksym.type_id) in bpf_object__resolve_ksyms_btf_id()
7755 if (obj->gen_loader) { in bpf_object__resolve_ksyms_btf_id()
7756 ext->is_set = true; in bpf_object__resolve_ksyms_btf_id()
7757 ext->ksym.kernel_btf_obj_fd = 0; in bpf_object__resolve_ksyms_btf_id()
7758 ext->ksym.kernel_btf_id = 0; in bpf_object__resolve_ksyms_btf_id()
7761 t = btf__type_by_id(obj->btf, ext->btf_id); in bpf_object__resolve_ksyms_btf_id()
7781 if (obj->nr_extern == 0) in bpf_object__resolve_externs()
7784 if (obj->kconfig_map_idx >= 0) in bpf_object__resolve_externs()
7785 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; in bpf_object__resolve_externs()
7787 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
7788 ext = &obj->externs[i]; in bpf_object__resolve_externs()
7790 if (ext->type == EXT_KSYM) { in bpf_object__resolve_externs()
7791 if (ext->ksym.type_id) in bpf_object__resolve_externs()
7796 } else if (ext->type == EXT_KCFG) { in bpf_object__resolve_externs()
7797 void *ext_ptr = kcfg_data + ext->kcfg.data_off; in bpf_object__resolve_externs()
7801 if (str_has_pfx(ext->name, "CONFIG_")) { in bpf_object__resolve_externs()
7807 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { in bpf_object__resolve_externs()
7810 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); in bpf_object__resolve_externs()
7811 return -EINVAL; in bpf_object__resolve_externs()
7813 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { in bpf_object__resolve_externs()
7815 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { in bpf_object__resolve_externs()
7817 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { in bpf_object__resolve_externs()
7825 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); in bpf_object__resolve_externs()
7826 return -EINVAL; in bpf_object__resolve_externs()
7833 ext->name, (long long)value); in bpf_object__resolve_externs()
7835 pr_warn("extern '%s': unrecognized extern kind\n", ext->name); in bpf_object__resolve_externs()
7836 return -EINVAL; in bpf_object__resolve_externs()
7842 return -EINVAL; in bpf_object__resolve_externs()
7844 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
7845 ext = &obj->externs[i]; in bpf_object__resolve_externs()
7846 if (ext->type == EXT_KCFG && !ext->is_set) { in bpf_object__resolve_externs()
7855 return -EINVAL; in bpf_object__resolve_externs()
7860 return -EINVAL; in bpf_object__resolve_externs()
7865 return -EINVAL; in bpf_object__resolve_externs()
7867 for (i = 0; i < obj->nr_extern; i++) { in bpf_object__resolve_externs()
7868 ext = &obj->externs[i]; in bpf_object__resolve_externs()
7870 if (!ext->is_set && !ext->is_weak) { in bpf_object__resolve_externs()
7871 pr_warn("extern '%s' (strong): not resolved\n", ext->name); in bpf_object__resolve_externs()
7872 return -ESRCH; in bpf_object__resolve_externs()
7873 } else if (!ext->is_set) { in bpf_object__resolve_externs()
7875 ext->name); in bpf_object__resolve_externs()
7887 st_ops = map->st_ops; in bpf_map_prepare_vdata()
7888 for (i = 0; i < btf_vlen(st_ops->type); i++) { in bpf_map_prepare_vdata()
7889 struct bpf_program *prog = st_ops->progs[i]; in bpf_map_prepare_vdata()
7897 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; in bpf_map_prepare_vdata()
7906 for (i = 0; i < obj->nr_maps; i++) in bpf_object_prepare_struct_ops()
7907 if (bpf_map__is_struct_ops(&obj->maps[i])) in bpf_object_prepare_struct_ops()
7908 bpf_map_prepare_vdata(&obj->maps[i]); in bpf_object_prepare_struct_ops()
7918 return libbpf_err(-EINVAL); in bpf_object_load()
7920 if (obj->loaded) { in bpf_object_load()
7921 pr_warn("object '%s': load can't be attempted twice\n", obj->name); in bpf_object_load()
7922 return libbpf_err(-EINVAL); in bpf_object_load()
7925 if (obj->gen_loader) in bpf_object_load()
7926 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); in bpf_object_load()
7930 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); in bpf_object_load()
7935 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); in bpf_object_load()
7940 if (obj->gen_loader) { in bpf_object_load()
7942 if (obj->btf) in bpf_object_load()
7943 btf__set_fd(obj->btf, -1); in bpf_object_load()
7944 for (i = 0; i < obj->nr_maps; i++) in bpf_object_load()
7945 obj->maps[i].fd = -1; in bpf_object_load()
7947 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); in bpf_object_load()
7951 zfree(&obj->fd_array); in bpf_object_load()
7954 for (i = 0; i < obj->btf_module_cnt; i++) { in bpf_object_load()
7955 close(obj->btf_modules[i].fd); in bpf_object_load()
7956 btf__free(obj->btf_modules[i].btf); in bpf_object_load()
7957 free(obj->btf_modules[i].name); in bpf_object_load()
7959 free(obj->btf_modules); in bpf_object_load()
7962 btf__free(obj->btf_vmlinux); in bpf_object_load()
7963 obj->btf_vmlinux = NULL; in bpf_object_load()
7965 obj->loaded = true; /* doesn't matter if successfully or not */ in bpf_object_load()
7972 /* unpin any maps that were auto-pinned during load */ in bpf_object_load()
7973 for (i = 0; i < obj->nr_maps; i++) in bpf_object_load()
7974 if (obj->maps[i].pinned && !obj->maps[i].reused) in bpf_object_load()
7975 bpf_map__unpin(&obj->maps[i], NULL); in bpf_object_load()
7978 pr_warn("failed to load object '%s'\n", obj->path); in bpf_object_load()
7995 return -ENOMEM; in make_parent_dir()
7999 err = -errno; in make_parent_dir()
8003 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in make_parent_dir()
8017 return -EINVAL; in check_path()
8021 return -ENOMEM; in check_path()
8027 err = -errno; in check_path()
8033 err = -EINVAL; in check_path()
8044 if (prog->fd < 0) { in bpf_program__pin()
8045 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); in bpf_program__pin()
8046 return libbpf_err(-EINVAL); in bpf_program__pin()
8057 if (bpf_obj_pin(prog->fd, path)) { in bpf_program__pin()
8058 err = -errno; in bpf_program__pin()
8060 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp); in bpf_program__pin()
8064 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); in bpf_program__pin()
8072 if (prog->fd < 0) { in bpf_program__unpin()
8073 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); in bpf_program__unpin()
8074 return libbpf_err(-EINVAL); in bpf_program__unpin()
8083 return libbpf_err(-errno); in bpf_program__unpin()
8085 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); in bpf_program__unpin()
8096 return libbpf_err(-EINVAL); in bpf_map__pin()
8099 if (map->pin_path) { in bpf_map__pin()
8100 if (path && strcmp(path, map->pin_path)) { in bpf_map__pin()
8102 bpf_map__name(map), map->pin_path, path); in bpf_map__pin()
8103 return libbpf_err(-EINVAL); in bpf_map__pin()
8104 } else if (map->pinned) { in bpf_map__pin()
8105 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", in bpf_map__pin()
8106 bpf_map__name(map), map->pin_path); in bpf_map__pin()
8113 return libbpf_err(-EINVAL); in bpf_map__pin()
8114 } else if (map->pinned) { in bpf_map__pin()
8116 return libbpf_err(-EEXIST); in bpf_map__pin()
8119 map->pin_path = strdup(path); in bpf_map__pin()
8120 if (!map->pin_path) { in bpf_map__pin()
8121 err = -errno; in bpf_map__pin()
8126 err = make_parent_dir(map->pin_path); in bpf_map__pin()
8130 err = check_path(map->pin_path); in bpf_map__pin()
8134 if (bpf_obj_pin(map->fd, map->pin_path)) { in bpf_map__pin()
8135 err = -errno; in bpf_map__pin()
8139 map->pinned = true; in bpf_map__pin()
8140 pr_debug("pinned map '%s'\n", map->pin_path); in bpf_map__pin()
8145 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); in bpf_map__pin()
8156 return libbpf_err(-EINVAL); in bpf_map__unpin()
8159 if (map->pin_path) { in bpf_map__unpin()
8160 if (path && strcmp(path, map->pin_path)) { in bpf_map__unpin()
8162 bpf_map__name(map), map->pin_path, path); in bpf_map__unpin()
8163 return libbpf_err(-EINVAL); in bpf_map__unpin()
8165 path = map->pin_path; in bpf_map__unpin()
8169 return libbpf_err(-EINVAL); in bpf_map__unpin()
8178 return libbpf_err(-errno); in bpf_map__unpin()
8180 map->pinned = false; in bpf_map__unpin()
8193 return libbpf_err(-errno); in bpf_map__set_pin_path()
8196 free(map->pin_path); in bpf_map__set_pin_path()
8197 map->pin_path = new; in bpf_map__set_pin_path()
8206 return map->pin_path; in bpf_map__pin_path()
8211 return map->pinned; in bpf_map__is_pinned()
8230 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8232 if (!obj->loaded) { in bpf_object__pin_maps()
8234 return libbpf_err(-ENOENT); in bpf_object__pin_maps()
8241 if (!map->autocreate) in bpf_object__pin_maps()
8250 } else if (!map->pin_path) { in bpf_object__pin_maps()
8263 if (!map->pin_path) in bpf_object__pin_maps()
8278 return libbpf_err(-ENOENT); in bpf_object__unpin_maps()
8290 } else if (!map->pin_path) { in bpf_object__unpin_maps()
8309 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8311 if (!obj->loaded) { in bpf_object__pin_programs()
8313 return libbpf_err(-ENOENT); in bpf_object__pin_programs()
8317 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__pin_programs()
8330 if (pathname_concat(buf, sizeof(buf), path, prog->name)) in bpf_object__pin_programs()
8345 return libbpf_err(-ENOENT); in bpf_object__unpin_programs()
8350 err = pathname_concat(buf, sizeof(buf), path, prog->name); in bpf_object__unpin_programs()
8396 if (map->inner_map) { in bpf_map__destroy()
8397 bpf_map__destroy(map->inner_map); in bpf_map__destroy()
8398 zfree(&map->inner_map); in bpf_map__destroy()
8401 zfree(&map->init_slots); in bpf_map__destroy()
8402 map->init_slots_sz = 0; in bpf_map__destroy()
8404 if (map->mmaped) { in bpf_map__destroy()
8407 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_map__destroy()
8408 munmap(map->mmaped, mmap_sz); in bpf_map__destroy()
8409 map->mmaped = NULL; in bpf_map__destroy()
8412 if (map->st_ops) { in bpf_map__destroy()
8413 zfree(&map->st_ops->data); in bpf_map__destroy()
8414 zfree(&map->st_ops->progs); in bpf_map__destroy()
8415 zfree(&map->st_ops->kern_func_off); in bpf_map__destroy()
8416 zfree(&map->st_ops); in bpf_map__destroy()
8419 zfree(&map->name); in bpf_map__destroy()
8420 zfree(&map->real_name); in bpf_map__destroy()
8421 zfree(&map->pin_path); in bpf_map__destroy()
8423 if (map->fd >= 0) in bpf_map__destroy()
8424 zclose(map->fd); in bpf_map__destroy()
8434 usdt_manager_free(obj->usdt_man); in bpf_object__close()
8435 obj->usdt_man = NULL; in bpf_object__close()
8437 bpf_gen__free(obj->gen_loader); in bpf_object__close()
8440 btf__free(obj->btf); in bpf_object__close()
8441 btf__free(obj->btf_vmlinux); in bpf_object__close()
8442 btf_ext__free(obj->btf_ext); in bpf_object__close()
8444 for (i = 0; i < obj->nr_maps; i++) in bpf_object__close()
8445 bpf_map__destroy(&obj->maps[i]); in bpf_object__close()
8447 zfree(&obj->btf_custom_path); in bpf_object__close()
8448 zfree(&obj->kconfig); in bpf_object__close()
8450 for (i = 0; i < obj->nr_extern; i++) in bpf_object__close()
8451 zfree(&obj->externs[i].essent_name); in bpf_object__close()
8453 zfree(&obj->externs); in bpf_object__close()
8454 obj->nr_extern = 0; in bpf_object__close()
8456 zfree(&obj->maps); in bpf_object__close()
8457 obj->nr_maps = 0; in bpf_object__close()
8459 if (obj->programs && obj->nr_programs) { in bpf_object__close()
8460 for (i = 0; i < obj->nr_programs; i++) in bpf_object__close()
8461 bpf_program__exit(&obj->programs[i]); in bpf_object__close()
8463 zfree(&obj->programs); in bpf_object__close()
8470 return obj ? obj->name : libbpf_err_ptr(-EINVAL); in bpf_object__name()
8475 return obj ? obj->kern_version : 0; in bpf_object__kversion()
8480 return obj ? obj->btf : NULL; in bpf_object__btf()
8485 return obj->btf ? btf__fd(obj->btf) : -1; in bpf_object__btf_fd()
8490 if (obj->loaded) in bpf_object__set_kversion()
8491 return libbpf_err(-EINVAL); in bpf_object__set_kversion()
8493 obj->kern_version = kern_version; in bpf_object__set_kversion()
8503 return -EFAULT; in bpf_object__gen_loader()
8505 return -EINVAL; in bpf_object__gen_loader()
8508 return -ENOMEM; in bpf_object__gen_loader()
8509 gen->opts = opts; in bpf_object__gen_loader()
8510 obj->gen_loader = gen; in bpf_object__gen_loader()
8518 size_t nr_programs = obj->nr_programs; in __bpf_program__iter()
8526 return forward ? &obj->programs[0] : in __bpf_program__iter()
8527 &obj->programs[nr_programs - 1]; in __bpf_program__iter()
8529 if (p->obj != obj) { in __bpf_program__iter()
8534 idx = (p - obj->programs) + (forward ? 1 : -1); in __bpf_program__iter()
8535 if (idx >= obj->nr_programs || idx < 0) in __bpf_program__iter()
8537 return &obj->programs[idx]; in __bpf_program__iter()
8566 prog->prog_ifindex = ifindex; in bpf_program__set_ifindex()
8571 return prog->name; in bpf_program__name()
8576 return prog->sec_name; in bpf_program__section_name()
8581 return prog->autoload; in bpf_program__autoload()
8586 if (prog->obj->loaded) in bpf_program__set_autoload()
8587 return libbpf_err(-EINVAL); in bpf_program__set_autoload()
8589 prog->autoload = autoload; in bpf_program__set_autoload()
8595 return prog->autoattach; in bpf_program__autoattach()
8600 prog->autoattach = autoattach; in bpf_program__set_autoattach()
8605 return prog->insns; in bpf_program__insns()
8610 return prog->insns_cnt; in bpf_program__insn_cnt()
8618 if (prog->obj->loaded) in bpf_program__set_insns()
8619 return -EBUSY; in bpf_program__set_insns()
8621 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); in bpf_program__set_insns()
8624 pr_warn("prog '%s': failed to realloc prog code\n", prog->name); in bpf_program__set_insns()
8625 return -ENOMEM; in bpf_program__set_insns()
8629 prog->insns = insns; in bpf_program__set_insns()
8630 prog->insns_cnt = new_insn_cnt; in bpf_program__set_insns()
8637 return libbpf_err(-EINVAL); in bpf_program__fd()
8639 if (prog->fd < 0) in bpf_program__fd()
8640 return libbpf_err(-ENOENT); in bpf_program__fd()
8642 return prog->fd; in bpf_program__fd()
8650 return prog->type; in bpf_program__type()
8661 if (prog->obj->loaded) in bpf_program__set_type()
8662 return libbpf_err(-EBUSY); in bpf_program__set_type()
8665 if (prog->type == type) in bpf_program__set_type()
8668 prog->type = type; in bpf_program__set_type()
8672 * fallback handler, which by definition is program type-agnostic and in bpf_program__set_type()
8673 * is a catch-all custom handler, optionally set by the application, in bpf_program__set_type()
8676 if (prog->sec_def != &custom_fallback_def) in bpf_program__set_type()
8677 prog->sec_def = NULL; in bpf_program__set_type()
8686 return prog->expected_attach_type; in bpf_program__expected_attach_type()
8692 if (prog->obj->loaded) in bpf_program__set_expected_attach_type()
8693 return libbpf_err(-EBUSY); in bpf_program__set_expected_attach_type()
8695 prog->expected_attach_type = type; in bpf_program__set_expected_attach_type()
8701 return prog->prog_flags; in bpf_program__flags()
8706 if (prog->obj->loaded) in bpf_program__set_flags()
8707 return libbpf_err(-EBUSY); in bpf_program__set_flags()
8709 prog->prog_flags = flags; in bpf_program__set_flags()
8715 return prog->log_level; in bpf_program__log_level()
8720 if (prog->obj->loaded) in bpf_program__set_log_level()
8721 return libbpf_err(-EBUSY); in bpf_program__set_log_level()
8723 prog->log_level = log_level; in bpf_program__set_log_level()
8729 *log_size = prog->log_size; in bpf_program__log_buf()
8730 return prog->log_buf; in bpf_program__log_buf()
8736 return -EINVAL; in bpf_program__set_log_buf()
8737 if (prog->log_size > UINT_MAX) in bpf_program__set_log_buf()
8738 return -EINVAL; in bpf_program__set_log_buf()
8739 if (prog->obj->loaded) in bpf_program__set_log_buf()
8740 return -EBUSY; in bpf_program__set_log_buf()
8742 prog->log_buf = log_buf; in bpf_program__set_log_buf()
8743 prog->log_size = log_size; in bpf_program__set_log_buf()
8871 return libbpf_err(-EINVAL); in libbpf_register_prog_handler()
8874 return libbpf_err(-E2BIG); in libbpf_register_prog_handler()
8880 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
8886 return libbpf_err(-EBUSY); in libbpf_register_prog_handler()
8891 sec_def->sec = sec ? strdup(sec) : NULL; in libbpf_register_prog_handler()
8892 if (sec && !sec_def->sec) in libbpf_register_prog_handler()
8893 return libbpf_err(-ENOMEM); in libbpf_register_prog_handler()
8895 sec_def->prog_type = prog_type; in libbpf_register_prog_handler()
8896 sec_def->expected_attach_type = exp_attach_type; in libbpf_register_prog_handler()
8897 sec_def->cookie = OPTS_GET(opts, cookie, 0); in libbpf_register_prog_handler()
8899 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); in libbpf_register_prog_handler()
8900 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); in libbpf_register_prog_handler()
8901 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); in libbpf_register_prog_handler()
8903 sec_def->handler_id = ++last_custom_sec_def_handler_id; in libbpf_register_prog_handler()
8910 return sec_def->handler_id; in libbpf_register_prog_handler()
8919 return libbpf_err(-EINVAL); in libbpf_unregister_prog_handler()
8933 return libbpf_err(-ENOENT); in libbpf_unregister_prog_handler()
8937 custom_sec_defs[i - 1] = custom_sec_defs[i]; in libbpf_unregister_prog_handler()
8938 custom_sec_def_cnt--; in libbpf_unregister_prog_handler()
8954 size_t len = strlen(sec_def->sec); in sec_def_matches()
8957 if (sec_def->sec[len - 1] == '/') { in sec_def_matches()
8958 if (str_has_pfx(sec_name, sec_def->sec)) in sec_def_matches()
8964 * well-formed SEC("type/extras") with proper '/' separator in sec_def_matches()
8966 if (sec_def->sec[len - 1] == '+') { in sec_def_matches()
8967 len--; in sec_def_matches()
8969 if (strncmp(sec_name, sec_def->sec, len) != 0) in sec_def_matches()
8977 return strcmp(sec_name, sec_def->sec) == 0; in sec_def_matches()
9022 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_get_type_names()
9025 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_get_type_names()
9047 return libbpf_err(-EINVAL); in libbpf_prog_type_by_name()
9051 *prog_type = sec_def->prog_type; in libbpf_prog_type_by_name()
9052 *expected_attach_type = sec_def->expected_attach_type; in libbpf_prog_type_by_name()
9063 return libbpf_err(-ESRCH); in libbpf_prog_type_by_name()
9105 for (i = 0; i < obj->nr_maps; i++) { in find_struct_ops_map_by_offset()
9106 map = &obj->maps[i]; in find_struct_ops_map_by_offset()
9109 if (map->sec_idx == sec_idx && in find_struct_ops_map_by_offset()
9110 map->sec_offset <= offset && in find_struct_ops_map_by_offset()
9111 offset - map->sec_offset < map->def.value_size) in find_struct_ops_map_by_offset()
9118 /* Collect the reloc from ELF and populate the st_ops->progs[] */
9135 btf = obj->btf; in bpf_object__collect_st_ops_relos()
9136 nrels = shdr->sh_size / shdr->sh_entsize; in bpf_object__collect_st_ops_relos()
9141 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9144 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9147 (size_t)ELF64_R_SYM(rel->r_info)); in bpf_object__collect_st_ops_relos()
9148 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9151 name = elf_sym_str(obj, sym->st_name) ?: "<?>"; in bpf_object__collect_st_ops_relos()
9152 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); in bpf_object__collect_st_ops_relos()
9154 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", in bpf_object__collect_st_ops_relos()
9155 (size_t)rel->r_offset); in bpf_object__collect_st_ops_relos()
9156 return -EINVAL; in bpf_object__collect_st_ops_relos()
9159 moff = rel->r_offset - map->sec_offset; in bpf_object__collect_st_ops_relos()
9160 shdr_idx = sym->st_shndx; in bpf_object__collect_st_ops_relos()
9161 st_ops = map->st_ops; in bpf_object__collect_st_ops_relos()
9162 …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()
9163 map->name, in bpf_object__collect_st_ops_relos()
9164 (long long)(rel->r_info >> 32), in bpf_object__collect_st_ops_relos()
9165 (long long)sym->st_value, in bpf_object__collect_st_ops_relos()
9166 shdr_idx, (size_t)rel->r_offset, in bpf_object__collect_st_ops_relos()
9167 map->sec_offset, sym->st_name, name); in bpf_object__collect_st_ops_relos()
9170 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()
9171 map->name, (size_t)rel->r_offset, shdr_idx); in bpf_object__collect_st_ops_relos()
9172 return -LIBBPF_ERRNO__RELOC; in bpf_object__collect_st_ops_relos()
9174 if (sym->st_value % BPF_INSN_SZ) { in bpf_object__collect_st_ops_relos()
9176 map->name, (unsigned long long)sym->st_value); in bpf_object__collect_st_ops_relos()
9177 return -LIBBPF_ERRNO__FORMAT; in bpf_object__collect_st_ops_relos()
9179 insn_idx = sym->st_value / BPF_INSN_SZ; in bpf_object__collect_st_ops_relos()
9181 member = find_member_by_offset(st_ops->type, moff * 8); in bpf_object__collect_st_ops_relos()
9184 map->name, moff); in bpf_object__collect_st_ops_relos()
9185 return -EINVAL; in bpf_object__collect_st_ops_relos()
9187 member_idx = member - btf_members(st_ops->type); in bpf_object__collect_st_ops_relos()
9188 name = btf__name_by_offset(btf, member->name_off); in bpf_object__collect_st_ops_relos()
9190 if (!resolve_func_ptr(btf, member->type, NULL)) { in bpf_object__collect_st_ops_relos()
9192 map->name, name); in bpf_object__collect_st_ops_relos()
9193 return -EINVAL; in bpf_object__collect_st_ops_relos()
9199 map->name, shdr_idx, name); in bpf_object__collect_st_ops_relos()
9200 return -EINVAL; in bpf_object__collect_st_ops_relos()
9204 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { in bpf_object__collect_st_ops_relos()
9206 map->name, prog->name); in bpf_object__collect_st_ops_relos()
9207 return -EINVAL; in bpf_object__collect_st_ops_relos()
9213 if (!prog->attach_btf_id) { in bpf_object__collect_st_ops_relos()
9214 prog->attach_btf_id = st_ops->type_id; in bpf_object__collect_st_ops_relos()
9215 prog->expected_attach_type = member_idx; in bpf_object__collect_st_ops_relos()
9218 /* struct_ops BPF prog can be re-used between multiple in bpf_object__collect_st_ops_relos()
9223 if (prog->attach_btf_id != st_ops->type_id || in bpf_object__collect_st_ops_relos()
9224 prog->expected_attach_type != member_idx) { in bpf_object__collect_st_ops_relos()
9226 map->name, prog->name, prog->sec_name, prog->type, in bpf_object__collect_st_ops_relos()
9227 prog->attach_btf_id, prog->expected_attach_type, name); in bpf_object__collect_st_ops_relos()
9228 return -EINVAL; in bpf_object__collect_st_ops_relos()
9231 st_ops->progs[member_idx] = prog; in bpf_object__collect_st_ops_relos()
9278 return -ENAMETOOLONG; in find_btf_by_prefix_kind()
9328 err = -EINVAL; in libbpf_find_prog_btf_id()
9355 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type); in find_kernel_btf_id()
9361 if (ret != -ENOENT) in find_kernel_btf_id()
9368 for (i = 0; i < obj->btf_module_cnt; i++) { in find_kernel_btf_id()
9369 const struct module_btf *mod = &obj->btf_modules[i]; in find_kernel_btf_id()
9371 ret = find_attach_btf_id(mod->btf, attach_name, attach_type); in find_kernel_btf_id()
9373 *btf_obj_fd = mod->fd; in find_kernel_btf_id()
9377 if (ret == -ENOENT) in find_kernel_btf_id()
9383 return -ESRCH; in find_kernel_btf_id()
9389 enum bpf_attach_type attach_type = prog->expected_attach_type; in libbpf_find_attach_btf_id()
9390 __u32 attach_prog_fd = prog->attach_prog_fd; in libbpf_find_attach_btf_id()
9394 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { in libbpf_find_attach_btf_id()
9396 pr_warn("prog '%s': attach program FD is not set\n", prog->name); in libbpf_find_attach_btf_id()
9397 return -EINVAL; in libbpf_find_attach_btf_id()
9402 prog->name, attach_prog_fd, attach_name, err); in libbpf_find_attach_btf_id()
9411 if (prog->obj->gen_loader) { in libbpf_find_attach_btf_id()
9412 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); in libbpf_find_attach_btf_id()
9416 err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id); in libbpf_find_attach_btf_id()
9420 prog->name, attach_name, err); in libbpf_find_attach_btf_id()
9433 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
9444 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
9447 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) in libbpf_attach_type_by_name()
9448 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
9449 if (!(sec_def->cookie & SEC_ATTACHABLE)) in libbpf_attach_type_by_name()
9450 return libbpf_err(-EINVAL); in libbpf_attach_type_by_name()
9452 *attach_type = sec_def->expected_attach_type; in libbpf_attach_type_by_name()
9458 return map ? map->fd : libbpf_err(-EINVAL); in bpf_map__fd()
9464 * their user-visible name differs from kernel-visible name. Users see in map_uses_real_name()
9469 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) in map_uses_real_name()
9471 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) in map_uses_real_name()
9482 return map->real_name; in bpf_map__name()
9484 return map->name; in bpf_map__name()
9489 return map->def.type; in bpf_map__type()
9494 if (map->fd >= 0) in bpf_map__set_type()
9495 return libbpf_err(-EBUSY); in bpf_map__set_type()
9496 map->def.type = type; in bpf_map__set_type()
9502 return map->def.map_flags; in bpf_map__map_flags()
9507 if (map->fd >= 0) in bpf_map__set_map_flags()
9508 return libbpf_err(-EBUSY); in bpf_map__set_map_flags()
9509 map->def.map_flags = flags; in bpf_map__set_map_flags()
9515 return map->map_extra; in bpf_map__map_extra()
9520 if (map->fd >= 0) in bpf_map__set_map_extra()
9521 return libbpf_err(-EBUSY); in bpf_map__set_map_extra()
9522 map->map_extra = map_extra; in bpf_map__set_map_extra()
9528 return map->numa_node; in bpf_map__numa_node()
9533 if (map->fd >= 0) in bpf_map__set_numa_node()
9534 return libbpf_err(-EBUSY); in bpf_map__set_numa_node()
9535 map->numa_node = numa_node; in bpf_map__set_numa_node()
9541 return map->def.key_size; in bpf_map__key_size()
9546 if (map->fd >= 0) in bpf_map__set_key_size()
9547 return libbpf_err(-EBUSY); in bpf_map__set_key_size()
9548 map->def.key_size = size; in bpf_map__set_key_size()
9554 return map->def.value_size; in bpf_map__value_size()
9568 btf = bpf_object__btf(map->obj); in map_btf_datasec_resize()
9570 return -ENOENT; in map_btf_datasec_resize()
9577 return -EINVAL; in map_btf_datasec_resize()
9585 return -EINVAL; in map_btf_datasec_resize()
9589 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
9590 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
9591 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); in map_btf_datasec_resize()
9595 return -EINVAL; in map_btf_datasec_resize()
9600 element_sz = btf__resolve_size(btf, array->type); in map_btf_datasec_resize()
9601 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { in map_btf_datasec_resize()
9604 return -EINVAL; in map_btf_datasec_resize()
9608 nr_elements = (size - var->offset) / element_sz; in map_btf_datasec_resize()
9609 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); in map_btf_datasec_resize()
9616 datasec_type = btf_type_by_id(btf, map->btf_value_type_id); in map_btf_datasec_resize()
9617 var = &btf_var_secinfos(datasec_type)[vlen - 1]; in map_btf_datasec_resize()
9618 var_type = btf_type_by_id(btf, var->type); in map_btf_datasec_resize()
9621 datasec_type->size = size; in map_btf_datasec_resize()
9622 var->size = size - var->offset; in map_btf_datasec_resize()
9623 var_type->type = new_array_id; in map_btf_datasec_resize()
9630 if (map->fd >= 0) in bpf_map__set_value_size()
9631 return libbpf_err(-EBUSY); in bpf_map__set_value_size()
9633 if (map->mmaped) { in bpf_map__set_value_size()
9637 mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_map__set_value_size()
9638 mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries); in bpf_map__set_value_size()
9641 pr_warn("map '%s': failed to resize memory-mapped region: %d\n", in bpf_map__set_value_size()
9646 if (err && err != -ENOENT) { in bpf_map__set_value_size()
9649 map->btf_value_type_id = 0; in bpf_map__set_value_size()
9650 map->btf_key_type_id = 0; in bpf_map__set_value_size()
9654 map->def.value_size = size; in bpf_map__set_value_size()
9660 return map ? map->btf_key_type_id : 0; in bpf_map__btf_key_type_id()
9665 return map ? map->btf_value_type_id : 0; in bpf_map__btf_value_type_id()
9671 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG || in bpf_map__set_initial_value()
9672 size != map->def.value_size || map->fd >= 0) in bpf_map__set_initial_value()
9673 return libbpf_err(-EINVAL); in bpf_map__set_initial_value()
9675 memcpy(map->mmaped, data, size); in bpf_map__set_initial_value()
9681 if (!map->mmaped) in bpf_map__initial_value()
9683 *psize = map->def.value_size; in bpf_map__initial_value()
9684 return map->mmaped; in bpf_map__initial_value()
9689 return map->libbpf_type != LIBBPF_MAP_UNSPEC; in bpf_map__is_internal()
9694 return map->map_ifindex; in bpf_map__ifindex()
9699 if (map->fd >= 0) in bpf_map__set_ifindex()
9700 return libbpf_err(-EBUSY); in bpf_map__set_ifindex()
9701 map->map_ifindex = ifindex; in bpf_map__set_ifindex()
9707 if (!bpf_map_type__is_map_in_map(map->def.type)) { in bpf_map__set_inner_map_fd()
9709 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
9711 if (map->inner_map_fd != -1) { in bpf_map__set_inner_map_fd()
9713 return libbpf_err(-EINVAL); in bpf_map__set_inner_map_fd()
9715 if (map->inner_map) { in bpf_map__set_inner_map_fd()
9716 bpf_map__destroy(map->inner_map); in bpf_map__set_inner_map_fd()
9717 zfree(&map->inner_map); in bpf_map__set_inner_map_fd()
9719 map->inner_map_fd = fd; in bpf_map__set_inner_map_fd()
9729 if (!obj || !obj->maps) in __bpf_map__iter()
9732 s = obj->maps; in __bpf_map__iter()
9733 e = obj->maps + obj->nr_maps; in __bpf_map__iter()
9741 idx = (m - obj->maps) + i; in __bpf_map__iter()
9742 if (idx >= obj->nr_maps || idx < 0) in __bpf_map__iter()
9744 return &obj->maps[idx]; in __bpf_map__iter()
9751 return obj->maps; in bpf_object__next_map()
9760 if (!obj->nr_maps) in bpf_object__prev_map()
9762 return obj->maps + obj->nr_maps - 1; in bpf_object__prev_map()
9765 return __bpf_map__iter(next, obj, -1); in bpf_object__prev_map()
9779 if (pos->real_name && strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
9785 if (strcmp(pos->real_name, name) == 0) in bpf_object__find_map_by_name()
9789 if (strcmp(pos->name, name) == 0) in bpf_object__find_map_by_name()
9804 if (map->fd <= 0) in validate_map_op()
9805 return -ENOENT; in validate_map_op()
9807 if (map->def.key_size != key_sz) { in validate_map_op()
9809 map->name, key_sz, map->def.key_size); in validate_map_op()
9810 return -EINVAL; in validate_map_op()
9816 switch (map->def.type) { in validate_map_op()
9822 size_t elem_sz = roundup(map->def.value_size, 8); in validate_map_op()
9825 … pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n", in validate_map_op()
9826 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); in validate_map_op()
9827 return -EINVAL; in validate_map_op()
9832 if (map->def.value_size != value_sz) { in validate_map_op()
9834 map->name, value_sz, map->def.value_size); in validate_map_op()
9835 return -EINVAL; in validate_map_op()
9852 return bpf_map_lookup_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_elem()
9865 return bpf_map_update_elem(map->fd, key, value, flags); in bpf_map__update_elem()
9877 return bpf_map_delete_elem_flags(map->fd, key, flags); in bpf_map__delete_elem()
9890 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); in bpf_map__lookup_and_delete_elem()
9902 return bpf_map_get_next_key(map->fd, cur_key, next_key); in bpf_map__get_next_key()
9911 errno = -PTR_ERR(ptr); in libbpf_get_error()
9918 return -errno; in libbpf_get_error()
9942 link->disconnected = true; in bpf_link__disconnect()
9952 if (!link->disconnected && link->detach) in bpf_link__destroy()
9953 err = link->detach(link); in bpf_link__destroy()
9954 if (link->pin_path) in bpf_link__destroy()
9955 free(link->pin_path); in bpf_link__destroy()
9956 if (link->dealloc) in bpf_link__destroy()
9957 link->dealloc(link); in bpf_link__destroy()
9966 return link->fd; in bpf_link__fd()
9971 return link->pin_path; in bpf_link__pin_path()
9976 return libbpf_err_errno(close(link->fd)); in bpf_link__detach_fd()
9986 fd = -errno; in bpf_link__open()
9994 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
9996 link->detach = &bpf_link__detach_fd; in bpf_link__open()
9997 link->fd = fd; in bpf_link__open()
9999 link->pin_path = strdup(path); in bpf_link__open()
10000 if (!link->pin_path) { in bpf_link__open()
10002 return libbpf_err_ptr(-ENOMEM); in bpf_link__open()
10010 return bpf_link_detach(link->fd) ? -errno : 0; in bpf_link__detach()
10017 if (link->pin_path) in bpf_link__pin()
10018 return libbpf_err(-EBUSY); in bpf_link__pin()
10026 link->pin_path = strdup(path); in bpf_link__pin()
10027 if (!link->pin_path) in bpf_link__pin()
10028 return libbpf_err(-ENOMEM); in bpf_link__pin()
10030 if (bpf_obj_pin(link->fd, link->pin_path)) { in bpf_link__pin()
10031 err = -errno; in bpf_link__pin()
10032 zfree(&link->pin_path); in bpf_link__pin()
10036 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); in bpf_link__pin()
10044 if (!link->pin_path) in bpf_link__unpin()
10045 return libbpf_err(-EINVAL); in bpf_link__unpin()
10047 err = unlink(link->pin_path); in bpf_link__unpin()
10049 return -errno; in bpf_link__unpin()
10051 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); in bpf_link__unpin()
10052 zfree(&link->pin_path); in bpf_link__unpin()
10073 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) in bpf_link_perf_detach()
10074 err = -errno; in bpf_link_perf_detach()
10076 if (perf_link->perf_event_fd != link->fd) in bpf_link_perf_detach()
10077 close(perf_link->perf_event_fd); in bpf_link_perf_detach()
10078 close(link->fd); in bpf_link_perf_detach()
10081 if (perf_link->legacy_probe_name) { in bpf_link_perf_detach()
10082 if (perf_link->legacy_is_kprobe) { in bpf_link_perf_detach()
10083 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10084 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10086 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, in bpf_link_perf_detach()
10087 perf_link->legacy_is_retprobe); in bpf_link_perf_detach()
10098 free(perf_link->legacy_probe_name); in bpf_link_perf_dealloc()
10107 int prog_fd, link_fd = -1, err; in bpf_program__attach_perf_event_opts()
10111 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10115 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10116 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10121 prog->name); in bpf_program__attach_perf_event_opts()
10122 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_perf_event_opts()
10127 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_perf_event_opts()
10128 link->link.detach = &bpf_link_perf_detach; in bpf_program__attach_perf_event_opts()
10129 link->link.dealloc = &bpf_link_perf_dealloc; in bpf_program__attach_perf_event_opts()
10130 link->perf_event_fd = pfd; in bpf_program__attach_perf_event_opts()
10133 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { in bpf_program__attach_perf_event_opts()
10139 err = -errno; in bpf_program__attach_perf_event_opts()
10141 prog->name, pfd, in bpf_program__attach_perf_event_opts()
10145 link->link.fd = link_fd; in bpf_program__attach_perf_event_opts()
10148 pr_warn("prog '%s': user context value is not supported\n", prog->name); in bpf_program__attach_perf_event_opts()
10149 err = -EOPNOTSUPP; in bpf_program__attach_perf_event_opts()
10154 err = -errno; in bpf_program__attach_perf_event_opts()
10156 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_perf_event_opts()
10157 if (err == -EPROTO) in bpf_program__attach_perf_event_opts()
10159 prog->name, pfd); in bpf_program__attach_perf_event_opts()
10162 link->link.fd = pfd; in bpf_program__attach_perf_event_opts()
10165 err = -errno; in bpf_program__attach_perf_event_opts()
10167 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_perf_event_opts()
10171 return &link->link; in bpf_program__attach_perf_event_opts()
10185 * this function is expected to parse integer in the range of [0, 2^31-1] from
10197 err = -errno; in parse_uint_from_file()
10204 err = err == EOF ? -EIO : -errno; in parse_uint_from_file()
10254 return -EINVAL; in perf_event_open_probe()
10286 pid < 0 ? -1 : pid /* pid */, in perf_event_open_probe()
10287 pid == -1 ? 0 : -1 /* cpu */, in perf_event_open_probe()
10288 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_probe()
10289 return pfd >= 0 ? pfd : -errno; in perf_event_open_probe()
10303 return -EINVAL; in append_to_file()
10307 return -errno; in append_to_file()
10310 err = -errno; in append_to_file()
10321 static int has_debugfs = -1; in use_debugfs()
10383 return append_to_file(tracefs_kprobe_events(), "-:%s/%s", in remove_kprobe_event_legacy()
10427 pid < 0 ? -1 : pid, /* pid */ in perf_event_kprobe_open_legacy()
10428 pid == -1 ? 0 : -1, /* cpu */ in perf_event_kprobe_open_legacy()
10429 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_kprobe_open_legacy()
10431 err = -errno; in perf_event_kprobe_open_legacy()
10461 return "riscv"; in arch_specific_syscall_pfx()
10517 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
10532 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
10536 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_kprobe_opts()
10537 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_kprobe_opts()
10542 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_opts()
10548 -1 /* pid */, 0 /* ref_ctr_off */); in bpf_program__attach_kprobe_opts()
10557 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_kprobe_opts()
10560 offset, -1 /* pid */); in bpf_program__attach_kprobe_opts()
10563 err = -errno; in bpf_program__attach_kprobe_opts()
10565 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
10575 prog->name, retprobe ? "kretprobe" : "kprobe", in bpf_program__attach_kprobe_opts()
10583 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_kprobe_opts()
10584 perf_link->legacy_is_kprobe = true; in bpf_program__attach_kprobe_opts()
10585 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_kprobe_opts()
10617 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_ksyscall()
10619 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { in bpf_program__attach_ksyscall()
10686 struct kprobe_multi_resolve *res = data->res; in avail_kallsyms_cb()
10689 if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) in avail_kallsyms_cb()
10692 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); in avail_kallsyms_cb()
10696 res->addrs[res->cnt++] = (unsigned long)sym_addr; in avail_kallsyms_cb()
10712 err = -errno; in libbpf_available_kallsyms_parse()
10726 err = -EINVAL; in libbpf_available_kallsyms_parse()
10730 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kallsyms_parse()
10739 err = -errno; in libbpf_available_kallsyms_parse()
10748 err = -ENOENT; in libbpf_available_kallsyms_parse()
10760 if (res->cnt == 0) in libbpf_available_kallsyms_parse()
10761 err = -ENOENT; in libbpf_available_kallsyms_parse()
10774 return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; in has_available_filter_functions_addrs()
10787 err = -errno; in libbpf_available_kprobes_parse()
10800 err = -EINVAL; in libbpf_available_kprobes_parse()
10804 if (!glob_match(sym_name, res->pattern)) in libbpf_available_kprobes_parse()
10807 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, in libbpf_available_kprobes_parse()
10808 sizeof(*res->addrs), res->cnt + 1); in libbpf_available_kprobes_parse()
10812 res->addrs[res->cnt++] = (unsigned long)sym_addr; in libbpf_available_kprobes_parse()
10815 if (res->cnt == 0) in libbpf_available_kprobes_parse()
10816 err = -ENOENT; in libbpf_available_kprobes_parse()
10842 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
10850 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
10852 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
10854 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
10856 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_kprobe_multi_opts()
10879 err = -ENOMEM; in bpf_program__attach_kprobe_multi_opts()
10882 link->detach = &bpf_link__detach_fd; in bpf_program__attach_kprobe_multi_opts()
10887 err = -errno; in bpf_program__attach_kprobe_multi_opts()
10889 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_kprobe_multi_opts()
10892 link->fd = link_fd; in bpf_program__attach_kprobe_multi_opts()
10912 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ in attach_kprobe()
10913 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) in attach_kprobe()
10916 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); in attach_kprobe()
10918 func_name = prog->sec_name + sizeof("kretprobe/") - 1; in attach_kprobe()
10920 func_name = prog->sec_name + sizeof("kprobe/") - 1; in attach_kprobe()
10922 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); in attach_kprobe()
10925 return -EINVAL; in attach_kprobe()
10930 return -EINVAL; in attach_kprobe()
10946 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ in attach_ksyscall()
10947 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) in attach_ksyscall()
10950 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); in attach_ksyscall()
10952 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; in attach_ksyscall()
10954 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; in attach_ksyscall()
10957 return *link ? 0 : -errno; in attach_ksyscall()
10963 const char *spec; in attach_kprobe_multi() local
10969 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ in attach_kprobe_multi()
10970 if (strcmp(prog->sec_name, "kprobe.multi") == 0 || in attach_kprobe_multi()
10971 strcmp(prog->sec_name, "kretprobe.multi") == 0) in attach_kprobe_multi()
10974 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); in attach_kprobe_multi()
10976 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; in attach_kprobe_multi()
10978 spec = prog->sec_name + sizeof("kprobe.multi/") - 1; in attach_kprobe_multi()
10980 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); in attach_kprobe_multi()
10983 return -EINVAL; in attach_kprobe_multi()
10995 int n, ret = -EINVAL; in attach_uprobe_multi()
10999 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%ms", in attach_uprobe_multi()
11003 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe_multi()
11008 *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); in attach_uprobe_multi()
11012 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe_multi()
11013 prog->sec_name); in attach_uprobe_multi()
11047 return append_to_file(tracefs_uprobe_events(), "-:%s/%s", in remove_uprobe_event_legacy()
11088 pid < 0 ? -1 : pid, /* pid */ in perf_event_uprobe_open_legacy()
11089 pid == -1 ? 0 : -1, /* cpu */ in perf_event_uprobe_open_legacy()
11090 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_uprobe_open_legacy()
11092 err = -errno; in perf_event_uprobe_open_legacy()
11140 ret = -LIBBPF_ERRNO__FORMAT; in elf_find_func_offset_from_archive()
11147 elf_errmsg(-1)); in elf_find_func_offset_from_archive()
11148 ret = -LIBBPF_ERRNO__LIBELF; in elf_find_func_offset_from_archive()
11175 return "/lib/x86_64-linux-gnu"; in arch_specific_lib_paths()
11177 return "/lib/i386-linux-gnu"; in arch_specific_lib_paths()
11179 return "/lib/s390x-linux-gnu"; in arch_specific_lib_paths()
11181 return "/lib/s390-linux-gnu"; in arch_specific_lib_paths()
11183 return "/lib/arm-linux-gnueabi"; in arch_specific_lib_paths()
11185 return "/lib/arm-linux-gnueabihf"; in arch_specific_lib_paths()
11187 return "/lib/aarch64-linux-gnu"; in arch_specific_lib_paths()
11189 return "/lib/mips64el-linux-gnuabi64"; in arch_specific_lib_paths()
11191 return "/lib/mipsel-linux-gnu"; in arch_specific_lib_paths()
11193 return "/lib/powerpc64le-linux-gnu"; in arch_specific_lib_paths()
11195 return "/lib/sparc64-linux-gnu"; in arch_specific_lib_paths()
11197 return "/lib/riscv64-linux-gnu"; in arch_specific_lib_paths()
11232 seg_len = next_path ? next_path - s : strlen(s); in resolve_full_path()
11243 return -ENOENT; in resolve_full_path()
11265 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11281 * - syms and offsets are mutually exclusive in bpf_program__attach_uprobe_multi()
11282 * - ref_ctr_offsets and cookies are optional in bpf_program__attach_uprobe_multi()
11288 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11290 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11294 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11297 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_multi()
11305 prog->name, path, err); in bpf_program__attach_uprobe_multi()
11337 err = -ENOMEM; in bpf_program__attach_uprobe_multi()
11340 link->detach = &bpf_link__detach_fd; in bpf_program__attach_uprobe_multi()
11345 err = -errno; in bpf_program__attach_uprobe_multi()
11346 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", in bpf_program__attach_uprobe_multi()
11347 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); in bpf_program__attach_uprobe_multi()
11350 link->fd = link_fd; in bpf_program__attach_uprobe_multi()
11377 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11385 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11392 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); in bpf_program__attach_uprobe_opts()
11399 prog->name, binary_path, err); in bpf_program__attach_uprobe_opts()
11428 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
11432 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) in bpf_program__attach_uprobe_opts()
11433 return libbpf_err_ptr(-ENOTSUP); in bpf_program__attach_uprobe_opts()
11438 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11448 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_uprobe_opts()
11455 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_uprobe_opts()
11461 err = -errno; in bpf_program__attach_uprobe_opts()
11463 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
11474 prog->name, retprobe ? "uretprobe" : "uprobe", in bpf_program__attach_uprobe_opts()
11482 perf_link->legacy_probe_name = legacy_probe; in bpf_program__attach_uprobe_opts()
11483 perf_link->legacy_is_kprobe = false; in bpf_program__attach_uprobe_opts()
11484 perf_link->legacy_is_retprobe = retprobe; in bpf_program__attach_uprobe_opts()
11496 /* Format of u[ret]probe section definition supporting auto-attach:
11503 * specified (and auto-attach is not possible) or the above format is specified for
11504 * auto-attach.
11510 int n, ret = -EINVAL; in attach_uprobe()
11515 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li", in attach_uprobe()
11519 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ in attach_uprobe()
11524 prog->name, prog->sec_name); in attach_uprobe()
11532 prog->name); in attach_uprobe()
11536 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); in attach_uprobe()
11540 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, in attach_uprobe()
11541 prog->sec_name); in attach_uprobe()
11567 struct bpf_object *obj = prog->obj; in bpf_program__attach_usdt()
11573 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
11577 prog->name); in bpf_program__attach_usdt()
11578 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
11582 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_usdt()
11588 prog->name, binary_path, err); in bpf_program__attach_usdt()
11597 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
11598 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
11599 if (!obj->usdt_man) { in bpf_program__attach_usdt()
11600 obj->usdt_man = usdt_manager_new(obj); in bpf_program__attach_usdt()
11601 if (IS_ERR(obj->usdt_man)) in bpf_program__attach_usdt()
11602 return libbpf_ptr(obj->usdt_man); in bpf_program__attach_usdt()
11606 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, in bpf_program__attach_usdt()
11622 /* no auto-attach for just SEC("usdt") */ in attach_usdt()
11631 err = -EINVAL; in attach_usdt()
11633 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, in attach_usdt()
11652 return -errno; in determine_tracepoint_id()
11656 return -E2BIG; in determine_tracepoint_id()
11682 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, in perf_event_open_tracepoint()
11683 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); in perf_event_open_tracepoint()
11685 err = -errno; in perf_event_open_tracepoint()
11705 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tracepoint_opts()
11712 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
11721 prog->name, tp_category, tp_name, in bpf_program__attach_tracepoint_opts()
11741 /* no auto-attach for SEC("tp") or SEC("tracepoint") */ in attach_tp()
11742 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0) in attach_tp()
11745 sec_name = strdup(prog->sec_name); in attach_tp()
11747 return -ENOMEM; in attach_tp()
11750 if (str_has_pfx(prog->sec_name, "tp/")) in attach_tp()
11751 tp_cat = sec_name + sizeof("tp/") - 1; in attach_tp()
11753 tp_cat = sec_name + sizeof("tracepoint/") - 1; in attach_tp()
11757 return -EINVAL; in attach_tp()
11776 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_raw_tracepoint()
11777 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_raw_tracepoint()
11782 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_raw_tracepoint()
11783 link->detach = &bpf_link__detach_fd; in bpf_program__attach_raw_tracepoint()
11787 pfd = -errno; in bpf_program__attach_raw_tracepoint()
11790 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); in bpf_program__attach_raw_tracepoint()
11793 link->fd = pfd; in bpf_program__attach_raw_tracepoint()
11813 if (!str_has_pfx(prog->sec_name, prefixes[i])) in attach_raw_tp()
11817 /* no auto-attach case of, e.g., SEC("raw_tp") */ in attach_raw_tp()
11818 if (prog->sec_name[pfx_len] == '\0') in attach_raw_tp()
11821 if (prog->sec_name[pfx_len] != '/') in attach_raw_tp()
11824 tp_name = prog->sec_name + pfx_len + 1; in attach_raw_tp()
11830 prog->name, prog->sec_name); in attach_raw_tp()
11831 return -EINVAL; in attach_raw_tp()
11848 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
11852 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_btf_id()
11853 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_btf_id()
11858 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_btf_id()
11859 link->detach = &bpf_link__detach_fd; in bpf_program__attach_btf_id()
11865 pfd = -errno; in bpf_program__attach_btf_id()
11868 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); in bpf_program__attach_btf_id()
11871 link->fd = pfd; in bpf_program__attach_btf_id()
11915 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program_attach_fd()
11916 return libbpf_err_ptr(-EINVAL); in bpf_program_attach_fd()
11921 return libbpf_err_ptr(-ENOMEM); in bpf_program_attach_fd()
11922 link->detach = &bpf_link__detach_fd; in bpf_program_attach_fd()
11927 link_fd = -errno; in bpf_program_attach_fd()
11930 prog->name, target_name, in bpf_program_attach_fd()
11934 link->fd = link_fd; in bpf_program_attach_fd()
11965 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
11970 /* validate we don't have unexpected combinations of non-zero fields */ in bpf_program__attach_tcx()
11973 prog->name); in bpf_program__attach_tcx()
11974 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
11978 prog->name); in bpf_program__attach_tcx()
11979 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_tcx()
11999 prog->name); in bpf_program__attach_freplace()
12000 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12003 if (prog->type != BPF_PROG_TYPE_EXT) { in bpf_program__attach_freplace()
12005 prog->name); in bpf_program__attach_freplace()
12006 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_freplace()
12039 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12046 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_iter()
12047 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_iter()
12052 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_iter()
12053 link->detach = &bpf_link__detach_fd; in bpf_program__attach_iter()
12058 link_fd = -errno; in bpf_program__attach_iter()
12061 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); in bpf_program__attach_iter()
12064 link->fd = link_fd; in bpf_program__attach_iter()
12082 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12086 pr_warn("prog '%s': can't attach before loaded\n", prog->name); in bpf_program__attach_netfilter()
12087 return libbpf_err_ptr(-EINVAL); in bpf_program__attach_netfilter()
12092 return libbpf_err_ptr(-ENOMEM); in bpf_program__attach_netfilter()
12094 link->detach = &bpf_link__detach_fd; in bpf_program__attach_netfilter()
12105 link_fd = -errno; in bpf_program__attach_netfilter()
12108 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); in bpf_program__attach_netfilter()
12111 link->fd = link_fd; in bpf_program__attach_netfilter()
12121 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_program__attach()
12122 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
12124 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); in bpf_program__attach()
12128 /* When calling bpf_program__attach() explicitly, auto-attach support in bpf_program__attach()
12134 return libbpf_err_ptr(-EOPNOTSUPP); in bpf_program__attach()
12151 if (st_link->map_fd < 0) in bpf_link__detach_struct_ops()
12153 return bpf_map_delete_elem(link->fd, &zero); in bpf_link__detach_struct_ops()
12155 return close(link->fd); in bpf_link__detach_struct_ops()
12164 if (!bpf_map__is_struct_ops(map) || map->fd == -1) in bpf_map__attach_struct_ops()
12165 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
12169 return libbpf_err_ptr(-EINVAL); in bpf_map__attach_struct_ops()
12172 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_map__attach_struct_ops()
12178 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { in bpf_map__attach_struct_ops()
12183 link->link.detach = bpf_link__detach_struct_ops; in bpf_map__attach_struct_ops()
12185 if (!(map->def.map_flags & BPF_F_LINK)) { in bpf_map__attach_struct_ops()
12187 link->link.fd = map->fd; in bpf_map__attach_struct_ops()
12188 link->map_fd = -1; in bpf_map__attach_struct_ops()
12189 return &link->link; in bpf_map__attach_struct_ops()
12192 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); in bpf_map__attach_struct_ops()
12198 link->link.fd = fd; in bpf_map__attach_struct_ops()
12199 link->map_fd = map->fd; in bpf_map__attach_struct_ops()
12201 return &link->link; in bpf_map__attach_struct_ops()
12213 if (!bpf_map__is_struct_ops(map) || map->fd < 0) in bpf_link__update_map()
12214 return -EINVAL; in bpf_link__update_map()
12218 if (st_ops_link->map_fd < 0) in bpf_link__update_map()
12219 return -EINVAL; in bpf_link__update_map()
12221 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); in bpf_link__update_map()
12227 if (err && err != -EBUSY) in bpf_link__update_map()
12230 err = bpf_link_update(link->fd, map->fd, NULL); in bpf_link__update_map()
12234 st_ops_link->map_fd = map->fd; in bpf_link__update_map()
12249 __u64 data_tail = header->data_tail;
12256 ehdr = base + (data_tail & (mmap_size - 1));
12257 ehdr_size = ehdr->size;
12261 size_t len_first = base + mmap_size - copy_start;
12262 size_t len_secnd = ehdr_size - len_first;
12296 /* sample_cb and lost_cb are higher-level common-case callbacks */
12335 if (cpu_buf->base && in perf_buffer__free_cpu_buf()
12336 munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) in perf_buffer__free_cpu_buf()
12337 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); in perf_buffer__free_cpu_buf()
12338 if (cpu_buf->fd >= 0) { in perf_buffer__free_cpu_buf()
12339 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); in perf_buffer__free_cpu_buf()
12340 close(cpu_buf->fd); in perf_buffer__free_cpu_buf()
12342 free(cpu_buf->buf); in perf_buffer__free_cpu_buf()
12352 if (pb->cpu_bufs) { in perf_buffer__free()
12353 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__free()
12354 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__free()
12359 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); in perf_buffer__free()
12362 free(pb->cpu_bufs); in perf_buffer__free()
12364 if (pb->epoll_fd >= 0) in perf_buffer__free()
12365 close(pb->epoll_fd); in perf_buffer__free()
12366 free(pb->events); in perf_buffer__free()
12380 return ERR_PTR(-ENOMEM); in perf_buffer__open_cpu_buf()
12382 cpu_buf->pb = pb; in perf_buffer__open_cpu_buf()
12383 cpu_buf->cpu = cpu; in perf_buffer__open_cpu_buf()
12384 cpu_buf->map_key = map_key; in perf_buffer__open_cpu_buf()
12386 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, in perf_buffer__open_cpu_buf()
12387 -1, PERF_FLAG_FD_CLOEXEC); in perf_buffer__open_cpu_buf()
12388 if (cpu_buf->fd < 0) { in perf_buffer__open_cpu_buf()
12389 err = -errno; in perf_buffer__open_cpu_buf()
12395 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, in perf_buffer__open_cpu_buf()
12397 cpu_buf->fd, 0); in perf_buffer__open_cpu_buf()
12398 if (cpu_buf->base == MAP_FAILED) { in perf_buffer__open_cpu_buf()
12399 cpu_buf->base = NULL; in perf_buffer__open_cpu_buf()
12400 err = -errno; in perf_buffer__open_cpu_buf()
12406 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { in perf_buffer__open_cpu_buf()
12407 err = -errno; in perf_buffer__open_cpu_buf()
12435 return libbpf_err_ptr(-EINVAL); in perf_buffer__new()
12465 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
12468 return libbpf_err_ptr(-EINVAL); in perf_buffer__new_raw()
12491 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { in __perf_buffer__new()
12494 return ERR_PTR(-EINVAL); in __perf_buffer__new()
12497 /* best-effort sanity checks */ in __perf_buffer__new()
12502 err = -errno; in __perf_buffer__new()
12504 * -EBADFD, -EFAULT, or -E2BIG on real error in __perf_buffer__new()
12506 if (err != -EINVAL) { in __perf_buffer__new()
12517 return ERR_PTR(-EINVAL); in __perf_buffer__new()
12523 return ERR_PTR(-ENOMEM); in __perf_buffer__new()
12525 pb->event_cb = p->event_cb; in __perf_buffer__new()
12526 pb->sample_cb = p->sample_cb; in __perf_buffer__new()
12527 pb->lost_cb = p->lost_cb; in __perf_buffer__new()
12528 pb->ctx = p->ctx; in __perf_buffer__new()
12530 pb->page_size = getpagesize(); in __perf_buffer__new()
12531 pb->mmap_size = pb->page_size * page_cnt; in __perf_buffer__new()
12532 pb->map_fd = map_fd; in __perf_buffer__new()
12534 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); in __perf_buffer__new()
12535 if (pb->epoll_fd < 0) { in __perf_buffer__new()
12536 err = -errno; in __perf_buffer__new()
12542 if (p->cpu_cnt > 0) { in __perf_buffer__new()
12543 pb->cpu_cnt = p->cpu_cnt; in __perf_buffer__new()
12545 pb->cpu_cnt = libbpf_num_possible_cpus(); in __perf_buffer__new()
12546 if (pb->cpu_cnt < 0) { in __perf_buffer__new()
12547 err = pb->cpu_cnt; in __perf_buffer__new()
12550 if (map.max_entries && map.max_entries < pb->cpu_cnt) in __perf_buffer__new()
12551 pb->cpu_cnt = map.max_entries; in __perf_buffer__new()
12554 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); in __perf_buffer__new()
12555 if (!pb->events) { in __perf_buffer__new()
12556 err = -ENOMEM; in __perf_buffer__new()
12560 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); in __perf_buffer__new()
12561 if (!pb->cpu_bufs) { in __perf_buffer__new()
12562 err = -ENOMEM; in __perf_buffer__new()
12573 for (i = 0, j = 0; i < pb->cpu_cnt; i++) { in __perf_buffer__new()
12577 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; in __perf_buffer__new()
12578 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; in __perf_buffer__new()
12583 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) in __perf_buffer__new()
12586 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); in __perf_buffer__new()
12592 pb->cpu_bufs[j] = cpu_buf; in __perf_buffer__new()
12594 err = bpf_map_update_elem(pb->map_fd, &map_key, in __perf_buffer__new()
12595 &cpu_buf->fd, 0); in __perf_buffer__new()
12597 err = -errno; in __perf_buffer__new()
12598 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", in __perf_buffer__new()
12599 cpu, map_key, cpu_buf->fd, in __perf_buffer__new()
12604 pb->events[j].events = EPOLLIN; in __perf_buffer__new()
12605 pb->events[j].data.ptr = cpu_buf; in __perf_buffer__new()
12606 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, in __perf_buffer__new()
12607 &pb->events[j]) < 0) { in __perf_buffer__new()
12608 err = -errno; in __perf_buffer__new()
12610 cpu, cpu_buf->fd, in __perf_buffer__new()
12616 pb->cpu_cnt = j; in __perf_buffer__new()
12645 struct perf_buffer *pb = cpu_buf->pb; in perf_buffer__process_record()
12649 if (pb->event_cb) in perf_buffer__process_record()
12650 return pb->event_cb(pb->ctx, cpu_buf->cpu, e); in perf_buffer__process_record()
12652 switch (e->type) { in perf_buffer__process_record()
12656 if (pb->sample_cb) in perf_buffer__process_record()
12657 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); in perf_buffer__process_record()
12663 if (pb->lost_cb) in perf_buffer__process_record()
12664 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); in perf_buffer__process_record()
12668 pr_warn("unknown perf sample type %d\n", e->type); in perf_buffer__process_record()
12679 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, in perf_buffer__process_records()
12680 pb->page_size, &cpu_buf->buf, in perf_buffer__process_records()
12681 &cpu_buf->buf_size, in perf_buffer__process_records()
12690 return pb->epoll_fd; in perf_buffer__epoll_fd()
12697 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); in perf_buffer__poll()
12699 return -errno; in perf_buffer__poll()
12702 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; in perf_buffer__poll()
12718 return pb->cpu_cnt; in perf_buffer__buffer_cnt()
12730 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer_fd()
12731 return libbpf_err(-EINVAL); in perf_buffer__buffer_fd()
12733 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer_fd()
12735 return libbpf_err(-ENOENT); in perf_buffer__buffer_fd()
12737 return cpu_buf->fd; in perf_buffer__buffer_fd()
12744 if (buf_idx >= pb->cpu_cnt) in perf_buffer__buffer()
12745 return libbpf_err(-EINVAL); in perf_buffer__buffer()
12747 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__buffer()
12749 return libbpf_err(-ENOENT); in perf_buffer__buffer()
12751 *buf = cpu_buf->base; in perf_buffer__buffer()
12752 *buf_size = pb->mmap_size; in perf_buffer__buffer()
12761 * - 0 on success;
12762 * - <0 on failure.
12768 if (buf_idx >= pb->cpu_cnt) in perf_buffer__consume_buffer()
12769 return libbpf_err(-EINVAL); in perf_buffer__consume_buffer()
12771 cpu_buf = pb->cpu_bufs[buf_idx]; in perf_buffer__consume_buffer()
12773 return libbpf_err(-ENOENT); in perf_buffer__consume_buffer()
12782 for (i = 0; i < pb->cpu_cnt; i++) { in perf_buffer__consume()
12783 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; in perf_buffer__consume()
12804 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
12806 if (prog->obj->loaded) in bpf_program__set_attach_target()
12807 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
12813 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
12824 return libbpf_err(-EINVAL); in bpf_program__set_attach_target()
12827 err = bpf_object__load_vmlinux_btf(prog->obj, true); in bpf_program__set_attach_target()
12830 err = find_kernel_btf_id(prog->obj, attach_func_name, in bpf_program__set_attach_target()
12831 prog->expected_attach_type, in bpf_program__set_attach_target()
12837 prog->attach_btf_id = btf_id; in bpf_program__set_attach_target()
12838 prog->attach_btf_obj_fd = btf_obj_fd; in bpf_program__set_attach_target()
12839 prog->attach_prog_fd = attach_prog_fd; in bpf_program__set_attach_target()
12845 int err = 0, n, len, start, end = -1; in parse_cpu_mask_str()
12851 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ in parse_cpu_mask_str()
12857 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); in parse_cpu_mask_str()
12860 err = -EINVAL; in parse_cpu_mask_str()
12868 err = -EINVAL; in parse_cpu_mask_str()
12873 err = -ENOMEM; in parse_cpu_mask_str()
12877 memset(tmp + *mask_sz, 0, start - *mask_sz); in parse_cpu_mask_str()
12878 memset(tmp + start, 1, end - start + 1); in parse_cpu_mask_str()
12884 return -EINVAL; in parse_cpu_mask_str()
12900 err = -errno; in parse_cpu_mask_file()
12907 err = len ? -errno : -EINVAL; in parse_cpu_mask_file()
12913 return -E2BIG; in parse_cpu_mask_file()
12960 return -ESRCH; in populate_skeleton_maps()
12963 /* externs shouldn't be pre-setup from user code */ in populate_skeleton_maps()
12964 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) in populate_skeleton_maps()
12965 *mmaped = (*map)->mmaped; in populate_skeleton_maps()
12983 return -ESRCH; in populate_skeleton_progs()
12993 .object_name = s->name, in bpf_object__open_skeleton()
12998 /* Attempt to preserve opts->object_name, unless overriden by user in bpf_object__open_skeleton()
13006 if (!opts->object_name) in bpf_object__open_skeleton()
13007 skel_opts.object_name = s->name; in bpf_object__open_skeleton()
13010 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); in bpf_object__open_skeleton()
13014 s->name, err); in bpf_object__open_skeleton()
13018 *s->obj = obj; in bpf_object__open_skeleton()
13019 err = populate_skeleton_maps(obj, s->maps, s->map_cnt); in bpf_object__open_skeleton()
13021 pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); in bpf_object__open_skeleton()
13025 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); in bpf_object__open_skeleton()
13027 pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); in bpf_object__open_skeleton()
13045 if (!s->obj) in bpf_object__open_subskeleton()
13046 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13048 btf = bpf_object__btf(s->obj); in bpf_object__open_subskeleton()
13051 bpf_object__name(s->obj)); in bpf_object__open_subskeleton()
13052 return libbpf_err(-errno); in bpf_object__open_subskeleton()
13055 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); in bpf_object__open_subskeleton()
13061 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); in bpf_object__open_subskeleton()
13067 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { in bpf_object__open_subskeleton()
13068 var_skel = &s->vars[var_idx]; in bpf_object__open_subskeleton()
13069 map = *var_skel->map; in bpf_object__open_subskeleton()
13077 return libbpf_err(-EINVAL); in bpf_object__open_subskeleton()
13083 var_type = btf__type_by_id(btf, var->type); in bpf_object__open_subskeleton()
13084 var_name = btf__name_by_offset(btf, var_type->name_off); in bpf_object__open_subskeleton()
13085 if (strcmp(var_name, var_skel->name) == 0) { in bpf_object__open_subskeleton()
13086 *var_skel->addr = map->mmaped + var->offset; in bpf_object__open_subskeleton()
13098 free(s->maps); in bpf_object__destroy_subskeleton()
13099 free(s->progs); in bpf_object__destroy_subskeleton()
13100 free(s->vars); in bpf_object__destroy_subskeleton()
13108 err = bpf_object__load(*s->obj); in bpf_object__load_skeleton()
13110 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err); in bpf_object__load_skeleton()
13114 for (i = 0; i < s->map_cnt; i++) { in bpf_object__load_skeleton()
13115 struct bpf_map *map = *s->maps[i].map; in bpf_object__load_skeleton()
13116 size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); in bpf_object__load_skeleton()
13118 void **mmaped = s->maps[i].mmaped; in bpf_object__load_skeleton()
13123 if (!(map->def.map_flags & BPF_F_MMAPABLE)) { in bpf_object__load_skeleton()
13128 if (map->def.map_flags & BPF_F_RDONLY_PROG) in bpf_object__load_skeleton()
13133 /* Remap anonymous mmap()-ed "map initialization image" as in bpf_object__load_skeleton()
13134 * a BPF map-backed mmap()-ed memory, but preserving the same in bpf_object__load_skeleton()
13143 *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0); in bpf_object__load_skeleton()
13145 err = -errno; in bpf_object__load_skeleton()
13147 pr_warn("failed to re-mmap() map '%s': %d\n", in bpf_object__load_skeleton()
13160 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__attach_skeleton()
13161 struct bpf_program *prog = *s->progs[i].prog; in bpf_object__attach_skeleton()
13162 struct bpf_link **link = s->progs[i].link; in bpf_object__attach_skeleton()
13164 if (!prog->autoload || !prog->autoattach) in bpf_object__attach_skeleton()
13167 /* auto-attaching not supported for this program */ in bpf_object__attach_skeleton()
13168 if (!prog->sec_def || !prog->sec_def->prog_attach_fn) in bpf_object__attach_skeleton()
13171 /* if user already set the link manually, don't attempt auto-attach */ in bpf_object__attach_skeleton()
13175 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); in bpf_object__attach_skeleton()
13177 pr_warn("prog '%s': failed to auto-attach: %d\n", in bpf_object__attach_skeleton()
13182 /* It's possible that for some SEC() definitions auto-attach in bpf_object__attach_skeleton()
13187 * auto-attached. But if not, it shouldn't trigger skeleton's in bpf_object__attach_skeleton()
13201 for (i = 0; i < s->prog_cnt; i++) { in bpf_object__detach_skeleton()
13202 struct bpf_link **link = s->progs[i].link; in bpf_object__detach_skeleton()
13214 if (s->progs) in bpf_object__destroy_skeleton()
13216 if (s->obj) in bpf_object__destroy_skeleton()
13217 bpf_object__close(*s->obj); in bpf_object__destroy_skeleton()
13218 free(s->maps); in bpf_object__destroy_skeleton()
13219 free(s->progs); in bpf_object__destroy_skeleton()