github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/link/ld/go_test.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ld 6 7 import ( 8 "os" 9 "path/filepath" 10 "reflect" 11 "runtime" 12 "testing" 13 14 "github.com/go-asm/go/testenv" 15 16 "github.com/go-asm/go/cmd/objabi" 17 ) 18 19 func TestDedupLibraries(t *testing.T) { 20 ctxt := &Link{} 21 ctxt.Target.HeadType = objabi.Hlinux 22 23 libs := []string{"libc.so", "libc.so.6"} 24 25 got := dedupLibraries(ctxt, libs) 26 if !reflect.DeepEqual(got, libs) { 27 t.Errorf("dedupLibraries(%v) = %v, want %v", libs, got, libs) 28 } 29 } 30 31 func TestDedupLibrariesOpenBSD(t *testing.T) { 32 ctxt := &Link{} 33 ctxt.Target.HeadType = objabi.Hopenbsd 34 35 tests := []struct { 36 libs []string 37 want []string 38 }{ 39 { 40 libs: []string{"libc.so"}, 41 want: []string{"libc.so"}, 42 }, 43 { 44 libs: []string{"libc.so", "libc.so.96.1"}, 45 want: []string{"libc.so.96.1"}, 46 }, 47 { 48 libs: []string{"libc.so.96.1", "libc.so"}, 49 want: []string{"libc.so.96.1"}, 50 }, 51 { 52 libs: []string{"libc.a", "libc.so.96.1"}, 53 want: []string{"libc.a", "libc.so.96.1"}, 54 }, 55 { 56 libs: []string{"libpthread.so", "libc.so"}, 57 want: []string{"libc.so", "libpthread.so"}, 58 }, 59 { 60 libs: []string{"libpthread.so.26.1", "libpthread.so", "libc.so.96.1", "libc.so"}, 61 want: []string{"libc.so.96.1", "libpthread.so.26.1"}, 62 }, 63 { 64 libs: []string{"libpthread.so.26.1", "libpthread.so", "libc.so.96.1", "libc.so", "libfoo.so"}, 65 want: []string{"libc.so.96.1", "libfoo.so", "libpthread.so.26.1"}, 66 }, 67 } 68 69 for _, test := range tests { 70 t.Run("dedup", func(t *testing.T) { 71 got := dedupLibraries(ctxt, test.libs) 72 if !reflect.DeepEqual(got, test.want) { 73 t.Errorf("dedupLibraries(%v) = %v, want %v", test.libs, got, test.want) 74 } 75 }) 76 } 77 } 78 79 func TestDedupLibrariesOpenBSDLink(t *testing.T) { 80 // The behavior we're checking for is of interest only on OpenBSD. 81 if runtime.GOOS != "openbsd" { 82 t.Skip("test only useful on openbsd") 83 } 84 85 testenv.MustHaveGoBuild(t) 86 testenv.MustHaveCGO(t) 87 t.Parallel() 88 89 dir := t.TempDir() 90 91 // cgo_import_dynamic both the unversioned libraries and pull in the 92 // net package to get a cgo package with a versioned library. 93 srcFile := filepath.Join(dir, "x.go") 94 src := `package main 95 96 import ( 97 _ "net" 98 ) 99 100 //go:cgo_import_dynamic _ _ "libc.so" 101 102 func main() {}` 103 if err := os.WriteFile(srcFile, []byte(src), 0644); err != nil { 104 t.Fatal(err) 105 } 106 107 exe := filepath.Join(dir, "deduped.exe") 108 out, err := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, srcFile).CombinedOutput() 109 if err != nil { 110 t.Fatalf("build failure: %s\n%s\n", err, string(out)) 111 } 112 113 // Result should be runnable. 114 if _, err = testenv.Command(t, exe).CombinedOutput(); err != nil { 115 t.Fatal(err) 116 } 117 }