From patchwork Thu Aug 3 11:45:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 130507 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1088484vqx; Thu, 3 Aug 2023 04:46:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlFCLNHobiZqfX37uVzgW42/L4FxqKpPlkLtbZyNetXMtXDsyi9Z+QvfQsQ+tT4YgvG+LEyH X-Received: by 2002:a17:906:77ce:b0:994:1844:549c with SMTP id m14-20020a17090677ce00b009941844549cmr7915784ejn.0.1691063190851; Thu, 03 Aug 2023 04:46:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691063190; cv=none; d=google.com; s=arc-20160816; b=LolFQfr7YJsMuPlWj9CByIXwlZuoup0V4vuhldsUfk3fcn+sZsbhKDmAEYA/zvTQU3 JQlkByNqDSvJXpB7nwuEUD4YCBQErXaiEkJwBTP+Pelv4YwQJ0Fv7omH3tbGNngsQRqe lk/2ifjQ/yHz4rYrvoi2Z6UdXeV/XnRx0/k42wxGgm/nUHxpI9aJFDJ+0xBE36RKa+XN 4Fbe5a52aHu284XVZP55iBPjg8DGDw5+L4JZ9ZJTtcHiFh+PLhCMqdKQNfI5VnpRmQfA EiIaVwdAC7OcBewICNzTBCnEKPGEoYRWFbpGFhSxpcm0yTkWSolTE4RoGmBljbYEfyRg Oyxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=a0I4+w73Mgb++3C/TnBMiThFUHEhrMnSy+1WZuCo1bo=; fh=HNIqzTA9Dp00ovCAyCdXfuEvWDT00OlFxFRPLybXRis=; b=oZv7umttsi2u2JuY81iJs8PoSiR57QkH53v8s7g7ix+E9BvQNDkrtWGPqsTdzRcu/7 RhQeXPRKcA7JDYqFgw7sdtIxtWUIki6xeMoFaZcsfQ3VTY4bkqF+omXcL40axlo813Dn A8udMVKKunyZWQeOk9W1RhyieOy81RUaXBU4g+Z5q15hpIdzv+/514+Z54wlpNmA1vYV eFFsIvGnaivNBEJtIc5HhG/c4NHvgabd8FK4YQEcpzzy5XRlh/n4CTz3uogHTepJwyGN ZC/c9KfpJtvC0kIkpvSfOBJ3va/lEoA51JEObiMvDxKfCGHxtT1fidrLtBiOiLubPam0 4+LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=JRfMQKu+; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id va16-20020a17090711d000b009871456e3cdsi2404423ejb.439.2023.08.03.04.46.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 04:46:30 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=JRfMQKu+; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DA8F83857014 for ; Thu, 3 Aug 2023 11:45:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DA8F83857014 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691063155; bh=a0I4+w73Mgb++3C/TnBMiThFUHEhrMnSy+1WZuCo1bo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JRfMQKu+B4KwxUCOwVVSdbJsMzT79OhCwk9DUZBG3Bj158eIgqRyWjttYc3K8glNU ufHZBdlo+vFA6+nLZ081I+crG7aITk+xShoy7OrulHNS2+GQfeadgf7vUjMpAV7wvM aNnTKRVjD7V3Qa9XvFpd9BMDromMcg/eJeticLSk= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id DB5793857806 for ; Thu, 3 Aug 2023 11:45:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB5793857806 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1bb8a89b975so5933725ad.1 for ; Thu, 03 Aug 2023 04:45:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691063140; x=1691667940; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=a0I4+w73Mgb++3C/TnBMiThFUHEhrMnSy+1WZuCo1bo=; b=chMRg8H5q94hp/TYS+p/ySz+lkoMm3BnY7SeCfVGQCJBL8UTJaOs6KIgd1pHgIvw3e EFOuEw2nSTOqDjIpDdl5MIYKadJ5CJf8D026G1hP0zk5xXS9/Bz4uGropPzrpI8tdq43 wL3J2zuMD5FNRs81dowb0+k450GX5545GbaU0ZLbNcjoonwLS5vrGPVVzwOrYoAATp8W LkTBvuHH5fj7hBdkWrgxjxL2b8rgVXCPy0P2tFX/V16uNP4t6u6CFhzKMpznXDNGXojV VnCyw1b6VT8VFK570mE9vbPQLzuS774KwTddqR0aUmnpKsXhvwtShUHxZt8XGVOW8d17 lB6A== X-Gm-Message-State: ABy/qLYqbRbO/RfmUk8S1GouCtCjOZJjCW5yPcas5DiB0vRW3WSmM/Dk p67ftKrOM6R+DlXrXjLBBPNV11eGE94= X-Received: by 2002:a17:903:41cd:b0:1bb:db5f:b21 with SMTP id u13-20020a17090341cd00b001bbdb5f0b21mr19779313ple.1.1691063140190; Thu, 03 Aug 2023 04:45:40 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:a029:aaba:dbee:92cb]) by smtp.gmail.com with ESMTPSA id q16-20020a170902dad000b001b89c313185sm14219281plx.205.2023.08.03.04.45.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 04:45:39 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 6FF3211427AD; Thu, 3 Aug 2023 21:15:37 +0930 (ACST) Date: Thu, 3 Aug 2023 21:15:37 +0930 To: binutils@sourceware.org Subject: ld: sprintf sanitizer null destination pointer Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773208276122337465 X-GMAIL-MSGID: 1773208276122337465 * configure.ac (stpcpy): AC_CHECK_DECLS. * sysdep.h (stpcpy): Add fallback declaraion. * config.in: Regenerate. * configure: Regenerate. * emultempl/pe.em (open_dynamic_archive): Use stpcpy rather than sprintf plus strlen. * emultempl/pep.em (open_dynamic_archive): Likewise. * emultempl/xtensaelf.em (elf_xtensa_before_allocation): Use auto rather than malloc'd buffer. Use sprintf count. * ldelf.c (ldelf_search_needed): Use memcpy in place of sprintf. * pe-dll.c (pe_process_import_defs): Use string already formed for alias match rather than recreating. diff --git a/ld/config.in b/ld/config.in index ad0dc6a106c..a453c7f7241 100644 --- a/ld/config.in +++ b/ld/config.in @@ -78,6 +78,10 @@ /* Is the prototype for getopt in in the expected format? */ #undef HAVE_DECL_GETOPT +/* Define to 1 if you have the declaration of `stpcpy', and to 0 if you don't. + */ +#undef HAVE_DECL_STPCPY + /* Define to 1 if you have the header file. */ #undef HAVE_DLFCN_H diff --git a/ld/configure b/ld/configure index 33e09fc6987..a4fcb9bf19b 100755 --- a/ld/configure +++ b/ld/configure @@ -16771,6 +16771,16 @@ fi cat >>confdefs.h <<_ACEOF #define HAVE_DECL_ENVIRON $ac_have_decl _ACEOF +ac_fn_c_check_decl "$LINENO" "stpcpy" "ac_cv_have_decl_stpcpy" "$ac_includes_default" +if test "x$ac_cv_have_decl_stpcpy" = xyes; then : + ac_have_decl=1 +else + ac_have_decl=0 +fi + +cat >>confdefs.h <<_ACEOF +#define HAVE_DECL_STPCPY $ac_have_decl +_ACEOF diff --git a/ld/configure.ac b/ld/configure.ac index 217ef7b2733..14628e5dcef 100644 --- a/ld/configure.ac +++ b/ld/configure.ac @@ -371,7 +371,7 @@ AC_CHECK_FUNCS(close glob lseek mkstemp open realpath waitpid) BFD_BINARY_FOPEN -AC_CHECK_DECLS([asprintf, environ]) +AC_CHECK_DECLS([asprintf, environ, stpcpy]) AC_FUNC_MMAP diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index cd2abe460b9..a583b02eeec 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -2464,8 +2464,8 @@ gld${EMULATION_NAME}_open_dynamic_archive search->name and the start of the format string. */ + 2); - sprintf (full_string, "%s/", search->name); - base_string = full_string + strlen (full_string); + base_string = stpcpy (full_string, search->name); + *base_string++ = '/'; for (i = 0; libname_fmt[i].format; i++) { diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index 28d8c8decb3..516d2afcaf0 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -2295,8 +2295,8 @@ gld${EMULATION_NAME}_open_dynamic_archive search->name and the start of the format string. */ + 2); - sprintf (full_string, "%s/", search->name); - base_string = full_string + strlen (full_string); + base_string = stpcpy (full_string, search->name); + *base_string++ = '/'; for (i = 0; libname_fmt[i].format; i++) { diff --git a/ld/emultempl/xtensaelf.em b/ld/emultempl/xtensaelf.em index 4cb9bda8f92..f2d10b51d13 100644 --- a/ld/emultempl/xtensaelf.em +++ b/ld/emultempl/xtensaelf.em @@ -490,15 +490,14 @@ elf_xtensa_before_allocation (void) if (info_sec) { int xtensa_info_size; - char *data; + char data[100]; info_sec->flags &= ~SEC_EXCLUDE; info_sec->flags |= SEC_IN_MEMORY; - data = xmalloc (100); - sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n", - XSHAL_USE_ABSOLUTE_LITERALS, xtensa_abi_choice ()); - xtensa_info_size = strlen (data) + 1; + xtensa_info_size + = 1 + sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n", + XSHAL_USE_ABSOLUTE_LITERALS, xtensa_abi_choice ()); /* Add enough null terminators to pad to a word boundary. */ do @@ -512,7 +511,6 @@ elf_xtensa_before_allocation (void) bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8); memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ); memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size); - free (data); } /* Enable relaxation by default if the "--no-relax" option was not diff --git a/ld/ldelf.c b/ld/ldelf.c index f9a6819366f..23a014d963b 100644 --- a/ld/ldelf.c +++ b/ld/ldelf.c @@ -524,10 +524,13 @@ ldelf_search_needed (const char *path, struct dt_needed *n, int force, else { char * current_dir = getpwd (); - - freeme = xmalloc (strlen (replacement) - + strlen (current_dir) + 2); - sprintf (freeme, "%s/%s", current_dir, replacement); + size_t cdir_len = strlen (current_dir); + size_t rep_len = strlen (replacement); + freeme = xmalloc (cdir_len + rep_len + 2); + memcpy (freeme, current_dir, cdir_len); + freeme[cdir_len] = '/'; + memcpy (freeme + cdir_len + 1, + replacement, rep_len + 1); } replacement = freeme; diff --git a/ld/pe-dll.c b/ld/pe-dll.c index a95b85c60dd..b45c530d6e7 100644 --- a/ld/pe-dll.c +++ b/ld/pe-dll.c @@ -3340,6 +3340,14 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo) false, false, false); if (blhe) is_undef = (blhe->type == bfd_link_hash_undefined); + + if (is_cdecl && (!blhe || !is_undef)) + { + blhe = pe_find_cdecl_alias_match (linfo, name + 6); + include_jmp_stub = true; + if (blhe) + is_undef = (blhe->type == bfd_link_hash_undefined); + } } else { @@ -3347,16 +3355,6 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo) is_undef = (blhe->type == bfd_link_hash_undefined); } - if (is_cdecl - && (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))) - { - sprintf (name, "%s%s",U (""), imp[i].internal_name); - blhe = pe_find_cdecl_alias_match (linfo, name); - include_jmp_stub = true; - if (blhe) - is_undef = (blhe->type == bfd_link_hash_undefined); - } - free (name); if (is_undef) diff --git a/ld/sysdep.h b/ld/sysdep.h index 3601a59a6ac..1573d5ed888 100644 --- a/ld/sysdep.h +++ b/ld/sysdep.h @@ -92,6 +92,10 @@ extern char **environ; #endif +#if !HAVE_DECL_STPCPY +extern char *stpcpy (char *__dest, const char *__src); +#endif + #define POISON_BFD_BOOLEAN 1 #endif /* ! defined (LD_SYSDEP_H) */