gcc shared library init problem (2nd try)
Ralf Fassel
ralf@akutech.de
Wed Jul 5 06:37:00 GMT 2000
[sorry if this appears twice, we had some problems with our mailer]
gcc 2.95.1, 2.95.2, 2.7.2.1
IRIX 6.5, IRIX 5.3, HP-UX A.09.05, SunOS 5.5
When loading a shared library, global structs are not initialized
properly, compared to loading from static libraries or compiled-in
code.
Consider the following program:
// t2.cc
#include <stdlib.h>
#include <stdio.h>
#ifndef MAINONLY
struct s {
char *c;
};
#define oneone "11" // OK
char *twotwo = "22"; // NOT OK
struct s foo1 = {oneone};
struct s foo2 = {twotwo};
static void foo_check(struct s array)
{
printf ("array.c %s\n", array.c ? array.c : "(null)");
}
void callme() {
foo_check(foo1);
foo_check(foo2);
}
#endif // !MAINONLY
#ifndef LIBONLY
void callme();
int
main()
{
callme();
return 0;
}
#endif // !LIBONLY
// End of file
Compile and run it:
% gcc -o t2 t2.cc
% ./t2
array.c 11
array.c 22
Fine. Now split it up into library and main:
% gcc -fpic -o t2.o -DLIBONLY -c t2.cc
% ld -shared -o libfoo.so t2.o
% gcc -o t2 -DMAINONLY t2.cc -L. -lfoo
% ./t2
array.c 11
array.c (null)
As you can see, the second array is no longer initialized properly.
This occurs on all versions/OS'es of gcc I have tested (see list
above), but also with the native compilers on IRIX, so I'm wondering
what is the problem here.
Anybody any hints? Please include a CC in your response, since I'm
not subscribed to the mailinglist.
Thanks for listening.
R'
More information about the Gcc-bugs
mailing list