Basically, I have posted a few questions regarding my issue. However, I do believe this is a different question.
Simply, Go tools, with a certain set of buildmode options, creates a non-PIC .o file (say, app.o) and a PIC shared library, say, libmain.so. As the name shows, libmain.so does have THE main function inside.
Following that, Go tools invoke gcc, the compiler driver, to link the .o and .so to make an executable, which is not necessarily PIE. To simplify, the linker command that Go tools invoke is as follows:
$ gcc -o app.x -L. -lmain -Wl,-rpath=$PWD -fuse-ld=gold app.o
The thing is this. At least three experts confirmed that Scrt1.o must be used to link them as main is inside the PIC shared library. However, gcc is pulling out crt1.o instead.
My question is whether this is a gcc bug or the user's fault. Definitely, if the user added "-pie" option to the link command above, gcc will use Scrt1.o instead. However, the users could claim that as they may not want a position independent executable, adding "-pie" might not make that much sense.
I tried to find appropriate information in the gcc manuals but it wasn't successful. In this situation, is the user supposed to specify "-pie"? Or, is the user allowed to make a non-PIE executable so that this issue is a gcc bug?
For your convenience, here is a simple example:
$ cat app.c
int foo(int x, int y)
{
volatile int z = x;
return z + y;
}
$ cat main.c
extern int foo(int, int);
int main(int argc, char* argv[])
{
return foo(argc, 4);
}
$ gcc -o app.o -c app.c
$ gcc -o main.o -c -fPIC main.c
$ gcc -o libmain.so -shared main.o
$ gcc -o app.x -L. -lmain -fuse-ld=gold -Wl,-v,-rpath=$PWD app.o
collect2 version 4.8.5 20150623 (Red Hat 4.8.5-36.0.2)
/usr/bin/ld.gold --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -dynamic-linker /lib/ld-linux-aarch64.so.1 -X -o app.x /usr/lib/gcc/aarch64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/aarch64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/aarch64-redhat-linux/4.8.5/crtbegin.o -L. -L/usr/lib/gcc/aarch64-redhat-linux/4.8.5 -L/usr/lib/gcc/aarch64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/aarch64-redhat-linux/4.8.5/../../.. -lmain -v -rpath=/home/aion1223/workspace/shared app.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/aarch64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/aarch64-redhat-linux/4.8.5/../../../../lib64/crtn.o
GNU gold (version 2.27-34.base.0.2.el7) 1.12