Problem with constructor routine in ID shared library

Sandeep Kumar Singh Sandeep.Singh2 at kpitcummins.com
Thu Dec 15 06:28:03 UTC 2011


Hello,

I am a new comer to uClibc domain. 

I am working on CR16 architecture and using uClinux toolchain for library 
source code compilation, kernel compilation and application compilation. 
Version details of my toolchain sources are:
binutils-2.20.51
gcc-4.5.1
uClibc-0.9.30.1 (with CR16 support added)
uClinux Kernel-2.6.19-uc1

I have a problem with the constructor routines defined inside my ID shared 
library source code. Mine is a "C" application and library source is also a 
C code. 

My sample library code is:
======================================================================
static int val = 1 ;
void init( void ) __attribute__((constructor));

void init(void)
{
    val = 13 ;
    printf( "\nEntering constructor\n") ;
}

int lib_func(void)
{ 
   printf( "\nEntering %s, Value is %d\n", __FUNCTION__, val ) ;
   return val ;
 }
======================================================================

Here, when lib_func() is called from main(), I expected it to print value of
 "val" as 13. However, it prints it as 1.

I use the following compilation command for id shared library:
======================================================================
cr16-uclinux-gcc -c -mint32 -mcr16cplus -mdata-model=far -O2 \
-mid-shared-library -fPIC -o lib.o -c lib.c

cr16-uclinux-ar cru libmy_lib.a lib.o
cr16-uclinux-ranlib libmy_lib.a

cr16-uclinux-gcc -mint32 -mcr16cplus -mdata-model=far \
-Wl,-elf2flt=-k -L./ -mid-shared-library -Wl,--no-relax -fPIC \
-o libmy_lib_so -nostartfiles \ 
/home/test/uclinux/prefix/cr16-uclinux/runtime/usr/lib/far-pic/int32 \
/mid-shared-library/Scrt1.o \
-Wl,-shared-lib-id,3 \
-Wl,--whole-archive,libmy_lib.a -Wl,--no-whole-archive -Wl,-M=lib.map
======================================================================

In lib.map file, I can see an entry under '.ctors' section as:
.ctors         0x03005bec        0x4 libmy_lib.a(lib.o)

However, there is no entry under '.init_array'. Is this a problem? However, 
even if I assign this routine to section '.init_array' explicitly, it still 
does not get called before main.

In uClibc/libc/sysdeps/linux/cr16/crt1.S, __shared_flat_init() is passed as 
fourth argument to __uClibc_main(). Therefore, I believe this function must 
be responsible for initializing the library constructor routines. Hence, in 
uClibc/libc/misc/internals/shared_flat_initfini.c, I tried putting printf 
statements inside __shared_flat_init() function. However, with this change,
 uClinux kernel crashes on hardware while executing sample application. What 
way I could verify/debug the ID shared library initialization flow inside 
uClibc sources?

If I compile same library source as Static Library, everything works fine.

Thanks in advance.
Sandeep
[KPIT Cummins, Pune]




More information about the uClibc mailing list