Skip to content

Commit

Permalink
Add the parents of imported libraries to links
Browse files Browse the repository at this point in the history
  • Loading branch information
elwinar committed Sep 1, 2020
1 parent e11a3d1 commit 3d55c0d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
31 changes: 29 additions & 2 deletions bin/ldd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ func main() {

executable := flag.Arg(0)
file, err := elfx.Open(executable)
defer file.Close()
if err != nil {
fmt.Println("opening binary file:", err)
os.Exit(1)
}

libraries, err := file.ImportedLibraries()
stack, err := file.ImportedLibraries()
if err != nil {
fmt.Println("parsing imported libraries:", err)
os.Exit(1)
}

for _, library := range libraries {
var known = make(map[string]struct{})
for _, l := range stack {
known[l] = struct{}{}
}

for len(stack) != 0 {
var library string
library, stack = stack[0], stack[1:]

path, ok, err := file.ResolveImportedLibrary(library)
if err != nil {
fmt.Printf("%s: error while resolving: %s\n", library, err)
Expand All @@ -43,5 +52,23 @@ func main() {
}

fmt.Printf("%s => %s\n", library, path)

libfile, err := elfx.Open(path)
if err != nil {
fmt.Println("opening library", library, "file:", err)
os.Exit(1)
}

libparents, err := libfile.ImportedLibraries()
if err != nil {
fmt.Println("parsing library", library, "parents:", err)
}
for _, l := range libparents {
if _, ok := known[l]; ok {
continue
}
stack = append(stack, l)
known[l] = struct{}{}
}
}
}
38 changes: 38 additions & 0 deletions bin/rcoredump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,20 @@ func (s *service) resolveLinks(executable string) ([]Link, error) {
}

links := make([]Link, 0, len(libraries))

// Maintain a map of the known libraries, so the recursive search for
// parent libraries doesn't do extra unnecessary work.
known := make(map[string]struct{})
for _, library := range libraries {
known[library] = struct{}{}
}

// For each library of the stack, locate it and add it to the links,
// then find its parents and add them to the stack.
for len(libraries) != 0 {
var library string
library, libraries = libraries[0], libraries[1:]

path, ok, err := f.ResolveImportedLibrary(library)
var errMsg string
if err != nil {
Expand All @@ -348,6 +361,31 @@ func (s *service) resolveLinks(executable string) ([]Link, error) {
Found: ok,
Error: errMsg,
})

// If there was an error while locating the library, don't try
// to find its parents.
if !ok || err != nil {
continue
}

libf, err := elfx.Open(path)
if err != nil {
return nil, wrap(err, "opening shared library %s (%q)", library, path)
}

parents, err := libf.ImportedLibraries()
if err != nil {
return nil, wrap(err, "parsing parent imported libraries for %s (%q)", library, path)
}

for _, parent := range parents {
if _, ok := known[parent]; ok {
continue
}

libraries = append(libraries, parent)
known[parent] = struct{}{}
}
}

return links, nil
Expand Down

0 comments on commit 3d55c0d

Please sign in to comment.