How to inspect the content of Go package/object files (.a
, .o
)
The only thing I found is showing the full disassembly with go tool objdump
.
But how to show the file structure, like imported and exported symbols, code, data and other sections, other metadata etc.?
Golang offers a vast inbuilt library that can be used to perform read and write operations on files. In order to read from files on the local system, the io/ioutil module is put to use. The io/ioutil module is also used to write content to the file.
This happens when the "package main" exist in more than 1 file, then you can't just simply run e.g. "go run main.go" or "go run app.go", you will need to run "go run ." === "go run file1.go file2.go file3.go..." === "go run *.go" Thanks for this answer.
A package can have many files but only one file with main function, since that file will be the entry point of the execution. If a package does not contain a file with main package declaration, then Go creates a package archive ( . a ) file inside pkg directory. Since, app is not an executable package, it created app.
To write to files in Go, we use the os , ioutil , and fmt packages. The functions that we use typically return the number of bytes written and an error, if any. We use Go version 1.18.
Compiled Go packages are stored in Unix ar archive files, so you can extract the files within them using ar
or go tool pack x pkg.a
.
Package files contain compiled code (_go_.o
), used by the linker to build binaries, and export data (__.PKGDEF
), used by the compiler when building dependent packages. If the package contained assembly files or cgo code, there may be additional .o
files, too.
You can use golang.org/x/tools/go/gcexportdata
to read export data into a format that can be understood by go/types
.
It sounds like you've already found go tool objdump
for disassembly. You might also find go tool nm
useful for listing symbols.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With