This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem with global variables in shared libraries


Hallo,

please read the README file with details of the problem.

Best regards
Wolfgang

Hallo,

i have shared libraries with global variable. But it is not possible
to use the variables from an executable. What is the reason?

I am not sure if it is a compiler, linker or my problem (compiler/linker
switches). The same program compiled and linked under sun works fine.
I read the egcs and ld documentation but i have no idea what can i
do now.

Under linux two variables are created. One in the executable and
one in the shared object. The second problem is that the runtime 
"environ" variable is not initialized in the .so file.

I think nm.exe.txt shows the problem. It includes all global
.so file variables.

What can i do to solve the problem?

# 
# Program output
#

main(1, BFFFF938, BFFFF940)
   &environ:      08049A84		# should be 400BF8B0 ?
   &cool_environ: 08049A80		# should be 4000E7C4 ?
    environ:      BFFFF940
    cool_environ: 00000000
call myDll()
inside myDll()
   &environ:      400BF8B0
   &cool_environ: 4000E7C4
    environ:      00000000
    cool_environ: 00000000
inside main()
    environ:      BFFFF940
    cool_environ: 00000000

SUSE:   6.0 and 6.1  and
egcs:   egcs 1.1.1 and 1.1.2 releases and all egcs-snapshots until 19990602
ld:	ld 2.9.1.0.15 and 2.9.1.0.22

mak:		S.u.S.E. 6.0 makefile
makt:		S.u.S.E. 6.1 makefile
main:		executable
libmydll.so     shared library
main.cpp	executable source
mydll.cpp	shared library source 1
mydll_def.cxx	shared library source 2
README		this file

ldd.txt		output of ldd main
nm.exe.txt	output of nm main  !!! I mean this file is not ok.
nm.so.txt 	output of nm libmydll

	libmydll.so => ./libmydll.so
	libpthread.so.0 => /lib/libpthread.so.0 (0x40007000)
	libc.so.6 => /lib/libc.so.6 (0x40015000)
	libdl.so.2 => /lib/libdl.so.2 (0x400c2000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00000000)
#ifdef __cplusplus
extern "C" {
#endif

int main(int argc, char *argv[], char **envp);
int myDll(void);

#ifdef __cplusplus
}
#endif


#include <stdio.h>

extern char **environ, **cool_environ;

int main(int argc, char* argv[], char **envp)
{
   fprintf(stderr, "main(%d, %08X, %08X)\n", argc,
                                             (unsigned int) argv,
                                             (unsigned int) envp);

   fprintf(stderr, "   &environ:      %08X\n", (unsigned int)      &environ);
   fprintf(stderr, "   &cool_environ: %08X\n", (unsigned int) &cool_environ);

// cool_environ = environ;
   fprintf(stderr, "    environ:      %08X\n", (unsigned int)       environ);
   fprintf(stderr, "    cool_environ: %08X\n", (unsigned int)  cool_environ);

   fprintf(stderr, "call myDll()\n");

   myDll();

   fprintf(stderr, "inside main()\n");

   fprintf(stderr, "    environ:      %08X\n", (unsigned int)       environ);
   fprintf(stderr, "    cool_environ: %08X\n", (unsigned int)  cool_environ);

   return 0;
}

# compile sources
cc -c -o ./mydll.o     ./mydll.cpp     -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -malign-double -mwide-multiply -fvtable-thunks -fno-gnu-linker -fPIC -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-comment -Wno-switch -Wno-write-strings -g 
cc -c -o ./mydll_def.o ./mydll_def.cxx -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -malign-double -mwide-multiply -fvtable-thunks -fno-gnu-linker -fPIC -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-comment -Wno-switch -Wno-write-strings -g 
cc -c -o ./main.o      ./main.cpp      -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -malign-double -mwide-multiply -fvtable-thunks -fno-gnu-linker -fPIC -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-comment -Wno-switch -Wno-write-strings -g 

