This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix dlls for non underscored targets.


Hi all,

The attached patch fixes dll generation / import lib usage
for non underscored targets.

This is needed for the arm-wince-pe target.

- Although Windows CE is not underscored, the __imp_ prefix always gets two
underscores.
- The second undercore on the right of _imp__ and _nm__ really belong to
the symbol being prefixed.
- The _head_ prefix only should only get a double underscore on underscored targets.


This amounts to:

U ("_imp__") -> "__imp_" + ("_")
U ("_nm__") -> U ("_nm_") + ("_")
"__head_" -> U ("_head_")

Tested on cross to arm-wince-pe, and cross to i686-pc-cygwin for regressions.

I had to touch some fastcall stuff, which arm-wince doesn't have,
so can anyone from the Cygwin/MinGW camps have a look?

Please review and commit.

Cheers,
Pedro Alves

---

2006-09-13 Pedro Alves <pedro_alves@portugalmail.pt>

ld/

* pe-dll.c : Fix typo. Unconditionally put a double underscore on __imp_ throughout.
(auto-export) : New parameter 'possibly_underscored'. Skip underscore skipping on
non underscored targets.
(process_def_file) : Move underscore skipping decision to auto_export.
Tell auto-export if the symbol is possibly underscored.
(make_one) : Fix underscore handling for non underscored targets.
(pe_create_runtime_relocator_reference) : Underscore _pei386_runtime_relocator
conditionally.
* pe.em (U) : New macro.
(set_pe_subsystem) : Remove underscore from wince/WinMainCRTStartup.
(pe_find_data_imports) : Use U on "_head_".


? ld.diff
Index: pe-dll.c
===================================================================
RCS file: /cvs/src/src/ld/pe-dll.c,v
retrieving revision 1.87
diff -u -p -r1.87 pe-dll.c
--- pe-dll.c	21 Aug 2006 08:12:46 -0000	1.87
+++ pe-dll.c	13 Sep 2006 19:58:07 -0000
@@ -101,7 +101,7 @@
     (so, DLL name is referenced by multiple entries), and pointer to symbol
     name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
     pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
-    containing imported name. Here comes that "om the edge" problem mentioned
+    containing imported name. Here comes that "on the edge" problem mentioned
     above: PE specification rambles that name vector (OriginalFirstThunk)
     should run in parallel with addresses vector (FirstThunk), i.e. that they
     should have same number of elements and terminated with zero. We violate
