From patchwork Wed Jun 10 03:06:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Pitre X-Patchwork-Id: 49673 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 84C0821419 for ; Wed, 10 Jun 2015 03:06:57 +0000 (UTC) Received: by wifx6 with SMTP id x6sf8913225wif.1 for ; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-subscribe:list-archive:list-post:list-help :sender:delivered-to:date:from:to:subject:message-id:user-agent :mime-version:content-type:x-original-sender :x-original-authentication-results; bh=sgJYCiitzytafxxkH11klMeZrhTNdY/DFREaTx0tqYg=; b=TyNnA/lt+XUCVPS0PK4ho+ZHEE1x416rZe8Lewq9so32F2GCyr63Gi5jYPydRFsRY9 Tgd7ZeIEsnOjzfndJsw/yEk7NSPpuRAVapA7rXn6N/FyM3yVWsiCJO5Y2TGvr4D6saCO mt+b75eFgICSiQ2g0HYrm72IqD/vdKYTnU3pn0X/QGOCrEOcEPz5n+VzGn3k2A6baB/I cUek/upeMXrkLe4vIkgRaV26d8Z4ciEBZLpKgy+0Xo9GIZ2W4FdDKNmKgHZKZudmSAcI hpplsqgdsI07Tq2qGKBMe1parj4GEGrLm1/gwlzWnj4fbu7tk4qsGVpiK8U+8DFDuggp rHeQ== X-Gm-Message-State: ALoCoQl4RLomNOMQO7CVXk7v7OfP+MBR+5OBxrfHLCzjmA0I2Ie+585qqg5V4IHMcEjnqpoo3gkr X-Received: by 10.194.58.164 with SMTP id s4mr873850wjq.3.1433905616813; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.121.98 with SMTP id lj2ls151682lab.94.gmail; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) X-Received: by 10.152.22.99 with SMTP id c3mr876432laf.32.1433905616535; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) Received: from mail-lb0-x233.google.com (mail-lb0-x233.google.com. [2a00:1450:4010:c04::233]) by mx.google.com with ESMTPS id r5si7449205lbp.16.2015.06.09.20.06.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jun 2015 20:06:56 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::233 as permitted sender) client-ip=2a00:1450:4010:c04::233; Received: by lbcmx3 with SMTP id mx3so20633548lbc.1 for ; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) X-Received: by 10.152.29.161 with SMTP id l1mr835570lah.76.1433905616396; Tue, 09 Jun 2015 20:06:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp3026980lbb; Tue, 9 Jun 2015 20:06:54 -0700 (PDT) X-Received: by 10.68.99.197 with SMTP id es5mr1458446pbb.131.1433905614164; Tue, 09 Jun 2015 20:06:54 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id mp9si11480989pbc.124.2015.06.09.20.06.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jun 2015 20:06:54 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-return-88072-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 36692 invoked by alias); 10 Jun 2015 03:06:39 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list List-Id: List-Unsubscribe: , List-Subscribe: List-Archive: List-Post: , List-Help: , Sender: binutils-owner@sourceware.org Delivered-To: mailing list binutils@sourceware.org Received: (qmail 36679 invoked by uid 89); 10 Jun 2015 03:06:36 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=BAYES_20, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-qc0-f169.google.com Received: from mail-qc0-f169.google.com (HELO mail-qc0-f169.google.com) (209.85.216.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 10 Jun 2015 03:06:34 +0000 Received: by qcej9 with SMTP id j9so13003604qce.1 for ; Tue, 09 Jun 2015 20:06:32 -0700 (PDT) X-Received: by 10.55.40.18 with SMTP id o18mr1677599qkh.58.1433905592400; Tue, 09 Jun 2015 20:06:32 -0700 (PDT) Received: from xanadu.home (modemcable142.180-131-66.mc.videotron.ca. [66.131.180.142]) by mx.google.com with ESMTPSA id 140sm3582675qhg.16.2015.06.09.20.06.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 09 Jun 2015 20:06:31 -0700 (PDT) Date: Tue, 9 Jun 2015 23:06:29 -0400 (EDT) From: Nicolas Pitre To: binutils@sourceware.org Subject: [PATCH] gas: section name substitution sequence Message-ID: User-Agent: Alpine 2.11 (LFD 23 2013-08-11) MIME-Version: 1.0 X-Original-Sender: nicolas.pitre@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::233 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@sourceware.org X-Google-Group-Id: 836684582541 I created a patch on top of upstream binutils for a feature I need which should be universally useful. Therefore I'm posting this here in the hope it could be applied to the mainline binutils tree. If this is not the right procedure for patch submission then please advise. ----- >8 Subject: [PATCH] gas: introduce section name substitution support This patch adds the ability to automatically construct a section name based on the prior section. When gas is invoked with --sectname-subst, the occurrence of %S in a section name will be substituted by the name of the current section. For example: .macro exception_code .pushsection %S.exception [exception code here] .popsection .endm .text [code] exception_code [...] .section .init [init code] exception_code [...] The first and second exception_code invocations create the .text.exception and the .init.exception sections respectively. This is useful e.g. to discriminate between anciliary sections that are tied to .init code and can be discarded at run time when initialization is over vs anciliary sections tied to .text sections that need to stay resident. This would also allow for actually omitting __exit sections from the Linux kernel binary for compiled-in modules even when that code generates exception table entries. Signed-off-by: Nicolas Pitre diff --git a/gas/ChangeLog b/gas/ChangeLog index 4b78a51..9fa602c 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,11 @@ +2015-06-03 Nicolas Pitre + + * as.c (show_usage): Document --sectname-subst. + (parse_args): Add --sectname-subst. + * as.h (flag_sectname_subst): New. + * config/obj-elf.c (obj_elf_section_name): Add %S substitution. + * doc/as.texinfo: Document it. + 2015-06-08 Nick Clifton * config/tc-rx.c (rx_op): Correct handling of integer bignums. diff --git a/gas/as.c b/gas/as.c index 2a8923f..fecfcd2 100644 --- a/gas/as.c +++ b/gas/as.c @@ -284,6 +284,8 @@ Options:\n\ fprintf (stream, _("\ --size-check=[error|warning]\n\ ELF .size directive check (default --size-check=error)\n")); + fprintf (stream, _("\ + --sectname-subst enable section name substitution sequences\n")); #endif fprintf (stream, _("\ -f skip whitespace and comment preprocessing\n")); @@ -447,6 +449,7 @@ parse_args (int * pargc, char *** pargv) OPTION_EXECSTACK, OPTION_NOEXECSTACK, OPTION_SIZE_CHECK, + OPTION_SECTNAME_SUBST, OPTION_ALTERNATE, OPTION_AL, OPTION_HASH_TABLE_SIZE, @@ -481,6 +484,7 @@ parse_args (int * pargc, char *** pargv) ,{"execstack", no_argument, NULL, OPTION_EXECSTACK} ,{"noexecstack", no_argument, NULL, OPTION_NOEXECSTACK} ,{"size-check", required_argument, NULL, OPTION_SIZE_CHECK} + ,{"sectname-subst", no_argument, NULL, OPTION_SECTNAME_SUBST} #endif ,{"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL} ,{"gdwarf-2", no_argument, NULL, OPTION_GDWARF2} @@ -848,6 +852,10 @@ This program has absolutely no warranty.\n")); else as_fatal (_("Invalid --size-check= option: `%s'"), optarg); break; + + case OPTION_SECTNAME_SUBST: + flag_sectname_subst = 1; + break; #endif case 'Z': flag_always_generate_output = 1; diff --git a/gas/as.h b/gas/as.h index 6de319e..635b2c5 100644 --- a/gas/as.h +++ b/gas/as.h @@ -589,6 +589,9 @@ COMMON enum size_check_warning } flag_size_check; + +/* If section name substitution sequences should be honored */ +COMMON int flag_sectname_subst; #endif #ifndef DOLLAR_AMBIGU diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c index 4d7a8a7..78dc6d9 100644 --- a/gas/config/obj-elf.c +++ b/gas/config/obj-elf.c @@ -917,6 +917,27 @@ obj_elf_section_name (void) name = (char *) xmalloc (end - input_line_pointer + 1); memcpy (name, input_line_pointer, end - input_line_pointer); name[end - input_line_pointer] = '\0'; + + while (flag_sectname_subst) + { + char *subst = strchr (name, '%'); + if (subst && subst[1] == 'S') + { + int oldlen = strlen (name); + int substlen = strlen (now_seg->name); + int newlen = oldlen - 2 + substlen; + char *newname = (char *) xmalloc (newlen + 1); + int headlen = subst - name; + memcpy (newname, name, headlen); + strcpy (newname + headlen, now_seg->name); + strcat (newname + headlen, subst + 2); + xfree (name); + name = newname; + } + else + break; + } + #ifdef tc_canonicalize_section_name name = tc_canonicalize_section_name (name); #endif diff --git a/gas/doc/as.texinfo b/gas/doc/as.texinfo index 5710e1c..6694b23 100644 --- a/gas/doc/as.texinfo +++ b/gas/doc/as.texinfo @@ -238,7 +238,7 @@ gcc(1), ld(1), and the Info entries for @file{binutils} and @file{ld}. @var{objfile}] [@b{-R}] [@b{--reduce-memory-overheads}] [@b{--statistics}] [@b{-v}] [@b{-version}] [@b{--version}] [@b{-W}] [@b{--warn}] [@b{--fatal-warnings}] [@b{-w}] [@b{-x}] [@b{-Z}] [@b{@@@var{FILE}}] - [@b{--size-check=[error|warning]}] + [@b{--sectname-subst}] [@b{--size-check=[error|warning]}] [@b{--target-help}] [@var{target-options}] [@b{--}|@var{files} @dots{}] @c @@ -766,6 +766,14 @@ This option reduces GAS's memory requirements, at the expense of making the assembly processes slower. Currently this switch is a synonym for @samp{--hash-size=4051}, but in the future it may have other effects as well. +@ifset ELF +@item --sectname-subst +Honor substitution sequences in section names. +@ifclear man +@xref{Section Name Substitutions,,@code{.section @var{name}}}. +@end ifclear +@end ifset + @item --statistics Print the maximum space (in bytes) and total time (in seconds) used by assembly. @@ -6259,6 +6267,38 @@ For ELF targets, the @code{.section} directive is used like this: .section @var{name} [, "@var{flags}"[, @@@var{type}[,@var{flag_specific_arguments}]]] @end smallexample +@anchor{Section Name Substitutions} +@kindex --sectname-subst +@cindex section name substitution +If the @samp{--sectname-subst} command-line option is provided, the @var{name} +argument may contain a substitution sequence. Only @code{%S} is supported +at the moment, and substitutes the current section name. For example: + +@smallexample +.macro exception_code +.section %S.exception +[exception code here] +.previous +.endm + +.text +[code] +exception_code +[...] + +.section .init +[init code] +exception_code +[...] +@end smallexample + +The two @code{exception_code} invocations above would create the +@code{.text.exception} and @code{.init.exception} sections respectively. +This is useful e.g. to discriminate between anciliary sections that are +tied to setup code to be discarded after use from anciliary sections that +need to stay resident without having to define multiple @code{exception_code} +macros just for that purpose. + The optional @var{flags} argument is a quoted string which may contain any combination of the following characters: @table @code