ddbb271744146c8b052740e9fc251f1acfc29dd9
[platform/upstream/gcc.git] / libgo / go / debug / pe / file_test.go
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 pe
6
7 import (
8         "reflect"
9         "testing"
10 )
11
12 type fileTest struct {
13         file     string
14         hdr      FileHeader
15         opthdr   interface{}
16         sections []*SectionHeader
17         symbols  []*Symbol
18 }
19
20 var fileTests = []fileTest{
21         {
22                 "testdata/gcc-386-mingw-obj",
23                 FileHeader{0x014c, 0x000c, 0x0, 0x64a, 0x1e, 0x0, 0x104},
24                 nil,
25                 []*SectionHeader{
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},
38                 },
39                 []*Symbol{
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},
56                 },
57         },
58         {
59                 "testdata/gcc-386-mingw-exec",
60                 FileHeader{0x014c, 0x000f, 0x4c6a1b60, 0x3c00, 0x282, 0xe0, 0x107},
61                 &OptionalHeader32{
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,
63                         [16]DataDirectory{
64                                 {0x0, 0x0},
65                                 {0x5000, 0x3c8},
66                                 {0x0, 0x0},
67                                 {0x0, 0x0},
68                                 {0x0, 0x0},
69                                 {0x0, 0x0},
70                                 {0x0, 0x0},
71                                 {0x0, 0x0},
72                                 {0x0, 0x0},
73                                 {0x7000, 0x18},
74                                 {0x0, 0x0},
75                                 {0x0, 0x0},
76                                 {0x0, 0x0},
77                                 {0x0, 0x0},
78                                 {0x0, 0x0},
79                                 {0x0, 0x0},
80                         },
81                 },
82                 []*SectionHeader{
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},
98                 },
99                 []*Symbol{},
100         },
101         {
102                 "testdata/gcc-amd64-mingw-obj",
103                 FileHeader{0x8664, 0x6, 0x0, 0x198, 0x12, 0x0, 0x4},
104                 nil,
105                 []*SectionHeader{
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},
112                 },
113                 []*Symbol{
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},
124                 },
125         },
126         {
127                 "testdata/gcc-amd64-mingw-exec",
128                 FileHeader{0x8664, 0x9, 0x53472993, 0x0, 0x0, 0xf0, 0x22f},
129                 &OptionalHeader64{
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,
131                         [16]DataDirectory{
132                                 {0x0, 0x0},
133                                 {0xe000, 0x990},
134                                 {0x0, 0x0},
135                                 {0xa000, 0x498},
136                                 {0x0, 0x0},
137                                 {0x0, 0x0},
138                                 {0x0, 0x0},
139                                 {0x0, 0x0},
140                                 {0x0, 0x0},
141                                 {0x10000, 0x28},
142                                 {0x0, 0x0},
143                                 {0x0, 0x0},
144                                 {0xe254, 0x218},
145                                 {0x0, 0x0},
146                                 {0x0, 0x0},
147                                 {0x0, 0x0},
148                         },
149                 },
150                 []*SectionHeader{
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},
160                 },
161                 []*Symbol{},
162         },
163 }
164
165 func isOptHdrEq(a, b interface{}) bool {
166         switch va := a.(type) {
167         case *OptionalHeader32:
168                 vb, ok := b.(*OptionalHeader32)
169                 if !ok {
170                         return false
171                 }
172                 return *vb == *va
173         case *OptionalHeader64:
174                 vb, ok := b.(*OptionalHeader64)
175                 if !ok {
176                         return false
177                 }
178                 return *vb == *va
179         case nil:
180                 return b == nil
181         }
182         return false
183 }
184
185 func TestOpen(t *testing.T) {
186         for i := range fileTests {
187                 tt := &fileTests[i]
188
189                 f, err := Open(tt.file)
190                 if err != nil {
191                         t.Error(err)
192                         continue
193                 }
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)
196                         continue
197                 }
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)
200                         continue
201                 }
202
203                 for i, sh := range f.Sections {
204                         if i >= len(tt.sections) {
205                                 break
206                         }
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)
211                         }
212                 }
213                 tn := len(tt.sections)
214                 fn := len(f.Sections)
215                 if tn != fn {
216                         t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
217                 }
218                 for i, have := range f.Symbols {
219                         if i >= len(tt.symbols) {
220                                 break
221                         }
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)
225                         }
226                 }
227         }
228 }
229
230 func TestOpenFailure(t *testing.T) {
231         filename := "file.go"    // not a PE file
232         _, err := Open(filename) // don't crash
233         if err == nil {
234                 t.Errorf("open %s: succeeded unexpectedly", filename)
235         }
236 }