Re: g++: -fno-rtti breaks polymorphic catch on dynamic library
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:references:message-id:from:to :in-reply-to:content-type:content-transfer-encoding:x-mailer :mime-version:subject:date:cc; bh=pBV07MBtUxm7ZFSS6z/iKPEjDxTuSWeln9ST8wKItIQ=; b=Y3jt7dmmcKADO4Sw0aHPPQ+Bpw/LeLXVBE7phfXFMEgoM6B2wMvbs3JlWanvVZuSrI DdD6mr4mf3CxWE/j13eVBuLPYmz7niE0+dmscbVMllIJvgnyowPhvt6PdWhfY/JDnIyo JJyCUG+qA0Gcd4/dinsgNxMX1k4T0fq03b9X0= Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=references:message-id:from:to:in-reply-to:content-type :content-transfer-encoding:x-mailer:mime-version:subject:date:cc; b=UGJMhgXCaQAT7uC6PY3/s/Z6Hg6ceFOcLR6SvpF7DSeI/XKE2dCxj+DStTjg5BvmmU oWnwY0dd5CvFvJ2BbZ9foBKP/SDjdvVXvpLqUc1RsVHIkmWBrm0iWXq2RS3wtjTAsHsa h4Qm2I3PgTMyKvYlcUINcVRQ/yGChFqdrarik= On Sep 3, 2010, at 11:33 AM, "Pedro d'Aquino" <budsbd@gmail.com> wrote: Hello, The following code reproduces the problem (g++ 4.2, Mac OS X 10.6): // library.cpp: exports f(), compiled with -fno-rtti #include <stdexcept> #include <iostream> extern "C" { void f() { try { throw std::invalid_argument("std::exception handler"); } catch( std::exception& e) { std::cout << e.what() << "\n"; } catch(...) { std::cout << "... handler\n"; } } } ---------- // main.cpp: the main executable, dynamically loads the library #include <dlfcn.h> typedef void(*fPtr)(); ---------- Output: ---------- Questions: 1. Is this a bug, undefined behavior or by design? 2. How could I make it work, short of linking against the library? Thanks a lot. Pedro d'Aquino _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/pinskia%40gmail.com _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This is my first time posting on one of Apple's mailing lists, so please forgive me if I'm in the wrong place. I did look for a gcc- specific mailing list but could not find any. I'm building a shared library with f-no-rtti. Internally, this library throws 'std:invalid_argument' and catches 'std::exception', but the catch clause is never entered. Stop right there. Exceptions require rtti info to figure out what the types are so compiling with -fno-rtti will break exceptions. int main() { void* handle = dlopen( "./libexception_problem.dylib", RTLD_LAZY ); fPtr p_f = reinterpret_cast<fPtr>( dlsym( handle, "f" ) ); p_f(); } MacBook-Pro:teste pfranco$ # works fine with rtti MacBook-Pro:teste pfranco$ g++ -c library.cpp && g++ -shared -o libexception_problem.dylib library.o && g++ main.cpp -o main && ./main std::exception handler MacBook-Pro:teste pfranco$ # breaks with -fno-rtti MacBook-Pro:teste pfranco$ g++ -c -fno-rtti library.cpp && g++ - shared -o libexception_problem.dylib library.o && g++ -fno-rtti main.cpp -o main && ./main ... handler MacBook-Pro:teste pfranco$ #-no_dead_strip_inits_and_terms doesn't change anything MacBook-Pro:teste pfranco$ g++ -c -no_dead_strip_inits_and_terms -fno-rtti library.cpp && g++ -no_dead_strip_inits_and_terms -shared - o libexception_problem.dylib library.o && g++ -fno-rtti - no_dead_strip_inits_and_terms main.cpp -o main && ./main ... handler MacBook-Pro:teste pfranco$ # linking against the shared library works, but this isn't always an option MacBook-Pro:teste pfranco$ g++ -c -fno-rtti library.cpp && g++ - shared -o libexception_problem.dylib library.o && g++ -fno-rtti main.cpp -o main -L. -lexception_problem && ./main std::exception handler This only happens if the code that throws is in a shared library, and only if the caught type is a base class of the actual exception - catch(std::invalid_argument&) works fine, std::logic_error& doesn't. Interestingly, this doesn't happen on Linux (g++-4.4), even when running the exact same commands. This email sent to pinskia@gmail.com This email sent to site_archiver@lists.apple.com
participants (1)
-
Andrew Pinski