[PATCH 2/3] dlltool: Add --short-implib [PR34527]

Zhongteng Gui dragon-archer@outlook.com
Wed Nov 25 10:27:04 GMT 2026


Add an option to write the import library given by --output-lib with
short import members, using bfd_pe_write_short_import_library.  Public
symbols and DLL lookup names follow the existing long format, including
--kill-at, --ext-prefix-alias and PRIVATE exports.  Delay import
libraries are not affected.

Short import members record the DLL name, so require one.  Build the
--ext-prefix-alias export by copying the whole export for both formats.

	PR binutils/34527

binutils/
	* dlltool.c (short_implib): New variable.
	(prefix_alias, short_export, gen_short_lib_file): New functions.
	(gen_lib_file): Call gen_short_lib_file for --short-implib.  Use
	prefix_alias.
	(usage): Mention --short-implib.
	(OPTION_SHORT_IMPLIB): New.
	(long_options): Add short-implib.
	(main): Handle OPTION_SHORT_IMPLIB.  Reject --short-implib with
	--compat-implib, -x and -c, or without a DLL name.
	* doc/binutils.texi (dlltool): Document --short-implib.
	* NEWS: Mention --short-implib.

Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
---
 binutils/NEWS              |  3 ++
 binutils/dlltool.c         | 98 ++++++++++++++++++++++++++++++++------
 binutils/doc/binutils.texi |  7 +++
 3 files changed, 94 insertions(+), 14 deletions(-)

diff --git a/binutils/NEWS b/binutils/NEWS
index 2a8bd84f65c..699cbdb37cc 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -1,5 +1,8 @@
 -*- text -*-
 
+* dlltool supports --short-implib to generate short PE/COFF import library
+  members for i386 and x86-64.
+
 * RISC-V disassembly now supports the -M arch=ISA option, which names the ISA
   to disassemble for in place of the one the object records.  It is useful for
   an object that records none, such as a core file or a raw binary.
diff --git a/binutils/dlltool.c b/binutils/dlltool.c
index d8937f2344f..0a63fcafdc6 100644
--- a/binutils/dlltool.c
+++ b/binutils/dlltool.c
@@ -413,6 +413,7 @@ static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
 /* TRUE if we should add __imp_<SYMBOL> to import libraries for backward
    compatibility to old Cygwin releases.  */
 static bool create_compat_implib;
+static bool short_implib;
 
 /* TRUE if we have to write PE+ import libraries.  */
 static bool create_for_pep;
@@ -2346,6 +2347,35 @@ make_imp_label (bfd *abfd, const char *prefix, const char *name)
   return copy;
 }
 
+/* Return the --ext-prefix-alias import for EXP.  */
+
+static export_type
+prefix_alias (bfd *abfd, const export_type *exp)
+{
+  export_type alias_exp = *exp;
+
+  alias_exp.name = make_imp_label (abfd, ext_prefix_alias, exp->name);
+  alias_exp.import_name = exp->name;
+  return alias_exp;
+}
+
+/* Return the short import for EXP, named as by make_one_lib_file.  */
+
+static struct bfd_pe_short_export
+short_export (bfd *archive, export_type *exp)
+{
+  struct bfd_pe_short_export entry;
+
+  entry.symbol = make_imp_label (archive, "", exp->name);
+  entry.name = NULL;
+  if (!exp->noname)
+    entry.name = (exp->its_name ? xstrdup (exp->its_name)
+		  : xlate (exp->import_name));
+  entry.hint = exp->noname ? exp->ordinal : exp->hint;
+  entry.data = exp->data;
+  return entry;
+}
+
 static bfd *
 make_one_lib_file (export_type *exp, int i, int delay)
 {
@@ -2984,6 +3014,38 @@ make_delay_tail (void)
   return abfd;
 }
 
