@@ -6,6 +6,9 @@ use std::collections::HashSet;
66use std:: ffi:: c_void;
77use std:: ffi:: CStr ;
88use std:: ffi:: CString ;
9+ use std:: fmt:: Display ;
10+ use std:: fmt:: Formatter ;
11+ use std:: fmt:: Result as FmtResult ;
912use std:: fmt:: Write as fmt_write;
1013use std:: fs:: File ;
1114use std:: io:: stdout;
@@ -34,23 +37,28 @@ use crate::metadata::UnprocessedObj;
3437use self :: btf:: GenBtf ;
3538
3639#[ derive( Debug , PartialEq ) ]
37- pub ( crate ) enum InternalMapType {
40+ pub ( crate ) enum InternalMapType < ' name > {
3841 Data ,
42+ CustomData ( & ' name str ) ,
3943 Rodata ,
44+ CustomRodata ( & ' name str ) ,
4045 Bss ,
46+ CustomBss ( & ' name str ) ,
4147 Kconfig ,
4248 StructOps ,
4349}
4450
45- impl AsRef < str > for InternalMapType {
46- #[ inline]
47- fn as_ref ( & self ) -> & ' static str {
51+ impl Display for InternalMapType < ' _ > {
52+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> FmtResult {
4853 match self {
49- Self :: Data => "data" ,
50- Self :: Rodata => "rodata" ,
51- Self :: Bss => "bss" ,
52- Self :: Kconfig => "kconfig" ,
53- Self :: StructOps => "struct_ops" ,
54+ Self :: Data => write ! ( f, "data" ) ,
55+ Self :: CustomData ( name) => write ! ( f, "data_{}" , name) ,
56+ Self :: Rodata => write ! ( f, "rodata" ) ,
57+ Self :: CustomRodata ( name) => write ! ( f, "rodata_{}" , name) ,
58+ Self :: Bss => write ! ( f, "bss" ) ,
59+ Self :: CustomBss ( name) => write ! ( f, "bss_{}" , name) ,
60+ Self :: Kconfig => write ! ( f, "kconfig" ) ,
61+ Self :: StructOps => write ! ( f, "struct_ops" ) ,
5462 }
5563 }
5664}
@@ -193,7 +201,7 @@ fn get_raw_map_name(map: *const libbpf_sys::bpf_map) -> Result<String> {
193201 Ok ( unsafe { CStr :: from_ptr ( name_ptr) } . to_str ( ) ?. to_string ( ) )
194202}
195203
196- pub ( crate ) fn canonicalize_internal_map_name ( s : & str ) -> Option < InternalMapType > {
204+ pub ( crate ) fn canonicalize_internal_map_name ( s : & str ) -> Option < InternalMapType < ' _ > > {
197205 if s. ends_with ( ".data" ) {
198206 Some ( InternalMapType :: Data )
199207 } else if s. ends_with ( ".rodata" ) {
@@ -208,6 +216,18 @@ pub(crate) fn canonicalize_internal_map_name(s: &str) -> Option<InternalMapType>
208216 // The `*.link` extension really only sets an additional flag in lower
209217 // layers. For our intents and purposes both can be treated similarly.
210218 Some ( InternalMapType :: StructOps )
219+ // Custom data sections don't prepend bpf_object name, so we can match from
220+ // start of name.
221+ // See https://github.com/libbpf/libbpf/blob/20ea95b4505c477af3b6ff6ce9d19cee868ddc5d/src/libbpf.c#L1789-L1794
222+ } else if s. starts_with ( ".data." ) {
223+ let name = s. get ( ".data." . len ( ) ..) . unwrap ( ) ;
224+ Some ( InternalMapType :: CustomData ( name) )
225+ } else if s. starts_with ( ".rodata." ) {
226+ let name = s. get ( ".rodata." . len ( ) ..) . unwrap ( ) ;
227+ Some ( InternalMapType :: CustomRodata ( name) )
228+ } else if s. starts_with ( ".bss." ) {
229+ let name = s. get ( ".bss." . len ( ) ..) . unwrap ( ) ;
230+ Some ( InternalMapType :: CustomBss ( name) )
211231 } else {
212232 eprintln ! ( "Warning: unrecognized map: {s}" ) ;
213233 None
@@ -221,7 +241,7 @@ fn get_map_name(map: *const libbpf_sys::bpf_map) -> Result<Option<String>> {
221241 if unsafe { !libbpf_sys:: bpf_map__is_internal ( map) } {
222242 Ok ( Some ( name) )
223243 } else {
224- Ok ( canonicalize_internal_map_name ( & name) . map ( |map| map. as_ref ( ) . to_string ( ) ) )
244+ Ok ( canonicalize_internal_map_name ( & name) . map ( |map| map. to_string ( ) ) )
225245 }
226246}
227247
0 commit comments