Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a library is compiled with certain GCC version

Tags:

linux

gcc

I'm compiling OpenSSL on Unix-machine. The default compiler is GCC-4.4.7. I put an alternative compiler GCC-5.2.0 in another folder. I usually will prepend the path of alternative compiler to $PATH so that gcc will always use the one in alternative path.

But now I'm uncertain with the library I compiled, is there a way to tell which gcc is used for compiling my library? A workaround on the .o files is appreciated as well.

like image 222
ZDunker Avatar asked Mar 15 '17 19:03

ZDunker


1 Answers

The gcc and clang compiler suites will put a version string in the .comment section of an ELF file. (If you generate an intermediate assembly language file, you can see this string as an .ident directive).

The GNU linker will merge all the .comment sections of its input object files into a single section, eliminating any duplicates.

You can read this section by running readelf -p .comment /path/to/your/objectfile. For example, here is an executable made from two relocatable object files, one compiled with gcc and the other with clang:

$ readelf -p .comment hello

String dump of section '.comment':
  [     0]  GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005
  [    2d]  clang version 3.8.1-12ubuntu1 (tags/RELEASE_381/final)
like image 153
Mark Plotnick Avatar answered Sep 23 '22 01:09

Mark Plotnick