Fix compile time warnings building the binutils with clang.
authorSimon Marchi <simon.marchi@ericsson.com>
Thu, 1 Feb 2018 13:21:41 +0000 (13:21 +0000)
committerNick Clifton <nickc@redhat.com>
Thu, 1 Feb 2018 13:21:41 +0000 (13:21 +0000)
bfdI would like to fix instances of the following warning, when building
with clang with no special CFLAGS other than -g3 -O0.

/home/emaisin/src/binutils-gdb/bfd/elflink.c:5425:45: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]
    return (struct elf_link_hash_entry *) 0 - 1;
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^

Replacing those with "(struct elf_link_hash_entry *) -1" gets rid of the
warning.  I wanted to check that it didn't change the resulting code, so
I tried to build this:

  $ cat test.c
  int *before()
  {
          return (int *) 0 - 1;
  }

  int *after()
  {
          return (int *) - 1;
  }

  $ gcc -c test.c -g
  $ objdump -d test.o

  test.o:     file format elf64-x86-64

  Disassembly of section .text:

  0000000000000000 <before>:
     0:   55                      push   %rbp
     1:   48 89 e5                mov    %rsp,%rbp
     4:   48 c7 c0 fc ff ff ff    mov    $0xfffffffffffffffc,%rax
     b:   5d                      pop    %rbp
     c:   c3                      retq

  000000000000000d <after>:
     d:   55                      push   %rbp
     e:   48 89 e5                mov    %rsp,%rbp
    11:   48 c7 c0 ff ff ff ff    mov    $0xffffffffffffffff,%rax
    18:   5d                      pop    %rbp
    19:   c3                      retq

This shows that the previous code doesn't actually return -1 as the
function documentation says, but the new one does, so it's kind of a
bugfix.

bfd * elf64-ppc.c (ppc64_elf_archive_symbol_lookup): Avoid pointer
arithmetic on NULL pointer.
* elflink.c (_bfd_elf_archive_symbol_lookup,
elf_link_add_archive_symbols): Likewise.

ld * ldexp.c (fold_name, exp_fold_tree_1): Avoid pointer arithmetic
on NULL pointer.

bfd/ChangeLog
bfd/elf64-ppc.c
bfd/elflink.c
ld/ChangeLog
ld/ldexp.c

index e71a53a32081487afe84dabad404472c38d34892..664a974b8ddc9d7a40b718a7d68d78d43c31b4e4 100644 (file)
@@ -1,3 +1,10 @@
+2018-02-01  Simon Marchi  <simon.marchi@ericsson.com>
+
+       * elf64-ppc.c (ppc64_elf_archive_symbol_lookup): Avoid pointer
+       arithmetic on NULL pointer.
+       * elflink.c (_bfd_elf_archive_symbol_lookup,
+       elf_link_add_archive_symbols): Likewise.
+
 2018-01-31  Michael Matz  <matz@suse.de>
 
        * elflink.c (bfd_elf_define_start_stop): Fix check of
index 5cbb0355329034ac36d891be5dc3fc4183172363..9e4a4b5574d1b456fcdaa865c273fd8dd644236a 100644 (file)
@@ -5051,7 +5051,7 @@ ppc64_elf_archive_symbol_lookup (bfd *abfd,
   len = strlen (name);
   dot_name = bfd_alloc (abfd, len + 2);
   if (dot_name == NULL)
-    return (struct elf_link_hash_entry *) 0 - 1;
+    return (struct elf_link_hash_entry *) -1;
   dot_name[0] = '.';
   memcpy (dot_name + 1, name, len + 1);
   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
index 3fe4555230ad4160aba37e988255163e3cc13a2a..d1eb82020cf4c5cef4cd609d5a1e0909fa8856d2 100644 (file)
@@ -5422,7 +5422,7 @@ _bfd_elf_archive_symbol_lookup (bfd *abfd,
   len = strlen (name);
   copy = (char *) bfd_alloc (abfd, len);
   if (copy == NULL)
-    return (struct elf_link_hash_entry *) 0 - 1;
+    return (struct elf_link_hash_entry *) -1;
 
   first = p - name + 1;
   memcpy (copy, name, first);
@@ -5520,7 +5520,7 @@ elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
            }
 
          h = archive_symbol_lookup (abfd, info, symdef->name);
-         if (h == (struct elf_link_hash_entry *) 0 - 1)
+         if (h == (struct elf_link_hash_entry *) -1)
            goto error_return;
 
          if (h == NULL)
index 2db90b5157bb2e58eb5cd05f609d933817e83c5a..c1a37c14a45e7c93c16689d9d18ad6e855ceab5a 100644 (file)
@@ -1,3 +1,8 @@
+2018-02-01  Simon Marchi  <simon.marchi@ericsson.com>
+
+       * ldexp.c (fold_name, exp_fold_tree_1): Avoid pointer arithmetic
+       on NULL pointer.
+
 2018-01-31  H.J. Lu  <hongjiu.lu@intel.com>
 
        * testsuite/ld-elf/pr21964-5.c (my_var): Mark as used.
index a0972058fe1a8eafb530b1a4f1267acdac7f97cc..bf0df44a438b19433ff44ccdbc352f8ddf39a00f 100644 (file)
@@ -782,7 +782,7 @@ fold_name (etree_type *tree)
          if (expld.assign_src == NULL)
            expld.assign_src = h;
          else
-           expld.assign_src = (struct bfd_link_hash_entry *) - 1;
+           expld.assign_src = (struct bfd_link_hash_entry *) - 1;
        }
       break;
 
@@ -1207,7 +1207,7 @@ exp_fold_tree_1 (etree_type *tree)
                     false branches.)  */
                  if (expld.assign_src != NULL
                      && (expld.assign_src
-                         != (struct bfd_link_hash_entry *) 0 - 1))
+                         != (struct bfd_link_hash_entry *) -1))
                    bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
                                                    expld.assign_src);
                }
This page took 0.038921 seconds and 4 git commands to generate.