Unix Technical Forum

Exception handling on AIX with static-libgcc - Segmentation fault

This is a discussion on Exception handling on AIX with static-libgcc - Segmentation fault within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi, I'm having a weird problem with exception handling on AIX5.3 with gcc 3.4.6. After couple of tests I ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-20-2008, 12:39 PM
devshlom@gmail.com
 
Posts: n/a
Default Exception handling on AIX with static-libgcc - Segmentation fault

Hi,
I'm having a weird problem with exception handling on AIX5.3 with gcc
3.4.6.
After couple of tests I find out that in order to work with
exceptions on AIX, I must use shared lib-gcc instead of the static
one.

Pleas see my code below and my conclusions and let me kwnow, how can I
solve this problem.
(The following is the simplest code I could reproduce the problem
with)

-------------
main.cpp
-------------
#include <string>
#include <iostream>
using namespace std ;

class XXX_DAD
{
public:
XXX_DAD() {m_sReason="dad's reason";}
virtual ~XXX_DAD(){};
string GetReason() {return m_sReason;}
protected:
string m_sReason;
};

class XXX : public XXX_DAD
{
public:
XXX(string sInput): XXX_DAD(){m_sExp=sInput;}
virtual ~XXX() {};
string GetExp() {return m_sExp;}
private:
string m_sExp;
};


void gTest()
{
XXX* s = new XXX("ben ben...");
throw s;
}

int main()
{
try
{
try
{
gTest();
}
catch(XXX* ex)
{
printf("XXX exception was caught and re-throw: %s\n", ex-
>GetExp().c_str());

throw;
}
}
catch (XXX_DAD* ex_dad)
{
printf("XXX_DAD was caught: %s\n", ex_dad->GetReason().c_str());
}
return 0;
}

---------------------------------
Compilation command:
---------------------------------
gcc -o tester_static main.cpp -L/usr/lib/threads -pthread -I. -static-
libgcc -L/usr/lib -ldl -lpthread `gcc -print-file-name=libstdc++.a` -o
* `gcc -print-file-name=libstdc++.a` is /usr/local/lib/gcc/powerpc-
ibm-aix5.3.0.0/3.4.6/../../../libstdc++.a

This compilation will generate the "tester_static" executable.
If I run it I get the following output:
XXX exception was caught and re-throw: ben ben ...
Segmentation fault (core dumped)
as you can see, the first exception wat caught as it should, but when
it was re-thrown in order to be caugth by the base exception, I got
"Segmentation fault (core dumped)"

=====
Notes:
=====
1. If instead of calling gTest(), I will create new XXX object and
throw the exception directly (without using a function) as you can see
below, everything works well and I got the following desired output
(both derived and based exception were caught)
......
try
{
try
{
XXX* s = new XXX("ben ben...");
throw s;
}
catch(XXX* ex)
{
printf("XXX exception was caught and re-throw: %s\n", ex-
>GetExp().c_str());

throw;
}
}
.....

desired output:
XXX exception was caught and re-throw: ben ben ...
XXX_DAD was caught: dad's reason

2. If I used the exact same code, but link it with shared lib-gcc,
everything works well and I also get the desired output (both derived
and based exception were caught)

here is the compilation command***:
gcc -o tester_shared main.cpp -I. -shared-libgcc -L/usr/lib -ldl -
lpthread -lstdc++

*** The problem here is that I don't want to dynamically link libgcc
=> I must use the static lib-gcc!!!
here is the ldd output of the "tester_shared":
/usr/local/lib/gcc/powerpc-ibm-aix5.3.0.0/3.4.6/../../../
libgcc_s.a(shr.o)
/usr/lib/libpthread.a(shr_xpg5.o)
/usr/lib/libpthread.a(shr_comm.o)
../tester_shared
/usr/lib/libcrypt.a(shr.o)
/usr/lib/libc.a(shr.o)

and this is (libgcc_s.a(shr.o)...) not good for me, since I don't want
to distribute also libstdc++.a with my products executables

Any suggestions???
















Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-20-2008, 12:39 PM
devshlom@gmail.com
 
Posts: n/a
Default Re: Exception handling on AIX with static-libgcc - Segmentation fault

