Blame SOURCES/gcc48-pr78875.patch

22033d
2017-01-17  Segher Boessenkool  <segher@kernel.crashing.org>
22033d
22033d
	PR target/78875
22033d
	* config/rs6000/rs6000-opts.h (stack_protector_guard): New enum.
22033d
	* config/rs6000/rs6000.c (rs6000_option_override_internal): Handle
22033d
	the new options.
22033d
	* config/rs6000/rs6000.md (stack_protect_set): Handle the new more
22033d
	flexible settings.
22033d
	(stack_protect_test): Ditto.
22033d
	* config/rs6000/rs6000.opt (mstack-protector-guard=,
22033d
	mstack-protector-guard-reg=, mstack-protector-guard-offset=): New
22033d
	options.
22033d
	* doc/invoke.texi (Option Summary) [RS/6000 and PowerPC Options]:
22033d
	Add -mstack-protector-guard=, -mstack-protector-guard-reg=, and
22033d
	-mstack-protector-guard-offset=.
22033d
	(RS/6000 and PowerPC Options): Ditto.
22033d
22033d
	* gcc.target/powerpc/ssp-1.c: New testcase.
22033d
	* gcc.target/powerpc/ssp-2.c: New testcase.
22033d
22033d
--- gcc/config/rs6000/rs6000.opt	(revision 244555)
22033d
+++ gcc/config/rs6000/rs6000.opt	(revision 244556)
22033d
@@ -593,3 +593,31 @@ Allow float variables in upper registers
22033d
 moptimize-swaps
22033d
 Target Undocumented Var(rs6000_optimize_swaps) Init(1) Save
22033d
 Analyze and remove doubleword swaps from VSX computations.
22033d
+
22033d
+mstack-protector-guard=
22033d
+Target RejectNegative Joined Enum(stack_protector_guard) Var(rs6000_stack_protector_guard) Init(SSP_TLS)
22033d
+Use given stack-protector guard.
22033d
+
22033d
+Enum
22033d
+Name(stack_protector_guard) Type(enum stack_protector_guard)
22033d
+Valid arguments to -mstack-protector-guard=:
22033d
+
22033d
+EnumValue
22033d
+Enum(stack_protector_guard) String(tls) Value(SSP_TLS)
22033d
+
22033d
+EnumValue
22033d
+Enum(stack_protector_guard) String(global) Value(SSP_GLOBAL)
22033d
+
22033d
+mstack-protector-guard-reg=
22033d
+Target RejectNegative Joined Var(rs6000_stack_protector_guard_reg_str)
22033d
+Use the given base register for addressing the stack-protector guard.
22033d
+
22033d
+TargetVariable
22033d
+int rs6000_stack_protector_guard_reg = 0
22033d
+
22033d
+mstack-protector-guard-offset=
22033d
+Target RejectNegative Joined Integer Var(rs6000_stack_protector_guard_offset_str)
22033d
+Use the given offset for addressing the stack-protector guard.
22033d
+
22033d
+TargetVariable
22033d
+long rs6000_stack_protector_guard_offset = 0
22033d
--- gcc/config/rs6000/rs6000.c	(revision 244555)
22033d
+++ gcc/config/rs6000/rs6000.c	(revision 244556)
22033d
@@ -3727,6 +3727,54 @@ rs6000_option_override_internal (bool gl
22033d
 				    atoi (rs6000_sched_insert_nops_str));
22033d
     }
22033d
 
