Open Source Development

Power Open Source Development

Explore the open source tools and capabilities for building and deploying modern applications on IBM Power platforms including AIX, IBM i, and Linux.


#Power


#Power

 View Only
Expand all | Collapse all

Exception handling issue using gcc for shared libraries

  • 1.  Exception handling issue using gcc for shared libraries

    Posted Mon May 20, 2019 08:45 AM

    Originally posted by: appd


    Given this simple C++ program:
     
        #include <vector>
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <cmath>
        
        
        void foo()
        {
          try {
            throw 1;
          } catch(int&) {
            std::cout << "catch int" << std::endl;
          }
        }
        
        int main()
        {
          foo();
        }
     
    Without the -Wl,-G compiler options, we see the following (correct) results:
     
    bare-08$ g++ 1.cc -o a.out
    bare-08$ ./a.out
    catch int
     
    If we add these options, the same program core dumps
    bare-08$ g++ 1.cc -o b.out   -Wl,-G
    bare-08$ ./b.out
    terminate called after throwing an instance of 'int'
    IOT/Abort trap (core dumped)
    gcc --version
    gcc (GCC) 8.3.0
     
     
    Does anyone have insight into what is going on here?

    #AIXOpenSource
    #AIX-Open-Source-Software


  • 2.  Re: Exception handling issue using gcc for shared libraries

    Posted Mon May 20, 2019 05:31 PM

    Originally posted by: H0W2_tomasz_lacki


    Try below.

     

    AIX> cat slib.cpp
    #include <iostream>

    void throw_in()
    {
      try {
        throw 1;
      } catch(int &e) {
        std::cout << "catch int shlib in " << e << std::endl;
      }
    }

    void throw_out()
    {
       throw 2;
    }

    AIX> cat catch_app.cpp
    #include <iostream>

    void throw_in();
    void throw_out();

    int main()
    {
       try {
          throw_in();
          throw_out();
       } catch (int &e) {
          std::cout << "catch int shlib out " << e << std::endl;
       }
       return 0;
    }
    AIX> ./a!
    + oslevel
    7.2.0.0
    + g++ --version
    g++ (GCC) 8.1.0
    Copyright (C) 2018 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    + g++ -O2 -c -pthread slib.cpp
    + g++ -o libslib.so -shared -Wl,-G -pthread slib.o
    + g++ -O2 -c -pthread catch_app.cpp
    + g++ -o catch_app -Wl,-brtl -pthread -L. -lslib catch_app.o
    ld: 0711-224 WARNING: Duplicate symbol: .__init_aix_libgcc_cxa_atexit
    ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
    + chmod 700 libslib.so
    + ./catch_app
    catch int shlib in 1
    catch int shlib out 2


    #AIX-Open-Source-Software
    #AIXOpenSource


  • 3.  Re: Exception handling issue using gcc for shared libraries

    Posted Wed May 22, 2019 08:09 AM

    Originally posted by: AyappanP


    The linker option " -Wl,-G" should only be used for creating shared libraries (suitable for runtime-linking) and should not used for creating executable binaries.

    For enabling runtime-linking in executable binaries, "-Wl,-brtl" is enough. 

    If we don't need runtime-linking, then there is no need to use these linker options. 

    Good document about AIX Linking and Loading mechanisms  -->  http://public.dhe.ibm.com/software/dw/aix/es-aix_ll.pdf

     


    #AIX-Open-Source-Software
    #AIXOpenSource