COFF: Check for symbols defined in discarded section

Message ID 20190315130151.3477-1-hjl.tools@gmail.com
State New
Headers show
Series
  • COFF: Check for symbols defined in discarded section
Related show

Commit Message

H.J. Lu March 15, 2019, 1:01 p.m.
For LTO, a symbol may be defined in discarded section.  We should mark
such symbol as undefined so that LTO plugin will make IR definition
available.

Verified with the original failed case.

	PR ld/24267
	* coffgen.c (_bfd_coff_section_already_linked): Skip discarded
	section.
	* cofflink.c (coff_link_add_symbols): Check for symbols defined
	in discarded section.
---
 bfd/coffgen.c  | 3 +++
 bfd/cofflink.c | 6 +++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

-- 
2.20.1

Comments

Alan Modra March 15, 2019, 1:30 p.m. | #1
On Fri, Mar 15, 2019 at 09:01:51PM +0800, H.J. Lu wrote:
> 	PR ld/24267

> 	* coffgen.c (_bfd_coff_section_already_linked): Skip discarded

> 	section.

> 	* cofflink.c (coff_link_add_symbols): Check for symbols defined

> 	in discarded section.


Looks good.

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index 5db35c7ddf..ccf4b4302c 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -2652,6 +2652,9 @@  _bfd_coff_section_already_linked (bfd *abfd,
   struct bfd_section_already_linked_hash_entry *already_linked_list;
   struct coff_comdat_info *s_comdat;
 
+  if (sec->output_section == bfd_abs_section_ptr)
+    return FALSE;
+
   flags = sec->flags;
   if ((flags & SEC_LINK_ONCE) == 0)
     return FALSE;
diff --git a/bfd/cofflink.c b/bfd/cofflink.c
index e4031b9a31..ac07d05e57 100644
--- a/bfd/cofflink.c
+++ b/bfd/cofflink.c
@@ -310,7 +310,9 @@  coff_link_add_symbols (bfd *abfd,
 	    case COFF_SYMBOL_GLOBAL:
 	      flags = BSF_EXPORT | BSF_GLOBAL;
 	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
-	      if (! obj_pe (abfd))
+	      if (discarded_section (section))
+		section = bfd_und_section_ptr;
+	      else if (! obj_pe (abfd))
 		value -= section->vma;
 	      break;
 
@@ -327,6 +329,8 @@  coff_link_add_symbols (bfd *abfd,
 	    case COFF_SYMBOL_PE_SECTION:
 	      flags = BSF_SECTION_SYM | BSF_GLOBAL;
 	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
+	      if (discarded_section (section))
+		section = bfd_und_section_ptr;
 	      break;
 	    }