github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/internal/xcoff/file_test.go (about)

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