~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/debug/pe/file.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
type File struct {
20
20
        FileHeader
21
21
        Sections []*Section
 
22
        Symbols  []*Symbol
22
23
 
23
24
        closer io.Closer
24
25
}
49
50
        sr *io.SectionReader
50
51
}
51
52
 
 
53
type Symbol struct {
 
54
        Name          string
 
55
        Value         uint32
 
56
        SectionNumber int16
 
57
        Type          uint16
 
58
        StorageClass  uint8
 
59
}
 
60
 
52
61
type ImportDirectory struct {
53
62
        OriginalFirstThunk uint32
54
63
        TimeDateStamp      uint32
122
131
        }
123
132
        var base int64
124
133
        if dosheader[0] == 'M' && dosheader[1] == 'Z' {
 
134
                signoff := int64(binary.LittleEndian.Uint32(dosheader[0x3c:]))
125
135
                var sign [4]byte
126
 
                r.ReadAt(sign[0:], int64(dosheader[0x3c]))
 
136
                r.ReadAt(sign[:], signoff)
127
137
                if !(sign[0] == 'P' && sign[1] == 'E' && sign[2] == 0 && sign[3] == 0) {
128
138
                        return nil, errors.New("Invalid PE File Format.")
129
139
                }
130
 
                base = int64(dosheader[0x3c]) + 4
 
140
                base = signoff + 4
131
141
        } else {
132
142
                base = int64(0)
133
143
        }
138
148
        if f.FileHeader.Machine != IMAGE_FILE_MACHINE_UNKNOWN && f.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64 && f.FileHeader.Machine != IMAGE_FILE_MACHINE_I386 {
139
149
                return nil, errors.New("Invalid PE File Format.")
140
150
        }
141
 
        // get symbol string table
142
 
        sr.Seek(int64(f.FileHeader.PointerToSymbolTable+18*f.FileHeader.NumberOfSymbols), os.SEEK_SET)
143
 
        var l uint32
144
 
        if err := binary.Read(sr, binary.LittleEndian, &l); err != nil {
145
 
                return nil, err
146
 
        }
147
 
        ss := make([]byte, l)
148
 
        if _, err := r.ReadAt(ss, int64(f.FileHeader.PointerToSymbolTable+18*f.FileHeader.NumberOfSymbols)); err != nil {
149
 
                return nil, err
150
 
        }
 
151
 
 
152
        var ss []byte
 
153
        if f.FileHeader.NumberOfSymbols > 0 {
 
154
                // Get COFF string table, which is located at the end of the COFF symbol table.
 
155
                sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), os.SEEK_SET)
 
156
                var l uint32
 
157
                if err := binary.Read(sr, binary.LittleEndian, &l); err != nil {
 
158
                        return nil, err
 
159
                }
 
160
                ss = make([]byte, l)
 
161
                if _, err := r.ReadAt(ss, int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols)); err != nil {
 
162
                        return nil, err
 
163
                }
 
164
 
 
165
                // Process COFF symbol table.
 
166
                sr.Seek(int64(f.FileHeader.PointerToSymbolTable), os.SEEK_SET)
 
167
                aux := uint8(0)
 
168
                for i := 0; i < int(f.FileHeader.NumberOfSymbols); i++ {
 
169
                        cs := new(COFFSymbol)
 
170
                        if err := binary.Read(sr, binary.LittleEndian, cs); err != nil {
 
171
                                return nil, err
 
172
                        }
 
173
                        if aux > 0 {
 
174
                                aux--
 
175
                                continue
 
176
                        }
 
177
                        var name string
 
178
                        if cs.Name[0] == 0 && cs.Name[1] == 0 && cs.Name[2] == 0 && cs.Name[3] == 0 {
 
179
                                si := int(binary.LittleEndian.Uint32(cs.Name[4:]))
 
180
                                name, _ = getString(ss, si)
 
181
                        } else {
 
182
                                name = cstring(cs.Name[:])
 
183
                        }
 
184
                        aux = cs.NumberOfAuxSymbols
 
185
                        s := &Symbol{
 
186
                                Name:          name,
 
187
                                Value:         cs.Value,
 
188
                                SectionNumber: cs.SectionNumber,
 
189
                                Type:          cs.Type,
 
190
                                StorageClass:  cs.StorageClass,
 
191
                        }
 
192
                        f.Symbols = append(f.Symbols, s)
 
193
                }
 
194
        }
 
195
 
 
196
        // Process sections.
151
197
        sr.Seek(base, os.SEEK_SET)
152
198
        binary.Read(sr, binary.LittleEndian, &f.FileHeader)
153
199
        sr.Seek(int64(f.FileHeader.SizeOfOptionalHeader), os.SEEK_CUR) //Skip OptionalHeader