9

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.?

Ivan
  • 1,552
  • 1
  • 16
  • 25
  • See also "[Easy to read Golang assembly output?](https://stackoverflow.com/a/73001689/6309)", which is not targeted to a `.a` file, but can help nonetheless. – VonC Jul 16 '22 at 05:11

1 Answers1

10

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.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110