There are two places where this information can come from: the pclntab, and DWARF debug information.

The pclntab has a mapping from program counter locations to filenames and line numbers, which makes stack traces more useful. Files which don't contain any code, only things like type definitions, global variables and constants, will not be listed. Since they do not have any mappings in the pclntab. So these files will not be listed here.

The DWARF debug info should also list source files for the binary, among other things. This is a bit more likely to be stripped out to save space, but if present, it's more useful as C-based source files coming from cgo will also be listed. The pclntab should almost always be present and readable, though might be unreachable by `go tool objdump` for dumping if the binary is stripped.

If the binary hasn't been stripped, then extracting this info via DWARF is fairly simple with elfutils[1].

eu-srcfiles -e /path/to/binary

Alternatively, for an unstripped binary via pclntab with the below one-liner:

go tool objdump /path/to/binary | grep '^TEXT ' | grep -o '(SB) .*' | sed 's/^.....//g' | sort -u

For a stripped binary with an untampered pclntab, something could be kludged together with the output of redress[2] (have to parse the output a bit to get the bare paths). Left as an exercise to the reader.
e.g

redress src -suv /path/to/binary

Or just writing something yourself with gore[3] directly.

If the binary's pclntab has been tampered with, e.g by compiling the source code with garble[4], then all bets are off, of course.

[1] https://sourceware.org/elfutils/
[2] https://github.com/goretk/redress
[3] https://github.com/goretk/gore
[4] https://github.com/burrowers/garble

Tharaneedharan Vilwanathan:
Hi All,

I have a quick question.

Is there a way to get the list of source code files of a Go binary?

Here is one solution I got:
$ strings /home/dharani/go1.22.6.linux-amd64/go/bin/gofmt | grep "\.go$"| sort -ur
unicode/utf8/utf8.go
unicode/tables.go
unicode/letter.go
unicode/graphic.go
unicode/digit.go
time/zoneinfo_unix.go
...

But I am wondering if there is a better way. Please let me know.

Regards
dharani

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang- nuts+unsubscr...@googlegroups.com>. To view this discussion visit https://groups.google.com/d/msgid/golang- nuts/CAN- HoC%3DdZW0sRTi4P%3DPjOxupOTrPbQLf10te01OMNotfoOyLRQ%40mail.gmail.com <https://groups.google.com/d/msgid/golang-nuts/CAN- HoC%3DdZW0sRTi4P%3DPjOxupOTrPbQLf10te01OMNotfoOyLRQ%40mail.gmail.com? utm_medium=email&utm_source=footer>.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/741b3ec1-1699-4f3a-b0fc-bc61a5a0b5f5%40gmail.com.

Reply via email to