GCC 4.0.3 -shared creates segfault

William Harrington wwh20610 at cmsu2.cmsu.edu
Tue Apr 17 14:14:59 PDT 2007


On Apr 17, 2007, at 4:24 AM, Tijnema ! wrote:

> On 4/17/07, Simon Geard <delgarde at ihug.co.nz> wrote:
>> On Mon, 2007-04-16 at 20:30 +0100, Tijnema ! wrote:
>>> Hi,
>>>
>>> Is it normal when using -shared to create binaries that they are  
>>> a segfault.
>>>
>>> This is what i did for example
>>> /# echo "main(){}" > dummy.c
>>> /# gcc -shared dummy.c -o dummy
>>> /# ./dummy
>>> Segmentation fault
>>
>> Yes. The -shared option indicates that gcc should produce a dynamic
>> library, rather than a runnable executable. What exactly are you  
>> trying
>> to do?
>>
>> Simon.
>
> Trying to create the smallest binaries, just the opposite of -static.
> but i guess that using no flags will create a binary that uses the
> most possible shared libraries.
> Just wondering that there was no error telling me it shouldn't be used
> on binaries.
>
> Tijnema
> -- 
> http://linuxfromscratch.org/mailman/listinfo/blfs-support
> FAQ: http://www.linuxfromscratch.org/blfs/faq.html
> Unsubscribe: See the above information page
>

It will link shared by default. You won' get an error, it works. The  
binary resulting from the -shared flag will segment fault on you  
because the interpret isn't set up like it is in a dynamically linked  
binary, which is probably ld-linux.so.2 on your system. You can see  
the resulting binary is indeed linked dynamically to the library, but  
when the ELF program is ran there is no interpreter. Basically  
without -shared you end up getting __libc_start_main in the  
relocation table.

ELF execution of the program starts inside the kernel. Without the  
interpreter in the relocation table, it doesn't get a segment. If an  
interpreter entry is present, the interpreter is loaded. Don't need  
that with statically linked binaries. Dynamically linked programs  
always need the interpreter because it includes startup code, loads  
shared libraries needed, and performs relocations. when using - 
shared, gcc  outputs a binary that doesn't use the interpreter.

Actually you could use readelf and look at the differences between  
the binary with and without -shared. Normally a binary to be executed  
with the interpreter has these program headers:

PHDR
INTERP
LOAD
DYNAMIC
GNU_STACK, etc

and without:

LOAD
DYNAMIC
GNU_STACK

Maybe this helps some.

Sincerely,

William



More information about the blfs-support mailing list