cmd/link: don't pass all linker args when testing flag

Some linker flags can actually be input files, which can cause
misleading errors when doing the trial link, which can cause the
linker to incorrectly decide that the flag is not supported, which can
cause the link to fail.

Fixes #27510
Updates #27110
Updates #27293

Change-Id: I70c1e913cee3c813e7b267bf779bcff26d4d194a
Reviewed-on: https://go-review.googlesource.com/134057
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Lynn Boger <laboger@linux.vnet.ibm.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Ian Lance Taylor 2018-09-07 12:53:52 -07:00
parent 1a1c45b32e
commit 023dbb188d
2 changed files with 61 additions and 2 deletions

View file

@ -1379,9 +1379,58 @@ func linkerFlagSupported(linker, flag string) bool {
}
})
flagsWithNextArgSkip := []string{
"-F",
"-l",
"-L",
"-framework",
"-Wl,-framework",
"-Wl,-rpath",
"-Wl,-undefined",
}
flagsWithNextArgKeep := []string{
"-arch",
"-isysroot",
"--sysroot",
"-target",
}
prefixesToKeep := []string{
"-f",
"-m",
"-p",
"-Wl,",
"-arch",
"-isysroot",
"--sysroot",
"-target",
}
var flags []string
flags = append(flags, ldflag...)
flags = append(flags, strings.Fields(*flagExtldflags)...)
keep := false
skip := false
extldflags := strings.Fields(*flagExtldflags)
for _, f := range append(extldflags, ldflag...) {
if keep {
flags = append(flags, f)
keep = false
} else if skip {
skip = false
} else if f == "" || f[0] != '-' {
} else if contains(flagsWithNextArgSkip, f) {
skip = true
} else if contains(flagsWithNextArgKeep, f) {
flags = append(flags, f)
keep = true
} else {
for _, p := range prefixesToKeep {
if strings.HasPrefix(f, p) {
flags = append(flags, f)
break
}
}
}
}
flags = append(flags, flag, "trivial.c")
cmd := exec.Command(linker, flags...)