*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elflink.c
index f06a49b2efa30d09cb5355d517f9775f86b332e7..3170035787e564c8abefcdaa692940480da8f000 100644 (file)
@@ -1,5 +1,5 @@
 /* ELF linking support for BFD.
-   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
+   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
    Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #define ARCH_SIZE 0
 #include "elf-bfd.h"
 
-boolean
+bfd_boolean
 _bfd_elf_create_got_section (abfd, info)
      bfd *abfd;
      struct bfd_link_info *info;
@@ -33,12 +33,13 @@ _bfd_elf_create_got_section (abfd, info)
   flagword flags;
   register asection *s;
   struct elf_link_hash_entry *h;
+  struct bfd_link_hash_entry *bh;
   struct elf_backend_data *bed = get_elf_backend_data (abfd);
   int ptralign;
 
   /* This function may be called more than once.  */
   if (bfd_get_section_by_name (abfd, ".got") != NULL)
-    return true;
+    return TRUE;
 
   switch (bed->s->arch_size)
     {
@@ -52,7 +53,7 @@ _bfd_elf_create_got_section (abfd, info)
 
     default:
       bfd_set_error (bfd_error_bad_value);
-      return false;
+      return FALSE;
     }
 
   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
@@ -62,7 +63,7 @@ _bfd_elf_create_got_section (abfd, info)
   if (s == NULL
       || !bfd_set_section_flags (abfd, s, flags)
       || !bfd_set_section_alignment (abfd, s, ptralign))
-    return false;
+    return FALSE;
 
   if (bed->want_got_plt)
     {
@@ -70,7 +71,7 @@ _bfd_elf_create_got_section (abfd, info)
       if (s == NULL
          || !bfd_set_section_flags (abfd, s, flags)
          || !bfd_set_section_alignment (abfd, s, ptralign))
-       return false;
+       return FALSE;
     }
 
   if (bed->want_got_sym)
@@ -79,18 +80,19 @@ _bfd_elf_create_got_section (abfd, info)
         (or .got.plt) section.  We don't do this in the linker script
         because we don't want to define the symbol if we are not creating
         a global offset table.  */
-      h = NULL;
+      bh = NULL;
       if (!(_bfd_generic_link_add_one_symbol
            (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
-            bed->got_symbol_offset, (const char *) NULL, false,
-            bed->collect, (struct bfd_link_hash_entry **) &h)))
-       return false;
+            bed->got_symbol_offset, (const char *) NULL, FALSE,
+            bed->collect, &bh)))
+       return FALSE;
+      h = (struct elf_link_hash_entry *) bh;
       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
       h->type = STT_OBJECT;
 
       if (info->shared
          && ! _bfd_elf_link_record_dynamic_symbol (info, h))
-       return false;
+       return FALSE;
 
       elf_hash_table (info)->hgot = h;
     }
@@ -98,12 +100,12 @@ _bfd_elf_create_got_section (abfd, info)
   /* The first bit of the global offset table is the header.  */
   s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
 
-  return true;
+  return TRUE;
 }
 \f
 /* Create dynamic sections when linking against a dynamic object.  */
 
-boolean
+bfd_boolean
 _bfd_elf_create_dynamic_sections (abfd, info)
      bfd *abfd;
      struct bfd_link_info *info;
@@ -125,7 +127,7 @@ _bfd_elf_create_dynamic_sections (abfd, info)
 
     default:
       bfd_set_error (bfd_error_bad_value);
-      return false;
+      return FALSE;
     }
 
   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
@@ -137,7 +139,7 @@ _bfd_elf_create_dynamic_sections (abfd, info)
   pltflags = flags;
   pltflags |= SEC_CODE;
   if (bed->plt_not_loaded)
-    pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
+    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
   if (bed->plt_readonly)
     pltflags |= SEC_READONLY;
 