+static void
+gen_short_lib_file (bfd *outarch)
+{
+  struct bfd_pe_short_export *exports;
+  export_type *exp;
+  size_t count = 0;
+  int i;
+
+  exports = xmalloc (sizeof (*exports) * d_nfuncs * (ext_prefix_alias ? 2 : 1));
+  for (i = 0; (exp = d_exports_lexically[i]); i++)
+    {
+      if (exp->private)
+	continue;
+      exports[count++] = short_export (outarch, exp);
+      if (ext_prefix_alias)
+	{
+	  export_type alias_exp = prefix_alias (outarch, exp);
+
+	  exports[count++] = short_export (outarch, &alias_exp);
+	}
+    }
+
+  if (!bfd_pe_write_short_import_library (outarch, bfd_scan_arch (mname),
+					  dll_name, exports, count,
+					  use_nul_prefixed_import_tables,
+					  NULL))
+    bfd_fatal (imp_name);
+  while (count--)
+    free ((char *) exports[count].name);
+  free (exports);
+}
+
 static void
 gen_lib_file (int delay)
 {
@@ -3015,6 +3077,15 @@ gen_lib_file (int delay)
   if (deterministic)
     outarch->flags |= BFD_DETERMINISTIC_OUTPUT;
 
+  if (short_implib && !delay)
+    {
+      gen_short_lib_file (outarch);
+      if (! bfd_close (outarch))
+	bfd_fatal (imp_name);
+      inform (_("Created lib file"));
+      return;
+    }
+
   /* Work out a reasonable size of things to put onto one line.  */
   if (delay)
     {
@@ -3041,21 +3112,9 @@ gen_lib_file (int delay)
       head = n;
       if (ext_prefix_alias)
 	{
-	  export_type alias_exp;
+	  export_type alias_exp = prefix_alias (outarch, exp);
 
 	  assert (i < PREFIX_ALIAS_BASE);
-	  alias_exp.name = make_imp_label (outarch, ext_prefix_alias, exp->name);
-	  alias_exp.internal_name = exp->internal_name;
-	  alias_exp.its_name = exp->its_name;
-	  alias_exp.import_name = exp->name;
-	  alias_exp.ordinal = exp->ordinal;
-	  alias_exp.constant = exp->constant;
-	  alias_exp.noname = exp->noname;
-	  alias_exp.private = exp->private;
-	  alias_exp.data = exp->data;
-	  alias_exp.hint = exp->hint;
-	  alias_exp.forward = exp->forward;
-	  alias_exp.next = exp->next;
 	  n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay);
 	  n->archive_next = head;
 	  head = n;
@@ -3739,6 +3798,7 @@ usage (FILE *file, int status)
   fprintf (file, _("        possible <machine>: arm[_interwork], arm64, i386, mcore[-elf]{-le|-be}, thumb\n"));
   fprintf (file, _("   -e --output-exp <outname> Generate an export file.\n"));
   fprintf (file, _("   -l --output-lib <outname> Generate an interface library.\n"));
+  fprintf (file, _("      --short-implib         Use short import library members.\n"));
   fprintf (file, _("   -y --output-delaylib <outname> Create a delay-import library.\n"));
   fprintf (file, _("      --deterministic-libraries\n"));
   if (DEFAULT_AR_DETERMINISTIC)
@@ -3803,7 +3863,8 @@ enum command_line_switch
   OPTION_NO_LEADING_UNDERSCORE,
   OPTION_LEADING_UNDERSCORE,
   OPTION_DETERMINISTIC_LIBRARIES,
-  OPTION_NON_DETERMINISTIC_LIBRARIES
+  OPTION_NON_DETERMINISTIC_LIBRARIES,
+  OPTION_SHORT_IMPLIB
 };
 
 static const struct option long_options[] =
@@ -3841,6 +3902,7 @@ static const struct option long_options[] =
   {"output-delaylib", required_argument, NULL, 'y'},
   {"output-exp", required_argument, NULL, 'e'},
   {"output-lib", required_argument, NULL, 'l'},
+  {"short-implib", no_argument, NULL, OPTION_SHORT_IMPLIB},
   {"temp-prefix", required_argument, NULL, 't'},
   {"use-nul-prefixed-import-tables", no_argument, NULL, OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
   {"verbose", no_argument, NULL, 'v'},
@@ -3880,6 +3942,9 @@ main (int ac, char **av)
     {
       switch (c)
 	{
+	case OPTION_SHORT_IMPLIB:
+	  short_implib = true;
+	  break;
 	case OPTION_EXPORT_ALL_SYMS:
 	  export_all_symbols = true;
 	  break;
@@ -4114,6 +4179,11 @@ main (int ac, char **av)
 
   if (imp_name)
     {
+      if (short_implib && (create_compat_implib || no_idata4 || no_idata5))
+	fatal (_("--short-implib is incompatible with --compat-implib, -x and -c"));
+      if (short_implib && !dll_name)
+	fatal (_("--short-implib requires a DLL name; use --dllname or"
+		 " LIBRARY in the .def file"));
       /* Make imp_name safe for use as a label.  */
       char *p;
 
diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index c40c89df631..52c266d52f7 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -4745,6 +4745,7 @@ dlltool [@option{-d}|@option{--input-def} @var{def-file-name}]
         [@option{-e}|@option{--output-exp} @var{exports-file-name}]
         [@option{-z}|@option{--output-def} @var{def-file-name}]
         [@option{-l}|@option{--output-lib} @var{library-file-name}]
+        [@option{--short-implib}]
         [@option{-y}|@option{--output-delaylib} @var{library-file-name}]
         [@option{--export-all-symbols}]
         [@option{--no-export-all-symbols}]
@@ -4890,6 +4891,12 @@ Specifies the name of the @file{.def} file to be created by dlltool.
 @itemx --output-lib @var{filename}
 Specifies the name of the library file to be created by dlltool.
 
+@item --short-implib
+Use short COFF import objects for the exports in the library created by
+@option{--output-lib}.  This option is available for i386 and x86-64.
+It is incompatible with @option{--compat-implib}, @option{--no-idata4},
+and @option{--no-idata5}.  It does not affect @option{--output-delaylib}.
+
 @item -y @var{filename}
 @itemx --output-delaylib @var{filename}
 Specifies the name of the delay-import library file to be created by dlltool.
-- 
2.56.1.windows.1



More information about the Binutils mailing list