-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathgoversion.go
More file actions
276 lines (239 loc) · 6.79 KB
/
Copy pathgoversion.go
File metadata and controls
276 lines (239 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (C) 2019-2026 GoRE Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gore
import (
"bytes"
"errors"
"go/version"
"regexp"
"strings"
"golang.org/x/arch/x86/x86asm"
)
var goVersionMatcher = regexp.MustCompile(`(go[\d+.]*(beta|rc)?[\d*])`)
// GoVersion holds information about the compiler version.
type GoVersion struct {
// Name is a string representation of the version.
Name string
// SHA is a digest of the git commit for the release.
SHA string
// Timestamp is a string of the timestamp when the commit was created.
Timestamp string
}
// stripVersionSuffix removes GOEXPERIMENT suffixes from Go version strings.
// Go ≤ 1.25 uses space: "go1.23.11 X:nocoverageredesign"
// Go ≥ 1.26 uses hyphen: "go1.26.0-X:jsonv2"
// Dev builds may use both: "go1.27-abc X:foo"
func stripVersionSuffix(v string) string {
v, _, _ = strings.Cut(v, " ")
v, _, _ = strings.Cut(v, "-")
return v
}
// ResolveGoVersion tries to return the GoVersion for the given tag.
// For example the tag: go1 will return a GoVersion struct representing version 1.0 of the compiler.
// It handles GOEXPERIMENT suffixes (both space and hyphen separated) by stripping them.
// If no goversion for the given tag is found, nil is returned.
func ResolveGoVersion(tag string) *GoVersion {
if v, ok := goversions[tag]; ok {
return v
}
if base := stripVersionSuffix(tag); base != tag {
if v, ok := goversions[base]; ok {
return v
}
}
return nil
}
// GoVersionCompare compares two version strings.
// If a < b, -1 is returned.
// If a == b, 0 is returned.
// If a > b, 1 is returned.
func GoVersionCompare(a, b string) int {
if a == b {
return 0
}
cleanA := stripVersionSuffix(a)
cleanB := stripVersionSuffix(b)
if !version.IsValid(cleanA) || !version.IsValid(cleanB) {
return strings.Compare(a, b)
}
return version.Compare(cleanA, cleanB)
}
func findGoCompilerVersion(f *GoFile) (*GoVersion, error) {
// if DWARF debug info exists, then this can simply be obtained from there
if dwarfVer, ok := getBuildVersionFromDwarf(f.fh); ok {
if ver := ResolveGoVersion(dwarfVer); ver != nil {
return ver, nil
}
// Unknown version but DWARF had a value — return it as-is
if stripped := stripVersionSuffix(dwarfVer); stripped != "" {
return &GoVersion{Name: stripped}, nil
}
}
// Try to determine the version based on the schedinit function.
if v := tryFromSchedInit(f); v != nil {
return v, nil
}
// If no version was found, search the sections for the
// version string.
data, err := f.fh.getRData()
// If a read-only data section does not exist, try text.
if errors.Is(err, ErrSectionDoesNotExist) {
_, data, err = f.fh.getCodeSection()
}
if err != nil {
return nil, err
}
for {
version := matchGoVersionString(data)
if version == "" {
return nil, ErrNoGoVersionFound
}
ver := ResolveGoVersion(version)
// Go before 1.4 does not have the version string, so if we have found
// a version string below 1.4beta1 it is a false positive.
if ver == nil || GoVersionCompare(ver.Name, "go1.4beta1") < 0 {
off := bytes.Index(data, []byte(version))
// No match
if off == -1 {
break
}
data = data[off+2:]
continue
}
return ver, nil
}
return nil, nil
}
// tryFromSchedInit tries to identify the version of the Go compiler that compiled the code.
// The function "schedinit" in the "runtime" package has the only reference to this string
// used to identify the version.
// The function returns nil if no version is found.
func tryFromSchedInit(f *GoFile) *GoVersion {
// Check for non-supported architectures.
if f.FileInfo.Arch != Arch386 && f.FileInfo.Arch != ArchAMD64 {
return nil
}
var addr, size uint64
var fcn *Function
var std []*Package
var err error
is32 := false
if f.FileInfo.Arch == Arch386 {
is32 = true
}
sym, err := f.fh.getSymbol("runtime.schedinit")
if err == nil {
addr = sym.Value
size = sym.Size
goto disasm
}
// Find schedinit function.
std, err = f.GetSTDLib()
if err != nil {
return nil
}
pkgLoop:
for _, v := range std {
if v.Name != "runtime" {
continue
}
for _, vv := range v.Functions {
if vv.Name != "schedinit" {
continue
}
fcn = vv
break pkgLoop
}
}
// Check if the function was found
if fcn == nil {
// If we can't find the function, there is nothing to do.
return nil
}
addr = fcn.Offset
size = fcn.End - fcn.Offset
disasm:
// Get the raw hex.
buf, err := f.Bytes(addr, size)
if err != nil {
return nil
}
/*
Disassemble the function until the loading of the Go version is found.
*/
// Counter for how many bytes has been read.
s := 0
mode := f.FileInfo.WordSize * 8
for s < len(buf) {
inst, err := x86asm.Decode(buf[s:], mode)
if err != nil {
// If we fail to decode the instruction, something is wrong so
// bailout.
return nil
}
// Update next instruction location.
s = s + inst.Len
// Check if it's a "lea" instruction.
if inst.Op != x86asm.LEA {
continue
}
// Check what it's loading and if it's pointing to the compiler version used.
// First assume that the address is a direct addressing.
arg := inst.Args[1].(x86asm.Mem)
disp := arg.Disp
if arg.Base == x86asm.EIP || arg.Base == x86asm.RIP {
// If the addressing is based on the instruction pointer, fix the address.
disp += int64(addr) + int64(s)
}
// If the addressing is based on the stack pointer, this is not the right
// instruction.
if arg.Base == x86asm.ESP || arg.Base == x86asm.RSP {
continue
}
// Resolve the pointer to the string. If we get no data, this is not the
// right instruction.
b, _ := f.Bytes(uint64(disp), uint64(0x20))
if b == nil {
continue
}
// Try two strategies:
// 1. Classic (Go <= 1.25): LEA loads pointer to string header {data_ptr, len}.
// 2. New (Go 1.26+): LEA loads pointer to string content directly
// (e.g. LEAQ go:string.*+offset(SB), AX).
var bstr []byte
// Strategy 1: interpret as string header.
r := bytes.NewReader(b)
ptr, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err == nil {
l, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err == nil && l > 0 && l < 1024 {
bstr, _ = f.Bytes(ptr, l)
}
}
// Strategy 2: LEA target is the string content itself.
if !bytes.HasPrefix(bstr, []byte("go1.")) {
bstr = b
}
if !bytes.HasPrefix(bstr, []byte("go1.")) {
continue
}
// Extract just the version part (stop at first non-version byte).
ver := string(goVersionMatcher.Find(bstr))
if ver == "" {
continue
}
ver, _, _ = strings.Cut(ver, "-")
resolvedVer := ResolveGoVersion(ver)
if resolvedVer != nil {
return resolvedVer
}
// An unknown version.
return &GoVersion{Name: ver}
}
return nil
}
func matchGoVersionString(data []byte) string {
return string(goVersionMatcher.Find(data))
}