On Tue, Jan 14, 2014 at 11:37 AM, Joseph S. Myers <jos...@codesourcery.com> wrote: > On Tue, 14 Jan 2014, H.J. Lu wrote: > >> When there is -static, -dynamic-linker won't passed to ld. -static, >> -shared, -pie should be mutually exclusive for GCC driver. > > A static PIE shouldn't specify an interpreter; it should handle all > dynamic relocation processing itself. Thus, it's correct not to specify > -dynamic-linker when building a static PIE.
If -dynamic-linker isn't specified, linker will use the default dynamic linker: [hjl@gnu-6 pr16428]$ cat x.c int main() { return 0; } [hjl@gnu-6 pr16428]$ make gcc -m32 -B./ -pie -static -fPIE -c -o x.o x.c gcc -m32 -B./ -pie -static -o x x.o ./x make: ./x: Command not found make: *** [all] Error 127 [hjl@gnu-6 pr16428]$ readelf -l x Elf file type is DYN (Shared object file) Entry point 0x10d0b There are 8 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000034 0x00000034 0x00000034 0x00100 0x00100 R E 0x4 INTERP 0x000134 0x00000134 0x00000134 0x00013 0x00013 R 0x1 [Requesting program interpreter: /usr/lib/libc.so.1] LOAD 0x000000 0x00000000 0x00000000 0xb6fda 0xb6fda R E 0x1000 LOAD 0x0b7000 0x000b7000 0x000b7000 0x00e40 0x02534 RW 0x1000 DYNAMIC 0x0b70b0 0x000b70b0 0x000b70b0 0x000d0 0x000d0 RW 0x4 NOTE 0x000148 0x00000148 0x00000148 0x00044 0x00044 R 0x4 TLS 0x0b7000 0x000b7000 0x000b7000 0x00010 0x00028 R 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x10 Section to Segment mapping: Segment Sections... 00 01 .interp 02 .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .rel.dyn .rel.plt .init .plt .text __libc_thread_freeres_fn __libc_freeres_fn .fini .rodata .stapsdt.base __libc_thread_subfreeres __libc_subfreeres __libc_atexit .eh_frame .gcc_except_table 03 .tdata .init_array .fini_array .jcr .data.rel.ro .dynamic .got .got.plt .data .bss __libc_freeres_ptrs 04 .dynamic > ld.so itself is a static PIE - it specifies no PT_INTERP or DT_NEEDED, and > is position-independent. The concept of an arbitrary program meeting > those conditions, including a copy of the relevant parts of the dynamic > linker to relocate itself, is perfectly meaningful; it just so happens > that glibc, and the associated GCC specs, don't support it, but I'm not > aware of any fundamental problem with supporting it in ld given > appropriate startup code and static libc built as PIC. (I don't know how > useful more general static PIE support would be.) -static -PIE may make sense to ld, but not to GCC driver, at least, not on Linux/x86 where the dynamic linker isn't /usr/lib/libc.so.1 > (-shared and -pie do seem mutually exclusive, as the first says what's > being linked operates as a library and the second says it operates as an > executable - although of course the same ET_DYN can operate as both.) > > -- > Joseph S. Myers > jos...@codesourcery.com -- H.J.