PR30198, Assertion and segfault when linking x86_64 elf and coff
authorAlan Modra <amodra@gmail.com>
Mon, 6 Mar 2023 00:13:53 +0000 (10:43 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 6 Mar 2023 02:41:22 +0000 (13:11 +1030)
PR 30198
* coff-x86_64.c (coff_amd64_reloc): Set *error_message when
returning bfd_reloc_dangerous.  Also check that __ImageBase is
defined before accessing h->u.def.

bfd/coff-x86_64.c

index 13061cdacff097a42635e33303ce60c1dbd46802..822504a339b1942101cb9474ef7899c991f8f461 100644 (file)
@@ -139,15 +139,19 @@ coff_amd64_reloc (bfd *abfd,
          break;
        case bfd_target_elf_flavour:
          /* Subtract __ImageBase.  */
+         h = NULL;
          link_info = _bfd_get_link_info (obfd);
-         if (link_info == NULL)
-           return bfd_reloc_dangerous;
-         h = bfd_link_hash_lookup (link_info->hash, "__ImageBase",
-                                   false, false, false);
-         if (h == NULL)
-           return bfd_reloc_dangerous;
-         while (h->type == bfd_link_hash_indirect)
-           h = h->u.i.link;
+         if (link_info != NULL)
+           h = bfd_link_hash_lookup (link_info->hash, "__ImageBase",
+                                     false, false, true);
+         if (h == NULL
+             || (h->type != bfd_link_hash_defined
+                 && h->type != bfd_link_hash_defweak))
+           {
+             *error_message
+               = (char *) _("R_AMD64_IMAGEBASE with __ImageBase undefined");
+             return bfd_reloc_dangerous;
+           }
          /* ELF symbols in relocatable files are section relative,
             but in nonrelocatable files they are virtual addresses.  */
          diff -= (h->u.def.value