简体   繁体   中英

How to determine the configuration of gcc from a bash script?

I have a bash script that compiles a program as well on older versions of Ubuntu (14.04.x and 16.04.x) than on the last one (18.04.x) and on other distributions (Arch, CentOS, Debian, Fedora, etc.) and therefore... with different gcc settings.

Then, to obtain an executable that can be launched (among other ways) by a double click, I must compile this program without the "-no-pie" option with older versions of gcc setting (Ubuntu 14.04.x and 16.04.x) when I have to use this option "=no-pie" for the new version of the gcc 7.3 setting (on Ubuntu 18.04.x).

The problem is that on the last Ubuntu release (18.04.x) and its derivatives (Kubuntu, Xbuntu, etc. and maybe with other distributions) with the new configuration of gcc (7.3) having the option "--enable-default-pie", if I compile my program without the option "-no-pie", the result is that the file created is an executable which is of the "shared library" type which can not be launched by a double click.

My question is either:

a) Is there a command that allows me to determine from a bash script if gcc is configured with the "--enable-default-pie" setting?

b) if not, is there a command that allows me to determine from a bash script if the compiled file is of the "shared library" or "executable" type?

For this second option, a solution could be how to save the response of "gcc -v" in a .txt file and check if there is the "--enable-default-pie" string but I've absolutely no clue how to do it.

If I there is not an answer to my first option, the second option (it is true less elegant but just as effective) would allow me to compile my program first without the "-no-pie" option, then check the status of such a created executable and if the result is a "shared library", of restart this compilation this time using the option "-no-pie" for, in one case as in the other, get an executable that can be launched by a double click whatever the setting of gcc may be.

Thank you in advance for your time, ideas and suggestions.

Best regards.

Yes, you can check GCC build options with gcc -v or gcc -###

In order to have pretty print you can use:

gcc -### -E 2>&1 | grep "Configured with" | sed 's/--/\n--/g'

So bash oneliner to say you have pie or not may be:

if [[ -n "`gcc -v -E 2>&1 | grep 'Configured with' | sed 's/--/\n--/g' | grep enable-default-spie`" ]]; then echo "PIE DEFAULT"; else echo "PIE NOT DEFAULT"; fi

To check file type just use file command, eg.:

file /usr/bin/x86_64-linux-gnu-gcc-7

/usr/bin/x86_64-linux-gnu-gcc-7: ELF 64-bit LSB executable , x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=02ac46ba938c15f55f6fab165133e0f527bc2197, stripped

file /usr/lib/libchm.so.1.0.0

/usr/lib/libchm.so.1.0.0: ELF 64-bit LSB shared object , x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=7c603d9a0771b5bfd5b869b4446e2f78ef13802a, stripped

File type function

function whatfile { file $1 -i | grep application | sed 's/^.*application\/x-//g;s/,.*$//g'; }

Example output:

aaa@xxx:~ $ whatfile /boot/grub/grub.conf
aaa@xxx:~ $ whatfile /usr/lib/libnss3.so
sharedlib
aaa@xxx:~ $ whatfile /bin/zcat
executable

The recommend way to check for PIE support is to compile C code like this

#if defined __PIC__ || defined __pic__ || defined PIC || defined pic            
# error PIC is default.                                                         
#endif                                                                          

with the requested compiler flags and check whether there is an error. If you need special treatment for PIE, this will recognize PIE if it has been specified through the CC or CFLAGS variables, even if is not immediately apparent there. For example, for technical reasons, Fedora hides the PIE flags behind a -specs argument .

Unfortunately, comments don't allow CR+LF (to show pre-formatted text).

Below is my formatted translation of your gcc setting command:

check_gcc_setting()
{

    if [ -n "`gcc -v -E 2>&1 | grep 'Configured with' | sed 's/--/\n--/g' | grep enable-default-pie`" ]
        then
            GCC_SETTING="1"
        else
            GCC_SETTING="0"
    fi
    read -p "The gcc setting is $GCC_SETTING " GCCRESULT
}

Below is the result:

whatfile { file $1 -i | grep application | sed 's/^.*application\/x-//g;s/,.*$//g'; }
-bash: syntax error near unexpected token `}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM