aboutsummaryrefslogtreecommitdiffhomepage
path: root/ldd/ldd.go
blob: ca4156c49591056971a9fcd1936b41204dd86df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package ldd retrieves linker information by invoking ldd from glibc or musl and parsing its output.
package ldd

import (
	"math"
	"path"
	"strconv"
	"strings"
)

type Entry struct {
	Name     string `json:"name,omitempty"`
	Path     string `json:"path,omitempty"`
	Location uint64 `json:"location"`
}

func Parse(p []byte) ([]*Entry, error) {
	payload := strings.Split(strings.TrimSpace(string(p)), "\n")
	result := make([]*Entry, len(payload))

	for i, ent := range payload {
		if len(ent) == 0 {
			return nil, ErrUnexpectedNewline
		}

		segment := strings.SplitN(ent, " ", 5)

		// location index
		var iL int

		switch len(segment) {
		case 2: // /lib/ld-musl-x86_64.so.1 (0x7f04d14ef000)
			iL = 1
			result[i] = &Entry{Name: strings.TrimSpace(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: strings.TrimSpace(segment[0]),
				Path: segment[2],
			}
		default:
			return nil, EntryUnexpectedSegmentsError(ent)
		}

		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)
}