github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/debug/macho/file_test.go (about) 1 // Copyright 2009 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 macho 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 type fileTest struct { 13 file string 14 hdr FileHeader 15 loads []interface{} 16 sections []*SectionHeader 17 } 18 19 var fileTests = []fileTest{ 20 { 21 "testdata/gcc-386-darwin-exec", 22 FileHeader{0xfeedface, Cpu386, 0x3, 0x2, 0xc, 0x3c0, 0x85}, 23 []interface{}{ 24 &SegmentHeader{LoadCmdSegment, 0x38, "__PAGEZERO", 0x0, 0x1000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 25 &SegmentHeader{LoadCmdSegment, 0xc0, "__TEXT", 0x1000, 0x1000, 0x0, 0x1000, 0x7, 0x5, 0x2, 0x0}, 26 &SegmentHeader{LoadCmdSegment, 0xc0, "__DATA", 0x2000, 0x1000, 0x1000, 0x1000, 0x7, 0x3, 0x2, 0x0}, 27 &SegmentHeader{LoadCmdSegment, 0x7c, "__IMPORT", 0x3000, 0x1000, 0x2000, 0x1000, 0x7, 0x7, 0x1, 0x0}, 28 &SegmentHeader{LoadCmdSegment, 0x38, "__LINKEDIT", 0x4000, 0x1000, 0x3000, 0x12c, 0x7, 0x1, 0x0, 0x0}, 29 nil, // LC_SYMTAB 30 nil, // LC_DYSYMTAB 31 nil, // LC_LOAD_DYLINKER 32 nil, // LC_UUID 33 nil, // LC_UNIXTHREAD 34 &Dylib{nil, "/usr/lib/libgcc_s.1.dylib", 0x2, 0x10000, 0x10000}, 35 &Dylib{nil, "/usr/lib/libSystem.B.dylib", 0x2, 0x6f0104, 0x10000}, 36 }, 37 []*SectionHeader{ 38 {"__text", "__TEXT", 0x1f68, 0x88, 0xf68, 0x2, 0x0, 0x0, 0x80000400}, 39 {"__cstring", "__TEXT", 0x1ff0, 0xd, 0xff0, 0x0, 0x0, 0x0, 0x2}, 40 {"__data", "__DATA", 0x2000, 0x14, 0x1000, 0x2, 0x0, 0x0, 0x0}, 41 {"__dyld", "__DATA", 0x2014, 0x1c, 0x1014, 0x2, 0x0, 0x0, 0x0}, 42 {"__jump_table", "__IMPORT", 0x3000, 0xa, 0x2000, 0x6, 0x0, 0x0, 0x4000008}, 43 }, 44 }, 45 { 46 "testdata/gcc-amd64-darwin-exec", 47 FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0x2, 0xb, 0x568, 0x85}, 48 []interface{}{ 49 &SegmentHeader{LoadCmdSegment64, 0x48, "__PAGEZERO", 0x0, 0x100000000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 50 &SegmentHeader{LoadCmdSegment64, 0x1d8, "__TEXT", 0x100000000, 0x1000, 0x0, 0x1000, 0x7, 0x5, 0x5, 0x0}, 51 &SegmentHeader{LoadCmdSegment64, 0x138, "__DATA", 0x100001000, 0x1000, 0x1000, 0x1000, 0x7, 0x3, 0x3, 0x0}, 52 &SegmentHeader{LoadCmdSegment64, 0x48, "__LINKEDIT", 0x100002000, 0x1000, 0x2000, 0x140, 0x7, 0x1, 0x0, 0x0}, 53 nil, // LC_SYMTAB 54 nil, // LC_DYSYMTAB 55 nil, // LC_LOAD_DYLINKER 56 nil, // LC_UUID 57 nil, // LC_UNIXTHREAD 58 &Dylib{nil, "/usr/lib/libgcc_s.1.dylib", 0x2, 0x10000, 0x10000}, 59 &Dylib{nil, "/usr/lib/libSystem.B.dylib", 0x2, 0x6f0104, 0x10000}, 60 }, 61 []*SectionHeader{ 62 {"__text", "__TEXT", 0x100000f14, 0x6d, 0xf14, 0x2, 0x0, 0x0, 0x80000400}, 63 {"__symbol_stub1", "__TEXT", 0x100000f81, 0xc, 0xf81, 0x0, 0x0, 0x0, 0x80000408}, 64 {"__stub_helper", "__TEXT", 0x100000f90, 0x18, 0xf90, 0x2, 0x0, 0x0, 0x0}, 65 {"__cstring", "__TEXT", 0x100000fa8, 0xd, 0xfa8, 0x0, 0x0, 0x0, 0x2}, 66 {"__eh_frame", "__TEXT", 0x100000fb8, 0x48, 0xfb8, 0x3, 0x0, 0x0, 0x6000000b}, 67 {"__data", "__DATA", 0x100001000, 0x1c, 0x1000, 0x3, 0x0, 0x0, 0x0}, 68 {"__dyld", "__DATA", 0x100001020, 0x38, 0x1020, 0x3, 0x0, 0x0, 0x0}, 69 {"__la_symbol_ptr", "__DATA", 0x100001058, 0x10, 0x1058, 0x2, 0x0, 0x0, 0x7}, 70 }, 71 }, 72 { 73 "testdata/gcc-amd64-darwin-exec-debug", 74 FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0xa, 0x4, 0x5a0, 0}, 75 []interface{}{ 76 nil, // LC_UUID 77 &SegmentHeader{LoadCmdSegment64, 0x1d8, "__TEXT", 0x100000000, 0x1000, 0x0, 0x0, 0x7, 0x5, 0x5, 0x0}, 78 &SegmentHeader{LoadCmdSegment64, 0x138, "__DATA", 0x100001000, 0x1000, 0x0, 0x0, 0x7, 0x3, 0x3, 0x0}, 79 &SegmentHeader{LoadCmdSegment64, 0x278, "__DWARF", 0x100002000, 0x1000, 0x1000, 0x1bc, 0x7, 0x3, 0x7, 0x0}, 80 }, 81 []*SectionHeader{ 82 {"__text", "__TEXT", 0x100000f14, 0x0, 0x0, 0x2, 0x0, 0x0, 0x80000400}, 83 {"__symbol_stub1", "__TEXT", 0x100000f81, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80000408}, 84 {"__stub_helper", "__TEXT", 0x100000f90, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0}, 85 {"__cstring", "__TEXT", 0x100000fa8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2}, 86 {"__eh_frame", "__TEXT", 0x100000fb8, 0x0, 0x0, 0x3, 0x0, 0x0, 0x6000000b}, 87 {"__data", "__DATA", 0x100001000, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0}, 88 {"__dyld", "__DATA", 0x100001020, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0}, 89 {"__la_symbol_ptr", "__DATA", 0x100001058, 0x0, 0x0, 0x2, 0x0, 0x0, 0x7}, 90 {"__debug_abbrev", "__DWARF", 0x100002000, 0x36, 0x1000, 0x0, 0x0, 0x0, 0x0}, 91 {"__debug_aranges", "__DWARF", 0x100002036, 0x30, 0x1036, 0x0, 0x0, 0x0, 0x0}, 92 {"__debug_frame", "__DWARF", 0x100002066, 0x40, 0x1066, 0x0, 0x0, 0x0, 0x0}, 93 {"__debug_info", "__DWARF", 0x1000020a6, 0x54, 0x10a6, 0x0, 0x0, 0x0, 0x0}, 94 {"__debug_line", "__DWARF", 0x1000020fa, 0x47, 0x10fa, 0x0, 0x0, 0x0, 0x0}, 95 {"__debug_pubnames", "__DWARF", 0x100002141, 0x1b, 0x1141, 0x0, 0x0, 0x0, 0x0}, 96 {"__debug_str", "__DWARF", 0x10000215c, 0x60, 0x115c, 0x0, 0x0, 0x0, 0x0}, 97 }, 98 }, 99 } 100 101 func TestOpen(t *testing.T) { 102 for i := range fileTests { 103 tt := &fileTests[i] 104 105 f, err := Open(tt.file) 106 if err != nil { 107 t.Error(err) 108 continue 109 } 110 if !reflect.DeepEqual(f.FileHeader, tt.hdr) { 111 t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr) 112 continue 113 } 114 for i, l := range f.Loads { 115 if i >= len(tt.loads) { 116 break 117 } 118 119 want := tt.loads[i] 120 if want == nil { 121 continue 122 } 123 124 switch l := l.(type) { 125 case *Segment: 126 have := &l.SegmentHeader 127 if !reflect.DeepEqual(have, want) { 128 t.Errorf("open %s, segment %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 129 } 130 case *Dylib: 131 have := l 132 have.LoadBytes = nil 133 if !reflect.DeepEqual(have, want) { 134 t.Errorf("open %s, segment %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 135 } 136 default: 137 t.Errorf("open %s, section %d: unknown load command\n\thave %#v\n\twant %#v\n", tt.file, i, l, want) 138 } 139 } 140 tn := len(tt.loads) 141 fn := len(f.Loads) 142 if tn != fn { 143 t.Errorf("open %s: len(Loads) = %d, want %d", tt.file, fn, tn) 144 } 145 146 for i, sh := range f.Sections { 147 if i >= len(tt.sections) { 148 break 149 } 150 have := &sh.SectionHeader 151 want := tt.sections[i] 152 if !reflect.DeepEqual(have, want) { 153 t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 154 } 155 } 156 tn = len(tt.sections) 157 fn = len(f.Sections) 158 if tn != fn { 159 t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn) 160 } 161 162 } 163 } 164 165 func TestOpenFailure(t *testing.T) { 166 filename := "file.go" // not a Mach-O file 167 _, err := Open(filename) // don't crash 168 if err == nil { 169 t.Errorf("open %s: succeeded unexpectedly", filename) 170 } 171 } 172 173 func TestOpenFat(t *testing.T) { 174 ff, err := OpenFat("testdata/fat-gcc-386-amd64-darwin-exec") 175 if err != nil { 176 t.Fatal(err) 177 } 178 179 if ff.Magic != MagicFat { 180 t.Errorf("OpenFat: got magic number %#x, want %#x", ff.Magic, MagicFat) 181 } 182 if len(ff.Arches) != 2 { 183 t.Errorf("OpenFat: got %d architectures, want 2", len(ff.Arches)) 184 } 185 186 for i := range ff.Arches { 187 arch := &ff.Arches[i] 188 ftArch := &fileTests[i] 189 190 if arch.Cpu != ftArch.hdr.Cpu || arch.SubCpu != ftArch.hdr.SubCpu { 191 t.Errorf("OpenFat: architecture #%d got cpu=%#x subtype=%#x, expected cpu=%#x, subtype=%#x", i, arch.Cpu, arch.SubCpu, ftArch.hdr.Cpu, ftArch.hdr.SubCpu) 192 } 193 194 if !reflect.DeepEqual(arch.FileHeader, ftArch.hdr) { 195 t.Errorf("OpenFat header:\n\tgot %#v\n\twant %#v\n", arch.FileHeader, ftArch.hdr) 196 } 197 } 198 } 199 200 func TestOpenFatFailure(t *testing.T) { 201 filename := "file.go" // not a Mach-O file 202 if _, err := OpenFat(filename); err == nil { 203 t.Errorf("OpenFat %s: succeeded unexpectedly", filename) 204 } 205 206 filename = "testdata/gcc-386-darwin-exec" // not a fat Mach-O 207 ff, err := OpenFat(filename) 208 if err != ErrNotFat { 209 t.Errorf("OpenFat %s: got %v, want ErrNotFat", filename, err) 210 } 211 if ff != nil { 212 t.Errorf("OpenFat %s: got %v, want nil", filename, ff) 213 } 214 } 215 216 func TestRelocTypeString(t *testing.T) { 217 if X86_64_RELOC_BRANCH.String() != "X86_64_RELOC_BRANCH" { 218 t.Errorf("got %v, want %v", X86_64_RELOC_BRANCH.String(), "X86_64_RELOC_BRANCH") 219 } 220 if X86_64_RELOC_BRANCH.GoString() != "macho.X86_64_RELOC_BRANCH" { 221 t.Errorf("got %v, want %v", X86_64_RELOC_BRANCH.GoString(), "macho.X86_64_RELOC_BRANCH") 222 } 223 } 224 225 func TestTypeString(t *testing.T) { 226 if TypeExec.String() != "Exec" { 227 t.Errorf("got %v, want %v", TypeExec.String(), "Exec") 228 } 229 if TypeExec.GoString() != "macho.Exec" { 230 t.Errorf("got %v, want %v", TypeExec.GoString(), "macho.Exec") 231 } 232 }