diff --git a/gnu/usr.bin/gcc/gcc/config/arm/openbsd.h b/gnu/usr.bin/gcc/gcc/config/arm/openbsd.h index 3e3f0741891..63844633908 100644 --- a/gnu/usr.bin/gcc/gcc/config/arm/openbsd.h +++ b/gnu/usr.bin/gcc/gcc/config/arm/openbsd.h @@ -93,7 +93,7 @@ Boston, MA 02111-1307, USA. */ #undef SUBTARGET_EXTRA_ASM_SPEC #define SUBTARGET_EXTRA_ASM_SPEC \ - "-matpcs %{fpic:-k} %{fPIC:-k}" + "-matpcs %{fpic|fPIC|fpie|fPIE:-k}" /* Default floating point model is soft-VFP. FIXME: -mhard-float currently implies FPA. */ diff --git a/gnu/usr.bin/gcc/gcc/config/mips/openbsd.h b/gnu/usr.bin/gcc/gcc/config/mips/openbsd.h index 16e45ed2818..e7ae4ffa585 100644 --- a/gnu/usr.bin/gcc/gcc/config/mips/openbsd.h +++ b/gnu/usr.bin/gcc/gcc/config/mips/openbsd.h @@ -21,7 +21,7 @@ Boston, MA 02111-1307, USA. */ /* Definitions needed for OpenBSD, to avoid picking mips 'defaults'. */ /* GAS must know this. */ -#define SUBTARGET_ASM_SPEC "%{fPIC:-KPIC} %|" +#define SUBTARGET_ASM_SPEC "%{fPIC|fPIE:-KPIC} %|" /* CPP specific OpenBSD specs. */ #define SUBTARGET_CPP_SPEC OBSD_CPP_SPEC diff --git a/gnu/usr.bin/gcc/gcc/config/sparc/openbsd64.h b/gnu/usr.bin/gcc/gcc/config/sparc/openbsd64.h index d7b43e19ba5..ea77d6360aa 100644 --- a/gnu/usr.bin/gcc/gcc/config/sparc/openbsd64.h +++ b/gnu/usr.bin/gcc/gcc/config/sparc/openbsd64.h @@ -52,7 +52,7 @@ Boston, MA 02111-1307, USA. */ #undef ASM_SPEC #define ASM_SPEC "\ -%{v:-V} -s %{fpic:-K PIC} %{fPIC:-K PIC} \ +%{v:-V} -s %{fpic|fPIC|fpie|fPIE:-K PIC} \ %{mlittle-endian:-EL} \ %(asm_cpu) %(asm_arch) \ " diff --git a/gnu/usr.bin/gcc/gcc/doc/invoke.texi b/gnu/usr.bin/gcc/gcc/doc/invoke.texi index 22a0e6bbdf1..030103ac0c6 100644 --- a/gnu/usr.bin/gcc/gcc/doc/invoke.texi +++ b/gnu/usr.bin/gcc/gcc/doc/invoke.texi @@ -304,7 +304,7 @@ in the following sections. @item Linker Options @xref{Link Options,,Options for Linking}. @gccoptlist{@var{object-file-name} -l@var{library} @gol --nostartfiles -nodefaultlibs -nostdlib @gol +-nostartfiles -nodefaultlibs -nostdlib -pie @gol -s -static -static-libgcc -shared -shared-libgcc -symbolic @gol -Wl,@var{option} -Xlinker @var{option} @gol -u @var{symbol}} @@ -657,7 +657,7 @@ in the following sections. -fasynchronous-unwind-tables @gol -finhibit-size-directive -finstrument-functions @gol -fno-common -fno-ident -fno-gnu-linker @gol --fpcc-struct-return -fpic -fPIC @gol +-fpcc-struct-return -fpic -fPIC -fpie -fPIE @gol -freg-struct-return -fshared-data -fshort-enums @gol -fshort-double -fshort-wchar -fvolatile @gol -fvolatile-global -fvolatile-static @gol @@ -4620,6 +4620,13 @@ library subroutines. (For example, @samp{__main}, used to ensure C++ constructors will be called; @pxref{Collect2,,@code{collect2}, gccint, GNU Compiler Collection (GCC) Internals}.) +@item -pie +@opindex pie +Produce a position independent executable on targets which support it. +For predictable results, you must also specify the same set of options +that were used to generate code (@option{-fpie}, @option{-fPIE}, +or model suboptions) when you specify this option. + @item -s @opindex s Remove all symbol table and relocation information from the executable. @@ -10776,6 +10783,15 @@ and the SPARC. Position-independent code requires special support, and therefore works only on certain machines. +@item -fpie +@itemx -fPIE +@opindex fpie +@opindex fPIE +These options are similar to @option{-fpic} and @option{-fPIC}, but +generated position independent code can be only linked into executables. +Usually these options are used when @option{-pie} GCC option will be +used during linking. + @item -ffixed-@var{reg} @opindex ffixed Treat the register named @var{reg} as a fixed register; generated code diff --git a/gnu/usr.bin/gcc/gcc/flags.h b/gnu/usr.bin/gcc/gcc/flags.h index d4e1adeb373..59a496a73bf 100644 --- a/gnu/usr.bin/gcc/gcc/flags.h +++ b/gnu/usr.bin/gcc/gcc/flags.h @@ -483,6 +483,16 @@ extern int flag_pedantic_errors; extern int flag_pic; +/* Nonzero if we are compiling position independent code for executable. + 1 vs 2 for a target-dependent "small" or "large" mode. */ + +extern int flag_pie; + +/* Nonzero if we are compiling code for a shared library, zero for + executable. */ + +extern int flag_shlib; + /* Nonzero means generate extra code for exception handling and enable exception handling. */ diff --git a/gnu/usr.bin/gcc/gcc/gcc.c b/gnu/usr.bin/gcc/gcc/gcc.c index 984d1e152b1..f7b0fe2949c 100644 --- a/gnu/usr.bin/gcc/gcc/gcc.c +++ b/gnu/usr.bin/gcc/gcc/gcc.c @@ -639,6 +639,8 @@ proper position among the other output files. */ #define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G" #endif +#define LINK_PIE_SPEC "%{pie: -pie}" + /* -u* was put back because both BSD and SysV seem to support it. */ /* %{static:} simply prevents an error message if the target machine doesn't handle -static. */ @@ -647,8 +649,12 @@ proper position among the other output files. */ directories. */ #ifndef LINK_COMMAND_SPEC #define LINK_COMMAND_SPEC "\ +%{pie:%{static:%e-pie and -static options are incompatible}} \ +%{shared:%{pie:%e-shared and -pie options are incompatible}} \ +%{pie:%{pg|p:%e-pie and -pg|p options are incompatible}} \ %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\ %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\ + " LINK_PIE_SPEC "\ %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\ %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}\ %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}" @@ -973,6 +979,7 @@ static const struct option_map option_map[] = {"--output-class-directory", "-foutput-class-dir=", "ja"}, {"--param", "--param", "a"}, {"--pedantic", "-pedantic", 0}, + {"--pie", "-pie", 0}, {"--pedantic-errors", "-pedantic-errors", 0}, {"--pipe", "-pipe", 0}, {"--prefix", "-B", "a"}, diff --git a/gnu/usr.bin/gcc/gcc/toplev.c b/gnu/usr.bin/gcc/gcc/toplev.c index d9891b2d14e..0964afb0c00 100644 --- a/gnu/usr.bin/gcc/gcc/toplev.c +++ b/gnu/usr.bin/gcc/gcc/toplev.c @@ -710,6 +710,17 @@ int flag_delayed_branch; int flag_pic; +/* Nonzero if we are compiling position independent code for executable. + The value is 1 if we are doing "small" pic; value is 2 if we're doing + "large" pic. */ +int flag_pie; + +/* Nonzero if we are compiling code for a shared library, zero for + executable. */ + +int flag_shlib; + + /* Set to the default thread-local storage (tls) model to use. */ enum tls_model flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC; @@ -1102,6 +1113,9 @@ static const lang_independent_options f_options[] = {"pic", &flag_pic, 1, N_("Generate position independent code, if possible") }, {"PIC", &flag_pic, 2, ""}, + {"pie", &flag_pie, 1, + N_("Generate position independent code for executables, if possible") }, + {"PIE", &flag_pie, 2, ""}, {"exceptions", &flag_exceptions, 1, N_("Enable exception handling") }, {"unwind-tables", &flag_unwind_tables, 1, @@ -5083,6 +5097,12 @@ parse_options_and_default_flags (argc, argv) } } + if (flag_pie) + flag_pic = flag_pie; + + if (flag_pic && !flag_pie) + flag_shlib = 1; + if (flag_no_inline == 2) flag_no_inline = 0; else diff --git a/gnu/usr.bin/gcc/gcc/varasm.c b/gnu/usr.bin/gcc/gcc/varasm.c index 4d1a3374695..231eed71934 100644 --- a/gnu/usr.bin/gcc/gcc/varasm.c +++ b/gnu/usr.bin/gcc/gcc/varasm.c @@ -5460,7 +5460,7 @@ bool default_binds_local_p (exp) tree exp; { - return default_binds_local_p_1 (exp, flag_pic); + return default_binds_local_p_1 (exp, flag_shlib); } bool