gpt4 book ai didi

c - 什么会导致 gcc 部分编译 C 代码?

转载 作者:行者123 更新时间:2023-11-30 14:49:14 25 4
gpt4 key购买 nike

我正在编译一个用 C 编写的 Python 扩展,它使用外部 C 扩展 (pycairo)。当我编译并链接外部库时,gcc 会生成一个有效的 .so 对象。但是,如果我使用 pycairo 提供的 PyCapsule 形式的 C API,gcc 会在我的一个 C 源代码中部分汇编一个函数。功能

static PyObject *
Canvas_show(Canvas* self) {
struct timespec ts = {self->interval / 1e9, self->interval % (unsigned int) 1e9};
int running;

// Input events
XSelectInput(self->display, self->win_id, ButtonPressMask | KeyPressMask);
XMapWindow(self->display, self->win_id);

PyObject *args = Py_BuildValue("(O)", PycairoContext_FromContext(self->context, &PycairoContext_Type, NULL));
for (running = 1; running;)
{
Canvas_on_draw(self, args, NULL);

nanosleep(&ts, NULL);
cairo_surface_flush(self->surface);

switch (cairo_check_event(self->surface, 0)) {
case 0xff1b: // Esc
case -1: // left mouse button
running = 0;
break;
}
}

Py_DECREF(args);
cairo_destroy(self->context);
cairo_surface_destroy(self->surface);
XCloseDisplay(self->display);

return Py_True;
}

被gcc编译成以下几条指令

00000000000010f0 <Canvas_show>:
10f0: 53 push rbx
10f1: 48 89 fb mov rbx,rdi
10f4: 48 8b 77 38 mov rsi,QWORD PTR [rdi+0x38]
10f8: 48 8b 7f 30 mov rdi,QWORD PTR [rdi+0x30]
10fc: ba 05 00 00 00 mov edx,0x5
1101: e8 8a fb ff ff call c90 <XSelectInput@plt>
1106: 48 8b 73 38 mov rsi,QWORD PTR [rbx+0x38]
110a: 48 8b 7b 30 mov rdi,QWORD PTR [rbx+0x30]
110e: e8 bd fb ff ff call cd0 <XMapWindow@plt>
1113: 48 8b 04 25 08 00 00 mov rax,QWORD PTR ds:0x8
111a: 00
111b: 0f 0b ud2

Disassembly of section .fini:

0000000000001120 <_fini>:
1120: 48 83 ec 08 sub rsp,0x8
1124: 48 83 c4 08 add rsp,0x8
1128: c3 ret
<EOF>

从 1113 开始的代码似乎已经是乱码,这就是我在运行测试时得到 SIGSEGV 的地方。

无论我使用 distutils 还是手动编译源代码,都会生成相同的 .so 文件

gcc -shared x11/x11module.c x11/canvas.c -o x11/x11.so -g -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python3.6 -I/usr/include/cairo/ -lcairo -lX11

什么会导致 gcc 部分编译有效的 C 代码而不引发错误?

最佳答案

正如评论中所指出的,原因很可能是未定义的行为。事实证明,原因是对从未初始化的静态变量进行了 NULL 指针取消引用。

关于c - 什么会导致 gcc 部分编译 C 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com