Source file src/cmd/link/internal/ld/cgorpath_test.go

     1  // Copyright 2026 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  	"cmd/internal/sys"
     9  	"slices"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestCgoRpathParser(t *testing.T) {
    15  	tests := []struct {
    16  		flags []string
    17  		want  []string
    18  	}{
    19  		{[]string{"-Wl,-rpath,/a"}, []string{"/a"}},
    20  		{[]string{"-Wl,-rpath=/a"}, []string{"/a"}},
    21  		{[]string{"-Wl,--rpath=/a"}, []string{"/a"}},
    22  		{[]string{"-Wl,-rpath,/a,-rpath,/b"}, []string{"/a", "/b"}},
    23  		{[]string{"-Wl,-rpath,/a,--enable-new-dtags"}, []string{"/a"}},
    24  		{[]string{"-Wl,--enable-new-dtags,-rpath,/a"}, []string{"/a"}},
    25  		{[]string{"-Wl,-rpath", "-Wl,/a"}, []string{"/a"}},
    26  		{[]string{"-Wl,-R,/a", "-Wl,-R=/b", "-Wl,-R/c"}, []string{"/a", "/b", "/c"}},
    27  		{[]string{"-Wl,-R", "/a"}, []string{"/a"}},
    28  		{[]string{"-Wl,-rpath,/a", "-lfoo", "-Wl,-rpath=/a", "-Wl,-R,/a"}, []string{"/a"}},
    29  		{[]string{"-Wl,-rpath,/a:/b"}, []string{"/a:/b"}},
    30  		{[]string{"-Wl,-rpath-link,/a", "-Wl,-rpath-link=/b"}, nil},
    31  		{[]string{"-Wl,-rpath", "-lfoo", "-Wl,/a"}, nil},
    32  		{[]string{"-Wl,-rpath,"}, nil},
    33  		{[]string{"-L/a", "-lfoo", "-Wl,--as-needed"}, nil},
    34  	}
    35  	defer func() { cgoRpaths = nil }()
    36  	for _, tt := range tests {
    37  		cgoRpaths = nil
    38  		var p cgoRpathParser
    39  		for _, f := range tt.flags {
    40  			p.parse(f)
    41  		}
    42  		if !slices.Equal(cgoRpaths, tt.want) {
    43  			t.Errorf("parse(%q): cgoRpaths = %q, want %q", tt.flags, cgoRpaths, tt.want)
    44  		}
    45  	}
    46  }
    47  
    48  func TestInternalRpaths(t *testing.T) {
    49  	defer func(r Rpath, c []string) { rpath, cgoRpaths = r, c }(rpath, cgoRpaths)
    50  
    51  	cgoRpaths = []string{"/a", "/b"}
    52  	rpath = Rpath{}
    53  	if got := internalRpaths(); !slices.Equal(got, []string{"/a", "/b"}) {
    54  		t.Errorf("without -r: internalRpaths() = %q, want cgo rpaths", got)
    55  	}
    56  	rpath = Rpath{set: true, val: "/x:/y"}
    57  	if got := internalRpaths(); !slices.Equal(got, []string{"/x", "/y"}) {
    58  		t.Errorf("with -r /x:/y: internalRpaths() = %q, want [/x /y]", got)
    59  	}
    60  	rpath = Rpath{set: true}
    61  	if got := internalRpaths(); len(got) != 0 {
    62  		t.Errorf(`with -r "": internalRpaths() = %q, want none`, got)
    63  	}
    64  }
    65  
    66  // TestMachoAddRpath verifies that the Mach-O header grows to hold the
    67  // LC_RPATH load commands for many or long cgo rpath entries.
    68  func TestMachoAddRpath(t *testing.T) {
    69  	defer func(h int32, ta int64, b int, r Rpath, l []MachoLoad) {
    70  		HEADR, *FlagTextAddr, loadBudget, rpath, load = h, ta, b, r, l
    71  	}(HEADR, *FlagTextAddr, loadBudget, rpath, load)
    72  	rpath = Rpath{}
    73  
    74  	for _, tt := range []struct {
    75  		name string
    76  		dirs []string
    77  	}{
    78  		{"many", slices.Repeat([]string{"/" + strings.Repeat("d", 199)}, 100)},
    79  		{"long", []string{"/" + strings.Repeat("d", 10000)}},
    80  	} {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			HEADR, *FlagTextAddr, loadBudget = INITIAL_MACHO_HEADR, 0, INITIAL_MACHO_HEADR-2*1024
    83  			budget := loadBudget
    84  			var need int
    85  			for _, dir := range tt.dirs {
    86  				machoaddrpath(dir, LinkInternal)
    87  				// The size of the load command written by asmbMacho.
    88  				need += 8 + 4*len(newMachoLoad(sys.ArchARM64, 0, uint32(1+(len(dir)+1+3)/4)).data)
    89  			}
    90  			load = nil
    91  			grow := int(HEADR - INITIAL_MACHO_HEADR)
    92  			if grow == 0 || grow%4096 != 0 || int64(grow) != *FlagTextAddr {
    93  				t.Fatalf("HEADR grew by %d and -T by %d, want the same nonzero multiple of 4096", grow, *FlagTextAddr)
    94  			}
    95  			if loadBudget < 0 || budget+grow-need != loadBudget {
    96  				t.Errorf("loadBudget = %d, want %d (budget %d + growth %d - load commands %d)", loadBudget, budget+grow-need, budget, grow, need)
    97  			}
    98  		})
    99  	}
   100  
   101  	// Nothing is reserved when linking externally or with -r.
   102  	HEADR, loadBudget = INITIAL_MACHO_HEADR, 100
   103  	machoaddrpath("/dir", LinkExternal)
   104  	rpath = Rpath{set: true}
   105  	machoaddrpath("/dir", LinkInternal)
   106  	if HEADR != INITIAL_MACHO_HEADR || loadBudget != 100 {
   107  		t.Errorf("HEADR = %d, loadBudget = %d; want unchanged", HEADR, loadBudget)
   108  	}
   109  }
   110  

View as plain text