public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Mumit Khan <khan@xraylith.wisc.EDU>
To: binutils@sourceware.cygnus.com
Subject: (patch) PE-COFF type tags in exported symbols
Date: Sat, 28 Aug 1999 13:03:00 -0000	[thread overview]
Message-ID: <199908282003.PAA32487@mercury.xraylith.wisc.edu> (raw)

The following adds support for type tags in exported symbols; Ian added
partial support in dlltool last year, and this is just some finishing
touches. 

This helps tracking down bugs in code that import variables from DLLs. 
The correct way to do import/export is the following:

  /* dll code. */
  __attribute__((dllexport)) int dll_var;

  /* user code. */
  __attribute__((dllimport)) extern int dll_var;
  
If you now forget the dllimport in user code, the code will simply crash
and it's a painful process when porting large packages. MSVC handles this
problem in one of two ways:
  
  - add a "data" type tag in the .drectve section (eg., 
   `-export:dll_var,data' instead of just `-export:dll_var'). This of
   course needs GCC support, and the patch is on its way. The syntax
   is compatible with what native compiler emits.
  - use DATA tag in the export definition file (GNU supports this)

In either case, the import library will not contain "dll_var", but only
__imp__dll_var, so that you'll get link errors instead of runtime crash.

(Note for DJ) I do have changes for pe-dll code as well, but not including
that since the changes are somewhat unclean, mostly due to the difference 
in the way pe-dll handles this issue (uses the same parser). Suggestions 
welcome. 

Sat Aug 28 12:43:04 1999  Mumit Khan  <khan@xraylith.wisc.edu>

	* dlltool.c (scan_drectve_symbols): Handle type tags in exported
	symbols.
	(scan_filtered_symbols): Likewise.
	
Index: dlltool.c
===================================================================
RCS file: /homes/khan/src/CVSROOT/binutils/binutils/dlltool.c,v
retrieving revision 1.1.1.1
diff -u -3 -p -r1.1.1.1 dlltool.c
--- dlltool.c	1999/08/14 17:26:25	1.1.1.1
+++ dlltool.c	1999/08/28 06:52:26
@@ -1178,7 +1178,9 @@ scan_drectve_symbols (abfd)
   inform (_("Sucking in info from %s section in %s\n"),
 	  DRECTVE_SECTION_NAME, bfd_get_filename (abfd));
 
-  /* Search for -export: strings */
+  /* Search for -export: strings. The exported symbols can optionally
+     have type tags (eg., -export:foo,data), so handle those as well. 
+     Currently only data tag is supported. */
   p = buf;
   e = buf + size;
   while (p < e)
@@ -1188,25 +1190,36 @@ scan_drectve_symbols (abfd)
 	{
 	  char * name;
 	  char * c;
+	  flagword flags = BSF_FUNCTION;
 	  
 	  p += 8;
 	  name = p;
-	  while (p < e && *p != ' ' && *p != '-')
+	  while (p < e && *p != ',' && *p != ' ' && *p != '-')
 	    p++;
 	  c = xmalloc (p - name + 1);
 	  memcpy (c, name, p - name);
 	  c[p - name] = 0;
+	  if (p < e && *p == ',')	/* found type tag. */
+	    {
+	      char *tag_start = ++p;
+	      while (p < e && *p != ' ' && *p != '-')
+		p++;
+	      if (strncmp (tag_start, "data", 4) == 0)
+	        flags &= ~BSF_FUNCTION;
+	    }
+
 
 	  /* FIXME: The 5th arg is for the `constant' field.
 	     What should it be?  Not that it matters since it's not
 	     currently useful.  */
-	  def_exports (c, 0, -1, 0, 0, 0);
+	  def_exports (c, 0, -1, 0, 0, ! (flags & BSF_FUNCTION));
 
 	  if (add_stdcall_alias && strchr (c, '@'))
 	    {
 	      char *exported_name = xstrdup (c);
 	      char *atsym = strchr (exported_name, '@');
 	      *atsym = '\0';
+	      /* Note: stdcall alias symbols can never be data. */
 	      def_exports (exported_name, xstrdup (c), -1, 0, 0, 0);
 	    }
 	}
@@ -1248,13 +1261,15 @@ scan_filtered_symbols (abfd, minisyms, s
       if (bfd_get_symbol_leading_char (abfd) == symbol_name[0])
 	++symbol_name;
 
-      def_exports (xstrdup (symbol_name) , 0, -1, 0, 0, 0);
+      def_exports (xstrdup (symbol_name) , 0, -1, 0, 0,
+                   ! (sym->flags & BSF_FUNCTION));
 
       if (add_stdcall_alias && strchr (symbol_name, '@'))
         {
 	  char *exported_name = xstrdup (symbol_name);
 	  char *atsym = strchr (exported_name, '@');
 	  *atsym = '\0';
+	  /* Note: stdcall alias symbols can never be data. */
 	  def_exports (exported_name, xstrdup (symbol_name), -1, 0, 0, 0);
 	}
     }

Regards,
Mumit

                 reply	other threads:[~1999-08-28 13:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=199908282003.PAA32487@mercury.xraylith.wisc.edu \
    --to=khan@xraylith.wisc.edu \
    --cc=binutils@sourceware.cygnus.com \
    /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).