github.com/filosottile/go@v0.0.0-20170906193555-dbed9972d994/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 "testdata/clang-386-darwin-exec-with-rpath", 101 FileHeader{0xfeedface, Cpu386, 0x3, 0x2, 0x10, 0x42c, 0x1200085}, 102 []interface{}{ 103 nil, // LC_SEGMENT 104 nil, // LC_SEGMENT 105 nil, // LC_SEGMENT 106 nil, // LC_SEGMENT 107 nil, // LC_DYLD_INFO_ONLY 108 nil, // LC_SYMTAB 109 nil, // LC_DYSYMTAB 110 nil, // LC_LOAD_DYLINKER 111 nil, // LC_UUID 112 nil, // LC_VERSION_MIN_MACOSX 113 nil, // LC_SOURCE_VERSION 114 nil, // LC_MAIN 115 nil, // LC_LOAD_DYLIB 116 &Rpath{nil, "/my/rpath"}, 117 nil, // LC_FUNCTION_STARTS 118 nil, // LC_DATA_IN_CODE 119 }, 120 nil, 121 }, 122 { 123 "testdata/clang-amd64-darwin-exec-with-rpath", 124 FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0x2, 0x10, 0x4c8, 0x200085}, 125 []interface{}{ 126 nil, // LC_SEGMENT 127 nil, // LC_SEGMENT 128 nil, // LC_SEGMENT 129 nil, // LC_SEGMENT 130 nil, // LC_DYLD_INFO_ONLY 131 nil, // LC_SYMTAB 132 nil, // LC_DYSYMTAB 133 nil, // LC_LOAD_DYLINKER 134 nil, // LC_UUID 135 nil, // LC_VERSION_MIN_MACOSX 136 nil, // LC_SOURCE_VERSION 137 nil, // LC_MAIN 138 nil, // LC_LOAD_DYLIB 139 &Rpath{nil, "/my/rpath"}, 140 nil, // LC_FUNCTION_STARTS 141 nil, // LC_DATA_IN_CODE 142 }, 143 nil, 144 }, 145 } 146 147 func TestOpen(t *testing.T) { 148 for i := range fileTests { 149 tt := &fileTests[i] 150 151 f, err := Open(tt.file) 152 if err != nil { 153 t.Error(err) 154 continue 155 } 156 if !reflect.DeepEqual(f.FileHeader, tt.hdr) { 157 t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr) 158 continue 159 } 160 for i, l := range f.Loads { 161 if i >= len(tt.loads) { 162 break 163 } 164 165 want := tt.loads[i] 166 if want == nil { 167 continue 168 } 169 170 switch l := l.(type) { 171 case *Segment: 172 have := &l.SegmentHeader 173 if !reflect.DeepEqual(have, want) { 174 t.Errorf("open %s, segment %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 175 } 176 case *Dylib: 177 have := l 178 have.LoadBytes = nil 179 if !reflect.DeepEqual(have, want) { 180 t.Errorf("open %s, segment %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 181 } 182 case *Rpath: 183 have := l 184 have.LoadBytes = nil 185 if !reflect.DeepEqual(have, want) { 186 t.Errorf("open %s, segment %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 187 } 188 default: 189 t.Errorf("open %s, section %d: unknown load command\n\thave %#v\n\twant %#v\n", tt.file, i, l, want) 190 } 191 } 192 tn := len(tt.loads) 193 fn := len(f.Loads) 194 if tn != fn { 195 t.Errorf("open %s: len(Loads) = %d, want %d", tt.file, fn, tn) 196 } 197 198 if tt.sections != nil { 199 for i, sh := range f.Sections { 200 if i >= len(tt.sections) { 201 break 202 } 203 have := &sh.SectionHeader 204 want := tt.sections[i] 205 if !reflect.DeepEqual(have, want) { 206 t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) 207 } 208 } 209 tn = len(tt.sections) 210 fn = len(f.Sections) 211 if tn != fn { 212 t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn) 213 } 214 } 215 } 216 } 217 218 func TestOpenFailure(t *testing.T) { 219 filename := "file.go" // not a Mach-O file 220 _, err := Open(filename) // don't crash 221 if err == nil { 222 t.Errorf("open %s: succeeded unexpectedly", filename) 223 } 224 } 225 226 func TestOpenFat(t *testing.T) { 227 ff, err := OpenFat("testdata/fat-gcc-386-amd64-darwin-exec") 228 if err != nil { 229 t.Fatal(err) 230 } 231 232 if ff.Magic != MagicFat { 233 t.Errorf("OpenFat: got magic number %#x, want %#x", ff.Magic, MagicFat) 234 } 235 if len(ff.Arches) != 2 { 236 t.Errorf("OpenFat: got %d architectures, want 2", len(ff.Arches)) 237 } 238 239 for i := range ff.Arches { 240 arch := &ff.Arches[i] 241 ftArch := &fileTests[i] 242 243 if arch.Cpu != ftArch.hdr.Cpu || arch.SubCpu != ftArch.hdr.SubCpu { 244 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) 245 } 246 247 if !reflect.DeepEqual(arch.FileHeader, ftArch.hdr) { 248 t.Errorf("OpenFat header:\n\tgot %#v\n\twant %#v\n", arch.FileHeader, ftArch.hdr) 249 } 250 } 251 } 252 253 func TestOpenFatFailure(t *testing.T) { 254 filename := "file.go" // not a Mach-O file 255 if _, err := OpenFat(filename); err == nil { 256 t.Errorf("OpenFat %s: succeeded unexpectedly", filename) 257 } 258 259 filename = "testdata/gcc-386-darwin-exec" // not a fat Mach-O 260 ff, err := OpenFat(filename) 261 if err != ErrNotFat { 262 t.Errorf("OpenFat %s: got %v, want ErrNotFat", filename, err) 263 } 264 if ff != nil { 265 t.Errorf("OpenFat %s: got %v, want nil", filename, ff) 266 } 267 } 268 269 func TestRelocTypeString(t *testing.T) { 270 if X86_64_RELOC_BRANCH.String() != "X86_64_RELOC_BRANCH" { 271 t.Errorf("got %v, want %v", X86_64_RELOC_BRANCH.String(), "X86_64_RELOC_BRANCH") 272 } 273 if X86_64_RELOC_BRANCH.GoString() != "macho.X86_64_RELOC_BRANCH" { 274 t.Errorf("got %v, want %v", X86_64_RELOC_BRANCH.GoString(), "macho.X86_64_RELOC_BRANCH") 275 } 276 } 277 278 func TestTypeString(t *testing.T) { 279 if TypeExec.String() != "Exec" { 280 t.Errorf("got %v, want %v", TypeExec.String(), "Exec") 281 } 282 if TypeExec.GoString() != "macho.Exec" { 283 t.Errorf("got %v, want %v", TypeExec.GoString(), "macho.Exec") 284 } 285 }