MCPcopy Create free account
hub / github.com/Binject/universal / CopySections

Function CopySections

loader_windows.go:74–116  ·  view source on GitHub ↗

CopySections - writes the sections of a PE image to the given base address in memory

(pefile *pe.File, image *[]byte, loc uintptr)

Source from the content-addressed store, hash-verified

72
73// CopySections - writes the sections of a PE image to the given base address in memory
74func CopySections(pefile *pe.File, image *[]byte, loc uintptr) error {
75 // Copy Headers
76 var sizeOfHeaders uint32
77 if pefile.Machine == pe.IMAGE_FILE_MACHINE_AMD64 {
78 sizeOfHeaders = pefile.OptionalHeader.(*pe.OptionalHeader64).SizeOfHeaders
79 } else {
80 sizeOfHeaders = pefile.OptionalHeader.(*pe.OptionalHeader32).SizeOfHeaders
81 }
82 hbuf := (*[^uint32(0)]byte)(unsafe.Pointer(uintptr(loc)))
83 for index := uint32(0); index < sizeOfHeaders; index++ {
84 hbuf[index] = (*image)[index]
85 }
86
87 // Copy Sections
88 for _, section := range pefile.Sections {
89 //fmt.Println("Writing:", fmt.Sprintf("%s %x %x", section.Name, loc, uint32(loc)+section.VirtualAddress))
90 if section.Size == 0 {
91 continue
92 }
93 d, err := section.Data()
94 if err != nil {
95 return err
96 }
97 dataLen := uint32(len(d))
98 dst := uint64(loc) + uint64(section.VirtualAddress)
99 buf := (*[^uint32(0)]byte)(unsafe.Pointer(uintptr(dst)))
100 for index := uint32(0); index < dataLen; index++ {
101 buf[index] = d[index]
102 }
103 }
104
105 // Write symbol and string tables
106 bbuf := bytes.NewBuffer(nil)
107 binary.Write(bbuf, binary.LittleEndian, pefile.COFFSymbols)
108 binary.Write(bbuf, binary.LittleEndian, pefile.StringTable)
109 b := bbuf.Bytes()
110 blen := uint32(len(b))
111 for index := uint32(0); index < blen; index++ {
112 hbuf[index+pefile.FileHeader.PointerToSymbolTable] = b[index]
113 }
114
115 return nil
116}
117
118var (
119 kernel32 = syscall.MustLoadDLL("kernel32.dll")

Callers 1

LoadLibraryImplFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…