c-Clang不同时接受共享的-fPIC-pie编译器标志



由以下代码给出:

#include <stdio.h>
void output()
{
printf("hello n");
}
int main()
{
output();
return 0;
}

当通过以下命令编译上述代码时:

gcc hello.c -shared -fPIC -pie -o libhello.so -Wl,-E

生成的libhello.so不仅是一个共享库,而且是一个可执行文件。然而,当将gcc更改为clang时,如所示

clang-10 hello.c -shared -fPIC -pie -o libhello.so -Wl,-E

汇编给出以下警告:

clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]

当执行clang-10编译的libhello.so时,它也崩溃了。

问题:1.是否可以使用clang编译可运行共享库作为gcc?

注意:这个问题只是出于我自己的好奇心,我没有面临任何实际问题。

作为clang-10的警告,clang-10不会作为GCC编译器生成以下内容:

  1. 共享库程序头中的INTERP段
  2. _start((函数

两者都可以手动操作,如下所示

#include <stdio.h>
const char interp_section[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
void output()
{
printf("hello n");
}
int main()
{
output();
return 0;
}
void _start()
{
printf("hello.c : %sn", __FUNCTION__);
exit(0);
}

但是,最好使用-Wl,-e,YourEntryFunction标志来创建可运行的共享对象,而不是上面问题中提出的方法。

最新更新