Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
- rename `getSectionDataFromOffset` to `getSectionDataFromAddress` for clarity
- use `io.SeekStart` instead of magic value 0
- fix some typoes
  • Loading branch information
monoidic committed Feb 9, 2024
1 parent 9a455b6 commit 8dfb9dc
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions goversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions macho.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions moduledata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down

0 comments on commit 8dfb9dc

Please sign in to comment.