6

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.

ZDunker
  • 437
  • 1
  • 6
  • 18

1 Answers1

14

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)
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • Can we merge relocatable object compiled using different GCC version into a single SO? If yes, does that imply that the SO can be linked using multiple GCC versions? Thanks. – Wilson Nov 05 '18 at 02:15
  • 1
    @Wilson I do not have a definitive answer for that, but see https://stackoverflow.com/questions/2801938/gcc-abi-compatibility . I believe that C relocatable objects that have been compiled PIC and whose dependent libraries are either identical or compatible can be linked into a single shared object if the gcc versions are different as long as they're all 4.7.0 or later. (5.1.0 or later if C++ is involved). – Mark Plotnick Nov 05 '18 at 18:36
  • "The GNU loader will merge...": is this not the linker that does that? – René Nyffenegger Feb 10 '19 at 17:48
  • @RenéNyffenegger Quite right. I am old and still call `ld` the loader. Fixed. Thanks. – Mark Plotnick Feb 10 '19 at 20:18