• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// run
2
3//go:build !nacl && !js && !wasip1 && !android && gc
4
5// Copyright 2016 The Go Authors. All rights reserved.
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file.
8
9package main
10
11import (
12	"bytes"
13	"log"
14	"os/exec"
15	"runtime"
16	"strings"
17)
18
19func main() {
20	// The cannot open file error indicates that the parsing of -B flag
21	// succeeded and it failed at a later step.
22	checkLinkOutput("0", "-B argument must start with 0x")
23	checkLinkOutput("0x", "cannot open file nonexistent.o")
24	checkLinkOutput("0x0", "-B argument must have even number of digits")
25	checkLinkOutput("0x00", "cannot open file nonexistent.o")
26	checkLinkOutput("0xYZ", "-B argument contains invalid hex digit")
27
28	maxLen := 32
29	if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
30		maxLen = 16
31	}
32	checkLinkOutput("0x"+strings.Repeat("00", maxLen), "cannot open file nonexistent.o")
33	checkLinkOutput("0x"+strings.Repeat("00", maxLen+1), "-B option too long")
34}
35
36func checkLinkOutput(buildid string, message string) {
37	cmd := exec.Command("go", "tool", "link", "-B", buildid, "nonexistent.o")
38	out, err := cmd.CombinedOutput()
39	if err == nil {
40		log.Fatalf("expected cmd/link to fail")
41	}
42
43	firstLine := string(bytes.SplitN(out, []byte("\n"), 2)[0])
44	if strings.HasPrefix(firstLine, "panic") {
45		log.Fatalf("cmd/link panicked:\n%s", out)
46	}
47
48	if !strings.Contains(firstLine, message) {
49		log.Fatalf("%s: cmd/link output did not include expected message %q: %s", buildid, message, firstLine)
50	}
51}
52