# remove targets
if [ -f ./libmydll.so ]; then rm -f ./libmydll.so; fi
if [ -f ./main ];        then rm -f ./main;        fi

# create shared object and executable
cc -o ./libmydll.so ./mydll.o ./mydll_def.o -shared -Wl,-Bsymbolic     -g         -lpthread  -lc -ldl `gcc --print-libgcc-file`  | c++filt
cc -o ./main        ./main.o                                       -L. -g -lmydll -lpthread  -lc -ldl `gcc --print-libgcc-file` -Xlinker -E -dy  | c++filt

# remove .o files
if [ -f ./mydll.o     ]; then rm -f ./mydll.o;     fi
if [ -f ./mydll_def.o ]; then rm -f ./mydll_def.o; fi
# if [ -f ./main.o      ]; then rm -f ./main.o;      fi
rm -rf *~

# execute test program
LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH
./main

# compile sources
g++ -c -o ./mydll.o     ./mydll.cpp     -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -m386 -mcpu=i386 -malign-double -mwide-multiply -fpermissive -fvtable-thunks -fno-gnu-linker -fPIC -fexceptions -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-unknown-pragmas -Wno-comment -Wno-switch -Wno-sign-compare -Wno-write-strings -g 
g++ -c -o ./mydll_def.o ./mydll_def.cxx -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -m386 -mcpu=i386 -malign-double -mwide-multiply -fpermissive -fvtable-thunks -fno-gnu-linker -fPIC -fexceptions -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-unknown-pragmas -Wno-comment -Wno-switch -Wno-sign-compare -Wno-write-strings -g 
g++ -c -o ./main.o      ./main.cpp      -I. -D_REENTRANT -D_POSIX_MAPPED_FILES -Dwcstombs=OSwcstombs -Dmbstowcs=OSmbstowcs -pipe -m386 -mcpu=i386 -malign-double -mwide-multiply -fpermissive -fvtable-thunks -fno-gnu-linker -fPIC -fexceptions -fwritable-strings -Wall -Wno-unused -Wno-parentheses -Woverloaded-virtual -Wno-unknown-pragmas -Wno-comment -Wno-switch -Wno-sign-compare -Wno-write-strings -g 

# remove targets
if [ -f ./libmydll.so ]; then rm -f ./libmydll.so; fi
if [ -f ./main ];        then rm -f ./main;        fi

# create shared object and executable
g++ -o ./libmydll.so ./mydll.o ./mydll_def.o -shared -Wl,-Bsymbolic     -g         -lpthread  -lc -ldl `gcc --print-libgcc-file`  | c++filt
g++ -o ./main        ./main.o                                       -L. -g -lmydll -lpthread  -lc -ldl `gcc --print-libgcc-file` -Xlinker -E -dy  | c++filt

# remove .o files
if [ -f ./mydll.o     ]; then rm -f ./mydll.o;     fi
if [ -f ./mydll_def.o ]; then rm -f ./mydll_def.o; fi
# if [ -f ./main.o      ]; then rm -f ./main.o;      fi
rm -rf *~

# execute test program
LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH
./main


#include <stdio.h>

extern "C" char **environ;

#ifdef __cplusplus
extern "C" {
#endif

char **cool_environ = 0;

int myDll(void);
int myDll(void) 
{
   fprintf(stderr, "inside myDll()\n");

   fprintf(stderr, "   &environ:      %08X\n", (unsigned int)      &environ);
   fprintf(stderr, "   &cool_environ: %08X\n", (unsigned int) &cool_environ);

   cool_environ = environ;
   fprintf(stderr, "    environ:      %08X\n", (unsigned int)       environ);
   fprintf(stderr, "    cool_environ: %08X\n", (unsigned int)  cool_environ);


   return 0;
}

#ifdef __cplusplus
}
#endif


#ifdef __cplusplus
extern "C" {
#endif

typedef struct {

  char * Name;
  void (* Function) (void);

} MutantDllHeaderExportEntry;

#ifdef __cplusplus
}
#endif


