public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alex Coplan <alex.coplan@arm.com>
To: binutils@sourceware.org
Subject: [PATCH] gas: Fix internal error in S_SET_SEGMENT
Date: Wed, 22 Jul 2020 15:23:02 +0100	[thread overview]
Message-ID: <20200722142302.wi2rotbr3hbz57iw@arm.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1181 bytes --]

Hello,

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().

Testing:
 * New test fails before and passes after the change.
 * Regression tested on a wide variety of targets.

OK for master?

Thanks,
Alex

---

gas/ChangeLog:

2020-07-22  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/aarch64/section-symbol-redef.d: New test.
	* testsuite/gas/aarch64/section-symbol-redef.s: Input for test.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1475 bytes --]

diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index de22b5a1da8..c11a1da2295 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -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));
     }
diff --git a/gas/testsuite/gas/aarch64/section-symbol-redef.d b/gas/testsuite/gas/aarch64/section-symbol-redef.d
new file mode 100644
index 00000000000..06a56b50553
--- /dev/null
+++ b/gas/testsuite/gas/aarch64/section-symbol-redef.d
@@ -0,0 +1,8 @@
+#objdump: -D
+
+.*:     file format .*
+
+Disassembly of section myseg:
+
+0+ <.*>:
+.*:	d65f03c0 	ret
diff --git a/gas/testsuite/gas/aarch64/section-symbol-redef.s b/gas/testsuite/gas/aarch64/section-symbol-redef.s
new file mode 100644
index 00000000000..a80398d129c
--- /dev/null
+++ b/gas/testsuite/gas/aarch64/section-symbol-redef.s
@@ -0,0 +1,3 @@
+myseg=not_defined_here
+.section myseg
+ret

             reply	other threads:[~2020-07-22 14:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-22 14:23 Alex Coplan [this message]
2020-07-22 14:40 ` H.J. Lu
2020-08-03 14:22   ` Alex Coplan
2020-08-17  9:07     ` Alex Coplan
2020-08-17 11:26       ` Nick Clifton
2020-08-17 13:28         ` Alex Coplan
2020-09-01 15:47           ` Alex Coplan
2020-09-01 23:44             ` Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200722142302.wi2rotbr3hbz57iw@arm.com \
    --to=alex.coplan@arm.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).