Improve error handling in compile, pass structs_sym_tab and maps_sym_tab to BpfProgram

This commit is contained in:
Pragyansh Chaturvedi
2025-10-17 03:25:15 +05:30
parent fc55b7ecaa
commit 60737d9894

View File

@ -55,6 +55,7 @@ def processor(source_code, filename, module):
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab) func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
globals_list_creation(tree, module) globals_list_creation(tree, module)
return structs_sym_tab, map_sym_tab
def compile_to_ir(filename: str, output: str, loglevel=logging.INFO): def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
@ -80,7 +81,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
True, True,
) )
processor(source, filename, module) structs_sym_tab, maps_sym_tab = processor(source, filename, module)
wchar_size = module.add_metadata( wchar_size = module.add_metadata(
[ [
@ -127,7 +128,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
f.write(str(module)) f.write(str(module))
f.write("\n") f.write("\n")
return output return output, structs_sym_tab, maps_sym_tab
def _run_llc(ll_file, obj_file): def _run_llc(ll_file, obj_file):
@ -165,15 +166,14 @@ def compile(loglevel=logging.INFO) -> bool:
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
o_file = caller_file.with_suffix(".o") o_file = caller_file.with_suffix(".o")
success = True compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel)
success = (
compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel) and success
)
success = _run_llc(ll_file, o_file) and success if not _run_llc(ll_file, o_file):
logger.error("Compilation to object file failed.")
return False
logger.info(f"Object written to {o_file}") logger.info(f"Object written to {o_file}")
return success return True
def BPF(loglevel=logging.INFO) -> BpfProgram: def BPF(loglevel=logging.INFO) -> BpfProgram:
@ -189,7 +189,11 @@ def BPF(loglevel=logging.INFO) -> BpfProgram:
f.write(src) f.write(src)
f.flush() f.flush()
source = f.name source = f.name
compile_to_ir(source, str(inter.name), loglevel=loglevel) _, structs_sym_tab, maps_sym_tab = compile_to_ir(
source, str(inter.name), loglevel=loglevel
)
_run_llc(str(inter.name), str(obj_file.name)) _run_llc(str(inter.name), str(obj_file.name))
return BpfProgram(str(obj_file.name)) return BpfProgram(
str(obj_file.name), structs=structs_sym_tab, maps=maps_sym_tab
)