Skip to content

Commit

Permalink
Merge pull request #22 from andreabolognani/ldd-usr-lib
Browse files Browse the repository at this point in the history
Look for libraries in /usr/lib too
  • Loading branch information
rmohr authored Sep 5, 2022
2 parents 2d3e53c + 8893816 commit 58fd7d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/ldd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewLddCmd() *cobra.Command {
}

files := []string{}
dependencies, err := ldd.Resolve(objects, filepath.Join(tmpRoot, "/usr/lib64"))
dependencies, err := ldd.Resolve(objects, []string{filepath.Join(tmpRoot, "/usr/lib64"), filepath.Join(tmpRoot, "/usr/lib")})
if err != nil {
return err
}
Expand Down
34 changes: 23 additions & 11 deletions pkg/ldd/ldd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ldd

import (
"debug/elf"
"fmt"
"os"
"path/filepath"
)

func Resolve(objects []string, library_path string) (finalFiles []string, err error) {
func Resolve(objects []string, library_path []string) (finalFiles []string, err error) {
discovered := map[string]struct{}{}
for _, obj := range objects {
if files, err := resolve(obj, library_path); err != nil {
Expand All @@ -23,7 +24,7 @@ func Resolve(objects []string, library_path string) (finalFiles []string, err er
return
}

func resolve(library string, library_path string) ([]string, error) {
func resolve(library string, library_path []string) ([]string, error) {

next := []string{library}
processed := map[string]struct{}{}
Expand Down Expand Up @@ -61,7 +62,7 @@ func resolve(library string, library_path string) ([]string, error) {
return finalFiles, nil
}

func ldd(library string, library_path string) (discovered []string, err error) {
func ldd(library string, library_path []string) (discovered []string, err error) {
bin, err := elf.Open(library)
if err != nil {
return nil, err
Expand All @@ -71,16 +72,27 @@ func ldd(library string, library_path string) (discovered []string, err error) {
return nil, err
}
for _, l := range libs {
_, err := os.Stat(filepath.Join(library_path, l))
if err != nil {
return nil, err
found := false
for _, dir := range library_path {
_, err := os.Stat(filepath.Join(dir, l))
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
discovered = append(discovered, filepath.Join(dir, l))
symlinks, err := followSymlinks(filepath.Join(dir, l))
if err != nil {
return nil, err
}
discovered = append(discovered, symlinks...)
found = true
break
}
discovered = append(discovered, filepath.Join(library_path, l))
symlinks, err := followSymlinks(filepath.Join(library_path, l))
if err != nil {
return nil, err
if !found {
return nil, fmt.Errorf("%v not found in any of %v", l, library_path)
}
discovered = append(discovered, symlinks...)
}
return discovered, nil
}
Expand Down

0 comments on commit 58fd7d2

Please sign in to comment.