1 |
747 |
jeremybenn |
// 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 pe
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"reflect"
|
9 |
|
|
"testing"
|
10 |
|
|
)
|
11 |
|
|
|
12 |
|
|
type fileTest struct {
|
13 |
|
|
file string
|
14 |
|
|
hdr FileHeader
|
15 |
|
|
sections []*SectionHeader
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
var fileTests = []fileTest{
|
19 |
|
|
{
|
20 |
|
|
"testdata/gcc-386-mingw-obj",
|
21 |
|
|
FileHeader{0x014c, 0x000c, 0x0, 0x64a, 0x1e, 0x0, 0x104},
|
22 |
|
|
[]*SectionHeader{
|
23 |
|
|
{".text", 0, 0, 36, 500, 1440, 0, 3, 0, 0x60300020},
|
24 |
|
|
{".data", 0, 0, 0, 0, 0, 0, 0, 0, 3224371264},
|
25 |
|
|
{".bss", 0, 0, 0, 0, 0, 0, 0, 0, 3224371328},
|
26 |
|
|
{".debug_abbrev", 0, 0, 137, 536, 0, 0, 0, 0, 0x42100000},
|
27 |
|
|
{".debug_info", 0, 0, 418, 673, 1470, 0, 7, 0, 1108344832},
|
28 |
|
|
{".debug_line", 0, 0, 128, 1091, 1540, 0, 1, 0, 1108344832},
|
29 |
|
|
{".rdata", 0, 0, 16, 1219, 0, 0, 0, 0, 1076887616},
|
30 |
|
|
{".debug_frame", 0, 0, 52, 1235, 1550, 0, 2, 0, 1110441984},
|
31 |
|
|
{".debug_loc", 0, 0, 56, 1287, 0, 0, 0, 0, 1108344832},
|
32 |
|
|
{".debug_pubnames", 0, 0, 27, 1343, 1570, 0, 1, 0, 1108344832},
|
33 |
|
|
{".debug_pubtypes", 0, 0, 38, 1370, 1580, 0, 1, 0, 1108344832},
|
34 |
|
|
{".debug_aranges", 0, 0, 32, 1408, 1590, 0, 2, 0, 1108344832},
|
35 |
|
|
},
|
36 |
|
|
},
|
37 |
|
|
{
|
38 |
|
|
"testdata/gcc-386-mingw-exec",
|
39 |
|
|
FileHeader{0x014c, 0x000f, 0x4c6a1b60, 0x3c00, 0x282, 0xe0, 0x107},
|
40 |
|
|
[]*SectionHeader{
|
41 |
|
|
{Name: ".text", VirtualSize: 0xcd8, VirtualAddress: 0x1000, Size: 0xe00, Offset: 0x400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500060},
|
42 |
|
|
{Name: ".data", VirtualSize: 0x10, VirtualAddress: 0x2000, Size: 0x200, Offset: 0x1200, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
|
43 |
|
|
{Name: ".rdata", VirtualSize: 0x120, VirtualAddress: 0x3000, Size: 0x200, Offset: 0x1400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x40300040},
|
44 |
|
|
{Name: ".bss", VirtualSize: 0xdc, VirtualAddress: 0x4000, Size: 0x0, Offset: 0x0, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0400080},
|
45 |
|
|
{Name: ".idata", VirtualSize: 0x3c8, VirtualAddress: 0x5000, Size: 0x400, Offset: 0x1600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
|
46 |
|
|
{Name: ".CRT", VirtualSize: 0x18, VirtualAddress: 0x6000, Size: 0x200, Offset: 0x1a00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
|
47 |
|
|
{Name: ".tls", VirtualSize: 0x20, VirtualAddress: 0x7000, Size: 0x200, Offset: 0x1c00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
|
48 |
|
|
{Name: ".debug_aranges", VirtualSize: 0x20, VirtualAddress: 0x8000, Size: 0x200, Offset: 0x1e00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
49 |
|
|
{Name: ".debug_pubnames", VirtualSize: 0x51, VirtualAddress: 0x9000, Size: 0x200, Offset: 0x2000, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
50 |
|
|
{Name: ".debug_pubtypes", VirtualSize: 0x91, VirtualAddress: 0xa000, Size: 0x200, Offset: 0x2200, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
51 |
|
|
{Name: ".debug_info", VirtualSize: 0xe22, VirtualAddress: 0xb000, Size: 0x1000, Offset: 0x2400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
52 |
|
|
{Name: ".debug_abbrev", VirtualSize: 0x157, VirtualAddress: 0xc000, Size: 0x200, Offset: 0x3400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
53 |
|
|
{Name: ".debug_line", VirtualSize: 0x144, VirtualAddress: 0xd000, Size: 0x200, Offset: 0x3600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
54 |
|
|
{Name: ".debug_frame", VirtualSize: 0x34, VirtualAddress: 0xe000, Size: 0x200, Offset: 0x3800, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42300000},
|
55 |
|
|
{Name: ".debug_loc", VirtualSize: 0x38, VirtualAddress: 0xf000, Size: 0x200, Offset: 0x3a00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
|
56 |
|
|
},
|
57 |
|
|
},
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
func TestOpen(t *testing.T) {
|
61 |
|
|
for i := range fileTests {
|
62 |
|
|
tt := &fileTests[i]
|
63 |
|
|
|
64 |
|
|
f, err := Open(tt.file)
|
65 |
|
|
if err != nil {
|
66 |
|
|
t.Error(err)
|
67 |
|
|
continue
|
68 |
|
|
}
|
69 |
|
|
if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
|
70 |
|
|
t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
|
71 |
|
|
continue
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
for i, sh := range f.Sections {
|
75 |
|
|
if i >= len(tt.sections) {
|
76 |
|
|
break
|
77 |
|
|
}
|
78 |
|
|
have := &sh.SectionHeader
|
79 |
|
|
want := tt.sections[i]
|
80 |
|
|
if !reflect.DeepEqual(have, want) {
|
81 |
|
|
t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
tn := len(tt.sections)
|
85 |
|
|
fn := len(f.Sections)
|
86 |
|
|
if tn != fn {
|
87 |
|
|
t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
func TestOpenFailure(t *testing.T) {
|
94 |
|
|
filename := "file.go" // not a PE file
|
95 |
|
|
_, err := Open(filename) // don't crash
|
96 |
|
|
if err == nil {
|
97 |
|
|
t.Errorf("open %s: succeeded unexpectedly", filename)
|
98 |
|
|
}
|
99 |
|
|
}
|