On Mar 17, 11:29*am, devsh...@gmail.com wrote:
> Hi,
> I'm having a weird problem with exception handling on AIX5.3 with gcc
> 3.4.6.
> After couple of tests I find out *that in order to work with
> exceptions on AIX, I must use shared lib-gcc instead of the static
> one.
>
> Pleas see my code below and my conclusions and let me kwnow, how can I
> solve this problem.
> (The following is the simplest code I could reproduce the problem
> with)
>
> -------------
> main.cpp
> -------------
> #include <string>
> #include <iostream>
> using namespace std ;
>
> class XXX_DAD
> {
> public:
> * *XXX_DAD() {m_sReason="dad's reason";}
> * *virtual ~XXX_DAD(){};
> * *string GetReason() {return m_sReason;}
> protected:
> * *string m_sReason;
>
> };
>
> class XXX : public XXX_DAD
> {
> public:
> * *XXX(string sInput): XXX_DAD(){m_sExp=sInput;}
> * *virtual ~XXX() {};
> * *string GetExp() {return m_sExp;}
> private:
> * *string m_sExp;
>
> };
>
> void gTest()
> {
> * XXX* s = new XXX("ben ben...");
> * throw s;
>
> }
>
> int main()
> {
> * *try
> * *{
> * * * * *try
> * * * * *{
> * * * * * * gTest();
> * * * * *}
> * * * * *catch(XXX* ex)
> * * * * *{
> * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());
>
> * * * * * * throw;
> * * * * *}
> * *}
> * *catch (XXX_DAD* ex_dad)
> * *{
> * * * printf("XXX_DAD was caught: %s\n", ex_dad->GetReason().c_str());
> * *}
> * *return 0;
>
> }
>
> ---------------------------------
> Compilation command:
> ---------------------------------
> gcc -o tester_static main.cpp -L/usr/lib/threads -pthread -I. -static-
> libgcc -L/usr/lib -ldl -lpthread `gcc -print-file-name=libstdc++.a` -o
> * `gcc -print-file-name=libstdc++.a` *is /usr/local/lib/gcc/powerpc-
> ibm-aix5.3.0.0/3.4.6/../../../libstdc++.a
>
> This compilation will generate the "tester_static" executable.
> If I run it I get the following output:
> * * *XXX exception was caught and re-throw: ben ben ...
> * * *Segmentation fault (core dumped)
> as you can see, the first exception wat caught as it should, but when
> it was re-thrown in order to be caugth by the base exception, I got
> "Segmentation fault (core dumped)"
>
> =====
> Notes:
> =====
> 1. If instead of calling gTest(), I will create new XXX object and
> throw the exception directly (without using a function) as you can see
> below, everything works well and I got the following desired output
> (both derived and based exception were caught)
> .....
> try
> * *{
> * * * * *try
> * * * * *{
> * * * * * * XXX* s = new XXX("ben ben...");
> * * * * * * throw s;
> * * * * *}
> * * * * *catch(XXX* ex)
> * * * * *{
> * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());
>
> * * * * * * throw;
> * * * * *}
> * *}
> ....
>
> desired output:
> XXX exception was caught and re-throw: ben ben ...
> XXX_DAD was caught: dad's reason
>
> 2. If I used the exact same code, but link it with shared lib-gcc,
> everything works well and I also get the desired output (both derived
> and based exception were caught)
>
> here is the compilation command***:
> gcc -o tester_shared main.cpp -I. -shared-libgcc -L/usr/lib -ldl -
> lpthread -lstdc++
>
> *** The problem here is that I don't want to dynamically link libgcc
> => I must use the static lib-gcc!!!
> here is the ldd output of the "tester_shared":
> /usr/local/lib/gcc/powerpc-ibm-aix5.3.0.0/3.4.6/../../../
> libgcc_s.a(shr.o)
> /usr/lib/libpthread.a(shr_xpg5.o)
> /usr/lib/libpthread.a(shr_comm.o)
> ./tester_shared
> /usr/lib/libcrypt.a(shr.o)
> /usr/lib/libc.a(shr.o)
>
> and this is (libgcc_s.a(shr.o)...) not good for me, since I don't want
> to distribute also libstdc++.a with my products executables
>
> Any suggestions???


B.T.W
if I throw and catch the exception by val instead of using pointers,
the program will always crash on run-time with the following error:
terminate called after throwing an instance of 'XXX'
IOT/Abort trap (core dumped)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-20-2008, 12:40 PM
devshlom@gmail.com
 
Posts: n/a
Default Re: Exception handling on AIX with static-libgcc - Segmentation fault

On Mar 19, 1:56*pm, devsh...@gmail.com wrote:
> On Mar 17, 11:29*am, devsh...@gmail.com wrote:
>
>
>
>
>
> > Hi,
> > I'm having a weird problem with exception handling on AIX5.3 with gcc
> > 3.4.6.
> > After couple of tests I find out *that in order to work with
> > exceptions on AIX, I must use shared lib-gcc instead of the static
> > one.

>
> > Pleas see my code below and my conclusions and let me kwnow, how can I
> > solve this problem.
> > (The following is the simplest code I could reproduce the problem
> > with)

>
> > -------------
> > main.cpp
> > -------------
> > #include <string>
> > #include <iostream>
> > using namespace std ;

