SWIG crashes on AIX (using python and possibly all the rest of SWIG support)

SWIG is compiled and easily installed on AIX. Unfortunately, a simple SWIG hello world (which also compiles, but not so easily) crashes with a segmentation error or illegal instruction (depending on some details, the compilation / linker process). This happens with both gcc and xlc (IBM c compiler). I tried only the native AIX linker ld because the GNU ld homonyms were not installed on my system.

File: example.c

#include <time.h> double My_variable = 3.0; int fact(int n) { if (n <= 1) return 1; else return n*fact(n-1); } int my_mod(int x, int y) { return (x%y); } char *get_time() { time_t ltime; time(&ltime); return ctime(&ltime); } 

File: example.i

 %module example %{ /* Put header files here or function declarations like below */ extern double My_variable; extern int fact(int n); extern int my_mod(int x, int y); extern char *get_time(); %} extern double My_variable; extern int fact(int n); extern int my_mod(int x, int y); extern char *get_time(); 

A fragment of the Makefile:

 swig -python example.i xlc -q64 -c example.c example_wrap.c -I/your-python-path/include/python2.5/ ld -G -b64 -berok -bnoentry -bexpall -brtl example.o example_wrap.o -o _example.so 

The linker step is problematic. If you follow the tutorial examples you should do

 ld -bshared example.o example_wrap.o -o _example.so #the b is not a typo, but a different syntax in AIX vd GNU ld 

Unfortunately, this does not work for several reasons. I believe that the IBM / AIX and Open Source communities have different thoughts about what a “shared library” means. The most common generic objects (s) that you get from your own AIX linker do not have any characters in them (and in fact, their size is less than 1 KB). It is also quite easy to get broken output from the linker (in this case, when linking, a rather long list of unresolved characters appears, such as :)

 ld: 0711-317 ERROR: Undefined symbol: PyType_Type 

When doing what it should do , it seems obvious that the solution breaks into various linker options, -berok , -bnoentry , -bexpall , -brtl , -bshared , -bM:SRE , -bexpfull . In fact, you can find some combinations that create a non-empty .so library without generating errors. One of these combinations is reported in the Makefile snippet above (there are others). Unfortunately, they all do not work in one of the following two modes!

 $ python -c "import example" Illegal instruction (core dumped) 

or

 $ python -c "import example" Segmentation fault (core dumped) 

Using gcc or another version of python (we have 7!) Either 32-bit or 64-bit doesn’t change anything: you may find the link is a “good” link, but it crashes at runtime. How to solve this?

+2
source share
2 answers

This is not a real question, but a report on how I fixed my problem (see here why I am doing this). And in fact, I myself could not solve it, but thanks to this other guy . I am rewriting it here because it was too specific (AIX 5.1 with perl and C ++, and I found it to be impartial while I was looking for something else!) Problem! I hope this post becomes more accessible to others! My problem is AIX 5.3 with python and C. I find this to be common for every SWIG installation on AIX (so I did not mark python and C). I will contact the developers soon so that they can fix the help in the first place.

Well, the fix is ​​simply to use a different link line, in particular as follows:

 ld -G -bI:/your-python-path/lib/python2.5/config/python.exp -bnoentry -bexpall -lC -lc -ldl example.o example_wrap.o -o _example.so 

The key is the exp file you should find for your language / installation:

 find /your-python-or-perl-or-other-language-path/ -name *exp 

Hope this helps!

0
source

This post did not help me directly, but again pointed me in the right direction - but then the situation is different, I am building a built-in python. See http://docs.python.org/extending/embedding.html#linking-requirements

 >>> import distutils.sysconfig >>> distutils.sysconfig.get_config_var("LINKFORSHARED") '-Wl,-bE:Modules/python.exp -lld' 
+1
source

Source: https://habr.com/ru/post/1277587/


All Articles