https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80044
Bug ID: 80044 Summary: Specifying both -static and -pie insanity Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: driver Assignee: unassigned at gcc dot gnu.org Reporter: amodra at gmail dot com Target Milestone: --- -static and -pie are incompatible, yet gcc neither warns nor errors on the combination. What's more, gcc's behaviour depends on whether gcc was built with --enable-default-pie. If gcc was built with --enable-default-pie then -static silently overrides an explicit -pie, at least on x86 and I suspect most targets. -static is passed on to ld, gcc specifies the static startup files, and no -dynamic-linker is passed to ld. This is reasonable, but a warning that the explicit -pie was overridden would be nicer. If gcc was built with --disable-default-pie then -static does not fully override -pie. On x86, you get the static startup files, no -dynamic-linker, and both -static and -pie are passed on to the linker. ld.gold errors on that combination. ld.bfd tries to generate a PIE using the static startup files and static (typically non-pic) libraries, and typically the wrong ld.so. The wrong ld.so leads to a somewhat cryptic "file not found" message on trying to run an executable. Non-pic startup and libraries result in a linker error on x86_64, and text relocations on powerpc64le. TEXTREL and ifunc as found in modern glibc are fundamentally incompatible: When applying the text relocations, ld.so makes the text segment temporarily writeable (and non-exec), but the text needs to be exec to run ifunc resolvers. A segfault before main is the result.