diff --git a/main.go b/main.go index ac07229..708c6e2 100644 --- a/main.go +++ b/main.go @@ -730,11 +730,6 @@ func transformGo(file *ast.File, info *types.Info, blacklist map[types.Object]st } obj := info.ObjectOf(node) if obj == nil { - switch cursor.Parent().(type) { - case *ast.AssignStmt: - // symbolic var v in v := expr.(type) - node.Name = hashWith(buildInfo.buildID, node.Name) - } return true } pkg := obj.Pkg() @@ -771,8 +766,17 @@ func transformGo(file *ast.File, info *types.Info, blacklist map[types.Object]st // encoding/json without struct tags return true } - case *types.Const: + + if obj.Parent() != pkg.Scope() { + // identifiers of non-global variables never show up in the binary + return true + } + case *types.TypeName: + if obj.Parent() != pkg.Scope() { + // identifiers of non-global types never show up in the binary + return true + } case *types.Func: sign := obj.Type().(*types.Signature) if obj.Exported() && sign.Recv() != nil { diff --git a/testdata/scripts/syntax.txt b/testdata/scripts/syntax.txt index 77c8b64..7173a59 100644 --- a/testdata/scripts/syntax.txt +++ b/testdata/scripts/syntax.txt @@ -1,9 +1,20 @@ -garble build main.go -exec ./main +go build +exec ./main$exe cmp stderr main.stderr -! binsubstr main$exe 'valuable information' +binsubstr main$exe 'globalVar' # 'globalType' only matches on go < 1.15 +! binsubstr main$exe 'localName' 'globalConst' +garble -debugdir=debug build +exec ./main$exe +cmp stderr main.stderr + +! binsubstr main$exe 'localName' 'globalConst' 'globalVar' 'globalType' 'valuable information' + +binsubstr debug/main/z1.go 'localName' 'globalConst' + +-- go.mod -- +module test/main -- main.go -- package main @@ -53,7 +64,41 @@ func main() { enc, _ := json.Marshal(EncodingT{Foo: 3}) println(string(enc)) + scopesTest() +} + +-- scopes.go -- +package main + +const globalConst = 1 + +type globalType int + +var ( + globalVar = 1 + globalVarTyped globalType = 1 +) + +func scopesTest() { + println(globalVar, globalConst, globalVarTyped) + const localNameConst = 1 + + localNameShort := 4 + + type localNameType int + + var ( + localNameVar = 5 + localNameTypeVar localNameType = 1 + ) + + println(localNameConst, localNameShort, localNameVar, localNameTypeVar, input("input")) } + +func input(localNameParam string) (localNameReturn string) { return localNameParam } + -- main.stderr -- nil case {"Foo":3} +1 1 1 +1 4 5 1 input