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.
12 type fileTest struct {
16 sections []*SectionHeader
20 var fileTests = []fileTest{
22 "testdata/gcc-386-mingw-obj",
23 FileHeader{0x014c, 0x000c, 0x0, 0x64a, 0x1e, 0x0, 0x104},
26 {".text", 0, 0, 36, 500, 1440, 0, 3, 0, 0x60300020},
27 {".data", 0, 0, 0, 0, 0, 0, 0, 0, 3224371264},
28 {".bss", 0, 0, 0, 0, 0, 0, 0, 0, 3224371328},
29 {".debug_abbrev", 0, 0, 137, 536, 0, 0, 0, 0, 0x42100000},
30 {".debug_info", 0, 0, 418, 673, 1470, 0, 7, 0, 1108344832},
31 {".debug_line", 0, 0, 128, 1091, 1540, 0, 1, 0, 1108344832},
32 {".rdata", 0, 0, 16, 1219, 0, 0, 0, 0, 1076887616},
33 {".debug_frame", 0, 0, 52, 1235, 1550, 0, 2, 0, 1110441984},
34 {".debug_loc", 0, 0, 56, 1287, 0, 0, 0, 0, 1108344832},
35 {".debug_pubnames", 0, 0, 27, 1343, 1570, 0, 1, 0, 1108344832},
36 {".debug_pubtypes", 0, 0, 38, 1370, 1580, 0, 1, 0, 1108344832},
37 {".debug_aranges", 0, 0, 32, 1408, 1590, 0, 2, 0, 1108344832},
40 {".file", 0x0, -2, 0x0, 0x67},
41 {"_main", 0x0, 1, 0x20, 0x2},
42 {".text", 0x0, 1, 0x0, 0x3},
43 {".data", 0x0, 2, 0x0, 0x3},
44 {".bss", 0x0, 3, 0x0, 0x3},
45 {".debug_abbrev", 0x0, 4, 0x0, 0x3},
46 {".debug_info", 0x0, 5, 0x0, 0x3},
47 {".debug_line", 0x0, 6, 0x0, 0x3},
48 {".rdata", 0x0, 7, 0x0, 0x3},
49 {".debug_frame", 0x0, 8, 0x0, 0x3},
50 {".debug_loc", 0x0, 9, 0x0, 0x3},
51 {".debug_pubnames", 0x0, 10, 0x0, 0x3},
52 {".debug_pubtypes", 0x0, 11, 0x0, 0x3},
53 {".debug_aranges", 0x0, 12, 0x0, 0x3},
54 {"___main", 0x0, 0, 0x20, 0x2},
55 {"_puts", 0x0, 0, 0x20, 0x2},
59 "testdata/gcc-386-mingw-exec",
60 FileHeader{0x014c, 0x000f, 0x4c6a1b60, 0x3c00, 0x282, 0xe0, 0x107},
62 0x10b, 0x2, 0x38, 0xe00, 0x1a00, 0x200, 0x1160, 0x1000, 0x2000, 0x400000, 0x1000, 0x200, 0x4, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x10000, 0x400, 0x14abb, 0x3, 0x0, 0x200000, 0x1000, 0x100000, 0x1000, 0x0, 0x10,
83 {".text", 0xcd8, 0x1000, 0xe00, 0x400, 0x0, 0x0, 0x0, 0x0, 0x60500060},
84 {".data", 0x10, 0x2000, 0x200, 0x1200, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
85 {".rdata", 0x120, 0x3000, 0x200, 0x1400, 0x0, 0x0, 0x0, 0x0, 0x40300040},
86 {".bss", 0xdc, 0x4000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0400080},
87 {".idata", 0x3c8, 0x5000, 0x400, 0x1600, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
88 {".CRT", 0x18, 0x6000, 0x200, 0x1a00, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
89 {".tls", 0x20, 0x7000, 0x200, 0x1c00, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
90 {".debug_aranges", 0x20, 0x8000, 0x200, 0x1e00, 0x0, 0x0, 0x0, 0x0, 0x42100000},
91 {".debug_pubnames", 0x51, 0x9000, 0x200, 0x2000, 0x0, 0x0, 0x0, 0x0, 0x42100000},
92 {".debug_pubtypes", 0x91, 0xa000, 0x200, 0x2200, 0x0, 0x0, 0x0, 0x0, 0x42100000},
93 {".debug_info", 0xe22, 0xb000, 0x1000, 0x2400, 0x0, 0x0, 0x0, 0x0, 0x42100000},
94 {".debug_abbrev", 0x157, 0xc000, 0x200, 0x3400, 0x0, 0x0, 0x0, 0x0, 0x42100000},
95 {".debug_line", 0x144, 0xd000, 0x200, 0x3600, 0x0, 0x0, 0x0, 0x0, 0x42100000},
96 {".debug_frame", 0x34, 0xe000, 0x200, 0x3800, 0x0, 0x0, 0x0, 0x0, 0x42300000},
97 {".debug_loc", 0x38, 0xf000, 0x200, 0x3a00, 0x0, 0x0, 0x0, 0x0, 0x42100000},
102 "testdata/gcc-amd64-mingw-obj",
103 FileHeader{0x8664, 0x6, 0x0, 0x198, 0x12, 0x0, 0x4},
106 {".text", 0x0, 0x0, 0x30, 0x104, 0x15c, 0x0, 0x3, 0x0, 0x60500020},
107 {".data", 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0500040},
108 {".bss", 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0500080},
109 {".rdata", 0x0, 0x0, 0x10, 0x134, 0x0, 0x0, 0x0, 0x0, 0x40500040},
110 {".xdata", 0x0, 0x0, 0xc, 0x144, 0x0, 0x0, 0x0, 0x0, 0x40300040},
111 {".pdata", 0x0, 0x0, 0xc, 0x150, 0x17a, 0x0, 0x3, 0x0, 0x40300040},
114 {".file", 0x0, -2, 0x0, 0x67},
115 {"main", 0x0, 1, 0x20, 0x2},
116 {".text", 0x0, 1, 0x0, 0x3},
117 {".data", 0x0, 2, 0x0, 0x3},
118 {".bss", 0x0, 3, 0x0, 0x3},
119 {".rdata", 0x0, 4, 0x0, 0x3},
120 {".xdata", 0x0, 5, 0x0, 0x3},
121 {".pdata", 0x0, 6, 0x0, 0x3},
122 {"__main", 0x0, 0, 0x20, 0x2},
123 {"puts", 0x0, 0, 0x20, 0x2},
127 "testdata/gcc-amd64-mingw-exec",
128 FileHeader{0x8664, 0x9, 0x53472993, 0x0, 0x0, 0xf0, 0x22f},
130 0x20b, 0x2, 0x16, 0x6a00, 0x2400, 0x1600, 0x14e0, 0x1000, 0x400000, 0x1000, 0x200, 0x4, 0x0, 0x0, 0x0, 0x5, 0x2, 0x0, 0x11000, 0x400, 0x1841e, 0x3, 0x0, 0x200000, 0x1000, 0x100000, 0x1000, 0x0, 0x10,
151 {".text", 0x6860, 0x1000, 0x6a00, 0x400, 0x0, 0x0, 0x0, 0x0, 0x60500020},
152 {".data", 0xe0, 0x8000, 0x200, 0x6e00, 0x0, 0x0, 0x0, 0x0, 0xc0500040},
153 {".rdata", 0x6b0, 0x9000, 0x800, 0x7000, 0x0, 0x0, 0x0, 0x0, 0x40600040},
154 {".pdata", 0x498, 0xa000, 0x600, 0x7800, 0x0, 0x0, 0x0, 0x0, 0x40300040},
155 {".xdata", 0x488, 0xb000, 0x600, 0x7e00, 0x0, 0x0, 0x0, 0x0, 0x40300040},
156 {".bss", 0x1410, 0xc000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0600080},
157 {".idata", 0x990, 0xe000, 0xa00, 0x8400, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
158 {".CRT", 0x68, 0xf000, 0x200, 0x8e00, 0x0, 0x0, 0x0, 0x0, 0xc0400040},
159 {".tls", 0x48, 0x10000, 0x200, 0x9000, 0x0, 0x0, 0x0, 0x0, 0xc0600040},
165 func isOptHdrEq(a, b interface{}) bool {
166 switch va := a.(type) {
167 case *OptionalHeader32:
168 vb, ok := b.(*OptionalHeader32)
173 case *OptionalHeader64:
174 vb, ok := b.(*OptionalHeader64)
185 func TestOpen(t *testing.T) {
186 for i := range fileTests {
189 f, err := Open(tt.file)
194 if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
195 t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
198 if !isOptHdrEq(tt.opthdr, f.OptionalHeader) {
199 t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.OptionalHeader, tt.opthdr)
203 for i, sh := range f.Sections {
204 if i >= len(tt.sections) {
207 have := &sh.SectionHeader
208 want := tt.sections[i]
209 if !reflect.DeepEqual(have, want) {
210 t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
213 tn := len(tt.sections)
214 fn := len(f.Sections)
216 t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
218 for i, have := range f.Symbols {
219 if i >= len(tt.symbols) {
222 want := tt.symbols[i]
223 if !reflect.DeepEqual(have, want) {
224 t.Errorf("open %s, symbol %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
230 func TestOpenFailure(t *testing.T) {
231 filename := "file.go" // not a PE file
232 _, err := Open(filename) // don't crash
234 t.Errorf("open %s: succeeded unexpectedly", filename)