/
main.go
103 lines (99 loc) · 2.79 KB
/
main.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"debug/elf"
"fmt"
"os"
glibccheck "github.com/guseggert/glibc-check"
"github.com/spf13/cobra"
)
var rootCmd = cobra.Command{
Use: "glibc-check",
Short: "tools for checking glibc versions on an executable",
}
func main() {
rootCmd.AddCommand(&cobra.Command{
Use: "list-versions",
Short: "list the glibc versions",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
versions, err := glibccheck.ParseFile(args[0])
// if the binary is statically linked, don't fail
if err != nil && err != elf.ErrNoSymbols {
return err
}
for _, v := range versions {
fmt.Println(v.String())
}
return nil
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "min filename",
Short: "print the min glibc version",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
versions, err := glibccheck.ParseFile(args[0])
if err != nil {
return err
}
if len(versions) == 0 {
fmt.Fprintln(os.Stderr, "no glibc versions found")
os.Exit(1)
}
fmt.Println(versions[0])
return nil
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "max filename",
Short: "print the max glibc version",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
versions, err := glibccheck.ParseFile(args[0])
if err != nil {
return err
}
if len(versions) == 0 {
fmt.Fprintln(os.Stderr, "no glibc versions found")
os.Exit(1)
}
fmt.Println(versions[len(versions)-1])
return nil
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "assert-all condition filename",
Short: "asserts that the given condition holds for every glibc version in the file",
Long: "The variables 'major', 'minor', and 'patch' are bound for the current version, 'patch' is bound to 0 for versions without patch versions, such as '2.32'. Example: 'glibc-check assert-all 'major == 2 && minor > 10'",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
versions, err := glibccheck.ParseFile(args[1])
if err != nil {
// if the binary is statically linked, there's nothing to check
if err == elf.ErrNoSymbols {
return nil
}
return err
}
violations, err := versions.FindViolations(args[0])
if len(violations) > 0 {
fmt.Fprintf(os.Stderr, "condition did not hold for versions: %s\n", violations)
os.Exit(len(violations))
}
return nil
},
})
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}