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.

Dependency Surface

Detected Declarations

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

Implementation Notes