Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect Go package files (.a files)?

Tags:

go

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

like image 973
Ivan Avatar asked Jun 25 '21 15:06

Ivan


People also ask

How do I read and write files in Golang?

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.

How do I run all files in Go?

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.

Can a Go package have multiple files?

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.

How do I write to a file in Go?

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.


1 Answers

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.

like image 126
Jay Conrod Avatar answered Sep 28 '22 00:09

Jay Conrod