Wednesday, March 16, 2022

[SOLVED] `file`, why executables not reported as executables?

Issue

In GNU/Linux Debian 9.9 (stretch) my program is reported as:

build/debug/program_g:              ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=be082fb3..., not stripped
build/debug/stripped_program_g:     ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=be082fb3..., stripped
build/release/program:              ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=6477469..., stripped
build/release/program_not_stripped: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=6477469..., not stripped

Compiler is GCC (vanilla distro's i.e. gcc (Debian 6.3.0-18+deb9u1) 6.3.0 2017051), flags are for "release":

 -ansi -pedantic -Wall -Wextra -Werror -Wunused-result -Wmissing-include-dirs 
 -Wparentheses -std=c89 -DPROGRAM_USE_STD_C89 -O2 -DNDEBUG

For "debug":

 -ansi -pedantic -Wall -Wextra -Werror -Wunused-result -Wmissing-include-dirs 
 -Wparentheses -std=c89 -DPROGRAM_USE_STD_C89 -O0 -g -fprofile-arcs -ftest-coverage -DTRACE_U0 -DTRACE_U1 -no-pie

Question is why for "release" the executable is reported as shared objectinstead of executable?


Solution

Because

1. You're using the -no-pie option with your "debug" build. Building position independent executables (-pie) is the default on recent Linux distros. Why are you disabling it in debug builds?

2. The file program on your system (Debian stretch) doesn't know about PIE executables. Newer versions of its magic files will correctly identify it as pie executable.

Example (/mnt/old is a Debian 9.9 root fs):

$ cc -xc - <<<'int main(){}'

$ file -m /mnt/old/usr/share/misc/magic a.out
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=df3780407016f3ea1a936c35d786288b1c0d4486, not stripped

$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=df3780407016f3ea1a936c35d786288b1c0d4486, not stripped


Answered By - mosvy
Answer Checked By - Gilberto Lyons (WPSolving Admin)