I am trying to figure out how to access the build-id generated by the linker at runtime.
From this page, https://linux.die.net/man/1/ld
When I build a test program like:
% gcc test.c -o test -Wl,--build-id=sha1
I can see that the build ID is present in the binary:
% readelf -n test
Displaying notes found in: .note.gnu.build-id
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
Build ID: 85aa97bd52ddc4dc2a704949c2545a3a9c69c6db
I would like to print this at run-time.
EDIT: Assume you can't access the elf file from which the running process was loaded (permissions, embedded/no filesystem, etc).
EDIT: the accepted answer works, but the linker does not necessarily have to place the variable at the end of the section. If there were a way to get a pointer to the start of the section, that would be more reliable.
--build-id
can beSHA1
, orMD5
, or an arbitrary hex value. – Rudie