@@ -11,44 +11,6 @@ const baremetal = true
1111
1212const GOOS = "uefi"
1313
14- // MS-DOS stub with PE header offset:
15- // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#ms-dos-stub-image-only
16- type exeHeader struct {
17- signature uint16
18- _ [58 ]byte // skip DOS header
19- peHeader uint32 // at offset 0x3C
20- }
21-
22- // COFF file header:
23- // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#file-headers
24- type peHeader struct {
25- magic uint32
26- machine uint16
27- numberOfSections uint16
28- timeDateStamp uint32
29- pointerToSymbolTable uint32
30- numberOfSymbols uint32
31- sizeOfOptionalHeader uint16
32- characteristics uint16
33- }
34-
35- // COFF section header:
36- // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#section-table-section-headers
37- type peSection struct {
38- name [8 ]byte
39- virtualSize uint32
40- virtualAddress uint32
41- sizeOfRawData uint32
42- pointerToRawData uint32
43- pointerToRelocations uint32
44- pointerToLinenumbers uint32
45- numberOfRelocations uint16
46- numberOfLinenumbers uint16
47- characteristics uint32
48- }
49-
50- var module * exeHeader
51-
5214// Mark global variables.
5315// Unfortunately, the linker doesn't provide symbols for the start and end of
5416// the data/bss sections. Therefore these addresses need to be determined at
@@ -57,15 +19,6 @@ var module *exeHeader
5719// Most of this function is based on the documentation in
5820// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format.
5921func findGlobals (found func (start , end uintptr )) {
60- // Constants used in this function.
61- const (
62- // https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexa
63- GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT = 0x00000002
64-
65- // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
66- IMAGE_SCN_MEM_WRITE = 0x80000000
67- )
68-
6922 if module == nil {
7023 var loadedImage * uefi.EFI_LOADED_IMAGE_PROTOCOL
7124
@@ -78,22 +31,5 @@ func findGlobals(found func(start, end uintptr)) {
7831 module = (* exeHeader )(unsafe .Pointer (loadedImage .ImageBase ))
7932 }
8033
81- // Find the PE header at offset 0x3C.
82- pe := (* peHeader )(unsafe .Add (unsafe .Pointer (module ), module .peHeader ))
83- if pe .magic != 0x00004550 { // 0x4550 is "PE"
84- uefi .DebugPrint ("cannot find PE header" , uint64 (pe .magic ))
85- return
86- }
87-
88- // Iterate through sections.
89- section := (* peSection )(unsafe .Pointer (uintptr (unsafe .Pointer (pe )) + uintptr (pe .sizeOfOptionalHeader ) + unsafe .Sizeof (peHeader {})))
90- for i := 0 ; i < int (pe .numberOfSections ); i ++ {
91- if section .characteristics & IMAGE_SCN_MEM_WRITE != 0 {
92- // Found a writable section. Scan the entire section for roots.
93- start := uintptr (unsafe .Pointer (module )) + uintptr (section .virtualAddress )
94- end := uintptr (unsafe .Pointer (module )) + uintptr (section .virtualAddress ) + uintptr (section .virtualSize )
95- found (start , end )
96- }
97- section = (* peSection )(unsafe .Add (unsafe .Pointer (section ), unsafe .Sizeof (peSection {})))
98- }
34+ findGlobalsForPE (found )
9935}
0 commit comments