[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

sigsegv in _dl_sym()



Hello,

I have a problem where when I try to load a shared object created by tcc, it
crashes in dl_sym. If shared object is built with gcc, the crash does not occur.

If anyone could tell me what might be the cause, I would greatly
appreciate any help.

ryan@avarice:~/503# ls -al /lib/libc.so.6
lrwxrwxrwx  1 root root 13 2004-11-19 02:46 /lib/libc.so.6 -> libc-2.3.2.so

Testcase:

ryan@avarice:~/503$ cat reduced.c

#include <stdio.h>
#include <dlfcn.h>

int main() {
   static char *code[] = {
                         "int test(int tmp, int tmp2, char *fp, char
*ip, char *sp ) {",
                         "int i; i = i + i;", "i++;", "i = i&&i;",
                         "printf(\"hello\");",
                         "}",
                         NULL};
   char filename[] = {"test.c"};
   char *array;
   char **ptr;
   FILE *fd;
   int i;
   fd = fopen("test.c", "w");
   for(ptr=code; *ptr; ptr++) {
     fprintf(fd, "%s\n", *ptr);
   }
   fclose(fd);
   system("tcc -shared -rdyanmic -o test.so test.c");
   void* handle = dlopen("./test.so", RTLD_LAZY);
   if (!handle) {
       printf("Cannot open library: %s\n",dlerror());
       return 1;
   }
   typedef void (*hello_t)();
   hello_t hello = (hello_t) dlsym(handle, "test");
   if (!hello) {
       fprintf(stderr, "Cannot load symbol 'hello': %\n",  dlerror());
       dlclose(handle);
       return 1;
   }
   hello();
   dlclose(handle);
}

ryan@avarice:~/503$ gcc reduced.c -ldl -g
ryan@avarice:~/503$ gdb ./a.out
GNU gdb 6.3-debian
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-linux"...Using host libthread_db
library "/lib/tls/libthread_db.so.1".

(gdb) run
Starting program: /home/ryan/503/a.out
Detaching after fork from child process 15133.

Program received signal SIGSEGV, Segmentation fault.
0x40011900 in realloc () from /lib/ld-linux.so.2
(gdb) bt
#0  0x40011900 in realloc () from /lib/ld-linux.so.2
#1  0x400076fb in _dl_unload_cache () from /lib/ld-linux.so.2
#2  0x40007def in _dl_lookup_symbol () from /lib/ld-linux.so.2
#3  0x40137d3c in _dl_sym () from /lib/tls/libc.so.6
(gdb)

If I compile the .so with gcc, it works fine.

ryan@avarice:~/503$ diff -u reduced.c reducedgcc.c
--- reduced.c   2004-11-30 16:13:28.000000000 -0700
+++ reducedgcc.c        2004-11-30 16:19:45.000000000 -0700
@@ -17,7 +17,7 @@
      fprintf(fd, "%s\n", *ptr);
    }
    fclose(fd);
-    system("tcc -shared -rdynamic -o test.so test.c");
+    system("gcc -shared -o test.so test.c");
    void* handle = dlopen("./test.so", RTLD_LAZY);
    if (!handle) {
        printf("Cannot open library: %s\n",dlerror());

ryan@avarice:~/503$ gcc -ldl -g reducedgcc.c
ryan@avarice:~/503$ ./a.out
hello

Thanks in advance,

Regards,

Ryan Mansfield



Reply to: