c++ - DSO 中的 SIGSEGV,混合 C/C++

标签 c++ c shared-libraries swi-prolog segmentation-fault

我正在使用 SWI-Prolog foreign language interface对于 C++,尝试集成一些其他资源。

它大部分都有效,但任何抛出异常的尝试都会导致 SIGSEGV。异常通常用于验证用户参数,因此是界面的基本部分。

我正在从源代码(通过提供的脚本)编译 SWI-Prolog,并且 CXX 标志是

-c -O2 -gdwarf-2 -g3 -Wall -pthread -fPIC

我使用相同的标志来编译我的 C++ 代码,这些代码在 .so 中组装,动态加载到 SWI-Prolog 中(我认为是通过 dlopen)。

在 SEGV 显示 __cxa_allocate_exception 内的 <+36> 处的 IP 后检查堆栈(通过 GDB)。可能无法访问 __cxa_get_globals@plt。

        Dump of assembler code for function __cxa_allocate_exception:
0x00007ffff1d80220  <+0>:               push   %r12
0x00007ffff1d80222  <+2>:               lea    0x80(%rdi),%r12
0x00007ffff1d80229  <+9>:               push   %rbp
0x00007ffff1d8022a  <+10>:              mov    %r12,%rdi
0x00007ffff1d8022d  <+13>:              push   %rbx
0x00007ffff1d8022e  <+14>:              callq  0x7ffff1d1de30 <malloc@plt>
0x00007ffff1d80233  <+19>:              test   %rax,%rax
0x00007ffff1d80236  <+22>:              mov    %rax,%rbx
0x00007ffff1d80239  <+25>:              je     0x7ffff1d802d8 <__cxa_allocate_exception+184>
0x00007ffff1d8023f  <+31>:              callq  0x7ffff1d1efc0 <__cxa_get_globals@plt>
0x00007ffff1d80244  <+36>:              addl   $0x1,0x8(%rax)
0x00007ffff1d80248  <+40>:              test   $0x1,%bl
0x00007ffff1d8024b  <+43>:              mov    %rbx,%rdi
0x00007ffff1d8024e  <+46>:              mov    $0x80,%edx
0x00007ffff1d80253  <+51>:              jne    0x7ffff1d803d0 <__cxa_allocate_exception+432>
0x00007ffff1d80259  <+57>:              test   $0x2,%dil

only resource我已经能够找到似乎相关的声明

the exception requires a typeinfo lookup

这可以理解 SIGSEGV。

但我现在无法继续。当然,我希望在一些神奇的 CXX 或 LD 标志。 或者我应该装饰我的库入口点(我熟悉 Windows declspec(s),我广泛使用它们来构建 MFC 扩展 DLL)或其他什么?

最佳答案

当从 Prolog 调用时,您不能向 Prolog 内核抛出任何异常。 C++ 接口(interface)将捕获 PlException 及其子类并将它们转换为 Prolog 异常。不得允许所有其他异常逃出您的库。

由于 SWI Prolog 是 LGPL,您可能正在动态链接到它。因此,您必须确保所有抛出的 C++ 在 ELF 系统上具有默认可见性。

关于c++ - DSO 中的 SIGSEGV,混合 C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9290625/

相关文章:

c++替换调用对象方法的宏函数

c++ - 获取 Windows ID C++

c - header 未找到 typedef 的结构

c - C中的字符串中存在子字符串

c - 编译 Opengl 和 glut 的问题

linux - ld-linux.so 以什么顺序搜索共享库?

javascript - map 究竟如何排序键?

c++ - 使用模板时,如何解决以下编译器错误,以实现程序所需的行为?

networking - 我可以编写 go 库以供其他语言使用吗?

qt - 如何从共享库初始化 Qt 资源?