Search code examples
csegmentation-faultstatic-linkinglibgcrypt

Static linking creates Segmentation Fault error


I have a problem linking my C application statically. All libraries exist (.a) and just a month ago I was able to static link my application without an error. But as soon as I activate the static linking option in eclipse, I can compile without an error but when I try to run it, I receive an "Segmentation Error" and it stops.

I tried to debug and that is what eclipse is showing me:

No source available for "_start() at 0x4017f7"
No source available for "__libc_start_main() at 0x522389"
No source available for "__libc_csu_init() at 0x5228f7"
No source available for "frame_dummy() at 0x4018bd" 
No source available for "__register_frame_info_bases() at 0x52194b" 
No source available for "0x0" 

I use the following libraries: -lgcrypt -lgpg-error -lmxml -lpthread -lrt. Any ideas what the problem could be? I can also post the gdb traces, but its long.

Linker command: Invoking: Invoking: GCC C Linker gcc -static -o "X - Client" ./src/lib/stopwatch-0.2/stopwatch.o ./src/lib/rscode-1.3/berlekamp.o ./src/lib/rscode-1.3/crcgen.o ./src/lib/rscode-1.3/galois.o ./src/lib/rscode-1.3/rs.o ./src/lib/Salsa20/ecrypt.o ./src/lib/helper-Client.o ./src/PoR-Client.o -lgcrypt -lgpg-error -lmxml -lpthread -lrt Finished building target: X - Client


Solution

  • This is probably not a problem with the linking. You probably have a problem with reading uninitialized memory, or reading and writing past the end of an array.

    What happens in such cases is that on one build the memory you are reading just happens to be set to a non-crashing value ( e.g. you read past the end of an array into an area that has zeroes ), but then in another build the data structures are in a different order and now you are reading from something with unexpected values.

    Or you could in one build be writing past the end into a data structure that you don't need any more, and this build, the thing past the end of the array is critical.

    Also check if your program runs differently on debug vs. optimized builds. Optimization changes the layout, padding and initialization of data structures. ( e.g. debug builds will for example typically zero all memory, and stack frames are padded out with debugging data ).

    I strongly suggest you run your program through a tool like valgrind. It will find these kinds of problems for you.