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