Skip to content

Commit

Permalink
pkg/symbol/elfutils: Fix handling of stripped Go binaries
Browse files Browse the repository at this point in the history
Go binaries that are stripped, for example built with:

```
go build -ldflags "-w -s" main.go
```

Have an empty symbol table section, which some additional build steps
may strip, since it's empty.

Before this patch, those Go binaries would run into an error case when
attempting to load the symbol table.

Since the only truly necessary section for symbolization is the
`.gopclntab` section, this patch proposes to only check for this
section.
  • Loading branch information
brancz committed Nov 9, 2022
1 parent a6cae59 commit 26fa1e9
Showing 1 changed file with 0 additions and 29 deletions.
29 changes: 0 additions & 29 deletions pkg/symbol/elfutils/elfutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,6 @@ func readableDWARFSections(f *elf.File) (map[string]struct{}, error) {
// IsSymbolizableGoObjFile checks whether the specified executable or library file is generated by Go toolchain
// and has necessary symbol information attached.
func IsSymbolizableGoObjFile(f *elf.File) (bool, error) {
// Checks ".note.go.buildid" section and symtab better to keep those sections in object file.
isGo := false
for _, s := range f.Sections {
if s.Name == ".note.go.buildid" {
isGo = true
}
}

// In case ".note.go.buildid" section is stripped, check for symbols.
if !isGo {
syms, err := f.Symbols()
if err != nil {
return false, fmt.Errorf("failed to read symbols: %w", err)
}
for _, sym := range syms {
name := sym.Name
if name == "runtime.main" || name == "main.main" {
isGo = true
}
if name == "runtime.buildVersion" {
isGo = true
}
}
}

if !isGo {
return false, nil
}

// Check if the Go binary symbolizable.
// Go binaries has a special case. They use ".gopclntab" section to symbolize addresses.
if sec := f.Section(".gopclntab"); sec != nil {
Expand Down

0 comments on commit 26fa1e9

Please sign in to comment.