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

pkg: get GoVersion by buildinfo package from ELF (by golang compiled) (#262) #295

Merged
merged 1 commit into from
Jan 17, 2023
Merged
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
38 changes: 6 additions & 32 deletions pkg/proc/proc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package proc

import (
"debug/dwarf"
"debug/elf"
"debug/buildinfo"
"errors"
"strconv"
"strings"
Expand Down Expand Up @@ -34,40 +33,15 @@ func (v *GoVersion) After(major, minor int) bool {

// ExtraceGoVersion extracts Go version info from a binary that is built with Go toolchain
func ExtraceGoVersion(path string) (*GoVersion, error) {
file, err := elf.Open(path)
if err != nil {
return nil, err
bi, e := buildinfo.ReadFile(path)
if e != nil {
return nil, ErrVersionNotFound
}
defer file.Close()

raw, err := file.DWARF()
gv, err := parseGoVersion(bi.GoVersion)
if err != nil {
return nil, err
}

reader := raw.Reader()
for {
entry, err := reader.Next()
if err != nil {
return nil, err
}

if entry == nil {
break
}

for _, field := range entry.Field {
if field.Attr == dwarf.AttrProducer {
val, ok := field.Val.(string)
if !ok {
continue
}
return parseGoVersion(val)
}
}
}

return nil, ErrVersionNotFound
return gv, nil
}

func parseGoVersion(r string) (*GoVersion, error) {
Expand Down