From 46bbc1c071ca5ceb1d6cfcd38e65eef28f27283c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Storsj=C3=B6?= Date: Tue, 23 Aug 2022 15:48:39 +0300 Subject: [PATCH] ld: pe: Fix linking against Microsoft import libraries with multiple DLLs Initially, since c6c37250e98f113755e0d787f7070e2ac80ce77e (in 1999), in order to fix linking against Microsoft import libraries, ld did internally rename members of such libraries. At that point, the criteria for being considered a Microsoft import library was that every archive member had the same name (no regard for exactly what that name was). This was later amended in 44dbf3639f127af46d569ad96b6242dfbc4c0a89 (in 2003) to allow for Microsoft import libraries with intermixed static object files. At this point, the criteria were extended, so that all members following the first member named *.dll either had the exact same member name, or be named *.obj. (Curiously, this would allow members with any name if it precedes the first one named *.dll.) In practice, Microsoft style import libraries can contain members for linking against more than one DLL (built by merging multiple regular import libraries into one). Instead of trying to do validation of the whole archive before considering it a Microsoft style import library, relax the criteria for doing the member renaming: If an archive member is named *.dll and it contains .idata sections, assume that that member is a Microsoft import file, and apply the renaming scheme. This works for imports for any number of DLLs in the same library, intermixed with other static object files (regardless of their names), and vastly simplifies the code. LLVM generates Microsoft style import libraries, and Rust builds seem to bundle up multiple import libraries together with some Rust specific static objects. This fixes linking directly against them with ld.bfd. --- ld/emultempl/pe.em | 70 +++++++++++---------------------------------- ld/emultempl/pep.em | 70 +++++++++++---------------------------------- 2 files changed, 32 insertions(+), 108 deletions(-) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 665c02cad6b..c01829670d6 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1649,10 +1649,6 @@ gld${EMULATION_NAME}_after_open (void) } { - int is_ms_arch = 0; - bfd *cur_arch = 0; - lang_input_statement_type *is2; - lang_input_statement_type *is3; /* Careful - this is a shell script. Watch those dollar signs! */ /* Microsoft import libraries have every member named the same, @@ -1668,60 +1664,18 @@ gld${EMULATION_NAME}_after_open (void) if (is->the_bfd->my_archive) { char *pnt; - bfd *arch = is->the_bfd->my_archive; - if (cur_arch != arch) - { - cur_arch = arch; - is_ms_arch = 1; - - for (is3 = is; - is3 && is3->the_bfd->my_archive == arch; - is3 = (lang_input_statement_type *) is3->next) - { - /* A MS dynamic import library can also contain static - members, so look for the first element with a .dll - extension, and use that for the remainder of the - comparisons. */ - pnt = strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt != NULL && fileext_cmp (pnt + 1, "dll") == 0) - break; - } - - if (is3 == NULL) - is_ms_arch = 0; - else - { - /* OK, found one. Now look to see if the remaining - (dynamic import) members use the same name. */ - for (is2 = is; - is2 && is2->the_bfd->my_archive == arch; - is2 = (lang_input_statement_type *) is2->next) - { - /* Skip static members, ie anything with a .obj - extension. */ - pnt = strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt != NULL && fileext_cmp (pnt + 1, "obj") == 0) - continue; - - if (filename_cmp (bfd_get_filename (is3->the_bfd), - bfd_get_filename (is2->the_bfd))) - { - is_ms_arch = 0; - break; - } - } - } - } - - /* This fragment might have come from an .obj file in a Microsoft - import, and not an actual import record. If this is the case, - then leave the filename alone. */ + /* Microsoft import libraries may contain archive members for + one or more DLLs, together with static object files. + Inspect all members that are named *.dll - check whether + they contain .idata sections. Do the renaming of all + archive members that seem to be Microsoft style import + objects. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (is_ms_arch && pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) + if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) { - int idata2 = 0, reloc_count=0; + int idata2 = 0, reloc_count = 0, idata = 0; asection *sec; char *new_name, seq; @@ -1729,9 +1683,17 @@ gld${EMULATION_NAME}_after_open (void) { if (strcmp (sec->name, ".idata\$2") == 0) idata2 = 1; + if (strncmp (sec->name, ".idata\$", 6) == 0) + idata = 1; reloc_count += sec->reloc_count; } + /* An archive member named .dll, but not having any .idata + sections - apparently not a Microsoft import object + after all: Skip renaming it. */ + if (!idata) + continue; + if (idata2) /* .idata2 is the TOC */ seq = 'a'; else if (reloc_count > 0) /* thunks */ diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index 05214b1ba3c..d94f18b05c5 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1613,10 +1613,6 @@ gld${EMULATION_NAME}_after_open (void) } { - int is_ms_arch = 0; - bfd *cur_arch = 0; - lang_input_statement_type *is2; - lang_input_statement_type *is3; /* Careful - this is a shell script. Watch those dollar signs! */ /* Microsoft import libraries have every member named the same, @@ -1632,60 +1628,18 @@ gld${EMULATION_NAME}_after_open (void) if (is->the_bfd->my_archive) { char *pnt; - bfd *arch = is->the_bfd->my_archive; - if (cur_arch != arch) - { - cur_arch = arch; - is_ms_arch = 1; - - for (is3 = is; - is3 && is3->the_bfd->my_archive == arch; - is3 = (lang_input_statement_type *) is3->next) - { - /* A MS dynamic import library can also contain static - members, so look for the first element with a .dll - extension, and use that for the remainder of the - comparisons. */ - pnt = strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt != NULL && fileext_cmp (pnt + 1, "dll") == 0) - break; - } - - if (is3 == NULL) - is_ms_arch = 0; - else - { - /* OK, found one. Now look to see if the remaining - (dynamic import) members use the same name. */ - for (is2 = is; - is2 && is2->the_bfd->my_archive == arch; - is2 = (lang_input_statement_type *) is2->next) - { - /* Skip static members, ie anything with a .obj - extension. */ - pnt = strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt != NULL && fileext_cmp (pnt + 1, "obj") == 0) - continue; - - if (filename_cmp (bfd_get_filename (is3->the_bfd), - bfd_get_filename (is2->the_bfd))) - { - is_ms_arch = 0; - break; - } - } - } - } - - /* This fragment might have come from an .obj file in a Microsoft - import, and not an actual import record. If this is the case, - then leave the filename alone. */ + /* Microsoft import libraries may contain archive members for + one or more DLLs, together with static object files. + Inspect all members that are named *.dll - check whether + they contain .idata sections. Do the renaming of all + archive members that seem to be Microsoft style import + objects. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (is_ms_arch && pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) + if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) { - int idata2 = 0, reloc_count=0; + int idata2 = 0, reloc_count = 0, idata = 0; asection *sec; char *new_name, seq; @@ -1693,9 +1647,17 @@ gld${EMULATION_NAME}_after_open (void) { if (strcmp (sec->name, ".idata\$2") == 0) idata2 = 1; + if (strncmp (sec->name, ".idata\$", 6) == 0) + idata = 1; reloc_count += sec->reloc_count; } + /* An archive member named .dll, but not having any .idata + sections - apparently not a Microsoft import object + after all: Skip renaming it. */ + if (!idata) + continue; + if (idata2) /* .idata2 is the TOC */ seq = 'a'; else if (reloc_count > 0) /* thunks */ -- 2.30.2