exception handling implementation - Linux

This is a discussion on exception handling implementation - Linux ; I'm currently struggling with the gory details of exception handling under Linux. Since I found no documents on the this, I wrote a simple test programm int foo(int a) { if(a==0) throw "exception"; return 1; } void bar() { try ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: exception handling implementation

  1. exception handling implementation

    I'm currently struggling with the gory details of exception
    handling under Linux. Since I found no documents on the this, I
    wrote a simple test programm



    int foo(int a)
    {
    if(a==0) throw "exception";
    return 1;
    }

    void bar()
    {
    try {
    foo(1);
    foo(0);
    } catch (char *msg) {
    puts(msg);
    } catch (...) {
    puts("unknown");
    }
    }



    and compiled that with
    $ gcc -S exception_test.cpp
    for x86-32 plattform. The compiler is GNU GCC 3.4.6 with
    extension patches ssp-3.4.5-1.0 and pie-8.7.9.

    Frankly I don't have the slightest idea how that stuff is
    working, due to the lack of information/documentation. I can see
    that everything evolves around the functions
    __cxa_allocate_exception, __cxa_throw, __cxa_begin_catch,
    __cxa_end_catch and the label _Unwind_Resume and the undefined,
    probably globally linked label _ZTIPKc. Please enlight me.

    Wolfgang Draxinger
    --
    E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867


  2. Re: exception handling implementation

    Wolfgang Draxinger wrote:
    > I'm currently struggling with the gory details of exception
    > handling under Linux. Since I found no documents on the this, I
    > wrote a simple test programm
    >
    >


    Okay, first thing to notice is that you are wrong here. This group deals
    with the Linux system, i.e. the kernel and related things. If you want
    information on how the GCC implements exception handling for C++ there is
    a group dedicated to just the C++ frontend of the GCC.

    > Frankly I don't have the slightest idea how that stuff is
    > working, due to the lack of information/documentation. I can see
    > that everything evolves around the functions
    > __cxa_allocate_exception, __cxa_throw, __cxa_begin_catch,
    > __cxa_end_catch and the label _Unwind_Resume and the undefined,
    > probably globally linked label _ZTIPKc. Please enlight me.


    IIRC, the exception handling mechanism is looking up the current position
    in the code and from there on unwinds the stack, calling destructors as
    necessary, until it reaches a handler (i.e. catch block) for the current
    exception.

    Uli


  3. Re: exception handling implementation

    Ulrich Eckhardt wrote:

    > Okay, first thing to notice is that you are wrong here. This
    > group deals with the Linux system, i.e. the kernel and related
    > things.


    I was somehow expecting, that exception handling is implemented
    somewhere in the guts of the standard runtime libraries which I
    definitely consider as part of the system.

    I'm aware, that it is not part of the kernel (well, that deals
    with exceptions, too, but these are of another kind).

    > IIRC, the exception handling mechanism is looking up the
    > current position in the code and from there on unwinds the
    > stack, calling destructors as necessary, until it reaches a
    > handler (i.e. catch block) for the current exception.


    So you say, the actual implementation is up to the compiler?
    Strange, I would really have expected, that there has been
    established some standard infrastructure, yet (that not named,
    other OS I don't like that much has such infrastructure, BTW).

    Wolfgang Draxinger
    --
    E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867


  4. Re: exception handling implementation

    On Sun, 03 Dec 2006 13:16:11 +0100
    Wolfgang Draxinger wrote:

    > I was somehow expecting, that exception handling is implemented
    > somewhere in the guts of the standard runtime libraries which I
    > definitely consider as part of the system.


    > So you say, the actual implementation is up to the compiler?
    > Strange, I would really have expected, that there has been
    > established some standard infrastructure, yet (that not named,
    > other OS I don't like that much has such infrastructure, BTW).


    Why would it be up to anyone else but the compiler to handle C++
    exceptions? The purpose of the kernel is to create a nice environment
    to you run you binary files and use your hardware. And there are no C++
    exceptions in any assembly language I know.

    There are a bunch of .h files related to the kernel that someone may
    use, but they're only there because the kernel is made in C. Were it
    made in other language, you'd be able to use kernel stuff using the
    other language's package mechanism.

    The standard library of your favorite language is not part of the
    system. At least no part of the kernel. You may need a C compiler and C
    libraries to compile the kernel, but once you do that, the kernel works
    as a standalone binary, it doesn't really need the compiler or the
    library anymore. Even if C library and compiler are considered as part
    of the kernel, the C++ library and compiler certanly would have nothing
    to do with it.

  5. Re: exception handling implementation

    In article ,
    Wolfgang Draxinger wrote:
    >
    >So you say, the actual implementation is up to the compiler?
    >Strange, I would really have expected, that there has been
    >established some standard infrastructure, yet (that not named,
    >other OS I don't like that much has such infrastructure, BTW).


    I think I know the name you're not speaking of, and the infrastructure you're
    speaking of, and there's nothing like that in the unix world. The unnamed
    beast uses the same low-level infrastructure for delivering the equivalent of
    SIGSEGV/SIGILL/etc. and to implement "throw"ing in high-level languages. It's
    not completely insane, but unix doesn't do that.

    Our only system-level "exception" concept is the signal, with signal handlers
    being the "catch". They have only a slight resemblance to C++ exceptions.

    setjmp/longjmp resembles C++ exceptions a bit more closely, but that's not
    really a system service at all, just a manipulation of memory and registers
    in a manner chosen by the C implementation which needs no assistance from the
    kernel.

    --
    The attacker\x92s overall goal would very probably be to convince other users
    to run an unsafe program, by using the digital signature to convince them
    that it is actually bona fide Microsoft software and therefore safe to run.
    -- security bulletin MS01-017 ushers in a new definition of "safe"

  6. Re: exception handling implementation

    Alan Curry wrote:

    > [things I know already ;-)]


    I didn't expcet an explanation of the POSIX signals nor
    setjmp/longjmp. I'm developing applications for *nix sytems
    since years and also a Linux kernel module now and then.

    The reason I'm asking this is, that I'm currently developing an
    draft for a portable ABI for the D language, thus I also have to
    deal with the gory details of exception handling. Some systems
    provide an API for that, others don't. POSIX defines no
    exception handling other than signals, but I wondered if there
    is standard exception handling code, that all OOP like languages
    could use running under Linux.

    Wolfgang Draxinger
    --
    E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867


  7. Re: exception handling implementation

    Wolfgang Draxinger wrote:
    > So you say, the actual implementation is up to the compiler?
    > Strange, I would really have expected, that there has been
    > established some standard infrastructure, yet (that not named,
    > other OS I don't like that much has such infrastructure, BTW).


    That other OS also distinguishes OS exception handling and C++
    exception handling. Support for the latter has only very recently come
    to all of that OS's architectural variants. As recently as maybe one or
    two years go, you were out of luck if you were running the StrongARM
    version of that unnamed OS and wanted C++ exception handling.


  8. Re: exception handling implementation

    Wolfgang Draxinger wrote:
    > I'm currently struggling with the gory details of exception
    > handling under Linux.


    This is a feature of the GNU toolchain. When you compile GCC, it builds
    a shared library called libgcc_s.so. This contains certain dynamically
    linked run-time support, including the _Unwind* routines contributed by
    libunwind.

    Run ``nm --dynamic'' on /lib/libgcc_s.so.1

    This shared library is linked to libc, because it needs some libc
    support in order to be thread safe.

    > working, due to the lack of information/documentation. I can see
    > that everything evolves around the functions
    > __cxa_allocate_exception, __cxa_throw, __cxa_begin_catch,
    > __cxa_end_catch and the label _Unwind_Resume and the undefined,
    > probably globally linked label _ZTIPKc. Please enlight me.


    Those __cxa functions are in the C++ run-time support library
    libstdc++, which is probably in your /usr/lib directory.

    (There is on exception: the __cxa_atexit function is in glibc
    (libc.so.6). This is used to register global destructor hooks when a
    shared library is unloaded.)

    libstdc++ does depend on the _Unwind* interfaces in the libgcc_a
    library; you're just not seeing those being directly used other than
    _Unwind_Resume.


  9. Re: exception handling implementation

    Wolfgang Draxinger wrote:
    > The reason I'm asking this is, that I'm currently developing an
    > draft for a portable ABI for the D language, thus I also have to
    > deal with the gory details of exception handling. Some systems
    > provide an API for that, others don't. POSIX defines no
    > exception handling other than signals, but I wondered if there
    > is standard exception handling code, that all OOP like languages
    > could use running under Linux.


    http://research.hp.com/research/linux/libunwind/
    http://www.codesourcery.com/cxx-abi/abi-eh.html


  10. Re: exception handling implementation

    Kaz Kylheku wrote:
    > Wolfgang Draxinger wrote:
    >> So you say, the actual implementation is up to the compiler?
    >> Strange, I would really have expected, that there has been
    >> established some standard infrastructure, yet (that not named,
    >> other OS I don't like that much has such infrastructure, BTW).

    >
    > That other OS also distinguishes OS exception handling and C++
    > exception handling. Support for the latter has only very recently come
    > to all of that OS's architectural variants. As recently as maybe one or
    > two years go, you were out of luck if you were running the StrongARM
    > version of that unnamed OS and wanted C++ exception handling.


    How about saying a name? The only OS that about matches with your statement
    is MS Windows CE, and that has had C++ exception support for several
    years.

    Uli