>
> > class XXX_DAD
> > {
> > public:
> > * *XXX_DAD() {m_sReason="dad's reason";}
> > * *virtual ~XXX_DAD(){};
> > * *string GetReason() {return m_sReason;}
> > protected:
> > * *string m_sReason;

>
> > };

>
> > class XXX : public XXX_DAD
> > {
> > public:
> > * *XXX(string sInput): XXX_DAD(){m_sExp=sInput;}
> > * *virtual ~XXX() {};
> > * *string GetExp() {return m_sExp;}
> > private:
> > * *string m_sExp;

>
> > };

>
> > void gTest()
> > {
> > * XXX* s = new XXX("ben ben...");
> > * throw s;

>
> > }

>
> > int main()
> > {
> > * *try
> > * *{
> > * * * * *try
> > * * * * *{
> > * * * * * * gTest();
> > * * * * *}
> > * * * * *catch(XXX* ex)
> > * * * * *{
> > * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());

>
> > * * * * * * throw;
> > * * * * *}
> > * *}
> > * *catch (XXX_DAD* ex_dad)
> > * *{
> > * * * printf("XXX_DAD was caught: %s\n", ex_dad->GetReason().c_str());
> > * *}
> > * *return 0;

>
> > }

>
> > ---------------------------------
> > Compilation command:
> > ---------------------------------
> > gcc -o tester_static main.cpp -L/usr/lib/threads -pthread -I. -static-
> > libgcc -L/usr/lib -ldl -lpthread `gcc -print-file-name=libstdc++.a` -o
> > * `gcc -print-file-name=libstdc++.a` *is /usr/local/lib/gcc/powerpc-
> > ibm-aix5.3.0.0/3.4.6/../../../libstdc++.a

>
> > This compilation will generate the "tester_static" executable.
> > If I run it I get the following output:
> > * * *XXX exception was caught and re-throw: ben ben ...
> > * * *Segmentation fault (core dumped)
> > as you can see, the first exception wat caught as it should, but when
> > it was re-thrown in order to be caugth by the base exception, I got
> > "Segmentation fault (core dumped)"

>
> > =====
> > Notes:
> > =====
> > 1. If instead of calling gTest(), I will create new XXX object and
> > throw the exception directly (without using a function) as you can see
> > below, everything works well and I got the following desired output
> > (both derived and based exception were caught)
> > .....
> > try
> > * *{
> > * * * * *try
> > * * * * *{
> > * * * * * * XXX* s = new XXX("ben ben...");
> > * * * * * * throw s;
> > * * * * *}
> > * * * * *catch(XXX* ex)
> > * * * * *{
> > * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());

>
> > * * * * * * throw;
> > * * * * *}
> > * *}
> > ....

>
> > desired output:
> > XXX exception was caught and re-throw: ben ben ...
> > XXX_DAD was caught: dad's reason

>
> > 2. If I used the exact same code, but link it with shared lib-gcc,
> > everything works well and I also get the desired output (both derived
> > and based exception were caught)

>
> > here is the compilation command***:
> > gcc -o tester_shared main.cpp -I. -shared-libgcc -L/usr/lib -ldl -
> > lpthread -lstdc++

>
> > *** The problem here is that I don't want to dynamically link libgcc
> > => I must use the static lib-gcc!!!
> > here is the ldd output of the "tester_shared":
> > /usr/local/lib/gcc/powerpc-ibm-aix5.3.0.0/3.4.6/../../../
> > libgcc_s.a(shr.o)
> > /usr/lib/libpthread.a(shr_xpg5.o)
> > /usr/lib/libpthread.a(shr_comm.o)
> > ./tester_shared
> > /usr/lib/libcrypt.a(shr.o)
> > /usr/lib/libc.a(shr.o)

>
> > and this is (libgcc_s.a(shr.o)...) not good for me, since I don't want
> > to distribute also libstdc++.a with my products executables

>
> > Any suggestions???

>
> B.T.W
> if I throw and catch the exception by val instead of using pointers,
> the program will always crash on run-time with the following error:
> terminate called after throwing an instance of 'XXX'
> IOT/Abort trap (core dumped)- Hide quoted text -
>
> - Show quoted text -


1. the conclusion about the "terminate call..." is incorecct and
should be ignored since it was env problem
2. here is more simple code that you can try:
#include <stdio.h>


void fun(void)
{
printf("in fun() - before throwing int...\n");
throw 1;
}

int main(void)
{
try
{
try
{
fun();
}
catch(int i)
{
printf("catch int exception: %d, and re-throw...\n",
i);
throw;
}
}
catch (...)
{
printf("the re-throw was caught...\n");
}
return 0;
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:49 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com