tools/lib/bpf/gen_loader.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/gen_loader.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/gen_loader.c- Extension
.c- Size
- 44806 bytes
- Lines
- 1295
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdlib.hstring.herrno.hasm/byteorder.hlinux/filter.hsys/param.hbtf.hbpf.hlibbpf.hlibbpf_internal.hhashmap.hbpf_gen_internal.hskel_internal.h
Detected Declarations
struct loader_stackfunction blob_fd_array_offfunction realloc_insn_buffunction realloc_data_buffunction emitfunction emit2function bpf_gen__initfunction add_datafunction add_map_fdfunction add_kfunc_btf_fdfunction insn_bytes_to_bpf_sizefunction emit_rel_storefunction move_blob2blobfunction move_blob2ctxfunction move_ctx2blobfunction move_stack2blobfunction move_stack2ctxfunction emit_sys_bpffunction is_simm16function emit_check_errfunction emit_debugfunction debug_regsfunction debug_retfunction __emit_sys_closefunction emit_sys_close_stackfunction emit_sys_close_blobfunction bpf_gen__finishfunction bpf_gen__freefunction compute_sha_update_offsetsfunction bpf_gen__load_btffunction bpf_gen__map_createfunction emit_signature_matchfunction bpf_gen__record_attach_targetfunction emit_find_attach_targetfunction bpf_gen__record_externfunction emit_bpf_find_by_name_kindfunction emit_bpf_kallsyms_lookup_namefunction fd_arrayfunction emit_ksym_relo_logfunction emit_relo_ksym_typelessfunction src_reg_maskfunction emit_relo_ksym_btffunction bpf_gen__record_relo_corefunction emit_relofunction emit_relosfunction cleanup_core_relofunction cleanup_relosfunction info_blob_bswap
Annotated Snippet
struct loader_stack {
__u32 btf_fd;
__u32 inner_map_fd;
__u32 prog_fd[MAX_USED_PROGS];
};
#define stack_off(field) \
(__s16)(-sizeof(struct loader_stack) + offsetof(struct loader_stack, field))
#define attr_field(attr, field) (attr + offsetof(union bpf_attr, field))
static int blob_fd_array_off(struct bpf_gen *gen, int index)
{
return gen->fd_array + index * sizeof(int);
}
static int realloc_insn_buf(struct bpf_gen *gen, __u32 size)
{
size_t off = gen->insn_cur - gen->insn_start;
void *insn_start;
if (gen->error)
return gen->error;
if (size > INT32_MAX || off + size > INT32_MAX) {
gen->error = -ERANGE;
return -ERANGE;
}
insn_start = realloc(gen->insn_start, off + size);
if (!insn_start) {
gen->error = -ENOMEM;
free(gen->insn_start);
gen->insn_start = NULL;
gen->insn_cur = NULL;
return -ENOMEM;
}
gen->insn_start = insn_start;
gen->insn_cur = insn_start + off;
return 0;
}
static int realloc_data_buf(struct bpf_gen *gen, __u32 size)
{
size_t off = gen->data_cur - gen->data_start;
void *data_start;
if (gen->error)
return gen->error;
if (size > INT32_MAX || off + size > INT32_MAX) {
gen->error = -ERANGE;
return -ERANGE;
}
data_start = realloc(gen->data_start, off + size);
if (!data_start) {
gen->error = -ENOMEM;
free(gen->data_start);
gen->data_start = NULL;
gen->data_cur = NULL;
return -ENOMEM;
}
gen->data_start = data_start;
gen->data_cur = data_start + off;
return 0;
}
static void emit(struct bpf_gen *gen, struct bpf_insn insn)
{
if (realloc_insn_buf(gen, sizeof(insn)))
return;
memcpy(gen->insn_cur, &insn, sizeof(insn));
gen->insn_cur += sizeof(insn);
}
static void emit2(struct bpf_gen *gen, struct bpf_insn insn1, struct bpf_insn insn2)
{
emit(gen, insn1);
emit(gen, insn2);
}
static int add_data(struct bpf_gen *gen, const void *data, __u32 size);
static void emit_sys_close_blob(struct bpf_gen *gen, int blob_off);
static void emit_signature_match(struct bpf_gen *gen);
void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps)
{
size_t stack_sz = sizeof(struct loader_stack), nr_progs_sz;
int i;
gen->fd_array = add_data(gen, NULL, MAX_FD_ARRAY_SZ * sizeof(int));
gen->log_level = log_level;
/* save ctx pointer into R6 */
Annotation
- Immediate include surface: `stdio.h`, `stdlib.h`, `string.h`, `errno.h`, `asm/byteorder.h`, `linux/filter.h`, `sys/param.h`, `btf.h`.
- Detected declarations: `struct loader_stack`, `function blob_fd_array_off`, `function realloc_insn_buf`, `function realloc_data_buf`, `function emit`, `function emit2`, `function bpf_gen__init`, `function add_data`, `function add_map_fd`, `function add_kfunc_btf_fd`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.