2024-10-09 20:39:27 +09:00
|
|
|
package ldd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Location uint64 `json:"location"`
|
|
|
|
}
|
|
|
|
|
2024-10-09 23:51:15 +09:00
|
|
|
func Parse(stdout fmt.Stringer) ([]*Entry, error) {
|
|
|
|
payload := strings.Split(strings.TrimSpace(stdout.String()), "\n")
|
2024-10-09 20:39:27 +09:00
|
|
|
result := make([]*Entry, len(payload))
|
|
|
|
|
|
|
|
for i, ent := range payload {
|
|
|
|
if len(ent) == 0 {
|
2024-10-09 23:51:15 +09:00
|
|
|
return nil, ErrUnexpectedNewline
|
2024-10-09 20:39:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
segment := strings.SplitN(ent, " ", 5)
|
|
|
|
|
2024-10-09 23:51:15 +09:00
|
|
|
// location index
|
2024-10-09 20:39:27 +09:00
|
|
|
var iL int
|
|
|
|
|
|
|
|
switch len(segment) {
|
|
|
|
case 2: // /lib/ld-musl-x86_64.so.1 (0x7f04d14ef000)
|
|
|
|
iL = 1
|
|
|
|
result[i] = &Entry{Name: segment[0]}
|
|
|
|
case 4: // libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f04d14ef000)
|
|
|
|
iL = 3
|
|
|
|
if segment[1] != "=>" {
|
|
|
|
return nil, ErrUnexpectedSeparator
|
|
|
|
}
|
|
|
|
if !path.IsAbs(segment[2]) {
|
|
|
|
return nil, ErrPathNotAbsolute
|
|
|
|
}
|
|
|
|
result[i] = &Entry{
|
|
|
|
Name: segment[0],
|
|
|
|
Path: segment[2],
|
|
|
|
}
|
|
|
|
default:
|
2024-10-09 23:51:15 +09:00
|
|
|
return nil, EntryUnexpectedSegmentsError(ent)
|
2024-10-09 20:39:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if loc, err := parseLocation(segment[iL]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
result[i].Location = loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLocation(s string) (uint64, error) {
|
|
|
|
if len(s) < 4 || s[len(s)-1] != ')' || s[:3] != "(0x" {
|
|
|
|
return math.MaxUint64, ErrBadLocationFormat
|
|
|
|
}
|
|
|
|
return strconv.ParseUint(s[3:len(s)-1], 16, 64)
|
|
|
|
}
|