views:

85

answers:

3

On Linux, is there a way to embed version information into an ELF binary? I would like to embed this info at compile time so it can then be extract it using a script later. A hackish way would be to plant something that can be extracted using the strings command. Is there a more conventional method, similar to how Visual Studio plant version info for Windows DLLs (note version tab in DLL properties)?

+2  A: 

One way to do it if using cvs or subversion is to have a special id string formatted specially in your source file. Then add a pre-commit hook to cvs or svn that updates that special variable with the new version of the file when a change is committed. Then, when the binary is built, you can use ident to extract that indformation. For example:

Add something like this to your cpp file:

static char fileid[] = "$Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $";

And running ident (which you can find by installing rcs) on the program should show the info about the files that have an id string in them.

ident program
program:
    $Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $
sashang
+1: I'd never come across ident before, most useful.
High Performance Mark
+1  A: 

The Intel Fortran and C++ compilers can certainly do this, use the -sox option. So, yes there is a way. I don't know of any widespread convention for embedding such information in a binary and I generally use Emacs in hexl-mode for reading the embedded information, which is quite hackish.

'-sox' also embeds the compiler options used to build an executable, which is very useful.

High Performance Mark
+1  A: 

If you declare a variable called program_version or similar you can find out at which address the variable is stored at and then proceed to extract its value. E.g.

objdump -t --demangle /tmp/libtest.so | grep program_version
0000000000600a24 g     O .data  0000000000000004              program_version

tells me that program_version resides at address 0000000000600a24 and is of size 4. Then just read the value at that address in the file.

Or you could just write a simple program that links the library in questions and prints the version, defined either as an exported variable or a function.

Staffan