2020-09-10 11:07:48 -04:00
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2016-04-06 07:11:24 -04:00
package main
2017-11-21 11:27:20 -08:00
import (
2019-10-10 12:11:06 -04:00
"bufio"
"bytes"
2025-11-17 18:29:42 -08:00
"debug/elf"
2019-05-08 09:36:07 +02:00
"debug/macho"
2025-11-17 18:29:42 -08:00
"debug/pe"
2023-06-28 13:27:17 -04:00
"errors"
2025-11-17 18:29:42 -08:00
"internal/abi"
2022-10-04 09:00:31 -04:00
"internal/platform"
2017-11-21 11:27:20 -08:00
"internal/testenv"
2025-11-17 18:29:42 -08:00
"internal/xcoff"
2017-11-21 11:27:20 -08:00
"os"
2023-06-28 13:27:17 -04:00
"os/exec"
2017-11-21 11:27:20 -08:00
"path/filepath"
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
"regexp"
2019-03-21 13:42:53 +01:00
"runtime"
2025-03-26 18:47:15 +03:00
"strconv"
2018-10-31 13:18:17 -04:00
"strings"
2017-11-21 11:27:20 -08:00
"testing"
2025-11-17 18:29:42 -08:00
"unsafe"
2022-06-27 14:58:58 -07:00
2024-10-08 13:17:47 -04:00
imacho "cmd/internal/macho"
2025-03-25 16:55:54 -04:00
"cmd/internal/objfile"
2022-06-27 14:58:58 -07:00
"cmd/internal/sys"
2017-11-21 11:27:20 -08:00
)
2016-04-06 07:11:24 -04:00
2025-11-17 11:54:48 -08:00
// TestMain allows this test binary to run as a -toolexec wrapper for
// the 'go' command. If LINK_TEST_TOOLEXEC is set, TestMain runs the
// binary as if it were cmd/link, and otherwise runs the requested
// tool as a subprocess.
//
// This allows the test to verify the behavior of the current contents of the
// cmd/link package even if the installed cmd/link binary is stale.
func TestMain ( m * testing . M ) {
// Are we running as a toolexec wrapper? If so then run either
// the correct tool or this executable itself (for the linker).
// Running as toolexec wrapper.
if os . Getenv ( "LINK_TEST_TOOLEXEC" ) != "" {
if strings . TrimSuffix ( filepath . Base ( os . Args [ 1 ] ) , ".exe" ) == "link" {
// Running as a -toolexec linker, and the tool is cmd/link.
// Substitute this test binary for the linker.
os . Args = os . Args [ 1 : ]
main ( )
os . Exit ( 0 )
}
// Running some other tool.
cmd := exec . Command ( os . Args [ 1 ] , os . Args [ 2 : ] ... )
cmd . Stdin = os . Stdin
cmd . Stdout = os . Stdout
cmd . Stderr = os . Stderr
if err := cmd . Run ( ) ; err != nil {
os . Exit ( 1 )
}
os . Exit ( 0 )
}
// Are we being asked to run as the linker (without toolexec)?
// If so then kick off main.
if os . Getenv ( "LINK_TEST_EXEC_LINKER" ) != "" {
main ( )
os . Exit ( 0 )
}
if testExe , err := os . Executable ( ) ; err == nil {
// on wasm, some phones, we expect an error from os.Executable()
testLinker = testExe
}
// Not running as a -toolexec wrapper or as a linker executable.
// Just run the tests.
os . Exit ( m . Run ( ) )
}
// testLinker is the path of the test executable being run.
// This is used by [TestScript].
var testLinker string
// goCmd returns a [*exec.Cmd] that runs the go tool using
// the current linker sources rather than the installed linker.
// The first element of the args parameter should be the go subcommand
// to run, such as "build" or "run". It must be a subcommand that
// takes the go command's build flags.
func goCmd ( t * testing . T , args ... string ) * exec . Cmd {
goArgs := [ ] string { args [ 0 ] , "-toolexec" , testenv . Executable ( t ) }
args = append ( goArgs , args [ 1 : ] ... )
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , args ... )
cmd = testenv . CleanCmdEnv ( cmd )
cmd . Env = append ( cmd . Env , "LINK_TEST_TOOLEXEC=1" )
return cmd
}
// linkCmd returns a [*exec.Cmd] that runs the linker built from
// the current sources. This is like "go tool link", but runs the
// current linker rather than the installed one.
func linkCmd ( t * testing . T , args ... string ) * exec . Cmd {
// Set up the arguments that TestMain looks for.
args = append ( [ ] string { "link" } , args ... )
cmd := testenv . Command ( t , testenv . Executable ( t ) , args ... )
cmd = testenv . CleanCmdEnv ( cmd )
cmd . Env = append ( cmd . Env , "LINK_TEST_TOOLEXEC=1" )
return cmd
}
2016-04-06 07:11:24 -04:00
var AuthorPaidByTheColumnInch struct {
2019-05-08 18:55:33 -04:00
fog int ` text:"London. Michaelmas term lately over, and the Lord Chancellor sitting in Lincoln’ s Inn Hall. Implacable November weather. As much mud in the streets as if the waters had but newly retired from the face of the earth, and it would not be wonderful to meet a Megalosaurus, forty feet long or so, waddling like an elephantine lizard up Holborn Hill. Smoke lowering down from chimney-pots, making a soft black drizzle, with flakes of soot in it as big as full-grown snowflakes—gone into mourning, one might imagine, for the death of the sun. Dogs, undistinguishable in mire. Horses, scarcely better; splashed to their very blinkers. Foot passengers, jostling one another’ s umbrellas in a general infection of ill temper, and losing their foot-hold at street-corners, where tens of thousands of other foot passengers have been slipping and sliding since the day broke (if this day ever broke), adding new deposits to the crust upon crust of mud, sticking at those points tenaciously to the pavement, and accumulating at compound interest. Fog everywhere. Fog up the river, where it flows among green aits and meadows; fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city. Fog on the Essex marshes, fog on the Kentish heights. Fog creeping into the cabooses of collier-brigs; fog lying out on the yards and hovering in the rigging of great ships; fog drooping on the gunwales of barges and small boats. Fog in the eyes and throats of ancient Greenwich pensioners, wheezing by the firesides of their wards; fog in the stem and bowl of the afternoon pipe of the wrathful skipper, down in his close cabin; fog cruelly pinching the toes and fingers of his shivering little ‘ prentice boy on deck. Chance people on the bridges peeping over the parapets into a nether sky of fog, with fog all round them, as if they were up in a balloon and hanging in the misty clouds. Gas looming through the fog in divers places in the streets, much as the sun may, from the spongey fields, be seen to loom by husbandman and ploughboy. Most of the shops lighted two hours before their time—as the gas seems to know, for it has a haggard and unwilling look. The raw afternoon is rawest, and the dense fog is densest, and the muddy streets are muddiest near that leaden-headed old obstruction, appropriate ornament for the threshold of a leaden-headed old corporation, Temple Bar. And hard by Temple Bar, in Lincoln’ s Inn Hall, at the very heart of the fog, sits the Lord High Chancellor in his High Court of Chancery." `
2016-04-06 07:11:24 -04:00
2019-05-08 18:55:33 -04:00
wind int ` text:"It was grand to see how the wind awoke, and bent the trees, and drove the rain before it like a cloud of smoke; and to hear the solemn thunder, and to see the lightning; and while thinking with awe of the tremendous powers by which our little lives are encompassed, to consider how beneficent they are, and how upon the smallest flower and leaf there was already a freshness poured from all this seeming rage, which seemed to make creation new again." `
2016-04-06 07:11:24 -04:00
2019-05-08 18:55:33 -04:00
jarndyce int ` text:"Jarndyce and Jarndyce drones on. This scarecrow of a suit has, over the course of time, become so complicated, that no man alive knows what it means. The parties to it understand it least; but it has been observed that no two Chancery lawyers can talk about it for five minutes, without coming to a total disagreement as to all the premises. Innumerable children have been born into the cause; innumerable young people have married into it; innumerable old people have died out of it. Scores of persons have deliriously found themselves made parties in Jarndyce and Jarndyce, without knowing how or why; whole families have inherited legendary hatreds with the suit. The little plaintiff or defendant, who was promised a new rocking-horse when Jarndyce and Jarndyce should be settled, has grown up, possessed himself of a real horse, and trotted away into the other world. Fair wards of court have faded into mothers and grandmothers; a long procession of Chancellors has come in and gone out; the legion of bills in the suit have been transformed into mere bills of mortality; there are not three Jarndyces left upon the earth perhaps, since old Tom Jarndyce in despair blew his brains out at a coffee-house in Chancery Lane; but Jarndyce and Jarndyce still drags its dreary length before the Court, perennially hopeless." `
2016-04-06 07:11:24 -04:00
2019-05-08 18:55:33 -04:00
principle int ` text:"The one great principle of the English law is, to make business for itself. There is no other principle distinctly, certainly, and consistently maintained through all its narrow turnings. Viewed by this light it becomes a coherent scheme, and not the monstrous maze the laity are apt to think it. Let them but once clearly perceive that its grand principle is to make business for itself at their expense, and surely they will cease to grumble." `
2016-04-06 07:11:24 -04:00
}
func TestLargeSymName ( t * testing . T ) {
// The compiler generates a symbol name using the string form of the
// type. This tests that the linker can read symbol names larger than
// the bufio buffer. Issue #15104.
_ = AuthorPaidByTheColumnInch
}
2017-11-21 11:27:20 -08:00
func TestIssue21703 ( t * testing . T ) {
2018-12-07 15:00:49 -08:00
t . Parallel ( )
2017-11-21 11:27:20 -08:00
testenv . MustHaveGoBuild ( t )
2025-01-22 17:18:19 -05:00
// N.B. the build below explictly doesn't pass through
// -asan/-msan/-race, so we don't care about those.
testenv . MustInternalLink ( t , testenv . NoSpecialBuildTypes )
2017-11-21 11:27:20 -08:00
const source = `
package main
const X = "\n!\n"
func main ( ) { }
`
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2023-02-02 10:42:46 -05:00
main := filepath . Join ( tmpdir , "main.go" )
2017-11-21 11:27:20 -08:00
2023-02-02 10:42:46 -05:00
err := os . WriteFile ( main , [ ] byte ( source ) , 0666 )
2017-11-21 11:27:20 -08:00
if err != nil {
t . Fatalf ( "failed to write main.go: %v\n" , err )
}
2023-02-02 10:42:46 -05:00
importcfgfile := filepath . Join ( tmpdir , "importcfg" )
testenv . WriteImportcfg ( t , importcfgfile , nil , main )
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgfile , "-p=main" , "main.go" )
2017-11-21 11:27:20 -08:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "failed to compile main.go: %v, output: %s\n" , err , out )
}
2025-11-17 11:54:48 -08:00
cmd = linkCmd ( t , "-importcfg=" + importcfgfile , "main.o" )
2017-11-21 11:27:20 -08:00
cmd . Dir = tmpdir
out , err = cmd . CombinedOutput ( )
if err != nil {
2023-03-01 13:45:18 +00:00
if runtime . GOOS == "android" && runtime . GOARCH == "arm64" {
testenv . SkipFlaky ( t , 58806 )
}
2017-11-21 11:27:20 -08:00
t . Fatalf ( "failed to link main.o: %v, output: %s\n" , err , out )
}
}
2018-10-31 13:18:17 -04:00
// TestIssue28429 ensures that the linker does not attempt to link
// sections not named *.o. Such sections may be used by a build system
// to, for example, save facts produced by a modular static analysis
// such as golang.org/x/tools/go/analysis.
func TestIssue28429 ( t * testing . T ) {
2018-12-07 15:00:49 -08:00
t . Parallel ( )
2018-10-31 13:18:17 -04:00
testenv . MustHaveGoBuild ( t )
2025-01-22 17:18:19 -05:00
// N.B. go build below explictly doesn't pass through
// -asan/-msan/-race, so we don't care about those.
testenv . MustInternalLink ( t , testenv . NoSpecialBuildTypes )
2018-10-31 13:18:17 -04:00
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2018-10-31 13:18:17 -04:00
write := func ( name , content string ) {
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( filepath . Join ( tmpdir , name ) , [ ] byte ( content ) , 0666 )
2018-10-31 13:18:17 -04:00
if err != nil {
t . Fatal ( err )
}
}
runGo := func ( args ... string ) {
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , args ... )
2018-10-31 13:18:17 -04:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "'go %s' failed: %v, output: %s" ,
strings . Join ( args , " " ) , err , out )
}
}
// Compile a main package.
write ( "main.go" , "package main; func main() {}" )
2023-02-02 10:42:46 -05:00
importcfgfile := filepath . Join ( tmpdir , "importcfg" )
testenv . WriteImportcfg ( t , importcfgfile , nil , filepath . Join ( tmpdir , "main.go" ) )
2022-09-21 15:51:27 -04:00
runGo ( "tool" , "compile" , "-importcfg=" + importcfgfile , "-p=main" , "main.go" )
2018-10-31 13:18:17 -04:00
runGo ( "tool" , "pack" , "c" , "main.a" , "main.o" )
// Add an extra section with a short, non-.o name.
// This simulates an alternative build system.
write ( ".facts" , "this is not an object file" )
runGo ( "tool" , "pack" , "r" , "main.a" , ".facts" )
// Verify that the linker does not attempt
// to compile the extra section.
2025-11-17 11:54:48 -08:00
cmd := linkCmd ( t , "-importcfg=" + importcfgfile , "main.a" )
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
if runtime . GOOS == "android" && runtime . GOARCH == "arm64" {
testenv . SkipFlaky ( t , 58806 )
}
t . Fatalf ( "linker failed: %v, output %s" , err , out )
}
2018-10-31 13:18:17 -04:00
}
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
func TestUnresolved ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
write := func ( name , content string ) {
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( filepath . Join ( tmpdir , name ) , [ ] byte ( content ) , 0666 )
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
if err != nil {
t . Fatal ( err )
}
}
// Test various undefined references. Because of issue #29852,
// this used to give confusing error messages because the
// linker would find an undefined reference to "zero" created
// by the runtime package.
2019-02-15 18:12:28 -05:00
write ( "go.mod" , "module testunresolved\n" )
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
write ( "main.go" , ` package main
func main ( ) {
x ( )
}
func x ( )
` )
write ( "main.s" , `
TEXT · x ( SB ) , 0 , $ 0
MOVD zero < > ( SB ) , AX
MOVD zero ( SB ) , AX
MOVD · zero ( SB ) , AX
RET
` )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" )
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
cmd . Dir = tmpdir
2025-11-17 11:54:48 -08:00
cmd . Env = append ( cmd . Env ,
2019-03-12 14:43:22 -04:00
"GOARCH=amd64" , "GOOS=linux" , "GOPATH=" + filepath . Join ( tmpdir , "_gopath" ) )
cmd/link: fix confusing error on unresolved symbol
Currently, if an assembly file includes a static reference to an
undefined symbol, and another package also has an undefined reference
to that symbol, the linker can report an error like:
x: relocation target zero not defined for ABI0 (but is defined for ABI0)
Since the symbol is referenced in another package, the code in
ErrorUnresolved that looks for alternative ABI symbols finds that
symbol in the symbol table, but doesn't check that it's actually
defined, which is where the "but is defined for ABI0" comes from. The
"not defined for ABI0" is because ErrorUnresolved failed to turn the
static symbol's version back into an ABI, and it happened to print the
zero value for an ABI.
This CL fixes both of these problems. It explicitly maps the
relocation version back to an ABI and detects if it can't be mapped
back (e.g., because it's a static reference). Then, if it finds a
symbol with a different ABI in the symbol table, it checks to make
sure it's a definition, and not simply an unresolved reference.
Fixes #29852.
Change-Id: Ice45cc41c1907919ce5750f74588e8047eaa888c
Reviewed-on: https://go-review.googlesource.com/c/159518
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-27 21:03:32 -05:00
out , err := cmd . CombinedOutput ( )
if err == nil {
t . Fatalf ( "expected build to fail, but it succeeded" )
}
out = regexp . MustCompile ( "(?m)^#.*\n" ) . ReplaceAll ( out , nil )
got := string ( out )
want := ` main . x : relocation target zero not defined
main . x : relocation target zero not defined
main . x : relocation target main . zero not defined
`
if want != got {
t . Fatalf ( "want:\n%sgot:\n%s" , want , got )
}
}
2019-03-21 13:42:53 +01:00
2020-02-26 02:48:24 +11:00
func TestIssue33979 ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
testenv . MustHaveCGO ( t )
2025-01-22 17:18:19 -05:00
// N.B. go build below explictly doesn't pass through
// -asan/-msan/-race, so we don't care about those.
2025-05-21 10:58:32 -04:00
testenv . MustInternalLink ( t , testenv . SpecialBuildTypes { Cgo : true } )
2020-02-26 02:48:24 +11:00
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-02-26 02:48:24 +11:00
write := func ( name , content string ) {
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( filepath . Join ( tmpdir , name ) , [ ] byte ( content ) , 0666 )
2020-02-26 02:48:24 +11:00
if err != nil {
t . Fatal ( err )
}
}
run := func ( name string , args ... string ) string {
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , name , args ... )
2020-02-26 02:48:24 +11:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "'go %s' failed: %v, output: %s" , strings . Join ( args , " " ) , err , out )
}
return string ( out )
}
runGo := func ( args ... string ) string {
return run ( testenv . GoToolPath ( t ) , args ... )
}
// Test object with undefined reference that was not generated
// by Go, resulting in an SXREF symbol being loaded during linking.
// Because of issue #33979, the SXREF symbol would be found during
// error reporting, resulting in confusing error messages.
write ( "main.go" , ` package main
func main ( ) {
x ( )
}
func x ( )
` )
// The following assembly must work on all architectures.
write ( "x.s" , `
TEXT · x ( SB ) , 0 , $ 0
CALL foo ( SB )
RET
` )
write ( "x.c" , `
void undefined ( ) ;
void foo ( ) {
undefined ( ) ;
}
` )
cc := strings . TrimSpace ( runGo ( "env" , "CC" ) )
cflags := strings . Fields ( runGo ( "env" , "GOGCCFLAGS" ) )
2022-09-21 15:51:27 -04:00
importcfgfile := filepath . Join ( tmpdir , "importcfg" )
2023-02-02 10:42:46 -05:00
testenv . WriteImportcfg ( t , importcfgfile , nil , "runtime" )
2022-09-21 15:51:27 -04:00
2020-02-26 02:48:24 +11:00
// Compile, assemble and pack the Go and C code.
2022-05-05 17:45:27 -04:00
runGo ( "tool" , "asm" , "-p=main" , "-gensymabis" , "-o" , "symabis" , "x.s" )
2022-09-21 15:51:27 -04:00
runGo ( "tool" , "compile" , "-importcfg=" + importcfgfile , "-symabis" , "symabis" , "-p=main" , "-o" , "x1.o" , "main.go" )
2022-05-05 17:45:27 -04:00
runGo ( "tool" , "asm" , "-p=main" , "-o" , "x2.o" , "x.s" )
2020-02-26 02:48:24 +11:00
run ( cc , append ( cflags , "-c" , "-o" , "x3.o" , "x.c" ) ... )
runGo ( "tool" , "pack" , "c" , "x.a" , "x1.o" , "x2.o" , "x3.o" )
// Now attempt to link using the internal linker.
2025-11-17 11:54:48 -08:00
cmd := linkCmd ( t , "-importcfg=" + importcfgfile , "-linkmode=internal" , "x.a" )
2020-02-26 02:48:24 +11:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err == nil {
t . Fatalf ( "expected link to fail, but it succeeded" )
}
re := regexp . MustCompile ( ` (?m)^main\(.*text\): relocation target undefined not defined$ ` )
if ! re . Match ( out ) {
t . Fatalf ( "got:\n%q\nwant:\n%s" , out , re )
}
}
2019-05-15 20:49:39 -04:00
func TestBuildForTvOS ( t * testing . T ) {
2019-03-21 13:42:53 +01:00
testenv . MustHaveCGO ( t )
testenv . MustHaveGoBuild ( t )
2023-09-27 22:13:58 -04:00
// Only run this on darwin, where we can cross build for tvOS.
if runtime . GOOS != "darwin" {
t . Skip ( "skipping on non-darwin platform" )
2019-03-21 13:42:53 +01:00
}
2025-09-11 23:48:04 +00:00
if testing . Short ( ) && testenv . Builder ( ) == "" {
2019-05-15 20:49:39 -04:00
t . Skip ( "skipping in -short mode with $GO_BUILDER_NAME empty" )
}
2022-11-15 10:21:21 -05:00
if err := testenv . Command ( t , "xcrun" , "--help" ) . Run ( ) ; err != nil {
2019-03-21 13:42:53 +01:00
t . Skipf ( "error running xcrun, required for iOS cross build: %v" , err )
}
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2022-11-15 10:21:21 -05:00
sdkPath , err := testenv . Command ( t , "xcrun" , "--sdk" , "appletvos" , "--show-sdk-path" ) . Output ( )
2019-03-21 13:42:53 +01:00
if err != nil {
t . Skip ( "failed to locate appletvos SDK, skipping" )
}
CC := [ ] string {
"clang" ,
"-arch" ,
"arm64" ,
"-isysroot" , strings . TrimSpace ( string ( sdkPath ) ) ,
"-mtvos-version-min=12.0" ,
"-fembed-bitcode" ,
}
cmd: support space and quotes in CC and CXX
The CC and CXX environment variables now support spaces and quotes
(both double and single). This fixes two issues: first, if CC is a
single path that contains spaces (like 'c:\Program
Files\gcc\bin\gcc.exe'), that should now work if the space is quoted
or escaped (#41400). Second, if CC or CXX has multiple arguments (like
'gcc -O2'), they are now split correctly, and the arguments are passed
before other arguments when invoking the C compiler. Previously,
strings.Fields was used to split arguments, and the arguments were
placed later in the command line. (#43078).
Fixes golang/go#41400
Fixes golang/go#43078
NOTE: This change also includes a fix (CL 341929) for a test that was
broken by the original CL. Commit message for the fix is below.
[dev.cmdgo] cmd/link: fix TestBuildForTvOS
This test was broken in CL 334732 on darwin.
The test invokes 'go build' with a CC containing the arguments
-framework CoreFoundation. Previously, the go command split CC on
whitespace, and inserted the arguments after the command line when
running CC directly. Those arguments weren't passed to cgo though,
so cgo ran CC without -framework CoreFoundation (or any of the other
flags).
In CL 334732, we pass CC through to cgo, and cgo splits arguments
using str.SplitQuotedFields. So -framework CoreFoundation actually
gets passed to the C compiler. It appears that -framework flags are
only meant to be used in linking operations, so when cgo invokes clang
with -E (run preprocessor only), clang emits an error that -framework
is unused.
This change fixes the test by moving -framework CoreFoundation out of
CC and into CGO_LDFLAGS.
Change-Id: I2d5d89ddb19c94adef65982a8137b01f037d5c11
Reviewed-on: https://go-review.googlesource.com/c/go/+/334732
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/341936
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-07-14 16:57:24 -07:00
CGO_LDFLAGS := [ ] string { "-framework" , "CoreFoundation" }
2020-04-22 23:25:41 -04:00
lib := filepath . Join ( "testdata" , "testBuildFortvOS" , "lib.go" )
2021-03-07 20:52:39 -08:00
tmpDir := t . TempDir ( )
2019-03-21 13:42:53 +01:00
ar := filepath . Join ( tmpDir , "lib.a" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-buildmode=c-archive" , "-o" , ar , lib )
2023-09-27 22:13:58 -04:00
env := [ ] string {
2019-03-21 13:42:53 +01:00
"CGO_ENABLED=1" ,
2020-10-16 20:25:54 -04:00
"GOOS=ios" ,
2019-03-21 13:42:53 +01:00
"GOARCH=arm64" ,
2023-09-27 22:13:58 -04:00
"CC=" + strings . Join ( CC , " " ) ,
2020-01-22 15:30:52 -05:00
"CGO_CFLAGS=" , // ensure CGO_CFLAGS does not contain any flags. Issue #35459
2023-09-27 22:13:58 -04:00
"CGO_LDFLAGS=" + strings . Join ( CGO_LDFLAGS , " " ) ,
}
2025-11-17 11:54:48 -08:00
cmd . Env = append ( cmd . Env , env ... )
2023-09-27 22:13:58 -04:00
t . Logf ( "%q %v" , env , cmd )
2019-03-21 13:42:53 +01:00
if out , err := cmd . CombinedOutput ( ) ; err != nil {
t . Fatalf ( "%v: %v:\n%s" , cmd . Args , err , out )
}
2022-11-15 10:21:21 -05:00
link := testenv . Command ( t , CC [ 0 ] , CC [ 1 : ] ... )
cmd: support space and quotes in CC and CXX
The CC and CXX environment variables now support spaces and quotes
(both double and single). This fixes two issues: first, if CC is a
single path that contains spaces (like 'c:\Program
Files\gcc\bin\gcc.exe'), that should now work if the space is quoted
or escaped (#41400). Second, if CC or CXX has multiple arguments (like
'gcc -O2'), they are now split correctly, and the arguments are passed
before other arguments when invoking the C compiler. Previously,
strings.Fields was used to split arguments, and the arguments were
placed later in the command line. (#43078).
Fixes golang/go#41400
Fixes golang/go#43078
NOTE: This change also includes a fix (CL 341929) for a test that was
broken by the original CL. Commit message for the fix is below.
[dev.cmdgo] cmd/link: fix TestBuildForTvOS
This test was broken in CL 334732 on darwin.
The test invokes 'go build' with a CC containing the arguments
-framework CoreFoundation. Previously, the go command split CC on
whitespace, and inserted the arguments after the command line when
running CC directly. Those arguments weren't passed to cgo though,
so cgo ran CC without -framework CoreFoundation (or any of the other
flags).
In CL 334732, we pass CC through to cgo, and cgo splits arguments
using str.SplitQuotedFields. So -framework CoreFoundation actually
gets passed to the C compiler. It appears that -framework flags are
only meant to be used in linking operations, so when cgo invokes clang
with -E (run preprocessor only), clang emits an error that -framework
is unused.
This change fixes the test by moving -framework CoreFoundation out of
CC and into CGO_LDFLAGS.
Change-Id: I2d5d89ddb19c94adef65982a8137b01f037d5c11
Reviewed-on: https://go-review.googlesource.com/c/go/+/334732
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/341936
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-07-14 16:57:24 -07:00
link . Args = append ( link . Args , CGO_LDFLAGS ... )
2021-01-15 13:19:31 -05:00
link . Args = append ( link . Args , "-o" , filepath . Join ( tmpDir , "a.out" ) ) // Avoid writing to package directory.
2020-04-22 23:25:41 -04:00
link . Args = append ( link . Args , ar , filepath . Join ( "testdata" , "testBuildFortvOS" , "main.m" ) )
2023-09-27 22:13:58 -04:00
t . Log ( link )
2019-03-21 13:42:53 +01:00
if out , err := link . CombinedOutput ( ) ; err != nil {
t . Fatalf ( "%v: %v:\n%s" , link . Args , err , out )
}
}
2019-04-25 12:09:42 -04:00
var testXFlagSrc = `
package main
var X = "hello"
var Z = [ 99999 ] int { 99998 : 12345 } // make it large enough to be mmaped
func main ( ) { println ( X ) }
`
func TestXFlag ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2019-04-25 12:09:42 -04:00
src := filepath . Join ( tmpdir , "main.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testXFlagSrc ) , 0666 )
2019-04-25 12:09:42 -04:00
if err != nil {
t . Fatal ( err )
}
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-X=main.X=meow" , "-o" , filepath . Join ( tmpdir , "main" ) , src )
2019-04-25 12:09:42 -04:00
if out , err := cmd . CombinedOutput ( ) ; err != nil {
t . Errorf ( "%v: %v:\n%s" , cmd . Args , err , out )
}
}
2019-05-08 09:36:07 +02:00
2024-02-06 18:08:34 -05:00
var trivialSrc = `
2019-05-08 09:36:07 +02:00
package main
func main ( ) { }
`
2021-04-21 19:12:38 -04:00
func TestMachOBuildVersion ( t * testing . T ) {
2019-05-08 09:36:07 +02:00
testenv . MustHaveGoBuild ( t )
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2019-05-08 09:36:07 +02:00
src := filepath . Join ( tmpdir , "main.go" )
2024-02-06 18:08:34 -05:00
err := os . WriteFile ( src , [ ] byte ( trivialSrc ) , 0666 )
2019-05-08 09:36:07 +02:00
if err != nil {
t . Fatal ( err )
}
exe := filepath . Join ( tmpdir , "main" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-linkmode=internal" , "-o" , exe , src )
cmd . Env = append ( cmd . Env ,
2019-05-08 09:36:07 +02:00
"CGO_ENABLED=0" ,
"GOOS=darwin" ,
"GOARCH=amd64" ,
)
if out , err := cmd . CombinedOutput ( ) ; err != nil {
t . Fatalf ( "%v: %v:\n%s" , cmd . Args , err , out )
}
exef , err := os . Open ( exe )
if err != nil {
t . Fatal ( err )
}
2021-03-07 20:52:39 -08:00
defer exef . Close ( )
2019-05-08 09:36:07 +02:00
exem , err := macho . NewFile ( exef )
if err != nil {
t . Fatal ( err )
}
found := false
checkMin := func ( ver uint32 ) {
2024-02-14 11:52:22 -05:00
major , minor , patch := ( ver >> 16 ) & 0xff , ( ver >> 8 ) & 0xff , ( ver >> 0 ) & 0xff
cmd/link/internal/ld: use 12.0.0 OS/SDK versions for macOS linking
Go 1.25 will require macOS 12 Monterey or later, so macOS 11 will be
unsupported. The comment here suggests using a supported macOS version,
and that it can be the most recent one.
For now, make a minimal change of going from 11.0.0 to 12.0.0 so that
the chosen version is a supported one (although not the most recent).
However, it looks like even in CL 460476 (where the comment was added)
we were staying with the macOS version that matched Go's oldest, so we
might not have have recent experience with going beyond that. Update
the comment accordingly.
For #69839.
Change-Id: I90908971b0d5a8235ce77dc6bc9649e86008270a
Cq-Include-Trybots: luci.golang.try:gotip-darwin-amd64-longtest,gotip-darwin-arm64-longtest,gotip-darwin-amd64_12,gotip-darwin-amd64_14,gotip-darwin-arm64_12,gotip-darwin-arm64_15
Reviewed-on: https://go-review.googlesource.com/c/go/+/675095
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2025-05-21 13:13:48 -04:00
if major < 12 {
t . Errorf ( "LC_BUILD_VERSION version %d.%d.%d < 12.0.0" , major , minor , patch )
2019-05-08 09:36:07 +02:00
}
}
for _ , cmd := range exem . Loads {
raw := cmd . Raw ( )
type_ := exem . ByteOrder . Uint32 ( raw )
2024-10-08 13:17:47 -04:00
if type_ != imacho . LC_BUILD_VERSION {
2019-05-08 09:36:07 +02:00
continue
}
2021-04-21 19:12:38 -04:00
osVer := exem . ByteOrder . Uint32 ( raw [ 12 : ] )
2019-05-08 09:36:07 +02:00
checkMin ( osVer )
2021-04-21 19:12:38 -04:00
sdkVer := exem . ByteOrder . Uint32 ( raw [ 16 : ] )
2019-05-08 09:36:07 +02:00
checkMin ( sdkVer )
found = true
break
}
if ! found {
2021-04-21 19:12:38 -04:00
t . Errorf ( "no LC_BUILD_VERSION load command found" )
2019-05-08 09:36:07 +02:00
}
}
2019-10-10 12:11:06 -04:00
2024-10-08 13:17:47 -04:00
func TestMachOUUID ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
if runtime . GOOS != "darwin" {
t . Skip ( "this is only for darwin" )
}
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "main.go" )
err := os . WriteFile ( src , [ ] byte ( trivialSrc ) , 0666 )
if err != nil {
t . Fatal ( err )
}
extractUUID := func ( exe string ) string {
exem , err := macho . Open ( exe )
if err != nil {
t . Fatal ( err )
}
defer exem . Close ( )
for _ , cmd := range exem . Loads {
raw := cmd . Raw ( )
type_ := exem . ByteOrder . Uint32 ( raw )
if type_ != imacho . LC_UUID {
continue
}
return string ( raw [ 8 : 24 ] )
}
return ""
}
tests := [ ] struct { name , ldflags , expect string } {
{ "default" , "" , "gobuildid" } ,
{ "gobuildid" , "-B=gobuildid" , "gobuildid" } ,
{ "specific" , "-B=0x0123456789ABCDEF0123456789ABCDEF" , "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45\x67\x89\xAB\xCD\xEF" } ,
{ "none" , "-B=none" , "" } ,
}
if testenv . HasCGO ( ) {
for _ , test := range tests {
t1 := test
t1 . name += "_external"
t1 . ldflags += " -linkmode=external"
tests = append ( tests , t1 )
}
}
for _ , test := range tests {
t . Run ( test . name , func ( t * testing . T ) {
exe := filepath . Join ( tmpdir , test . name )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=" + test . ldflags , "-o" , exe , src )
2024-10-08 13:17:47 -04:00
if out , err := cmd . CombinedOutput ( ) ; err != nil {
t . Fatalf ( "%v: %v:\n%s" , cmd . Args , err , out )
}
uuid := extractUUID ( exe )
if test . expect == "gobuildid" {
// Go buildid is not known in source code. Check UUID is present,
2024-11-20 21:56:27 +08:00
// and satisfies UUIDv3.
2024-10-08 13:17:47 -04:00
if uuid == "" {
t . Fatal ( "expect nonempty UUID, got empty" )
}
// The version number is the high 4 bits of byte 6.
if uuid [ 6 ] >> 4 != 3 {
t . Errorf ( "expect v3 UUID, got %X (version %d)" , uuid , uuid [ 6 ] >> 4 )
}
} else if uuid != test . expect {
t . Errorf ( "UUID mismatch: got %X, want %X" , uuid , test . expect )
}
} )
}
}
2019-10-10 12:11:06 -04:00
const Issue34788src = `
package blah
func Blah ( i int ) int {
a := [ ... ] int { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }
return a [ i & 7 ]
}
`
func TestIssue34788Android386TLSSequence ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
// This is a cross-compilation test, so it doesn't make
// sense to run it on every GOOS/GOARCH combination. Limit
// the test to amd64 + darwin/linux.
if runtime . GOARCH != "amd64" ||
( runtime . GOOS != "darwin" && runtime . GOOS != "linux" ) {
t . Skip ( "skipping on non-{linux,darwin}/amd64 platform" )
}
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2019-10-10 12:11:06 -04:00
src := filepath . Join ( tmpdir , "blah.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( Issue34788src ) , 0666 )
2019-10-10 12:11:06 -04:00
if err != nil {
t . Fatal ( err )
}
obj := filepath . Join ( tmpdir , "blah.o" )
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-p=blah" , "-o" , obj , src )
2019-10-10 12:11:06 -04:00
cmd . Env = append ( os . Environ ( ) , "GOARCH=386" , "GOOS=android" )
if out , err := cmd . CombinedOutput ( ) ; err != nil {
2020-08-25 15:19:50 +02:00
t . Fatalf ( "failed to compile blah.go: %v, output: %s\n" , err , out )
2019-10-10 12:11:06 -04:00
}
// Run objdump on the resulting object.
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "objdump" , obj )
2019-10-10 12:11:06 -04:00
out , oerr := cmd . CombinedOutput ( )
if oerr != nil {
t . Fatalf ( "failed to objdump blah.o: %v, output: %s\n" , oerr , out )
}
// Sift through the output; we should not be seeing any R_TLS_LE relocs.
scanner := bufio . NewScanner ( bytes . NewReader ( out ) )
for scanner . Scan ( ) {
line := scanner . Text ( )
if strings . Contains ( line , "R_TLS_LE" ) {
t . Errorf ( "objdump output contains unexpected R_TLS_LE reloc: %s" , line )
}
}
}
2019-11-02 00:38:21 -04:00
const testStrictDupGoSrc = `
package main
func f ( )
func main ( ) { f ( ) }
`
const testStrictDupAsmSrc1 = `
# include "textflag.h"
TEXT · f ( SB ) , NOSPLIT | DUPOK , $ 0 - 0
RET
`
const testStrictDupAsmSrc2 = `
# include "textflag.h"
TEXT · f ( SB ) , NOSPLIT | DUPOK , $ 0 - 0
JMP 0 ( PC )
`
2021-06-08 19:45:41 -04:00
const testStrictDupAsmSrc3 = `
# include "textflag.h"
GLOBL · rcon ( SB ) , RODATA | DUPOK , $ 64
`
const testStrictDupAsmSrc4 = `
# include "textflag.h"
GLOBL · rcon ( SB ) , RODATA | DUPOK , $ 32
`
2019-11-02 00:38:21 -04:00
func TestStrictDup ( t * testing . T ) {
// Check that -strictdups flag works.
testenv . MustHaveGoBuild ( t )
2021-06-08 19:45:41 -04:00
asmfiles := [ ] struct {
fname string
payload string
} {
{ "a" , testStrictDupAsmSrc1 } ,
{ "b" , testStrictDupAsmSrc2 } ,
{ "c" , testStrictDupAsmSrc3 } ,
{ "d" , testStrictDupAsmSrc4 } ,
}
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2019-11-02 00:38:21 -04:00
src := filepath . Join ( tmpdir , "x.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testStrictDupGoSrc ) , 0666 )
2019-11-02 00:38:21 -04:00
if err != nil {
t . Fatal ( err )
}
2021-06-08 19:45:41 -04:00
for _ , af := range asmfiles {
src = filepath . Join ( tmpdir , af . fname + ".s" )
2022-08-28 03:38:00 +08:00
err = os . WriteFile ( src , [ ] byte ( af . payload ) , 0666 )
2021-06-08 19:45:41 -04:00
if err != nil {
t . Fatal ( err )
}
2019-11-02 00:38:21 -04:00
}
2019-11-23 00:31:39 -05:00
src = filepath . Join ( tmpdir , "go.mod" )
2022-08-28 03:38:00 +08:00
err = os . WriteFile ( src , [ ] byte ( "module teststrictdup\n" ) , 0666 )
2019-11-23 00:31:39 -05:00
if err != nil {
t . Fatal ( err )
}
2019-11-02 00:38:21 -04:00
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-strictdups=1" )
2019-11-02 00:38:21 -04:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
2021-06-08 19:45:41 -04:00
t . Errorf ( "linking with -strictdups=1 failed: %v\n%s" , err , string ( out ) )
2019-11-02 00:38:21 -04:00
}
if ! bytes . Contains ( out , [ ] byte ( "mismatched payload" ) ) {
t . Errorf ( "unexpected output:\n%s" , out )
}
2025-11-17 11:54:48 -08:00
cmd = goCmd ( t , "build" , "-ldflags=-strictdups=2" )
2019-11-02 00:38:21 -04:00
cmd . Dir = tmpdir
out , err = cmd . CombinedOutput ( )
if err == nil {
t . Errorf ( "linking with -strictdups=2 did not fail" )
}
2021-06-08 19:45:41 -04:00
// NB: on amd64 we get the 'new length' error, on arm64 the 'different
// contents' error.
if ! ( bytes . Contains ( out , [ ] byte ( "mismatched payload: new length" ) ) ||
bytes . Contains ( out , [ ] byte ( "mismatched payload: same length but different contents" ) ) ) ||
! bytes . Contains ( out , [ ] byte ( "mismatched payload: different sizes" ) ) {
2019-11-02 00:38:21 -04:00
t . Errorf ( "unexpected output:\n%s" , out )
}
}
2020-03-20 17:02:47 -04:00
2020-04-02 12:48:13 -04:00
const testFuncAlignSrc = `
package main
import (
"fmt"
)
func alignPc ( )
2021-06-01 19:16:33 -04:00
var alignPcFnAddr uintptr
2020-04-02 12:48:13 -04:00
func main ( ) {
2021-06-01 19:16:33 -04:00
if alignPcFnAddr % 512 != 0 {
fmt . Printf ( "expected 512 bytes alignment, got %v\n" , alignPcFnAddr )
2020-04-02 12:48:13 -04:00
} else {
fmt . Printf ( "PASS" )
}
}
`
2023-03-28 19:30:04 +08:00
var testFuncAlignAsmSources = map [ string ] string {
"arm64" : `
2020-04-02 12:48:13 -04:00
# include "textflag.h"
TEXT · alignPc ( SB ) , NOSPLIT , $ 0 - 0
MOVD $ 2 , R0
PCALIGN $ 512
MOVD $ 3 , R1
RET
2021-06-01 19:16:33 -04:00
GLOBL · alignPcFnAddr ( SB ) , RODATA , $ 8
DATA · alignPcFnAddr ( SB ) / 8 , $ · alignPc ( SB )
2023-03-28 19:30:04 +08:00
` ,
"loong64" : `
# include "textflag.h"
TEXT · alignPc ( SB ) , NOSPLIT , $ 0 - 0
MOVV $ 2 , R4
PCALIGN $ 512
MOVV $ 3 , R5
RET
GLOBL · alignPcFnAddr ( SB ) , RODATA , $ 8
DATA · alignPcFnAddr ( SB ) / 8 , $ · alignPc ( SB )
` ,
}
2020-04-02 12:48:13 -04:00
// TestFuncAlign verifies that the address of a function can be aligned
2023-03-28 19:30:04 +08:00
// with a specific value on arm64 and loong64.
2020-04-02 12:48:13 -04:00
func TestFuncAlign ( t * testing . T ) {
2023-03-28 19:30:04 +08:00
testFuncAlignAsmSrc := testFuncAlignAsmSources [ runtime . GOARCH ]
if len ( testFuncAlignAsmSrc ) == 0 || runtime . GOOS != "linux" {
t . Skip ( "skipping on non-linux/{arm64,loong64} platform" )
2020-04-02 12:48:13 -04:00
}
testenv . MustHaveGoBuild ( t )
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-04-02 12:48:13 -04:00
2020-09-21 12:10:16 -04:00
src := filepath . Join ( tmpdir , "go.mod" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( "module cmd/link/TestFuncAlign/falign" ) , 0666 )
2020-09-21 12:10:16 -04:00
if err != nil {
t . Fatal ( err )
}
src = filepath . Join ( tmpdir , "falign.go" )
2022-08-28 03:38:00 +08:00
err = os . WriteFile ( src , [ ] byte ( testFuncAlignSrc ) , 0666 )
2020-04-02 12:48:13 -04:00
if err != nil {
t . Fatal ( err )
}
src = filepath . Join ( tmpdir , "falign.s" )
2022-08-28 03:38:00 +08:00
err = os . WriteFile ( src , [ ] byte ( testFuncAlignAsmSrc ) , 0666 )
2020-04-02 12:48:13 -04:00
if err != nil {
t . Fatal ( err )
}
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , "falign" )
2020-04-02 12:48:13 -04:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "build failed: %v" , err )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , tmpdir + "/falign" )
2020-04-02 12:48:13 -04:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "failed to run with err %v, output: %s" , err , out )
}
if string ( out ) != "PASS" {
t . Errorf ( "unexpected output: %s\n" , out )
}
}
2020-04-10 14:58:54 -04:00
2025-03-26 18:47:15 +03:00
const testFuncAlignOptionSrc = `
package main
//go:noinline
func foo ( ) {
}
//go:noinline
func bar ( ) {
}
//go:noinline
func baz ( ) {
}
func main ( ) {
foo ( )
bar ( )
baz ( )
}
`
// TestFuncAlignOption verifies that the -funcalign option changes the function alignment
func TestFuncAlignOption ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "falign.go" )
err := os . WriteFile ( src , [ ] byte ( testFuncAlignOptionSrc ) , 0666 )
if err != nil {
t . Fatal ( err )
}
alignTest := func ( align uint64 ) {
exeName := "falign.exe"
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-funcalign=" + strconv . FormatUint ( align , 10 ) , "-o" , exeName , "falign.go" )
2025-03-26 18:47:15 +03:00
cmd . Dir = tmpdir
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "build failed: %v \n%s" , err , out )
}
exe := filepath . Join ( tmpdir , exeName )
cmd = testenv . Command ( t , exe )
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "failed to run with err %v, output: %s" , err , out )
}
// Check function alignment
f , err := objfile . Open ( exe )
if err != nil {
t . Fatalf ( "failed to open file:%v\n" , err )
}
defer f . Close ( )
fname := map [ string ] bool { "_main.foo" : false ,
"_main.bar" : false ,
"_main.baz" : false }
syms , err := f . Symbols ( )
for _ , s := range syms {
fn := s . Name
if _ , ok := fname [ fn ] ; ! ok {
fn = "_" + s . Name
if _ , ok := fname [ fn ] ; ! ok {
continue
}
}
if s . Addr % align != 0 {
t . Fatalf ( "unaligned function: %s %x. Expected alignment: %d\n" , fn , s . Addr , align )
}
fname [ fn ] = true
}
for k , v := range fname {
if ! v {
t . Fatalf ( "function %s not found\n" , k )
}
}
}
alignTest ( 16 )
alignTest ( 32 )
}
2020-05-14 19:22:59 -04:00
const testTrampSrc = `
2020-04-10 14:58:54 -04:00
package main
import "fmt"
2020-05-14 19:22:59 -04:00
func main ( ) {
fmt . Println ( "hello" )
defer func ( ) {
if e := recover ( ) ; e == nil {
panic ( "did not panic" )
}
} ( )
f1 ( )
}
// Test deferreturn trampolines. See issue #39049.
func f1 ( ) { defer f2 ( ) }
func f2 ( ) { panic ( "XXX" ) }
2020-04-10 14:58:54 -04:00
`
func TestTrampoline ( t * testing . T ) {
// Test that trampoline insertion works as expected.
// For stress test, we set -debugtramp=2 flag, which sets a very low
// threshold for trampoline generation, and essentially all cross-package
// calls will use trampolines.
2021-05-03 11:00:54 -05:00
buildmodes := [ ] string { "default" }
2020-04-10 14:58:54 -04:00
switch runtime . GOARCH {
2023-11-01 17:25:20 +08:00
case "arm" , "arm64" , "ppc64" , "loong64" :
2021-05-03 11:00:54 -05:00
case "ppc64le" :
// Trampolines are generated differently when internal linking PIE, test them too.
buildmodes = append ( buildmodes , "pie" )
2020-04-10 14:58:54 -04:00
default :
t . Skipf ( "trampoline insertion is not implemented on %s" , runtime . GOARCH )
}
testenv . MustHaveGoBuild ( t )
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-04-10 14:58:54 -04:00
src := filepath . Join ( tmpdir , "hello.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testTrampSrc ) , 0666 )
2020-04-10 14:58:54 -04:00
if err != nil {
t . Fatal ( err )
}
exe := filepath . Join ( tmpdir , "hello.exe" )
2021-05-03 11:00:54 -05:00
for _ , mode := range buildmodes {
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-buildmode=" + mode , "-ldflags=-debugtramp=2" , "-o" , exe , src )
2021-05-03 11:00:54 -05:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build (%s) failed: %v\n%s" , mode , err , out )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , exe )
2021-05-03 11:00:54 -05:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "executable failed to run (%s): %v\n%s" , mode , err , out )
}
if string ( out ) != "hello\n" {
t . Errorf ( "unexpected output (%s):\n%s" , mode , out )
}
2024-03-21 11:20:58 +08:00
out , err = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "nm" , exe ) . CombinedOutput ( )
if err != nil {
t . Errorf ( "nm failure: %s\n%s\n" , err , string ( out ) )
}
2024-10-21 09:40:44 -05:00
if ok , _ := regexp . Match ( "T runtime.deferreturn(\\+0)?-tramp0" , out ) ; ! ok {
t . Errorf ( "Trampoline T runtime.deferreturn(+0)?-tramp0 is missing" )
2024-03-21 11:20:58 +08:00
}
2021-04-27 14:57:11 -04:00
}
}
const testTrampCgoSrc = `
package main
// #include <stdio.h>
// void CHello() { printf("hello\n"); fflush(stdout); }
import "C"
func main ( ) {
C . CHello ( )
}
`
func TestTrampolineCgo ( t * testing . T ) {
// Test that trampoline insertion works for cgo code.
// For stress test, we set -debugtramp=2 flag, which sets a very low
// threshold for trampoline generation, and essentially all cross-package
// calls will use trampolines.
2021-05-03 11:00:54 -05:00
buildmodes := [ ] string { "default" }
2021-04-27 14:57:11 -04:00
switch runtime . GOARCH {
2023-11-01 17:25:20 +08:00
case "arm" , "arm64" , "ppc64" , "loong64" :
2021-05-03 11:00:54 -05:00
case "ppc64le" :
// Trampolines are generated differently when internal linking PIE, test them too.
buildmodes = append ( buildmodes , "pie" )
2021-04-27 14:57:11 -04:00
default :
t . Skipf ( "trampoline insertion is not implemented on %s" , runtime . GOARCH )
}
testenv . MustHaveGoBuild ( t )
testenv . MustHaveCGO ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "hello.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testTrampCgoSrc ) , 0666 )
2021-04-27 14:57:11 -04:00
if err != nil {
t . Fatal ( err )
}
exe := filepath . Join ( tmpdir , "hello.exe" )
2021-05-03 11:00:54 -05:00
for _ , mode := range buildmodes {
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-buildmode=" + mode , "-ldflags=-debugtramp=2" , "-o" , exe , src )
2021-05-03 11:00:54 -05:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build (%s) failed: %v\n%s" , mode , err , out )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , exe )
2021-05-03 11:00:54 -05:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "executable failed to run (%s): %v\n%s" , mode , err , out )
}
if string ( out ) != "hello\n" && string ( out ) != "hello\r\n" {
t . Errorf ( "unexpected output (%s):\n%s" , mode , out )
}
2021-04-27 14:57:11 -04:00
2021-05-03 11:00:54 -05:00
// Test internal linking mode.
2021-04-27 14:57:11 -04:00
2023-03-01 16:11:07 +00:00
if ! testenv . CanInternalLink ( true ) {
continue
2021-05-03 11:00:54 -05:00
}
2025-11-17 11:54:48 -08:00
cmd = goCmd ( t , "build" , "-buildmode=" + mode , "-ldflags=-debugtramp=2 -linkmode=internal" , "-o" , exe , src )
2021-05-03 11:00:54 -05:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build (%s) failed: %v\n%s" , mode , err , out )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , exe )
2021-05-03 11:00:54 -05:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "executable failed to run (%s): %v\n%s" , mode , err , out )
}
if string ( out ) != "hello\n" && string ( out ) != "hello\r\n" {
t . Errorf ( "unexpected output (%s):\n%s" , mode , out )
}
2020-04-10 14:58:54 -04:00
}
}
2020-04-22 22:20:44 -04:00
func TestIndexMismatch ( t * testing . T ) {
// Test that index mismatch will cause a link-time error (not run-time error).
// This shouldn't happen with "go build". We invoke the compiler and the linker
// manually, and try to "trick" the linker with an inconsistent object file.
testenv . MustHaveGoBuild ( t )
2025-01-22 17:18:19 -05:00
// N.B. the build below explictly doesn't pass through
// -asan/-msan/-race, so we don't care about those.
testenv . MustInternalLink ( t , testenv . NoSpecialBuildTypes )
2020-04-22 22:20:44 -04:00
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-04-22 22:20:44 -04:00
aSrc := filepath . Join ( "testdata" , "testIndexMismatch" , "a.go" )
bSrc := filepath . Join ( "testdata" , "testIndexMismatch" , "b.go" )
mSrc := filepath . Join ( "testdata" , "testIndexMismatch" , "main.go" )
aObj := filepath . Join ( tmpdir , "a.o" )
mObj := filepath . Join ( tmpdir , "main.o" )
exe := filepath . Join ( tmpdir , "main.exe" )
2023-02-02 10:42:46 -05:00
importcfgFile := filepath . Join ( tmpdir , "runtime.importcfg" )
testenv . WriteImportcfg ( t , importcfgFile , nil , "runtime" )
2022-09-21 15:51:27 -04:00
importcfgWithAFile := filepath . Join ( tmpdir , "witha.importcfg" )
2023-02-02 10:42:46 -05:00
testenv . WriteImportcfg ( t , importcfgWithAFile , map [ string ] string { "a" : aObj } , "runtime" )
2022-09-21 15:51:27 -04:00
2020-04-22 22:20:44 -04:00
// Build a program with main package importing package a.
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgFile , "-p=a" , "-o" , aObj , aSrc )
2020-04-22 22:20:44 -04:00
t . Log ( cmd )
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compiling a.go failed: %v\n%s" , err , out )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgWithAFile , "-p=main" , "-I" , tmpdir , "-o" , mObj , mSrc )
2020-04-22 22:20:44 -04:00
t . Log ( cmd )
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compiling main.go failed: %v\n%s" , err , out )
}
2025-11-17 11:54:48 -08:00
cmd = linkCmd ( t , "-importcfg=" + importcfgWithAFile , "-L" , tmpdir , "-o" , exe , mObj )
2020-04-22 22:20:44 -04:00
t . Log ( cmd )
out , err = cmd . CombinedOutput ( )
if err != nil {
2023-03-01 13:45:18 +00:00
if runtime . GOOS == "android" && runtime . GOARCH == "arm64" {
testenv . SkipFlaky ( t , 58806 )
}
2020-04-22 22:20:44 -04:00
t . Errorf ( "linking failed: %v\n%s" , err , out )
}
// Now, overwrite a.o with the object of b.go. This should
// result in an index mismatch.
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgFile , "-p=a" , "-o" , aObj , bSrc )
2020-04-22 22:20:44 -04:00
t . Log ( cmd )
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compiling a.go failed: %v\n%s" , err , out )
}
2025-11-17 11:54:48 -08:00
cmd = linkCmd ( t , "-importcfg=" + importcfgWithAFile , "-L" , tmpdir , "-o" , exe , mObj )
2020-04-22 22:20:44 -04:00
t . Log ( cmd )
out , err = cmd . CombinedOutput ( )
if err == nil {
t . Fatalf ( "linking didn't fail" )
}
if ! bytes . Contains ( out , [ ] byte ( "fingerprint mismatch" ) ) {
t . Errorf ( "did not see expected error message. out:\n%s" , out )
}
}
2020-06-18 18:05:10 -04:00
2021-02-04 00:11:12 +01:00
func TestPErsrcBinutils ( t * testing . T ) {
2020-06-18 18:05:10 -04:00
// Test that PE rsrc section is handled correctly (issue 39658).
testenv . MustHaveGoBuild ( t )
2021-02-04 00:11:12 +01:00
if ( runtime . GOARCH != "386" && runtime . GOARCH != "amd64" ) || runtime . GOOS != "windows" {
// This test is limited to amd64 and 386, because binutils is limited as such
t . Skipf ( "this is only for windows/amd64 and windows/386" )
2020-06-18 18:05:10 -04:00
}
2020-07-06 17:49:24 -04:00
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-06-18 18:05:10 -04:00
2021-02-04 00:11:12 +01:00
pkgdir := filepath . Join ( "testdata" , "pe-binutils" )
2020-06-18 18:05:10 -04:00
exe := filepath . Join ( tmpdir , "a.exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , exe )
2020-06-18 18:05:10 -04:00
cmd . Dir = pkgdir
// cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "building failed: %v, output:\n%s" , err , out )
}
// Check that the binary contains the rsrc data
2022-08-28 03:38:00 +08:00
b , err := os . ReadFile ( exe )
2020-06-18 18:05:10 -04:00
if err != nil {
t . Fatalf ( "reading output failed: %v" , err )
}
if ! bytes . Contains ( b , [ ] byte ( "Hello Gophers!" ) ) {
t . Fatalf ( "binary does not contain expected content" )
}
2021-02-04 00:11:12 +01:00
}
func TestPErsrcLLVM ( t * testing . T ) {
// Test that PE rsrc section is handled correctly (issue 39658).
testenv . MustHaveGoBuild ( t )
if runtime . GOOS != "windows" {
t . Skipf ( "this is a windows-only test" )
}
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
cmd/link: handle grouped resource sections
The Go PE linker does not support enough generalized PE logic to
properly handle .rsrc sections gracefully. Instead a few things are
special cased for these. The linker also does not support PE's "grouped
sections" features, in which input objects have several named sections
that are sorted, merged, and renamed in the output file. In the past,
more sophisticated support for resources or for PE features like grouped
sections have not been necessary, as Go's own object formats are pretty
vanilla, and GNU binutils also produces pretty vanilla objects where all
sections are already merged.
However, GNU binutils is lagging with arm support, and here LLVM has
picked up the slack. In particular, LLVM has its own rc/cvtres combo,
which are glued together in mingw LLVM distributions as windres, a
command line compatible tool with binutils' windres, which supports arm
and arm64. But there's a key difference between binutils' windres and
LLVM's windres: the LLVM one uses proper grouped sections.
So, this commit adds grouped sections support for resource sections to
the linker. We don't attempt to plumb generic support for grouped
sections, just as there isn't generic support already for what resources
require. Instead we augment the resource handling logic to deal with
standard two-section resource objects.
We also add a test for this, akin to the current test for more vanilla
binutils resource objects, and make sure that the rsrc tests are always
performed.
Fixes #42866.
Fixes #43182.
Change-Id: I059450021405cdf2ef1c195ddbab3960764ad711
Reviewed-on: https://go-review.googlesource.com/c/go/+/268337
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-08 11:57:42 +01:00
2021-02-04 00:11:12 +01:00
pkgdir := filepath . Join ( "testdata" , "pe-llvm" )
exe := filepath . Join ( tmpdir , "a.exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , exe )
cmd/link: handle grouped resource sections
The Go PE linker does not support enough generalized PE logic to
properly handle .rsrc sections gracefully. Instead a few things are
special cased for these. The linker also does not support PE's "grouped
sections" features, in which input objects have several named sections
that are sorted, merged, and renamed in the output file. In the past,
more sophisticated support for resources or for PE features like grouped
sections have not been necessary, as Go's own object formats are pretty
vanilla, and GNU binutils also produces pretty vanilla objects where all
sections are already merged.
However, GNU binutils is lagging with arm support, and here LLVM has
picked up the slack. In particular, LLVM has its own rc/cvtres combo,
which are glued together in mingw LLVM distributions as windres, a
command line compatible tool with binutils' windres, which supports arm
and arm64. But there's a key difference between binutils' windres and
LLVM's windres: the LLVM one uses proper grouped sections.
So, this commit adds grouped sections support for resource sections to
the linker. We don't attempt to plumb generic support for grouped
sections, just as there isn't generic support already for what resources
require. Instead we augment the resource handling logic to deal with
standard two-section resource objects.
We also add a test for this, akin to the current test for more vanilla
binutils resource objects, and make sure that the rsrc tests are always
performed.
Fixes #42866.
Fixes #43182.
Change-Id: I059450021405cdf2ef1c195ddbab3960764ad711
Reviewed-on: https://go-review.googlesource.com/c/go/+/268337
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-08 11:57:42 +01:00
cmd . Dir = pkgdir
// cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment
2021-02-04 00:11:12 +01:00
out , err := cmd . CombinedOutput ( )
cmd/link: handle grouped resource sections
The Go PE linker does not support enough generalized PE logic to
properly handle .rsrc sections gracefully. Instead a few things are
special cased for these. The linker also does not support PE's "grouped
sections" features, in which input objects have several named sections
that are sorted, merged, and renamed in the output file. In the past,
more sophisticated support for resources or for PE features like grouped
sections have not been necessary, as Go's own object formats are pretty
vanilla, and GNU binutils also produces pretty vanilla objects where all
sections are already merged.
However, GNU binutils is lagging with arm support, and here LLVM has
picked up the slack. In particular, LLVM has its own rc/cvtres combo,
which are glued together in mingw LLVM distributions as windres, a
command line compatible tool with binutils' windres, which supports arm
and arm64. But there's a key difference between binutils' windres and
LLVM's windres: the LLVM one uses proper grouped sections.
So, this commit adds grouped sections support for resource sections to
the linker. We don't attempt to plumb generic support for grouped
sections, just as there isn't generic support already for what resources
require. Instead we augment the resource handling logic to deal with
standard two-section resource objects.
We also add a test for this, akin to the current test for more vanilla
binutils resource objects, and make sure that the rsrc tests are always
performed.
Fixes #42866.
Fixes #43182.
Change-Id: I059450021405cdf2ef1c195ddbab3960764ad711
Reviewed-on: https://go-review.googlesource.com/c/go/+/268337
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-08 11:57:42 +01:00
if err != nil {
t . Fatalf ( "building failed: %v, output:\n%s" , err , out )
}
// Check that the binary contains the rsrc data
2022-08-28 03:38:00 +08:00
b , err := os . ReadFile ( exe )
cmd/link: handle grouped resource sections
The Go PE linker does not support enough generalized PE logic to
properly handle .rsrc sections gracefully. Instead a few things are
special cased for these. The linker also does not support PE's "grouped
sections" features, in which input objects have several named sections
that are sorted, merged, and renamed in the output file. In the past,
more sophisticated support for resources or for PE features like grouped
sections have not been necessary, as Go's own object formats are pretty
vanilla, and GNU binutils also produces pretty vanilla objects where all
sections are already merged.
However, GNU binutils is lagging with arm support, and here LLVM has
picked up the slack. In particular, LLVM has its own rc/cvtres combo,
which are glued together in mingw LLVM distributions as windres, a
command line compatible tool with binutils' windres, which supports arm
and arm64. But there's a key difference between binutils' windres and
LLVM's windres: the LLVM one uses proper grouped sections.
So, this commit adds grouped sections support for resource sections to
the linker. We don't attempt to plumb generic support for grouped
sections, just as there isn't generic support already for what resources
require. Instead we augment the resource handling logic to deal with
standard two-section resource objects.
We also add a test for this, akin to the current test for more vanilla
binutils resource objects, and make sure that the rsrc tests are always
performed.
Fixes #42866.
Fixes #43182.
Change-Id: I059450021405cdf2ef1c195ddbab3960764ad711
Reviewed-on: https://go-review.googlesource.com/c/go/+/268337
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-08 11:57:42 +01:00
if err != nil {
t . Fatalf ( "reading output failed: %v" , err )
}
if ! bytes . Contains ( b , [ ] byte ( "resname RCDATA a.rc" ) ) {
t . Fatalf ( "binary does not contain expected content" )
}
2020-06-18 18:05:10 -04:00
}
2020-07-20 18:07:00 -04:00
func TestContentAddressableSymbols ( t * testing . T ) {
// Test that the linker handles content-addressable symbols correctly.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
src := filepath . Join ( "testdata" , "testHashedSyms" , "p.go" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "run" , src )
2020-07-20 18:07:00 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "command %s failed: %v\n%s" , cmd , err , out )
}
}
2020-09-10 11:14:27 -04:00
func TestReadOnly ( t * testing . T ) {
// Test that read-only data is indeed read-only.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
src := filepath . Join ( "testdata" , "testRO" , "x.go" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "run" , src )
2020-09-10 11:14:27 -04:00
out , err := cmd . CombinedOutput ( )
if err == nil {
t . Errorf ( "running test program did not fail. output:\n%s" , out )
}
}
2020-10-22 10:05:44 -04:00
const testIssue38554Src = `
package main
type T [ 10 << 20 ] byte
//go:noinline
func f ( ) T {
return T { } // compiler will make a large stmp symbol, but not used.
}
func main ( ) {
x := f ( )
println ( x [ 1 ] )
}
`
func TestIssue38554 ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-10-22 10:05:44 -04:00
src := filepath . Join ( tmpdir , "x.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testIssue38554Src ) , 0666 )
2020-10-22 10:05:44 -04:00
if err != nil {
t . Fatalf ( "failed to write source file: %v" , err )
}
exe := filepath . Join ( tmpdir , "x.exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , exe , src )
2020-10-22 10:05:44 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build failed: %v\n%s" , err , out )
}
fi , err := os . Stat ( exe )
if err != nil {
t . Fatalf ( "failed to stat output file: %v" , err )
}
// The test program is not much different from a helloworld, which is
// typically a little over 1 MB. We allow 5 MB. If the bad stmp is live,
// it will be over 10 MB.
const want = 5 << 20
if got := fi . Size ( ) ; got > want {
t . Errorf ( "binary too big: got %d, want < %d" , got , want )
}
}
cmd/link: report error if builtin referenced but not defined
When the compiler refers to a runtime builtin, it emits an indexed
symbol reference in the object file via predetermined/preassigned ID
within the PkgIdxBuiltin pseudo-package. At link time when the loader
encounters these references, it redirects them to the corresponding
defined symbol in the runtime package. This redirection process
currently assumes that if a runtime builtin is referenced, we'll
always have a definition for it. This assumption holds in most cases,
however for the builtins "runtime.racefuncenter" and
"runtime.racefuncexit", we'll only see definitions if the runtime
package we're linking against was built with "-race".
In the bug in question, build passes "-gcflags=-race" during
compilation of the main package, but doesn't pass "-race" directly to
'go build', and as a result the final link combines a
race-instrumented main with a non-race runtime; this results in R_CALL
relocations with zero-valued target symbols, resulting in a panic
during stack checking.
This patch changes the loader's resolve method to detect situations
where we're asking for builtin "runtime.X", but the runtime package
read in doesn't contain a definition for X.
Fixes #42396.
Change-Id: Iafd38bd3b0f7f462868d120ccd4d7d1b88b27436
Reviewed-on: https://go-review.googlesource.com/c/go/+/267881
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-11-05 14:19:47 -05:00
const testIssue42396src = `
package main
//go:noinline
//go:nosplit
func callee ( x int ) {
}
func main ( ) {
callee ( 9 )
}
`
func TestIssue42396 ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
2022-10-04 09:00:31 -04:00
if ! platform . RaceDetectorSupported ( runtime . GOOS , runtime . GOARCH ) {
cmd/link: report error if builtin referenced but not defined
When the compiler refers to a runtime builtin, it emits an indexed
symbol reference in the object file via predetermined/preassigned ID
within the PkgIdxBuiltin pseudo-package. At link time when the loader
encounters these references, it redirects them to the corresponding
defined symbol in the runtime package. This redirection process
currently assumes that if a runtime builtin is referenced, we'll
always have a definition for it. This assumption holds in most cases,
however for the builtins "runtime.racefuncenter" and
"runtime.racefuncexit", we'll only see definitions if the runtime
package we're linking against was built with "-race".
In the bug in question, build passes "-gcflags=-race" during
compilation of the main package, but doesn't pass "-race" directly to
'go build', and as a result the final link combines a
race-instrumented main with a non-race runtime; this results in R_CALL
relocations with zero-valued target symbols, resulting in a panic
during stack checking.
This patch changes the loader's resolve method to detect situations
where we're asking for builtin "runtime.X", but the runtime package
read in doesn't contain a definition for X.
Fixes #42396.
Change-Id: Iafd38bd3b0f7f462868d120ccd4d7d1b88b27436
Reviewed-on: https://go-review.googlesource.com/c/go/+/267881
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-11-05 14:19:47 -05:00
t . Skip ( "no race detector support" )
}
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
cmd/link: report error if builtin referenced but not defined
When the compiler refers to a runtime builtin, it emits an indexed
symbol reference in the object file via predetermined/preassigned ID
within the PkgIdxBuiltin pseudo-package. At link time when the loader
encounters these references, it redirects them to the corresponding
defined symbol in the runtime package. This redirection process
currently assumes that if a runtime builtin is referenced, we'll
always have a definition for it. This assumption holds in most cases,
however for the builtins "runtime.racefuncenter" and
"runtime.racefuncexit", we'll only see definitions if the runtime
package we're linking against was built with "-race".
In the bug in question, build passes "-gcflags=-race" during
compilation of the main package, but doesn't pass "-race" directly to
'go build', and as a result the final link combines a
race-instrumented main with a non-race runtime; this results in R_CALL
relocations with zero-valued target symbols, resulting in a panic
during stack checking.
This patch changes the loader's resolve method to detect situations
where we're asking for builtin "runtime.X", but the runtime package
read in doesn't contain a definition for X.
Fixes #42396.
Change-Id: Iafd38bd3b0f7f462868d120ccd4d7d1b88b27436
Reviewed-on: https://go-review.googlesource.com/c/go/+/267881
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-11-05 14:19:47 -05:00
src := filepath . Join ( tmpdir , "main.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testIssue42396src ) , 0666 )
cmd/link: report error if builtin referenced but not defined
When the compiler refers to a runtime builtin, it emits an indexed
symbol reference in the object file via predetermined/preassigned ID
within the PkgIdxBuiltin pseudo-package. At link time when the loader
encounters these references, it redirects them to the corresponding
defined symbol in the runtime package. This redirection process
currently assumes that if a runtime builtin is referenced, we'll
always have a definition for it. This assumption holds in most cases,
however for the builtins "runtime.racefuncenter" and
"runtime.racefuncexit", we'll only see definitions if the runtime
package we're linking against was built with "-race".
In the bug in question, build passes "-gcflags=-race" during
compilation of the main package, but doesn't pass "-race" directly to
'go build', and as a result the final link combines a
race-instrumented main with a non-race runtime; this results in R_CALL
relocations with zero-valued target symbols, resulting in a panic
during stack checking.
This patch changes the loader's resolve method to detect situations
where we're asking for builtin "runtime.X", but the runtime package
read in doesn't contain a definition for X.
Fixes #42396.
Change-Id: Iafd38bd3b0f7f462868d120ccd4d7d1b88b27436
Reviewed-on: https://go-review.googlesource.com/c/go/+/267881
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-11-05 14:19:47 -05:00
if err != nil {
t . Fatalf ( "failed to write source file: %v" , err )
}
exe := filepath . Join ( tmpdir , "main.exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-gcflags=-race" , "-o" , exe , src )
cmd/link: report error if builtin referenced but not defined
When the compiler refers to a runtime builtin, it emits an indexed
symbol reference in the object file via predetermined/preassigned ID
within the PkgIdxBuiltin pseudo-package. At link time when the loader
encounters these references, it redirects them to the corresponding
defined symbol in the runtime package. This redirection process
currently assumes that if a runtime builtin is referenced, we'll
always have a definition for it. This assumption holds in most cases,
however for the builtins "runtime.racefuncenter" and
"runtime.racefuncexit", we'll only see definitions if the runtime
package we're linking against was built with "-race".
In the bug in question, build passes "-gcflags=-race" during
compilation of the main package, but doesn't pass "-race" directly to
'go build', and as a result the final link combines a
race-instrumented main with a non-race runtime; this results in R_CALL
relocations with zero-valued target symbols, resulting in a panic
during stack checking.
This patch changes the loader's resolve method to detect situations
where we're asking for builtin "runtime.X", but the runtime package
read in doesn't contain a definition for X.
Fixes #42396.
Change-Id: Iafd38bd3b0f7f462868d120ccd4d7d1b88b27436
Reviewed-on: https://go-review.googlesource.com/c/go/+/267881
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-11-05 14:19:47 -05:00
out , err := cmd . CombinedOutput ( )
if err == nil {
t . Fatalf ( "build unexpectedly succeeded" )
}
// Check to make sure that we see a reasonable error message
// and not a panic.
if strings . Contains ( string ( out ) , "panic:" ) {
t . Fatalf ( "build should not fail with panic:\n%s" , out )
}
const want = "reference to undefined builtin"
if ! strings . Contains ( string ( out ) , want ) {
t . Fatalf ( "error message incorrect: expected it to contain %q but instead got:\n%s\n" , want , out )
}
}
2020-12-14 18:52:13 -05:00
const testLargeRelocSrc = `
package main
var x = [ 1 << 25 ] byte { 1 << 23 : 23 , 1 << 24 : 24 }
cmd/link: don't use label symbol for absolute address relocations on ARM64 PE
On ARM64 PE, when external linking, the PE relocation does not
have an explicit addend, and instead has the addend encoded in
the instruction or data. An instruction (e.g. ADRP, ADD) has
limited width for the addend, so when the addend is large we use
a label symbol, which points to the middle of the original target
symbol, and a smaller addend. But for an absolute address
relocation in the data section, we have the full width to encode
the addend and we should not use the label symbol. Also, since we
do not adjust the addend in the data, using the label symbol will
actually make it point to the wrong address. E.g for an R_ADDR
relocation targeting x+0x123456, we should emit 0x123456 in the
data with an IMAGE_REL_ARM64_ADDR64 relocation pointing to x,
whereas the current code emits 0x123456 in the data with an
IMAGE_REL_ARM64_ADDR64 relocation pointing to the label symbol
x+1MB, so it will actually be resolved to x+0x223456. This CL
fixes this.
Fixes #47557.
Change-Id: I64e02b56f1d792f8c20ca61b78623ef5c3e34d7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/360895
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-11-02 18:30:08 -04:00
var addr = [ ... ] * byte {
& x [ 1 << 23 - 1 ] ,
& x [ 1 << 23 ] ,
& x [ 1 << 23 + 1 ] ,
& x [ 1 << 24 - 1 ] ,
& x [ 1 << 24 ] ,
& x [ 1 << 24 + 1 ] ,
}
2020-12-14 18:52:13 -05:00
func main ( ) {
cmd/link: don't use label symbol for absolute address relocations on ARM64 PE
On ARM64 PE, when external linking, the PE relocation does not
have an explicit addend, and instead has the addend encoded in
the instruction or data. An instruction (e.g. ADRP, ADD) has
limited width for the addend, so when the addend is large we use
a label symbol, which points to the middle of the original target
symbol, and a smaller addend. But for an absolute address
relocation in the data section, we have the full width to encode
the addend and we should not use the label symbol. Also, since we
do not adjust the addend in the data, using the label symbol will
actually make it point to the wrong address. E.g for an R_ADDR
relocation targeting x+0x123456, we should emit 0x123456 in the
data with an IMAGE_REL_ARM64_ADDR64 relocation pointing to x,
whereas the current code emits 0x123456 in the data with an
IMAGE_REL_ARM64_ADDR64 relocation pointing to the label symbol
x+1MB, so it will actually be resolved to x+0x223456. This CL
fixes this.
Fixes #47557.
Change-Id: I64e02b56f1d792f8c20ca61b78623ef5c3e34d7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/360895
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-11-02 18:30:08 -04:00
// check relocations in instructions
2020-12-14 18:52:13 -05:00
check ( x [ 1 << 23 - 1 ] , 0 )
check ( x [ 1 << 23 ] , 23 )
check ( x [ 1 << 23 + 1 ] , 0 )
check ( x [ 1 << 24 - 1 ] , 0 )
check ( x [ 1 << 24 ] , 24 )
check ( x [ 1 << 24 + 1 ] , 0 )
cmd/link: don't use label symbol for absolute address relocations on ARM64 PE
On ARM64 PE, when external linking, the PE relocation does not
have an explicit addend, and instead has the addend encoded in
the instruction or data. An instruction (e.g. ADRP, ADD) has
limited width for the addend, so when the addend is large we use
a label symbol, which points to the middle of the original target
symbol, and a smaller addend. But for an absolute address
relocation in the data section, we have the full width to encode
the addend and we should not use the label symbol. Also, since we
do not adjust the addend in the data, using the label symbol will
actually make it point to the wrong address. E.g for an R_ADDR
relocation targeting x+0x123456, we should emit 0x123456 in the
data with an IMAGE_REL_ARM64_ADDR64 relocation pointing to x,
whereas the current code emits 0x123456 in the data with an
IMAGE_REL_ARM64_ADDR64 relocation pointing to the label symbol
x+1MB, so it will actually be resolved to x+0x223456. This CL
fixes this.
Fixes #47557.
Change-Id: I64e02b56f1d792f8c20ca61b78623ef5c3e34d7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/360895
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-11-02 18:30:08 -04:00
// check absolute address relocations in data
check ( * addr [ 0 ] , 0 )
check ( * addr [ 1 ] , 23 )
check ( * addr [ 2 ] , 0 )
check ( * addr [ 3 ] , 0 )
check ( * addr [ 4 ] , 24 )
check ( * addr [ 5 ] , 0 )
2020-12-14 18:52:13 -05:00
}
func check ( x , y byte ) {
if x != y {
panic ( "FAIL" )
}
}
`
func TestLargeReloc ( t * testing . T ) {
// Test that large relocation addend is handled correctly.
// In particular, on darwin/arm64 when external linking,
// Mach-O relocation has only 24-bit addend. See issue #42738.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
2021-03-07 20:52:39 -08:00
tmpdir := t . TempDir ( )
2020-12-14 18:52:13 -05:00
src := filepath . Join ( tmpdir , "x.go" )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( src , [ ] byte ( testLargeRelocSrc ) , 0666 )
2020-12-14 18:52:13 -05:00
if err != nil {
t . Fatalf ( "failed to write source file: %v" , err )
}
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "run" , src )
2020-12-14 18:52:13 -05:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "build failed: %v. output:\n%s" , err , out )
}
if testenv . HasCGO ( ) { // currently all targets that support cgo can external link
2025-11-17 11:54:48 -08:00
cmd = goCmd ( t , "run" , "-ldflags=-linkmode=external" , src )
2020-12-14 18:52:13 -05:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build failed: %v. output:\n%s" , err , out )
}
}
}
2022-03-21 13:45:50 -04:00
func TestUnlinkableObj ( t * testing . T ) {
// Test that the linker emits an error with unlinkable object.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
2022-12-01 16:14:11 -08:00
if true /* was buildcfg.Experiment.Unified */ {
cmd/compile: set LocalPkg.Path to -p flag
Since CL 391014, cmd/compile now requires the -p flag to be set the
build system. This CL changes it to initialize LocalPkg.Path to the
provided path, rather than relying on writing out `"".` into object
files and expecting cmd/link to substitute them.
However, this actually involved a rather long tail of fixes. Many have
already been submitted, but a few notable ones that have to land
simultaneously with changing LocalPkg:
1. When compiling package runtime, there are really two "runtime"
packages: types.LocalPkg (the source package itself) and
ir.Pkgs.Runtime (the compiler's internal representation, for synthetic
references). Previously, these ended up creating separate link
symbols (`"".xxx` and `runtime.xxx`, respectively), but now they both
end up as `runtime.xxx`, which causes lsym collisions (notably
inittask and funcsyms).
2. test/codegen tests need to be updated to expect symbols to be named
`command-line-arguments.xxx` rather than `"".foo`.
3. The issue20014 test case is sensitive to the sort order of field
tracking symbols. In particular, the local package now sorts to its
natural place in the list, rather than to the front.
Thanks to David Chase for helping track down all of the fixes needed
for this CL.
Updates #51734.
Change-Id: Iba3041cf7ad967d18c6e17922fa06ba11798b565
Reviewed-on: https://go-review.googlesource.com/c/go/+/393715
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2022-03-17 13:27:40 -07:00
t . Skip ( "TODO(mdempsky): Fix ICE when importing unlinkable objects for GOEXPERIMENT=unified" )
}
2022-03-21 13:45:50 -04:00
tmpdir := t . TempDir ( )
2022-03-22 17:08:56 -04:00
xSrc := filepath . Join ( tmpdir , "x.go" )
pSrc := filepath . Join ( tmpdir , "p.go" )
xObj := filepath . Join ( tmpdir , "x.o" )
pObj := filepath . Join ( tmpdir , "p.o" )
2022-03-22 16:38:13 -04:00
exe := filepath . Join ( tmpdir , "x.exe" )
2022-09-21 15:51:27 -04:00
importcfgfile := filepath . Join ( tmpdir , "importcfg" )
testenv . WriteImportcfg ( t , importcfgfile , map [ string ] string { "p" : pObj } )
2022-08-28 03:38:00 +08:00
err := os . WriteFile ( xSrc , [ ] byte ( "package main\nimport _ \"p\"\nfunc main() {}\n" ) , 0666 )
2022-03-21 13:45:50 -04:00
if err != nil {
t . Fatalf ( "failed to write source file: %v" , err )
}
2022-08-28 03:38:00 +08:00
err = os . WriteFile ( pSrc , [ ] byte ( "package p\n" ) , 0666 )
2022-03-22 17:08:56 -04:00
if err != nil {
t . Fatalf ( "failed to write source file: %v" , err )
}
2022-11-15 10:21:21 -05:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgfile , "-o" , pObj , pSrc ) // without -p
2022-03-21 13:45:50 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
2022-03-22 17:08:56 -04:00
t . Fatalf ( "compile p.go failed: %v. output:\n%s" , err , out )
2022-03-21 13:45:50 -04:00
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgfile , "-p=main" , "-o" , xObj , xSrc )
2022-03-22 17:08:56 -04:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compile x.go failed: %v. output:\n%s" , err , out )
}
2025-11-17 11:54:48 -08:00
cmd = linkCmd ( t , "-importcfg=" + importcfgfile , "-o" , exe , xObj )
2022-03-21 13:45:50 -04:00
out , err = cmd . CombinedOutput ( )
if err == nil {
t . Fatalf ( "link did not fail" )
}
if ! bytes . Contains ( out , [ ] byte ( "unlinkable object" ) ) {
t . Errorf ( "did not see expected error message. out:\n%s" , out )
}
2022-03-22 17:08:56 -04:00
// It is okay to omit -p for (only) main package.
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgfile , "-p=p" , "-o" , pObj , pSrc )
2022-03-22 17:08:56 -04:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compile p.go failed: %v. output:\n%s" , err , out )
}
2022-11-15 10:21:21 -05:00
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "compile" , "-importcfg=" + importcfgfile , "-o" , xObj , xSrc ) // without -p
2022-03-22 17:08:56 -04:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "compile failed: %v. output:\n%s" , err , out )
}
2022-09-21 15:51:27 -04:00
2025-11-17 11:54:48 -08:00
cmd = linkCmd ( t , "-importcfg=" + importcfgfile , "-o" , exe , xObj )
2022-03-22 17:08:56 -04:00
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "link failed: %v. output:\n%s" , err , out )
}
2022-03-21 13:45:50 -04:00
}
2022-06-27 14:58:58 -07:00
2023-07-12 16:54:32 -04:00
func TestExtLinkCmdlineDeterminism ( t * testing . T ) {
// Test that we pass flags in deterministic order to the external linker
testenv . MustHaveGoBuild ( t )
testenv . MustHaveCGO ( t ) // this test requires -linkmode=external
t . Parallel ( )
// test source code, with some cgo exports
testSrc := `
package main
import "C"
//export F1
func F1 ( ) { }
//export F2
func F2 ( ) { }
//export F3
func F3 ( ) { }
func main ( ) { }
`
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "x.go" )
if err := os . WriteFile ( src , [ ] byte ( testSrc ) , 0666 ) ; err != nil {
t . Fatal ( err )
}
exe := filepath . Join ( tmpdir , "x.exe" )
2024-03-08 02:59:26 +00:00
// Use a deterministic tmp directory so the temporary file paths are
// deterministic.
2023-07-12 16:54:32 -04:00
linktmp := filepath . Join ( tmpdir , "linktmp" )
if err := os . Mkdir ( linktmp , 0777 ) ; err != nil {
t . Fatal ( err )
}
// Link with -v -linkmode=external to see the flags we pass to the
// external linker.
ldflags := "-ldflags=-v -linkmode=external -tmpdir=" + linktmp
var out0 [ ] byte
for i := 0 ; i < 5 ; i ++ {
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , ldflags , "-o" , exe , src )
2023-07-12 16:54:32 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build failed: %v, output:\n%s" , err , out )
}
if err := os . Remove ( exe ) ; err != nil {
t . Fatal ( err )
}
2024-03-08 02:59:26 +00:00
// extract the "host link" invocation
2023-07-12 16:54:32 -04:00
j := bytes . Index ( out , [ ] byte ( "\nhost link:" ) )
if j == - 1 {
t . Fatalf ( "host link step not found, output:\n%s" , out )
}
out = out [ j + 1 : ]
k := bytes . Index ( out , [ ] byte ( "\n" ) )
if k == - 1 {
t . Fatalf ( "no newline after host link, output:\n%s" , out )
}
out = out [ : k ]
// filter out output file name, which is passed by the go
// command and is nondeterministic.
fs := bytes . Fields ( out )
for i , f := range fs {
if bytes . Equal ( f , [ ] byte ( ` "-o" ` ) ) && i + 1 < len ( fs ) {
fs [ i + 1 ] = [ ] byte ( "a.out" )
break
}
}
out = bytes . Join ( fs , [ ] byte { ' ' } )
if i == 0 {
out0 = out
continue
}
if ! bytes . Equal ( out0 , out ) {
t . Fatalf ( "output differ:\n%s\n==========\n%s" , out0 , out )
}
}
}
2022-06-27 14:58:58 -07:00
// TestResponseFile tests that creating a response file to pass to the
// external linker works correctly.
func TestResponseFile ( t * testing . T ) {
t . Parallel ( )
testenv . MustHaveGoBuild ( t )
// This test requires -linkmode=external. Currently all
// systems that support cgo support -linkmode=external.
testenv . MustHaveCGO ( t )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "x.go" )
if err := os . WriteFile ( src , [ ] byte ( ` package main; import "C"; func main() { } ` ) , 0666 ) ; err != nil {
t . Fatal ( err )
}
2025-11-17 11:54:48 -08:00
// We don't use goCmd here, as -toolexec doesn't use response files.
// This test is more for the go command than the linker anyhow.
2022-06-27 14:58:58 -07:00
cmd := testenv . Command ( t , testenv . GoToolPath ( t ) , "build" , "-o" , "output" , "x.go" )
cmd . Dir = tmpdir
// Add enough arguments to push cmd/link into creating a response file.
var sb strings . Builder
sb . WriteString ( ` '-ldflags=all="-extldflags= ` )
for i := 0 ; i < sys . ExecArgLengthLimit / len ( "-g" ) ; i ++ {
if i > 0 {
sb . WriteString ( " " )
}
sb . WriteString ( "-g" )
}
sb . WriteString ( ` "' ` )
cmd = testenv . CleanCmdEnv ( cmd )
cmd . Env = append ( cmd . Env , "GOFLAGS=" + sb . String ( ) )
out , err := cmd . CombinedOutput ( )
if len ( out ) > 0 {
t . Logf ( "%s" , out )
}
if err != nil {
t . Error ( err )
}
}
2023-06-08 12:19:54 -04:00
func TestDynimportVar ( t * testing . T ) {
// Test that we can access dynamically imported variables.
// Currently darwin only.
if runtime . GOOS != "darwin" {
t . Skip ( "skip on non-darwin platform" )
}
testenv . MustHaveGoBuild ( t )
testenv . MustHaveCGO ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
exe := filepath . Join ( tmpdir , "a.exe" )
src := filepath . Join ( "testdata" , "dynimportvar" , "main.go" )
for _ , mode := range [ ] string { "internal" , "external" } {
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-linkmode=" + mode , "-o" , exe , src )
2023-06-08 12:19:54 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build (linkmode=%s) failed: %v\n%s" , mode , err , out )
}
cmd = testenv . Command ( t , exe )
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "executable failed to run (%s): %v\n%s" , mode , err , out )
}
}
}
2023-06-28 13:27:17 -04:00
const helloSrc = `
package main
var X = 42
var Y int
func main ( ) { println ( "hello" , X , Y ) }
`
func TestFlagS ( t * testing . T ) {
// Test that the -s flag strips the symbol table.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
exe := filepath . Join ( tmpdir , "a.exe" )
src := filepath . Join ( tmpdir , "a.go" )
err := os . WriteFile ( src , [ ] byte ( helloSrc ) , 0666 )
if err != nil {
t . Fatal ( err )
}
modes := [ ] string { "auto" }
if testenv . HasCGO ( ) {
modes = append ( modes , "external" )
}
// check a text symbol, a data symbol, and a BSS symbol
syms := [ ] string { "main.main" , "main.X" , "main.Y" }
for _ , mode := range modes {
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-s -linkmode=" + mode , "-o" , exe , src )
2023-06-28 13:27:17 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build (linkmode=%s) failed: %v\n%s" , mode , err , out )
}
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "nm" , exe )
out , err = cmd . CombinedOutput ( )
2025-10-01 20:08:18 +00:00
if err != nil {
if _ , ok := errors . AsType [ * exec . ExitError ] ( err ) ; ! ok {
// Error exit is fine as it may have no symbols.
// On darwin we need to emit dynamic symbol references so it
// actually has some symbols, and nm succeeds.
t . Errorf ( "(mode=%s) go tool nm failed: %v\n%s" , mode , err , out )
}
2023-06-28 13:27:17 -04:00
}
for _ , s := range syms {
if bytes . Contains ( out , [ ] byte ( s ) ) {
t . Errorf ( "(mode=%s): unexpected symbol %s" , mode , s )
}
}
}
}
2024-02-06 18:08:34 -05:00
func TestRandLayout ( t * testing . T ) {
// Test that the -randlayout flag randomizes function order and
// generates a working binary.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "hello.go" )
err := os . WriteFile ( src , [ ] byte ( trivialSrc ) , 0666 )
if err != nil {
t . Fatal ( err )
}
var syms [ 2 ] string
for i , seed := range [ ] string { "123" , "456" } {
exe := filepath . Join ( tmpdir , "hello" + seed + ".exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-ldflags=-randlayout=" + seed , "-o" , exe , src )
2024-02-06 18:08:34 -05:00
out , err := cmd . CombinedOutput ( )
if err != nil {
2024-05-24 13:18:50 -04:00
t . Fatalf ( "seed=%v: build failed: %v\n%s" , seed , err , out )
2024-02-06 18:08:34 -05:00
}
cmd = testenv . Command ( t , exe )
err = cmd . Run ( )
if err != nil {
2024-05-24 13:18:50 -04:00
t . Fatalf ( "seed=%v: executable failed to run: %v\n%s" , seed , err , out )
2024-02-06 18:08:34 -05:00
}
cmd = testenv . Command ( t , testenv . GoToolPath ( t ) , "tool" , "nm" , exe )
out , err = cmd . CombinedOutput ( )
if err != nil {
2024-05-24 13:18:50 -04:00
t . Fatalf ( "seed=%v: fail to run \"go tool nm\": %v\n%s" , seed , err , out )
2024-02-06 18:08:34 -05:00
}
syms [ i ] = string ( out )
}
if syms [ 0 ] == syms [ 1 ] {
t . Errorf ( "randlayout with different seeds produced same layout:\n%s\n===\n\n%s" , syms [ 0 ] , syms [ 1 ] )
}
}
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
cmd/link: disallow pull-only linknames
As mentioned in CL 584598, linkname is a mechanism that, when
abused, can break API integrity and even safety of Go programs.
CL 584598 is a first step to restrict the use of linknames, by
implementing a blocklist. This CL takes a step further, tightening
up the restriction by allowing linkname references ("pull") only
when the definition side explicitly opts into it, by having a
linkname on the definition (possibly to itself). This way, it is at
least clear on the definition side that the symbol, despite being
unexported, is accessed outside of the package. Unexported symbols
without linkname can now be actually private. This is similar to
the symbol visibility rule used by gccgo for years (which defines
unexported non-linknamed symbols as C static symbols).
As there can be pull-only linknames in the wild that may be broken
by this change, we currently only enforce this rule for symbols
defined in the standard library. Push linknames are added in the
standard library to allow things build.
Linkname references to external (non-Go) symbols are still allowed,
as their visibility is controlled by the C symbol visibility rules
and enforced by the C (static or dynamic) linker.
Assembly symbols are treated similar to linknamed symbols.
This is controlled by -checklinkname linker flag, currently not
enabled by default. A follow-up CL will enable it by default.
Change-Id: I07344f5c7a02124dbbef0fbc8fec3b666a4b2b0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/585358
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2024-05-14 00:01:49 -04:00
func TestCheckLinkname ( t * testing . T ) {
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
// Test that code containing blocked linknames does not build.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
tests := [ ] struct {
src string
ok bool
} {
// use (instantiation) of public API is ok
{ "ok.go" , true } ,
// push linkname is ok
{ "push.go" , true } ,
cmd/link: allow linkname reference to a TEXT symbol regardless of size
In CL 660696, we made the linker to choose the symbol of the
larger size in case there are multiple contentless declarations of
the same symbol. We also made it emit an error in the case that
there are a contentless declaration of a larger size and a
definition with content of a smaller size. In this case, we should
choose the definition with content, but the code accesses it
through the declaration of the larger size could fall into the
next symbol, potentially causing data corruption. So we disallowed
it.
There is one spcial case, though, that some code uses a linknamed
variable declaration to reference a function in assembly, in order
to take its address. The variable is often declared as uintptr.
The function symbol is the definition, which could sometimes be
shorter. This would trigger the error case above, causing existing
code failing to build.
This CL allows it as a special case. It is still not safe to
access the variable's content. But it is actually okay to just
take its address, which the existing code often do.
Fixes #73617.
Change-Id: I467381bc5f6baa16caee6752a0a824c7185422f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/676636
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-21 14:32:21 -04:00
// using a linknamed variable to reference an assembly
// function in the same package is ok
{ "textvar" , true } ,
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
// pull linkname of blocked symbol is not ok
{ "coro.go" , false } ,
{ "coro_var.go" , false } ,
// assembly reference is not ok
{ "coro_asm" , false } ,
cmd/link: disallow pull-only linknames
As mentioned in CL 584598, linkname is a mechanism that, when
abused, can break API integrity and even safety of Go programs.
CL 584598 is a first step to restrict the use of linknames, by
implementing a blocklist. This CL takes a step further, tightening
up the restriction by allowing linkname references ("pull") only
when the definition side explicitly opts into it, by having a
linkname on the definition (possibly to itself). This way, it is at
least clear on the definition side that the symbol, despite being
unexported, is accessed outside of the package. Unexported symbols
without linkname can now be actually private. This is similar to
the symbol visibility rule used by gccgo for years (which defines
unexported non-linknamed symbols as C static symbols).
As there can be pull-only linknames in the wild that may be broken
by this change, we currently only enforce this rule for symbols
defined in the standard library. Push linknames are added in the
standard library to allow things build.
Linkname references to external (non-Go) symbols are still allowed,
as their visibility is controlled by the C symbol visibility rules
and enforced by the C (static or dynamic) linker.
Assembly symbols are treated similar to linknamed symbols.
This is controlled by -checklinkname linker flag, currently not
enabled by default. A follow-up CL will enable it by default.
Change-Id: I07344f5c7a02124dbbef0fbc8fec3b666a4b2b0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/585358
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2024-05-14 00:01:49 -04:00
// pull-only linkname is not ok
{ "coro2.go" , false } ,
2024-12-12 14:31:45 -05:00
// pull linkname of a builtin symbol is not ok
{ "builtin.go" , false } ,
2025-08-28 17:32:09 +08:00
{ "addmoduledata.go" , false } ,
runtime: add runtime.freegc to reduce GC work
This CL is part of a set of CLs that attempt to reduce how much work the
GC must do. See the design in https://go.dev/design/74299-runtime-freegc
This CL adds runtime.freegc:
func freegc(ptr unsafe.Pointer, uintptr size, noscan bool)
Memory freed via runtime.freegc is made immediately reusable for
the next allocation in the same size class, without waiting for a
GC cycle, and hence can dramatically reduce pressure on the GC. A sample
microbenchmark included below shows strings.Builder operating roughly
2x faster.
An experimental modification to reflect to use runtime.freegc
and then using that reflect with json/v2 gave reported memory
allocation reductions of -43.7%, -32.9%, -21.9%, -22.0%, -1.0%
for the 5 official real-world unmarshalling benchmarks from
go-json-experiment/jsonbench by the authors of json/v2, covering
the CanadaGeometry through TwitterStatus datasets.
Note: there is no intent to modify the standard library to have
explicit calls to runtime.freegc, and of course such an ability
would never be exposed to end-user code.
Later CLs in this stack teach the compiler how to automatically
insert runtime.freegc calls when it can prove it is safe to do so.
(The reflect modification and other experimental changes to
the standard library were just that -- experiments. It was
very helpful while initially developing runtime.freegc to see
more complex uses and closer-to-real-world benchmark results
prior to updating the compiler.)
This CL only addresses noscan span classes (heap objects without
pointers), such as the backing memory for a []byte or string. A
follow-on CL adds support for heap objects with pointers.
If we update strings.Builder to explicitly call runtime.freegc on its
internal buf after a resize operation (but without freeing the usually
final incarnation of buf that will be returned to the user as a string),
we can see some nice benchmark results on the existing strings
benchmarks that call Builder.Write N times and then call Builder.String.
Here, the (uncommon) case of a single Builder.Write is not helped (given
it never resizes after first alloc if there is only one Write), but the
impact grows such that it is up to ~2x faster as there are more resize
operations due to more strings.Builder.Write calls:
│ disabled.out │ new-free-20.txt │
│ sec/op │ sec/op vs base │
BuildString_Builder/1Write_36Bytes_NoGrow-4 55.82n ± 2% 55.86n ± 2% ~ (p=0.794 n=20)
BuildString_Builder/2Write_36Bytes_NoGrow-4 125.2n ± 2% 115.4n ± 1% -7.86% (p=0.000 n=20)
BuildString_Builder/3Write_36Bytes_NoGrow-4 224.0n ± 1% 188.2n ± 2% -16.00% (p=0.000 n=20)
BuildString_Builder/5Write_36Bytes_NoGrow-4 239.1n ± 9% 205.1n ± 1% -14.20% (p=0.000 n=20)
BuildString_Builder/8Write_36Bytes_NoGrow-4 422.8n ± 3% 325.4n ± 1% -23.04% (p=0.000 n=20)
BuildString_Builder/10Write_36Bytes_NoGrow-4 436.9n ± 2% 342.3n ± 1% -21.64% (p=0.000 n=20)
BuildString_Builder/100Write_36Bytes_NoGrow-4 4.403µ ± 1% 2.381µ ± 2% -45.91% (p=0.000 n=20)
BuildString_Builder/1000Write_36Bytes_NoGrow-4 48.28µ ± 2% 21.38µ ± 2% -55.71% (p=0.000 n=20)
See the design document for more discussion of the strings.Builder case.
For testing, we add tests that attempt to exercise different aspects
of the underlying freegc and mallocgc behavior on the reuse path.
Validating the assist credit manipulations turned out to be subtle,
so a test for that is added in the next CL. There are also
invariant checks added, controlled by consts (primarily the
doubleCheckReusable const currently).
This CL also adds support in runtime.freegc for GODEBUG=clobberfree=1
to immediately overwrite freed memory with 0xdeadbeef, which
can help a higher-level test fail faster in the event of a bug,
and also the GC specifically looks for that pattern and throws
a fatal error if it unexpectedly finds it.
A later CL (currently experimental) adds GODEBUG=clobberfree=2,
which uses mprotect (or VirtualProtect on Windows) to set
freed memory to fault if read or written, until the runtime
later unprotects the memory on the mallocgc reuse path.
For the cases where a normal allocation is happening without any reuse,
some initial microbenchmarks suggest the impact of these changes could
be small to negligible (at least with GOAMD64=v3):
goos: linux
goarch: amd64
pkg: runtime
cpu: AMD EPYC 7B13
│ base-512M-v3.bench │ ps16-512M-goamd64-v3.bench │
│ sec/op │ sec/op vs base │
Malloc8-16 11.01n ± 1% 10.94n ± 1% -0.68% (p=0.038 n=20)
Malloc16-16 17.15n ± 1% 17.05n ± 0% -0.55% (p=0.007 n=20)
Malloc32-16 18.65n ± 1% 18.42n ± 0% -1.26% (p=0.000 n=20)
MallocTypeInfo8-16 18.63n ± 0% 18.36n ± 0% -1.45% (p=0.000 n=20)
MallocTypeInfo16-16 22.32n ± 0% 22.65n ± 0% +1.50% (p=0.000 n=20)
MallocTypeInfo32-16 23.37n ± 0% 23.89n ± 0% +2.23% (p=0.000 n=20)
geomean 18.02n 18.01n -0.05%
These last benchmark results include the runtime updates to support
span classes with pointers (which was originally part of this CL,
but later split out for ease of review).
Updates #74299
Change-Id: Icceaa0f79f85c70cd1a718f9a4e7f0cf3d77803c
Reviewed-on: https://go-review.googlesource.com/c/go/+/673695
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
2025-11-04 09:33:17 -05:00
{ "freegc.go" , false } ,
cmd/link: disallow pull-only linknames
As mentioned in CL 584598, linkname is a mechanism that, when
abused, can break API integrity and even safety of Go programs.
CL 584598 is a first step to restrict the use of linknames, by
implementing a blocklist. This CL takes a step further, tightening
up the restriction by allowing linkname references ("pull") only
when the definition side explicitly opts into it, by having a
linkname on the definition (possibly to itself). This way, it is at
least clear on the definition side that the symbol, despite being
unexported, is accessed outside of the package. Unexported symbols
without linkname can now be actually private. This is similar to
the symbol visibility rule used by gccgo for years (which defines
unexported non-linknamed symbols as C static symbols).
As there can be pull-only linknames in the wild that may be broken
by this change, we currently only enforce this rule for symbols
defined in the standard library. Push linknames are added in the
standard library to allow things build.
Linkname references to external (non-Go) symbols are still allowed,
as their visibility is controlled by the C symbol visibility rules
and enforced by the C (static or dynamic) linker.
Assembly symbols are treated similar to linknamed symbols.
This is controlled by -checklinkname linker flag, currently not
enabled by default. A follow-up CL will enable it by default.
Change-Id: I07344f5c7a02124dbbef0fbc8fec3b666a4b2b0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/585358
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2024-05-14 00:01:49 -04:00
// legacy bad linkname is ok, for now
{ "fastrand.go" , true } ,
2024-05-16 17:19:15 -04:00
{ "badlinkname.go" , true } ,
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
}
for _ , test := range tests {
test := test
t . Run ( test . src , func ( t * testing . T ) {
t . Parallel ( )
cmd/link: allow linkname reference to a TEXT symbol regardless of size
In CL 660696, we made the linker to choose the symbol of the
larger size in case there are multiple contentless declarations of
the same symbol. We also made it emit an error in the case that
there are a contentless declaration of a larger size and a
definition with content of a smaller size. In this case, we should
choose the definition with content, but the code accesses it
through the declaration of the larger size could fall into the
next symbol, potentially causing data corruption. So we disallowed
it.
There is one spcial case, though, that some code uses a linknamed
variable declaration to reference a function in assembly, in order
to take its address. The variable is often declared as uintptr.
The function symbol is the definition, which could sometimes be
shorter. This would trigger the error case above, causing existing
code failing to build.
This CL allows it as a special case. It is still not safe to
access the variable's content. But it is actually okay to just
take its address, which the existing code often do.
Fixes #73617.
Change-Id: I467381bc5f6baa16caee6752a0a824c7185422f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/676636
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-21 14:32:21 -04:00
src := "./testdata/linkname/" + test . src
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
exe := filepath . Join ( tmpdir , test . src + ".exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , exe , src )
cmd/compile, cmd/link: disallow linkname of some newly added internal functions
Go API is defined through exported symbols. When a package is
imported, the compiler ensures that only exported symbols can be
accessed, and the go command ensures that internal packages cannot
be imported. This ensures API integrity. But there is a hole:
using linkname, one can access internal or non-exported symbols.
Linkname is a mechanism to give access of a symbol to a package
without adding it to the public API. It is intended for coupled
packages to share some implementation details, or to break
circular dependencies, and both "push" (definition) and "pull"
(reference) sides are controlled, so they can be updated in sync.
Nevertheless, it is abused as a mechanism to reach into internal
details of other packages uncontrolled by the user, notably the
runtime. As the other package evolves, the code often breaks,
because the linknamed symbol may no longer exist, or change its
signature or semantics.
This CL adds a mechanism to enforce the integrity of linknames.
Generally, "push" linkname is allowed, as the package defining
the symbol explicitly opt in for access outside of the package.
"Pull" linkname is checked and only allowed in some circumstances.
Given that there are existing code that use "pull"-only linkname
to access other package's internals, disallowing it completely is
too much a change at this point in the release cycle. For a start,
implement a hard-coded blocklist, which contains some newly added
internal functions that, if used inappropriately, may break memory
safety or runtime integrity. All blocked symbols are newly added
in Go 1.23. So existing code that builds with Go 1.22 will
continue to build.
For the implementation, when compiling a package, we mark
linknamed symbols in the current package with an attribute. At
link time, marked linknamed symbols are checked against the
blocklist. Care is taken so it distinguishes a linkname reference
in the current package vs. a reference of a linkname from another
package and propagated to the current package (e.g. through
inlining or instantiation).
Symbol references in assembly code are similar to linknames, and
are treated similarly.
Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/584598
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-09 17:07:43 -04:00
out , err := cmd . CombinedOutput ( )
if test . ok && err != nil {
t . Errorf ( "build failed unexpectedly: %v:\n%s" , err , out )
}
if ! test . ok && err == nil {
t . Errorf ( "build succeeded unexpectedly: %v:\n%s" , err , out )
}
} )
}
}
2025-03-25 16:55:54 -04:00
func TestLinknameBSS ( t * testing . T ) {
// Test that the linker chooses the right one as the definition
// for linknamed variables. See issue #72032.
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( "testdata" , "linkname" , "sched.go" )
exe := filepath . Join ( tmpdir , "sched.exe" )
2025-11-17 11:54:48 -08:00
cmd := goCmd ( t , "build" , "-o" , exe , src )
2025-03-25 16:55:54 -04:00
out , err := cmd . CombinedOutput ( )
if err != nil {
t . Fatalf ( "build failed unexpectedly: %v:\n%s" , err , out )
}
// Check the symbol size.
f , err := objfile . Open ( exe )
if err != nil {
t . Fatalf ( "fail to open executable: %v" , err )
}
2025-03-31 15:03:14 -07:00
defer f . Close ( )
2025-03-25 16:55:54 -04:00
syms , err := f . Symbols ( )
if err != nil {
t . Fatalf ( "fail to get symbols: %v" , err )
}
found := false
for _ , s := range syms {
if s . Name == "runtime.sched" || s . Name == "_runtime.sched" {
found = true
if s . Size < 100 {
// As of Go 1.25 (Mar 2025), runtime.sched has 6848 bytes on
// darwin/arm64. It should always be larger than 100 bytes on
// all platforms.
t . Errorf ( "runtime.sched symbol size too small: want > 100, got %d" , s . Size )
}
}
}
if ! found {
t . Errorf ( "runtime.sched symbol not found" )
}
// Executable should run.
cmd = testenv . Command ( t , exe )
out , err = cmd . CombinedOutput ( )
if err != nil {
t . Errorf ( "executable failed to run: %v\n%s" , err , out )
}
}
2025-11-17 18:29:42 -08:00
// setValueFromBytes copies from a []byte to a variable.
// This is used to get correctly aligned values in TestFuncdataPlacement.
func setValueFromBytes [ T any ] ( p * T , s [ ] byte ) {
copy ( unsafe . Slice ( ( * byte ) ( unsafe . Pointer ( p ) ) , unsafe . Sizeof ( * p ) ) , s )
}
// Test that all funcdata values are stored in the .gopclntab section.
// This is pretty ugly as there is no API for accessing this data.
// This test will have to be updated when the data formats change.
func TestFuncdataPlacement ( t * testing . T ) {
testenv . MustHaveGoBuild ( t )
t . Parallel ( )
tmpdir := t . TempDir ( )
src := filepath . Join ( tmpdir , "x.go" )
if err := os . WriteFile ( src , [ ] byte ( trivialSrc ) , 0 o444 ) ; err != nil {
t . Fatal ( err )
}
exe := filepath . Join ( tmpdir , "x.exe" )
cmd := goCmd ( t , "build" , "-o" , exe , src )
if out , err := cmd . CombinedOutput ( ) ; err != nil {
t . Fatalf ( "build failed; %v, output:\n%s" , err , out )
}
// We want to find the funcdata in the executable.
// We look at the section table to find the .gopclntab section,
// which starts with the pcHeader.
// That will give us the table of functions,
// which we can use to find the funcdata.
ef , _ := elf . Open ( exe )
mf , _ := macho . Open ( exe )
pf , _ := pe . Open ( exe )
xf , _ := xcoff . Open ( exe )
// TODO: plan9
if ef == nil && mf == nil && pf == nil && xf == nil {
t . Skip ( "unrecognized executable file format" )
}
const moddataSymName = "runtime.firstmoduledata"
const gofuncSymName = "go:func.*"
var (
pclntab [ ] byte
pclntabAddr uint64
pclntabEnd uint64
moddataAddr uint64
moddataBytes [ ] byte
gofuncAddr uint64
imageBase uint64
)
switch {
case ef != nil :
defer ef . Close ( )
syms , err := ef . Symbols ( )
if err != nil {
t . Fatal ( err )
}
for _ , sym := range syms {
switch sym . Name {
case moddataSymName :
moddataAddr = sym . Value
case gofuncSymName :
gofuncAddr = sym . Value
}
}
for _ , sec := range ef . Sections {
if sec . Name == ".gopclntab" {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
pclntab = data
pclntabAddr = sec . Addr
pclntabEnd = sec . Addr + sec . Size
}
if sec . Flags & elf . SHF_ALLOC != 0 && moddataAddr >= sec . Addr && moddataAddr < sec . Addr + sec . Size {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
moddataBytes = data [ moddataAddr - sec . Addr : ]
}
}
case mf != nil :
defer mf . Close ( )
for _ , sym := range mf . Symtab . Syms {
switch sym . Name {
case moddataSymName :
moddataAddr = sym . Value
case gofuncSymName :
gofuncAddr = sym . Value
}
}
for _ , sec := range mf . Sections {
if sec . Name == "__gopclntab" {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
pclntab = data
pclntabAddr = sec . Addr
pclntabEnd = sec . Addr + sec . Size
}
if moddataAddr >= sec . Addr && moddataAddr < sec . Addr + sec . Size {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
moddataBytes = data [ moddataAddr - sec . Addr : ]
}
}
case pf != nil :
defer pf . Close ( )
switch ohdr := pf . OptionalHeader . ( type ) {
case * pe . OptionalHeader32 :
imageBase = uint64 ( ohdr . ImageBase )
case * pe . OptionalHeader64 :
imageBase = ohdr . ImageBase
}
var moddataSym , gofuncSym , pclntabSym , epclntabSym * pe . Symbol
for _ , sym := range pf . Symbols {
switch sym . Name {
case moddataSymName :
moddataSym = sym
case gofuncSymName :
gofuncSym = sym
case "runtime.pclntab" :
pclntabSym = sym
case "runtime.epclntab" :
epclntabSym = sym
}
}
if moddataSym == nil {
t . Fatalf ( "could not find symbol %s" , moddataSymName )
}
if gofuncSym == nil {
t . Fatalf ( "could not find symbol %s" , gofuncSymName )
}
if pclntabSym == nil {
t . Fatal ( "could not find symbol runtime.pclntab" )
}
if epclntabSym == nil {
t . Fatal ( "could not find symbol runtime.epclntab" )
}
sec := pf . Sections [ moddataSym . SectionNumber - 1 ]
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
moddataBytes = data [ moddataSym . Value : ]
moddataAddr = uint64 ( sec . VirtualAddress + moddataSym . Value )
sec = pf . Sections [ gofuncSym . SectionNumber - 1 ]
gofuncAddr = uint64 ( sec . VirtualAddress + gofuncSym . Value )
if pclntabSym . SectionNumber != epclntabSym . SectionNumber {
t . Fatalf ( "runtime.pclntab section %d != runtime.epclntab section %d" , pclntabSym . SectionNumber , epclntabSym . SectionNumber )
}
sec = pf . Sections [ pclntabSym . SectionNumber - 1 ]
data , err = sec . Data ( )
if err != nil {
t . Fatal ( err )
}
pclntab = data [ pclntabSym . Value : epclntabSym . Value ]
pclntabAddr = uint64 ( sec . VirtualAddress + pclntabSym . Value )
pclntabEnd = uint64 ( sec . VirtualAddress + epclntabSym . Value )
case xf != nil :
defer xf . Close ( )
for _ , sym := range xf . Symbols {
switch sym . Name {
case moddataSymName :
moddataAddr = sym . Value
case gofuncSymName :
gofuncAddr = sym . Value
}
}
for _ , sec := range xf . Sections {
if sec . Name == ".go.pclntab" {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
pclntab = data
pclntabAddr = sec . VirtualAddress
pclntabEnd = sec . VirtualAddress + sec . Size
}
if moddataAddr >= sec . VirtualAddress && moddataAddr < sec . VirtualAddress + sec . Size {
data , err := sec . Data ( )
if err != nil {
t . Fatal ( err )
}
moddataBytes = data [ moddataAddr - sec . VirtualAddress : ]
}
}
default :
panic ( "can't happen" )
}
if len ( pclntab ) == 0 {
t . Fatal ( "could not find pclntab section" )
}
if moddataAddr == 0 {
t . Fatalf ( "could not find %s symbol" , moddataSymName )
}
if gofuncAddr == 0 {
t . Fatalf ( "could not find %s symbol" , gofuncSymName )
}
if gofuncAddr < pclntabAddr || gofuncAddr >= pclntabEnd {
t . Fatalf ( "%s out of range: value %#x not between %#x and %#x" , gofuncSymName , gofuncAddr , pclntabAddr , pclntabEnd )
}
if len ( moddataBytes ) == 0 {
t . Fatal ( "could not find module data" )
}
// What a slice looks like in the object file.
type moddataSlice struct {
addr uintptr
len int
cap int
}
// This needs to match the struct defined in runtime/symtab.go,
// and written out by (*Link).symtab.
// This is not the complete moddata struct, only what we need here.
type moddataType struct {
pcHeader uintptr
funcnametab moddataSlice
cutab moddataSlice
filetab moddataSlice
pctab moddataSlice
pclntable moddataSlice
ftab moddataSlice
findfunctab uintptr
minpc , maxpc uintptr
text , etext uintptr
noptrdata , enoptrdata uintptr
data , edata uintptr
bss , ebss uintptr
noptrbss , enoptrbss uintptr
covctrs , ecovctrs uintptr
end , gcdata , gcbss uintptr
types , etypes uintptr
rodata uintptr
gofunc uintptr
}
// The executable is on the same system as we are running,
// so the sizes and alignments should match.
// But moddataBytes itself may not be aligned as needed.
// Copy to a variable to ensure alignment.
var moddata moddataType
setValueFromBytes ( & moddata , moddataBytes )
ftabAddr := uint64 ( moddata . ftab . addr ) - imageBase
if ftabAddr < pclntabAddr || ftabAddr >= pclntabEnd {
t . Fatalf ( "ftab address %#x not between %#x and %#x" , ftabAddr , pclntabAddr , pclntabEnd )
}
// From runtime/symtab.go and the linker function writePCToFunc.
type functab struct {
entryoff uint32
funcoff uint32
}
// The ftab slice in moddata has one extra entry used to record
// the final PC.
ftabLen := moddata . ftab . len - 1
ftab := make ( [ ] functab , ftabLen )
copy ( ftab , unsafe . Slice ( ( * functab ) ( unsafe . Pointer ( & pclntab [ ftabAddr - pclntabAddr ] ) ) , ftabLen ) )
ftabBase := uint64 ( moddata . pclntable . addr ) - imageBase
// From runtime/runtime2.go _func and the linker function writeFuncs.
type funcEntry struct {
entryOff uint32
nameOff int32
args int32
deferreturn uint32
pcsp uint32
pcfile uint32
pcln uint32
npcdata uint32
cuOffset uint32
startLine int32
funcID abi . FuncID
flag abi . FuncFlag
_ [ 1 ] byte
nfuncdata uint8
}
for i , ftabEntry := range ftab {
funcAddr := ftabBase + uint64 ( ftabEntry . funcoff )
if funcAddr < pclntabAddr || funcAddr >= pclntabEnd {
t . Errorf ( "ftab entry %d address %#x not between %#x and %#x" , i , funcAddr , pclntabAddr , pclntabEnd )
continue
}
var fe funcEntry
setValueFromBytes ( & fe , pclntab [ funcAddr - pclntabAddr : ] )
funcdataVals := funcAddr + uint64 ( unsafe . Sizeof ( fe ) ) + uint64 ( fe . npcdata * 4 )
for j := range fe . nfuncdata {
var funcdataVal uint32
setValueFromBytes ( & funcdataVal , pclntab [ funcdataVals + uint64 ( j ) * 4 - pclntabAddr : ] )
if funcdataVal == ^ uint32 ( 0 ) {
continue
}
funcdataAddr := gofuncAddr + uint64 ( funcdataVal )
if funcdataAddr < pclntabAddr || funcdataAddr >= pclntabEnd {
t . Errorf ( "ftab entry %d funcdata %d address %#x not between %#x and %#x" , i , j , funcdataAddr , pclntabAddr , pclntabEnd )
}
}
}
}