From patchwork Fri Aug 14 07:20:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 1344699 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Crrkp90C; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BSZbB0dlWz9sPB for ; Fri, 14 Aug 2020 17:20:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD9633857016; Fri, 14 Aug 2020 07:20:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD9633857016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1597389617; bh=lJUPZILT4vsgN+mq6MaGeNKG+pATxaVB1xVTkPN/v8k=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Crrkp90CGEaxB5HofAsnDbelIrYhe/l9y5mYgx0toKeSrZcTytyI3d9qm/78Pi/rh KttlgN++HEj4il/GTl3cViLAxFW+arVtgbEzUeKwYZ+c4ax1Q6ZT9osN4pknh9F4JW l1ZG6n4xXF/ybDfira+aByIZHegd9u0VQ8Z+cEXc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 859E03857C42 for ; Fri, 14 Aug 2020 07:20:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 859E03857C42 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07E73Qbb087875; Fri, 14 Aug 2020 03:20:13 -0400 Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 32w24ht23a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Aug 2020 03:20:12 -0400 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 07E7AtLG028414; Fri, 14 Aug 2020 07:20:10 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma03ams.nl.ibm.com with ESMTP id 32skp8ebcg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Aug 2020 07:20:10 +0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 07E7K7hb30736698 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 14 Aug 2020 07:20:07 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8571B52073; Fri, 14 Aug 2020 07:20:07 +0000 (GMT) Received: from [9.197.244.182] (unknown [9.197.244.182]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 6615452052; Fri, 14 Aug 2020 07:20:06 +0000 (GMT) To: gcc-patches Subject: [PATCH] Add support for putting jump table into relocation read-only section Message-ID: <5b8f4979-bc2d-2ace-b28e-08e6bde0bb3f@linux.ibm.com> Date: Fri, 14 Aug 2020 15:20:03 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-14_02:2020-08-13, 2020-08-14 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 suspectscore=9 spamscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 clxscore=1015 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008140054 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: HAO CHEN GUI via Gcc-patches From: HAO CHEN GUI Reply-To: HAO CHEN GUI Cc: wschmidt@linux.ibm.com, segher@kernel.crashing.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch adds a section selection for jump tables. The jump tables can be put into read-only data section or relocation read-only data section by the relocation flags. When the PIC flag is set and jump table is non-relative, the jump table is put into relocation read-only section. section * select_jump_table_section (tree decl) {   int reloc;   bool section_reloc;   reloc = (! CASE_VECTOR_PC_RELATIVE && flag_pic &&            ! targetm.asm_out.generate_pic_addr_diff_vec ()) ? 1 : 0;   section_reloc = (reloc & targetm.asm_out.reloc_rw_mask ());   return targetm.asm_out.function_rodata_section (decl, section_reloc); } The attachments are the patch diff file and change log file. Bootstrapped and tested on powerpc64le-linux-gnu with no regressions.  Is this okay for trunk? Any recommendations? Thanks a lot. * final.c (select_jump_table_section): Implement a function to select the section of jump tables by reloc_rw_mask and other flags. * output.h (default_function_rodata_section, default_no_function_rodata_section): Add the second argument to the declarations. * target.def (function_rodata_section): Change the doc and add the second argument. * doc/tm.texi: Regenerate. * varasm.c (default_function_rodata_section, default_no_function_rodata_section, function_mergeable_rodata_prefix): Add the second argument in default_function_rodata_section. It indicates the section should be read-only or relocation read-only. Add the second argument in default_function_rodata_section. Set the second argument to false when default_function_rodata_section calls function_rodata_section. * config/mips/mips.c (mips_function_rodata_section): Add the second arugment and set it to false when it calls function_rodata_section. * config/s390/s390.c (targetm.asm_out.function_rodata_section): Set the second argument to false. * config/s390/s390.md Likewise. diff --git a/gcc/varasm.c b/gcc/varasm.c diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 6e7d9dc54a9..3ff7527f2cc 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -7712,6 +7712,15 @@ if function is in @code{.text.name}, and the normal readonly-data section otherwise. @end deftypefn +@deftypefn {Target Hook} {section *} TARGET_ASM_FUNCTION_RELRO_SECTION (tree @var{decl}) +Return the relro section associated with +@samp{DECL_SECTION_NAME (@var{decl})}. +The default version of this function selects @code{.gnu.linkonce.r.name} if +the function's section is @code{.gnu.linkonce.t.name}, +@code{.data.rel.ro.name} if function is in @code{.text.name}, +and the normal data relro section otherwise. +@end deftypefn + @deftypevr {Target Hook} {const char *} TARGET_ASM_MERGEABLE_RODATA_PREFIX Usually, the compiler uses the prefix @code{".rodata"} to construct section names for mergeable constant data. Define this macro to override diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 3be984bbd5c..0ac1488c837 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -5007,6 +5007,8 @@ it is unlikely to be called. @hook TARGET_ASM_FUNCTION_RODATA_SECTION +@hook TARGET_ASM_FUNCTION_RELRO_SECTION + @hook TARGET_ASM_MERGEABLE_RODATA_PREFIX @hook TARGET_ASM_TM_CLONE_TABLE_SECTION diff --git a/gcc/final.c b/gcc/final.c index a3601964a8d..ba198182663 100644 --- a/gcc/final.c +++ b/gcc/final.c @@ -107,6 +107,10 @@ along with GCC; see the file COPYING3. If not see #define JUMP_TABLES_IN_TEXT_SECTION 0 #endif +#ifndef JUMP_TABLES_IN_RELRO +#define JUMP_TABLES_IN_RELRO 0 +#endif + /* Bitflags used by final_scan_insn. */ #define SEEN_NOTE 1 #define SEEN_EMITTED 2 @@ -2154,6 +2158,23 @@ asm_show_source (const char *filename, int linenum) fputc ('\n', asm_out_file); } +/* Select sections for jump table. + If the jump table need to be relocated, + select relro sections. Otherwise in readonly section */ + +section * +select_jump_table_section (tree decl) +{ + int relco; + + relco = JUMP_TABLES_IN_RELRO; + + if (relco & targetm.asm_out.reloc_rw_mask ()) + return targetm.asm_out.function_relro_section (decl); + else + return targetm.asm_out.function_rodata_section (decl); +} + /* The final scan for one insn, INSN. Args are same as in `final', except that INSN is the insn being scanned. @@ -2492,7 +2513,7 @@ final_scan_insn_1 (rtx_insn *insn, FILE *file, int optimize_p ATTRIBUTE_UNUSED, { int log_align; - switch_to_section (targetm.asm_out.function_rodata_section + switch_to_section (select_jump_table_section (current_function_decl)); #ifdef ADDR_VEC_ALIGN @@ -2571,7 +2592,7 @@ final_scan_insn_1 (rtx_insn *insn, FILE *file, int optimize_p ATTRIBUTE_UNUSED, #endif if (! JUMP_TABLES_IN_TEXT_SECTION) - switch_to_section (targetm.asm_out.function_rodata_section + switch_to_section (select_jump_table_section (current_function_decl)); else switch_to_section (current_function_section ()); diff --git a/gcc/output.h b/gcc/output.h index eb253c50329..9a463c42955 100644 --- a/gcc/output.h +++ b/gcc/output.h @@ -573,6 +573,7 @@ extern section *default_elf_select_section (tree, int, unsigned HOST_WIDE_INT); extern void default_unique_section (tree, int); extern section *default_function_rodata_section (tree); extern section *default_no_function_rodata_section (tree); +extern section *default_function_relro_section (tree); extern section *default_clone_table_section (void); extern section *default_select_rtx_section (machine_mode, rtx, unsigned HOST_WIDE_INT); diff --git a/gcc/target.def b/gcc/target.def index 07059a87caf..9bd61b4e018 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -561,6 +561,18 @@ otherwise.", section *, (tree decl), default_function_rodata_section) +/* Return the relro section associated with function DECL. */ +DEFHOOK +(function_relro_section, + "Return the relro section associated with\n\ +@samp{DECL_SECTION_NAME (@var{decl})}.\n\ +The default version of this function selects @code{.gnu.linkonce.r.name} if\n\ +the function's section is @code{.gnu.linkonce.t.name}, \n\ +@code{.data.rel.ro.name} if function is in @code{.text.name}, \n\ +and the normal data relro section otherwise.", + section *, (tree decl), + default_function_relro_section) + /* Nonnull if the target wants to override the default ".rodata" prefix for mergeable data sections. */ DEFHOOKPOD