Reputation: 429
Given by below code:
#include <stdio.h>
void output()
{
printf("hello \n");
}
int main()
{
output();
return 0;
}
When the above code is compiled by below command:
gcc hello.c -shared -fPIC -pie -o libhello.so -Wl,-E
The generated libhello.so is not only a shared lib, but also an executable. However, when change gcc to clang as below
clang-10 hello.c -shared -fPIC -pie -o libhello.so -Wl,-E
The compilation gives below warning:
clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]
When executing libhello.so compiled by clang-10, it also crashed.
Question: 1. Is it possible to use clang compile runnable shared lib as gcc?
Note : This question is asked only for my own curiosity and I am not facing any practical problem.
Upvotes: 1
Views: 2094
Reputation: 429
As warning by the clang-10, clang-10 does not generate below things as GCC compilers:
Both of them can be manually as below
#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 : %s\n", __FUNCTION__);
exit(0);
}
However, it's better to use -Wl,-e,YourEntryFunction
flags to create shared object runnable instead of the approach presented in the above question.
Upvotes: 0