From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7894) id 038B038276E5; Tue, 30 Aug 2022 20:49:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 038B038276E5 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Martin Storsjo To: bfd-cvs@sourceware.org Subject: [binutils-gdb] ld: pe: Fix linking against Microsoft import libraries with multiple DLLs X-Act-Checkin: binutils-gdb X-Git-Author: =?utf-8?q?Martin_Storsj=C3=B6?= X-Git-Refname: refs/heads/master X-Git-Oldrev: c95dd24e796615b9f41e8821137fc088049eef92 X-Git-Newrev: 46bbc1c071ca5ceb1d6cfcd38e65eef28f27283c Message-Id: <20220830204927.038B038276E5@sourceware.org> Date: Tue, 30 Aug 2022 20:49:27 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Aug 2022 20:49:27 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D46bbc1c071ca= 5ceb1d6cfcd38e65eef28f27283c commit 46bbc1c071ca5ceb1d6cfcd38e65eef28f27283c Author: Martin Storsj=C3=B6 Date: Tue Aug 23 15:48:39 2022 +0300 ld: pe: Fix linking against Microsoft import libraries with multiple DL= Ls =20 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). =20 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.) =20 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). =20 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. =20 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. =20 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. Diff: --- 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) } =20 { - int is_ms_arch =3D 0; - bfd *cur_arch =3D 0; - lang_input_statement_type *is2; - lang_input_statement_type *is3; =20 /* 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 =3D is->the_bfd->my_archive; =20 - if (cur_arch !=3D arch) - { - cur_arch =3D arch; - is_ms_arch =3D 1; - - for (is3 =3D is; - is3 && is3->the_bfd->my_archive =3D=3D arch; - is3 =3D (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 =3D strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt !=3D NULL && fileext_cmp (pnt + 1, "dll") =3D=3D 0) - break; - } - - if (is3 =3D=3D NULL) - is_ms_arch =3D 0; - else - { - /* OK, found one. Now look to see if the remaining - (dynamic import) members use the same name. */ - for (is2 =3D is; - is2 && is2->the_bfd->my_archive =3D=3D arch; - is2 =3D (lang_input_statement_type *) is2->next) - { - /* Skip static members, ie anything with a .obj - extension. */ - pnt =3D strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt !=3D NULL && fileext_cmp (pnt + 1, "obj") =3D=3D 0) - continue; - - if (filename_cmp (bfd_get_filename (is3->the_bfd), - bfd_get_filename (is2->the_bfd))) - { - is_ms_arch =3D 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 =3D strrchr (bfd_get_filename (is->the_bfd), '.'); =20 - if (is_ms_arch && pnt !=3D NULL && (fileext_cmp (pnt + 1, "dll") =3D= =3D 0)) + if (pnt !=3D NULL && (fileext_cmp (pnt + 1, "dll") =3D=3D 0)) { - int idata2 =3D 0, reloc_count=3D0; + int idata2 =3D 0, reloc_count =3D 0, idata =3D 0; asection *sec; char *new_name, seq; =20 @@ -1729,9 +1683,17 @@ gld${EMULATION_NAME}_after_open (void) { if (strcmp (sec->name, ".idata\$2") =3D=3D 0) idata2 =3D 1; + if (strncmp (sec->name, ".idata\$", 6) =3D=3D 0) + idata =3D 1; reloc_count +=3D sec->reloc_count; } =20 + /* 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 =3D '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) } =20 { - int is_ms_arch =3D 0; - bfd *cur_arch =3D 0; - lang_input_statement_type *is2; - lang_input_statement_type *is3; =20 /* 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 =3D is->the_bfd->my_archive; =20 - if (cur_arch !=3D arch) - { - cur_arch =3D arch; - is_ms_arch =3D 1; - - for (is3 =3D is; - is3 && is3->the_bfd->my_archive =3D=3D arch; - is3 =3D (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 =3D strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt !=3D NULL && fileext_cmp (pnt + 1, "dll") =3D=3D 0) - break; - } - - if (is3 =3D=3D NULL) - is_ms_arch =3D 0; - else - { - /* OK, found one. Now look to see if the remaining - (dynamic import) members use the same name. */ - for (is2 =3D is; - is2 && is2->the_bfd->my_archive =3D=3D arch; - is2 =3D (lang_input_statement_type *) is2->next) - { - /* Skip static members, ie anything with a .obj - extension. */ - pnt =3D strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt !=3D NULL && fileext_cmp (pnt + 1, "obj") =3D=3D 0) - continue; - - if (filename_cmp (bfd_get_filename (is3->the_bfd), - bfd_get_filename (is2->the_bfd))) - { - is_ms_arch =3D 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 =3D strrchr (bfd_get_filename (is->the_bfd), '.'); =20 - if (is_ms_arch && pnt !=3D NULL && (fileext_cmp (pnt + 1, "dll") =3D= =3D 0)) + if (pnt !=3D NULL && (fileext_cmp (pnt + 1, "dll") =3D=3D 0)) { - int idata2 =3D 0, reloc_count=3D0; + int idata2 =3D 0, reloc_count =3D 0, idata =3D 0; asection *sec; char *new_name, seq; =20 @@ -1693,9 +1647,17 @@ gld${EMULATION_NAME}_after_open (void) { if (strcmp (sec->name, ".idata\$2") =3D=3D 0) idata2 =3D 1; + if (strncmp (sec->name, ".idata\$", 6) =3D=3D 0) + idata =3D 1; reloc_count +=3D sec->reloc_count; } =20 + /* 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 =3D 'a'; else if (reloc_count > 0) /* thunks */