Subject: Why does dlopen() go boom?
To: None <netbsd-help@netbsd.org>
From: None <kpneal@pobox.com>
List: netbsd-help
Date: 08/26/2002 23:48:21
--ReaqsoxgOBHFXBhH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Ok, I'm trying to get perl58 working out of pkgsrc. I'm running 1.5.3
with the latest pkgsrc on an Alpha 164LX. I can't get perl (perl5 or perl58)
to configure because of this problem.

There are two source files, dyna.c and fred.c. The fred executable
does a dlopen on dyna.so, and the dlopen call blows up good. Any ideas?
Thanks.


rune# cc -fno-strict-aliasing -I/usr/pkg/include -fPIC -DPIC -c dyna.c
rune# mv dyna.o tmp-dyna.o
rune# cc -o dyna.so -Wl,-R/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib -Wl,-rpath,/usr/local/lib -L/usr/pkg/lib -shared -Wl,-R/usr/pkg/lib -L/usr/pkg/lib tmp-dyna.o

rune# cc -o fred -g -fno-strict-aliasing -I/usr/pkg/include -Wl,-R/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib -Wl,-rpath,/usr/local/lib -L/usr/pkg/lib -DPIC -fPIC -Wl,-whole-archive -lgcc -Wl,-no-whole-archive -Wl,-E -Wl,-R/usr/pkg/lib/perl5/5.8.0/alpha-netbsd/CORE fred.c -lm -lcrypt

rune# ./fred 
pid 2490 (fred): unaligned access: va=0x16010433c pc=0x160104398 ra=0x1606128d8 op=ldq
Segmentation fault (core dumped)
rune# 

fatal user trap:

    trap entry = 0x2 (memory management fault)
    a0         = 0xb43e0010b41e0098
    a1         = 0x1
    a2         = 0x0
    pc         = 0x160106170
    ra         = 0x1601043a8
    curproc    = 0xfffffc000e467b98
        pid = 2490, comm = fred

pid 2490 (fred), uid 0: exited on signal 11 (core dumped)

(gdb) bt
#0  0x160106170 in _rtld_bind ()
Cannot access memory at address 0xb43e0010b41e0008.

From                To                  Syms Read   Shared Object Library
0x0000000160100000  0x000000016020ea84  Yes         /usr/libexec/ld.elf_so
0x0000000160210000  0x0000000160336420  Yes         /usr/lib/libm.so.0
0x0000000160338000  0x000000016043eaf0  Yes         /usr/lib/libcrypt.so.0
0x0000000160440000  0x0000000160611a18  Yes         /usr/lib/libc.so.12
0x0000000160612000  0x0000000160712c08  Yes         /usr/pkgobj/lang/perl58/work
/perl-5.8.0/UU/./dyna.so

0x0000000160106170 in _rtld_bind (): 0x6170 into ld.elf_so

0000000000006160 T _rtld_bind

So, 0x10 into _rtld_bind (in ld.elf_so) it blows up.

0000000000006160 <_rtld_bind>:  
    6160:       11 00 bb 27     ldah    gp,17(t12)
    6164:       28 f9 bd 23     lda     gp,-1752(gp)
    6168:       3e 15 c6 43     subq    sp,0x30,sp 
    616c:       00 00 5e b7     stq     ra,0(sp)
    6170:       90 00 30 a4     ldq     t0,144(a0)
    6174:       07 00 20 e4     beq     t0,6194 <_rtld_bind+0x34>
    6178:       01 04 31 40     addq    t0,a1,t0 
    617c:       11 14 c2 43     addq    sp,0x10,a1

If anyone wants the entire register set I can provide that as well.
Perhaps the core file would be nice?
-- 
Kevin P. Neal                                http://www.pobox.com/~kpn/

"Nonbelievers found it difficult to defend their position in \ 
    the presense of a working computer." -- a DEC Jensen paper

--ReaqsoxgOBHFXBhH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="dyna.c"

fred () { }

--ReaqsoxgOBHFXBhH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="fred.c"


#include <stdio.h>
#define I_DLFCN
#ifdef I_DLFCN
#include <dlfcn.h>      /* the dynamic linker include file for SunOS/Solaris */
#else
#include <sys/types.h>
#include <nlist.h>
#include <link.h>
#endif

extern int fred() ;

int main()
{
    void * handle ;
    void * symbol ;
#ifndef RTLD_LAZY
    int mode = 1 ;
#else
    int mode = RTLD_LAZY ;
#endif
    handle = dlopen("./dyna.so", mode) ;
    if (handle == NULL) {
	printf ("1\n") ;
	fflush (stdout) ;
	exit(0);
    }
    printf ("%d\n", __LINE__) ;
    fflush (stdout) ;
    exit(0);
    symbol = dlsym(handle, "fred") ;
    printf ("%d\n", __LINE__) ;
    fflush (stdout) ;
    if (symbol == NULL) {
	/* try putting a leading underscore */
	symbol = dlsym(handle, "_fred") ;
	if (symbol == NULL) {
	    printf ("2\n") ;
	    fflush (stdout) ;
	    exit(0);
	}
	printf ("3\n") ;
    }
    else
	printf ("4\n") ;
    fflush (stdout) ;
    exit(0);
}

--ReaqsoxgOBHFXBhH--