public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Decorated symbols in import libs (BUG 30421)
@ 2023-05-15  8:50 Luca Bacci
  2023-05-16 14:12 ` Nick Clifton
  0 siblings, 1 reply; 5+ messages in thread
From: Luca Bacci @ 2023-05-15  8:50 UTC (permalink / raw)
  To: binutils; +Cc: Luca Bacci

Signed-off-by: Luca Bacci <luca.bacci@outlook.com>
---
 bfd/cofflink.c      | 34 +++++++++++++++++++++++++++++++++-
 bfd/libcoff-in.h    | 11 +++++++++++
 bfd/libcoff.h       | 11 +++++++++++
 ld/emultempl/pe.em  | 29 ++++++++++++++++++++++++++++-
 ld/emultempl/pep.em | 25 +++++++++++++++++++++++++
 ld/pe-dll.c         | 39 ++++++++++++++++++++++++++++++++-------
 6 files changed, 140 insertions(+), 9 deletions(-)

diff --git a/bfd/cofflink.c b/bfd/cofflink.c
index 9baec2fc712..24c4a2b0ad7 100644
--- a/bfd/cofflink.c
+++ b/bfd/cofflink.c
@@ -89,6 +89,34 @@ _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
   return (struct bfd_hash_entry *) ret;
 }
 
+struct bfd_hash_entry *
+_decoration_hash_newfunc (struct bfd_hash_entry *entry,
+			  struct bfd_hash_table *table,
+			  const char *string)
+{
+  struct decoration_hash_entry *ret = (struct decoration_hash_entry *) entry;
+
+  /* Allocate the structure if it has not already been allocated by a
+     subclass.  */
+  if (ret == NULL)
+    {
+      ret = (struct decoration_hash_entry *)
+	    bfd_hash_allocate (table, sizeof (struct decoration_hash_entry));
+      if (ret == NULL)
+	return NULL;
+    }
+
+  /* Call the allocation method of the superclass.  */
+  ret = (struct decoration_hash_entry *)
+	_bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
+  if (ret != NULL)
+    {
+      ret->decorated_link = NULL;
+    }
+
+  return (struct bfd_hash_entry *) ret;
+}
+
 /* Initialize a COFF linker hash table.  */
 
 bool
@@ -100,7 +128,11 @@ _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
 				unsigned int entsize)
 {
   memset (&table->stab_info, 0, sizeof (table->stab_info));
-  return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
+
+  return bfd_hash_table_init (&table->decoration_hash,
+			      _decoration_hash_newfunc,
+			      sizeof (struct decoration_hash_entry))
+	 &&_bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
 }
 
 /* Create a COFF linker hash table.  */
diff --git a/bfd/libcoff-in.h b/bfd/libcoff-in.h
index d0839c76e96..af59486c925 100644
--- a/bfd/libcoff-in.h
+++ b/bfd/libcoff-in.h
@@ -282,6 +282,9 @@ struct coff_link_hash_table
   struct bfd_link_hash_table root;
   /* A pointer to information used to link stabs in sections.  */
   struct stab_info stab_info;
+  /* Hash table that maps undecorated names to their respective
+   * decorated coff_link_hash_entry as found in fixup_stdcalls  */
+  struct bfd_hash_table decoration_hash;
 };
 
 struct coff_reloc_cookie
@@ -313,6 +316,12 @@ struct coff_reloc_cookie
 
 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
 
+struct decoration_hash_entry
+{
+  struct bfd_hash_entry root;
+  struct bfd_link_hash_entry *decorated_link;
+};
+
 /* Functions in coffgen.c.  */
 extern bfd_cleanup coff_object_p
   (bfd *);
@@ -560,6 +569,8 @@ struct coff_section_alignment_entry
   unsigned int alignment_power;
 };
 
+extern struct bfd_hash_entry *_decoration_hash_newfunc
+  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
 extern struct bfd_hash_entry *_bfd_coff_link_hash_newfunc
   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
 extern bool _bfd_coff_link_hash_table_init
diff --git a/bfd/libcoff.h b/bfd/libcoff.h
index 235d5c3c843..9084832da97 100644
--- a/bfd/libcoff.h
+++ b/bfd/libcoff.h
@@ -286,6 +286,9 @@ struct coff_link_hash_table
   struct bfd_link_hash_table root;
   /* A pointer to information used to link stabs in sections.  */
   struct stab_info stab_info;
+  /* Hash table that maps undecorated names to their respective
+   * decorated coff_link_hash_entry as found in fixup_stdcalls  */
+  struct bfd_hash_table decoration_hash;
 };
 
 struct coff_reloc_cookie
@@ -317,6 +320,12 @@ struct coff_reloc_cookie
 
 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
 
+struct decoration_hash_entry
+{
+  struct bfd_hash_entry root;
+  struct bfd_link_hash_entry *decorated_link;
+};
+
 /* Functions in coffgen.c.  */
 extern bfd_cleanup coff_object_p
   (bfd *);
@@ -564,6 +573,8 @@ struct coff_section_alignment_entry
   unsigned int alignment_power;
 };
 
+extern struct bfd_hash_entry *_decoration_hash_newfunc
+  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
 extern struct bfd_hash_entry *_bfd_coff_link_hash_newfunc
   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
 extern bool _bfd_coff_link_hash_table_init
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 38cb61138bd..7d956bf555c 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1188,6 +1188,30 @@ change_undef (struct bfd_link_hash_entry * undef,
   lang_add_gc_name (sym->root.string);
 }
 
+static void
+set_decoration (const char *undecorated_name,
+		struct bfd_link_hash_entry * decoration)
+{
+  static bool  gave_warning_message = false;
+  struct decoration_hash_entry *entry;
+
+  if (is_underscoring () && undecorated_name[0] == '_')
+    undecorated_name++;
+
+  entry = (struct decoration_hash_entry *)
+	  bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
+			   undecorated_name, true /* create */, false /* copy */);
+
+  if (entry->decorated_link != NULL && !gave_warning_message)
+    {
+      einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
+	     entry->decorated_link->root.string, undecorated_name);
+      gave_warning_message = true;
+    }
+
+  entry->decorated_link = decoration;
+}
+
 static void
 pe_fixup_stdcalls (void)
 {
@@ -1231,7 +1255,10 @@ pe_fixup_stdcalls (void)
 	    bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
 				    (char *) name);
 	    if (pe_undef_found_sym)
-	      change_undef (undef, pe_undef_found_sym);
+	      {
+		change_undef (undef, pe_undef_found_sym);
+		set_decoration (undef->root.string, pe_undef_found_sym);
+	      }
 	  }
       }
 }
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 32883b27706..99e71951b8a 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1119,6 +1119,30 @@ pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
   return true;
 }
 
+static void
+set_decoration (const char *undecorated_name,
+		struct bfd_link_hash_entry * decoration)
+{
+  static bool  gave_warning_message = false;
+  struct decoration_hash_entry *entry;
+
+  if (is_underscoring () && undecorated_name[0] == '_')
+    undecorated_name++;
+
+  entry = (struct decoration_hash_entry *)
+	  bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
+			   undecorated_name, true /* create */, false /* copy */);
+
+  if (entry->decorated_link != NULL && !gave_warning_message)
+    {
+      einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
+	     entry->decorated_link->root.string, undecorated_name);
+      gave_warning_message = true;
+    }
+
+  entry->decorated_link = decoration;
+}
+
 static void
 pep_fixup_stdcalls (void)
 {
@@ -1180,6 +1204,7 @@ pep_fixup_stdcalls (void)
 		undef->type = bfd_link_hash_defined;
 		undef->u.def.value = sym->u.def.value;
 		undef->u.def.section = sym->u.def.section;
+		set_decoration (undef->root.string, sym);
 
 		if (pep_enable_stdcall_fixup == -1)
 		  {
diff --git a/ld/pe-dll.c b/ld/pe-dll.c
index 8d74dba6af0..e45ae104265 100644
--- a/ld/pe-dll.c
+++ b/ld/pe-dll.c
@@ -2321,6 +2321,31 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
   bfd *abfd;
   const unsigned char *jmp_bytes = NULL;
   int jmp_byte_count = 0;
+  const char *internal_name = exp->internal_name;
+
+  if (!exp->flag_noname)
+    {
+      /* Check for a decorated symbol name */
+      struct decoration_hash_entry *entry;
+
+      entry = (struct decoration_hash_entry *)
+	      bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
+			       internal_name, false, false);
+      if (entry)
+	{
+	  if (entry->decorated_link)
+	    {
+	      internal_name = entry->decorated_link->root.string;
+
+	      if (pe_details->underscored && internal_name[0] == '_')
+		internal_name++;
+	    }
+	  else
+	    {
+	      einfo (_("%P: error: NULL decorated name for %s\n"), internal_name);
+	    }
+	}
+    }
 
   /* Include the jump stub section only if it is needed. A jump
      stub is needed if the symbol being imported <sym> is a function
@@ -2382,13 +2407,13 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
 
-  if  (*exp->internal_name == '@')
+  if  (*internal_name == '@')
     {
       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
 		    BSF_GLOBAL, 0);
       if (include_jmp_stub)
-	quick_symbol (abfd, "", exp->internal_name, "", tx, BSF_GLOBAL, 0);
-      quick_symbol (abfd, "__imp_", exp->internal_name, "", id5,
+	quick_symbol (abfd, "", internal_name, "", tx, BSF_GLOBAL, 0);
+      quick_symbol (abfd, "__imp_", internal_name, "", id5,
 		    BSF_GLOBAL, 0);
       /* Fastcall applies only to functions,
 	 so no need for auto-import symbol.  */
@@ -2398,18 +2423,18 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
 		    BSF_GLOBAL, 0);
       if (include_jmp_stub)
-	quick_symbol (abfd, U (""), exp->internal_name, "", tx,
+	quick_symbol (abfd, U (""), internal_name, "", tx,
 		      BSF_GLOBAL, 0);
-      quick_symbol (abfd, "__imp_", U (""), exp->internal_name, id5,
+      quick_symbol (abfd, "__imp_", U (""), internal_name, id5,
 		    BSF_GLOBAL, 0);
       /* Symbol to reference ord/name of imported
 	 data symbol, used to implement auto-import.  */
       if (exp->flag_data)
-	quick_symbol (abfd, "__nm_", U (""), exp->internal_name, id6,
+	quick_symbol (abfd, "__nm_", U (""), internal_name, id6,
 		      BSF_GLOBAL,0);
     }
   if (pe_dll_compat_implib)
-    quick_symbol (abfd, "___imp_", exp->internal_name, "", id5,
+    quick_symbol (abfd, "___imp_", internal_name, "", id5,
 		  BSF_GLOBAL, 0);
 
   if (include_jmp_stub)
-- 
2.40.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Decorated symbols in import libs (BUG 30421)
  2023-05-15  8:50 [PATCH] Decorated symbols in import libs (BUG 30421) Luca Bacci
@ 2023-05-16 14:12 ` Nick Clifton
  2023-05-16 17:04   ` R: " Luca Bacci
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Clifton @ 2023-05-16 14:12 UTC (permalink / raw)
  To: Luca Bacci, binutils

Hi Luca,

   With this patch applied, I am seeing a regression in the linker
   testsuite:

     FAIL: ld-pe/pr19803

   This is happening for at least the following targets:

     sh-pe, arm-wince-pe, i686-pc-mingw32, x86_64-pc-cygwin

   Please could you investigate.

Cheers
   Nick


^ permalink raw reply	[flat|nested] 5+ messages in thread

* R: [PATCH] Decorated symbols in import libs (BUG 30421)
  2023-05-16 14:12 ` Nick Clifton
