From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id 738063851ABF for ; Wed, 24 Aug 2022 12:25:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 738063851ABF 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-x12f.google.com with SMTP id d8so11376347lfq.0 for ; Wed, 24 Aug 2022 05:25:06 -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:references:in-reply-to :message-id:date:subject:to:from:from:to:cc; bh=EN9+XOsfI2Q6jousY3dLkE6R05/LhxmF9m8+omXNA1k=; b=3oNVZYoopp0tnQjnnYI0SM+hjMyfqt59jaz1OiIwoNX/w3dpWPSQ7rCnsbL6ki/3go j4A4APeyk3A+wjG5Zc0bIZ5BDrOuHLpTSW9sn3KdwdmwkLF+ZPweedBDxQN8HYKuL6SC A9mUUBCLKsYxIFLNpfQLkMFj0opyLofya9UuLJtWY5QsGhnJRrPcvpXEuuUikkYhJ8Vn wCr0OLLHWzEhyHHjyb+w864oMCLCZHVcDbOsjsblbVqXS89B6hXVf3kNlKB30CJqYYzi F6L+I5+QkV8vDu/Xy08rHiGjgVjNilF3bl3RPZwk7YRrXD3N8re+xYrMUHumT4CwJVjg qRgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc; bh=EN9+XOsfI2Q6jousY3dLkE6R05/LhxmF9m8+omXNA1k=; b=VDpg3bQslLbhhrM+/wIgQ4JOk5UAYpwTsJ6JsXzm9QqEe4QYICuXaGqc7eDd+N5xjn kMnqQ0V5WcvZbGz68dnCAxoQ06oFrYX8NmWddSi/wtlxb9j7CkWJTWRfVzc+sjbSOfjn xvHwR/qT9MtiBWBlR9ok/vxR6AIdiYhSA9jnGgArRlUpmsXZRqE2kIipAbt/KsbcyXTD j9tNfNbL5+jNJYQ+PWcurhQZB77A9H+jXsz7CvmVJ+iSb1VQJpiDc1ND4Rzdvr2JKsgF rjPDXfpaVawDV/PmjnOGxIAnjHCgjRJkhOyOIZ+qeI0Tc8HYIrZ5bpWM+dCcDxqcSx4C oOKw== X-Gm-Message-State: ACgBeo3IRksNGlRBDU1yX0TEHQ81MLIZ0TZYNG+ffmEe2g1vopnKN1IN J7745SrCjq341VW4gMS88nUTN7VuZ8w0XPpb X-Google-Smtp-Source: AA6agR7Rw2KHdroVD76A6+7CLJAYM8K0MAZvq12rsy8n0yXWlfQNMcKCK3R2cnjtgxaIQVbwduk6yQ== X-Received: by 2002:a05:6512:3f91:b0:492:f17d:33d9 with SMTP id x17-20020a0565123f9100b00492f17d33d9mr3320815lfa.73.1661343904972; Wed, 24 Aug 2022 05:25:04 -0700 (PDT) Received: from localhost.localdomain (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id p3-20020a05651212c300b0048b18d65998sm2987904lfg.38.2022.08.24.05.25.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:25:04 -0700 (PDT) From: =?UTF-8?q?Martin=20Storsj=C3=B6?= To: binutils@sourceware.org Subject: [PATCH v7] ld: Make archive member file extension comparisons case insensitive when cross compiling too Date: Wed, 24 Aug 2022 15:25:03 +0300 Message-Id: <20220824122503.19441-1-martin@martin.st> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: 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: On Windows, filename_cmp is case insensitive, but when cross compiling with libraries that may contain members with uppercase file names, we should keep those comparisons case insensitive when running the build tools on other OSes too. Also make the check for .def consistent with the other ones, fixing out of bounds reads if file names are shorter than 4 characters. --- v7: Skip the '.' entirely in the first parameter to fileext_cmp too, simplifying the function and making the two parameters more symmetrical. --- ld/emultempl/pe.em | 27 ++++++++++++++++++++++----- ld/emultempl/pep.em | 27 ++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index ad969ccec13..2706d73d144 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -534,6 +534,23 @@ gld${EMULATION_NAME}_list_options (FILE *file) fprintf (file, _(" --build-id[=STYLE] Generate build ID\n")); } +/* A case insensitive comparison, regardless of the host platform, used for + comparing file extensions. */ +static int +fileext_cmp (const char *s1, const char *s2) +{ + for (;;) + { + int c1 = TOLOWER (*s1++); + int c2 = *s2++; /* Assumed to be lower case from the caller. */ + + if (c1 != c2) + return (c1 - c2); + + if (c1 == '\0') + return 0; + } +} static void set_pe_name (char *name, long val) @@ -1666,7 +1683,7 @@ gld${EMULATION_NAME}_after_open (void) extension, and use that for the remainder of the comparisons. */ pnt = strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt != NULL && filename_cmp (pnt, ".dll") == 0) + if (pnt != NULL && fileext_cmp (pnt + 1, "dll") == 0) break; } @@ -1683,7 +1700,7 @@ gld${EMULATION_NAME}_after_open (void) /* Skip static members, ie anything with a .obj extension. */ pnt = strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt != NULL && filename_cmp (pnt, ".obj") == 0) + if (pnt != NULL && fileext_cmp (pnt + 1, "obj") == 0) continue; if (filename_cmp (bfd_get_filename (is3->the_bfd), @@ -1701,7 +1718,7 @@ gld${EMULATION_NAME}_after_open (void) then leave the filename alone. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0)) + if (is_ms_arch && pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) { int idata2 = 0, reloc_count=0; asection *sec; @@ -1855,9 +1872,9 @@ static bool gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED) { #ifdef DLL_SUPPORT - const char *ext = entry->filename + strlen (entry->filename) - 4; + const char *ext = strrchr (entry->filename, '.'); - if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0) + if (ext != NULL && fileext_cmp (ext + 1, "def") == 0) { pe_def_file = def_file_parse (entry->filename, pe_def_file); diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index ee36a9a7e56..accefc0ed60 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -181,6 +181,23 @@ static int is_underscoring (void) return pep_leading_underscore; } +/* A case insensitive comparison, regardless of the host platform, used for + comparing file extensions. */ +static int +fileext_cmp (const char *s1, const char *s2) +{ + for (;;) + { + int c1 = TOLOWER (*s1++); + int c2 = *s2++; /* Assumed to be lower case from the caller. */ + + if (c1 != c2) + return (c1 - c2); + + if (c1 == '\0') + return 0; + } +} static void gld${EMULATION_NAME}_before_parse (void) @@ -1630,7 +1647,7 @@ gld${EMULATION_NAME}_after_open (void) extension, and use that for the remainder of the comparisons. */ pnt = strrchr (bfd_get_filename (is3->the_bfd), '.'); - if (pnt != NULL && filename_cmp (pnt, ".dll") == 0) + if (pnt != NULL && fileext_cmp (pnt + 1, "dll") == 0) break; } @@ -1647,7 +1664,7 @@ gld${EMULATION_NAME}_after_open (void) /* Skip static members, ie anything with a .obj extension. */ pnt = strrchr (bfd_get_filename (is2->the_bfd), '.'); - if (pnt != NULL && filename_cmp (pnt, ".obj") == 0) + if (pnt != NULL && fileext_cmp (pnt + 1, "obj") == 0) continue; if (filename_cmp (bfd_get_filename (is3->the_bfd), @@ -1665,7 +1682,7 @@ gld${EMULATION_NAME}_after_open (void) then leave the filename alone. */ pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); - if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0)) + if (is_ms_arch && pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0)) { int idata2 = 0, reloc_count=0; asection *sec; @@ -1725,9 +1742,9 @@ static bool gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED) { #ifdef DLL_SUPPORT - const char *ext = entry->filename + strlen (entry->filename) - 4; + const char *ext = strrchr (entry->filename, '.'); - if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0) + if (ext != NULL && fileext_cmp (ext + 1, "def") == 0) { pep_def_file = def_file_parse (entry->filename, pep_def_file); -- 2.25.1