@@ -15,6 +15,7 @@ import (
15
15
"github.com/cilium/ebpf/asm"
16
16
"github.com/cilium/ebpf/btf"
17
17
"github.com/cilium/ebpf/internal"
18
+ "github.com/cilium/ebpf/internal/sys"
18
19
"github.com/cilium/ebpf/internal/unix"
19
20
)
20
21
@@ -1181,109 +1182,106 @@ func (ec *elfCode) loadKsymsSection() error {
1181
1182
return nil
1182
1183
}
1183
1184
1185
+ type libbpfElfSectionDef struct {
1186
+ pattern string
1187
+ programType sys.ProgType
1188
+ attachType sys.AttachType
1189
+ flags libbpfElfSectionFlag
1190
+ }
1191
+
1192
+ type libbpfElfSectionFlag uint32
1193
+
1194
+ // The values correspond to enum sec_def_flags in libbpf.
1195
+ const (
1196
+ _SEC_NONE libbpfElfSectionFlag = 0
1197
+
1198
+ _SEC_EXP_ATTACH_OPT libbpfElfSectionFlag = 1 << (iota - 1 )
1199
+ _SEC_ATTACHABLE
1200
+ _SEC_ATTACH_BTF
1201
+ _SEC_SLEEPABLE
1202
+ _SEC_XDP_FRAGS
1203
+ _SEC_USDT
1204
+
1205
+ // Ignore any present extra in order to preserve backwards compatibility
1206
+ // with earlier versions of the library.
1207
+ ignoreExtra
1208
+
1209
+ _SEC_ATTACHABLE_OPT = _SEC_ATTACHABLE | _SEC_EXP_ATTACH_OPT
1210
+ )
1211
+
1212
+ func init () {
1213
+ // Compatibility with older versions of the library.
1214
+ // We prepend libbpf definitions since they contain a prefix match
1215
+ // for "xdp".
1216
+ elfSectionDefs = append ([]libbpfElfSectionDef {
1217
+ {"xdp.frags/" , sys .BPF_PROG_TYPE_XDP , sys .BPF_XDP , _SEC_XDP_FRAGS | ignoreExtra },
1218
+ {"xdp.frags_devmap/" , sys .BPF_PROG_TYPE_XDP , sys .BPF_XDP_DEVMAP , _SEC_XDP_FRAGS },
1219
+ {"xdp_devmap/" , sys .BPF_PROG_TYPE_XDP , sys .BPF_XDP_DEVMAP , 0 },
1220
+ {"xdp.frags_cpumap/" , sys .BPF_PROG_TYPE_XDP , sys .BPF_XDP_CPUMAP , _SEC_XDP_FRAGS },
1221
+ {"xdp_cpumap/" , sys .BPF_PROG_TYPE_XDP , sys .BPF_XDP_CPUMAP , 0 },
1222
+ // This has been in the library since the beginning of time. Not sure
1223
+ // where it came from.
1224
+ {"seccomp" , sys .BPF_PROG_TYPE_SOCKET_FILTER , 0 , _SEC_NONE },
1225
+ }, elfSectionDefs ... )
1226
+ }
1227
+
1184
1228
func getProgType (sectionName string ) (ProgramType , AttachType , uint32 , string ) {
1185
- types := []struct {
1186
- prefix string
1187
- progType ProgramType
1188
- attachType AttachType
1189
- progFlags uint32
1190
- }{
1191
- // Please update the types from libbpf.c and follow the order of it.
1192
- // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/lib/bpf/libbpf.c
1193
- {"socket" , SocketFilter , AttachNone , 0 },
1194
- {"sk_reuseport/migrate" , SkReuseport , AttachSkReuseportSelectOrMigrate , 0 },
1195
- {"sk_reuseport" , SkReuseport , AttachSkReuseportSelect , 0 },
1196
- {"kprobe/" , Kprobe , AttachNone , 0 },
1197
- {"uprobe/" , Kprobe , AttachNone , 0 },
1198
- {"kretprobe/" , Kprobe , AttachNone , 0 },
1199
- {"uretprobe/" , Kprobe , AttachNone , 0 },
1200
- {"tc" , SchedCLS , AttachNone , 0 },
1201
- {"classifier" , SchedCLS , AttachNone , 0 },
1202
- {"action" , SchedACT , AttachNone , 0 },
1203
- {"tracepoint/" , TracePoint , AttachNone , 0 },
1204
- {"tp/" , TracePoint , AttachNone , 0 },
1205
- {"raw_tracepoint/" , RawTracepoint , AttachNone , 0 },
1206
- {"raw_tp/" , RawTracepoint , AttachNone , 0 },
1207
- {"raw_tracepoint.w/" , RawTracepointWritable , AttachNone , 0 },
1208
- {"raw_tp.w/" , RawTracepointWritable , AttachNone , 0 },
1209
- {"tp_btf/" , Tracing , AttachTraceRawTp , 0 },
1210
- {"fentry/" , Tracing , AttachTraceFEntry , 0 },
1211
- {"fmod_ret/" , Tracing , AttachModifyReturn , 0 },
1212
- {"fexit/" , Tracing , AttachTraceFExit , 0 },
1213
- {"fentry.s/" , Tracing , AttachTraceFEntry , unix .BPF_F_SLEEPABLE },
1214
- {"fmod_ret.s/" , Tracing , AttachModifyReturn , unix .BPF_F_SLEEPABLE },
1215
- {"fexit.s/" , Tracing , AttachTraceFExit , unix .BPF_F_SLEEPABLE },
1216
- {"freplace/" , Extension , AttachNone , 0 },
1217
- {"lsm/" , LSM , AttachLSMMac , 0 },
1218
- {"lsm.s/" , LSM , AttachLSMMac , unix .BPF_F_SLEEPABLE },
1219
- {"iter/" , Tracing , AttachTraceIter , 0 },
1220
- {"iter.s/" , Tracing , AttachTraceIter , unix .BPF_F_SLEEPABLE },
1221
- {"syscall" , Syscall , AttachNone , 0 },
1222
- {"xdp.frags_devmap/" , XDP , AttachXDPDevMap , unix .BPF_F_XDP_HAS_FRAGS },
1223
- {"xdp_devmap/" , XDP , AttachXDPDevMap , 0 },
1224
- {"xdp.frags_cpumap/" , XDP , AttachXDPCPUMap , unix .BPF_F_XDP_HAS_FRAGS },
1225
- {"xdp_cpumap/" , XDP , AttachXDPCPUMap , 0 },
1226
- {"xdp.frags" , XDP , AttachNone , unix .BPF_F_XDP_HAS_FRAGS },
1227
- {"xdp" , XDP , AttachNone , 0 },
1228
- {"perf_event" , PerfEvent , AttachNone , 0 },
1229
- {"lwt_in" , LWTIn , AttachNone , 0 },
1230
- {"lwt_out" , LWTOut , AttachNone , 0 },
1231
- {"lwt_xmit" , LWTXmit , AttachNone , 0 },
1232
- {"lwt_seg6local" , LWTSeg6Local , AttachNone , 0 },
1233
- {"cgroup_skb/ingress" , CGroupSKB , AttachCGroupInetIngress , 0 },
1234
- {"cgroup_skb/egress" , CGroupSKB , AttachCGroupInetEgress , 0 },
1235
- {"cgroup/skb" , CGroupSKB , AttachNone , 0 },
1236
- {"cgroup/sock_create" , CGroupSock , AttachCGroupInetSockCreate , 0 },
1237
- {"cgroup/sock_release" , CGroupSock , AttachCgroupInetSockRelease , 0 },
1238
- {"cgroup/sock" , CGroupSock , AttachCGroupInetSockCreate , 0 },
1239
- {"cgroup/post_bind4" , CGroupSock , AttachCGroupInet4PostBind , 0 },
1240
- {"cgroup/post_bind6" , CGroupSock , AttachCGroupInet6PostBind , 0 },
1241
- {"cgroup/dev" , CGroupDevice , AttachCGroupDevice , 0 },
1242
- {"sockops" , SockOps , AttachCGroupSockOps , 0 },
1243
- {"sk_skb/stream_parser" , SkSKB , AttachSkSKBStreamParser , 0 },
1244
- {"sk_skb/stream_verdict" , SkSKB , AttachSkSKBStreamVerdict , 0 },
1245
- {"sk_skb" , SkSKB , AttachNone , 0 },
1246
- {"sk_msg" , SkMsg , AttachSkMsgVerdict , 0 },
1247
- {"lirc_mode2" , LircMode2 , AttachLircMode2 , 0 },
1248
- {"flow_dissector" , FlowDissector , AttachFlowDissector , 0 },
1249
- {"cgroup/bind4" , CGroupSockAddr , AttachCGroupInet4Bind , 0 },
1250
- {"cgroup/bind6" , CGroupSockAddr , AttachCGroupInet6Bind , 0 },
1251
- {"cgroup/connect4" , CGroupSockAddr , AttachCGroupInet4Connect , 0 },
1252
- {"cgroup/connect6" , CGroupSockAddr , AttachCGroupInet6Connect , 0 },
1253
- {"cgroup/sendmsg4" , CGroupSockAddr , AttachCGroupUDP4Sendmsg , 0 },
1254
- {"cgroup/sendmsg6" , CGroupSockAddr , AttachCGroupUDP6Sendmsg , 0 },
1255
- {"cgroup/recvmsg4" , CGroupSockAddr , AttachCGroupUDP4Recvmsg , 0 },
1256
- {"cgroup/recvmsg6" , CGroupSockAddr , AttachCGroupUDP6Recvmsg , 0 },
1257
- {"cgroup/getpeername4" , CGroupSockAddr , AttachCgroupInet4GetPeername , 0 },
1258
- {"cgroup/getpeername6" , CGroupSockAddr , AttachCgroupInet6GetPeername , 0 },
1259
- {"cgroup/getsockname4" , CGroupSockAddr , AttachCgroupInet4GetSockname , 0 },
1260
- {"cgroup/getsockname6" , CGroupSockAddr , AttachCgroupInet6GetSockname , 0 },
1261
- {"cgroup/sysctl" , CGroupSysctl , AttachCGroupSysctl , 0 },
1262
- {"cgroup/getsockopt" , CGroupSockopt , AttachCGroupGetsockopt , 0 },
1263
- {"cgroup/setsockopt" , CGroupSockopt , AttachCGroupSetsockopt , 0 },
1264
- {"struct_ops+" , StructOps , AttachNone , 0 },
1265
- {"sk_lookup/" , SkLookup , AttachSkLookup , 0 },
1266
- {"seccomp" , SocketFilter , AttachNone , 0 },
1267
- {"kprobe.multi" , Kprobe , AttachTraceKprobeMulti , 0 },
1268
- {"kretprobe.multi" , Kprobe , AttachTraceKprobeMulti , 0 },
1269
- // Document all prefixes in docs/ebpf/concepts/elf-sections.md.
1270
- }
1229
+ // Skip optional program marking for now.
1230
+ sectionName = strings .TrimPrefix (sectionName , "?" )
1271
1231
1272
- for _ , t := range types {
1273
- if ! strings .HasPrefix (sectionName , t .prefix ) {
1232
+ for _ , t := range elfSectionDefs {
1233
+ extra , ok := matchSectionName (sectionName , t .pattern )
1234
+ if ! ok {
1274
1235
continue
1275
1236
}
1276
1237
1277
- if ! strings .HasSuffix (t .prefix , "/" ) {
1278
- return t .progType , t .attachType , t .progFlags , ""
1238
+ programType := ProgramType (t .programType )
1239
+ attachType := AttachType (t .attachType )
1240
+
1241
+ var flags uint32
1242
+ if t .flags & _SEC_SLEEPABLE > 0 {
1243
+ flags |= unix .BPF_F_SLEEPABLE
1244
+ }
1245
+ if t .flags & _SEC_XDP_FRAGS > 0 {
1246
+ flags |= unix .BPF_F_XDP_HAS_FRAGS
1247
+ }
1248
+ if t .flags & _SEC_EXP_ATTACH_OPT > 0 {
1249
+ if programType == XDP {
1250
+ // The library doesn't yet have code to fallback to not specifying
1251
+ // attach type. Only do this for XDP since we've enforced correct
1252
+ // attach type for all other program types.
1253
+ attachType = AttachNone
1254
+ }
1255
+ }
1256
+ if t .flags & ignoreExtra > 0 {
1257
+ extra = ""
1279
1258
}
1280
1259
1281
- return t . progType , t . attachType , t . progFlags , sectionName [ len ( t . prefix ):]
1260
+ return programType , attachType , flags , extra
1282
1261
}
1283
1262
1284
1263
return UnspecifiedProgram , AttachNone , 0 , ""
1285
1264
}
1286
1265
1266
+ // matchSectionName checks a section name against a pattern.
1267
+ //
1268
+ // It's behaviour mirrors that of libbpf's sec_def_matches.
1269
+ func matchSectionName (sectionName , pattern string ) (extra string , found bool ) {
1270
+ have , extra , found := strings .Cut (sectionName , "/" )
1271
+ want := strings .TrimRight (pattern , "+/" )
1272
+
1273
+ if strings .HasSuffix (pattern , "/" ) {
1274
+ // Section name must have a slash and extra may be empty.
1275
+ return extra , have == want && found
1276
+ } else if strings .HasSuffix (pattern , "+" ) {
1277
+ // Section name may have a slash and extra may be empty.
1278
+ return extra , have == want
1279
+ }
1280
+
1281
+ // Section name must have a prefix. extra is ignored.
1282
+ return "" , strings .HasPrefix (sectionName , pattern )
1283
+ }
1284
+
1287
1285
func (ec * elfCode ) loadSectionRelocations (sec * elf.Section , symbols []elf.Symbol ) (map [uint64 ]elf.Symbol , error ) {
1288
1286
rels := make (map [uint64 ]elf.Symbol )
1289
1287
0 commit comments