Subject: Re: Dynamic linking is go (again)
To: None <port-sh3@netbsd.org>
From: Valeriy E. Ushakov <uwe@ptc.spbu.ru>
List: port-sh3
Date: 07/06/2003 00:49:42
On Sun, Jul 06, 2003 at 05:09:29 +0900, Izumi Tsutsui wrote:

> > Hmm, so it doesn't work.  Apparently it crashes in _rtld_setup.
> 
> Maybe it's _start vs __start ENTRY problem in ld(1) which I posted.
> You have to fix ld in binutils or add -e __start in bsd.prog.mk.
> http://mail-index.netbsd.org/port-sh3/2003/07/05/0004.html
> http://mail-index.netbsd.org/port-sh3/2003/07/05/0005.html

I saw your posting, so I changed linker scripts:
s/ENTRY(_start)/ENTRY(__start)/


> FYI, what I tried is:
> 
> (1) Build usual toolchain for sh3el:
> % cd /usr/src
> % sh build.sh -m dreamcast -D [somewhere for ${DESTDIR}] tools

Yes.

Then I built stock binutils-2.14 and tell gcc-3.3 (next step) to use
those.

> (2) Build gcc-3.3:
> % tar jxvf gcc-3.3.tar.bz2
> % cd gcc-3.3
> % ./configure --target=shle--netbsdelf \
> --with-as=/usr/tools/sh3el/bin/shle--netbsdelf-as \
> --with-ld=/usr/tools/sh3el/bin/shle--netbsdelf-ld \
> --disable-shared --enable-languages=c,c++
> % gmake
> [I might see some errors but not sure..]
> % su
> # gmake install

Yes.


> (3) Build binaries with make script
> % cd /usr/src
> % ${TOOLDIR}/bin/make-dreamcast CC=/usr/local/bin/shle--netbsdelf-gcc \
> HAVE_GCC3=yes MKPROFILE=no TOOLCHAIN_MISSING=yes BUILD_tools=no -k build
> 
> (I'm not sure 'make -k build' works. Maybe you should visit
>  directories (lib/csu, lib/libc, libexec/ld.elf_so..) manually)

Yes.  BTW, we have convenient do-* targets in the toplevel Makefile to
build and install each of the above.  I ran nbmake-hpcsh with:

    TOOLCHAIN_MISSING=yes
    EXTERNAL_TOOLCHAIN=/export/netbsd/xtools
    HAVE_GCC3=yes



> > Perhaps, can you send me your working (on the DC) ld.elf_so, libc.so
> > and a sample binary?
> 
> Marcus has already put some binaries, but I'll put other binaries later.

I recompiled ld.elf_so with debugging and now it prints something
like:

<toor@nada:/mnt/distrib/bin> # ldd date
date:
         -lutil.7 => /lib/libutil.so.7
         -lc.12 => /lib/libc.so.12
<toor@nada:/mnt/distrib/bin> # ./date
sp = 0x7fffe954, argc = 1, argv = 0x7fffe960 <./date> relocbase 0x20420000
Memory fault (core dumped) 

Looking at the code in rtld.c:

        dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
            (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
        dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
            &_DYNAMIC));

it looks like it crashes trying to pass _GLOBAL_OFFSET_TABLE_:

gdb (the one i already have installed on the jornada, 5.0nb1)
complains about that ./date binary:

    Dwarf Error: Cannot handler DW_FORM_strp in DWARF reader.

[... run, get sigsegv ...]
(gdb) x/i $pc
0x2042310c:	mov.l	@(r0,r12),r5
(gdb) disass $pc-4 $pc+2
0x20423108:	mov	r12,r4
0x2042310a:	mov.l	0x204232c8,r0	! 0x17454
0x2042310c:	mov.l	@(r0,r12),r5
(gdb) p/x $r0
$1 = 0x17454
(gdb) p/x $12
$2 = 0x2043a71c

Diassembling the PIC setup at start of that function (_rtld()):

0x204230cc:	mova	0x204232b4,r0
0x204230ce:	mov.l	0x204232b4,r12	! 0x17468
0x204230d0:	add	r0,r12		# uwe: 0x2043a71c

Hmm, actually looking at the build log for the ld.eld_so (with
debugging enabled) I can see

/var/tmp//ccG2h5LE.s:864: Warning: rest of line ignored; first ignored character is `@'

And that's exactly the

    .long   _GLOBAL_OFFSET_TABLE_@GOT

that we pass as the second argument to the debug_printf.

Having no clue about PIC for sh3 I wonder if something is Wrong(TM)
here.

SY, Uwe
-- 
uwe@ptc.spbu.ru                         |       Zu Grunde kommen
http://www.ptc.spbu.ru/~uwe/            |       Ist zu Grunde gehen