Why does the comment section of an elf binary contain multiple compiler comments?

Viewed 475

When I compile a binary with the clang compiler shipped in my distro, then read the ELF headers of said binary with readelf -p .comment <path>, it shows me multiple compilers in the comment section:

String dump of section '.comment':
  [     0]  GCC: (GNU) 7.3.0
  [    11]  GCC: (GNU) 10.3.0
  [    23]  clang version 13.0.0

I expected it to show the clang version, but why are there also comments about gcc, and different versions non the less?

1 Answers

why are there also comments about gcc,

Your linked binary contains object files compiled by the 3 different compilers.

  • Your own object files are compiled by clang.
  • The object files which part of GLIBC, such as crt0.o, are (likely) compiled by GCC.
  • You are linking in some library(ies) compiled by a different version of GCC.

You can figure out all files the linker uses by linking with clang ... -Wl,-t flag. Once you know all the files participating in the link, you can run readelf -p.comment on each one to figure out what files where compiled with a given version of the compiler.

Related