]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gas: section name substitution sequence
authorNicolas Pitre <nicolas.pitre@linaro.org>
Wed, 10 Jun 2015 03:06:29 +0000 (23:06 -0400)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 23 Aug 2017 20:57:45 +0000 (17:57 -0300)
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.

* 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.

Change-Id: Id580667a5ef03a1d7a11e9bdabd8012703025311

gas/ChangeLog
gas/as.c
gas/as.h
gas/config/obj-elf.c
gas/doc/as.texinfo

index 627940a381467dbb7d387d2e5c61904179078f6b..248a50c99cad5c8208fb5d03fef0573b885e782e 100644 (file)
@@ -1,3 +1,11 @@
+2015-06-17  Nicolas Pitre <nico@linaro.org>
+
+       * 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.
+
 2014-12-23  Tristan Gingold  <gingold@adacore.com>
 
        * configure: Regenerate.
index 85a04a3757c6a0c210441bc988460ed53acb3329..dd9194585dccc88f49e2c10f3746617abb8fb3fc 100644 (file)
--- a/gas/as.c
+++ b/gas/as.c
@@ -286,6 +286,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"));
@@ -449,6 +451,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,
@@ -483,6 +486,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}
@@ -834,6 +838,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;
index df04dce4f413039c52b70b67ce25127e48daa463..c81621a3aed90cff1b5ef8256fed9eebc9c8c843 100644 (file)
--- 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
index e2ef99e28d2c6ded702c66b05d1c79452e198a6c..063c7f9ea08d2cc75426f03e66f0e5e7d6673eab 100644 (file)
@@ -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
index 243851bead74beb845e15922bb2c554f8a905c4b..23f64e32c2bb4eb0f77dee113e80143424c88fc5 100644 (file)
@@ -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
@@ -747,6 +747,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.
@@ -6175,6 +6183,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