gas: Generate a new section for SHF_GNU_RETAIN
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 9 Dec 2020 00:41:22 +0000 (16:41 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 9 Dec 2020 00:41:32 +0000 (16:41 -0800)
For
.globl foo2
.section .data.foo,"aR"
.align 4
.type foo2, @object
.size foo2, 4
foo2:
.long 2
.globl foo1
.section .data.foo
.align 4
.type foo1, @object
.size foo1, 4
foo1:
.long 1

generate a new section if the SHF_GNU_RETAIN bit doesn't match.

* config/obj-elf.c (SEC_ASSEMBLER_SHF_MASK): New.
(get_section_by_match): Also check if SEC_ASSEMBLER_SHF_MASK of
sh_flags matches.  Rename info to sh_info.
(obj_elf_change_section): Don't check previous SHF_GNU_RETAIN.
Rename info to sh_info.
(obj_elf_section): Rename info to sh_info.  Set sh_flags for
SHF_GNU_RETAIN.
* config/obj-elf.h (elf_section_match): Rename info to sh_info.
Add sh_flags.
* testsuite/gas/elf/elf.exp: Run section27.
* testsuite/gas/elf/section24b.d: Updated.
* testsuite/gas/elf/section27.d: New file.
* testsuite/gas/elf/section27.s: Likewise.

gas/ChangeLog
gas/config/obj-elf.c
gas/config/obj-elf.h
gas/testsuite/gas/elf/elf.exp
gas/testsuite/gas/elf/section24b.d
gas/testsuite/gas/elf/section27.d [new file with mode: 0644]
gas/testsuite/gas/elf/section27.s [new file with mode: 0644]

index c1310373ab193d721e2cbb18f11476464ec35cfd..987a47c154f92e3e7135d5d29d53b328d0687d6b 100644 (file)
@@ -1,3 +1,19 @@
+2020-12-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * config/obj-elf.c (SEC_ASSEMBLER_SHF_MASK): New.
+       (get_section_by_match): Also check if SEC_ASSEMBLER_SHF_MASK of
+       sh_flags matches.  Rename info to sh_info.
+       (obj_elf_change_section): Don't check previous SHF_GNU_RETAIN.
+       Rename info to sh_info.
+       (obj_elf_section): Rename info to sh_info.  Set sh_flags for
+       SHF_GNU_RETAIN.
+       * config/obj-elf.h (elf_section_match): Rename info to sh_info.
+       Add sh_flags.
+       * testsuite/gas/elf/elf.exp: Run section27.
+       * testsuite/gas/elf/section24b.d: Updated.
+       * testsuite/gas/elf/section27.d: New file.
+       * testsuite/gas/elf/section27.s: Likewise.
+
 2020-12-04  Andreas Krebbel  <krebbel@linux.ibm.com>
 
        * testsuite/gas/s390/zarch-z10.s: Add tests for risbgz.
index 54d42d9ecbf3555625372429c06bca10eb34a008..5d3b1a0edc4f8742bf3a9a49072b7c459e320726 100644 (file)
@@ -519,6 +519,9 @@ struct section_stack
 
 static struct section_stack *section_stack;
 
+/* ELF section flags for unique sections.  */
+#define SEC_ASSEMBLER_SHF_MASK SHF_GNU_RETAIN
+
 /* Return TRUE iff SEC matches the section info INF.  */
 
 static bfd_boolean
@@ -529,9 +532,12 @@ get_section_by_match (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
   const char *group_name = elf_group_name (sec);
   const char *linked_to_symbol_name
     = sec->map_head.linked_to_symbol_name;
-  unsigned int info = elf_section_data (sec)->this_hdr.sh_info;
+  unsigned int sh_info = elf_section_data (sec)->this_hdr.sh_info;
+  bfd_vma sh_flags = (elf_section_data (sec)->this_hdr.sh_flags
+                     & SEC_ASSEMBLER_SHF_MASK);
 
-  return (info == match->info
+  return (sh_info == match->sh_info
+         && sh_flags == match->sh_flags
          && ((bfd_section_flags (sec) & SEC_ASSEMBLER_SECTION_ID)
               == (match->flags & SEC_ASSEMBLER_SECTION_ID))
          && sec->section_id == match->section_id
@@ -740,7 +746,7 @@ obj_elf_change_section (const char *name,
        type = bfd_elf_get_default_section_type (flags);
       elf_section_type (sec) = type;
       elf_section_flags (sec) = attr;
-      elf_section_data (sec)->this_hdr.sh_info = match_p->info;
+      elf_section_data (sec)->this_hdr.sh_info = match_p->sh_info;
 
       /* Prevent SEC_HAS_CONTENTS from being inadvertently set.  */
       if (type == SHT_NOBITS)
@@ -806,17 +812,9 @@ obj_elf_change_section (const char *name,
                as_bad (_("changed section attributes for %s"), name);
            }
          else
-           {
-             /* Don't overwrite a previously set SHF_GNU_RETAIN flag for the
-                section.  The entire section must be marked retained.  */
-             if ((elf_tdata (stdoutput)->has_gnu_osabi & elf_gnu_osabi_retain)
-                 && ((elf_section_flags (old_sec) & SHF_GNU_RETAIN)))
-               attr |= SHF_GNU_RETAIN;
-
-             /* FIXME: Maybe we should consider removing a previously set
-                processor or application specific attribute as suspicious ?  */
-             elf_section_flags (sec) = attr;
-           }
+           /* FIXME: Maybe we should consider removing a previously set
+              processor or application specific attribute as suspicious?  */
+           elf_section_flags (sec) = attr;
 
          if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
            as_bad (_("changed section entity size for %s"), name);
@@ -1322,18 +1320,21 @@ obj_elf_section (int push)
              if (ISDIGIT (* input_line_pointer))
                {
                  char *t = input_line_pointer;
-                 match.info = strtoul (input_line_pointer,
+                 match.sh_info = strtoul (input_line_pointer,
                                        &input_line_pointer, 0);
-                 if (match.info == (unsigned int) -1)
+                 if (match.sh_info == (unsigned int) -1)
                    {
                      as_warn (_("unsupported mbind section info: %s"), t);
-                     match.info = 0;
+                     match.sh_info = 0;
                    }
                }
              else
                input_line_pointer = save;
            }
 
+         if ((gnu_attr & SHF_GNU_RETAIN) != 0)
+           match.sh_flags |= SHF_GNU_RETAIN;
+
          if (*input_line_pointer == ',')
            {
              char *save = input_line_pointer;
index 4f29572eef70f8b6219d59b80ec8a1c673ad5da7..c714ba7a7088d8afc1eba2582b2c47b52caea485 100644 (file)
@@ -106,8 +106,9 @@ struct elf_section_match
 {
   const char *   group_name;
   const char *   linked_to_symbol_name;
-  unsigned int   info;
   unsigned int   section_id;
+  unsigned int   sh_info;              /* ELF section information.  */
+  bfd_vma        sh_flags;             /* ELF section flags.  */
   flagword       flags;
 };
 
index 25c40a28105ba36350bf552fc25485f55ef881a3..80dec4effcbb62eefd6aa9c286688097ce24a1d9 100644 (file)
@@ -268,6 +268,7 @@ if { [is_elf_format] } then {
     run_dump_test "section24b"
     run_dump_test "section25"
     run_dump_test "section26"
+    run_dump_test "section27"
     run_dump_test "sh-link-zero"
     run_dump_test "dwarf2-1" $dump_opts
     run_dump_test "dwarf2-2" $dump_opts
index 451ec21635420acaf6959327674c502e0d46ad60..03dd2916efcf9686f353bc34f9618e36625e9d4c 100644 (file)
@@ -3,8 +3,12 @@
 #source: section24.s
 #readelf: -S --wide
 
-#failif
 #...
-  \[..\] .(text|data|bss|rodata)[      ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 [^R] .*
+  \[..\] .text[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 +AX .*
+#...
+  \[..\] .data[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 +WA .*
+#...
+  \[..\] .bss[         ]+NOBITS[       ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 +WA .*
+#...
+  \[..\] .rodata[      ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 +A .*
 #pass
-
diff --git a/gas/testsuite/gas/elf/section27.d b/gas/testsuite/gas/elf/section27.d
new file mode 100644 (file)
index 0000000..da0734f
--- /dev/null
@@ -0,0 +1,19 @@
+#readelf: -h -S --wide
+#name: SHF_GNU_RETAIN sections 27
+#notarget: ![supports_gnu_osabi]
+
+#...
+ +OS/ABI: +UNIX - (GNU|FreeBSD)
+#...
+  \[..\] .text[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00  AX.*
+#...
+  \[..\] .data[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00  WA.*
+#...
+  \[..\] .bss[         ]+NOBITS[       ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00  WA.*
+#...
+  \[..\] .bss[         ]+NOBITS[       ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 WAR.*
+#...
+  \[..\] .data[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 WAR.*
+#...
+  \[..\] .text[        ]+PROGBITS[     ]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ 00 AXR.*
+#pass
diff --git a/gas/testsuite/gas/elf/section27.s b/gas/testsuite/gas/elf/section27.s
new file mode 100644 (file)
index 0000000..78e410f
--- /dev/null
@@ -0,0 +1,34 @@
+       .section        .bss,"aw"
+       .global discard0
+       .type   discard0, %object
+discard0:
+       .zero   2
+
+       .section        .data,"aw"
+       .global discard1
+       .type   discard1, %object
+discard1:
+       .word   1
+
+       .text
+       .global discard2
+       .type   discard2, %function
+discard2:
+       .word   0
+
+       .section        .bss,"awR",%nobits
+       .global retain0
+       .type   retain0, %object
+retain0:
+       .zero   2
+
+       .section        .data,"awR",%progbits
+       .type   retain1, %object
+retain1:
+       .word   1
+
+       .section        .text,"axR",%progbits
+       .global retain2
+       .type   retain2, %function
+retain2:
+       .word   0