Skip to content

Commit

Permalink
Optionally skip printing the function sections (#60)
Browse files Browse the repository at this point in the history
* Optionally skip printing the function sections

Add a new -no-functions command line flag to skip printing the
UserFunctions and StdFunctions detailed sections.
This is useful to get a quick summary of what is in a Go binary.
The default is to always print the function sections.

Reference: #49
Signed-off-by: Philippe Ombredanne <[email protected]>

* Optionally skip printing the function sections

Use -nofuncs command line flag instead.
Reorder main_impl() function arguments.

Reference: #49
Signed-off-by: Philippe Ombredanne <[email protected]>

---------

Signed-off-by: Philippe Ombredanne <[email protected]>
  • Loading branch information
pombredanne authored Aug 2, 2024
1 parent 128f46b commit e3bc188
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
33 changes: 18 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type ExtractMetadata struct {
StdFunctions []FuncMetadata
}

func main_impl_tmpfile(fileBytes []byte, printStdPkgs bool, printFilePaths bool, printTypes bool, manualTypeAddress int, versionOverride string) (metadata ExtractMetadata, err error) {
func main_impl_tmpfile(fileBytes []byte, printStdPkgs bool, printFilePaths bool, printTypes bool, noPrintFunctions bool, manualTypeAddress int, versionOverride string) (metadata ExtractMetadata, err error) {
tmpFile, err := os.CreateTemp(os.TempDir(), "goresym_tmp-")
if err != nil {
return ExtractMetadata{}, fmt.Errorf("failed to create temporary file: %s", err)
Expand All @@ -81,10 +81,10 @@ func main_impl_tmpfile(fileBytes []byte, printStdPkgs bool, printFilePaths bool,
return ExtractMetadata{}, fmt.Errorf("failed to close temporary file: %s", err)
}

return main_impl(tmpFile.Name(), printStdPkgs, printFilePaths, printTypes, manualTypeAddress, versionOverride)
return main_impl(tmpFile.Name(), printStdPkgs, printFilePaths, printTypes, noPrintFunctions, manualTypeAddress, versionOverride)
}

func main_impl(fileName string, printStdPkgs bool, printFilePaths bool, printTypes bool, manualTypeAddress int, versionOverride string) (metadata ExtractMetadata, err error) {
func main_impl(fileName string, printStdPkgs bool, printFilePaths bool, printTypes bool, noPrintFunctions bool, manualTypeAddress int, versionOverride string) (metadata ExtractMetadata, err error) {
extractMetadata := ExtractMetadata{}

file, err := objfile.Open(fileName)
Expand Down Expand Up @@ -286,23 +286,25 @@ restartParseWithRealTextBase:
}
}

for _, elem := range finalTab.ParsedPclntab.Funcs {
if isStdPackage(elem.PackageName()) {
if printStdPkgs {
extractMetadata.StdFunctions = append(extractMetadata.StdFunctions, FuncMetadata{
if !noPrintFunctions {
for _, elem := range finalTab.ParsedPclntab.Funcs {
if isStdPackage(elem.PackageName()) {
if printStdPkgs {
extractMetadata.StdFunctions = append(extractMetadata.StdFunctions, FuncMetadata{
Start: elem.Entry,
End: elem.End,
PackageName: elem.PackageName(),
FullName: elem.Name,
})
}
} else {
extractMetadata.UserFunctions = append(extractMetadata.UserFunctions, FuncMetadata{
Start: elem.Entry,
End: elem.End,
PackageName: elem.PackageName(),
FullName: elem.Name,
})
}
} else {
extractMetadata.UserFunctions = append(extractMetadata.UserFunctions, FuncMetadata{
Start: elem.Entry,
End: elem.End,
PackageName: elem.PackageName(),
FullName: elem.Name,
})
}
}

Expand Down Expand Up @@ -422,6 +424,7 @@ func main() {
printStdPkgs := flag.Bool("d", false, "Print Default Packages")
printFilePaths := flag.Bool("p", false, "Print File Paths")
printTypes := flag.Bool("t", false, "Print types automatically, enumerate typelinks and itablinks")
noPrintFunctions := flag.Bool("nofuncs", false, "Do not print user and standard function sections")
typeAddress := flag.Int("m", 0, "Manually parse the RTYPE at the provided virtual address, disables automated enumeration of moduledata typelinks itablinks")
versionOverride := flag.String("v", "", "Override the automated version detection, ex: 1.17. If this is wrong, parsing may fail or produce nonsense")
humanView := flag.Bool("human", false, "Human view, print information flat rather than json, some information is omitted for clarity")
Expand All @@ -442,7 +445,7 @@ func main() {
os.Exit(1)
}

metadata, err := main_impl(flag.Arg(0), *printStdPkgs, *printFilePaths, *printTypes, *typeAddress, *versionOverride)
metadata, err := main_impl(flag.Arg(0), *printStdPkgs, *printFilePaths, *printTypes, *noPrintFunctions, *typeAddress, *versionOverride)
if err != nil {
fmt.Println(TextToJson("error", fmt.Sprintf("Failed to parse file: %s", err)))
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAllVersions(t *testing.T) {
}

t.Run(versionPath, func(t *testing.T) {
data, err := main_impl(filePath, true, true, true, 0, "")
data, err := main_impl(filePath, true, true, true, true, 0, "")
if err != nil {
t.Errorf("Go %s failed on %s: %s", v, file, err)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func testSymbolRecovery(t *testing.T, workingDirectory string, binaryName string
return
}

data, err := main_impl(filePath, true, true, true, 0, "")
data, err := main_impl(filePath, true, true, true, true, 0, "")
if err != nil {
t.Errorf("GoReSym failed: %s", err)
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestWeirdBins(t *testing.T) {
return
}

_, err := main_impl(filePath, true, true, true, 0, "")
_, err := main_impl(filePath, true, true, true, true, 0, "")
if err == nil {
t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.")
}
Expand All @@ -228,7 +228,7 @@ func TestWeirdBins(t *testing.T) {
return
}

_, err := main_impl(filePath, true, true, true, 0, "")
_, err := main_impl(filePath, true, true, true, true, 0, "")
if err == nil {
t.Errorf("GoReSym found pclntab in a non-go binary, this is not possible.")
}
Expand Down

0 comments on commit e3bc188

Please sign in to comment.