@@ -145,25 +147,27 @@ _bfd_elf_create_dynamic_sections (abfd, info)
   if (s == NULL
       || ! bfd_set_section_flags (abfd, s, pltflags)
       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
-    return false;
+    return FALSE;
 
   if (bed->want_plt_sym)
     {
       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
         .plt section.  */
-      struct elf_link_hash_entry *h = NULL;
+      struct elf_link_hash_entry *h;
+      struct bfd_link_hash_entry *bh = NULL;
+
       if (! (_bfd_generic_link_add_one_symbol
             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
-             (bfd_vma) 0, (const char *) NULL, false,
-             get_elf_backend_data (abfd)->collect,
-             (struct bfd_link_hash_entry **) &h)))
-       return false;
+             (bfd_vma) 0, (const char *) NULL, FALSE,
+             get_elf_backend_data (abfd)->collect, &bh)))
+       return FALSE;
+      h = (struct elf_link_hash_entry *) bh;
       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
       h->type = STT_OBJECT;
 
       if (info->shared
          && ! _bfd_elf_link_record_dynamic_symbol (info, h))
-       return false;
+       return FALSE;
     }
 
   s = bfd_make_section (abfd,
@@ -171,10 +175,10 @@ _bfd_elf_create_dynamic_sections (abfd, info)
   if (s == NULL
       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
       || ! bfd_set_section_alignment (abfd, s, ptralign))
-    return false;
+    return FALSE;
 
   if (! _bfd_elf_create_got_section (abfd, info))
-    return false;
+    return FALSE;
 
   if (bed->want_dynbss)
     {
@@ -187,7 +191,7 @@ _bfd_elf_create_dynamic_sections (abfd, info)
       s = bfd_make_section (abfd, ".dynbss");
       if (s == NULL
          || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
-       return false;
+       return FALSE;
 
       /* The .rel[a].bss section holds copy relocs.  This section is not
      normally needed.  We need to create it here, though, so that the
@@ -208,11 +212,11 @@ _bfd_elf_create_dynamic_sections (abfd, info)
          if (s == NULL
              || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
              || ! bfd_set_section_alignment (abfd, s, ptralign))
-           return false;
+           return FALSE;
        }
     }
 
-  return true;
+  return TRUE;
 }
 \f
 /* Record a new dynamic symbol.  We record the dynamic symbols as we
@@ -223,17 +227,17 @@ _bfd_elf_create_dynamic_sections (abfd, info)
    symbol should be in the dynamic symbol table, but only if there is
    one.  */
 
-boolean
+bfd_boolean
 _bfd_elf_link_record_dynamic_symbol (info, h)
      struct bfd_link_info *info;
      struct elf_link_hash_entry *h;
 {
   if (h->dynindx == -1)
     {
-      struct bfd_strtab_hash *dynstr;
+      struct elf_strtab_hash *dynstr;
       char *p, *alc;
       const char *name;
-      boolean copy;
+      bfd_boolean copy;
       bfd_size_type indx;
 
       /* XXX: The ABI draft says the linker must turn hidden and
@@ -248,7 +252,7 @@ _bfd_elf_link_record_dynamic_symbol (info, h)
              && h->root.type != bfd_link_hash_undefweak)
            {
              h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
-             return true;
+             return TRUE;
            }
 
        default:
@@ -262,9 +266,9 @@ _bfd_elf_link_record_dynamic_symbol (info, h)
       if (dynstr == NULL)
        {
          /* Create a strtab to hold the dynamic symbol names.  */
-         elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
+         elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
          if (dynstr == NULL)
-           return false;
+           return FALSE;
        }
 
       /* We don't put any version information in the dynamic string
@@ -274,30 +278,124 @@ _bfd_elf_link_record_dynamic_symbol (info, h)
       if (p == NULL)
        {
          alc = NULL;
-         copy = false;
+         copy = FALSE;
        }
       else
        {
-         alc = bfd_malloc (p - name + 1);
+         size_t len = p - name + 1;
+
+         alc = bfd_malloc ((bfd_size_type) len);
          if (alc == NULL)
-           return false;
-         strncpy (alc, name, p - name);
-         alc[p - name] = '\0';
+           return FALSE;
+         memcpy (alc, name, len - 1);
+         alc[len - 1] = '\0';
          name = alc;
-         copy = true;
+         copy = TRUE;
        }
 
-      indx = _bfd_stringtab_add (dynstr, name, true, copy);
+      indx = _bfd_elf_strtab_add (dynstr, name, copy);
 
       if (alc != NULL)
        free (alc);
 
       if (indx == (bfd_size_type) -1)
-       return false;
+       return FALSE;
       h->dynstr_index = indx;
     }
 
-  return true;
+  return TRUE;
+}
+
+/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
+   success, and 2 on a failure caused by attempting to record a symbol
+   in a discarded section, eg. a discarded link-once section symbol.  */
+
+int
+elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
+     struct bfd_link_info *info;
+     bfd *input_bfd;
+     long input_indx;
+{
+  bfd_size_type amt;
+  struct elf_link_local_dynamic_entry *entry;
+  struct elf_link_hash_table *eht;
+  struct elf_strtab_hash *dynstr;
+  unsigned long dynstr_index;
+  char *name;
+  Elf_External_Sym_Shndx eshndx;
+  char esym[sizeof (Elf64_External_Sym)];
+
+  if (! is_elf_hash_table (info))
+    return 0;
+
+  /* See if the entry exists already.  */
+  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
+    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
+      return 1;
+
+  amt = sizeof (*entry);
+  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
+  if (entry == NULL)
+    return 0;
+
+  /* Go find the symbol, so that we can find it's name.  */
+  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
+                            (size_t) 1, (size_t) input_indx,
+                            &entry->isym, esym, &eshndx))
+    {
+      bfd_release (input_bfd, entry);
+      return 0;
+    }
+
+  if (entry->isym.st_shndx != SHN_UNDEF
+      && (entry->isym.st_shndx < SHN_LORESERVE
+         || entry->isym.st_shndx > SHN_HIRESERVE))
+    {
+      asection *s;
+
+      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
+      if (s == NULL || bfd_is_abs_section (s->output_section))
+       {
+         /* We can still bfd_release here as nothing has done another
+            bfd_alloc.  We can't do this later in this function.  */
+         bfd_release (input_bfd, entry);
+         return 2;
+       }
+    }
+
+  name = (bfd_elf_string_from_elf_section
+         (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
+          entry->isym.st_name));
+
+  dynstr = elf_hash_table (info)->dynstr;
+  if (dynstr == NULL)
+    {
+      /* Create a strtab to hold the dynamic symbol names.  */
+      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
+      if (dynstr == NULL)
+       return 0;
+    }
+
+  dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
+  if (dynstr_index == (unsigned long) -1)
+    return 0;
+  entry->isym.st_name = dynstr_index;
+
+  eht = elf_hash_table (info);
+
+  entry->next = eht->dynlocal;
+  eht->dynlocal = entry;
+  entry->input_bfd = input_bfd;
+  entry->input_indx = input_indx;
+  eht->dynsymcount++;
+
+  /* Whatever binding the symbol had before, it's now local.  */
+  entry->isym.st_info
+    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
+
+  /* The dynindx will be set at the end of size_dynamic_sections.  */
+
+  return 1;
 }
 
 /* Return the dynindex of a local dynamic symbol.  */
@@ -320,20 +418,23 @@ _bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx)
    them are removed because they are marked as local.  This is called
    via elf_link_hash_traverse.  */
 
-static boolean elf_link_renumber_hash_table_dynsyms
+static bfd_boolean elf_link_renumber_hash_table_dynsyms
   PARAMS ((struct elf_link_hash_entry *, PTR));
 
-static boolean
+static bfd_boolean
 elf_link_renumber_hash_table_dynsyms (h, data)
      struct elf_link_hash_entry *h;
      PTR data;
 {
   size_t *count = (size_t *) data;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if (h->dynindx != -1)
     h->dynindx = ++(*count);
 
-  return true;
+  return TRUE;
 }
 
 /* Assign dynsym indices.  In a shared library we generate a section
@@ -352,7 +453,8 @@ _bfd_elf_link_renumber_dynsyms (output_bfd, info)
     {
       asection *p;
       for (p = output_bfd->sections; p ; p = p->next)
-       elf_section_data (p)->dynindx = ++dynsymcount;
+       if ((p->flags & SEC_EXCLUDE) == 0)
+         elf_section_data (p)->dynindx = ++dynsymcount;
     }
 
   if (elf_hash_table (info)->dynlocal)
@@ -397,14 +499,14 @@ _bfd_elf_create_linker_section (abfd, info, which, defaults)
   if (!lsect)
     {
       asection *s;
+      bfd_size_type amt = sizeof (elf_linker_section_t);
 
-      lsect = (elf_linker_section_t *)
-       bfd_alloc (dynobj, sizeof (elf_linker_section_t));
+      lsect = (elf_linker_section_t *) bfd_alloc (dynobj, amt);
 
       *lsect = *defaults;
       elf_linker_section (dynobj, which) = lsect;
       lsect->which = which;
-      lsect->hole_written_p = false;
+      lsect->hole_written_p = FALSE;
 
       /* See if the sections already exist */
       lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
@@ -431,10 +533,10 @@ _bfd_elf_create_linker_section (abfd, info, which, defaults)
          s->_raw_size += lsect->hole_size;
          if (lsect->hole_offset > lsect->max_hole_offset)
            {
-             (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"),
+             (*_bfd_error_handler) (_("%s: Section %s is too large to add hole of %ld bytes"),
                                     bfd_get_filename (abfd),
                                     lsect->name,
-                                    (long)lsect->hole_size);
+                                    (long) lsect->hole_size);
 
              bfd_set_error (bfd_error_bad_value);
              return (elf_linker_section_t *)0;
@@ -448,29 +550,27 @@ _bfd_elf_create_linker_section (abfd, info, which, defaults)
 
       if (lsect->sym_name)
        {
-         struct elf_link_hash_entry *h = NULL;
+         struct elf_link_hash_entry *h;
+         struct bfd_link_hash_entry *bh;
+
 #ifdef DEBUG
          fprintf (stderr, "Adding %s to section %s\n",
                   lsect->sym_name,
                   lsect->name);
 #endif
-         h = (struct elf_link_hash_entry *)
-           bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false);
-
-         if ((h == NULL || h->root.type == bfd_link_hash_undefined)
-             && !(_bfd_generic_link_add_one_symbol (info,
-                                                    abfd,
-                                                    lsect->sym_name,
-                                                    BSF_GLOBAL,
-                                                    s,
-                                                    ((lsect->hole_size)
-                                                     ? s->_raw_size - lsect->hole_size + lsect->sym_offset
-                                                     : lsect->sym_offset),
-                                                    (const char *) NULL,
-                                                    false,
-                                                    get_elf_backend_data (abfd)->collect,
-                                                    (struct bfd_link_hash_entry **) &h)))
-           return (elf_linker_section_t *)0;
+         bh = bfd_link_hash_lookup (info->hash, lsect->sym_name,
+                                    FALSE, FALSE, FALSE);
+
+         if ((bh == NULL || bh->type == bfd_link_hash_undefined)
+             && !(_bfd_generic_link_add_one_symbol
+                  (info, abfd, lsect->sym_name, BSF_GLOBAL, s,
+                   (lsect->hole_size
+                    ? s->_raw_size - lsect->hole_size + lsect->sym_offset
+                    : lsect->sym_offset),
+                   (const char *) NULL, FALSE,
+                   get_elf_backend_data (abfd)->collect, &bh)))
+           return (elf_linker_section_t *) 0;
+         h = (struct elf_link_hash_entry *) bh;
 
          if ((defaults->which != LINKER_SECTION_SDATA)
              && (defaults->which != LINKER_SECTION_SDATA2))
@@ -481,7 +581,7 @@ _bfd_elf_create_linker_section (abfd, info, which, defaults)
 
          if (info->shared
              && ! _bfd_elf_link_record_dynamic_symbol (info, h))
-           return (elf_linker_section_t *)0;
+           return (elf_linker_section_t *) 0;
        }
     }
 
@@ -506,7 +606,7 @@ _bfd_elf_create_linker_section (abfd, info, which, defaults)
 elf_linker_section_pointers_t *
 _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
      elf_linker_section_pointers_t *linker_pointers;
-     bfd_signed_vma addend;
+     bfd_vma addend;
      elf_linker_section_enum_t which;
 {
   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
@@ -520,14 +620,14 @@ _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
 \f
 /* Make the .rela section corresponding to the generated linker section.  */
 
-boolean
+bfd_boolean
 _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
      bfd *dynobj;
      elf_linker_section_t *lsect;
      int alignment;
 {
   if (lsect->rel_section)
-    return true;
+    return TRUE;
 
   lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
   if (lsect->rel_section == NULL)
@@ -543,8 +643,8 @@ _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
                                       | SEC_LINKER_CREATED
                                       | SEC_READONLY))
          || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
-       return false;
+       return FALSE;
     }
 
-  return true;
+  return TRUE;
 }
This page took 0.042068 seconds and 4 git commands to generate.