Source file src/internal/xcoff/file_test.go

     1  // Copyright 2018 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 xcoff
     6  
     7  import (
     8  	"reflect"
     9  	"slices"
    10  	"testing"
    11  )
    12  
    13  type fileTest struct {
    14  	file     string
    15  	hdr      FileHeader
    16  	sections []*SectionHeader
    17  	needed   []string
    18  }
    19  
    20  var fileTests = []fileTest{
    21  	{
    22  		"testdata/gcc-ppc32-aix-dwarf2-exec",
    23  		FileHeader{U802TOCMAGIC},
    24  		[]*SectionHeader{
    25  			{".text", 0x10000290, 0x00000bbd, STYP_TEXT, 0x7ae6, 0x36},
    26  			{".data", 0x20000e4d, 0x00000437, STYP_DATA, 0x7d02, 0x2b},
    27  			{".bss", 0x20001284, 0x0000021c, STYP_BSS, 0, 0},
    28  			{".loader", 0x00000000, 0x000004b3, STYP_LOADER, 0, 0},
    29  			{".dwline", 0x00000000, 0x000000df, STYP_DWARF | SSUBTYP_DWLINE, 0x7eb0, 0x7},
    30  			{".dwinfo", 0x00000000, 0x00000314, STYP_DWARF | SSUBTYP_DWINFO, 0x7ef6, 0xa},
    31  			{".dwabrev", 0x00000000, 0x000000d6, STYP_DWARF | SSUBTYP_DWABREV, 0, 0},
    32  			{".dwarnge", 0x00000000, 0x00000020, STYP_DWARF | SSUBTYP_DWARNGE, 0x7f5a, 0x2},
    33  			{".dwloc", 0x00000000, 0x00000074, STYP_DWARF | SSUBTYP_DWLOC, 0, 0},
    34  			{".debug", 0x00000000, 0x00005e4f, STYP_DEBUG, 0, 0},
    35  		},
    36  		[]string{"libc.a/shr.o"},
    37  	},
    38  	{
    39  		"testdata/gcc-ppc64-aix-dwarf2-exec",
    40  		FileHeader{U64_TOCMAGIC},
    41  		[]*SectionHeader{
    42  			{".text", 0x10000480, 0x00000afd, STYP_TEXT, 0x8322, 0x34},
    43  			{".data", 0x20000f7d, 0x000002f3, STYP_DATA, 0x85fa, 0x25},
    44  			{".bss", 0x20001270, 0x00000428, STYP_BSS, 0, 0},
    45  			{".loader", 0x00000000, 0x00000535, STYP_LOADER, 0, 0},
    46  			{".dwline", 0x00000000, 0x000000b4, STYP_DWARF | SSUBTYP_DWLINE, 0x8800, 0x4},
    47  			{".dwinfo", 0x00000000, 0x0000036a, STYP_DWARF | SSUBTYP_DWINFO, 0x8838, 0x7},
    48  			{".dwabrev", 0x00000000, 0x000000b5, STYP_DWARF | SSUBTYP_DWABREV, 0, 0},
    49  			{".dwarnge", 0x00000000, 0x00000040, STYP_DWARF | SSUBTYP_DWARNGE, 0x889a, 0x2},
    50  			{".dwloc", 0x00000000, 0x00000062, STYP_DWARF | SSUBTYP_DWLOC, 0, 0},
    51  			{".debug", 0x00000000, 0x00006605, STYP_DEBUG, 0, 0},
    52  		},
    53  		[]string{"libc.a/shr_64.o"},
    54  	},
    55  }
    56  
    57  func TestOpen(t *testing.T) {
    58  	for i := range fileTests {
    59  		tt := &fileTests[i]
    60  
    61  		f, err := Open(tt.file)
    62  		if err != nil {
    63  			t.Error(err)
    64  			continue
    65  		}
    66  		if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
    67  			t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
    68  			continue
    69  		}
    70  
    71  		for i, sh := range f.Sections {
    72  			if i >= len(tt.sections) {
    73  				break
    74  			}
    75  			have := &sh.SectionHeader
    76  			want := tt.sections[i]
    77  			if !reflect.DeepEqual(have, want) {
    78  				t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
    79  			}
    80  		}
    81  		tn := len(tt.sections)
    82  		fn := len(f.Sections)
    83  		if tn != fn {
    84  			t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
    85  		}
    86  		tl := tt.needed
    87  		fl, err := f.ImportedLibraries()
    88  		if err != nil {
    89  			t.Error(err)
    90  		}
    91  		if !slices.Equal(tl, fl) {
    92  			t.Errorf("open %s: loader import = %v, want %v", tt.file, tl, fl)
    93  		}
    94  	}
    95  }
    96  
    97  func TestOpenFailure(t *testing.T) {
    98  	filename := "file.go"    // not an XCOFF object file
    99  	_, err := Open(filename) // don't crash
   100  	if err == nil {
   101  		t.Errorf("open %s: succeeded unexpectedly", filename)
   102  	}
   103  }
   104  

View as plain text