22033d
+  /* Handle stack protector */
22033d
+  if (!global_options_set.x_rs6000_stack_protector_guard)
22033d
+#ifdef TARGET_THREAD_SSP_OFFSET
22033d
+    rs6000_stack_protector_guard = SSP_TLS;
22033d
+#else
22033d
+    rs6000_stack_protector_guard = SSP_GLOBAL;
22033d
+#endif
22033d
+
22033d
+#ifdef TARGET_THREAD_SSP_OFFSET
22033d
+  rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
22033d
+  rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
22033d
+#endif
22033d
+
22033d
+  if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
22033d
+    {
22033d
+      char *endp;
22033d
+      const char *str = rs6000_stack_protector_guard_offset_str;
22033d
+
22033d
+      errno = 0;
22033d
+      long offset = strtol (str, &endp, 0);
22033d
+      if (!*str || *endp || errno)
22033d
+	error ("%qs is not a valid number "
22033d
+	       "in -mstack-protector-guard-offset=", str);
22033d
+
22033d
+      if (!IN_RANGE (offset, -0x8000, 0x7fff)
22033d
+	  || (TARGET_64BIT && (offset & 3)))
22033d
+	error ("%qs is not a valid offset "
22033d
+	       "in -mstack-protector-guard-offset=", str);
22033d
+
22033d
+      rs6000_stack_protector_guard_offset = offset;
22033d
+    }
22033d
+
22033d
+  if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
22033d
+    {
22033d
+      const char *str = rs6000_stack_protector_guard_reg_str;
22033d
+      int reg = decode_reg_name (str);
22033d
+
22033d
+      if (!IN_RANGE (reg, 1, 31))
22033d
+	error ("%qs is not a valid base register "
22033d
+	       "in -mstack-protector-guard-reg=", str);
22033d
+
22033d
+      rs6000_stack_protector_guard_reg = reg;
22033d
+    }
22033d
+
22033d
+  if (rs6000_stack_protector_guard == SSP_TLS
22033d
+      && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
22033d
+    error ("-mstack-protector-guard=tls needs a valid base register");
22033d
+
22033d
   if (global_init_p)
22033d
     {
22033d
 #ifdef TARGET_REGNAMES
22033d
--- gcc/config/rs6000/rs6000.md	(revision 244555)
22033d
+++ gcc/config/rs6000/rs6000.md	(revision 244556)
22033d
@@ -13092,19 +13092,23 @@
22033d
 
22033d
 
22033d
 (define_expand "stack_protect_set"
22033d
-  [(match_operand 0 "memory_operand" "")
22033d
-   (match_operand 1 "memory_operand" "")]
22033d
+  [(match_operand 0 "memory_operand")
22033d
+   (match_operand 1 "memory_operand")]
22033d
   ""
22033d
 {
22033d
-#ifdef TARGET_THREAD_SSP_OFFSET
22033d
-  rtx tlsreg = gen_rtx_REG (Pmode, TARGET_64BIT ? 13 : 2);
22033d
-  rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET));
22033d
-  operands[1] = gen_rtx_MEM (Pmode, addr);
22033d
-#endif
22033d
+  if (rs6000_stack_protector_guard == SSP_TLS)
22033d
+    {
22033d
+      rtx reg = gen_rtx_REG (Pmode, rs6000_stack_protector_guard_reg);
22033d
+      rtx offset = GEN_INT (rs6000_stack_protector_guard_offset);
22033d
+      rtx addr = gen_rtx_PLUS (Pmode, reg, offset);
22033d
+      operands[1] = gen_rtx_MEM (Pmode, addr);
22033d
+    }
22033d
+
22033d
   if (TARGET_64BIT)
22033d
     emit_insn (gen_stack_protect_setdi (operands[0], operands[1]));
22033d
   else
22033d
     emit_insn (gen_stack_protect_setsi (operands[0], operands[1]));
22033d
+
22033d
   DONE;
22033d
 })
22033d
 
22033d
@@ -13127,21 +13131,26 @@
22033d
    (set_attr "length" "12")])
22033d
 
22033d
 (define_expand "stack_protect_test"
22033d
-  [(match_operand 0 "memory_operand" "")
22033d
-   (match_operand 1 "memory_operand" "")
22033d
-   (match_operand 2 "" "")]
22033d
+  [(match_operand 0 "memory_operand")
22033d
+   (match_operand 1 "memory_operand")
22033d
+   (match_operand 2 "")]
22033d
   ""
22033d
 {
22033d
-  rtx test, op0, op1;
22033d
-#ifdef TARGET_THREAD_SSP_OFFSET
22033d
-  rtx tlsreg = gen_rtx_REG (Pmode, TARGET_64BIT ? 13 : 2);
22033d
-  rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET));
22033d
-  operands[1] = gen_rtx_MEM (Pmode, addr);
22033d
-#endif
22033d
-  op0 = operands[0];
22033d
-  op1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, operands[1]), UNSPEC_SP_TEST);
22033d
-  test = gen_rtx_EQ (VOIDmode, op0, op1);
22033d
-  emit_jump_insn (gen_cbranchsi4 (test, op0, op1, operands[2]));
22033d
+  rtx guard = operands[1];
22033d
+
22033d
+  if (rs6000_stack_protector_guard == SSP_TLS)
22033d
+    {
22033d
+      rtx reg = gen_rtx_REG (Pmode, rs6000_stack_protector_guard_reg);
22033d
+      rtx offset = GEN_INT (rs6000_stack_protector_guard_offset);
22033d
+      rtx addr = gen_rtx_PLUS (Pmode, reg, offset);
22033d
+      guard = gen_rtx_MEM (Pmode, addr);
22033d
+    }
22033d
+
22033d
+  operands[1] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, guard), UNSPEC_SP_TEST);
22033d
+  rtx test = gen_rtx_EQ (VOIDmode, operands[0], operands[1]);
22033d
+  rtx jump = gen_cbranchsi4 (test, operands[0], operands[1], operands[2]);
22033d
+  emit_jump_insn (jump);
22033d
+
22033d
   DONE;
