English 中文(简体)
SWIG crashes on AIX (with python, and probably everything else SWIG support)
原标题:
  • 时间:2009-11-14 18:55:30
  •  标签:
  • swig
  • aix

SWIG compiles and install easily on AIX. Unfortunately, a simple SWIG hello world (which also compiles - but not so easily) crashes with Segmentation Fault or Illegal Instruction (depending on some details of the compilation/linker process). This happens with both gcc and xlc (IBM c compiler). I tried only the native AIX linker ld, because the homonyms GNU ld was 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();

Makefile snippet:

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 the problematic one. If you follow the examples on the tutorial, 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 IBM/AIX and the Open Source communities have quite a different thoughts on what "shared library" means. The most common shared objects (so) that you get from the AIX native linker have no symbols at all in them (and are in fact less than 1kB in size). It s also pretty easy to get broken output from the linker (in such a case a quite long list of unresolved symbols like the following appears while linking):

ld: 0711-317 ERROR: Undefined symbol: PyType_Type

Doing what one is supposed to do, it seems clear that the solution is hacking with the various linker options, -berok, -bnoentry, -bexpall, -brtl, -bshared, -bM:SRE, -bexpfull. In fact, it is possible to find some combinations which create a non-empty .so library, without generating errors. One of these combinations is reported in the Makefile snippet above (there are others). Unfortunately, all of them fail 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 the gcc, or a different version of python (we have 7!) either 32 bit or 64 bit does not change anything: you can find a "good" link option, but it crashes at runtime. How to solve this?

最佳回答

This is not an actual question, but a report on how I fixed my problem (see here why I m doing this). And actually I wasn t able to solve it myself, but it was thanks to this other guy. I am rewriting it here, because he was too specific (AIX 5.1 with perl and C++, and I found him serendipitously, while I was searching for something else! I wasn t able to find his answer at all when I was searching for this problem! I hope this post will be more findable to others! My problem is on AIX 5.3 with python and C. I believe that it is common to every SWIG installation on AIX (thus I didn t tag python and C). I ll contact the developers soon so they might 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, which you should find yourself for your language/installation:

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

Hope this helps!

问题回答

This post didn t help me directly, but again pointed me in the right direction - but then the situation is different, I am building an embedded 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 




相关问题
strndup call is currupting stack frames

I have seen a strange behavior with "strndup" call on AIX 5.3 and 6.1. If I call strndup with size more than the size of actual source string length, then there is a stack corruption after that call. ...

How can I get the uptime of a IBM AIX box in seconds?

I m writting a Perl script for which I need the uptime in seconds to do some calculation, in all the machine in the shop (i.e. linux, SunOS, and AIX). I have a way to get the uptime for linux (/proc/...

How to find dll loaded by a process in AIX?

How to find which dynamic libs .so are loaded currently by a process, also those dll are dynamically loaded with dllopen. I want to debug a process to find out which libs its using currently.

SNMP on AIX 5.3

I need to monitor a system with AIX 5.3, but can t find a good source for the OIDs. Does anybody know the OIDs for CPU, Memory and Harddisk values?

Unix: fast remove directory for cleaning up daily builds

Is there a faster way to remove a directory then simply submitting rm -r -f *directory* ? I am asking this because our daily cross-platform builds are really huge (e.g. 4GB per build). So the ...

热门标签