Add startswith function and use it instead of CONST_STRNEQ.
[binutils-gdb.git] / ld / pe-dll.c
index 39c8e4c5ea790d39123f0286793a90b432855c2b..154c6846beb8c576e57c86bab729ab2f033fcb6a 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines to help build PEI-format DLLs (Win32 etc)
-   Copyright (C) 1998-2020 Free Software Foundation, Inc.
+   Copyright (C) 1998-2021 Free Software Foundation, Inc.
    Written by DJ Delorie <dj@cygnus.com>
 
    This file is part of the GNU Binutils.
@@ -160,7 +160,7 @@ int pe_dll_extra_pe_debug = 0;
 int pe_use_nul_prefixed_import_tables = 0;
 int pe_use_coff_long_section_names = -1;
 int pe_leading_underscore = -1;
-int pe_dll_enable_reloc_section = 0;
+int pe_dll_enable_reloc_section = 1;
 
 /* Static variables and types.  */
 
@@ -545,7 +545,7 @@ pe_dll_add_excludes (const char *new_excludes, const exclude_type type)
 static bfd_boolean
 is_import (const char* n)
 {
-  return (CONST_STRNEQ (n, "__imp_"));
+  return (startswith (n, "__imp_"));
 }
 
 /* abfd is a bfd containing n (or NULL)
@@ -1506,7 +1506,6 @@ pe_find_data_imports (const char *symhead,
 static void
 generate_reloc (bfd *abfd, struct bfd_link_info *info)
 {
-
   /* For .reloc stuff.  */
   reloc_data_type *reloc_data;
   int total_relocs = 0;
@@ -1517,6 +1516,8 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
   bfd *b;
   struct bfd_section *s;
 
+  if (reloc_s == NULL)
+    return;
   total_relocs = 0;
   for (b = info->input_bfds; b; b = b->link.next)
     for (s = b->sections; s; s = s->next)
@@ -1548,9 +1549,11 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
          if (s->output_section->vma == 0)
            {
              /* Huh?  Shouldn't happen, but punt if it does.  */
+#if 0 /* This happens when linking with --just-symbols=<file>, so do not generate an error.  */
              einfo (_("%P: zero vma section reloc detected: `%s' #%d f=%d\n"),
                     s->output_section->name, s->output_section->index,
                     s->output_section->flags);
+#endif
              continue;
            }
 
@@ -1576,13 +1579,13 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
                  && relocs[i]->howto->type != pe_details->imagebase_reloc)
                {
                  struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
+                 const struct bfd_link_hash_entry *blhe
+                   = bfd_wrapped_link_hash_lookup (abfd, info, sym->name,
+                                                   FALSE, FALSE, FALSE);
 
                  /* Don't create relocs for undefined weak symbols.  */
                  if (sym->flags == BSF_WEAK)
                    {
-                     struct bfd_link_hash_entry *blhe
-                       = bfd_wrapped_link_hash_lookup (abfd, info, sym->name,
-                                               FALSE, FALSE, FALSE);
                      if (blhe && blhe->type == bfd_link_hash_undefweak)
                        {
                          /* Check aux sym and see if it is defined or not. */
@@ -1614,6 +1617,12 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
                      if (!strcmp (s->name, ".eh_frame"))
                        continue;
                    }
+                 /* Nor for absolute symbols.  */
+                 else if (blhe && ldexp_is_final_sym_absolute (blhe)
+                          && (!blhe->linker_def
+                              || (strcmp (sym->name, "__image_base__")
+                                  && strcmp (sym->name, U ("__ImageBase")))))
+                   continue;
 
                  reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
                  reloc_data[total_relocs].idx = total_relocs;
@@ -1743,9 +1752,6 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
 
   if (page_ptr != (bfd_vma) -1)
     bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
-
-  while (reloc_sz < reloc_s->size)
-    reloc_d[reloc_sz++] = 0;
 }
 
 /* Given the exiting def_file structure, print out a .DEF file that
@@ -3033,7 +3039,9 @@ pe_find_cdecl_alias_match (struct bfd_link_info *linfo, char *name)
          if (pe_details->underscored)
            lname[0] = '_';
          else
-           strcpy (lname, lname + 1);
+           /* Use memmove rather than strcpy as that
+              can handle overlapping buffers.  */
+           memmove (lname, lname + 1, strlen (lname));
          key.key = lname;
          kv = bsearch (&key, udef_table, undef_count,
                        sizeof (struct key_value), undef_sort_cmp);
@@ -3523,7 +3531,7 @@ pe_implied_import_dll (const char *filename)
 
       /* Skip unwanted symbols, which are
         exported in buggy auto-import releases.  */
-      if (! CONST_STRNEQ (erva + name_rva, "__nm_"))
+      if (! startswith (erva + name_rva, "__nm_"))
        {
          int is_dup = 0;
          /* is_data is true if the address is in the data, rdata or bss
@@ -3631,7 +3639,8 @@ pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
       /* Do the assignments again.  */
       lang_do_assignments (lang_final_phase_enum);
     }
-  reloc_s->contents = reloc_d;
+  if (reloc_s)
+    reloc_s->contents = reloc_d;
 }
 
 bfd_boolean