AS 最有可能的是,我将列出的大部分内容根本不相关,但我只是不知道用哪种方式挖掘。
问题的本质:当我编译一个依赖于动态库的 C-shny 项目时,一切正常。当使用相同但静态时,它会陷入分段错误。
单函数库
fun kt_print(string: String) {
println("Hello, $string!!!")
}
我这样编译
kotlinc ./hellokt.kt -o hellokt -produce static //libhellokt.a
kotlinc ./hellokt.kt -o hellokt -produce dynamic //libhellokt.so
我将它连接到 C-shny PHP 扩展项目
配置.m4
PHP_ARG_ENABLE(hello, whether to enable hello support,[ --enable-hello Enable hello support])
if test "$PHP_HELLO" != "no"; then
PHP_ADD_INCLUDE(/root/simpleExtension)
PHP_ADD_LIBRARY_WITH_PATH(hellokt, /root/simpleExtension, HELLO_SHARED_LIBADD)
PHP_SUBST(HELLO_SHARED_LIBADD)
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
使用静态库:1)如果我在代码中的某处使用 kt_print,它会崩溃。2) 如果我不使用 kt_print 调用,它不会崩溃。2)它不是在函数调用上崩溃,而是在 zend_register_functions 上崩溃(即至少 php_load_extension 和 zend_register_module_ex 工作)
(gdb) run -dextension=./modules/hello.so -r "echo hello('JoE');"
Starting program: /opt/rh/rh-php71/root/usr/bin/php -dextension=./modules/hello.so -r "echo hello('JoE');"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib64/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00005555557b0682 in zend_register_functions ()
(gdb) bt
#0 0x00005555557b0682 in zend_register_functions ()
#1 0x00005555557b16b7 in zend_register_module_ex ()
#2 0x00005555556ecc36 in php_load_extension ()
#3 0x000055555579dace in zend_llist_apply ()
#4 0x000055555574f59a in php_ini_register_extensions ()
#5 0x0000555555747da9 in php_module_startup ()
#6 0x000055555584796d in php_cli_startup ()
#7 0x0000555555624838 in main ()
一个输出make
,任意一个连接
/bin/sh /root/simpleExtension/libtool --mode=link cc \
-DPHP_ATOM_INC -I/root/simpleExtension/include \
-I/root/simpleExtension/main \
-I/root/simpleExtension \
-I/opt/rh/rh-php71/root/usr/include/php \
-I/opt/rh/rh-php71/root/usr/include/php/main
-I/opt/rh/rh-php71/root/usr/include/php/TSRM \
-I/opt/rh/rh-php71/root/usr/include/php/Zend \
-I/opt/rh/rh-php71/root/usr/include/php/ext \
-I/opt/rh/rh-php71/root/usr/include/php/ext/date/lib \
-I/root/simpleExtension \
-DHAVE_CONFIG_H -g -O2 -o hello.la -export-dynamic -avoid-version
-prefer-pic -module -rpath /root/simpleExtension/modules hello.lo \
-Wl,-rpath,/root/simpleExtension -L/root/simpleExtension -lhellokt
libtool: link: cc -shared -fPIC -DPIC .libs/hello.o \
-L/root/simpleExtension -lhellokt -O2 \
-Wl,-rpath -Wl,/root/simpleExtension -Wl,-soname -Wl,hello.so -o .libs/hello.so
可能是什么问题呢?也许需要额外的诊断?
找到了解决方法,但根本原因仍然不清楚。
问题出在 C 代码中(我提醒您这是一个 PHP 扩展)。在定义函数数组的时候,需要加一行
为什么在使用共享库时没有这条线,一切编译和工作正常,而在使用静态库时崩溃,目前尚不清楚。
来自@VTT 的解释性评论