[2/2] ld: Fix calls to strchr that discard or use wrong const qualifiers

Message ID 20260502170132.3303884-3-mark@klomp.org
State New
Headers
Series [1/2] gprof: Fix strchr discarded qualifier call |

Commit Message

Mark Wielaard May 2, 2026, 4:56 p.m. UTC
  * 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.
---
 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(-)
  

Comments

Alan Modra May 3, 2026, 2:59 a.m. UTC | #1
On Sat, May 02, 2026 at 06:56:52PM +0200, Mark Wielaard wrote:
> 	* 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.

OK.
  

Patch

diff --git a/ld/emultempl/beos.em b/ld/emultempl/beos.em
index e456112fc54e..c027a7536dc8 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 46abac789469..07ef2ca59531 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 280949b6e516..25ce3963b363 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 ae9d6e31f5ce..98e5d7636bd0 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 15d4b576bc03..eec13501ef7e 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);