8

I use OpenWRT. it's a linux distribution for embedded systems

I want to know the gcc version used to compile the linux

I made some researchs in the net but without results.

I tried to execute these commands for some existing binary in the linux OpenWRT (like wget)

strings -a <default binary> | grep "GCC"
strings -a <default binary> | grep "gcc"

But I did not get any result

even the

strings -a  /lib/libgcc_s.so.1 | grep  "gcc"
strings -a /lib/libuClibc-0.9.30.1.so | grep   "gcc"

does not give any result

Are there a way to know used gcc to build the whole linux (For both user space and kernel space)?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    The kernel or userspace programs? – tangrs Apr 16 '13 at 09:30
  • There is no way to determine which gcc is used to build the "whole linux". Each executable may have been built with a different gcc, or even a library within the executable was built with one gcc, or it may not have even been a C program at all (C++, Fortran, Java (with gcj), etc). – Andre Kostur Apr 16 '13 at 14:29

2 Answers2

14

For programs, it appears in the .comment section of ELF executables, if your system is using ELF.

$ cat main.c
int main() { }
$ gcc main.c
$ objdump -s -j .comment a.out

a.out:     file format elf64-x86-64

Contents of section .comment:
 0000 00474343 3a202844 65626961 6e20342e  .GCC: (Debian 4.
 0010 372e322d 35292034 2e372e32 00474343  7.2-5) 4.7.2.GCC
 0020 3a202844 65626961 6e20342e 342e372d  : (Debian 4.4.7-
 0030 33292034 2e342e37 00                 3) 4.4.7.       

The compiler used to compile the kernel is available from the string in /proc/version, for example:

$ cat /proc/version
Linux version 3.8.5 (...) (gcc version 4.7.2 (Debian 4.7.2-5) ) ...

A major caveat

The .comment section is optional. Many distributions will strip it from the executable when the executable is bundled into a package. The section will be placed in a separate debug package.

For example, on my system:

$ objdump -s -j .comment /usr/lib/x86_64-linux-gnu/libcurl.so.4.2.0
/usr/lib/x86_64-linux-gnu/libcurl.so.4.2.0:     file format elf64-x86-64

objdump: section '.comment' mentioned in a -j option, but not found in any input
file

After installing the libcurl3-dbg package, we get an image with the stripped sections by following the GNU debug link:

$ objdump -s -j .comment  \
    /usr/lib/debug/.build-id/8c/4ae0ad17a4e76bab47c487047490061bd49de3.debug

/usr/lib/debug/.build-id/8c/4ae0ad17a4e76bab47c487047490061bd49de3.debug:
    file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 20284465 6269616e 20342e37  GCC: (Debian 4.7
 0010 2e322d35 2920342e 372e3200           .2-5) 4.7.2.    
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    any way the `cat /proc/version` helped me (+1) to find out the gcc used to build the kernel and I think it's the same used to build the user space for OpenWRT – MOHAMED Apr 16 '13 at 10:00
  • @MOHAMED: I didn't notice the `-a`. Anyway, your binaries are probably stripped, you'll have to get the version from the debug files. – Dietrich Epp Apr 16 '13 at 10:02
  • where I can find the debug files ? – MOHAMED Apr 16 '13 at 10:03
  • @MOHAMED: It depends on the distribution. They don't necessarily exist, and might not exist for all packages. – Dietrich Epp Apr 16 '13 at 19:31
1

For building the OpenWRT workspace your main gcc is used:

gcc --version

For cross compile all the needed tools are located under you openwrt build dir.

The gcc used during the compile can be found in the staging directory of OpenWRT. Go to you openwrt home directory and look for the toolchain directory under the staging dir. Here you will find a bin directory, where all the cross-compile tools are located. For example for ar71xx:

$ ./staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc --version
mips-openwrt-linux-gcc (OpenWrt/Linaro GCC 4.6-2013.05 r57678) 4.6.4
molnarg
  • 445
  • 4
  • 8