[RFA/hpux] SEGV when running program using dlopen

Joel Brobecker brobecker@adacore.com
Wed Dec 15 09:20:00 GMT 2004


This is on HP/UX 11.00.

Compile the following little program:

    #include <stdio.h>
    #include <dl.h>
    
    int
    main (void)
    {
      void * o = shl_load ("/usr/lib/libnss_files.1", BIND_IMMEDIATE, 0L);
    
      printf ("Got o = %p\n", o);
    
      shl_unload (o);
    
      return 0;
    }

With this command:

        % gcc -g -o dl dl.c /opt/langtools/lib/end.o

Now try to run the program under the debugger:

        % gdb dl
        (gdb) run
        Starting program: /[...]/dl

        Program received signal SIGSEGV, Segmentation fault.
        0xc08ee090 in ?? ()

The expected behavior is:

        (gdb) run
        Starting program: /[...]/dl 
        Got o = 7b03d180
        
        Program exited normally.

The problem is in solib-som.c:som_solib_create_inferior_hook(): We're
looking up the address of __d_trap, and end up finding the address of
the function. But we need the stub, not the function. So we have the
following code that tries to do that:

  /* Grrr, this might not be an export symbol!  We have to find the
     export stub.  */
  ALL_OBJFILES (objfile)
  {
    struct unwind_table_entry *u;
    struct minimal_symbol *msymbol2;

    /* What a crock.  */
    msymbol2 =
      lookup_minimal_symbol_solib_trampoline (SYMBOL_LINKAGE_NAME (msymbol),
                                              objfile);
    /* Found a symbol with the right name.  */
    if (msymbol2)
      {
        struct unwind_table_entry *u;
        /* It must be a shared library trampoline.  */
        if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
          continue;

        /* It must also be an export stub.  */
        u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
        if (!u || u->stub_unwind.stub_type != EXPORT)
          continue;

        /* OK.  Looks like the correct import stub.  */
        anaddr = SYMBOL_VALUE (msymbol2);
        dld_cache.hook_stub.address = anaddr;
      }
  }

Unfortunately for us, that doesnt' work for __d_trap, as that symbol
lives in crt0. So lookup_minimal_symbol_solib_trampoline never returns
any match, and we end up stuck with the non-stub address.

I replaced the above block by a simply ALL_MSYMBOLS loop that simply
matches the msymbol name before checking for the associated stub type.
That fixes the problem.

2004-12-15  Joel Brobecker  <brobecker@gnat.com>

        * solib-som.c (som_solib_create_inferior_hook): Extend stub
        msymbol search to all objfiles, not just shared libraries.

Tested on HP/UX 11.00, no regression.
OK to commit?

Thanks,
-- 
Joel
-------------- next part --------------
Index: solib-som.c
===================================================================
RCS file: /cvs/src/src/gdb/solib-som.c,v
retrieving revision 1.1
diff -u -p -r1.1 solib-som.c
--- solib-som.c	8 Dec 2004 01:36:42 -0000	1.1
+++ solib-som.c	15 Dec 2004 07:30:02 -0000
@@ -157,7 +157,7 @@ som_relocate_section_addresses (struct s
 static void
 som_solib_create_inferior_hook (void)
 {
-  struct minimal_symbol *msymbol;
+  struct minimal_symbol *msymbol, *msymbol2;
   unsigned int dld_flags, status, have_endo;
   asection *shlib_info;
   char buf[4];
@@ -226,33 +226,22 @@ som_solib_create_inferior_hook (void)
 
   /* Grrr, this might not be an export symbol!  We have to find the
      export stub.  */
-  ALL_OBJFILES (objfile)
-  {
-    struct unwind_table_entry *u;
-    struct minimal_symbol *msymbol2;
-
-    /* What a crock.  */
-    msymbol2 =
-      lookup_minimal_symbol_solib_trampoline (SYMBOL_LINKAGE_NAME (msymbol),
-					      objfile);
-    /* Found a symbol with the right name.  */
-    if (msymbol2)
-      {
-	struct unwind_table_entry *u;
-	/* It must be a shared library trampoline.  */
-	if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
-	  continue;
-
-	/* It must also be an export stub.  */
-	u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
-	if (!u || u->stub_unwind.stub_type != EXPORT)
-	  continue;
-
-	/* OK.  Looks like the correct import stub.  */
-	anaddr = SYMBOL_VALUE (msymbol2);
-	dld_cache.hook_stub.address = anaddr;
-      }
-  }
+  ALL_MSYMBOLS (objfile, msymbol2)
+    {
+      if (strcmp (SYMBOL_LINKAGE_NAME (msymbol2),
+                  SYMBOL_LINKAGE_NAME (msymbol)) == 0)
+        {
+          struct unwind_table_entry *u;
+
+          u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
+          if (u != NULL && u->stub_unwind.stub_type == EXPORT)
+            {
+	      anaddr = SYMBOL_VALUE (msymbol2);
+	      dld_cache.hook_stub.address = anaddr;
+              break;
+            }
+        }
+    }
   store_unsigned_integer (buf, 4, anaddr);
 
   msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);


More information about the Gdb-patches mailing list