github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/link/link_test.go (about) 1 package main 2 3 import ( 4 "debug/macho" 5 "internal/testenv" 6 "io/ioutil" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "regexp" 11 "strings" 12 "testing" 13 ) 14 15 var AuthorPaidByTheColumnInch struct { 16 fog int ` 17 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. 18 19 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. 20 21 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. 22 23 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.` 24 25 wind int ` 26 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.` 27 28 jarndyce int ` 29 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.` 30 31 principle int ` 32 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.` 33 } 34 35 func TestLargeSymName(t *testing.T) { 36 // The compiler generates a symbol name using the string form of the 37 // type. This tests that the linker can read symbol names larger than 38 // the bufio buffer. Issue #15104. 39 _ = AuthorPaidByTheColumnInch 40 } 41 42 func TestIssue21703(t *testing.T) { 43 testenv.MustHaveGoBuild(t) 44 45 const source = ` 46 package main 47 const X = "\n!\n" 48 func main() {} 49 ` 50 51 tmpdir, err := ioutil.TempDir("", "issue21703") 52 if err != nil { 53 t.Fatalf("failed to create temp dir: %v\n", err) 54 } 55 defer os.RemoveAll(tmpdir) 56 57 err = ioutil.WriteFile(filepath.Join(tmpdir, "main.go"), []byte(source), 0666) 58 if err != nil { 59 t.Fatalf("failed to write main.go: %v\n", err) 60 } 61 62 cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "main.go") 63 cmd.Dir = tmpdir 64 out, err := cmd.CombinedOutput() 65 if err != nil { 66 t.Fatalf("failed to compile main.go: %v, output: %s\n", err, out) 67 } 68 69 cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "main.o") 70 cmd.Dir = tmpdir 71 out, err = cmd.CombinedOutput() 72 if err != nil { 73 t.Fatalf("failed to link main.o: %v, output: %s\n", err, out) 74 } 75 } 76 77 // TestIssue28429 ensures that the linker does not attempt to link 78 // sections not named *.o. Such sections may be used by a build system 79 // to, for example, save facts produced by a modular static analysis 80 // such as golang.org/x/tools/go/analysis. 81 func TestIssue28429(t *testing.T) { 82 testenv.MustHaveGoBuild(t) 83 84 tmpdir, err := ioutil.TempDir("", "issue28429-") 85 if err != nil { 86 t.Fatalf("failed to create temp dir: %v", err) 87 } 88 defer os.RemoveAll(tmpdir) 89 90 write := func(name, content string) { 91 err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) 92 if err != nil { 93 t.Fatal(err) 94 } 95 } 96 97 runGo := func(args ...string) { 98 cmd := exec.Command(testenv.GoToolPath(t), args...) 99 cmd.Dir = tmpdir 100 out, err := cmd.CombinedOutput() 101 if err != nil { 102 t.Fatalf("'go %s' failed: %v, output: %s", 103 strings.Join(args, " "), err, out) 104 } 105 } 106 107 // Compile a main package. 108 write("main.go", "package main; func main() {}") 109 runGo("tool", "compile", "-p", "main", "main.go") 110 runGo("tool", "pack", "c", "main.a", "main.o") 111 112 // Add an extra section with a short, non-.o name. 113 // This simulates an alternative build system. 114 write(".facts", "this is not an object file") 115 runGo("tool", "pack", "r", "main.a", ".facts") 116 117 // Verify that the linker does not attempt 118 // to compile the extra section. 119 runGo("tool", "link", "main.a") 120 } 121 122 func TestUnresolved(t *testing.T) { 123 testenv.MustHaveGoBuild(t) 124 125 tmpdir, err := ioutil.TempDir("", "unresolved-") 126 if err != nil { 127 t.Fatalf("failed to create temp dir: %v", err) 128 } 129 defer os.RemoveAll(tmpdir) 130 131 write := func(name, content string) { 132 err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) 133 if err != nil { 134 t.Fatal(err) 135 } 136 } 137 138 // Test various undefined references. Because of issue #29852, 139 // this used to give confusing error messages because the 140 // linker would find an undefined reference to "zero" created 141 // by the runtime package. 142 143 write("main.go", `package main 144 145 func main() { 146 x() 147 } 148 149 func x() 150 `) 151 write("main.s", ` 152 TEXT ·x(SB),0,$0 153 MOVD zero<>(SB), AX 154 MOVD zero(SB), AX 155 MOVD ·zero(SB), AX 156 RET 157 `) 158 cmd := exec.Command(testenv.GoToolPath(t), "build") 159 cmd.Dir = tmpdir 160 cmd.Env = append(os.Environ(), []string{"GOARCH=amd64", "GOOS=linux"}...) 161 out, err := cmd.CombinedOutput() 162 if err == nil { 163 t.Fatalf("expected build to fail, but it succeeded") 164 } 165 out = regexp.MustCompile("(?m)^#.*\n").ReplaceAll(out, nil) 166 got := string(out) 167 want := `main.x: relocation target zero not defined 168 main.x: relocation target zero not defined 169 main.x: relocation target main.zero not defined 170 ` 171 if want != got { 172 t.Fatalf("want:\n%sgot:\n%s", want, got) 173 } 174 } 175 176 var testMacOSVersionSrc = ` 177 package main 178 func main() { } 179 ` 180 181 func TestMacOSVersion(t *testing.T) { 182 testenv.MustHaveGoBuild(t) 183 184 tmpdir, err := ioutil.TempDir("", "TestMacOSVersion") 185 if err != nil { 186 t.Fatal(err) 187 } 188 defer os.RemoveAll(tmpdir) 189 190 src := filepath.Join(tmpdir, "main.go") 191 err = ioutil.WriteFile(src, []byte(testMacOSVersionSrc), 0666) 192 if err != nil { 193 t.Fatal(err) 194 } 195 196 exe := filepath.Join(tmpdir, "main") 197 cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-linkmode=internal", "-o", exe, src) 198 cmd.Env = append(os.Environ(), 199 "CGO_ENABLED=0", 200 "GOOS=darwin", 201 "GOARCH=amd64", 202 ) 203 if out, err := cmd.CombinedOutput(); err != nil { 204 t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) 205 } 206 exef, err := os.Open(exe) 207 if err != nil { 208 t.Fatal(err) 209 } 210 exem, err := macho.NewFile(exef) 211 if err != nil { 212 t.Fatal(err) 213 } 214 found := false 215 const LC_VERSION_MIN_MACOSX = 0x24 216 checkMin := func(ver uint32) { 217 major, minor := (ver>>16)&0xff, (ver>>8)&0xff 218 if major != 10 || minor < 9 { 219 t.Errorf("LC_VERSION_MIN_MACOSX version %d.%d < 10.9", major, minor) 220 } 221 } 222 for _, cmd := range exem.Loads { 223 raw := cmd.Raw() 224 type_ := exem.ByteOrder.Uint32(raw) 225 if type_ != LC_VERSION_MIN_MACOSX { 226 continue 227 } 228 osVer := exem.ByteOrder.Uint32(raw[8:]) 229 checkMin(osVer) 230 sdkVer := exem.ByteOrder.Uint32(raw[12:]) 231 checkMin(sdkVer) 232 found = true 233 break 234 } 235 if !found { 236 t.Errorf("no LC_VERSION_MIN_MACOSX load command found") 237 } 238 }