luajit2.0.0 — Segmentation fault: 11

China☆狼群 提交于 2020-01-03 09:22:30

问题


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  

回答1:


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.



来源:https://stackoverflow.com/questions/13359821/luajit2-0-0-segmentation-fault-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!