Skip to content

Commit 42f2a98

Browse files
committedFeb 18, 2022
linker: rename fixupJumpsAndCalls to fixupAndValidate
Also moves the probe_read_kernel rewrite to its own function. Signed-off-by: Timo Beckers <timo@isovalent.com>
1 parent 2fa712e commit 42f2a98

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed
 

‎linker.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ func marshalLineInfos(layout []reference) ([]byte, error) {
114114
return buf.Bytes(), nil
115115
}
116116

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 {
120121
iter := insns.Iterate()
121122
for iter.Next() {
122123
ins := iter.Ins
@@ -125,21 +126,28 @@ func fixupJumpsAndCalls(insns asm.Instructions) error {
125126
return fmt.Errorf("instruction %d: map %s: %w", iter.Index, ins.Reference, asm.ErrUnsatisfiedMapReference)
126127
}
127128

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)
142130
}
143131

144132
return nil
145133
}
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+
}

‎prog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
330330
return nil, fmt.Errorf("CO-RE fixup: %w", err)
331331
}
332332

333-
if err := fixupJumpsAndCalls(insns); err != nil {
333+
if err := fixupAndValidate(insns); err != nil {
334334
return nil, err
335335
}
336336

0 commit comments

Comments
 (0)