Automatic date update in version.in
[binutils-gdb.git] / bfd / arc-got.h
index 81ce88fe21a039ddc822f6c4fdefc3fd19b8b817..0d9b9a18f87134bc2dbbc9b53f0a50bb0577de99 100644 (file)
@@ -1,5 +1,5 @@
 /* ARC-specific support for 32-bit ELF
-   Copyright (C) 1994-2018 Free Software Foundation, Inc.
+   Copyright (C) 1994-2022 Free Software Foundation, Inc.
    Contributed by Cupertino Miranda (cmiranda@synopsys.com).
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -24,6 +24,9 @@
 
 #define TCB_SIZE (8)
 
+#define        align_power(addr, align)        \
+  (((addr) + ((bfd_vma) 1 << (align)) - 1) & (-((bfd_vma) 1 << (align))))
+
 enum tls_type_e
 {
   GOT_UNKNOWN = 0,
@@ -46,32 +49,31 @@ struct got_entry
   struct got_entry *next;
   enum tls_type_e type;
   bfd_vma offset;
-  bfd_boolean processed;
-  bfd_boolean created_dyn_relocation;
+  bool processed;
+  bool created_dyn_relocation;
   enum tls_got_entries existing_entries;
 };
 
+/* Return the local got list, if not defined, create an empty one.  */
+
 static struct got_entry **
 arc_get_local_got_ents (bfd * abfd)
 {
-  static struct got_entry **local_got_ents = NULL;
-
-  if (local_got_ents == NULL)
+  if (elf_local_got_ents (abfd) == NULL)
     {
-      size_t      size;
-      Elf_Internal_Shdr *symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
-
-      size = symtab_hdr->sh_info * sizeof (bfd_vma);
-      local_got_ents = (struct got_entry **)
-       bfd_alloc (abfd, sizeof (struct got_entry *) * size);
-      if (local_got_ents == NULL)
-       return FALSE;
-
-      memset (local_got_ents, 0, sizeof (struct got_entry *) * size);
-      elf_local_got_ents (abfd) = local_got_ents;
+      bfd_size_type amt = (elf_tdata (abfd)->symtab_hdr.sh_info
+                          * sizeof (*elf_local_got_ents (abfd)));
+      elf_local_got_ents (abfd) = bfd_zmalloc (amt);
+      if (elf_local_got_ents (abfd) == NULL)
+       {
+         _bfd_error_handler (_("%pB: cannot allocate memory for local "
+                               "GOT entries"), abfd);
+         bfd_set_error (bfd_error_bad_value);
+         return NULL;
+       }
     }
 
-  return local_got_ents;
+  return elf_local_got_ents (abfd);
 }
 
 static struct got_entry *
@@ -112,8 +114,8 @@ new_got_entry_to_list (struct got_entry **list,
   entry->type = type;
   entry->offset = offset;
   entry->next = NULL;
-  entry->processed = FALSE;
-  entry->created_dyn_relocation = FALSE;
+  entry->processed = false;
+  entry->created_dyn_relocation = false;
   entry->existing_entries = existing_entries;
 
   ARC_DEBUG ("New GOT got entry added to list: "
@@ -164,9 +166,7 @@ get_got_entry_list_for_symbol (bfd *abfd,
     }
   else
     {
-      struct got_entry **local_got_ents
-       = arc_get_local_got_ents (abfd);
-      return &local_got_ents[r_symndx];
+      return arc_get_local_got_ents (abfd) + r_symndx;
     }
 }
 
@@ -208,13 +208,13 @@ arc_got_entry_type_for_reloc (reloc_howto_type *howto)
                     __LINE__, name_for_global_symbol (H));             \
       }                                                                        \
     if (H)                                                             \
-      if (h->dynindx == -1 && !h->forced_local)                                \
+      if (H->dynindx == -1 && !H->forced_local)                                \
        if (! bfd_elf_link_record_dynamic_symbol (info, H))             \
-         return FALSE;                                                 \
+         return false;                                                 \
      htab->s##SECNAME->size += 4;                                      \
    }                                                                   \
 
