From 8dfb9dc3c36bd55cea671e2cf8f2489fa4fd1906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikk=20Margus=20M=C3=B6ll?= Date: Sat, 10 Feb 2024 00:35:17 +0200 Subject: [PATCH] tweaks - rename `getSectionDataFromOffset` to `getSectionDataFromAddress` for clarity - use `io.SeekStart` instead of magic value 0 - fix some typoes --- elf.go | 4 ++-- file.go | 6 +++--- file_test.go | 8 ++++---- goversion.go | 4 ++-- macho.go | 4 ++-- moduledata.go | 7 ++++--- pe.go | 4 ++-- testdata/gold | 2 +- type.go | 8 ++++---- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/elf.go b/elf.go index cf285d8..b5fb75e 100644 --- a/elf.go +++ b/elf.go @@ -113,14 +113,14 @@ func (e *elfFile) moduledataSection() string { return ".noptrdata" } -func (e *elfFile) getSectionDataFromOffset(off uint64) (uint64, []byte, error) { +func (e *elfFile) getSectionDataFromAddress(address uint64) (uint64, []byte, error) { for _, section := range e.file.Sections { if section.Offset == 0 { // Only exist in memory continue } - if section.Addr <= off && off < (section.Addr+section.Size) { + if section.Addr <= address && address < (section.Addr+section.Size) { data, err := section.Data() return section.Addr, data, err } diff --git a/file.go b/file.go index 0efb75e..548a6ad 100644 --- a/file.go +++ b/file.go @@ -47,7 +47,7 @@ func Open(filePath string) (*GoFile, error) { return nil, err } - _, err = f.Seek(0, 0) + _, err = f.Seek(0, io.SeekStart) if err != nil { return nil, err } @@ -398,7 +398,7 @@ func (f *GoFile) GetTypes() ([]*GoType, error) { // Bytes return a slice of raw bytes with the length in the file from the address. func (f *GoFile) Bytes(address uint64, length uint64) ([]byte, error) { - base, section, err := f.fh.getSectionDataFromOffset(address) + base, section, err := f.fh.getSectionDataFromAddress(address) if err != nil { return nil, err } @@ -432,7 +432,7 @@ type fileHandler interface { getPCLNTab() (*gosym.Table, error) getRData() ([]byte, error) getCodeSection() (uint64, []byte, error) - getSectionDataFromOffset(uint64) (uint64, []byte, error) + getSectionDataFromAddress(uint64) (uint64, []byte, error) getSectionData(string) (uint64, []byte, error) getFileInfo() *FileInfo getPCLNTABData() (uint64, []byte, error) diff --git a/file_test.go b/file_test.go index df817a1..edaad21 100644 --- a/file_test.go +++ b/file_test.go @@ -171,7 +171,7 @@ func TestSetGoVersion(t *testing.T) { } type mockFileHandler struct { - mGetSectionDataFromOffset func(uint64) (uint64, []byte, error) + mGetSectionDataFromAddress func(uint64) (uint64, []byte, error) } func (m *mockFileHandler) getFile() *os.File { @@ -198,8 +198,8 @@ func (m *mockFileHandler) getCodeSection() (uint64, []byte, error) { panic("not implemented") } -func (m *mockFileHandler) getSectionDataFromOffset(o uint64) (uint64, []byte, error) { - return m.mGetSectionDataFromOffset(o) +func (m *mockFileHandler) getSectionDataFromAddress(a uint64) (uint64, []byte, error) { + return m.mGetSectionDataFromAddress(a) } func (m *mockFileHandler) getSectionData(string) (uint64, []byte, error) { @@ -230,7 +230,7 @@ func TestBytes(t *testing.T) { address := uint64(expectedBase + 2) length := uint64(len(expectedBytes)) fh := &mockFileHandler{ - mGetSectionDataFromOffset: func(a uint64) (uint64, []byte, error) { + mGetSectionDataFromAddress: func(a uint64) (uint64, []byte, error) { if a > expectedBase+uint64(len(expectedSection)) || a < expectedBase { return 0, nil, errors.New("out of bound") } diff --git a/goversion.go b/goversion.go index a11d5bd..eeccfd0 100644 --- a/goversion.go +++ b/goversion.go @@ -133,7 +133,7 @@ func tryFromSchedInit(f *GoFile) *GoVersion { is32 = true } - // Find shedinit function. + // Find schedinit function. var fcn *Function std, err := f.GetSTDLib() if err != nil { @@ -154,7 +154,7 @@ pkgLoop: } } - // Check if the functions was found + // Check if the function was found if fcn == nil { // If we can't find the function there is nothing to do. return nil diff --git a/macho.go b/macho.go index 5337b7f..f8bf1d9 100644 --- a/macho.go +++ b/macho.go @@ -82,14 +82,14 @@ func (m *machoFile) getCodeSection() (uint64, []byte, error) { return m.getSectionData("__text") } -func (m *machoFile) getSectionDataFromOffset(off uint64) (uint64, []byte, error) { +func (m *machoFile) getSectionDataFromAddress(address uint64) (uint64, []byte, error) { for _, section := range m.file.Sections { if section.Offset == 0 { // Only exist in memory continue } - if section.Addr <= off && off < (section.Addr+section.Size) { + if section.Addr <= address && address < (section.Addr+section.Size) { data, err := section.Data() return section.Addr, data, err } diff --git a/moduledata.go b/moduledata.go index ab1b901..995f781 100644 --- a/moduledata.go +++ b/moduledata.go @@ -24,10 +24,11 @@ import ( "encoding/binary" "errors" "fmt" - "golang.org/x/mod/semver" "io" "strconv" "strings" + + "golang.org/x/mod/semver" ) // Moduledata holds information about the layout of the executable image in memory. @@ -157,7 +158,7 @@ func (m moduledata) ITabLinks() ModuleDataSection { // TypeLink returns the typelink section. func (m moduledata) TypeLink() ([]int32, error) { - base, data, err := m.fh.getSectionDataFromOffset(m.TypelinkAddr) + base, data, err := m.fh.getSectionDataFromAddress(m.TypelinkAddr) if err != nil { return nil, fmt.Errorf("failed to get the typelink data section: %w", err) } @@ -198,7 +199,7 @@ func (m ModuleDataSection) Data() ([]byte, error) { if m.Length == 0 { return []byte{}, nil } - base, data, err := m.fh.getSectionDataFromOffset(m.Address) + base, data, err := m.fh.getSectionDataFromAddress(m.Address) if err != nil { return nil, fmt.Errorf("getting module data section failed: %w", err) } diff --git a/pe.go b/pe.go index df0a919..249109d 100644 --- a/pe.go +++ b/pe.go @@ -111,14 +111,14 @@ func (p *peFile) getPCLNTABData() (uint64, []byte, error) { return p.imageBase + uint64(b), d, e } -func (p *peFile) getSectionDataFromOffset(off uint64) (uint64, []byte, error) { +func (p *peFile) getSectionDataFromAddress(address uint64) (uint64, []byte, error) { for _, section := range p.file.Sections { if section.Offset == 0 { // Only exist in memory continue } - if p.imageBase+uint64(section.VirtualAddress) <= off && off < p.imageBase+uint64(section.VirtualAddress+section.Size) { + if p.imageBase+uint64(section.VirtualAddress) <= address && address < p.imageBase+uint64(section.VirtualAddress+section.Size) { data, err := section.Data() return p.imageBase + uint64(section.VirtualAddress), data, err } diff --git a/testdata/gold b/testdata/gold index af0dbc4..4b3bd35 160000 --- a/testdata/gold +++ b/testdata/gold @@ -1 +1 @@ -Subproject commit af0dbc4e57a9775a5855d5b175ad66bec3583b0d +Subproject commit 4b3bd3514cd366783c6019325f3a958471851194 diff --git a/type.go b/type.go index 79d0b4d..3f24035 100644 --- a/type.go +++ b/type.go @@ -91,7 +91,7 @@ func getLegacyTypes(fileInfo *FileInfo, f fileHandler) (map[uint64]*GoType, erro if err != nil { return nil, err } - typelinkAddr, typelinkData, err := f.getSectionDataFromOffset(md.TypelinkAddr) + typelinkAddr, typelinkData, err := f.getSectionDataFromAddress(md.TypelinkAddr) if err != nil { return nil, fmt.Errorf("no typelink section found: %w", err) } @@ -104,15 +104,15 @@ func getLegacyTypes(fileInfo *FileInfo, f fileHandler) (map[uint64]*GoType, erro goTypes := make(map[uint64]*GoType) for i := uint64(0); i < md.TypelinkLen; i++ { // Type offsets are always *_type - off, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32) + address, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32) if err != nil { return nil, err } - baseAddr, baseData, err := f.getSectionDataFromOffset(off) + baseAddr, baseData, err := f.getSectionDataFromAddress(address) if err != nil { continue } - typ := typeParse(goTypes, fileInfo, off-baseAddr, baseData, baseAddr) + typ := typeParse(goTypes, fileInfo, address-baseAddr, baseData, baseAddr) if typ == nil { continue }