]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/xcofflink.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / xcofflink.c
index b7a50de4171736246e51de3449af241ec11dd798..4df5ee17142d285c9d21e4c14d41fde96368777d 100644 (file)
@@ -1,5 +1,5 @@
 /* POWER/PowerPC XCOFF linker support.
-   Copyright (C) 1995-2018 Free Software Foundation, Inc.
+   Copyright (C) 1995-2021 Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -28,6 +28,7 @@
 #include "libcoff.h"
 #include "libxcoff.h"
 #include "libiberty.h"
+#include "xcofflink.h"
 
 /* This file holds the XCOFF linker code.  */
 
@@ -213,7 +214,7 @@ xcoff_get_section_contents (bfd *abfd, asection *sec)
 {
   if (coff_section_data (abfd, sec) == NULL)
     {
-      bfd_size_type amt = sizeof (struct coff_section_tdata);
+      size_t amt = sizeof (struct coff_section_tdata);
 
       sec->used_by_bfd = bfd_zalloc (abfd, amt);
       if (sec->used_by_bfd == NULL)
@@ -226,8 +227,7 @@ xcoff_get_section_contents (bfd *abfd, asection *sec)
 
       if (! bfd_malloc_and_get_section (abfd, sec, &contents))
        {
-         if (contents != NULL)
-           free (contents);
+         free (contents);
          return FALSE;
        }
       coff_section_data (abfd, sec)->contents = contents;
@@ -525,7 +525,7 @@ xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
   entryp = *slot;
   if (!entryp)
     {
-      entryp = bfd_zalloc (archive, sizeof (entry));
+      entryp = bfd_zalloc (info->output_bfd, sizeof (entry));
       if (!entryp)
        return NULL;
 
@@ -592,7 +592,7 @@ struct bfd_link_hash_table *
 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
 {
   struct xcoff_link_hash_table *ret;
-  bfd_size_type amt = sizeof (* ret);
+  size_t amt = sizeof (* ret);
 
   ret = bfd_zmalloc (amt);
   if (ret == NULL)
@@ -763,7 +763,7 @@ xcoff_set_import_path (struct bfd_link_info *info,
       if (*pp == NULL)
        {
          struct xcoff_import_file *n;
-         bfd_size_type amt = sizeof (* n);
+         size_t amt = sizeof (*n);
 
          n = bfd_alloc (info->output_bfd, amt);
          if (n == NULL)
@@ -999,7 +999,7 @@ xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
 
   if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
     {
-      if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
+      if (!bfd_xcoff_split_import_path (abfd, bfd_get_filename (abfd),
                                        &n->path, &n->file))
        return FALSE;
       n->member = "";
@@ -1012,7 +1012,8 @@ xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
       if (!archive_info->impfile)
        {
          if (!bfd_xcoff_split_import_path (archive_info->archive,
-                                           archive_info->archive->filename,
+                                           bfd_get_filename (archive_info
+                                                             ->archive),
                                            &archive_info->imppath,
                                            &archive_info->impfile))
            return FALSE;
@@ -1316,14 +1317,17 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
        {
          bfd_byte *linenos;
 
-         amt = linesz * o->lineno_count;
-         linenos = bfd_malloc (amt);
+         if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
+           goto error_return;
+         if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
+           {
+             bfd_set_error (bfd_error_file_too_big);
+             goto error_return;
+           }
+         linenos = _bfd_malloc_and_read (abfd, amt, amt);
          if (linenos == NULL)
            goto error_return;
          reloc_info[o->target_index].linenos = linenos;
-         if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
-             || bfd_bread (linenos, amt, abfd) != amt)
-           goto error_return;
        }
     }
 
@@ -1882,7 +1886,7 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
 
       if (EXTERN_SYM_P (sym.n_sclass))
        {
-         bfd_boolean copy;
+         bfd_boolean copy, ok;
          flagword flags;
 
          BFD_ASSERT (section != NULL);
@@ -2022,12 +2026,12 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
          BFD_ASSERT (last_real->next == first_csect);
          last_real->next = NULL;
          flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
-         if (! (_bfd_generic_link_add_one_symbol
-                (info, abfd, name, flags, section, value,
-                 NULL, copy, TRUE,
-                 (struct bfd_link_hash_entry **) sym_hash)))
-           goto error_return;
+         ok = (_bfd_generic_link_add_one_symbol
+               (info, abfd, name, flags, section, value, NULL, copy, TRUE,
+                (struct bfd_link_hash_entry **) sym_hash));
          last_real->next = first_csect;
+         if (!ok)
+           goto error_return;
 
          if (smtyp == XTY_CM)
            {
@@ -2080,14 +2084,14 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   for (o = abfd->sections; o != first_csect; o = o->next)
     {
       /* Debugging sections have no csects.  */
-      if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING)
+      if (bfd_section_flags (o) & SEC_DEBUGGING)
        continue;
 
       /* Reset the section size and the line number count, since the
         data is now attached to the csects.  Don't reset the size of
         the .debug section, since we need to read it below in
         bfd_xcoff_size_dynamic_sections.  */
-      if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
+      if (strcmp (bfd_section_name (o), ".debug") != 0)
        o->size = 0;
       o->lineno_count = 0;
 
@@ -2174,7 +2178,6 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
          /* If we are not keeping memory, free the reloc information.  */
          if (! info->keep_memory
              && coff_section_data (abfd, o) != NULL
-             && coff_section_data (abfd, o)->relocs != NULL
              && ! coff_section_data (abfd, o)->keep_relocs)
            {
              free (coff_section_data (abfd, o)->relocs);
@@ -2184,11 +2187,8 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
 
       /* Free up the line numbers.  FIXME: We could cache these
         somewhere for the final link, to avoid reading them again.  */
-      if (reloc_info[o->target_index].linenos != NULL)
-       {
-         free (reloc_info[o->target_index].linenos);
-         reloc_info[o->target_index].linenos = NULL;
-       }
+      free (reloc_info[o->target_index].linenos);
+      reloc_info[o->target_index].linenos = NULL;
     }
 
   free (reloc_info);
@@ -2202,10 +2202,8 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
     {
       for (o = abfd->sections; o != NULL; o = o->next)
        {
-         if (reloc_info[o->target_index].csects != NULL)
-           free (reloc_info[o->target_index].csects);
-         if (reloc_info[o->target_index].linenos != NULL)
-           free (reloc_info[o->target_index].linenos);
+         free (reloc_info[o->target_index].csects);
+         free (reloc_info[o->target_index].linenos);
        }
       free (reloc_info);
     }
@@ -2519,7 +2517,7 @@ xcoff_find_function (struct bfd_link_info *info,
     {
       char *fnname;
       struct xcoff_link_hash_entry *hfn;
-      bfd_size_type amt;
+      size_t amt;
 
       amt = strlen (h->root.root.string) + 2;
       fnname = bfd_malloc (amt);
@@ -2687,9 +2685,17 @@ xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
     case R_RLA:
       /* Absolute relocations against absolute symbols can be
         resolved statically.  */
-      if (h != NULL && bfd_is_abs_symbol (&h->root))
-       return FALSE;
-
+      if (h != NULL
+         && (h->root.type == bfd_link_hash_defined
+             || h->root.type == bfd_link_hash_defweak)
+         && !h->root.rel_from_abs)
+       {
+         asection *sec = h->root.u.def.section;
+         if (bfd_is_abs_section (sec)
+             || (sec != NULL
+                 && bfd_is_abs_section (sec->output_section)))
+           return FALSE;
+       }
       return TRUE;
     }
 }
@@ -2900,7 +2906,7 @@ xcoff_mark_symbol_by_name (struct bfd_link_info *info,
 static bfd_boolean
 xcoff_mark (struct bfd_link_info *info, asection *sec)
 {
-  if (bfd_is_abs_section (sec)
+  if (bfd_is_const_section (sec)
       || (sec->flags & SEC_MARK) != 0)
     return TRUE;
 
@@ -2980,7 +2986,6 @@ xcoff_mark (struct bfd_link_info *info, asection *sec)
 
          if (! info->keep_memory
              && coff_section_data (sec->owner, sec) != NULL
-             && coff_section_data (sec->owner, sec)->relocs != NULL
              && ! coff_section_data (sec->owner, sec)->keep_relocs)
            {
              free (coff_section_data (sec->owner, sec)->relocs);
@@ -3019,7 +3024,7 @@ xcoff_sweep (struct bfd_link_info *info)
                  || o == xcoff_hash_table (info)->loader_section
                  || o == xcoff_hash_table (info)->linkage_section
                  || o == xcoff_hash_table (info)->descriptor_section
-                 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING)
+                 || (bfd_section_flags (o) & SEC_DEBUGGING)
                  || strcmp (o->name, ".debug") == 0)
                o->flags |= SEC_MARK;
              else
@@ -3043,7 +3048,7 @@ bfd_xcoff_link_record_set (bfd *output_bfd,
 {
   struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
   struct xcoff_link_size_list *n;
-  bfd_size_type amt;
+  size_t amt;
 
   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
     return TRUE;
@@ -3121,9 +3126,7 @@ bfd_xcoff_import_symbol (bfd *output_bfd,
 
   if (val != (bfd_vma) -1)
     {
-      if (h->root.type == bfd_link_hash_defined
-         && (!bfd_is_abs_symbol (&h->root)
-             || h->root.u.def.value != val))
+      if (h->root.type == bfd_link_hash_defined)
        (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
                                                 bfd_abs_section_ptr, val);
 
@@ -3213,7 +3216,13 @@ bfd_xcoff_link_count_reloc (bfd *output_bfd,
 }
 
 /* This function is called for each symbol to which the linker script
-   assigns a value.  */
+   assigns a value.
+   FIXME: In cases like the linker test ld-scripts/defined5 where a
+   symbol is defined both by an input object file and the script,
+   the script definition doesn't override the object file definition
+   as is usual for other targets.  At least not when the symbol is
+   output.  Other uses of the symbol value by the linker do use the
+   script value.  */
 
 bfd_boolean
 bfd_xcoff_record_link_assignment (bfd *output_bfd,
@@ -3291,7 +3300,7 @@ static bfd_boolean
 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
                   struct xcoff_link_hash_entry *h)
 {
-  bfd_size_type amt;
+  size_t amt;
 
   /* Warn if this symbol is exported but not defined.  */
   if ((h->flags & XCOFF_EXPORT) != 0
@@ -3639,7 +3648,7 @@ bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
   bfd *sub;
   struct bfd_strtab_hash *debug_strtab;
   bfd_byte *debug_contents = NULL;
-  bfd_size_type amt;
+  size_t amt;
 
   if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
     {
@@ -3966,10 +3975,8 @@ bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
   return TRUE;
 
  error_return:
-  if (ldinfo.strings != NULL)
-    free (ldinfo.strings);
-  if (debug_contents != NULL)
-    free (debug_contents);
+  free (ldinfo.strings);
+  free (debug_contents);
   return FALSE;
 }
 
@@ -4781,7 +4788,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
        {
          bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
          if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
-           return FALSE;
+           goto err_out;
          contents = flinfo->contents;
        }
 
@@ -4803,7 +4810,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
                              (flinfo->section_info[target_index].relocs
                               + o->output_section->reloc_count)));
          if (internal_relocs == NULL)
-           return FALSE;
+           goto err_out;
 
          /* Call processor specific code to relocate the section
             contents.  */
@@ -4813,7 +4820,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
                                           internal_relocs,
                                           flinfo->internal_syms,
                                           xcoff_data (input_bfd)->csects))
-           return FALSE;
+           goto err_out;
 
          offset = o->output_section->vma + o->output_offset - o->vma;
          irel = internal_relocs;
@@ -4860,12 +4867,12 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
                        {
                          struct xcoff_toc_rel_hash *n;
                          struct xcoff_link_section_info *si;
-                         bfd_size_type amt;
+                         size_t amt;
 
                          amt = sizeof (* n);
                          n = bfd_alloc (flinfo->output_bfd, amt);
                          if (n == NULL)
-                           return FALSE;
+                           goto err_out;
                          si = flinfo->section_info + target_index;
                          n->next = si->toc_rel_hashes;
                          n->h = h;
@@ -4947,7 +4954,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
                                      (input_bfd, is, buf));
 
                              if (name == NULL)
-                               return FALSE;
+                               goto err_out;
 
                              (*flinfo->info->callbacks->unattached_reloc)
                                (flinfo->info, name,
@@ -4971,7 +4978,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
                  if (!xcoff_create_ldrel (output_bfd, flinfo,
                                           o->output_section, input_bfd,
                                           irel, sec, h))
-                   return FALSE;
+                   goto err_out;
                }
            }
 
@@ -4982,7 +4989,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
       if (! bfd_set_section_contents (output_bfd, o->output_section,
                                      contents, (file_ptr) o->output_offset,
                                      o->size))
-       return FALSE;
+       goto err_out;
     }
 
   obj_coff_keep_syms (input_bfd) = keep_syms;
@@ -4994,6 +5001,10 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
     }
 
   return TRUE;
+
+ err_out:
+  obj_coff_keep_syms (input_bfd) = keep_syms;
+  return FALSE;
 }
 
 #undef N_TMASK
@@ -5589,7 +5600,7 @@ xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
       BFD_ASSERT (bfd_is_abs_symbol (&h->root));
       isym.n_value = h->root.u.def.value;
       isym.n_scnum = N_UNDEF;
-      if (h->root.type == bfd_link_hash_undefweak
+      if (h->root.type == bfd_link_hash_defweak
          && C_WEAKEXT == C_AIX_WEAKEXT)
        isym.n_sclass = C_WEAKEXT;
       else
@@ -5655,7 +5666,7 @@ xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
       /* We just output an SD symbol.  Now output an LD symbol.  */
       h->indx += 2;
 
-      if (h->root.type == bfd_link_hash_undefweak
+      if (h->root.type == bfd_link_hash_defweak
          && C_WEAKEXT == C_AIX_WEAKEXT)
        isym.n_sclass = C_WEAKEXT;
       else
@@ -6198,31 +6209,16 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
     }
 
   /* Free up the buffers used by xcoff_link_input_bfd.  */
-  if (flinfo.internal_syms != NULL)
-    {
-      free (flinfo.internal_syms);
-      flinfo.internal_syms = NULL;
-    }
-  if (flinfo.sym_indices != NULL)
-    {
-      free (flinfo.sym_indices);
-      flinfo.sym_indices = NULL;
-    }
-  if (flinfo.linenos != NULL)
-    {
-      free (flinfo.linenos);
-      flinfo.linenos = NULL;
-    }
-  if (flinfo.contents != NULL)
-    {
-      free (flinfo.contents);
-      flinfo.contents = NULL;
-    }
-  if (flinfo.external_relocs != NULL)
-    {
-      free (flinfo.external_relocs);
-      flinfo.external_relocs = NULL;
-    }
+  free (flinfo.internal_syms);
+  flinfo.internal_syms = NULL;
+  free (flinfo.sym_indices);
+  flinfo.sym_indices = NULL;
+  free (flinfo.linenos);
+  flinfo.linenos = NULL;
+  free (flinfo.contents);
+  flinfo.contents = NULL;
+  free (flinfo.external_relocs);
+  flinfo.external_relocs = NULL;
 
   /* The value of the last C_FILE symbol is supposed to be -1.  Write
      it out again.  */
@@ -6241,11 +6237,8 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
      input files.  */
   bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
 
-  if (flinfo.outsyms != NULL)
-    {
-      free (flinfo.outsyms);
-      flinfo.outsyms = NULL;
-    }
+  free (flinfo.outsyms);
+  flinfo.outsyms = NULL;
 
   /* Now that we have written out all the global symbols, we know the
      symbol indices to use for relocs against them, and we can finally
@@ -6327,11 +6320,8 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
        goto error_return;
     }
 
-  if (external_relocs != NULL)
-    {
-      free (external_relocs);
-      external_relocs = NULL;
-    }
+  free (external_relocs);
+  external_relocs = NULL;
 
   /* Free up the section information.  */
   if (flinfo.section_info != NULL)
@@ -6340,10 +6330,8 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 
       for (i = 0; i < abfd->section_count; i++)
        {
-         if (flinfo.section_info[i].relocs != NULL)
-           free (flinfo.section_info[i].relocs);
-         if (flinfo.section_info[i].rel_hashes != NULL)
-           free (flinfo.section_info[i].rel_hashes);
+         free (flinfo.section_info[i].relocs);
+         free (flinfo.section_info[i].rel_hashes);
        }
       free (flinfo.section_info);
       flinfo.section_info = NULL;
@@ -6351,7 +6339,9 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 
   /* Write out the loader section contents.  */
   o = xcoff_hash_table (info)->loader_section;
-  if (o)
+  if (o != NULL
+      && o->size != 0
+      && o->output_section != bfd_abs_section_ptr)
     {
       BFD_ASSERT ((bfd_byte *) flinfo.ldrel
                  == (xcoff_hash_table (info)->loader_section->contents
@@ -6363,19 +6353,25 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 
   /* Write out the magic sections.  */
   o = xcoff_hash_table (info)->linkage_section;
-  if (o->size > 0
+  if (o != NULL
+      && o->size != 0
+      && o->output_section != bfd_abs_section_ptr
       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
                                     (file_ptr) o->output_offset,
                                     o->size))
     goto error_return;
   o = xcoff_hash_table (info)->toc_section;
-  if (o->size > 0
+  if (o != NULL
+      && o->size != 0
+      && o->output_section != bfd_abs_section_ptr
       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
                                     (file_ptr) o->output_offset,
                                     o->size))
     goto error_return;
   o = xcoff_hash_table (info)->descriptor_section;
-  if (o->size > 0
+  if (o != NULL
+      && o->size != 0
+      && o->output_section != bfd_abs_section_ptr
       && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
                                     (file_ptr) o->output_offset,
                                     o->size))
@@ -6398,7 +6394,9 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 
   /* Write out the debugging string table.  */
   o = xcoff_hash_table (info)->debug_section;
-  if (o != NULL)
+  if (o != NULL
+      && o->size != 0
+      && o->output_section != bfd_abs_section_ptr)
     {
       struct bfd_strtab_hash *debug_strtab;
 
@@ -6412,9 +6410,9 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
        goto error_return;
     }
 
-  /* Setting bfd_get_symcount to 0 will cause write_object_contents to
+  /* Setting symcount to 0 will cause write_object_contents to
      not try to write out the symbols.  */
-  bfd_get_symcount (abfd) = 0;
+  abfd->symcount = 0;
 
   return TRUE;
 
@@ -6428,27 +6426,18 @@ _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 
       for (i = 0; i < abfd->section_count; i++)
        {
-         if (flinfo.section_info[i].relocs != NULL)
-           free (flinfo.section_info[i].relocs);
-         if (flinfo.section_info[i].rel_hashes != NULL)
-           free (flinfo.section_info[i].rel_hashes);
+         free (flinfo.section_info[i].relocs);
+         free (flinfo.section_info[i].rel_hashes);
        }
       free (flinfo.section_info);
     }
 
-  if (flinfo.internal_syms != NULL)
-    free (flinfo.internal_syms);
-  if (flinfo.sym_indices != NULL)
-    free (flinfo.sym_indices);
-  if (flinfo.outsyms != NULL)
-    free (flinfo.outsyms);
-  if (flinfo.linenos != NULL)
-    free (flinfo.linenos);
-  if (flinfo.contents != NULL)
-    free (flinfo.contents);
-  if (flinfo.external_relocs != NULL)
-    free (flinfo.external_relocs);
-  if (external_relocs != NULL)
-    free (external_relocs);
+  free (flinfo.internal_syms);
+  free (flinfo.sym_indices);
+  free (flinfo.outsyms);
+  free (flinfo.linenos);
+  free (flinfo.contents);
+  free (flinfo.external_relocs);
+  free (external_relocs);
   return FALSE;
 }