-static bfd_boolean
+static bool
 arc_fill_got_info_for_reloc (enum tls_type_e type,
                             struct got_entry **list,
                             struct bfd_link_info *  info,
@@ -223,7 +223,7 @@ arc_fill_got_info_for_reloc (enum tls_type_e type,
   struct elf_link_hash_table *htab = elf_hash_table (info);
 
   if (got_entry_for_type (list, type) != NULL)
-    return TRUE;
+    return true;
 
   switch (type)
     {
@@ -240,9 +240,9 @@ arc_fill_got_info_for_reloc (enum tls_type_e type,
       case GOT_TLS_GD:
        {
          bfd_vma offset
-           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
+           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, true, h);
          bfd_vma ATTRIBUTE_UNUSED notneeded
-           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
+           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, true, h);
          new_got_entry_to_list (list, type, offset, TLS_GOT_MOD_AND_OFF);
        }
        break;
@@ -250,18 +250,60 @@ arc_fill_got_info_for_reloc (enum tls_type_e type,
       case GOT_TLS_LE:
        {
          bfd_vma offset
-           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
+           = ADD_SYMBOL_REF_SEC_AND_RELOC (got, true, h);
          new_got_entry_to_list (list, type, offset, TLS_GOT_OFF);
        }
        break;
 
       default:
-       return FALSE;
+       return false;
        break;
     }
-  return TRUE;
+  return true;
 }
 
+struct arc_static_sym_data {
+  bfd_vma sym_value;
+  const char *symbol_name;
+};
+
+static struct arc_static_sym_data
+get_static_sym_data (unsigned long  r_symndx,
+                    Elf_Internal_Sym  *local_syms,
+                    asection **local_sections,
+                    struct elf_link_hash_entry *h,
+                    struct arc_relocation_data *reloc_data)
+{
+  static const char local_name[] = "(local)";
+  struct arc_static_sym_data ret = { 0, NULL };
+
+  if (h != NULL)
+    {
+      BFD_ASSERT (h->root.type != bfd_link_hash_undefweak
+                 && h->root.type != bfd_link_hash_undefined);
+      /* TODO: This should not be here.  */
+      reloc_data->sym_value = h->root.u.def.value;
+      reloc_data->sym_section = h->root.u.def.section;
+
+      ret.sym_value = h->root.u.def.value
+       + h->root.u.def.section->output_section->vma
+       + h->root.u.def.section->output_offset;
+
+      ret.symbol_name = h->root.root.string;
+    }
+  else
+  {
+    Elf_Internal_Sym *sym = local_syms + r_symndx;
+    asection *sec = local_sections[r_symndx];
+
+    ret.sym_value = sym->st_value
+      + sec->output_section->vma
+      + sec->output_offset;
+
+    ret.symbol_name = local_name;
+  }
+  return ret;
+}
 
 static bfd_vma
 relocate_fix_got_relocs_for_got_info (struct got_entry **         list_p,
@@ -284,43 +326,13 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **    list_p,
   BFD_ASSERT (entry);
 
   if (h == NULL
+      || h->forced_local == true
       || (! elf_hash_table (info)->dynamic_sections_created
          || (bfd_link_pic (info)
              && SYMBOL_REFERENCES_LOCAL (info, h))))
     {
       const char ATTRIBUTE_UNUSED *symbol_name;
-      static const char local_name[] = "(local)";
-      asection *tls_sec = NULL;
-      bfd_vma sym_value = 0;
-
-      if (h != NULL)
-       {
-         // TODO: This should not be here.
-         reloc_data->sym_value = h->root.u.def.value;
-         reloc_data->sym_section = h->root.u.def.section;
-
-         sym_value = h->root.u.def.value
-           + h->root.u.def.section->output_section->vma
-           + h->root.u.def.section->output_offset;
-
-         tls_sec = elf_hash_table (info)->tls_sec;
-
-         symbol_name = h->root.root.string;
-       }
-      else
-       {
-         Elf_Internal_Sym *sym = local_syms + r_symndx;
-         asection *sec = local_sections[r_symndx];
-
-         sym_value = sym->st_value
-           + sec->output_section->vma
-           + sec->output_offset;
-
-         tls_sec = elf_hash_table (info)->tls_sec;
-
-         symbol_name = local_name;
-       }
-
+      asection *tls_sec = elf_hash_table (info)->tls_sec;
 
       if (entry && !entry->processed)
        {
@@ -331,23 +343,35 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **    list_p,
                BFD_ASSERT (tls_sec && tls_sec->output_section);
                bfd_vma sec_vma = tls_sec->output_section->vma;
 
-               bfd_put_32 (output_bfd,
-                           sym_value - sec_vma,
+               if (h == NULL || h->forced_local
+                  || !elf_hash_table (info)->dynamic_sections_created)
+                 {
+                   struct arc_static_sym_data tmp =
+                     get_static_sym_data (r_symndx, local_syms, local_sections,
+                                          h, reloc_data);
+
+                   bfd_put_32 (output_bfd,
+                           tmp.sym_value - sec_vma
+                           + (elf_hash_table (info)->dynamic_sections_created
+                              ? 0
+                              : (align_power (0,
+                                              tls_sec->alignment_power))),
                            htab->sgot->contents + entry->offset
                            + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
                               ? 4 : 0));
 
-               ARC_DEBUG ("arc_info: FIXED -> %s value = %#lx "
-                          "@ %lx, for symbol %s\n",
-                          (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
-                           "GOT_TLS_IE"),
-                          (long) (sym_value - sec_vma),
-                          (long) (htab->sgot->output_section->vma
-                             + htab->sgot->output_offset->vma
-                             + entry->offset
-                             + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
-                                ? 4 : 0)),
-                          symbol_name);
+                   ARC_DEBUG ("arc_info: FIXED -> %s value = %#lx "
+                         "@ %lx, for symbol %s\n",
+                         (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
+                          "GOT_TLS_IE"),
+                         (long) (sym_value - sec_vma),
+                         (long) (htab->sgot->output_section->vma
+                            + htab->sgot->output_offset
+                            + entry->offset
+                            + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
+                               ? 4 : 0)),
+                         tmp.symbol_name);
+                 }
              }
              break;
 
@@ -357,9 +381,16 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **     list_p,
                bfd_vma ATTRIBUTE_UNUSED sec_vma
                  = tls_sec->output_section->vma;
 
+               struct arc_static_sym_data tmp =
+                 get_static_sym_data (r_symndx, local_syms, local_sections,
+                                      h, reloc_data);
+
                bfd_put_32 (output_bfd,
-                           sym_value - sec_vma
-                           + (elf_hash_table (info)->dynamic_sections_created ? 0 : TCB_SIZE),
+                           tmp.sym_value - sec_vma
+                           + (elf_hash_table (info)->dynamic_sections_created
+                              ? 0
+                              : (align_power (TCB_SIZE,
+                                              tls_sec->alignment_power))),
                            htab->sgot->contents + entry->offset
                            + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
                               ? 4 : 0));
