سؤال

I use a simple example from http://lua-users.org/wiki/SimpleLuaApiExample to make a test. The sample can be statically linked with libluajit.a with a success, but this error message occurs when you run it:

Segmentation fault: 11

I use LuaJIT-2.0.0 released at 2012-11-08. My OS is Mac OSX Lion 10.7.5.

$ uname -a
Darwin macmatoMacBook-Pro.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

The test steps:

compile luajit-2.0.0

$ cd lj2
$ ls
COPYRIGHT Makefile  README    doc       dynasm    etc       src
$ make
==== Building LuaJIT 2.0.0 ====
make -C src
DYNLINK   libluajit.so
LINK      luajit
OK        Successfully built LuaJIT
==== Successfully built LuaJIT 2.0.0 ====
$ rm src/*.so            # force to use the static version: libluajit.a
$ cd ..

compile and run the sample app

Both test.c and script.lua come from here. The folder lj2 contains the source code of the above luajit-2.0.0, just compiled successfully.

$ ls
lj2 script.lua test.c 
use clang compiler
$ clang -o test test.c -I./lj2/src -L./lj2/src -lluajit
$ ./test
Segmentation fault: 11
use gcc compiler
$ gcc -o test test.c -I./lj2/src -L./lj2/src -lluajit
$ ./test 
Segmentation fault: 11

But if I replace lj2/src/luajit.c with test.c, it will give me a success. This is very strange. See below:

$ cd lj2
$ make clean
$ mv src/luajit.c src/luajit.c.orig 
$ cp ../test.c src/luajit.c
$ make
$ cp src/luajit ../
$ cd ..
$ ./luajit
The table the script received has:
1   2
2   4
3   6
4   8
5   10
Returning data back to C
Script returned: 30  
هل كانت مفيدة؟

المحلول

Problem resolved. There is an section which explains how to Embedding LuaJIT in this page:
http://luajit.org/install.html

  • If you're building a 64 bit application on OSX which links directly or indirectly against > LuaJIT, you need to link your main executable with these flags:

    -pagezero_size 10000 -image_base 100000000

Also, it's recommended to rebase all (self-compiled) shared libraries which are loaded at runtime on OSX/x64 (e.g. C extension modules for Lua). See: man rebase

Now, let me test it again:

$ clang -o test test.c -O3 -I./lj2/src -L./lj2/src -lluajit -pagezero_size 10000 -image_base 100000000
$ ./test 
The table the script received has:
1   2
2   4
3   6
4   8
5   10
Returning data back to C
Script returned: 30

And valgrind returns

$ valgrind ./test
bad executable (__PAGEZERO is not 4 GB)
valgrind: ./test: cannot execute binary file

That's another question.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top