[PATCH 3/3] ld: Add --short-implib for PE targets [PR34527]
Zhongteng Gui
dragon-archer@outlook.com
Wed Nov 25 10:27:52 GMT 2026
Add an option to write the import library given by --out-implib with
short import members, using bfd_pe_write_short_import_library. Name the
import symbols as make_one does: with --kill-at or a stdcall fixup the
DLL exports the undecorated name, but callers refer to the decorated
one. Make the archive deterministic when --no-insert-timestamp is given.
Rerun the import table tests with short import libraries from both ld
and dlltool, check that the members are short, and check that both tools
write the same archive. Add a test that links a caller of a stdcall
function through a short import library.
PR binutils/34527
ld/
* deffile.h (def_file_export): Add implib_name.
* deffilep.y (def_file_free): Free implib_name.
* pe-dll.h (pe_dll_short_implib): Declare.
* pep-dll.h (pep_dll_short_implib): Declare.
* pep-dll.c (pe_dll_short_implib): Define as pep_dll_short_implib.
* pe-dll.c (pe_dll_short_implib): New variable.
(process_def_file_and_drectve): Keep the decorated name in
implib_name for --kill-at. Free implib_name of duplicates.
(import_symbol_name): New function, split out of make_one.
(short_export): New function.
(make_one): Use import_symbol_name.
(pe_dll_generate_implib): Write a short import library for
--short-implib.
* ldlex.h (OPTION_SHORT_IMPLIB): New.
* emultempl/pe.em (gld${EMULATION_NAME}_add_options): Add
short-implib.
(gld${EMULATION_NAME}_list_options): Mention it.
(gld${EMULATION_NAME}_handle_option): Handle it.
* emultempl/pep.em: Likewise.
* ld.texi (--short-implib): Document.
* NEWS: Mention --short-implib.
* testsuite/ld-pe/pe.exp: Run the import table tests with short
import libraries. Run short-implib-stdcall.
* testsuite/ld-pe/short-implib-stdcall.d: New test.
* testsuite/ld-pe/short-implib-stdcall.def: New file.
* testsuite/ld-pe/short-implib-stdcall.s: New file.
* testsuite/ld-pe/short-implib-stdcall-user.s: New file.
Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
---
ld/NEWS | 3 +
ld/deffile.h | 1 +
ld/deffilep.y | 1 +
ld/emultempl/pe.em | 5 ++
ld/emultempl/pep.em | 5 ++
ld/ld.texi | 8 ++
ld/ldlex.h | 1 +
ld/pe-dll.c | 86 ++++++++++++++++---
ld/pe-dll.h | 1 +
ld/pep-dll.c | 1 +
ld/pep-dll.h | 1 +
ld/testsuite/ld-pe/pe.exp | 41 +++++++++
.../ld-pe/short-implib-stdcall-user.s | 8 ++
ld/testsuite/ld-pe/short-implib-stdcall.d | 14 +++
ld/testsuite/ld-pe/short-implib-stdcall.def | 3 +
ld/testsuite/ld-pe/short-implib-stdcall.s | 4 +
16 files changed, 172 insertions(+), 11 deletions(-)
create mode 100644 ld/testsuite/ld-pe/short-implib-stdcall-user.s
create mode 100644 ld/testsuite/ld-pe/short-implib-stdcall.d
create mode 100644 ld/testsuite/ld-pe/short-implib-stdcall.def
create mode 100644 ld/testsuite/ld-pe/short-implib-stdcall.s
diff --git a/ld/NEWS b/ld/NEWS
index c6438422396..bcc1ebe269d 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,8 @@
-*- text -*-
+* The i386 and x86-64 PE targets support --short-implib to generate short
+ COFF import library members with --out-implib.
+
* The alpha target supports indirect functions (STT_GNU_IFUNC), using the
newly allocated R_ALPHA_IRELATIVE (42) dynamic relocation. No dynamic
linker or C library startup code implements that relocation yet, so a
diff --git a/ld/deffile.h b/ld/deffile.h
index 83bc4b1c90d..92025490057 100644
--- a/ld/deffile.h
+++ b/ld/deffile.h
@@ -35,6 +35,7 @@ typedef struct def_file_export {
char *name; /* always set */
char *internal_name; /* always set, may == name */
char *its_name; /* optional export table name referred to. */
+ char *implib_name; /* public name before --kill-at, or NULL */
int ordinal; /* -1 if not specified */
int hint;
char flag_private, flag_constant, flag_noname, flag_data, flag_forward;
diff --git a/ld/deffilep.y b/ld/deffilep.y
index e715c4d25f4..44b520fb70e 100644
--- a/ld/deffilep.y
+++ b/ld/deffilep.y
@@ -464,6 +464,7 @@ def_file_free (def_file *fdef)
free (fdef->exports[i].internal_name);
free (fdef->exports[i].name);
free (fdef->exports[i].its_name);
+ free (fdef->exports[i].implib_name);
}
free (fdef->exports);
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index d6e2ae11822..d346b701e4a 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -290,6 +290,7 @@ gld${EMULATION_NAME}_add_options
treating -c as an abbreviation for these --compat-implib. */
{"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
{"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
+ {"short-implib", no_argument, NULL, OPTION_SHORT_IMPLIB},
{"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
{"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
{"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
@@ -432,6 +433,7 @@ gld${EMULATION_NAME}_list_options (FILE *file)
fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
+ fprintf (file, _(" --short-implib Use short import library members\n"));
fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
create __imp_<SYMBOL> as well.\n"));
fprintf (file, _(" --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
@@ -805,6 +807,9 @@ gld${EMULATION_NAME}_handle_option (int optc)
case OPTION_IMP_COMPAT:
pe_dll_compat_implib = 1;
break;
+ case OPTION_SHORT_IMPLIB:
+ pe_dll_short_implib = 1;
+ break;
case OPTION_ENABLE_AUTO_IMAGE_BASE:
pe_enable_auto_image_base = 1;
if (optarg && *optarg)
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 81e86054a57..608b27b0e4a 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -303,6 +303,7 @@ gld${EMULATION_NAME}_add_options
treating -c as an abbreviation for these --compat-implib. */
{"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
{"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
+ {"short-implib", no_argument, NULL, OPTION_SHORT_IMPLIB},
{"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
{"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
{"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
@@ -435,6 +436,7 @@ gld${EMULATION_NAME}_list_options (FILE *file)
fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
+ fprintf (file, _(" --short-implib Use short import library members\n"));
fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
create __imp_<SYMBOL> as well\n"));
fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
@@ -776,6 +778,9 @@ gld${EMULATION_NAME}_handle_option (int optc)
case OPTION_IMP_COMPAT:
pep_dll_compat_implib = 1;
break;
+ case OPTION_SHORT_IMPLIB:
+ pep_dll_short_implib = 1;
+ break;
case OPTION_ENABLE_AUTO_IMAGE_BASE:
pep_enable_auto_image_base = 1;
break;
diff --git a/ld/ld.texi b/ld/ld.texi
index 963d47fd59a..bc0ceee9c04 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -2500,6 +2500,14 @@ behaviour makes it possible to skip a separate import library creation
step (eg. @code{dlltool} for DLLs). This option is only available for
the PE and ELF targetted ports of the linker.
+@kindex --short-implib
+@item --short-implib
+Use short COFF import objects for the exports in the import library
+created by @option{--out-implib}. This option is available for i386 and
+x86-64 PE targets and is incompatible with @option{--compat-implib}.
+Use @option{--no-insert-timestamp} to make the short import library
+deterministic.
+
@kindex -pie
@kindex --pic-executable
@item -pie
diff --git a/ld/ldlex.h b/ld/ldlex.h
index 1f6e6e5d387..35ec1ad885e 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -350,6 +350,7 @@ enum option_values
OPTION_DISABLE_STDCALL_FIXUP,
OPTION_WARN_DUPLICATE_EXPORTS,
OPTION_IMP_COMPAT,
+ OPTION_SHORT_IMPLIB,
OPTION_ENABLE_AUTO_IMAGE_BASE,
OPTION_DISABLE_AUTO_IMAGE_BASE,
OPTION_DLL_SEARCH_PREFIX,
diff --git a/ld/pe-dll.c b/ld/pe-dll.c
index 5e3c651642c..96eeb6d28fe 100644
--- a/ld/pe-dll.c
+++ b/ld/pe-dll.c
@@ -156,6 +156,7 @@ int pe_dll_kill_ats = 0;
int pe_dll_stdcall_aliases = 0;
int pe_dll_warn_dup_exports = 0;
int pe_dll_compat_implib = 0;
+int pe_dll_short_implib = 0;
int pe_dll_extra_pe_debug = 0;
int pe_use_nul_prefixed_import_tables = 0;
int pe_use_coff_long_section_names = -1;
@@ -869,6 +870,9 @@ process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *
else
einfo (_("%X%P: cannot export %s: invalid export name\n"),
pe_def_file->exports[i].name);
+ if (pe_dll_short_implib)
+ pe_def_file->exports[i].implib_name
+ = pe_def_file->exports[i].name;
pe_def_file->exports[i].name = tmp;
resort_needed = true;
}
@@ -939,6 +943,7 @@ process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *
free (e[i].name);
free (e[i].internal_name);
free (e[i].its_name);
+ free (e[i].implib_name);
}
else
{
@@ -2309,16 +2314,12 @@ static const unsigned char jmp_arm_bytes[] =
};
-static bfd *
-make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
+/* Return the name, without the user label prefix, of the symbols that
+ import EXP. */
+
+static const char *
+import_symbol_name (const def_file_export *exp)
{
- asection *tx, *id7, *id5, *id4, *id6;
- unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
- int len;
- char *oname;
- bfd *abfd;
- const unsigned char *jmp_bytes = NULL;
- int jmp_byte_count = 0;
const char *internal_name = exp->internal_name;
if (!exp->flag_noname)
@@ -2344,6 +2345,37 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
}
}
}
+ return internal_name;
+}
+
+/* Return the short import for EXP, named as by make_one. */
+
+static struct bfd_pe_short_export
+short_export (const def_file_export *exp)
+{
+ const char *name = (exp->implib_name ? exp->implib_name
+ : import_symbol_name (exp));
+ struct bfd_pe_short_export entry;
+
+ entry.symbol = concat (name[0] == '@' ? "" : U (""), name, NULL);
+ entry.name = (exp->flag_noname ? NULL
+ : exp->its_name ? exp->its_name : exp->name);
+ entry.hint = exp->flag_noname ? exp->ordinal : exp->hint;
+ entry.data = exp->flag_data;
+ return entry;
+}
+
+static bfd *
+make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
+{
+ asection *tx, *id7, *id5, *id4, *id6;
+ unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
+ int len;
+ char *oname;
+ bfd *abfd;
+ const unsigned char *jmp_bytes = NULL;
+ int jmp_byte_count = 0;
+ const char *internal_name = import_symbol_name (exp);
/* Include the jump stub section only if it is needed. A jump
stub is needed if the symbol being imported <sym> is a function
@@ -3052,10 +3084,16 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
bfd *outarch;
bfd *ibfd;
bfd *head = 0;
+ struct bfd_pe_short_export *short_exports = NULL;
+ size_t short_count = 0;
+
+ if (pe_dll_short_implib && pe_dll_compat_implib)
+ fatal (_("%P: --short-implib is incompatible with --compat-implib\n"));
unlink_if_ordinary (impfilename);
- outarch = bfd_openw (impfilename, 0);
+ outarch = bfd_openw (impfilename, pe_dll_short_implib
+ ? pe_details->object_target : NULL);
if (!outarch)
{
@@ -3072,7 +3110,9 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
outarch->has_armap = 1;
/* Work out a reasonable size of things to put onto one line. */
- ar_head = make_head (outarch);
+ ar_head = pe_dll_short_implib ? NULL : make_head (outarch);
+ if (pe_dll_short_implib)
+ short_exports = XNEWVEC (struct bfd_pe_short_export, def->num_exports);
/* Iterate the input BFDs, looking for exclude-modules-for-implib. */
for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
@@ -3180,12 +3220,36 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
}
}
+ if (pe_dll_short_implib)
+ {
+ short_exports[short_count++] = short_export (def->exports + i);
+ def->exports[i].internal_name = internal;
+ continue;
+ }
+
n = make_one (def->exports + i, outarch, !(def->exports + i)->flag_data);
n->archive_next = head;
head = n;
def->exports[i].internal_name = internal;
}
+ if (pe_dll_short_implib)
+ {
+ if (pe_data (info->output_bfd)->timestamp == 0)
+ outarch->flags |= BFD_DETERMINISTIC_OUTPUT;
+ if (!bfd_pe_write_short_import_library
+ (outarch, bfd_get_arch_info (info->output_bfd), dll_filename,
+ short_exports, short_count, pe_use_nul_prefixed_import_tables,
+ head))
+ einfo (_("%X%P: cannot create %s: %E\n"), impfilename);
+ for (i = 0; i < (int) short_count; i++)
+ free ((char *) short_exports[i].symbol);
+ free (short_exports);
+ if (! bfd_close (outarch))
+ einfo ("%X%P: bfd_close %s: %E\n", impfilename);
+ return;
+ }
+
ar_tail = make_tail (outarch);
if (ar_head == NULL || ar_tail == NULL)
diff --git a/ld/pe-dll.h b/ld/pe-dll.h
index a23a7b22ee6..75206422299 100644
--- a/ld/pe-dll.h
+++ b/ld/pe-dll.h
@@ -34,6 +34,7 @@ extern int pe_dll_kill_ats;
extern int pe_dll_stdcall_aliases;
extern int pe_dll_warn_dup_exports;
extern int pe_dll_compat_implib;
+extern int pe_dll_short_implib;
extern int pe_dll_extra_pe_debug;
extern int pe_use_nul_prefixed_import_tables;
extern int pe_use_coff_long_section_names;
diff --git a/ld/pep-dll.c b/ld/pep-dll.c
index 7b1e70fd24d..190fa3eaeab 100644
--- a/ld/pep-dll.c
+++ b/ld/pep-dll.c
@@ -26,6 +26,7 @@
#define pe_def_file pep_def_file
#define pe_details pep_details
#define pe_dll_compat_implib pep_dll_compat_implib
+#define pe_dll_short_implib pep_dll_short_implib
#define pe_dll_extra_pe_debug pep_dll_extra_pe_debug
#define pe_dll_export_everything pep_dll_export_everything
#define pe_dll_exclude_all_symbols pep_dll_exclude_all_symbols
diff --git a/ld/pep-dll.h b/ld/pep-dll.h
index 8529e99f30a..717b10aba11 100644
--- a/ld/pep-dll.h
+++ b/ld/pep-dll.h
@@ -35,6 +35,7 @@ extern int pep_dll_kill_ats;
extern int pep_dll_stdcall_aliases;
extern int pep_dll_warn_dup_exports;
extern int pep_dll_compat_implib;
+extern int pep_dll_short_implib;
extern int pep_dll_extra_pe_debug;
extern int pep_use_nul_prefixed_import_tables;
extern int pep_use_coff_long_section_names;
diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp
index 22b000956ec..50711526ac0 100644
--- a/ld/testsuite/ld-pe/pe.exp
+++ b/ld/testsuite/ld-pe/pe.exp
@@ -202,7 +202,48 @@ if [check_shared_lib_support] {
run_dump_test "symbols-ordinals-hints-imports-ld"
setup_xfail sh-*-*
run_dump_test "symbols-ordinals-hints-imports-dlltool"
+ if {[istarget i*86-*-*] || [istarget x86_64-*-*]} {
+ # Reuse the import-table checks for both deterministic short libraries.
+ foreach {tool deterministic} {
+ ld --no-insert-timestamp
+ dlltool --deterministic-libraries
+ } {
+ set test "symbols-ordinals-hints-imports-$tool"
+ if {![runtest_file_p $runtests $test]} {
+ continue
+ }
+ set archive "tmpdir/libimports-$tool.a"
+ remote_file host delete $archive
+ run_dump_test $test [list [list $tool "--short-implib $deterministic"] \
+ [list name "$tool short import table"]]
+ if {![is_remote host]} {
+ set contents($tool) ""
+ if {[file exists $archive]} {
+ set lib [open $archive rb]
+ set contents($tool) [read $lib]
+ close $lib
+ }
+ # Count archive member header terminators followed by a short
+ # import signature and version zero.
+ set count [regexp -all {`\n\x00\x00\xff\xff\x00\x00} $contents($tool)]
+ if {$count == 5} {
+ pass "$tool short import members"
+ } else {
+ fail "$tool short import members"
+ }
+ }
+ }
+ if {[info exists contents(ld)] && [info exists contents(dlltool)]} {
+ set test "ld and dlltool produce identical short import libraries"
+ if {$contents(ld) ne "" && $contents(ld) eq $contents(dlltool)} {
+ pass $test
+ } else {
+ fail $test
+ }
+ }
+ }
set ASFLAGS $old_ASFLAGS
+ run_dump_test "short-implib-stdcall"
}
run_dump_test "image-fixup-16"
diff --git a/ld/testsuite/ld-pe/short-implib-stdcall-user.s b/ld/testsuite/ld-pe/short-implib-stdcall-user.s
new file mode 100644
index 00000000000..b32e4756536
--- /dev/null
+++ b/ld/testsuite/ld-pe/short-implib-stdcall-user.s
@@ -0,0 +1,8 @@
+ .text
+ .global _use
+_use:
+ pushl $0
+ call _call@4
+ pushl $0
+ call *__imp__call@4
+ ret
diff --git a/ld/testsuite/ld-pe/short-implib-stdcall.d b/ld/testsuite/ld-pe/short-implib-stdcall.d
new file mode 100644
index 00000000000..a44a775b110
--- /dev/null
+++ b/ld/testsuite/ld-pe/short-implib-stdcall.d
@@ -0,0 +1,14 @@
+#source: short-implib-stdcall.s
+#ld: -shared --enable-stdcall-fixup --short-implib ${srcdir}/${subdir}/short-implib-stdcall.def --out-implib tmpdir/libshort-implib-stdcall.a
+#ld_after_inputfiles: && $AS $ASFLAGS ${srcdir}/${subdir}/short-implib-stdcall-user.s -o tmpdir/short-implib-stdcall-user.o && $LD -shared tmpdir/short-implib-stdcall-user.o tmpdir/libshort-implib-stdcall.a -o tmpdir/dump
+#objdump: -p
+#target: i?86-*-cygwin* i?86-*-pe i?86-*-mingw*
+
+# The DLL exports call, but the caller refers to _call@4 through the
+# stdcall fixup, so the short import library must keep the decorated name.
+
+#...
+.*DLL Name: short-implib-stdcall.dll
+.* Ordinal Hint Member-Name Bound-To
+.* <none> 0000 call
+#pass
diff --git a/ld/testsuite/ld-pe/short-implib-stdcall.def b/ld/testsuite/ld-pe/short-implib-stdcall.def
new file mode 100644
index 00000000000..e38aab4aa69
--- /dev/null
+++ b/ld/testsuite/ld-pe/short-implib-stdcall.def
@@ -0,0 +1,3 @@
+LIBRARY short-implib-stdcall.dll
+EXPORTS
+call
diff --git a/ld/testsuite/ld-pe/short-implib-stdcall.s b/ld/testsuite/ld-pe/short-implib-stdcall.s
new file mode 100644
index 00000000000..daf9c3eaacf
--- /dev/null
+++ b/ld/testsuite/ld-pe/short-implib-stdcall.s
@@ -0,0 +1,4 @@
+ .text
+ .global _call@4
+_call@4:
+ ret $4
--
2.56.1.windows.1
More information about the Binutils
mailing list