我正在开发一个共享库,可以独立执行并打印自己的版本号。
我定义了一个自定义入口点:
const char my_interp[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
void my_main() {
printf("VERSION: %d\n", 0);
_exit(0);
}
我使用以下命令进行编译:
gcc -o list.os -c -g -Wall -fPIC list.c
gcc -o liblist.so -g -Wl,-e,my_main -shared list.os -lc
这段代码可以编译和运行得很完美。
我的问题在于当我将printf的参数更改为float或double(%f或%lf)时,库会编译但在运行时出现segfault。
有人有任何想法吗?
编辑1:
以下是导致系统崩溃的代码:
const char my_interp[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
void my_main() {
printf("VERSION: %f\n", 0.1f);
_exit(0);
}
编辑2:
其他环境细节:
uname -a
Linux mjolnir.site 3.1.10-1.16-desktop #1 SMP PREEMPT Wed Jun 27 05:21:40 UTC 2012 (d016078) x86_64 x86_64 x86_64 GNU/Linux
gcc --version
gcc (SUSE Linux) 4.6.2
/lib64/libc.so.6
配置为x86_64-suse-linux。 使用GNU CC版本4.6.2进行编译。 于2012-03-30在Linux 3.1.0系统上编译。
编辑3:
发生segfault时,/var/log/messages中的输出:
Aug 11 08:27:45 mjolnir kernel: [10560.068741] liblist.so[11222] general protection ip:7fc2b3cb2314 sp:7fff4f5c7de8 error:0 in libc-2.14.1.so[7fc2b3c63000+187000]