GCC – Dump default compile macros

Use following commands if you want to see default macros passed by the compiler  during compilation:

# Compiler default macros in standard build
gcc -dM -E - < /dev/null

# Compiler default macros with optimization level 0
gcc -dM -E -O0 - < /dev/null

# Compiler default macros in case of c++11
g++ -dM -E -x c++ -std=c++11 - < /dev/null
Advertisements

GCC multilib

Compiler comes with different versions of the same library. This support is known as compiler multilib supports. The compiler will choose a specific version of the targeted library depending upon the compiler options used to build source code.

Let’s see how compiler manages this multilib support:

Find out compiler version

$ arm-none-linux-gnueabi-gcc --version
arm-none-linux-gnueabi-gcc (Sourcery CodeBench 2015.12-159) 5.2.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

Find out supported multilibs

$ arm-none-linux-gnueabi-gcc -print-multi-lib
.;
armv4t;@march=armv4t
vfp;@mfloat-abi=softfp
be;@mbig-endian
vfp-be;@mfloat-abi=softfp@mbig-endian
armv7-a-neon;@march=armv7-a@mfloat-abi=softfp@mfpu=neon
armv7-a-hard;@march=armv7-a@mfloat-abi=hard@mfpu=neon
thumb2;@mthumb@march=armv7-a
thumb2-be;@mthumb@march=armv7-a@mbig-endian
thumb2-neon;@mthumb@march=armv7-a@mfloat-abi=softfp@mfpu=neon

Let’s take a single line of the information shown by the compiler to understand what’s going on
armv4t;@march=armv4t

Compiler will use libraries from the armv4t folder if source code is compiled using -march=armv4t.

armv7-a-hard;@march=armv7-a@mfloat-abi=hard@mfpu=neon

Compiler will use libraries from armv7-a-hard folder if source code is compiled using -march=armv7-a -mfloat-abi=hard -mfpu=neon.

You can also find out directory using another trick:

$ arm-none-linux-gnueabi-gcc -print-multi-directory -march=armv4t
armv4t

$ arm-none-linux-gnueabi-gcc -print-multi-directory -march=armv7-a -mfloat-abi=hard -mfpu=neon
armv7-a-hard

$ arm-none-linux-gnueabi-gcc --print-multi-directory -mthumb -march=armv7e-m -mfloat-abi=softfp -mfpu=vfpv3-d16
vfp

There are some options supported by GCC to play with multi-lib. https://gcc.gnu.org/onlinedocs/gccint/Target-Fragment.html (Look for MULTILIB_)

 

Generate assembly code using GCC

To generate assembly code using GCC, use following command:

gcc -S {XYZ GCC compiler flags} -c test.c

{XYZ GCC compiler flags} –> Feel free to add other compiler options as per your requirement. Above command will generate test.s for assembly code.

To mix C language code alongwith assembly language for ease of code review/understanding, use following command:

gcc -g -Wa,-a,-ad {XYZ GCC compiler flags} -c test.c > test.list

test.list will have C & Assembly language.

-march=native @ GCC

The native Linux/GNU toolchain GCC got an option where you map architecture flag to native (-march=native). It allows compiler to generate optimized code supported by the underlying targeted machine.

To determine which other options are enabled on passing -march=native, use following command:

gcc -Q --help=target -march=native

Following command can be useful too to see help

gcc --help=target -march=native

Link: https://gcc.gnu.org/onlinedocs/gcc-7.3.0/gcc/Overall-Options.html#Overall-Options