@ 2023-05-16 17:04   ` Luca Bacci
  2023-05-17 12:40     ` Nick Clifton
  0 siblings, 1 reply; 5+ messages in thread
From: Luca Bacci @ 2023-05-16 17:04 UTC (permalink / raw)
  To: Nick Clifton, binutils

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

Hi Nick, thanks for taking a look at this!

I have prepared local builds for the four triplets above, but ld tests always pass, regardless if the patch is applied or not.

May I ask how your local build is configured? Moreover:

* On top of which commit is the patch applied?
* Do the dx.dll dx.dll.a files ever get created in <builddir>/ld when pr19803 fails?
* If so, does dx.dll.a contain the testval symbol?

Best Regards!
Luca
________________________________
Da: Nick Clifton <nickc@redhat.com>
Inviato: martedì 16 maggio 2023 16:12
A: Luca Bacci <luca.bacci@outlook.com>; binutils@sourceware.org <binutils@sourceware.org>
Oggetto: Re: [PATCH] Decorated symbols in import libs (BUG 30421)

Hi Luca,

   With this patch applied, I am seeing a regression in the linker
   testsuite:

     FAIL: ld-pe/pr19803

   This is happening for at least the following targets:

     sh-pe, arm-wince-pe, i686-pc-mingw32, x86_64-pc-cygwin

   Please could you investigate.

