From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mumit Khan To: binutils@sourceware.cygnus.com Subject: (patch) PE-COFF type tags in exported symbols Date: Sat, 28 Aug 1999 13:03:00 -0000 Message-id: <199908282003.PAA32487@mercury.xraylith.wisc.edu> X-SW-Source: 1999-08/msg00173.html 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 * 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