add dwarf behaviour conditions

This commit is contained in:
2025-09-30 20:26:38 +05:30
parent 6afcffb4ed
commit ce9be8750d

View File

@ -15,6 +15,7 @@ import tempfile
VERSION = "v0.1.3" VERSION = "v0.1.3"
def find_bpf_chunks(tree): def find_bpf_chunks(tree):
"""Find all functions decorated with @bpf in the AST.""" """Find all functions decorated with @bpf in the AST."""
bpf_functions = [] bpf_functions = []
@ -58,7 +59,7 @@ def compile_to_ir(filename: str, output: str):
}) })
module._debug_compile_unit = module.add_debug_info("DICompileUnit", { # type: ignore module._debug_compile_unit = module.add_debug_info("DICompileUnit", { # type: ignore
"language": 29, # DW_LANG_C11 "language": DW_LANG_C11,
"file": module._file_metadata, # type: ignore "file": module._file_metadata, # type: ignore
"producer": f"PythonBPF {VERSION}", "producer": f"PythonBPF {VERSION}",
"isOptimized": True, "isOptimized": True,
@ -73,21 +74,21 @@ def compile_to_ir(filename: str, output: str):
processor(source, filename, module) processor(source, filename, module)
wchar_size = module.add_metadata([ir.Constant(ir.IntType(32), 1), wchar_size = module.add_metadata([DwarfBehaviorEnum.ERROR_IF_MISMATCH,
"wchar_size", "wchar_size",
ir.Constant(ir.IntType(32), 4)]) ir.Constant(ir.IntType(32), 4)])
frame_pointer = module.add_metadata([ir.Constant(ir.IntType(32), 7), frame_pointer = module.add_metadata([DwarfBehaviorEnum.OVERRIDE_USE_LARGEST,
"frame-pointer", "frame-pointer",
ir.Constant(ir.IntType(32), 2)]) ir.Constant(ir.IntType(32), 2)])
# Add Debug Info Version (3 = DWARF v3, which LLVM expects) # Add Debug Info Version (3 = DWARF v3, which LLVM expects)
debug_info_version = module.add_metadata([ir.Constant(ir.IntType(32), 2), debug_info_version = module.add_metadata([DwarfBehaviorEnum.WARNING_IF_MISMATCH,
"Debug Info Version", "Debug Info Version",
ir.Constant(ir.IntType(32), 3)]) ir.Constant(ir.IntType(32), 3)])
# Add explicit DWARF version (4 is common, works with LLVM BPF backend) # Add explicit DWARF version 5
dwarf_version = module.add_metadata([ir.Constant(ir.IntType(32), 2), dwarf_version = module.add_metadata([DwarfBehaviorEnum.OVERRIDE_USE_LARGEST,
"Dwarf Version", "Dwarf Version",
ir.Constant(ir.IntType(32), 4)]) ir.Constant(ir.IntType(32), 5)])
module.add_named_metadata("llvm.module.flags", wchar_size) module.add_named_metadata("llvm.module.flags", wchar_size)
module.add_named_metadata("llvm.module.flags", frame_pointer) module.add_named_metadata("llvm.module.flags", frame_pointer)