From 526f113a5af1975ef04735348cf9dfe85dd04fe9 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 3 Sep 2026 11:30:21 +0530 Subject: [PATCH] Core: Give every symbol kind a common Symbol base class LocalSymbol, BpfGlobalSymbol and MapSymbol each named the storage behind a name differently (var, var, sym) and shared no type. They now all derive from symbols.Symbol, which carries what every symbol has -- the storage pointer (alloca, GlobalVariable, or None for the context parameter) and its IR type -- and each subclass adds only what its kind needs: LocalSymbol its metadata and declared_global flag, BpfGlobalSymbol its ctypes name, MapSymbol its map type and params. MapSymbol.sym becomes var to match. local_symbol.py is folded into the new symbols.py. LocalSymbol keeps its three-field __iter__ for the call sites that tuple-unpack a symbol. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU --- pythonbpf/allocation_pass.py | 2 +- pythonbpf/context.py | 2 +- pythonbpf/globals_pass.py | 16 +----- pythonbpf/helper/bpf_helper_handler.py | 2 +- pythonbpf/local_symbol.py | 24 --------- pythonbpf/maps/maps_pass.py | 13 +++-- pythonbpf/maps/maps_utils.py | 9 ++-- pythonbpf/symbols.py | 53 +++++++++++++++++++ .../vmlinux_parser/vmlinux_exports_handler.py | 2 +- 9 files changed, 71 insertions(+), 52 deletions(-) delete mode 100644 pythonbpf/local_symbol.py create mode 100644 pythonbpf/symbols.py diff --git a/pythonbpf/allocation_pass.py b/pythonbpf/allocation_pass.py index 567e945..e89d658 100644 --- a/pythonbpf/allocation_pass.py +++ b/pythonbpf/allocation_pass.py @@ -2,7 +2,7 @@ import ast import logging import ctypes from llvmlite import ir -from .local_symbol import LocalSymbol +from .symbols import LocalSymbol from pythonbpf.helper import HelperHandlerRegistry from pythonbpf.vmlinux_parser.dependency_node import Field from .expr import VmlinuxHandlerRegistry diff --git a/pythonbpf/context.py b/pythonbpf/context.py index 21a252b..c9a2f80 100644 --- a/pythonbpf/context.py +++ b/pythonbpf/context.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from pythonbpf.structs.struct_type import StructType from pythonbpf.maps.maps_utils import MapSymbol - from pythonbpf.globals_pass import BpfGlobalSymbol + from pythonbpf.symbols import BpfGlobalSymbol logger = logging.getLogger(__name__) diff --git a/pythonbpf/globals_pass.py b/pythonbpf/globals_pass.py index a1cb251..aabe8fd 100644 --- a/pythonbpf/globals_pass.py +++ b/pythonbpf/globals_pass.py @@ -1,10 +1,10 @@ from llvmlite import ir import ast -from dataclasses import dataclass from logging import Logger import logging from .type_deducer import ctypes_to_ir +from .symbols import BpfGlobalSymbol from .debuginfo import DebugInfoGenerator from .expr import VmlinuxHandlerRegistry from .debuginfo import dwarf_constants as dc @@ -26,20 +26,6 @@ _SIGNED_CTYPES = { _C_NAME_BY_WIDTH = {8: "char", 16: "short", 32: "int", 64: "long long"} -@dataclass -class BpfGlobalSymbol: - """A mutable BPF global variable declared with @bpfglobal. - - Lands in .bss (zero initializer) or .data (non-zero) and is read with a - plain load / written with a plain store; libbpf exposes the sections to - userspace as global-data maps. - """ - - var: ir.GlobalVariable - ir_type: ir.Type - ctype_name: str - - def populate_global_symbol_table(tree, compilation_context): """ compilation_context: CompilationContext diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index 9fde71f..3b3e61a 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -1111,6 +1111,6 @@ def handle_helper_call( if not map_sym_tab or map_name not in map_sym_tab: raise ValueError(f"Map '{map_name}' not found in symbol table") - return invoke_helper(method_name, map_sym_tab[map_name].sym) + return invoke_helper(method_name, map_sym_tab[map_name].var) return None diff --git a/pythonbpf/local_symbol.py b/pythonbpf/local_symbol.py deleted file mode 100644 index 0abcec9..0000000 --- a/pythonbpf/local_symbol.py +++ /dev/null @@ -1,24 +0,0 @@ -import llvmlite.ir as ir -from dataclasses import dataclass -from typing import Any - - -@dataclass -class LocalSymbol: - """One name visible in a BPF function's scope. - - `var` is the storage the name resolves to: an alloca for locals, the - GlobalVariable for a name brought in by a `global` statement, or None for - the context parameter (which arrives as func.args[0], not a slot). - """ - - var: ir.AllocaInstr | ir.GlobalVariable | None - ir_type: ir.Type - metadata: Any = None - declared_global: bool = False - - def __iter__(self): - # Three fields on purpose: several call sites tuple-unpack a symbol. - yield self.var - yield self.ir_type - yield self.metadata diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index ca07845..362b34b 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -50,7 +50,12 @@ def create_bpf_map(compilation_context, map_name, map_params): map_global.align = 8 logger.info(f"Created BPF map: {map_name} with params {map_params}") - return MapSymbol(type=map_params["type"], sym=map_global, params=map_params) + return MapSymbol( + var=map_global, + ir_type=map_global.value_type, + type=map_params["type"], + params=map_params, + ) def _parse_map_params(rval, expected_args=None): @@ -112,7 +117,7 @@ def process_ringbuf_map(map_name, rval, compilation_context): map_global = create_bpf_map(compilation_context, map_name, map_params) create_ringbuf_debug_info( compilation_context, - map_global.sym, + map_global.var, map_name, map_params, ) @@ -131,7 +136,7 @@ def process_hash_map(map_name, rval, compilation_context): # Generate debug info for BTF create_map_debug_info( compilation_context, - map_global.sym, + map_global.var, map_name, map_params, ) @@ -150,7 +155,7 @@ def process_perf_event_map(map_name, rval, compilation_context): # Generate debug info for BTF create_map_debug_info( compilation_context, - map_global.sym, + map_global.var, map_name, map_params, ) diff --git a/pythonbpf/maps/maps_utils.py b/pythonbpf/maps/maps_utils.py index a271697..b8d3a1c 100644 --- a/pythonbpf/maps/maps_utils.py +++ b/pythonbpf/maps/maps_utils.py @@ -1,16 +1,15 @@ from collections.abc import Callable from dataclasses import dataclass -from llvmlite import ir from typing import Any from .map_types import BPFMapType +from ..symbols import Symbol @dataclass -class MapSymbol: - """Class representing a symbol on the map""" +class MapSymbol(Symbol): + """A BPF map: var is the map's GlobalVariable in the .maps section.""" - type: BPFMapType - sym: ir.GlobalVariable + type: BPFMapType = BPFMapType.UNSPEC params: dict[str, Any] | None = None diff --git a/pythonbpf/symbols.py b/pythonbpf/symbols.py new file mode 100644 index 0000000..b6a8bd6 --- /dev/null +++ b/pythonbpf/symbols.py @@ -0,0 +1,53 @@ +"""Symbols: what a name in a BPF program resolves to. + +Every symbol table in the compiler maps a name to one of these. The base class +carries what all of them share -- the storage behind the name and its IR type; +subclasses add what each kind of name needs on top. +""" + +from dataclasses import dataclass +from typing import Any + +import llvmlite.ir as ir + + +@dataclass +class Symbol: + """The storage a name resolves to, and the type of what is stored there. + + `var` is a pointer to that storage: an alloca for a local, a GlobalVariable + for a BPF global or a map, or None for the context parameter (which arrives + as func.args[0] rather than living in a slot). + """ + + var: ir.Value | None + ir_type: ir.Type | None + + +@dataclass +class LocalSymbol(Symbol): + """One name visible in a BPF function's scope. + + `declared_global` marks a name bound by a `global` statement: its var is + the @bpfglobal's GlobalVariable rather than an alloca. + """ + + metadata: Any = None + declared_global: bool = False + + def __iter__(self): + # Three fields on purpose: several call sites tuple-unpack a symbol. + yield self.var + yield self.ir_type + yield self.metadata + + +@dataclass +class BpfGlobalSymbol(Symbol): + """A mutable BPF global variable declared with @bpfglobal. + + Lands in .bss (zero initializer) or .data (non-zero); libbpf exposes the + section to userspace as a global-data map. + """ + + ctype_name: str = "" diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index df1b9d7..97a84c1 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -3,7 +3,7 @@ from typing import Any import ctypes from llvmlite import ir -from pythonbpf.local_symbol import LocalSymbol +from pythonbpf.symbols import LocalSymbol from pythonbpf.vmlinux_parser.assignment_info import AssignmentType logger = logging.getLogger(__name__)