2018-01-04 08:39:54 +01:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-10-15 09:35:59 +02:00
|
|
|
package symbol_inject
|
2018-01-04 08:39:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"debug/pe"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sort"
|
2018-03-01 20:20:25 +01:00
|
|
|
"strings"
|
2018-01-04 08:39:54 +01:00
|
|
|
)
|
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
func peSymbolsFromFile(r io.ReaderAt) (*File, error) {
|
2018-01-04 08:39:54 +01:00
|
|
|
peFile, err := pe.NewFile(r)
|
|
|
|
if err != nil {
|
2018-03-01 20:20:25 +01:00
|
|
|
return nil, cantParseError{err}
|
2018-01-04 08:39:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
return extractPESymbols(peFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractPESymbols(peFile *pe.File) (*File, error) {
|
|
|
|
var prefix string
|
2018-02-24 02:10:34 +01:00
|
|
|
if peFile.FileHeader.Machine == pe.IMAGE_FILE_MACHINE_I386 {
|
|
|
|
// symbols in win32 exes seem to be prefixed with an underscore
|
2018-03-01 20:20:25 +01:00
|
|
|
prefix = "_"
|
2018-02-24 02:10:34 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:05:39 +01:00
|
|
|
symbols := peFile.Symbols
|
2018-03-01 20:20:25 +01:00
|
|
|
sort.SliceStable(symbols, func(i, j int) bool {
|
2018-02-28 22:05:39 +01:00
|
|
|
if symbols[i].SectionNumber != symbols[j].SectionNumber {
|
|
|
|
return symbols[i].SectionNumber < symbols[j].SectionNumber
|
2018-01-04 08:39:54 +01:00
|
|
|
}
|
2018-02-28 22:05:39 +01:00
|
|
|
return symbols[i].Value < symbols[j].Value
|
2018-01-04 08:39:54 +01:00
|
|
|
})
|
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
file := &File{}
|
|
|
|
|
|
|
|
for _, section := range peFile.Sections {
|
|
|
|
file.Sections = append(file.Sections, &Section{
|
|
|
|
Name: section.Name,
|
|
|
|
Addr: uint64(section.VirtualAddress),
|
|
|
|
Offset: uint64(section.Offset),
|
|
|
|
Size: uint64(section.VirtualSize),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:05:39 +01:00
|
|
|
for _, symbol := range symbols {
|
2018-03-01 20:20:25 +01:00
|
|
|
if symbol.SectionNumber > 0 {
|
|
|
|
file.Symbols = append(file.Symbols, &Symbol{
|
|
|
|
Name: strings.TrimPrefix(symbol.Name, prefix),
|
|
|
|
// PE symbol value is the offset of the symbol into the section
|
|
|
|
Addr: uint64(symbol.Value),
|
|
|
|
// PE symbols don't have size information
|
|
|
|
Size: 0,
|
|
|
|
Section: file.Sections[symbol.SectionNumber-1],
|
2018-02-28 22:05:39 +01:00
|
|
|
})
|
2018-03-01 20:20:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 22:05:39 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
return file, nil
|
|
|
|
}
|
2018-02-28 22:05:39 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
func dumpPESymbols(r io.ReaderAt) error {
|
|
|
|
peFile, err := pe.NewFile(r)
|
|
|
|
if err != nil {
|
|
|
|
return cantParseError{err}
|
|
|
|
}
|
2018-01-04 08:39:54 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
fmt.Println("&pe.File{")
|
|
|
|
fmt.Println("\tFileHeader: pe.FileHeader{")
|
|
|
|
fmt.Printf("\t\tMachine: %#v,\n", peFile.FileHeader.Machine)
|
|
|
|
fmt.Println("\t},")
|
2018-01-04 08:39:54 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
fmt.Println("\tSections: []*pe.Section{")
|
|
|
|
for _, section := range peFile.Sections {
|
|
|
|
fmt.Printf("\t\t&pe.Section{SectionHeader: %#v},\n", section.SectionHeader)
|
|
|
|
}
|
|
|
|
fmt.Println("\t},")
|
2018-01-04 08:39:54 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
fmt.Println("\tSymbols: []*pe.Symbol{")
|
|
|
|
for _, symbol := range peFile.Symbols {
|
|
|
|
fmt.Printf("\t\t%#v,\n", symbol)
|
2018-01-04 08:39:54 +01:00
|
|
|
}
|
2018-03-01 20:20:25 +01:00
|
|
|
fmt.Println("\t},")
|
|
|
|
|
|
|
|
fmt.Println("}")
|
2018-01-04 08:39:54 +01:00
|
|
|
|
2018-03-01 20:20:25 +01:00
|
|
|
return nil
|
2018-01-04 08:39:54 +01:00
|
|
|
}
|