tools/lib/bpf/btf.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/btf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/btf.c- Extension
.c- Size
- 171014 bytes
- Lines
- 6377
- 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
byteswap.hendian.hstdio.hstdlib.hstring.hfcntl.hunistd.herrno.hsys/utsname.hsys/param.hsys/stat.hsys/mman.hlinux/kernel.hlinux/err.hlinux/btf.hgelf.hbtf.hbpf.hlibbpf.hlibbpf_internal.hhashmap.hstrset.h
Detected Declarations
struct btfstruct btf_elf_secsstruct btf_pipestruct btf_ext_sec_info_paramstruct btf_dedupstruct btf_dedupstruct btf_distillstruct btf_permutefunction ptr_to_u64function capacityfunction libbpf_ensure_memfunction btf_add_type_idx_entryfunction btf_bswap_hdrfunction btf_parse_hdrfunction btf_ensure_modifiablefunction btf_parse_str_secfunction btf_parse_layout_secfunction btf_type_size_unknownfunction btf_type_sizefunction btf_bswap_type_basefunction btf_bswap_type_restfunction btf_parse_type_secfunction btf_validate_strfunction btf_validate_idfunction btf_validate_typefunction correctfunction btf__type_cntfunction determine_ptr_sizefunction btf_ptr_szfunction btf__set_pointer_sizefunction btf__set_pointer_sizefunction is_host_big_endianfunction btf__endiannessfunction btf__set_endiannessfunction btf_type_is_voidfunction btf_type_is_void_or_nullfunction btf__resolve_sizefunction btf__align_offunction btf__resolve_typefunction btf_check_sortedfunction btf_find_type_by_name_bsearchfunction btf_find_by_name_kindfunction btf__find_by_namefunction btf__find_by_name_kind_ownfunction btf__find_by_name_kindfunction btf_is_modifiablefunction btf_free_raw_datafunction btf__free
Annotated Snippet
struct btf {
/* raw BTF data in native endianness */
void *raw_data;
/* raw BTF data in non-native endianness */
void *raw_data_swapped;
__u32 raw_size;
/* whether target endianness differs from the native one */
bool swapped_endian;
/*
* When BTF is loaded from an ELF or raw memory it is stored
* in a contiguous memory block. The type_data, layout and strs_data
* point inside that memory region to their respective parts of BTF
* representation:
*
* +----------------------------------------+---------------+
* | Header | Types | Optional layout | Strings |
* +--------------------------------------------------------+
* ^ ^ ^ ^
* | | | |
* raw_data | | |
* types_data-+ | |
* layout---------------+ |
* strs_data--------------------------------+
*
* A separate struct btf_header is embedded as btf->hdr,
* and header information is copied into it. This allows us
* to handle header data for various header formats; the original,
* the extended header with layout info, etc.
*
* If BTF data is later modified, e.g., due to types added or
* removed, BTF deduplication performed, etc, this contiguous
* representation is broken up into four independent memory
* regions.
*
* raw_data is nulled out at that point, but can be later allocated
* and cached again if user calls btf__raw_data(), at which point
* raw_data will contain a contiguous copy of header, types, optional
* layout and strings. layout optionally points to a
* btf_layout array - this allows us to encode information about
* the kinds known at encoding time. If layout is NULL no
* layout information is encoded.
*
* +----------+ +---------+ +-----------+ +-----------+
* | Header | | Types | | Layout | | Strings |
* +----------+ +---------+ +-----------+ +-----------+
* ^ ^ ^ ^
* | | | |
* hdr | | |
* types_data----+ | |
* layout---------------------+ |
* strset__data(strs_set)---------------------+
*
* +----------+---------+-------------------+-----------+
* | Header | Types | Optional Layout | Strings |
* raw_data----->+----------+---------+-------------------+-----------+
*/
struct btf_header hdr;
void *types_data;
size_t types_data_cap; /* used size stored in hdr->type_len */
/* type ID to `struct btf_type *` lookup index
* type_offs[0] corresponds to the first non-VOID type:
* - for base BTF it's type [1];
* - for split BTF it's the first non-base BTF type.
*/
__u32 *type_offs;
size_t type_offs_cap;
/* number of types in this BTF instance:
* - doesn't include special [0] void type;
* - for split BTF counts number of types added on top of base BTF.
*/
__u32 nr_types;
/* the start IDs of named types in sorted BTF */
int named_start_id;
/* if not NULL, points to the base BTF on top of which the current
* split BTF is based
*/
struct btf *base_btf;
/* BTF type ID of the first type in this BTF instance:
* - for base BTF it's equal to 1;
* - for split BTF it's equal to biggest type ID of base BTF plus 1.
*/
int start_id;
/* logical string offset of this BTF instance:
* - for base BTF it's equal to 0;
* - for split BTF it's equal to total size of base BTF's string section size.
*/
int start_str_off;
Annotation
- Immediate include surface: `byteswap.h`, `endian.h`, `stdio.h`, `stdlib.h`, `string.h`, `fcntl.h`, `unistd.h`, `errno.h`.
- Detected declarations: `struct btf`, `struct btf_elf_secs`, `struct btf_pipe`, `struct btf_ext_sec_info_param`, `struct btf_dedup`, `struct btf_dedup`, `struct btf_distill`, `struct btf_permute`, `function ptr_to_u64`, `function capacity`.
- 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.