tools/lib/bpf/relo_core.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/relo_core.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/relo_core.c- Extension
.c- Size
- 52402 bytes
- Lines
- 1705
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf.hlinux/btf.hlinux/string.hlinux/bpf_verifier.hrelo_core.hstdio.hstring.herrno.hctype.hlinux/err.hlibbpf.hbpf.hbtf.hlibbpf_internal.h
Detected Declarations
enum libbpf_print_levelfunction is_ldimm64_insnfunction skip_mods_and_typedefsfunction btf__resolve_sizefunction is_flex_arrfunction core_relo_is_field_basedfunction core_relo_is_type_basedfunction core_relo_is_enumval_basedfunction __bpf_core_types_are_compatfunction bpf_core_parse_specfunction bpf_core_fields_are_compatfunction bpf_core_match_memberfunction specfunction typefunction bpf_core_calc_field_relofunction bpf_core_calc_type_relofunction bpf_core_calc_enumval_relofunction bpf_core_calc_relofunction bpf_core_poison_insnfunction insn_bpf_size_to_bytesfunction insn_bytes_to_bpf_sizefunction bpf_core_patch_insnfunction bpf_core_format_specfunction underscorefunction unreachablefunction bpf_core_names_matchfunction bpf_core_enums_matchfunction bpf_core_composites_matchfunction enum64
Annotated Snippet
if (btf_is_composite(t)) {
const struct btf_member *m;
__u32 bit_offset;
if (access_idx >= btf_vlen(t))
return -EINVAL;
bit_offset = btf_member_bit_offset(t, access_idx);
spec->bit_offset += bit_offset;
m = btf_members(t) + access_idx;
if (m->name_off) {
name = btf__name_by_offset(btf, m->name_off);
if (str_is_empty(name))
return -EINVAL;
acc->type_id = id;
acc->idx = access_idx;
acc->name = name;
spec->len++;
}
id = m->type;
} else if (btf_is_array(t)) {
const struct btf_array *a = btf_array(t);
bool flex;
t = skip_mods_and_typedefs(btf, a->type, &id);
if (!t)
return -EINVAL;
flex = is_flex_arr(btf, acc - 1, a);
if (!flex && access_idx >= a->nelems)
return -EINVAL;
spec->spec[spec->len].type_id = id;
spec->spec[spec->len].idx = access_idx;
spec->len++;
sz = btf__resolve_size(btf, id);
if (sz < 0)
return sz;
spec->bit_offset += access_idx * sz * 8;
} else {
pr_warn("prog '%s': relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
prog_name, relo->type_id, spec_str, i, id, btf_kind_str(t));
return -EINVAL;
}
}
return 0;
}
/* Check two types for compatibility for the purpose of field access
* relocation. const/volatile/restrict and typedefs are skipped to ensure we
* are relocating semantically compatible entities:
* - any two STRUCTs/UNIONs are compatible and can be mixed;
* - any two FWDs are compatible, if their names match (modulo flavor suffix);
* - any two PTRs are always compatible;
* - for ENUMs, names should be the same (ignoring flavor suffix) or at
* least one of enums should be anonymous;
* - for ENUMs, check sizes, names are ignored;
* - for INT, size and signedness are ignored;
* - any two FLOATs are always compatible;
* - for ARRAY, dimensionality is ignored, element types are checked for
* compatibility recursively;
* - everything else shouldn't be ever a target of relocation.
* These rules are not set in stone and probably will be adjusted as we get
* more experience with using BPF CO-RE relocations.
*/
static int bpf_core_fields_are_compat(const struct btf *local_btf,
__u32 local_id,
const struct btf *targ_btf,
__u32 targ_id)
{
const struct btf_type *local_type, *targ_type;
recur:
local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
if (!local_type || !targ_type)
return -EINVAL;
if (btf_is_composite(local_type) && btf_is_composite(targ_type))
return 1;
if (!btf_kind_core_compat(local_type, targ_type))
return 0;
switch (btf_kind(local_type)) {
case BTF_KIND_PTR:
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/btf.h`, `linux/string.h`, `linux/bpf_verifier.h`, `relo_core.h`, `stdio.h`, `string.h`, `errno.h`.
- Detected declarations: `enum libbpf_print_level`, `function is_ldimm64_insn`, `function skip_mods_and_typedefs`, `function btf__resolve_size`, `function is_flex_arr`, `function core_relo_is_field_based`, `function core_relo_is_type_based`, `function core_relo_is_enumval_based`, `function __bpf_core_types_are_compat`, `function bpf_core_parse_spec`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.