diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e14c665 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/garble diff --git a/main.go b/main.go index 5a07aa2..c8093d1 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,8 @@ Usage of garble: func main() { os.Exit(main1()) } +var workingDir string + func main1() int { if err := flagSet.Parse(os.Args[1:]); err != nil { return 2 @@ -36,14 +38,19 @@ func main1() int { if len(args) < 1 { flagSet.Usage() } + var err error + workingDir, err = os.Getwd() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + _, tool := filepath.Split(args[0]) // TODO: trim ".exe" for windows? transformed := args[1:] - // fmt.Fprintln(os.Stderr, tool, transformed) + // log.Println(tool, transformed) if transform := transformFuncs[tool]; transform != nil { - var err error - transformed, err = transform(transformed) - if err != nil { + if transformed, err = transform(transformed); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } @@ -60,7 +67,7 @@ func main1() int { var transformFuncs = map[string]func([]string) ([]string, error){ "compile": transformCompile, - "link": transformLink, + "link": transformLink, } func transformCompile(args []string) ([]string, error) { @@ -70,16 +77,10 @@ func transformCompile(args []string) ([]string, error) { return args, nil } - // TODO: find a way to do this. -trimpath is always present for some reason. - // trimpath := false - // for _, flag := range flags { - // if strings.HasPrefix(flag, "-trimpath") { - // trimpath = true - // } - // } - // if !trimpath { - // return nil, fmt.Errorf("-toolexec=garble should be used alongside -trimpath") - // } + trimpath := flagValue(flags, "-trimpath") + if !strings.Contains(trimpath, workingDir) { + return nil, fmt.Errorf("-toolexec=garble should be used alongside -trimpath") + } return append(flags, files...), nil } @@ -101,3 +102,17 @@ func splitFlagsFromFiles(args []string, ext string) (flags, files []string) { } return args, nil } + +func flagValue(flags []string, name string) string { + for i, arg := range flags { + if val := strings.TrimPrefix(arg, name+"="); val != arg { + // -name=value + return val + } + if arg == name && i+1 < len(flags) { + // -name value + return flags[i+1] + } + } + return "" +} diff --git a/testdata/scripts/basic.txt b/testdata/scripts/basic.txt index 0fb3ad5..7279bca 100644 --- a/testdata/scripts/basic.txt +++ b/testdata/scripts/basic.txt @@ -15,14 +15,17 @@ exec ./main cmp stderr main.stderr grep $WORK main -# The default compilation includes DWARF and the symbol table. +# The default build includes DWARF and the symbol table. exec readelf --section-details --symbols main stdout 'debug_info$' stdout 'globalVar' -# TODO +# The default build includes full non-trimmed paths. +grep $WORK main + # Check that we fail if the user forgot -trimpath. -# ! exec go build -a -toolexec=garble main.go +! exec go build -a -toolexec=garble main.go +stderr 'should be used alongside -trimpath' # Check that the simplest use of garble works. exec go build -a -trimpath -toolexec=garble main.go @@ -33,7 +36,8 @@ exec readelf --section-details --symbols main ! stdout 'debug_info$' ! stdout 'globalVar' -# ! grep $WORK main +! grep $WORK main + # TODO # ! grep 'globalVar' main # ! grep 'globalFunc' main