Blame SOURCES/gcc48-pr78875.patch

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