Skip to content

Commit

Permalink
proc: fix dynamic library loading with musl loader
Browse files Browse the repository at this point in the history
With the glibc loader the link map entry for the static executable has
an empty name field and address equal to 0x0. This case was already
handled by the check in bininfo.go AddImage for names to be valid
paths.
With the musl loader however the first entry, corresponding to the
static executable, has a valid path with address equal to 0x0, since we
record a real address for the image corresponding to the static
executable this results in having two entries for the executable when
musl is used to link go programs.

Change the code scanning the debug link map so that the first entry is
skipped if it has address equal to zero.

Fixes #3617
  • Loading branch information
aarzilli committed Jan 3, 2024
1 parent 3372f5c commit 61dbd1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/proc/linutil/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func ElfUpdateSharedObjects(p proc.Process) error {

libs := []string{}

first := true

for {
if r_map == 0 {
break
Expand All @@ -176,8 +178,13 @@ func ElfUpdateSharedObjects(p proc.Process) error {
if err != nil {
return err
}
bi.AddImage(lm.name, lm.addr)
if !first || lm.addr != 0 {
// First entry is the executable, we don't need to add it, and doing so
// can cause duplicate entries due to base address mismatches.
bi.AddImage(lm.name, lm.addr)
}
libs = append(libs, lm.name)
first = false
r_map = lm.next
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/terminal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ func libraries(t *Term, ctx callContext, args string) error {
for i := range libs {
fmt.Fprintf(t.stdout, "%"+strconv.Itoa(d)+"d. %#x %s\n", i, libs[i].Address, libs[i].Path)
if libs[i].LoadError != "" {
fmt.Fprintf(t.stdout, " Load error: %s", libs[i].LoadError)
fmt.Fprintf(t.stdout, " Load error: %s\n", libs[i].LoadError)
}
}
return nil
Expand Down

0 comments on commit 61dbd1a

Please sign in to comment.