Skip to content

Commit

Permalink
refactor: removes debug/macho
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Oct 16, 2024
1 parent 5522c9f commit 4718353
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
17 changes: 4 additions & 13 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 Down Expand Up @@ -483,21 +483,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)
mf := f.fh.getParsedFile().(*macho.File)
fixups, err := mf.DyldChainedFixups()
if err != nil {
return 0, err
}

f2, err := macho2.NewFile(of)
if err != nil {
return 0, err
}
fixups, err := f2.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
3 changes: 2 additions & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package gore
import (
"debug/dwarf"
"debug/elf"
"debug/macho"
"debug/pe"
"errors"
"os"
Expand All @@ -30,6 +29,8 @@ import (
"strings"
"testing"

"github.com/blacktop/go-macho"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
113 changes: 106 additions & 7 deletions macho.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
package gore

import (
"bytes"
"cmp"
"compress/zlib"
"debug/dwarf"
"debug/macho"
"encoding/binary"
"fmt"
"io"
"os"
"slices"
"strings"
"sync"

"github.com/blacktop/go-macho"
"github.com/blacktop/go-macho/types"
)

func openMachO(fp string) (*machoFile, error) {
Expand Down Expand Up @@ -133,7 +140,13 @@ func (m *machoFile) getSectionDataFromAddress(address uint64) (uint64, []byte, e
}

func (m *machoFile) getSectionData(s string) (uint64, []byte, error) {
section := m.file.Section(s)
var section *types.Section
for _, sect := range m.file.Sections {
if sect.Name == s {
section = sect
break
}
}
if section == nil {
return 0, nil, ErrSectionDoesNotExist
}
Expand All @@ -146,14 +159,14 @@ func (m *machoFile) getFileInfo() *FileInfo {
ByteOrder: m.file.ByteOrder,
OS: "macOS",
}
switch m.file.Cpu {
case macho.Cpu386:
switch m.file.CPU {
case types.CPUI386:
fi.WordSize = intSize32
fi.Arch = Arch386
case macho.CpuAmd64:
case types.CPUAmd64:
fi.WordSize = intSize64
fi.Arch = ArchAMD64
case macho.CpuArm64:
case types.CPUArm64:
fi.WordSize = intSize64
fi.Arch = ArchARM64
default:
Expand All @@ -178,6 +191,92 @@ func (m *machoFile) getBuildID() (string, error) {
return parseBuildIDFromRaw(data)
}

// getDwarf mostly a copy of github.com/blacktop/go-macho.File.DWARF() function
// removes dependency on github.com/blacktop/go-dwarf package
func (m *machoFile) getDwarf() (*dwarf.Data, error) {
return m.file.DWARF()
dwarfSuffix := func(s *types.Section) string {
switch {
case strings.HasPrefix(s.Name, "__debug_"):
return s.Name[8:]
case strings.HasPrefix(s.Name, "__zdebug_"):
return s.Name[9:]
default:
return ""
}
}
sectionData := func(s *types.Section) ([]byte, error) {
b, err := s.Data()
if err != nil && uint64(len(b)) < s.Size {
return nil, err
}

if len(b) >= 12 && string(b[:4]) == "ZLIB" {
dlen := binary.BigEndian.Uint64(b[4:12])
dbuf := make([]byte, dlen)
r, err := zlib.NewReader(bytes.NewBuffer(b[12:]))
if err != nil {
return nil, err
}
if _, err := io.ReadFull(r, dbuf); err != nil {
return nil, err
}
if err := r.Close(); err != nil {
return nil, err
}
b = dbuf
}
return b, nil
}

// There are many other DWARF sections, but these
// are the ones the debug/dwarf package uses.
// Don't bother loading others.
var dat = map[string][]byte{"abbrev": nil, "info": nil, "str": nil, "line": nil, "ranges": nil}
for _, s := range m.file.Sections {
suffix := dwarfSuffix(s)
if suffix == "" {
continue
}
if _, ok := dat[suffix]; !ok {
continue
}
b, err := sectionData(s)
if err != nil {
return nil, err
}
dat[suffix] = b
}

d, err := dwarf.New(dat["abbrev"], nil, nil, dat["info"], dat["line"], nil, dat["ranges"], dat["str"])
if err != nil {
return nil, err
}

// Look for DWARF4 .debug_types sections and DWARF5 sections.
for i, s := range m.file.Sections {
suffix := dwarfSuffix(s)
if suffix == "" {
continue
}
if _, ok := dat[suffix]; ok {
// Already handled.
continue
}

b, err := sectionData(s)
if err != nil {
return nil, err
}

if suffix == "types" {
err = d.AddTypes(fmt.Sprintf("types-%d", i), b)
} else {
err = d.AddSection(".debug_"+suffix, b)
}
if err != nil {
return nil, err
}
}

return d, nil
}
2 changes: 1 addition & 1 deletion symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var ErrSymbolNotFound = errors.New("symbol not found")

// Symbol A generic representation of [debug/elf.Symbol], [debug/pe.Symbol], and [debug/macho.Symbol].
// Symbol A generic representation of [debug/elf.Symbol], [debug/pe.Symbol], and [github.com/blacktop/go-macho.Symbol].
type Symbol struct {
// Name of the symbol.
Name string
Expand Down

0 comments on commit 4718353

Please sign in to comment.