@@ -370,11 +401,11 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **    list_p,
                            "GOT_TLS_IE"),
                           (long) (sym_value - sec_vma),
                           (long) (htab->sgot->output_section->vma
-                             + htab->sgot->output_offset->vma
+                             + htab->sgot->output_offset
                              + entry->offset
                              + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
                                 ? 4 : 0)),
-                          symbol_name);
+                          tmp.symbol_name);
              }
              break;
 
@@ -403,7 +434,8 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **      list_p,
                               "@ %#08lx for sym %s in got offset %#lx\n",
                               (long) (reloc_data->sym_value + sec_vma),
                               (long) (htab->sgot->output_section->vma
-                                      + htab->sgot->output_offset + entry->offset),
+                                      + htab->sgot->output_offset
+                                      + entry->offset),
                               symbol_name,
                               (long) entry->offset);
                  }
@@ -413,7 +445,7 @@ relocate_fix_got_relocs_for_got_info (struct got_entry **      list_p,
              BFD_ASSERT (0);
              break;
            }
-         entry->processed = TRUE;
+         entry->processed = true;
        }
     }
 
@@ -448,7 +480,7 @@ create_got_dynrelocs_for_single_entry (struct got_entry *list,
        {
          ADD_RELA (output_bfd, got, got_offset, h->dynindx, R_ARC_GLOB_DAT, 0);
        }
-      list->created_dyn_relocation = TRUE;
+      list->created_dyn_relocation = true;
     }
   else if (list->existing_entries != TLS_GOT_NONE
           && !list->created_dyn_relocation)
@@ -503,7 +535,7 @@ GOT_OFFSET = %#lx, GOT_VMA = %#lx, INDEX = %ld, ADDEND = %#lx\n",
                             + htab->sgot->output_offset + got_offset),
                     (long) dynindx, (long) addend);
        }
-      list->created_dyn_relocation = TRUE;
+      list->created_dyn_relocation = true;
     }
 }