Skip to content

Commit

Permalink
cmd/link: don't pass all linker args when testing flag
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Lynn Boger <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
ianlancetaylor committed Sep 11, 2018
1 parent 1a1c45b commit 023dbb1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/link/internal/ld/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ var start = time.Now()
func elapsed() float64 {
return time.Since(start).Seconds()
}

// contains reports whether v is in s.
func contains(s []string, v string) bool {
for _, x := range s {
if x == v {
return true
}
}
return false
}

0 comments on commit 023dbb1

Please sign in to comment.