Source file src/cmd/internal/objfile/macho_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 objfile
     6  
     7  import (
     8  	"debug/macho"
     9  	"testing"
    10  )
    11  
    12  func TestMachoSymbolsUseSectionEndForLastTextSymbol(t *testing.T) {
    13  	f := &machoFile{macho: &macho.File{
    14  		Symtab: &macho.Symtab{Syms: []macho.Symbol{
    15  			{Name: "_f", Sect: 1, Value: 0x1000},
    16  			{Name: "_g", Sect: 1, Value: 0x1010},
    17  		}},
    18  		Sections: []*macho.Section{
    19  			{SectionHeader: macho.SectionHeader{Name: "__text", Seg: "__TEXT", Addr: 0x1000, Size: 0x30}},
    20  		},
    21  	}}
    22  
    23  	syms, err := f.symbols()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	got := map[string]Sym{}
    29  	for _, sym := range syms {
    30  		got[sym.Name] = sym
    31  	}
    32  
    33  	if got["_f"].Size != 0x10 {
    34  		t.Fatalf("_f size = %#x, want %#x", got["_f"].Size, int64(0x10))
    35  	}
    36  	if got["_g"].Size != 0x20 {
    37  		t.Fatalf("_g size = %#x, want %#x", got["_g"].Size, int64(0x20))
    38  	}
    39  	if got["_g"].Code != 'T' {
    40  		t.Fatalf("_g code = %q, want %q", got["_g"].Code, 'T')
    41  	}
    42  }
    43  
    44  func TestMachoSymbolsDoNotCrossSectionBoundaries(t *testing.T) {
    45  	f := &machoFile{macho: &macho.File{
    46  		Symtab: &macho.Symtab{Syms: []macho.Symbol{
    47  			{Name: "_f", Sect: 1, Value: 0x1000},
    48  			{Name: "_g", Sect: 1, Value: 0x1010},
    49  			{Name: "_data", Sect: 2, Value: 0x2000},
    50  		}},
    51  		Sections: []*macho.Section{
    52  			{SectionHeader: macho.SectionHeader{Name: "__text", Seg: "__TEXT", Addr: 0x1000, Size: 0x30}},
    53  			{SectionHeader: macho.SectionHeader{Name: "__data", Seg: "__DATA", Addr: 0x2000, Size: 0x10}},
    54  		},
    55  	}}
    56  
    57  	syms, err := f.symbols()
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	got := map[string]Sym{}
    63  	for _, sym := range syms {
    64  		got[sym.Name] = sym
    65  	}
    66  
    67  	if got["_g"].Size != 0x20 {
    68  		t.Fatalf("_g size = %#x, want %#x", got["_g"].Size, int64(0x20))
    69  	}
    70  	if got["_data"].Size != 0x10 {
    71  		t.Fatalf("_data size = %#x, want %#x", got["_data"].Size, int64(0x10))
    72  	}
    73  }
    74  

View as plain text