]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
bfd: fix build with C23
authorGuinevere Larsen <blarsen@redhat.com>
Fri, 5 Dec 2025 18:55:38 +0000 (15:55 -0300)
committerGuinevere Larsen <guinevere@redhat.com>
Mon, 8 Dec 2025 11:46:11 +0000 (08:46 -0300)
Starting in C23, strchr and strrchr will return const char *, if fed a
const char *.  This means that several files in the BFD directory will
fail to build as they are assigning the return of those functions to a
char *.

Fix this by const-ifying several variables. The only place where that
wasn't just that was in targets.c, where a variable was being used in
subsequent strrchr invocations to change the underlying string, so a new
variable had to be introduced.

No user-visible change should happen after this commit.

bfd/bfd.c
bfd/elflink.c
bfd/targets.c

index 11ce75669d3fd18dd18de2a7e65cce2ea987656e..4fa8fdd029faedb6a5c6279c3482872db486f633 100644 (file)
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -1060,7 +1060,7 @@ _bfd_doprnt (bfd_print_callback print, void *stream, const char *format,
       if (*ptr != '%')
        {
          /* While we have regular characters, print them.  */
-         char *end = strchr (ptr, '%');
+         const char *end = strchr (ptr, '%');
          if (end != NULL)
            result = print (stream, "%.*s", (int) (end - ptr), ptr);
          else
index 5fe83cc6693c21436a12671b981cf8d3bc7f6b68..39fac0639a0c8009c0a6b83d6cdedadb5ce7e6ea 100644 (file)
@@ -169,7 +169,7 @@ _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
   struct elf_link_hash_entry *h;
 
   h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
-  
+
   if (h != NULL)
     {
       if (h->root.type == bfd_link_hash_defined
@@ -596,7 +596,7 @@ bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
   if (h->dynindx == -1)
     {
       struct elf_strtab_hash *dynstr;
-      char *p;
+      const char *p;
       const char *name;
       size_t indx;
 
@@ -726,7 +726,7 @@ bfd_elf_record_link_assignment (bfd *output_bfd,
   if (h->versioned == unknown)
     {
       /* Set versioned if symbol version is unknown.  */
-      char *version = strrchr (name, ELF_VER_CHR);
+      const char *version = strrchr (name, ELF_VER_CHR);
       if (version)
        {
          if (version > name && version[-1] != ELF_VER_CHR)
@@ -1161,7 +1161,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
   bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
   bool newweak, oldweak, newfunc, oldfunc;
   const struct elf_backend_data *bed;
-  char *new_version;
+  const char *new_version;
   bool default_sym = *matched;
   struct elf_link_hash_table *htab;
 
@@ -1241,7 +1241,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
            {
              /* OLD_VERSION is the symbol version of the existing
                 symbol. */
-             char *old_version;
+             const char *old_version;
 
              if (h->versioned >= versioned)
                old_version = strrchr (h->root.root.string,
@@ -1965,7 +1965,7 @@ _bfd_elf_add_default_symbol (bfd *abfd,
   bool collect;
   bool dynamic;
   bfd *override;
-  char *p;
+  const char *p;
   size_t len, shortlen;
   asection *tmp_sec;
   bool matched;
@@ -2657,7 +2657,7 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
   struct bfd_link_info *info;
   const struct elf_backend_data *bed;
   struct elf_info_failed eif;
-  char *p;
+  const char *p;
   bool hide;
 
   sinfo = (struct elf_info_failed *) data;
@@ -5665,7 +5665,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
              && !dynamic
              && (abfd->flags & BFD_PLUGIN) == 0)
            {
-             char *p = strchr (name, ELF_VER_CHR);
+             const char *p = strchr (name, ELF_VER_CHR);
              if (p != NULL && p[1] != ELF_VER_CHR)
                {
                  /* Queue non-default versions so that .symver x, x@FOO
@@ -5915,7 +5915,8 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
        {
          struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
-         char *shortname, *p;
+         char *shortname;
+         const char *p;
          size_t amt;
 
          p = strchr (h->root.root.string, ELF_VER_CHR);
@@ -6187,7 +6188,8 @@ _bfd_elf_archive_symbol_lookup (bfd *abfd,
                                const char *name)
 {
   struct bfd_link_hash_entry *h;
-  char *p, *copy;
+  const char *p;
+  char *copy;
   size_t len, first;
 
   h = bfd_link_hash_lookup (info->hash, name, false, false, true);
@@ -6482,7 +6484,7 @@ elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
   name = h->root.root.string;
   if (h->versioned >= versioned)
     {
-      char *p = strchr (name, ELF_VER_CHR);
+      const char *p = strchr (name, ELF_VER_CHR);
       if (p != NULL)
        {
          alc = (char *) bfd_malloc (p - name + 1);
@@ -6556,7 +6558,7 @@ elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
   name = h->root.root.string;
   if (h->versioned >= versioned)
     {
-      char *p = strchr (name, ELF_VER_CHR);
+      const char *p = strchr (name, ELF_VER_CHR);
       if (p != NULL)
        {
          alc = (char *) bfd_malloc (p - name + 1);
@@ -10373,8 +10375,8 @@ elf_link_output_symstrtab (void *finf,
            {
              /* Keep only one '@' for versioned symbols defined in
                 shared objects.  */
-             char *version = strrchr (name, ELF_VER_CHR);
-             char *base_end = strchr (name, ELF_VER_CHR);
+             const char *version = strrchr (name, ELF_VER_CHR);
+             const char *base_end = strchr (name, ELF_VER_CHR);
              if (version != base_end)
                {
                  size_t base_len;
@@ -11117,7 +11119,7 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
              || h->ref_dynamic
              || !h->def_regular))
        {
-         char *p = strrchr (h->root.root.string, ELF_VER_CHR);
+         const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
 
          if (p && p [1] != '\0')
            {
@@ -11676,7 +11678,7 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
                     input_bfd);
                  bfd_set_error (bfd_error_bad_value);
                  return false;
-               }             
+               }
 
              /* Arrange for symbol to be output.  */
              h->indx = -2;
@@ -15152,7 +15154,7 @@ bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
       struct elf_link_hash_entry *h;
 
       h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
-      
+
       if (h != NULL)
        {
          if ((h->root.type == bfd_link_hash_defined
index 4ac70921c35376cdeeb802a46b690e4dd68fca02..2eac73962ef349f0a97b1537e601f573a85503d7 100644 (file)
@@ -1664,7 +1664,7 @@ bfd_get_target_info (const char *target_name, bfd *abfd,
 
       if (arches && tname)
        {
-         char *hyp = strchr (tname, '-');
+         const char *hyp = strchr (tname, '-');
 
          if (hyp != NULL)
            {
@@ -1677,9 +1677,10 @@ bfd_get_target_info (const char *target_name, bfd *abfd,
                  char new_tname[50];
 
                  strcpy (new_tname, hyp);
-                 while ((hyp = strrchr (new_tname, '-')) != NULL)
+                 char *new_hyp;
+                 while ((new_hyp = strrchr (new_tname, '-')) != NULL)
                    {
-                     *hyp = 0;
+                     *new_hyp = 0;
                      if (_bfd_find_arch_match (new_tname, arches,
                                                def_target_arch))
                        break;