@@ -114,9 +114,10 @@ func marshalLineInfos(layout []reference) ([]byte, error) {
114
114
return buf .Bytes (), nil
115
115
}
116
116
117
- func fixupJumpsAndCalls (insns asm.Instructions ) error {
118
- // fixupBPFCalls replaces bpf_probe_read_{kernel,user}[_str] with bpf_probe_read[_str] on older kernels
119
- // https://github.com/libbpf/libbpf/blob/master/src/libbpf.c#L6009
117
+ // fixupAndValidate is called by the ELF reader right before marshaling the
118
+ // instruction stream. It performs last-minute adjustments to the program and
119
+ // runs some sanity checks before sending it off to the kernel.
120
+ func fixupAndValidate (insns asm.Instructions ) error {
120
121
iter := insns .Iterate ()
121
122
for iter .Next () {
122
123
ins := iter .Ins
@@ -125,21 +126,28 @@ func fixupJumpsAndCalls(insns asm.Instructions) error {
125
126
return fmt .Errorf ("instruction %d: map %s: %w" , iter .Index , ins .Reference , asm .ErrUnsatisfiedMapReference )
126
127
}
127
128
128
- if ! ins .IsBuiltinCall () {
129
- continue
130
- }
131
-
132
- switch asm .BuiltinFunc (ins .Constant ) {
133
- case asm .FnProbeReadKernel , asm .FnProbeReadUser :
134
- if err := haveProbeReadKernel (); err != nil {
135
- ins .Constant = int64 (asm .FnProbeRead )
136
- }
137
- case asm .FnProbeReadKernelStr , asm .FnProbeReadUserStr :
138
- if err := haveProbeReadKernel (); err != nil {
139
- ins .Constant = int64 (asm .FnProbeReadStr )
140
- }
141
- }
129
+ fixupProbeReadKernel (ins )
142
130
}
143
131
144
132
return nil
145
133
}
134
+
135
+ // fixupProbeReadKernel replaces calls to bpf_probe_read_{kernel,user}(_str)
136
+ // with bpf_probe_read(_str) on kernels that don't support it yet.
137
+ func fixupProbeReadKernel (ins * asm.Instruction ) {
138
+ if ! ins .IsBuiltinCall () {
139
+ return
140
+ }
141
+
142
+ // Kernel supports bpf_probe_read_kernel, nothing to do.
143
+ if haveProbeReadKernel () == nil {
144
+ return
145
+ }
146
+
147
+ switch asm .BuiltinFunc (ins .Constant ) {
148
+ case asm .FnProbeReadKernel , asm .FnProbeReadUser :
149
+ ins .Constant = int64 (asm .FnProbeRead )
150
+ case asm .FnProbeReadKernelStr , asm .FnProbeReadUserStr :
151
+ ins .Constant = int64 (asm .FnProbeReadStr )
152
+ }
153
+ }
0 commit comments