On 4/22/21, Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> wrote: > On Thu, 2021-04-22 at 10:17 -0500, Peng Yu via Gcc-help wrote: >> Hi, >> >> As written on Wikipedia, PIC and PIE seems to be synonymous. >> >> "In computing, position-independent code[1] (PIC[1]) or >> position-independent executable (PIE)[2] is ..." >> >> https://dev.gentoo.org/~vapier/crt.txt >> >> But according to the above page, they seem to be different. Could >> anybody explain what is the difference between them? >> > > -fPIE generates code for position-independent executable (i. e., "main" > program), -fPIC generates code for shared libraries. Use -fPIE for > libraries breaks semantic interposition (some programs require it, and > it's also very useful in debugging). Using -fPIC for an executable > makes the compiler *lose* some opportunities to optimize the code (some > optimizations are perfectly well for main program, but breaking semantic > interposition in libraries). > > I have some code showing the semantic interposition breakage by misusing > -fPIE for a lib: > > https://linux.xidian.edu.cn/git/xry111/pie_vs_pic ### program output running the program linked to the correct shared object: ./exe_link_to_pic foo in exe foo in exe running the program linked to the buggy (-fPIE) shared object: ./exe_link_to_pie foo in exe foo in lib ### According to my understanding, it is always the foo in the lib.c be called by bar in the same file lib.c, because when lib.c is written, people should understand so no matter how the library is linked. So I'd think the -fPIE result is correct. Why do you think the -fPIC result is correct? -- Regards, Peng