From patchwork Fri Apr 12 10:08:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Hsieh X-Patchwork-Id: 236007 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id DD76B2C00B3 for ; Fri, 12 Apr 2013 20:08:55 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=eFBJCVX1P2uqWYxDQ1 guZ9r4/0UBtfJrTlXkbZWw5t7xtNAfXao9K7A/8gmlstp4RfsdSAjHJng1FOgY8y KzV7799EMAZwSryk3XhNvrfGD5ppgSIsJqVK6K8Udux5YRqSseW/fLEGhBiyDOMO tI9LJ5bPdF8d1Rz5Vfvo3CXOk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=7HQ/8qWTTKaZKmh3i5LMa/C1 /FM=; b=JFcby9fzKs7Ca12KMYzWFnkR73K+/wYCL1EZWOMGz3iQChcr5Q+YP+dE ScThEIiwUdAJwFTtvZFJpxGXp02OapUmJWgAljp0JQp6dtuvLytKCHcpIP6KGBXu 7LYqATbbcsx0UtWprY/VtS1zMAmQNVzIAFWlYObgGJaXVta+zGg= Received: (qmail 15603 invoked by alias); 12 Apr 2013 10:08:49 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 15592 invoked by uid 89); 12 Apr 2013 10:08:48 -0000 X-Spam-SWARE-Status: No, score=-6.1 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_TRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, TW_AV, TW_ZJ autolearn=ham version=3.3.1 Received: from mail-oa0-f53.google.com (HELO mail-oa0-f53.google.com) (209.85.219.53) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Fri, 12 Apr 2013 10:08:47 +0000 Received: by mail-oa0-f53.google.com with SMTP id m17so2390830oag.12 for ; Fri, 12 Apr 2013 03:08:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=4oLbH6cL0ABffQQ2xUyKLWPLPguWU5Xe2hzAMMX7sWs=; b=p224+Vz1qIR6YfEbibjGF9wsNGsIA7zNrZFa+Ik9iKK1XzYQ7E1PPrOrfkwu+OozKe NtgYRByMELfKEssSG8+oAMr5qvBuUj/7sNsxK72E/wbhbMsLZbTzbE3a0Ieb6SdD4PIy DcdpPBgSf90JcY+vh7BljbvHBQe/6DnH2PzgIhNCnPymZl9AcmMH1WeVZsFRN5JolcD5 oN45M0wq0aPaGsPe6Av1Z31IKISJeWG+M5bdL+9mUjqWPPqy0vyjWnbogiflXJlUu2ct oU95io9ICVctr6OpbwgkSbiGZ9JtwYtRWjdUL9DvonJzXG/ddLzkMHAZXDaA2XVpxjFZ WKDw== MIME-Version: 1.0 X-Received: by 10.60.31.42 with SMTP id x10mr3602813oeh.18.1365761325728; Fri, 12 Apr 2013 03:08:45 -0700 (PDT) Received: by 10.60.124.130 with HTTP; Fri, 12 Apr 2013 03:08:45 -0700 (PDT) In-Reply-To: References: Date: Fri, 12 Apr 2013 18:08:45 +0800 Message-ID: Subject: Re: [PATCH,i386] Add -mstack-protector-guard= for i386 From: Andrew Hsieh To: Uros Bizjak , Jakub Jelinek Cc: "gcc-patches@gcc.gnu.org" X-Gm-Message-State: ALoCoQmt/ziUUOEfvPU5x8WBgaHuwCiKavC9e2LgPRWue3fEBqkKF2z9e1+UBeoSR9P9+XQ2f9KGTLIGpcxSOUHnTobnQ0ByyDvJrvRwfWuQ9aC5HG/uFuxi3h8BtgUSaHQcCs76dWrG2NSHM5RJIdiQj6rHYMcCE+WZIb9wK0FBw1C8L8TQNaoDiWB2AShxuiOEM6N7iKk+ Changed comment, corrected formatting, and removed Init(SSP_TLS). Thanks! On Fri, Apr 12, 2013 at 5:49 PM, Uros Bizjak wrote: > On Fri, Apr 12, 2013 at 10:43 AM, Andrew Hsieh wrote: >> Introduced TARGET_SSP_GLOBAL_GUARD and TARGET_SSP_TLS_GUARD as suggested. >> As for handling default in ix86_option_override_internal, is it >> possible to achieve the same "default to SSP_GLOBAL if bionic" >> entirely in i386.opt? > > No, AFAIK option system won't dynamically initialize a variable. > > +mstack-protector-guard= > +Target RejectNegative Joined Enum(stack_protector_guard) > Var(ix86_stack_protector_ > guard) Init(SSP_TLS) > +Use given stack-protector guard > > So, you don't need Init in the code above ... > >> + >> + /* Handle stack protector */ >> + if (!global_options_set.x_ix86_stack_protector_guard) >> + { >> + ix86_stack_protector_guard = TARGET_HAS_BIONIC? SSP_GLOBAL : SSP_TLS; >> + } > > ... since you initialize it here. > > Please, remove the braces here. > > Also, as Jakub said, please also update the comment for 32bit case. > > The patch is OK with these changes. > > Thanks, > Uros. ===== 2013-04-12 Andrew Hsieh * config/i386/i386.opt: New option mstack-protector-guard=. * config/i386/i386-opts.h: Add enum stack_protector_guard. * config/i386/i386.h: Introduce TARGET_SSP_GLOBAL_GUARD and TARGET_SSP_TLS_GUARD. * config/i386/i386.c (ix86_option_override_internal): Default to SSP_TLS unless it's bionic * config/i386/i386.md: define_expand/insn "stack_protect_set/test..." if TARGET_SSP_TLS_GUARD * doc/invoke.texi (i386 Option): Document. Index: gcc/config/i386/i386.opt =================================================================== --- gcc/config/i386/i386.opt (revision 197837) +++ gcc/config/i386/i386.opt (working copy) @@ -626,3 +626,17 @@ mrtm Target Report Mask(ISA_RTM) Var(ix86_isa_flags) Save Support RTM built-in functions and code generation + +mstack-protector-guard= +Target RejectNegative Joined Enum(stack_protector_guard) Var(ix86_stack_protector_guard) +Use given stack-protector guard + +Enum +Name(stack_protector_guard) Type(enum stack_protector_guard) +Known stack protector guard (for use with the -mstack-protector-guard= option): + +EnumValue +Enum(stack_protector_guard) String(tls) Value(SSP_TLS) + +EnumValue +Enum(stack_protector_guard) String(global) Value(SSP_GLOBAL) Index: gcc/config/i386/i386.md =================================================================== --- gcc/config/i386/i386.md (revision 197837) +++ gcc/config/i386/i386.md (working copy) @@ -17058,7 +17058,7 @@ (define_expand "stack_protect_set" [(match_operand 0 "memory_operand") (match_operand 1 "memory_operand")] - "!TARGET_HAS_BIONIC" + "TARGET_SSP_TLS_GUARD" { rtx (*insn)(rtx, rtx); @@ -17083,7 +17083,7 @@ UNSPEC_SP_SET)) (set (match_scratch:PTR 2 "=&r") (const_int 0)) (clobber (reg:CC FLAGS_REG))] - "!TARGET_HAS_BIONIC" + "TARGET_SSP_TLS_GUARD" "mov{}\t{%1, %2|%2, %1}\;mov{}\t{%2, %0|%0, %2}\;xor{l}\t%k2, %k2" [(set_attr "type" "multi")]) @@ -17101,7 +17101,7 @@ [(match_operand 0 "memory_operand") (match_operand 1 "memory_operand") (match_operand 2)] - "!TARGET_HAS_BIONIC" + "TARGET_SSP_TLS_GUARD" { rtx flags = gen_rtx_REG (CCZmode, FLAGS_REG); @@ -17131,7 +17131,7 @@ (match_operand:PTR 2 "memory_operand" "m")] UNSPEC_SP_TEST)) (clobber (match_scratch:PTR 3 "=&r"))] - "!TARGET_HAS_BIONIC" + "TARGET_SSP_TLS_GUARD" "mov{}\t{%1, %3|%3, %1}\;xor{}\t{%2, %3|%3, %2}" [(set_attr "type" "multi")]) Index: gcc/config/i386/i386-opts.h =================================================================== --- gcc/config/i386/i386-opts.h (revision 197837) +++ gcc/config/i386/i386-opts.h (working copy) @@ -85,4 +85,9 @@ ix86_veclibabi_type_acml }; +enum stack_protector_guard { + SSP_TLS, /* per-thread canary in TLS block */ + SSP_GLOBAL /* global canary */ +}; + #endif Index: gcc/config/i386/i386.c =================================================================== --- gcc/config/i386/i386.c (revision 197837) +++ gcc/config/i386/i386.c (working copy) @@ -3922,6 +3922,10 @@ if (main_args_p) target_option_default_node = target_option_current_node = build_target_option_node (); + + /* Handle stack protector */ + if (!global_options_set.x_ix86_stack_protector_guard) + ix86_stack_protector_guard = TARGET_HAS_BIONIC ? SSP_GLOBAL : SSP_TLS; } /* Implement the TARGET_OPTION_OVERRIDE hook. */ Index: gcc/config/i386/i386.h =================================================================== --- gcc/config/i386/i386.h (revision 197837) +++ gcc/config/i386/i386.h (working copy) @@ -486,6 +486,9 @@ #define TARGET_TLS_DIRECT_SEG_REFS_DEFAULT 0 #endif +#define TARGET_SSP_GLOBAL_GUARD (ix86_stack_protector_guard == SSP_GLOBAL) +#define TARGET_SSP_TLS_GUARD (ix86_stack_protector_guard == SSP_TLS) + /* Fence to use after loop using storent. */ extern tree x86_mfence; Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 197837) +++ gcc/doc/invoke.texi (working copy) @@ -656,7 +656,8 @@ -mcmodel=@var{code-model} -mabi=@var{name} -maddress-mode=@var{mode} @gol -m32 -m64 -mx32 -mlarge-data-threshold=@var{num} @gol -msse2avx -mfentry -m8bit-idiv @gol --mavx256-split-unaligned-load -mavx256-split-unaligned-store} +-mavx256-split-unaligned-load -mavx256-split-unaligned-store @gol +-mstack-protector-guard=@var{guard}} @emph{i386 and x86-64 Windows Options} @gccoptlist{-mconsole -mcygwin -mno-cygwin -mdll @gol @@ -14521,6 +14522,13 @@ @opindex avx256-split-unaligned-store Split 32-byte AVX unaligned load and store. +@item -mstack-protector-guard=@var{guard} +@opindex mstack-protector-guard=@var{guard} +Generate stack protection code using canary at @var{guard}. Supported +locations are @samp{global} or @samp{tls} per thread at %gs:20 (the default). +This option has effect only when @option{-fstack-protector} +or @option{-fstack-protector-all} is also specified. + @end table These @samp{-m} switches are supported in addition to the above