Carmelo AMOROSO wrote:
Carmelo AMOROSO wrote:
Takashi Yoshii wrote:
Hi,

As a comment in libc/sysdeps/linux/sh/crt1.S says
    /* __uClibc_main (main, argc, argv, init, fini) */
, something wrong here. There should be two more args.

7th arg "stack_end" seems to be missing.
6th one is not listed, but the original code pushed register "r4" here.
It is not documented, but because ldso/ldso/sh/dl-startup.h explicitly sets 0 to r4 just before jump here, there must be a kind of calling convention.
So, I preserve the code, and add a comment that this is the way to pass
rtld_fini from prior routine.
I really can't find any documents about this convention, though.

/yoshii

---
sh: Fix args for __uClibc_main() in crt1.S
  add missing 7th arg "stack_end".
  add comment of undocumented usage of r4.
  fix comment of expected __uClibc_main() prototype.

diff --git a/libc/sysdeps/linux/sh/crt1.S b/libc/sysdeps/linux/sh/crt1.S
--- a/libc/sysdeps/linux/sh/crt1.S
+++ b/libc/sysdeps/linux/sh/crt1.S
@@ -24,6 +24,11 @@
At this entry point, most registers' values are unspecified, except: + r4 Contains a function pointer to be registered with `atexit'.
+        This is how the dynamic linker arranges to have DT_FINI
+        functions called for shared libraries that have been loaded
+        before this code runs.
+
    sp        The stack contains the arguments and environment:
            0(sp)            argc
         4(sp)            argv[0]
@@ -48,7 +53,8 @@ _start:
     mov.l @r15+,r5
     mov r15, r6
- /* Push the fini func onto the stack */
+    /* Push the stack_end, rtld_fini and fini func onto the stack */
+    mov.l r6,@-r15
     mov.l r4,@-r15
     mov.l L_fini,r0
     mov.l r0,@-r15
@@ -57,7 +63,7 @@ _start:
     mov.l L_main,r4
     mov.l L_init,r7
- /* __uClibc_main (main, argc, argv, init, fini) */ + /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */ /* Let the libc call main and exit with its return code. */
     mov.l L_uClibc_main,r1


Hi Takashi,
your point is correct and the patch looks fine. Looking again at the ld.so -> _start flow, yes sh4 forces rtld_fini to be NULL (r4 = 0 set in dl-startup.h as you told), so this means that on sh4 we do not call the _dl_fini destructor, loosing eventually to call the destructors of the dependant shared objects.
It should be simple enough to write a test showing this.

Well, I've written the test and, as I thought, we do not call the destructor for any dependant shared objects.

I'll spend a few time longer to look at what it's happening on glibc part, and eventually integrate your patch by setting the rtld_fini (6 ^ args) too.

Indeed glibc (sysdeps/sh/dl-machine.h: _start) passes in r4 the _dl_fini
instead of NULL. This makes dso's destructor get properly called.

I'll see how to integrate a test case into the test-suite too.

Full fix will come soon.
Carmelo

Thanks a lot,
Carmelo


Hi,
please find attached another patch to completely fix the sh4 startup sequence (in addition to the patch of Yoshii).

As you can see from the log below, DSO's destructor now is called.

Paul, does it sound good for you ?

........
_dl_protect_relro:124: RELRO protecting /home/carmelo/uclibc-libs/lib/ld-uClibc.so.0: start:0x2956f000, end:0x29570000 _dl_get_ready_to_run:898: calling INIT: /home/carmelo/uclibc-libs/lib/libc.so.0

_dl_get_ready_to_run:898: calling INIT: ./libfoo.so

foo constructor called !
_dl_get_ready_to_run:927: Calling _dl_allocate_tls_init()!
transfering control to application @ 0x400354
Calling foo()
foo called ... by carmelo
_dl_fini:132:
calling FINI: ./libfoo.so

foo destructor called !

Thanks,
Carmelo
Fix ldso startup sequence by passing via r4 the rtls finalizer
_dl_fini to the user application. This will be the 6^ arg of
__uClibc_main and will be registered with 'atexit'.
In this way the dynamic linker will be able to call destructors
defined within the loaded DSO.

Signed-off-by: Carmelo Amoroso <[EMAIL PROTECTED]>

Index: ldso/ldso/sh/dl-startup.h
===================================================================
--- ldso/ldso/sh/dl-startup.h   (revision 162)
+++ ldso/ldso/sh/dl-startup.h   (working copy)
@@ -12,10 +12,20 @@
     "  bsrf    r0\n"
     "  add     #4, r4\n"
     ".jmp_loc:\n"
-    "  jmp     @r0\n"
-    "  mov    #0, r4   !call _start with arg == 0\n"
+       "       mov     r0, r8  ! Save the user entry point address in r8\n"
+       "       mov.l   .L_got, r12             ! Load the GOT on r12\n"
+       "       mova    .L_got, r0\n"
+       "       add             r0, r12\n"
+       "       mov.l   .L_dl_fini, r0\n"
+       "       mov.l   @(r0,r12), r4   ! Pass the finalizer in r4\n"
+    "  jmp     @r8\n"
+       "       nop\n"
     ".L_dl_start:\n"
     "  .long   _dl_start-.jmp_loc\n"
+       ".L_dl_fini:\n"
+       "       .long   [EMAIL PROTECTED]"
+       ".L_got:\n"
+       "       .long _GLOBAL_OFFSET_TABLE_\n"
     "  .size   _start,.-_start\n"
     "  .previous\n"
 );
_______________________________________________
uClibc mailing list
uClibc@uclibc.org
http://busybox.net/cgi-bin/mailman/listinfo/uclibc

Reply via email to