Skip to content

Commit

Permalink
linker: rename fixupJumpsAndCalls to fixupAndValidate
Browse files Browse the repository at this point in the history
Also moves the probe_read_kernel rewrite to its own function.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Feb 18, 2022
1 parent 2fa712e commit 42f2a98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
42 changes: 25 additions & 17 deletions linker.go
Expand Up @@ -114,9 +114,10 @@ func marshalLineInfos(layout []reference) ([]byte, error) {
return buf.Bytes(), nil
}

func fixupJumpsAndCalls(insns asm.Instructions) error {
// fixupBPFCalls replaces bpf_probe_read_{kernel,user}[_str] with bpf_probe_read[_str] on older kernels
// https://github.com/libbpf/libbpf/blob/master/src/libbpf.c#L6009
// fixupAndValidate is called by the ELF reader right before marshaling the
// instruction stream. It performs last-minute adjustments to the program and
// runs some sanity checks before sending it off to the kernel.
func fixupAndValidate(insns asm.Instructions) error {
iter := insns.Iterate()
for iter.Next() {
ins := iter.Ins
Expand All @@ -125,21 +126,28 @@ func fixupJumpsAndCalls(insns asm.Instructions) error {
return fmt.Errorf("instruction %d: map %s: %w", iter.Index, ins.Reference, asm.ErrUnsatisfiedMapReference)
}

if !ins.IsBuiltinCall() {
continue
}

switch asm.BuiltinFunc(ins.Constant) {
case asm.FnProbeReadKernel, asm.FnProbeReadUser:
if err := haveProbeReadKernel(); err != nil {
ins.Constant = int64(asm.FnProbeRead)
}
case asm.FnProbeReadKernelStr, asm.FnProbeReadUserStr:
if err := haveProbeReadKernel(); err != nil {
ins.Constant = int64(asm.FnProbeReadStr)
}
}
fixupProbeReadKernel(ins)
}

return nil
}

// fixupProbeReadKernel replaces calls to bpf_probe_read_{kernel,user}(_str)
// with bpf_probe_read(_str) on kernels that don't support it yet.
func fixupProbeReadKernel(ins *asm.Instruction) {
if !ins.IsBuiltinCall() {
return
}

// Kernel supports bpf_probe_read_kernel, nothing to do.
if haveProbeReadKernel() == nil {
return
}

switch asm.BuiltinFunc(ins.Constant) {
case asm.FnProbeReadKernel, asm.FnProbeReadUser:
ins.Constant = int64(asm.FnProbeRead)
case asm.FnProbeReadKernelStr, asm.FnProbeReadUserStr:
ins.Constant = int64(asm.FnProbeReadStr)
}
}
2 changes: 1 addition & 1 deletion prog.go
Expand Up @@ -330,7 +330,7 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
return nil, fmt.Errorf("CO-RE fixup: %w", err)
}

if err := fixupJumpsAndCalls(insns); err != nil {
if err := fixupAndValidate(insns); err != nil {
return nil, err
}

Expand Down

0 comments on commit 42f2a98

Please sign in to comment.