-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Generate debug info for BPF extern declarations #161390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ use rustc_middle::ty::{self, Instance, TypeVisitableExt}; | |
| use rustc_target::spec::{Arch, Env}; | ||
| use tracing::debug; | ||
|
|
||
| use crate::base; | ||
| use crate::context::CodegenCx; | ||
| use crate::llvm::{self, Value}; | ||
|
|
||
|
|
@@ -152,6 +153,13 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t | |
|
|
||
| cx.assume_dso_local(llfn, true); | ||
|
|
||
| if tcx.is_foreign_item(instance_def_id) { | ||
| base::set_link_section(llfn, tcx.codegen_fn_attrs(instance_def_id)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted LLM-generated analysis (Codex):
|
||
| if tcx.sess.target.arch == Arch::Bpf { | ||
| cx.dbg_scope_foreign_fn(instance, fn_abi, Some(llfn)); | ||
| } | ||
| } | ||
|
|
||
| llfn | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -465,6 +465,13 @@ impl<'ll> CodegenCx<'ll, '_> { | |
| llvm::set_dllimport_storage_class(g); | ||
| } | ||
|
|
||
| if self.tcx.is_foreign_item(def_id) { | ||
| base::set_link_section(g, fn_attrs); | ||
| if self.tcx.sess.target.arch == Arch::Bpf { | ||
| debuginfo::build_extern_static_di_node(self, def_id, g); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted LLM-generated analysis (Codex):
|
||
| } | ||
| } | ||
|
|
||
| self.instances.borrow_mut().insert(instance, g); | ||
| g | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,10 +27,10 @@ use smallvec::SmallVec; | |
| use tracing::debug; | ||
|
|
||
| pub(crate) use self::di_builder::DIBuilderExt; | ||
| pub(crate) use self::metadata::build_global_var_di_node; | ||
| use self::metadata::{ | ||
| UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, spanned_type_di_node, type_di_node, | ||
| }; | ||
| pub(crate) use self::metadata::{build_extern_static_di_node, build_global_var_di_node}; | ||
| use self::namespace::mangled_name_of_instance; | ||
| use self::utils::{DIB, create_DIArray, is_node_local_to_unit}; | ||
| use crate::builder::Builder; | ||
|
|
@@ -755,3 +755,90 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { | |
| metadata::create_vtable_di_node(self, ty, trait_ref, vtable) | ||
| } | ||
| } | ||
|
|
||
| impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { | ||
| /// Creates a `DISubprogram` for a foreign function declaration (without `SPFlagDefinition`). | ||
| /// | ||
| /// This is gated to BPF targets and emits the debug info that LLVM's BPF backend | ||
| /// needs to generate BTF FUNC entries for kfunc resolution. | ||
| pub(crate) fn dbg_scope_foreign_fn( | ||
| &self, | ||
| instance: Instance<'tcx>, | ||
| fn_abi: &FnAbi<'tcx, Ty<'tcx>>, | ||
| llfn: Option<&'ll Value>, | ||
| ) { | ||
| if self.dbg_cx.is_none() { | ||
| return; | ||
| } | ||
|
|
||
| if self.sess().opts.debuginfo != DebugInfo::Full { | ||
| return; | ||
| } | ||
|
|
||
| let tcx = self.tcx; | ||
| let def_id = instance.def_id(); | ||
|
|
||
| let scope = namespace::item_namespace( | ||
| self, | ||
| DefId { | ||
| krate: def_id.krate, | ||
| index: tcx.def_key(def_id).parent.expect("dbg_scope_foreign_fn: missing parent?"), | ||
| }, | ||
| ); | ||
|
|
||
| let span = tcx.def_span(def_id); | ||
| let loc = self.lookup_debug_loc(span.lo()); | ||
| let file_metadata = file_metadata(self, &loc.file); | ||
|
|
||
| let signature: Vec<_> = iter::once(if fn_abi.ret.is_ignore() { | ||
| None | ||
| } else { | ||
| Some(type_di_node(self, fn_abi.ret.layout.ty)) | ||
| }) | ||
| .chain(fn_abi.args.iter().map(|arg| Some(type_di_node(self, arg.layout.ty)))) | ||
| .collect(); | ||
|
|
||
| let function_type_metadata = create_subroutine_type(self, &signature); | ||
|
|
||
| let mut name = String::with_capacity(64); | ||
| type_names::push_item_name(tcx, def_id, false, &mut name); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted LLM-generated analysis (Codex):
|
||
|
|
||
| let linkage_name = &mangled_name_of_instance(self, instance).name; | ||
| let linkage_name = if &name == linkage_name { "" } else { linkage_name }; | ||
|
|
||
| let scope_line = loc.line; | ||
|
|
||
| let mut flags = DIFlags::FlagPrototyped; | ||
| if fn_abi.ret.layout.is_uninhabited() { | ||
| flags |= DIFlags::FlagNoReturn; | ||
| } | ||
|
|
||
| // No SPFlagDefinition -- this is a declaration only. | ||
| let mut spflags = DISPFlags::SPFlagZero; | ||
| if self.sess().opts.optimize != config::OptLevel::No { | ||
| spflags |= DISPFlags::SPFlagOptimized; | ||
| } | ||
|
|
||
| let template_parameters = create_DIArray(DIB(self), &[]); | ||
|
|
||
| unsafe { | ||
| llvm::LLVMRustDIBuilderCreateFunction( | ||
| DIB(self), | ||
| scope, | ||
| name.as_c_char_ptr(), | ||
| name.len(), | ||
| linkage_name.as_c_char_ptr(), | ||
| linkage_name.len(), | ||
| file_metadata, | ||
| loc.line, | ||
| function_type_metadata, | ||
| scope_line, | ||
| flags, | ||
| spflags, | ||
| llfn, | ||
| template_parameters, | ||
| None, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Checks that BPF extern declarations are emitted as debug info declarations. | ||
| // | ||
| //@ only-bpf | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted LLM-generated analysis (Codex):
|
||
| //@ needs-llvm-components: bpf | ||
| //@ compile-flags: --target bpfel-unknown-none -C debuginfo=2 | ||
|
|
||
| #![no_std] | ||
| #![no_main] | ||
| #![crate_type = "lib"] | ||
|
|
||
| extern "C" { | ||
| // CHECK: !DIGlobalVariable(name: "KERNEL_VERSION" | ||
| // CHECK-SAME: isLocal: false | ||
| // CHECK-SAME: isDefinition: false | ||
| #[link_section = ".ksyms"] | ||
| pub static KERNEL_VERSION: u64; | ||
| } | ||
|
|
||
| extern "C" { | ||
| // CHECK: !DISubprogram(name: "bpf_kfunc" | ||
| // CHECK-SAME: flags: DIFlagPrototyped | ||
| // CHECK-NOT: DISPFlagDefinition | ||
| #[link_section = ".ksyms"] | ||
| pub fn bpf_kfunc(x: u64) -> u64; | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub fn test_extern_items() -> u64 { | ||
| unsafe { KERNEL_VERSION + bpf_kfunc(42) } | ||
| } | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| loop {} | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be gated to ebpf.
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've added a target gate in LinkSectionParser::convert() that rejects #[link_section] on ForeignStatic and ForeignFn when the target arch is not BPF following the same pattern as the import_name_type x86 gate a few lines above, let me know if you'd like me to follow a different approach!