Skip to content

Commit 071e915

Browse files
author
Holger Hans Peter Freyther
committed
cmd/go: permit additional cflags when compiling
In CL 475375 the Go command started to generate the "preferlinkext" token file for "strange/dangerous" compiler flags. This serves as a hint to the Go linker whether to call the external linker or not. Permit compiler flags used by bazel and bazelbuild/rules_go during compilation of cgo code to not prefer external linking. This restores the behavior of previous versions of Go. As a side effect, it also allows these flags to appear in #cgo directives in source code. We don't know of any cases where that is actually useful, but it appears to be harmless and simplifies the implementation of the internal linking change. Fixes #60865 Change-Id: I176a6a2a2cf36293dd9aed24be928f98fa2fb6d9 GitHub-Pull-Request: #60868
1 parent 54f78cf commit 071e915

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/cmd/go/internal/work/security.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ var validCompilerFlags = []*lazyregexp.Regexp{
5959
re(`-f(no-)builtin-[a-zA-Z0-9_]*`),
6060
re(`-f(no-)?common`),
6161
re(`-f(no-)?constant-cfstrings`),
62+
re(`-fdebug-prefix-map=([^@]+)=([^@]+)`),
6263
re(`-fdiagnostics-show-note-include-stack`),
64+
re(`-ffile-prefix-map=([^@]+)=([^@]+)`),
65+
re(`-fno-canonical-system-headers`),
6366
re(`-f(no-)?eliminate-unused-debug-types`),
6467
re(`-f(no-)?exceptions`),
6568
re(`-f(no-)?fast-math`),
@@ -114,6 +117,7 @@ var validCompilerFlags = []*lazyregexp.Regexp{
114117
re(`-mthumb(-interwork)?`),
115118
re(`-mthreads`),
116119
re(`-mwindows`),
120+
re(`-no-canonical-prefixes`),
117121
re(`--param=ssp-buffer-size=[0-9]*`),
118122
re(`-pedantic(-errors)?`),
119123
re(`-pipe`),

src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ stderr preferlinkext
5555
env CGO_CFLAGS=-fprofile-instr-generate
5656
go build -x -n -o dummy.exe ./usesInternalCgo
5757
stderr preferlinkext
58+
59+
# The -fdebug-prefix-map=path is permitted for internal linking.
60+
env CGO_CFLAGS=-fdebug-prefix-map=/some/sandbox/execroot/workspace=/tmp/new
61+
go build -x -n -o dummy.exe ./usesInternalCgo
62+
! stderr preferlinkext
63+
env CGO_CFLAGS=-fdebug-prefix-map=/Users/someone/.cache/bazel/_bazel_someone/3fa7e4650c43657ead684537951f49e2/sandbox/linux-sandbox/10/execroot/rules_go_static=.
64+
go build -x -n -o dummy.exe ./usesInternalCgo
65+
! stderr preferlinkext
66+
# The -ffile-prefix-map=path is permitted for internal linking too.
67+
env CGO_CFLAGS=-ffile-prefix-map=/Users/someone/.cache/bazel/_bazel_someone/3fa7e4650c43657ead684537951f49e2/sandbox/linux-sandbox/10/execroot/rules_go_static/bazel-out/aarch64-fastbuild-ST-b33d65c724e6/bin/external/io_bazel_rules_go/stdlib_=.
68+
go build -x -n -o dummy.exe ./usesInternalCgo
69+
! stderr preferlinkext
70+
# Verifying that -fdebug-prefix-map=path, -ffile-prefix-map, -no-canonical-prefixes
71+
# and -fno-canonical-systemd-headers are permitted for internal linking.
72+
env CGO_CFLAGS=-fdebug-prefix-map=old=/tmp/new
73+
go build -x -n -o dummy.exe ./usesInternalCgo
74+
! stderr preferlinkext
75+
env CGO_CFLAGS=-ffile-prefix-map=/Users/someone/_11233/things=new
76+
go build -x -n -o dummy.exe ./usesInternalCgo
77+
! stderr preferlinkext
78+
env CGO_CFLAGS=-no-canonical-prefixes
79+
go build -x -n -o dummy.exe ./usesInternalCgo
80+
! stderr preferlinkext
81+
env CGO_CFLAGS=-fno-canonical-system-headers
82+
go build -x -n -o dummy.exe ./usesInternalCgo
83+
! stderr preferlinkext
5884
env CGO_CFLAGS=
5985

6086
[short] skip

0 commit comments

Comments
 (0)