From patchwork Tue Nov 19 13:27:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Storsj=C3=B6?= X-Patchwork-Id: 101531 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B3C13858428 for ; Tue, 19 Nov 2024 13:28:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B3C13858428 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=martin-st.20230601.gappssmtp.com header.i=@martin-st.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=D/P/SWeJ X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id CCFB23858D34 for ; Tue, 19 Nov 2024 13:27:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCFB23858D34 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=martin.st Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=martin.st ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CCFB23858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732022850; cv=none; b=dkfZu7xYv99W25fSLovGzScy4FAJIND/51TM/hoA002HIQhMGbMdISLE/niqV45/ombCi1MMGE+Ho2iPraTBqTGxg7wdhebAYLwG16WQntD1ZApO049poxbwNRyX8LAFN9mGUBLHOmEuChpa8KPdrwS9yfDUw5o+uQ7KggF9S7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732022850; c=relaxed/simple; bh=g7V6F+04Sx3J+78Gl+/j6DpT+1ROOalJLKM5WUSkZ8E=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=JWc02ckVbh/fLgpZJ5Ze1BxNVsVrg4QOfG8y3MUK+N9RWjyG7pVH3xmw2jh8egtx7H2dlY/tmv2vgscvV+Sak+ObmkBPwL7mOOemu6J1yUTZlDQiuPFOxgFiUuSuH55sSQ1PSJZJ6DDepojopbcaD4s02sOoTYBXoTVjMHawNis= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCFB23858D34 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-53da3545908so3621798e87.1 for ; Tue, 19 Nov 2024 05:27:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20230601.gappssmtp.com; s=20230601; t=1732022848; x=1732627648; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lUu8dYoXxN+SR0cjYWuWaLrte3qg5lhYCtoMrFXxAjs=; b=D/P/SWeJgE9zYb8Q4MD+kda/2ABfdBd8dS5PhuFy0cDnUfEBESaMu9WjT/kmsXnpqy 2hoGBk9MjloDIuCv/MRuBABVm7FKLE9ta1S10fBsmUJp62H4qS8CfCfLfhq+hX0qJwmj xT1yTVdBPjzqYvpsCIxoyIn4czTuyAFCqWF/hy0ZpJSEoWe2CD2wBs4dyYRKpYefasa3 REEX4C65ztm1nr5UKss0QG0jHSPfHS9r0+T+Pe1XlFzQI7/it2+sDcCyMAKfsm8CZ5tL /l0lkpgrD1jCA1gkFUc7gyNXnxni8SXJqEXqvAR30ubUlIwiQFgRWGDsdz1Jbj5FUeSq K5TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732022848; x=1732627648; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lUu8dYoXxN+SR0cjYWuWaLrte3qg5lhYCtoMrFXxAjs=; b=UtjtsNgUcf027pKhfwjPMUqx9JvkMKderht8KX5rYqCRikG0FsU4Tkhl0AwzfY46Fv gFJyddA0Jw6vIQ9PB5VZI0UwdTzyRM57omQzo05YLe5stBsDcmDIF5xa11b9QVroGsAL z9K36NUv6hvx4hlOLUAoKk/yVWSmLNNxf26Y4rYE072oUPNRKrepOZB9NnTMOhngufmz o8qy2PN9ExqJrkVgI+Nd3RW+iQUyiCpeiNOsURu0gBSFJlStZyXEcIt0wb8wRFgnxxMb 2RlYYPNdg1qbiWmAv7Hqiq50NVQRbUilYzhS7t4JiOntsMEhpZEYw2/HqfWi45tzx9zy pJwA== X-Gm-Message-State: AOJu0YxgcAdiYPy0whT9WqLWqlgng+pTjuUYKlcEuj8TCPwDBl/RNHX2 tPL2flBfsSd4poTjc1MfL/firUUZhUGPvZUqlq9DUE5r9PW9+mD20Lj0DfJOMwaOJOu2CC7a62H jHw== X-Google-Smtp-Source: AGHT+IFGJUzDvPOcuc1zuusbJ/rNshMwbkpylQpCPGT25PFHQyabI5/7QREujBRCfHANS5sCjOefaw== X-Received: by 2002:a05:6512:e88:b0:53d:a321:db74 with SMTP id 2adb3069b0e04-53dab3b62b4mr7729215e87.50.1732022847974; Tue, 19 Nov 2024 05:27:27 -0800 (PST) Received: from localhost (dsl-tkubng21-50de30-198.dhcp.inet.fi. [80.222.48.198]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-53dbd4722efsm301295e87.187.2024.11.19.05.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 05:27:27 -0800 (PST) From: =?utf-8?q?Martin_Storsj=C3=B6?= To: binutils@sourceware.org Cc: jbeulich@suse.com Subject: [PATCH v3] ld/PE: Handle MS style import libraries for files named *.exe too Date: Tue, 19 Nov 2024 15:27:26 +0200 Message-Id: <20241119132727.3218276-1-martin@martin.st> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-9.5 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org When handling MS style import libraries (also called short import libraries, or ILF), we need to detect the kind of library. So far, this has been done by looking at the member file names in the import library - in an MS style import library, all the member files for a specific library have the same member file name - the name of the runtime module to link against. Usually this is a DLL - thus we do a case insensitive comparison and check if the suffix is .dll. However, an .exe can also export symbols which can be linked against in the same way. In particular, if linking against WDK (Windows Driver Kit) import libraries, e.g. wdmsec.lib, the import libraries can provide imports for ntoskrnl.exe. Instead of specifically checking for *.dll (and *.exe, etc), invert the condition and skip archive members named *.o and *.obj. For any remaining archive members, that do contain .idata sections, apply the renaming. (The renaming is also mostly harmless if applied where it isn't needed; if archive members already have unique file names, their relative ordering should remain intact except for very contrieved cases.) --- v3: Updated based on the earlier discussion. Also tested the effect of applying renaming on GNU style import libraries where no renaming is needed - where it is quite harmless. --- ld/emultempl/pe.em | 29 ++++++++++++++++++++++------- ld/emultempl/pep.em | 29 ++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 869ffd3f623..f37807cb95a 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1747,13 +1747,28 @@ gld${EMULATION_NAME}_after_open (void) /* 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. */ + The head and sentinels are regular COFF object files, + while the thunks are special ILF files that get synthesized + by bfd into COFF object files. + + As Microsoft import libraries can be for a module with + almost any file name (*.dll, *.exe, etc), we can't easily + know which archive members to inspect. + + Inspect all members unless ones named *.o or *.obj (which + is the case both for regular static libraries or for GNU + style import libraries). Archive members with file names other + than *.o or *.obj, that do contain .idata sections, are + considered to be Microsoft style import objects, and are + renamed accordingly. + + If this heuristic is wrong and we apply this on archive members + that already have unique names, it shouldn't make any difference + as we only append a suffix on the names. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) + if (pnt != NULL && (fileext_cmp (pnt + 1, "o") != 0 && + fileext_cmp (pnt + 1, "obj") != 0)) { int idata2 = 0, reloc_count = 0, idata = 0; asection *sec; @@ -1768,8 +1783,8 @@ gld${EMULATION_NAME}_after_open (void) reloc_count += sec->reloc_count; } - /* An archive member named .dll, but not having any .idata - sections - apparently not a Microsoft import object + /* An archive member not named .o or .obj, but not having any + .idata sections - apparently not a Microsoft import object after all: Skip renaming it. */ if (!idata) continue; diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index c225d052fb8..35ddae07984 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1730,13 +1730,28 @@ gld${EMULATION_NAME}_after_open (void) /* 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. */ + The head and sentinels are regular COFF object files, + while the thunks are special ILF files that get synthesized + by bfd into COFF object files. + + As Microsoft import libraries can be for a module with + almost any file name (*.dll, *.exe, etc), we can't easily + know which archive members to inspect. + + Inspect all members unless ones named *.o or *.obj (which + is the case both for regular static libraries or for GNU + style import libraries). Archive members with file names other + than *.o or *.obj, that do contain .idata sections, are + considered to be Microsoft style import objects, and are + renamed accordingly. + + If this heuristic is wrong and we apply this on archive members + that already have unique names, it shouldn't make any difference + as we only append a suffix on the names. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) + if (pnt != NULL && (fileext_cmp (pnt + 1, "o") != 0 && + fileext_cmp (pnt + 1, "obj") != 0)) { int idata2 = 0, reloc_count = 0, idata = 0; asection *sec; @@ -1751,8 +1766,8 @@ gld${EMULATION_NAME}_after_open (void) reloc_count += sec->reloc_count; } - /* An archive member named .dll, but not having any .idata - sections - apparently not a Microsoft import object + /* An archive member not named .o or .obj, but not having any + .idata sections - apparently not a Microsoft import object after all: Skip renaming it. */ if (!idata) continue;