22033d
 })
22033d
 
22033d
--- gcc/config/rs6000/rs6000-opts.h	(revision 244555)
22033d
+++ gcc/config/rs6000/rs6000-opts.h	(revision 244556)
22033d
@@ -154,6 +154,12 @@ enum rs6000_vector {
22033d
   VECTOR_OTHER			/* Some other vector unit */
22033d
 };
22033d
 
22033d
+/* Where to get the canary for the stack protector.  */
22033d
+enum stack_protector_guard {
22033d
+  SSP_TLS,			/* per-thread canary in TLS block */
22033d
+  SSP_GLOBAL			/* global canary */
22033d
+};
22033d
+
22033d
 /* No enumeration is defined to index the -mcpu= values (entries in
22033d
    processor_target_table), with the type int being used instead, but
22033d
    we need to distinguish the special "native" value.  */
22033d
--- gcc/doc/invoke.texi	(revision 244555)
22033d
+++ gcc/doc/invoke.texi	(revision 244556)
22033d
@@ -862,7 +862,9 @@ See RS/6000 and PowerPC Options.
22033d
 -mcrypto -mno-crypto -mdirect-move -mno-direct-move @gol
22033d
 -mquad-memory -mno-quad-memory @gol
22033d
 -mquad-memory-atomic -mno-quad-memory-atomic @gol
22033d
--mcompat-align-parm -mno-compat-align-parm}
22033d
+-mcompat-align-parm -mno-compat-align-parm @gol
22033d
+-mstack-protector-guard=@var{guard} -mstack-protector-guard-reg=@var{reg} @gol
22033d
+-mstack-protector-guard-offset=@var{offset}}
22033d
 
22033d
 @emph{RX Options}
22033d
 @gccoptlist{-m64bit-doubles  -m32bit-doubles  -fpu  -nofpu@gol
22033d
@@ -18295,6 +18297,23 @@ GCC.
22033d
 
22033d
 In this version of the compiler, the @option{-mcompat-align-parm}
22033d
 is the default, except when using the Linux ELFv2 ABI.
22033d
+
22033d
+@item -mstack-protector-guard=@var{guard}
22033d
+@itemx -mstack-protector-guard-reg=@var{reg}
22033d
+@itemx -mstack-protector-guard-offset=@var{offset}
22033d
+@opindex mstack-protector-guard
22033d
+@opindex mstack-protector-guard-reg
22033d
+@opindex mstack-protector-guard-offset
22033d
+Generate stack protection code using canary at @var{guard}.  Supported
22033d
+locations are @samp{global} for global canary or @samp{tls} for per-thread
22033d
+canary in the TLS block (the default with GNU libc version 2.4 or later).
22033d
+
22033d
+With the latter choice the options
22033d
+@option{-mstack-protector-guard-reg=@var{reg}} and
22033d
+@option{-mstack-protector-guard-offset=@var{offset}} furthermore specify
22033d
+which register to use as base register for reading the canary, and from what
22033d
+offset from that base register. The default for those is as specified in the
22033d
+relevant ABI.
22033d
 @end table
22033d
 
22033d
 @node RX Options
22033d
--- gcc/testsuite/gcc.target/powerpc/ssp-1.c	(nonexistent)
22033d
+++ gcc/testsuite/gcc.target/powerpc/ssp-1.c	(revision 244562)
22033d
@@ -0,0 +1,6 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-protector-all -mstack-protector-guard=global" } */
22033d
+
22033d
+/* { dg-final { scan-assembler "__stack_chk_guard" } } */
22033d
+
22033d
+void f(void) { }
22033d
--- gcc/testsuite/gcc.target/powerpc/ssp-2.c	(nonexistent)
22033d
+++ gcc/testsuite/gcc.target/powerpc/ssp-2.c	(revision 244562)
22033d
@@ -0,0 +1,6 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-protector-all -mstack-protector-guard=tls -mstack-protector-guard-reg=r18 -mstack-protector-guard-offset=0x3038" } */
22033d
+
22033d
+/* { dg-final { scan-assembler {\m12344\(r?18\)} } } */
22033d
+
22033d
+void f(void) { }