github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/link/link_test.go (about) 1 package main 2 3 import ( 4 "internal/testenv" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "regexp" 10 "strings" 11 "testing" 12 ) 13 14 var AuthorPaidByTheColumnInch struct { 15 fog int ` 16 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. 17 18 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. 19 20 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. 21 22 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.` 23 24 wind int ` 25 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.` 26 27 jarndyce int ` 28 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.` 29 30 principle int ` 31 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.` 32 } 33 34 func TestLargeSymName(t *testing.T) { 35 // The compiler generates a symbol name using the string form of the 36 // type. This tests that the linker can read symbol names larger than 37 // the bufio buffer. Issue #15104. 38 _ = AuthorPaidByTheColumnInch 39 } 40 41 func TestIssue21703(t *testing.T) { 42 testenv.MustHaveGoBuild(t) 43 44 const source = ` 45 package main 46 const X = "\n!\n" 47 func main() {} 48 ` 49 50 tmpdir, err := ioutil.TempDir("", "issue21703") 51 if err != nil { 52 t.Fatalf("failed to create temp dir: %v\n", err) 53 } 54 defer os.RemoveAll(tmpdir) 55 56 err = ioutil.WriteFile(filepath.Join(tmpdir, "main.go"), []byte(source), 0666) 57 if err != nil { 58 t.Fatalf("failed to write main.go: %v\n", err) 59 } 60 61 cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "main.go") 62 cmd.Dir = tmpdir 63 out, err := cmd.CombinedOutput() 64 if err != nil { 65 t.Fatalf("failed to compile main.go: %v, output: %s\n", err, out) 66 } 67 68 cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "main.o") 69 cmd.Dir = tmpdir 70 out, err = cmd.CombinedOutput() 71 if err != nil { 72 t.Fatalf("failed to link main.o: %v, output: %s\n", err, out) 73 } 74 } 75 76 // TestIssue28429 ensures that the linker does not attempt to link 77 // sections not named *.o. Such sections may be used by a build system 78 // to, for example, save facts produced by a modular static analysis 79 // such as golang.org/x/tools/go/analysis. 80 func TestIssue28429(t *testing.T) { 81 testenv.MustHaveGoBuild(t) 82 83 tmpdir, err := ioutil.TempDir("", "issue28429-") 84 if err != nil { 85 t.Fatalf("failed to create temp dir: %v", err) 86 } 87 defer os.RemoveAll(tmpdir) 88 89 write := func(name, content string) { 90 err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) 91 if err != nil { 92 t.Fatal(err) 93 } 94 } 95 96 runGo := func(args ...string) { 97 cmd := exec.Command(testenv.GoToolPath(t), args...) 98 cmd.Dir = tmpdir 99 out, err := cmd.CombinedOutput() 100 if err != nil { 101 t.Fatalf("'go %s' failed: %v, output: %s", 102 strings.Join(args, " "), err, out) 103 } 104 } 105 106 // Compile a main package. 107 write("main.go", "package main; func main() {}") 108 runGo("tool", "compile", "-p", "main", "main.go") 109 runGo("tool", "pack", "c", "main.a", "main.o") 110 111 // Add an extra section with a short, non-.o name. 112 // This simulates an alternative build system. 113 write(".facts", "this is not an object file") 114 runGo("tool", "pack", "r", "main.a", ".facts") 115 116 // Verify that the linker does not attempt 117 // to compile the extra section. 118 runGo("tool", "link", "main.a") 119 } 120 121 func TestUnresolved(t *testing.T) { 122 testenv.MustHaveGoBuild(t) 123 124 tmpdir, err := ioutil.TempDir("", "unresolved-") 125 if err != nil { 126 t.Fatalf("failed to create temp dir: %v", err) 127 } 128 defer os.RemoveAll(tmpdir) 129 130 write := func(name, content string) { 131 err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) 132 if err != nil { 133 t.Fatal(err) 134 } 135 } 136 137 // Test various undefined references. Because of issue #29852, 138 // this used to give confusing error messages because the 139 // linker would find an undefined reference to "zero" created 140 // by the runtime package. 141 142 write("main.go", `package main 143 144 func main() { 145 x() 146 } 147 148 func x() 149 `) 150 write("main.s", ` 151 TEXT ·x(SB),0,$0 152 MOVD zero<>(SB), AX 153 MOVD zero(SB), AX 154 MOVD ·zero(SB), AX 155 RET 156 `) 157 cmd := exec.Command(testenv.GoToolPath(t), "build") 158 cmd.Dir = tmpdir 159 cmd.Env = append(os.Environ(), []string{"GOARCH=amd64", "GOOS=linux"}...) 160 out, err := cmd.CombinedOutput() 161 if err == nil { 162 t.Fatalf("expected build to fail, but it succeeded") 163 } 164 out = regexp.MustCompile("(?m)^#.*\n").ReplaceAll(out, nil) 165 got := string(out) 166 want := `main.x: relocation target zero not defined 167 main.x: relocation target zero not defined 168 main.x: relocation target main.zero not defined 169 ` 170 if want != got { 171 t.Fatalf("want:\n%sgot:\n%s", want, got) 172 } 173 }