-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpe.go
More file actions
68 lines (62 loc) · 2.03 KB
/
pe.go
File metadata and controls
68 lines (62 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2012-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux,amd64
package main
import (
"debug/pe"
"flag"
"fmt"
"syscall"
"github.com/linuxboot/voodoo/trace"
)
func loadPE(t trace.Trace, n string, r *syscall.PtraceRegs, log func(string, ...interface{})) error {
f, err := pe.Open(flag.Args()[0])
if err != nil {
return err
}
defer f.Close()
h, ok := f.OptionalHeader.(*pe.OptionalHeader64)
if !ok {
return fmt.Errorf("File type is %T, but has to be %T", f.OptionalHeader, pe.OptionalHeader64{})
}
// We need to relocate to start at *offset.
// UEFI runs in page zero. I can't believe it.
base := uintptr(h.ImageBase)
eip := base + uintptr(h.BaseOfCode)
heap := base + uintptr(h.SizeOfImage)
// heap is at end of the image.
// Stack goes at top of reserved stack area.
sp := heap + uintptr(h.SizeOfHeapReserve+h.SizeOfStackReserve)
totalsize := int(h.SizeOfImage) + int(h.SizeOfHeapReserve+h.SizeOfStackReserve)
log("Write %d bytes to %#x", totalsize, base)
if err := t.Write(base, make([]byte, totalsize)); err != nil {
return fmt.Errorf("Can't write %d bytes of zero @ %#x for this section to process:%v", totalsize, base, err)
}
for i, s := range f.Sections {
log("Section %d", i)
log(show("\t", &s.SectionHeader))
addr := base + uintptr(s.VirtualAddress)
dat, err := s.Data()
if err != nil {
return fmt.Errorf("Can't get data for this section: %v", err)
}
// Zero it out.
log("Copy section to %#x:%#x", addr, s.VirtualSize)
bb := make([]byte, s.VirtualSize)
if false {
for i := range bb {
bb[i] = 0xf4
}
}
if err := t.Write(addr, bb); err != nil {
return fmt.Errorf("Can't write %d bytes of zero @ %#x for this section to process:%v", len(bb), addr, err)
}
if err := t.Write(addr, dat); err != nil {
return fmt.Errorf("Can't write %d bytes of data @ %#x for this section to process: %v", len(dat), addr, err)
}
}
r.Rsp = uint64(sp)
r.Rip = uint64(eip)
return nil
}