Skip to content

Commit 1a1506e

Browse files
committed
builder,loader: fix -ldflags -X not overriding variables with default values
When a string variable already had a source-level default value, the -ldflags "-X" flag was silently ignored and the variable remained the default value at runtime. Fix by stripping the InitOrder entry for each -X variable before LoadSSA() is called. With no entry in InitOrder, go/ssa emits no init store, so the global stays zero-valued in the IR. makeGlobalsModule still injects the actual -X values at final link time. The -X values remain out of the per-package build cache with only the variable names appearing in the cache key. Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 8793dc3 commit 1a1506e

6 files changed

Lines changed: 59 additions & 2 deletions

File tree

builder/build.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
255255
result.PackagePathMap[pkg.OriginalDir()] = pkg.Pkg.Path()
256256
}
257257

258+
// Strip default initializers for -X globals from the type info before
259+
// building SSA. This prevents go/ssa from emitting init stores for them,
260+
// so that makeGlobalsModule can supply the correct values at final link
261+
// time without any runtime init overwriting them. The -X values themselves
262+
// are kept out of the per-package build cache; only the variable names
263+
// appear in the cache key.
264+
for _, pkg := range lprogram.Sorted() {
265+
for name := range globalValues[pkg.Pkg.Path()] {
266+
pkg.StripVarInitializer(name)
267+
}
268+
}
269+
258270
// Create the *ssa.Program. This does not yet build the entire SSA of the
259271
// program so it's pretty fast and doesn't need to be parallelized.
260272
program := lprogram.LoadSSA()

loader/loader.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,29 @@ func (p *Program) Sorted() []*Package {
304304
return p.sorted
305305
}
306306

307+
// StripVarInitializer removes the package-level initializer for the named
308+
// variable from the type info. This prevents go/ssa from emitting an init
309+
// store for it, leaving the global zero-initialized in the IR. An external
310+
// value (e.g. from -ldflags -X via makeGlobalsModule) can then be linked in
311+
// at final link time without any runtime init overwriting it.
312+
//
313+
// Must be called after Parse() (typechecking populates InitOrder) and before
314+
// LoadSSA() (which consumes InitOrder to build the package init function).
315+
//
316+
// Only 1:1 var initializers (var x = expr) are matched. Multi-variable
317+
// initializers (var x, y = f()) are left untouched.
318+
func (p *Package) StripVarInitializer(name string) {
319+
n := 0
320+
for _, init := range p.info.InitOrder {
321+
if len(init.Lhs) == 1 && init.Lhs[0].Name() == name {
322+
continue // drop this initializer
323+
}
324+
p.info.InitOrder[n] = init
325+
n++
326+
}
327+
p.info.InitOrder = p.info.InitOrder[:n]
328+
}
329+
307330
// MainPkg returns the last package in the Sorted() slice. This is the main
308331
// package of the program.
309332
func (p *Program) MainPkg() *Package {

main_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ func TestBuild(t *testing.T) {
155155
}
156156
runTestWithConfig("ldflags.go", t, opts, nil, nil)
157157
})
158+
159+
// Same as ldflags, but the global has a default value in source.
160+
// -ldflags -X must override it, matching standard Go behaviour.
161+
t.Run("ldflags-initialized", func(t *testing.T) {
162+
t.Parallel()
163+
opts := optionsFromTarget("", sema)
164+
opts.GlobalValues = map[string]map[string]string{
165+
"main": {
166+
"someGlobal": "foobar",
167+
},
168+
}
169+
runTestWithConfig("ldflags-initialized.go", t, opts, nil, nil)
170+
})
158171
})
159172

160173
if testing.Short() {

testdata/ldflags-initialized.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
// This global has a default value. It should be overridable via
4+
// -ldflags="-X main.someGlobal=value" just like an uninitialized global.
5+
var someGlobal = "default"
6+
7+
func main() {
8+
println("someGlobal:", someGlobal)
9+
}

testdata/ldflags-initialized.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
someGlobal: foobar

testdata/ldflags.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

3-
// These globals can be changed using -ldflags="-X main.someGlobal=value".
4-
// At the moment, only globals without an initializer can be replaced this way.
3+
// This global can be changed using -ldflags="-X main.someGlobal=value".
54
var someGlobal string
65

76
func main() {

0 commit comments

Comments
 (0)