gas: Fix internal error in S_SET_SEGMENT
authorAlex Coplan <alex.coplan@arm.com>
Mon, 17 Aug 2020 13:23:14 +0000 (14:23 +0100)
committerAlex Coplan <alex.coplan@arm.com>
Mon, 17 Aug 2020 13:23:14 +0000 (14:23 +0100)
This patch fixes an internal error in GAS when defining a section using
a symbol that has already been named but not defined. For a minimal
reproducer, try the following input:

a=b
.sect a

The problem is that obj_elf_change_section() happily reuses the symbol
"a" created by equals() without clearing the sy_value field: prior to
this patch, it just set bsym. This caused a problem when attempting to
resolve the section symbol, since resolve_symbol_value() ended up
resolving the symbol as if it were the original symbol created by
equals(), which ends up leaving the section symbol in the undefined
section instead of in section a, hence the call to abort() in
S_SET_SEGMENT().

gas/ChangeLog:

* config/obj-elf.c (obj_elf_change_section): When repurposing an
existing symbol, ensure that we set sy_value as per other (fresh)
section symbols.
* testsuite/gas/elf/elf.exp: Add new test.
* testsuite/gas/elf/section-symbol-redef.d: New test.
* testsuite/gas/elf/section-symbol-redef.s: Input for test.

gas/ChangeLog
gas/config/obj-elf.c
gas/testsuite/gas/elf/elf.exp
gas/testsuite/gas/elf/section-symbol-redef.d [new file with mode: 0644]
gas/testsuite/gas/elf/section-symbol-redef.s [new file with mode: 0644]

index d9b57f8662b07a3755769cce9cd6aef1cd279eaf..7c06ec9349c11cb4bf926d2ac7d124cdeffcbc60 100644 (file)
@@ -1,3 +1,12 @@
+2020-08-17  Alex Coplan  <alex.coplan@arm.com>
+
+       * config/obj-elf.c (obj_elf_change_section): When repurposing an
+       existing symbol, ensure that we set sy_value as per other (fresh)
+       section symbols.
+       * testsuite/gas/elf/elf.exp: Add new test.
+       * testsuite/gas/elf/section-symbol-redef.d: New test.
+       * testsuite/gas/elf/section-symbol-redef.s: Input for test.
+
 2020-08-13  Nick Clifton  <nickc@redhat.com>
 
        PR 26359
index de22b5a1da807ae232458ca1357dc1c8b9f1b892..c11a1da229592c7c017074ee942d0c8aa5ffae89 100644 (file)
@@ -759,7 +759,14 @@ obj_elf_change_section (const char *name,
       /* Add a symbol for this section to the symbol table.  */
       secsym = symbol_find (name);
       if (secsym != NULL)
-       symbol_set_bfdsym (secsym, sec->symbol);
+       {
+         /* We could be repurposing an undefined symbol here: make sure we
+            reset sy_value to look like other section symbols in order to avoid
+            trying to incorrectly resolve this section symbol later on.  */
+         static const expressionS expr = { .X_op = O_constant };
+         symbol_set_value_expression (secsym, &expr);
+         symbol_set_bfdsym (secsym, sec->symbol);
+       }
       else
        symbol_table_insert (section_symbol (sec));
     }
index 155f78efa7f8bed282d753d2f21aa72b69b51315..2f9893b3d2b23c1ed76d1cb39caa3d6683f26585 100644 (file)
@@ -305,6 +305,7 @@ if { [is_elf_format] } then {
     run_dump_test "strtab"
 
     run_dump_test "bignums"
+    run_dump_test "section-symbol-redef"
     
     load_lib gas-dg.exp
     dg-init
diff --git a/gas/testsuite/gas/elf/section-symbol-redef.d b/gas/testsuite/gas/elf/section-symbol-redef.d
new file mode 100644 (file)
index 0000000..2ce2452
--- /dev/null
@@ -0,0 +1,5 @@
+#readelf: -x myseg
+#notarget: bfin-*-* h8300-*
+
+Hex dump of section .*:
+  0x0+ 2a\s+\*
diff --git a/gas/testsuite/gas/elf/section-symbol-redef.s b/gas/testsuite/gas/elf/section-symbol-redef.s
new file mode 100644 (file)
index 0000000..87e6569
--- /dev/null
@@ -0,0 +1,3 @@
+  myseg=not_defined_here
+  .section myseg
+  .byte 42