Skip to content

Commit

Permalink
fix: error handling by replacing SupportsPrebuiltLoaderSet with `S…
Browse files Browse the repository at this point in the history
…upportsProgramTrie`
  • Loading branch information
blacktop committed Sep 1, 2024
1 parent efa4b09 commit 179656b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
8 changes: 6 additions & 2 deletions cmd/ipsw/cmd/dyld/dyld_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ var ImageCmd = &cobra.Command{
if pbl, err := f.GetDylibPrebuiltLoader(image.Name); err == nil {
fmt.Println(pbl.String(f))
} else {
if !errors.Is(err, dyld.ErrPrebuiltLoaderSetNotSupported) {
if errors.Is(err, dyld.ErrPrebuiltLoaderSetNotSupported) {
log.Warn("prebuilt loader sets not supported for this version of dyld_shared_cache")
} else {
return fmt.Errorf("failed parsing launch loader sets: %v", err)
}
// try to parse the dylib closures using the old iOS14.x method
Expand Down Expand Up @@ -154,7 +156,9 @@ var ImageCmd = &cobra.Command{
if err := f.ForEachLaunchLoaderSetPath(func(execPath string) {
fmt.Println(execPath)
}); err != nil {
if !errors.Is(err, dyld.ErrPrebuiltLoaderSetNotSupported) {
if errors.Is(err, dyld.ErrPrebuiltLoaderSetNotSupported) {
log.Warn("prebuilt loader sets not supported for this version of dyld_shared_cache")
} else {
return fmt.Errorf("failed parsing launch loader sets: %v", err)
}
// try to parse the image array using the old iOS14.x method
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/dsc/dsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func GetDylibsThatImport(f *dyld.File, name string) (*ImportedBy, error) {
}
}

if f.SupportsPrebuiltLoaderSet() {
if f.SupportsProgramTrie() {
if err := f.ForEachLaunchLoaderSet(func(execPath string, pset *dyld.PrebuiltLoaderSet) {
for _, loader := range pset.Loaders {
for _, dep := range loader.Dependents {
Expand Down
7 changes: 0 additions & 7 deletions pkg/dyld/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,14 +961,7 @@ func (f *File) GetDylibsImageArray() error {
var size uint64

if f.Headers[f.UUID].DylibsImageArrayAddr == 0 {
if f.Headers[f.UUID].DylibsPblSetAddr > 0 {
return fmt.Errorf("ipsw cannot parse dylibs image array info for macOS12+/iOS15+ yet 😔")
}
return fmt.Errorf("cache does not contain dylibs image array info")
// } else {
// addr = f.Headers[f.UUID].DylibsPblSetAddr
// size = f.Headers[f.UUID].ProgramsPblSetPoolAddr - f.Headers[f.UUID].DylibsPblSetAddr
// }
} else {
addr = f.Headers[f.UUID].DylibsImageArrayAddr
size = f.Headers[f.UUID].DylibsImageArraySize
Expand Down
18 changes: 7 additions & 11 deletions pkg/dyld/prebuilt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ import (
"github.com/blacktop/go-macho/types"
)

func (f *File) SupportsPrebuiltLoaderSet() bool {
func (f *File) SupportsProgramTrie() bool {
if f.Headers[f.UUID].MappingOffset < uint32(unsafe.Offsetof(f.Headers[f.UUID].ProgramTrieSize)) {
return false
}
// FIXME: REMOVE once I have added iOS 18.x support
if f.Headers[f.UUID].MappingOffset > uint32(unsafe.Offsetof(f.Headers[f.UUID].TPROMappingOffset)) {
return false
}
if f.Headers[f.UUID].ProgramTrieAddr == 0 {
return false
}
return true
}

func (f *File) ForEachLaunchLoaderSet(handler func(execPath string, pset *PrebuiltLoaderSet)) error {
if !f.SupportsPrebuiltLoaderSet() {
return ErrPrebuiltLoaderSetNotSupported
if !f.SupportsProgramTrie() {
return ErrProgramTrieNotSupported
}

uuid, off, err := f.GetOffset(f.Headers[f.UUID].ProgramTrieAddr)
Expand Down Expand Up @@ -74,8 +70,8 @@ func (f *File) ForEachLaunchLoaderSet(handler func(execPath string, pset *Prebui
}

func (f *File) ForEachLaunchLoaderSetPath(handler func(execPath string)) error {
if !f.SupportsPrebuiltLoaderSet() {
return ErrPrebuiltLoaderSetNotSupported
if !f.SupportsProgramTrie() {
return ErrProgramTrieNotSupported
}

uuid, off, err := f.GetOffset(f.Headers[f.UUID].ProgramTrieAddr)
Expand Down Expand Up @@ -104,8 +100,8 @@ func (f *File) ForEachLaunchLoaderSetPath(handler func(execPath string)) error {

// GetLaunchLoaderSet returns the PrebuiltLoaderSet for the given executable app path.
func (f *File) GetLaunchLoaderSet(executablePath string) (*PrebuiltLoaderSet, error) {
if !f.SupportsPrebuiltLoaderSet() {
return nil, ErrPrebuiltLoaderSetNotSupported
if !f.SupportsProgramTrie() {
return nil, ErrProgramTrieNotSupported
}

var psetOffset uint64
Expand Down
1 change: 1 addition & 0 deletions pkg/dyld/prebuilt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
)

var ErrPrebuiltLoaderSetNotSupported = fmt.Errorf("dyld_shared_cache has no launch prebuilt loader set info")
var ErrProgramTrieNotSupported = fmt.Errorf("dyld_shared_cache has no program trie info")

type LoaderRef uint16

Expand Down

0 comments on commit 179656b

Please sign in to comment.