From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id A390338515CE for ; Thu, 25 Aug 2022 07:29:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A390338515CE Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=martin.st Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=martin.st Received: by mail-lf1-x129.google.com with SMTP id l8so9567827lfc.12 for ; Thu, 25 Aug 2022 00:29:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc; bh=Mpe8xamS7yJ21vdDdIG4zExFJ/WjeVyjCtM5NcZGVfI=; b=T8V55Vo5nPDDI80RYy5E0q+cSVngL5XHgD090WbcFZFXkCYNNIqxyf7gFLHs0lxplK HhEDir70xmWkmfa5uIzLOtrvG3Mt4id6r3DGn9Ow+xeA4hDTnhPaEvHBjczNe8Yda66p PRNAWTQA4gMrtCSbNxWuR4art8FTkpHVLxnSkvZFaO9nFB/iOccq8gLxultLM6Wvykve Kdru0y4s9fd9x40wG3aqaNyA7WERI8vqg60pZqg9/ashsSmZOV7kb2PegFvM+vv14GT/ HEq0CKNwfu55addgu8SIF+evK75FUrqg3/N6vAxf3CBztIrc11xbdEDFn3QbKI7V0woC b/ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc; bh=Mpe8xamS7yJ21vdDdIG4zExFJ/WjeVyjCtM5NcZGVfI=; b=OF3s4keeLabUxJ5Tto1uG/7rAo+gG/vQLyWe4AqJsauIj9VDb8496BljwQAL0aI/bQ YtjpYj3ccTbBh7LxXzbGwcpBB0ES9GKuLT+SSDS/n4gKOqo1MmDOlkIvD3Abp+Fkzo8A BY4fqFqCCCzbxJwMtbqa6+pumSIP7kWkYjUPlG+RhIx1EnD3NN/MvlXkEr0c6XpRMRwR wLR/J99PnQ+j1wp1Ac9XGnWiPBvfTXGzKISf/G6Jbm9iPfs7PkGoQKtmoOYLJ4Db8Ey5 3YcmUmzlhRMZVgQ/owbkAAEjYJyJ9eiV1Agck0niUo0uKktaExpwJY5I26nv21BC0l7g oIzQ== X-Gm-Message-State: ACgBeo0srMClsz2dsoCXpotfdeN6qin2Ctl2F9fZaZ/zanqfVNA3UdNv VbG0bFj9J05Nffk64n8Ri2QDeLXBFGiKNdHd X-Google-Smtp-Source: AA6agR4qc925YgImonPlWTZhGRIpDBDJeHVpGkM2mN0B+Klwi0n8yi08L6u4uLsJ/hyP4FNZzPXLgw== X-Received: by 2002:a05:6512:3984:b0:492:da1b:9683 with SMTP id j4-20020a056512398400b00492da1b9683mr772273lfu.58.1661412555924; Thu, 25 Aug 2022 00:29:15 -0700 (PDT) Received: from localhost.localdomain (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id r28-20020a2e8e3c000000b0025e5cd1620fsm378165ljk.57.2022.08.25.00.29.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 00:29:15 -0700 (PDT) From: =?UTF-8?q?Martin=20Storsj=C3=B6?= To: binutils@sourceware.org Subject: [PATCH] ld: Fix linking against Microsoft import libraries with multiple DLLs Date: Thu, 25 Aug 2022 10:29:14 +0300 Message-Id: <20220825072914.1629214-1-martin@martin.st> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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.25.1