Cheers
   Nick


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: R: [PATCH] Decorated symbols in import libs (BUG 30421)
  2023-05-16 17:04   ` R: " Luca Bacci
@ 2023-05-17 12:40     ` Nick Clifton
  2023-05-17 13:12       ` Luca Bacci
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Clifton @ 2023-05-17 12:40 UTC (permalink / raw)
  To: Luca Bacci, binutils

Hi Luca,

> I have prepared local builds for the four triplets above, but ld tests always pass, regardless if the patch is applied or not.

Sorry - it was my fault.  I had another local patch applied
at the same time and this was causing the problems that I
reported.

I have now checked your patch with a clean set of sources,
found no problems, and gone ahead and committed it.

Cheers
   Nick



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: R: [PATCH] Decorated symbols in import libs (BUG 30421)
  2023-05-17 12:40     ` Nick Clifton
@ 2023-05-17 13:12       ` Luca Bacci
  0 siblings, 0 replies; 5+ messages in thread
From: Luca Bacci @ 2023-05-17 13:12 UTC (permalink / raw)
  To: Nick Clifton, binutils

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

Great, thank you very much!

BR,
Luca

Inviato da Outlook per Android<https://aka.ms/AAb9ysg>
________________________________
From: Nick Clifton <nickc@redhat.com>
Sent: Wednesday, May 17, 2023 2:40:45 PM
To: Luca Bacci <luca.bacci@outlook.com>; binutils@sourceware.org <binutils@sourceware.org>
Subject: Re: R: [PATCH] Decorated symbols in import libs (BUG 30421)

Hi Luca,

> I have prepared local builds for the four triplets above, but ld tests always pass, regardless if the patch is applied or not.

Sorry - it was my fault.  I had another local patch applied
at the same time and this was causing the problems that I
reported.

I have now checked your patch with a clean set of sources,
found no problems, and gone ahead and committed it.

Cheers
   Nick



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-05-17 13:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-15  8:50 [PATCH] Decorated symbols in import libs (BUG 30421) Luca Bacci
2023-05-16 14:12 ` Nick Clifton
2023-05-16 17:04   ` R: " Luca Bacci
2023-05-17 12:40     ` Nick Clifton
2023-05-17 13:12       ` Luca Bacci

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