55from .maps import maps_proc
66from .structs import structs_proc
77from .vmlinux_parser import vmlinux_proc
8+ from pythonbpf .vmlinux_parser .vmlinux_exports_handler import VmlinuxHandler
9+ from .expr import VmlinuxHandlerRegistry
810from .globals_pass import (
911 globals_list_creation ,
1012 globals_processing ,
1921import tempfile
2022from logging import Logger
2123import logging
24+ import re
2225
2326logger : Logger = logging .getLogger (__name__ )
2427
25- VERSION = "v0.1.4"
28+ VERSION = "v0.1.5"
29+
30+
31+ def finalize_module (original_str ):
32+ """After all IR generation is complete, we monkey patch btf_ama attribute"""
33+
34+ # Create a string with applied transformation of btf_ama attribute addition to BTF struct field accesses.
35+ pattern = r'(@"llvm\.[^"]+:[^"]*" = external global i64, !llvm\.preserve\.access\.index ![0-9]+)'
36+ replacement = r'\1 "btf_ama"'
37+ return re .sub (pattern , replacement , original_str )
2638
2739
2840def find_bpf_chunks (tree ):
@@ -45,11 +57,14 @@ def processor(source_code, filename, module):
4557 for func_node in bpf_chunks :
4658 logger .info (f"Found BPF function/struct: { func_node .name } " )
4759
48- vmlinux_proc (tree , module )
60+ vmlinux_symtab = vmlinux_proc (tree , module )
61+ if vmlinux_symtab :
62+ handler = VmlinuxHandler .initialize (vmlinux_symtab )
63+ VmlinuxHandlerRegistry .set_handler (handler )
64+
4965 populate_global_symbol_table (tree , module )
5066 license_processing (tree , module )
5167 globals_processing (tree , module )
52-
5368 structs_sym_tab = structs_proc (tree , module , bpf_chunks )
5469 map_sym_tab = maps_proc (tree , module , bpf_chunks )
5570 func_proc (tree , module , bpf_chunks , map_sym_tab , structs_sym_tab )
@@ -122,10 +137,12 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
122137
123138 module .add_named_metadata ("llvm.ident" , [f"PythonBPF { VERSION } " ])
124139
140+ module_string = finalize_module (str (module ))
141+
125142 logger .info (f"IR written to { output } " )
126143 with open (output , "w" ) as f :
127144 f .write (f'source_filename = "{ filename } "\n ' )
128- f .write (str ( module ) )
145+ f .write (module_string )
129146 f .write ("\n " )
130147
131148 return output , structs_sym_tab , maps_sym_tab
0 commit comments