#define VerboseEnabled 0

#if VerboseEnabled
#include <stdio.h>
#define IfVerbose(X) do { X; } while (0);
#else
#define IfVerbose(X) /* empty */
#endif

extern "C" {
  typedef void (* EXPORT_FUNC) ();
}
extern "C" void myDll (void);

/*
 * definition of the library name and description
 */

static char *g_LibraryName = "wsrtst3";
static char *g_LibraryDesc = "Software AG wsrtst3 library (wsr) 1.00";

/*
 * list of all exported names
 */

static MutantDllHeaderExportEntry g_MutantDllExports[] = {
  /* BEGIN */
  {"myDll", (EXPORT_FUNC)myDll},
  /* END */
  {0, 0}
};

void Mutant_wsrtst3_SillyStub (char **ppName, char **ppDest, MutantDllHeaderExportEntry **ppExports)
{
  *ppName = g_LibraryName;
  *ppDest = g_LibraryDesc;
  *ppExports = g_MutantDllExports;
}


0804998c A _DYNAMIC
08049950 A _GLOBAL_OFFSET_TABLE_
08049a30 B _IO_stderr_
08049944 ? __CTOR_END__
08049940 ? __CTOR_LIST__
0804994c ? __DTOR_END__
08049948 ? __DTOR_LIST__
0804993c ? __EH_FRAME_BEGIN__
0804993c ? __FRAME_END__
08049a2c A __bss_start
0804986c D __data_start
         U __deregister_frame_info
08048810 t __do_global_ctors_aux
08048620 t __do_global_dtors_aux
08049a84 B __environ
         U __gmon_start__
         U __libc_init_first
         U __register_frame_info
08049a2c A _edata
08049aa0 A _end
08049a84 W _environ
08048848 A _etext
08048850 ? _fini
08048500 ? _init
080485d0 T _start
         U atexit
08049874 d completed.3
08049a80 B cool_environ
0804986c W data_start
08049a84 W environ
         U exit
08048660 t fini_dummy
08049878 d force_to_data
0804993c d force_to_data
         U fprintf
08048670 t frame_dummy
08048614 t gcc2_compiled.
08048620 t gcc2_compiled.
08048810 t gcc2_compiled.
08048848 t gcc2_compiled.
08048690 t init_dummy
08048840 t init_dummy
080486a0 T main
         U myDll
08049a88 b object.8
08049870 d p.2
000006d0 T Mutant_wsrtst3_SillyStub__FPPcT0PP26MutantDllHeaderExportEntry
000018d4 A _DYNAMIC
0000189c A _GLOBAL_OFFSET_TABLE_
         U _IO_stderr_
00001890 ? __CTOR_END__
0000188c ? __CTOR_LIST__
00001898 ? __DTOR_END__
00001894 ? __DTOR_LIST__
00001888 ? __EH_FRAME_BEGIN__
00001888 ? __FRAME_END__
         U ___brk_addr
0000196c A __bss_start
         U __curbrk
         U __deregister_frame_info
00000730 t __do_global_ctors_aux
00000500 t __do_global_dtors_aux
         U __environ
         U __gmon_start__
         U __register_frame_info
0000196c A _edata
00001984 A _end
0000079c A _etext
000007a0 ? _fini
00000460 ? _init
         U atexit
000017c0 d completed.3
000017c4 D cool_environ
         U environ
00000560 t fini_dummy
000017c4 d force_to_data
00001888 d force_to_data
         U fprintf
00000580 t frame_dummy
0000186c d g_LibraryDesc
00001840 d g_LibraryName
00001878 d g_MutantDllExports
00000500 t gcc2_compiled.
00000500 t gcc2_compiled.
00000730 t gcc2_compiled.
0000079c t gcc2_compiled.
000005b0 t init_dummy
00000770 t init_dummy
000005d0 T myDll
0000196c b object.8
000017bc d p.2

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]