From d9ccfe9efda5645e028fb8b22f7a9ffbed8f6698 Mon Sep 17 00:00:00 2001 From: awgh Date: Wed, 30 Dec 2020 02:57:35 -0800 Subject: [PATCH] Fixed read issue with RelocationBlock by adding new type RelocationTableEntry. --- pe/file.go | 2 +- pe/reloc.go | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pe/file.go b/pe/file.go index bc9cb1d..3187453 100644 --- a/pe/file.go +++ b/pe/file.go @@ -31,7 +31,7 @@ type File struct { FileHeader OptionalHeader interface{} // of type *OptionalHeader32 or *OptionalHeader64 Sections []*Section - BaseRelocationTable *[]RelocationBlock + BaseRelocationTable *[]RelocationTableEntry Symbols []*Symbol // COFF symbols with auxiliary symbol records removed COFFSymbols []COFFSymbol // all COFF symbols (including auxiliary symbol records) StringTable StringTable diff --git a/pe/reloc.go b/pe/reloc.go index 42303ac..c68fb1a 100644 --- a/pe/reloc.go +++ b/pe/reloc.go @@ -6,11 +6,16 @@ import ( "io" ) +// RelocationTable - for base relocation entries +type RelocationTableEntry struct { + RelocationBlock + BlockItems []BlockItem +} + // RelocationBlock - for base relocation entries type RelocationBlock struct { VirtualAddress uint32 SizeOfBlock uint32 - BlockItems []BlockItem } // BlockItem - relocation block item @@ -49,7 +54,7 @@ const ( ) // readBaseRelocationTable - reads the base relocation table from the file and stores it -func readBaseRelocationTable(f *File, r io.ReadSeeker) (*[]RelocationBlock, error) { +func readBaseRelocationTable(f *File, r io.ReadSeeker) (*[]RelocationTableEntry, error) { var dd DataDirectory if f.Machine == IMAGE_FILE_MACHINE_AMD64 { @@ -61,7 +66,7 @@ func readBaseRelocationTable(f *File, r io.ReadSeeker) (*[]RelocationBlock, erro if err != nil { return nil, fmt.Errorf("fail to seek to base relocation table: %v", err) } - var reloBlocks []RelocationBlock + var reloBlocks []RelocationTableEntry bytesRead := 0 for bytesRead < int(dd.Size) { var reloBlock RelocationBlock @@ -85,8 +90,7 @@ func readBaseRelocationTable(f *File, r io.ReadSeeker) (*[]RelocationBlock, erro item.Offset = val & 0x0fff blocks[i] = item } - reloBlock.BlockItems = blocks - reloBlocks = append(reloBlocks, reloBlock) + reloBlocks = append(reloBlocks, RelocationTableEntry{reloBlock, blocks}) } return &reloBlocks, nil }