[binutils-gdb/binutils-2_46-branch] ld: Fix calls to strchr that discard or use wrong const qualifiers

"H.J. Lu via Binutils-cvs" <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1d74be61a50257fb78ff9f3081a746d82bcc64a5

commit 1d74be61a50257fb78ff9f3081a746d82bcc64a5
Author: Mark Wielaard <[email protected]>
Date:   Sat May 2 18:56:52 2026 +0200

    ld: Fix calls to strchr that discard or use wrong const qualifiers
    
            PR binutils/34125
            * ld/emultempl/pe.em (pe_fixup_stdcalls): Make at a const char *.
            (gldEMULATION_NAME_after_open): Make pnt a const char *.
            (gldEMULATION_NAME_place_orphan): Make dollar a const char *.
            * ld/emultempl/pep.em (pep_fixup_stdcalls): Make at a
            const char *. Introduce at2 as non-const char *.
            (gldEMULATION_NAME_after_open): Make pnt a const char *.
            (gldEMULATION_NAME_place_orphan): Make dollar a const char *.
            * ld/emultempl/spuelf.em (spu_elf_load_ovl_mgr): Make p a
            const char *. Introduce np as char *.
            * ld/ldelf.c (ldelf_search_needed): Call strchr on freeme, so
            slash is non-const, then assign freeme to replacement.
    
    (cherry picked from commit 34ca322d8a2b55e4ee193492a489aaa68195cffb)

Diff:
---
 ld/emultempl/beos.em   |  2 +-
 ld/emultempl/pe.em     |  6 +++---
 ld/emultempl/pep.em    | 13 +++++++------
 ld/emultempl/spuelf.em | 10 +++++-----
 ld/ldelf.c             |  6 +++---
 5 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/ld/emultempl/beos.em b/ld/emultempl/beos.em
index e456112fc54..c027a7536dc 100644
--- a/ld/emultempl/beos.em
+++ b/ld/emultempl/beos.em
@@ -554,7 +554,7 @@ sort_sections (lang_statement_union_type *s)
 		 the linker handle the rest).  */
 	      if (sec->spec.name != NULL)
 		{
-		  char *q = strchr (sec->spec.name, '\$');
+		  const char *q = strchr (sec->spec.name, '\$');
 
 		  if (q != NULL
 		      && (q[1] == '\0'
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 46abac78946..07ef2ca5953 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1149,7 +1149,7 @@ pe_fixup_stdcalls (void)
     if (undef->type == bfd_link_hash_undefined)
       {
 	const char * name = undef->root.string;
-	char * at;
+	const char * at;
 	int lead_at = (*name == '@');
 
 	if (lead_at)
@@ -1750,7 +1750,7 @@ gld${EMULATION_NAME}_after_open (void)
       {
 	if (is->the_bfd->my_archive)
 	  {
-	    char *pnt;
+	    const char *pnt;
 
 	    /* Microsoft import libraries may contain archive members for
 	       one or more DLLs, together with static object files.
@@ -2146,7 +2146,7 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
 				   int constraint)
 {
   const char *orig_secname = secname;
-  char *dollar = NULL;
+  const char *dollar = NULL;
   lang_output_section_statement_type *os;
   lang_statement_list_type add_child;
   lang_output_section_statement_type *match_by_name = NULL;
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 280949b6e51..25ce3963b36 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1089,7 +1089,7 @@ pep_fixup_stdcalls (void)
   for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
     if (undef->type == bfd_link_hash_undefined)
       {
-	char* at = strchr (undef->root.string, '@');
+	const char* at = strchr (undef->root.string, '@');
 	int lead_at = (*undef->root.string == '@');
 	if (lead_at)
 	  at = strchr (undef->root.string + 1, '@');
@@ -1098,12 +1098,13 @@ pep_fixup_stdcalls (void)
 	    /* The symbol is a stdcall symbol, so let's look for a
 	       cdecl symbol with the same name and resolve to that.  */
 	    char *cname = xstrdup (undef->root.string);
+	    char *at2;
 
 	    if (lead_at)
 	      *cname = '_';
-	    at = strchr (cname, '@');
-	    if (at)
-	      *at = 0;
+	    at2 = strchr (cname, '@');
+	    if (at2)
+	      *at2 = 0;
 	    sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
 
 	    if (sym && sym->type == bfd_link_hash_defined)
@@ -1737,7 +1738,7 @@ gld${EMULATION_NAME}_after_open (void)
       {
 	if (is->the_bfd->my_archive)
 	  {
-	    char *pnt;
+	    const char *pnt;
 
 	    /* Microsoft import libraries may contain archive members for
 	       one or more DLLs, together with static object files.
@@ -1986,7 +1987,7 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
 				    int constraint)
 {
   const char *orig_secname = secname;
-  char *dollar = NULL;
+  const char *dollar = NULL;
   lang_output_section_statement_type *os;
   lang_statement_list_type add_child;
   lang_output_section_statement_type *match_by_name = NULL;
diff --git a/ld/emultempl/spuelf.em b/ld/emultempl/spuelf.em
index ae9d6e31f5c..98e5d7636bd 100644
--- a/ld/emultempl/spuelf.em
+++ b/ld/emultempl/spuelf.em
@@ -235,14 +235,14 @@ spu_elf_load_ovl_mgr (void)
 		    if (!lang_output_section_find (oname))
 		      {
 			lang_output_section_statement_type *os = NULL;
-			char *p = strchr (oname + 1, '.');
+			const char *p = strchr (oname + 1, '.');
 			if (p != NULL)
 			  {
 			    size_t len = p - oname;
-			    p = memcpy (xmalloc (len + 1), oname, len);
-			    p[len] = '\0';
-			    os = lang_output_section_find (p);
-			    free (p);
+			    char *np = memcpy (xmalloc (len + 1), oname, len);
+			    np[len] = '\0';
+			    os = lang_output_section_find (np);
+			    free (np);
 			  }
 			if (os != NULL)
 			  oname = os->name;
diff --git a/ld/ldelf.c b/ld/ldelf.c
index a623d37c4d0..2f6c496f8cb 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -534,9 +534,9 @@ ldelf_search_needed (const char *path, struct dt_needed *n, int force,
 				  replacement, rep_len + 1);
 			}
 
-		      replacement = freeme;
-		      if ((slash = strrchr (replacement, '/')) != NULL)
+		      if ((slash = strrchr (freeme, '/')) != NULL)
 			* slash = 0;
+		      replacement = freeme;
 		    }
 		}
 	      break;
@@ -794,7 +794,7 @@ ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf *info,
 
   if (pattern[0] != '/')
     {
-      char *p = strrchr (filename, '/');
+      const char *p = strrchr (filename, '/');
       size_t patlen = strlen (pattern) + 1;
 
       newp = xmalloc (p - filename + 1 + patlen);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.