@@ -6,6 +6,9 @@ use std::collections::HashSet;
6
6
use std:: ffi:: c_void;
7
7
use std:: ffi:: CStr ;
8
8
use std:: ffi:: CString ;
9
+ use std:: fmt:: Display ;
10
+ use std:: fmt:: Formatter ;
11
+ use std:: fmt:: Result as FmtResult ;
9
12
use std:: fmt:: Write as fmt_write;
10
13
use std:: fs:: File ;
11
14
use std:: io:: stdout;
@@ -34,23 +37,28 @@ use crate::metadata::UnprocessedObj;
34
37
use self :: btf:: GenBtf ;
35
38
36
39
#[ derive( Debug , PartialEq ) ]
37
- pub ( crate ) enum InternalMapType {
40
+ pub ( crate ) enum InternalMapType < ' name > {
38
41
Data ,
42
+ CustomData ( & ' name str ) ,
39
43
Rodata ,
44
+ CustomRodata ( & ' name str ) ,
40
45
Bss ,
46
+ CustomBss ( & ' name str ) ,
41
47
Kconfig ,
42
48
StructOps ,
43
49
}
44
50
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 {
48
53
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" ) ,
54
62
}
55
63
}
56
64
}
@@ -193,7 +201,7 @@ fn get_raw_map_name(map: *const libbpf_sys::bpf_map) -> Result<String> {
193
201
Ok ( unsafe { CStr :: from_ptr ( name_ptr) } . to_str ( ) ?. to_string ( ) )
194
202
}
195
203
196
- pub ( crate ) fn canonicalize_internal_map_name ( s : & str ) -> Option < InternalMapType > {
204
+ pub ( crate ) fn canonicalize_internal_map_name ( s : & str ) -> Option < InternalMapType < ' _ > > {
197
205
if s. ends_with ( ".data" ) {
198
206
Some ( InternalMapType :: Data )
199
207
} else if s. ends_with ( ".rodata" ) {
@@ -208,6 +216,18 @@ pub(crate) fn canonicalize_internal_map_name(s: &str) -> Option<InternalMapType>
208
216
// The `*.link` extension really only sets an additional flag in lower
209
217
// layers. For our intents and purposes both can be treated similarly.
210
218
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) )
211
231
} else {
212
232
eprintln ! ( "Warning: unrecognized map: {s}" ) ;
213
233
None
@@ -221,7 +241,7 @@ fn get_map_name(map: *const libbpf_sys::bpf_map) -> Result<Option<String>> {
221
241
if unsafe { !libbpf_sys:: bpf_map__is_internal ( map) } {
222
242
Ok ( Some ( name) )
223
243
} 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 ( ) ) )
225
245
}
226
246
}
227
247
0 commit comments