Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removes debug/macho and GetFile #103

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@ import (
"debug/elf"
"errors"
"fmt"
"os"
"io"
"sync"
)

func openELF(fp string) (*elfFile, error) {
osFile, err := os.Open(fp)
if err != nil {
return nil, fmt.Errorf("error when opening the ELF file: %w", err)
}

f, err := elf.NewFile(osFile)
func openELF(r io.ReaderAt) (*elfFile, error) {
f, err := elf.NewFile(r)
if err != nil {
return nil, fmt.Errorf("error when parsing the ELF file: %w", err)
}
ret := &elfFile{file: f, osFile: osFile}
ret := &elfFile{file: f, reader: r}
ret.getsymtab = sync.OnceValues(ret.initSymTab)
return ret, nil
}
Expand All @@ -45,7 +40,7 @@ var _ fileHandler = (*elfFile)(nil)

type elfFile struct {
file *elf.File
osFile *os.File
reader io.ReaderAt
getsymtab func() (map[string]Symbol, error)
}

Expand Down Expand Up @@ -85,16 +80,16 @@ func (e *elfFile) getParsedFile() any {
return e.file
}

func (e *elfFile) getFile() *os.File {
return e.osFile
func (e *elfFile) getReader() io.ReaderAt {
return e.reader
}

func (e *elfFile) Close() error {
err := e.file.Close()
if err != nil {
return err
}
return e.osFile.Close()
return tryClose(e.reader)
}

func (e *elfFile) getRData() ([]byte, error) {
Expand Down
44 changes: 19 additions & 25 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"sort"
"sync"

macho2 "github.com/blacktop/go-macho"
"github.com/blacktop/go-macho"
"github.com/blacktop/go-macho/pkg/fixupchains"
)

Expand All @@ -50,15 +50,18 @@ func Open(filePath string) (*GoFile, error) {
if err != nil {
return nil, err
}

_, err = f.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}

return OpenReader(f)
}

// OpenReader opens a reader and returns a handler to the file.
func OpenReader(f io.ReaderAt) (*GoFile, error) {
buf := make([]byte, maxMagicBufLen)
n, err := f.Read(buf)
_ = f.Close()
n, err := f.ReadAt(buf, 0)
if err != nil {
return nil, err
}
Expand All @@ -67,23 +70,23 @@ func Open(filePath string) (*GoFile, error) {
}
gofile := new(GoFile)
if fileMagicMatch(buf, elfMagic) {
elf, err := openELF(filePath)
elf, err := openELF(f)
if err != nil {
return nil, err
}
gofile.fh = elf
} else if fileMagicMatch(buf, peMagic) {
pe, err := openPE(filePath)
pe, err := openPE(f)
if err != nil {
return nil, err
}
gofile.fh = pe
} else if fileMagicMatch(buf, machoMagic1) || fileMagicMatch(buf, machoMagic2) || fileMagicMatch(buf, machoMagic3) || fileMagicMatch(buf, machoMagic4) {
macho, err := openMachO(filePath)
machO, err := openMachO(f)
if err != nil {
return nil, err
}
gofile.fh = macho
gofile.fh = machO
} else {
return nil, ErrUnsupportedFile
}
Expand Down Expand Up @@ -181,16 +184,16 @@ func (f *GoFile) initPackages() error {
return f.initPackagesError
}

// GetFile returns the raw file opened by the library.
func (f *GoFile) GetFile() *os.File {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removing an exported method which likely will break some stuff downstream. I know it's not a 1.0 version yet so breakage is not guaranteed but I like to avoid it as much as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we didn't hold a *os.File right now, there's no way to keep providing this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/search?q=lang%3AGo+github.com%2Fgoretk%2Fgore&type=code

Additionally, I performed a search in GitHub and no repositories seem to be calling this API except for my project https://github.com/Zxilly/go-size-analyzer.

return f.fh.getFile()
// GetReader returns the reader passed to the file handler.
func (f *GoFile) GetReader() io.ReaderAt {
return f.fh.getReader()
}

// GetParsedFile returns the parsed file, should be cast based on the file type.
// Possible types are:
// - *elf.File
// - *pe.File
// - *macho.File
// - *github.com/blacktop/go-macho.File
//
// all from the debug package.
func (f *GoFile) GetParsedFile() any {
Expand Down Expand Up @@ -483,21 +486,12 @@ func (f *GoFile) PCLNTab() (*gosym.Table, error) {
}

func (f *GoFile) findRuntimeTextMachoChainedFixups(pclntabAddr uint64) (uint64, error) {
of := f.fh.getFile()
_, err := of.Seek(0, io.SeekStart)
if err != nil {
return 0, err
}

f2, err := macho2.NewFile(of)
if err != nil {
return 0, err
}
fixups, err := f2.DyldChainedFixups()
mf := f.fh.getParsedFile().(*macho.File)
fixups, err := mf.DyldChainedFixups()
if err != nil {
return 0, err
}
baseAddr := f2.GetBaseAddress()
baseAddr := mf.GetBaseAddress()
var rebases []fixupchains.Rebase
for _, start := range fixups.Starts {
rebases = append(rebases, start.Rebases()...)
Expand Down Expand Up @@ -624,7 +618,7 @@ type fileHandler interface {
getPCLNTABData() (uint64, []byte, error)
moduledataSection() string
getBuildID() (string, error)
getFile() *os.File
getReader() io.ReaderAt
getParsedFile() any
getDwarf() (*dwarf.Data, error)
}
Expand Down
11 changes: 6 additions & 5 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ package gore
import (
"debug/dwarf"
"debug/elf"
"debug/macho"
"debug/pe"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/blacktop/go-macho"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -170,9 +172,8 @@ func TestGoldFiles(t *testing.T) {
require.NotNil(version, "Version should not be nil")
assert.Equal("go"+actualVersion, version.Name, "Incorrect version for "+file)

osFile := f.GetFile()
require.NotNil(osFile, "File should not be nil")
assert.IsType(&os.File{}, osFile, "File should be of type *os.File")
reader := f.GetReader()
require.NotNil(reader, "File should not be nil")

switch f.GetParsedFile().(type) {
case *elf.File:
Expand Down Expand Up @@ -221,7 +222,7 @@ type mockFileHandler struct {
mGetSectionDataFromAddress func(uint64) (uint64, []byte, error)
}

func (m *mockFileHandler) getFile() *os.File {
func (m *mockFileHandler) getReader() io.ReaderAt {
panic("not implemented")
}

Expand Down
Loading
Loading