@@ -422,7 +422,7 @@ pe_dll_add_excludes (const char *new_exc
    It can be used for contextual checks.  */
 
 static int
-auto_export (bfd *abfd, def_file *d, const char *n)
+auto_export (bfd *abfd, def_file *d, const char *n, bfd_boolean possibly_underscored)
 {
   int i;
   struct exclude_list_struct *ex;
@@ -432,9 +432,14 @@ auto_export (bfd *abfd, def_file *d, con
     libname = lbasename (abfd->my_archive->filename);
 
   /* We should not re-export imported stuff.  */
-  if (strncmp (n, "_imp_", 5) == 0)
+  if (strncmp (n, "__imp_", 6) == 0)
     return 0;
 
+  /* Some symbols are never underscored. Ex: fastcall functions.
+     In those cases, the underscore is part of the symbol name.  */
+  if (possibly_underscored && pe_details->underscored && *n == '_')
+    n++;
+
   for (i = 0; i < d->num_exports; i++)
     if (strcmp (d->exports[i].name, n) == 0)
       return 0;
@@ -588,8 +593,8 @@ process_def_file (bfd *abfd ATTRIBUTE_UN
 
 		  /* We should not re-export imported stuff.  */
 		  {
-		    char *name = xmalloc (strlen (sn) + 2 + 6);
-		    sprintf (name, "%s%s", U("_imp_"), sn);
+		    char *name = xmalloc (strlen (sn) + 6 + 1);
+		    sprintf (name, "%s%s", "__imp_", sn);
 
 		    blhe = bfd_link_hash_lookup (info->hash, name,
 						 FALSE, FALSE, FALSE);
@@ -599,10 +604,7 @@ process_def_file (bfd *abfd ATTRIBUTE_UN
 		      continue;
 		  }
 
-		  if (*sn == '_')
-		    sn++;
-
-		  if (auto_export (b, pe_def_file, sn))
+		  if (auto_export (b, pe_def_file, sn, TRUE))
 		    {
 		      def_file_export *p;
 		      p=def_file_add_export (pe_def_file, sn, 0, -1);
@@ -651,7 +653,8 @@ process_def_file (bfd *abfd ATTRIBUTE_UN
 	      char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
 
 	      *(strchr (tmp, '@')) = 0;
-	      if (auto_export (NULL, pe_def_file, tmp))
+	      /* Fastcall functions are never underscored.  */
+	      if (auto_export (NULL, pe_def_file, tmp, !lead_at))
 		def_file_add_export (pe_def_file, tmp,
 				     pe_def_file->exports[i].internal_name,
 				     -1);
@@ -1881,7 +1884,7 @@ make_one (def_file_export *exp, bfd *par
 		    BSF_GLOBAL, 0);
       if (! exp->flag_data)
 	quick_symbol (abfd, "", exp->internal_name, "", tx, BSF_GLOBAL, 0);
-      quick_symbol (abfd, U ("_imp_"), exp->internal_name, "", id5,
+      quick_symbol (abfd, "__imp_", exp->internal_name, "", id5,
 		    BSF_GLOBAL, 0);
       /* Fastcall applies only to functions,
 	 so no need for auto-import symbol.  */
@@ -1893,12 +1896,12 @@ make_one (def_file_export *exp, bfd *par
       if (! exp->flag_data)
 	quick_symbol (abfd, U (""), exp->internal_name, "", tx,
 		      BSF_GLOBAL, 0);
-      quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5,
+      quick_symbol (abfd, "__imp_", U (""), exp->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, U("_nm__"), exp->internal_name, "", id6,
+	quick_symbol (abfd, U ("_nm_"), U (""), exp->internal_name, id6,
 		      BSF_GLOBAL,0);
     }
   if (pe_dll_compat_implib)
@@ -2231,7 +2234,7 @@ pe_create_runtime_relocator_reference (b
   symtab = xmalloc (2 * sizeof (asymbol *));
   extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
 
-  quick_symbol (abfd, "", "__pei386_runtime_relocator", "", UNDSEC,
+  quick_symbol (abfd, "", U ("_pei386_runtime_relocator"), "", UNDSEC,
 		BSF_NO_FLAGS, 0);
 
   bfd_set_section_size (abfd, extern_rt_rel, 4);
@@ -2432,7 +2435,7 @@ pe_process_import_defs (bfd *output_bfd,
 	    char *name = xmalloc (len + 2 + 6);
 
  	    if (lead_at)
-	      sprintf (name, "%s%s", "",
+	      sprintf (name, "%s",
 		       pe_def_file->imports[i].internal_name);
 	    else
 	      sprintf (name, "%s%s",U (""),
@@ -2444,10 +2447,10 @@ pe_process_import_defs (bfd *output_bfd,
 	    if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
 	      {
 		if (lead_at)
-		  sprintf (name, "%s%s", U ("_imp_"),
+		  sprintf (name, "%s%s", "__imp_", 
 			   pe_def_file->imports[i].internal_name);
 		else
-		  sprintf (name, "%s%s", U ("_imp__"),
+		  sprintf (name, "%s%s%s", "__imp_", U (""),
 			   pe_def_file->imports[i].internal_name);
 
 		blhe = bfd_link_hash_lookup (link_info->hash, name,
Index: emultempl/pe.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/pe.em,v
retrieving revision 1.119
diff -u -p -r1.119 pe.em
--- emultempl/pe.em	21 Aug 2006 08:12:46 -0000	1.119
+++ emultempl/pe.em	13 Sep 2006 19:58:13 -0000
@@ -115,6 +115,7 @@ cat >>e${EMULATION_NAME}.c <<EOF
 #define PE_DEF_FILE_ALIGNMENT		0x00000200
 #endif
 
+#define U(S) (${INITIAL_SYMBOL_CHAR} S)
 
 static struct internal_extra_pe_aouthdr pe;
 static int dll;
@@ -400,7 +401,7 @@ set_pe_subsystem (void)
       { "windows", 2, "WinMainCRTStartup" },
       { "console", 3, "mainCRTStartup" },
       { "posix",   7, "__PosixProcessStartup"},
-      { "wince",   9, "_WinMainCRTStartup" },
+      { "wince",   9, "WinMainCRTStartup" },
       { "xbox",   14, "mainCRTStartup" },
       { NULL, 0, NULL }
     };
@@ -925,15 +926,15 @@ pe_find_data_imports (void)
 
 	      for (i = 0; i < nsyms; i++)
 		{
-		  if (memcmp (symbols[i]->name, "__head_",
-			      sizeof ("__head_") - 1))
+		  if (memcmp (symbols[i]->name, U ("_head_"),
+			      sizeof (U ("_head_")) - 1))
 		    continue;
 
 		  if (pe_dll_extra_pe_debug)
 		    printf ("->%s\n", symbols[i]->name);
 
 		  pe_data_import_dll = (char*) (symbols[i]->name +
-						sizeof ("__head_") - 1);
+						sizeof (U ("_head_")) - 1);
 		  break;
 		}
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]