Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support stripped binaries built wit CGO_ENABLED #1641

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
### Fixed

- Respect `OTEL_EXPORTER_OTLP_PROTOCOL` when `OTEL_TRACES_EXPORTER` is not set. ([#1572](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1572))
- Improve support for stripped binaries, including those built with CGO libraries. ([#1641](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1641))

## [v0.19.0-alpha] - 2024-12-05

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ docker-build:
docker-build-base:
docker buildx build -t $(IMG_NAME_BASE) --target base .

.PHONY: sample-app/nethttp sample-app/gin sample-app/databasesql sample-app/nethttp-custom sample-app/otelglobal sample-app/autosdk sample-app/kafka-go
.PHONY: sample-app/%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not expanded in a .PHONY target. It removes the functionality and completion of this target. It should not be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current version of the Makefile is not working for me for the fixture targets. The sample-app targets are not executed.

make sample-app/nethttp
make: Nothing to be done for `sample-app/nethttp'.

This change fixed that, I'm not sure why.

sample-app/%: LIBRARY=$*
sample-app/%:
if [ -f ./internal/test/e2e/$(LIBRARY)/build.sh ]; then \
Expand Down
75 changes: 46 additions & 29 deletions internal/pkg/process/binary/funcs_stripped.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package binary
import (
"debug/elf"
"debug/gosym"
"encoding/binary"
"fmt"
)

Expand All @@ -18,16 +19,21 @@ func FindFunctionsStripped(elfF *elf.File, relevantFuncs map[string]interface{})
if err != nil {
return nil, err
}
sec = elfF.Section(".gosymtab")
if sec == nil {
return nil, fmt.Errorf("%s section not found in target binary, make sure this is a Go application", ".gosymtab")
}
symTabRaw, err := sec.Data()
if err != nil {
return nil, err

// we extract the `textStart` value based on the header of the pclntab,
// this is used to parse the line number table, and is not necessarily the start of the `.text` section.
// when a binary is build with C code, the value of `textStart` is not the same as the start of the `.text` section.
// https://github.com/golang/go/blob/master/src/runtime/symtab.go#L374
var runtimeText uint64
ptrSize := uint32(pclndat[7])
if ptrSize == 4 {
runtimeText = uint64(binary.LittleEndian.Uint32(pclndat[8+2*ptrSize:]))
} else {
runtimeText = binary.LittleEndian.Uint64(pclndat[8+2*ptrSize:])
}
pcln := gosym.NewLineTable(pclndat, elfF.Section(".text").Addr)
symTab, err := gosym.NewTable(symTabRaw, pcln)

pcln := gosym.NewLineTable(pclndat, runtimeText)
symTab, err := gosym.NewTable(nil, pcln)
if err != nil {
return nil, err
}
Expand All @@ -54,34 +60,45 @@ func FindFunctionsStripped(elfF *elf.File, relevantFuncs map[string]interface{})
}

func findFuncOffsetStripped(f *gosym.Func, elfF *elf.File) (uint64, []uint64, error) {
text := elfF.Section(".text")
if text == nil {
return 0, nil, fmt.Errorf(".text section not found in target binary")
}

var off uint64
funcLen := f.End - f.Entry
data := make([]byte, funcLen)

_, err := text.ReadAt(data, int64(f.Entry-text.Addr))
if err != nil {
return 0, nil, err
}

retInstructionOffsets, err := findRetInstructions(data)
if err != nil {
return 0, nil, err
}

for _, prog := range elfF.Progs {
if prog.Type != elf.PT_LOAD || (prog.Flags&elf.PF_X) == 0 {
continue
}

// For more info on this calculation: stackoverflow.com/a/40249502
if prog.Vaddr <= f.Value && f.Value < (prog.Vaddr+prog.Memsz) {
off := f.Value - prog.Vaddr + prog.Off

funcLen := f.End - f.Entry
data := make([]byte, funcLen)
_, err := prog.ReadAt(data, int64(f.Value-prog.Vaddr))
if err != nil {
return 0, nil, err
}

instructionIndices, err := findRetInstructions(data)
if err != nil {
return 0, nil, err
}
off = f.Value - prog.Vaddr + prog.Off
break
}
}

newLocations := make([]uint64, len(instructionIndices))
for i, instructionIndex := range instructionIndices {
newLocations[i] = instructionIndex + off
}
if off == 0 {
return 0, nil, fmt.Errorf("could not find function offset")
}

return off, newLocations, nil
}
retOffsets := make([]uint64, len(retInstructionOffsets))
for i, instructionOffset := range retInstructionOffsets {
retOffsets[i] = instructionOffset + off
}
return 0, nil, fmt.Errorf("prog not found")

return off, retOffsets, nil
}
Loading