problems catching std::runtime_error in a .so built with -fPIC
Hasan Mahmood
hasan.mahm@gmail.com
Wed Apr 22 16:04:00 GMT 2009
Hi,
I am having a problem trying to catch std::runtime_error (thrown from
libstdc++) in a .so I built with -fPIC. If I build without -fPIC, I
can catch the exception. Here is the code in the .so:
#include <locale>
#include <iostream>
#include <stdexcept>
#pragma GCC visibility push(default)
extern "C" void foo();
#pragma GCC visibility pop
void foo()
{
try {
std::locale loc("foo");
} catch ( std::runtime_error const & e ) {
std::cout << e.what() << "\n";
}
}
Code in the program that loads the .so:
#include <dlfcn.h>
#include <iostream>
int main() {
void * h = dlopen("./test.so", RTLD_NOW);
if ( h ) {
typedef void (*foo_func_type)();
foo_func_type fn = (foo_func_type)dlsym(h, "foo");
if ( !fn )
std::cout << dlerror() << "\n";
else
fn();
} else
std::cout << dlerror() << "\n";
}
Here is the g++ command line for the .so:
g++ -shared -fPIC -fexceptions -frtti main.cpp
I thought at first that this was an issue with visibility of the
typeinfo for std::runtime_error, but then I found out that
std::runtime_error is marked "default" visibility in the stdexcept
header. An objdump of the .so has these symbols when built with -fPIC:
00002044 l O .data 00000004 .hidden
DW.ref._ZTISt13runtime_error
00000000 O *UND* 0000000c _ZTISt13runtime_error@@GLIBCXX_3.4
Without -fPIC, I get:
00000000 O *UND* 0000000c _ZTISt13runtime_error@@GLIBCXX_3.4
Can anyone explain to me what is going on? How can I fix this or work
around it? Thanks.
Hasan.
More information about the Gcc-help
mailing list