[Bug c++/17721] New: exception not caught in shared lib if using dlopen

frank dot schaedlich at asg dot com gcc-bugzilla@gcc.gnu.org
Wed Sep 29 11:08:00 GMT 2004


I have a function f() which throws
an exception of the type Exception:

class Exception {
};

extern "C" void f() {
    throw Exception();
}

The exception should be catched with

try { f(); } 
catch (Exception& e) .

This works not if f() is placed in 
a shared library and the library was
loaded with dlopen(). If the type of the 
exception is a class then this type 
seems to be missing and it can't be determind 
which handler can catch the exception. Only 
catch (...) works. Other types, e. g. int,
works fine.

The bug occurs on (1) Linux / gcc 3.4.0,
(2) Linux gcc 3.3.2 but not on (3) Solaris /
gcc 3.3.2.

(1)  

>Release:       3.4.0
>Environment:
System: Linux mars 2.4.21-99-smp #1 SMP Wed Sep 24 13:31:14 UTC 2003 i686 i686
i386 GNU/Linux
Architecture: i686

        <machine, os, target, libraries (multiple lines)>
host: i586-suse-linux-gnu
build: i586-suse-linux-gnu
target: i586-suse-linux-gnu
configured with: /tmp/1/GCC340/gcc-3.4.0/configure --enable-threads=posix
--prefix=/usr/gcc340 --enable-languages=c,c++,f77,objc,java,ada
--disable-checking --enable-libgcj --with-system-zlib --enable-shared
--enable-__cxa_atexit i586-suse-linux

(2)

>Release:       3.3.2
>Environment:
System: Linux mars 2.4.21-99-smp #1 SMP Wed Sep 24 13:31:14 UTC 2003 i686 i686
i386 GNU/Linux
Architecture: i686

        <machine, os, target, libraries (multiple lines)>
host: i586-suse-linux-gnu
build: i586-suse-linux-gnu
target: i586-suse-linux-gnu
configured with: /tmp/1/GCC332/gcc-3.3.2/configure --enable-threads=posix
--prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib
--enable-languages=c,c++,f77,objc,java,ada --disable-checking --enable-libgcj
--with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib
--enable-shared --enable-__cxa_atexit i586-suse-linux

(3)

>Release:       3.2.3
>Environment:
System: SunOS oberon 5.7 Generic_106541-34 sun4u sparc SUNW,UltraSPARC-IIi-Engin
e
Architecture: sun4

        <machine, os, target, libraries (multiple lines)>
host: sparc-sun-solaris2.7
build: sparc-sun-solaris2.7
target: sparc-sun-solaris2.7
configured with: ../gcc-3.2.3/configure --prefix=/usr/local --enable-languages=c
,c++

Here is a small testcase:

$ cat exception.h 

#ifndef _exception_h_
#define _exception_h_

class Exception {
};

#endif

$ cat libf1.cpp 

#include "exception.h"

extern "C" void f1() {
    throw Exception();
}

$ cat test_exception.cpp

#include <iostream>
#include <dlfcn.h>
#include "exception.h"
using namespace std;


void f0 () {
   throw Exception() ;
}

main() {

    try {
            f0();
    } catch (Exception& e) {
            cerr << "f0: Exception. " << endl;
    } catch (...) {
            cerr << "f0: unknown exception - expected: Exception. " << endl;
    }


    void *lib_f1;
    void (*f1_call)();

    if (!(lib_f1=dlopen("./libf1.so",RTLD_LAZY))) {
            cerr << endl;
            cerr << "Can't open ./libf1.so." << endl;
            cerr << endl;
            exit(1);
    }

    if (!(f1_call=(void (*)())dlsym(lib_f1,"f1"))) {
            cerr << endl;
            cerr << "Can't bind f1." << endl;
            cerr << endl;
            exit(2);
    }

    try {
            (*f1_call)();
    } catch (Exception& e) {
            cerr << "f1: Exception. " << endl;
    } catch (...) {
            cerr << "f1: unknown exception - expected: Exception. " << endl;
    }

    dlclose(lib_f1);
}

$ cat Makefile

CC = g++

all: test_exception libf1.so

test_exception: test_exception.cpp exception.h
        $(CC) -ldl -o test_exception test_exception.cpp

libf1.so: libf1.cpp exception.h
        $(CC) -fPIC -shared -Wl,-soname,libf1.so -o libf1.so libf1.cpp
        #$(CC) -fPIC -G -o libf1.so libf1.cpp

clean:
        rm -f *.o libf?.so core* a.out test_exception

-- 
           Summary: exception not caught in shared lib if using dlopen
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: frank dot schaedlich at asg dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i586-suse-linux-gnu
  GCC host triplet: i586-suse-linux-gnu
GCC target triplet: i586-suse-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17721



More information about the Gcc-bugs mailing list