OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [elf-ifunc.c] - Diff between revs 14 and 163

Only display areas with differences | Details | Blame | View Log

Rev 14 Rev 163
/* ELF STT_GNU_IFUNC support.
/* ELF STT_GNU_IFUNC support.
   Copyright 2009
   Copyright 2009
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
   This file is part of BFD, the Binary File Descriptor library.
   This file is part of BFD, the Binary File Descriptor library.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
   MA 02110-1301, USA.  */
 
 
#include "sysdep.h"
#include "sysdep.h"
#include "bfd.h"
#include "bfd.h"
#include "bfdlink.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "libbfd.h"
#define ARCH_SIZE 0
#define ARCH_SIZE 0
#include "elf-bfd.h"
#include "elf-bfd.h"
#include "safe-ctype.h"
#include "safe-ctype.h"
#include "libiberty.h"
#include "libiberty.h"
#include "objalloc.h"
#include "objalloc.h"
 
 
/* Create sections needed by STT_GNU_IFUNC symbol.  */
/* Create sections needed by STT_GNU_IFUNC symbol.  */
 
 
bfd_boolean
bfd_boolean
_bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
_bfd_elf_create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
{
{
  flagword flags, pltflags;
  flagword flags, pltflags;
  asection *s;
  asection *s;
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  struct elf_link_hash_table *htab = elf_hash_table (info);
  struct elf_link_hash_table *htab = elf_hash_table (info);
 
 
  if (htab->irelifunc != NULL || htab->iplt != NULL)
  if (htab->irelifunc != NULL || htab->iplt != NULL)
    return TRUE;
    return TRUE;
 
 
  flags = bed->dynamic_sec_flags;
  flags = bed->dynamic_sec_flags;
  pltflags = flags;
  pltflags = flags;
  if (bed->plt_not_loaded)
  if (bed->plt_not_loaded)
    /* We do not clear SEC_ALLOC here because we still want the OS to
    /* We do not clear SEC_ALLOC here because we still want the OS to
       allocate space for the section; it's just that there's nothing
       allocate space for the section; it's just that there's nothing
       to read in from the object file.  */
       to read in from the object file.  */
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  else
  else
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
  if (bed->plt_readonly)
  if (bed->plt_readonly)
    pltflags |= SEC_READONLY;
    pltflags |= SEC_READONLY;
 
 
  if (info->shared)
  if (info->shared)
    {
    {
      /* We need to create .rel[a].ifunc for shared objects.  */
      /* We need to create .rel[a].ifunc for shared objects.  */
      const char *rel_sec = (bed->rela_plts_and_copies_p
      const char *rel_sec = (bed->rela_plts_and_copies_p
                             ? ".rela.ifunc" : ".rel.ifunc");
                             ? ".rela.ifunc" : ".rel.ifunc");
 
 
      s = bfd_make_section_with_flags (abfd, rel_sec,
      s = bfd_make_section_with_flags (abfd, rel_sec,
                                       flags | SEC_READONLY);
                                       flags | SEC_READONLY);
      if (s == NULL
      if (s == NULL
          || ! bfd_set_section_alignment (abfd, s,
          || ! bfd_set_section_alignment (abfd, s,
                                          bed->s->log_file_align))
                                          bed->s->log_file_align))
        return FALSE;
        return FALSE;
      htab->irelifunc = s;
      htab->irelifunc = s;
    }
    }
  else
  else
    {
    {
      /* We need to create .iplt, .rel[a].iplt, .igot and .igot.plt
      /* We need to create .iplt, .rel[a].iplt, .igot and .igot.plt
         for static executables.   */
         for static executables.   */
      s = bfd_make_section_with_flags (abfd, ".iplt", pltflags);
      s = bfd_make_section_with_flags (abfd, ".iplt", pltflags);
      if (s == NULL
      if (s == NULL
          || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
          || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
        return FALSE;
        return FALSE;
      htab->iplt = s;
      htab->iplt = s;
 
 
      s = bfd_make_section_with_flags (abfd,
      s = bfd_make_section_with_flags (abfd,
                                       (bed->rela_plts_and_copies_p
                                       (bed->rela_plts_and_copies_p
                                        ? ".rela.iplt" : ".rel.iplt"),
                                        ? ".rela.iplt" : ".rel.iplt"),
                                       flags | SEC_READONLY);
                                       flags | SEC_READONLY);
      if (s == NULL
      if (s == NULL
          || ! bfd_set_section_alignment (abfd, s,
          || ! bfd_set_section_alignment (abfd, s,
                                          bed->s->log_file_align))
                                          bed->s->log_file_align))
        return FALSE;
        return FALSE;
      htab->irelplt = s;
      htab->irelplt = s;
 
 
      /* We don't need the .igot section if we have the .igot.plt
      /* We don't need the .igot section if we have the .igot.plt
         section.  */
         section.  */
      if (bed->want_got_plt)
      if (bed->want_got_plt)
        s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
        s = bfd_make_section_with_flags (abfd, ".igot.plt", flags);
      else
      else
        s = bfd_make_section_with_flags (abfd, ".igot", flags);
        s = bfd_make_section_with_flags (abfd, ".igot", flags);
      if (s == NULL
      if (s == NULL
          || !bfd_set_section_alignment (abfd, s,
          || !bfd_set_section_alignment (abfd, s,
                                         bed->s->log_file_align))
                                         bed->s->log_file_align))
        return FALSE;
        return FALSE;
      htab->igotplt = s;
      htab->igotplt = s;
    }
    }
 
 
  return TRUE;
  return TRUE;
}
}
 
 
/* For a STT_GNU_IFUNC symbol, create a dynamic reloc section, SRELOC,
/* For a STT_GNU_IFUNC symbol, create a dynamic reloc section, SRELOC,
   for the input section, SEC, and append this reloc to HEAD.  */
   for the input section, SEC, and append this reloc to HEAD.  */
 
 
asection *
asection *
_bfd_elf_create_ifunc_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
_bfd_elf_create_ifunc_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
                                 asection *sec, asection *sreloc,
                                 asection *sec, asection *sreloc,
                                 struct elf_dyn_relocs **head)
                                 struct elf_dyn_relocs **head)
{
{
  struct elf_dyn_relocs *p;
  struct elf_dyn_relocs *p;
  struct elf_link_hash_table *htab = elf_hash_table (info);
  struct elf_link_hash_table *htab = elf_hash_table (info);
 
 
  if (sreloc == NULL)
  if (sreloc == NULL)
    {
    {
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
 
      if (htab->dynobj == NULL)
      if (htab->dynobj == NULL)
        htab->dynobj = abfd;
        htab->dynobj = abfd;
 
 
      sreloc = _bfd_elf_make_dynamic_reloc_section (sec, htab->dynobj,
      sreloc = _bfd_elf_make_dynamic_reloc_section (sec, htab->dynobj,
                                                    bed->s->log_file_align,
                                                    bed->s->log_file_align,
                                                    abfd,
                                                    abfd,
                                                    bed->rela_plts_and_copies_p);
                                                    bed->rela_plts_and_copies_p);
      if (sreloc == NULL)
      if (sreloc == NULL)
        return NULL;
        return NULL;
    }
    }
 
 
  p = *head;
  p = *head;
  if (p == NULL || p->sec != sec)
  if (p == NULL || p->sec != sec)
    {
    {
      bfd_size_type amt = sizeof *p;
      bfd_size_type amt = sizeof *p;
 
 
      p = ((struct elf_dyn_relocs *) bfd_alloc (htab->dynobj, amt));
      p = ((struct elf_dyn_relocs *) bfd_alloc (htab->dynobj, amt));
      if (p == NULL)
      if (p == NULL)
        return NULL;
        return NULL;
      p->next = *head;
      p->next = *head;
      *head = p;
      *head = p;
      p->sec = sec;
      p->sec = sec;
      p->count = 0;
      p->count = 0;
      p->pc_count = 0;
      p->pc_count = 0;
    }
    }
  p->count += 1;
  p->count += 1;
 
 
  return sreloc;
  return sreloc;
}
}
 
 
/* Allocate space in .plt, .got and associated reloc sections for
/* Allocate space in .plt, .got and associated reloc sections for
   dynamic relocs against a STT_GNU_IFUNC symbol definition.  */
   dynamic relocs against a STT_GNU_IFUNC symbol definition.  */
 
 
bfd_boolean
bfd_boolean
_bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
_bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
                                    struct elf_link_hash_entry *h,
                                    struct elf_link_hash_entry *h,
                                    struct elf_dyn_relocs **head,
                                    struct elf_dyn_relocs **head,
                                    unsigned int plt_entry_size,
                                    unsigned int plt_entry_size,
                                    unsigned int got_entry_size)
                                    unsigned int got_entry_size)
{
{
  asection *plt, *gotplt, *relplt;
  asection *plt, *gotplt, *relplt;
  struct elf_dyn_relocs *p;
  struct elf_dyn_relocs *p;
  unsigned int sizeof_reloc;
  unsigned int sizeof_reloc;
  const struct elf_backend_data *bed;
  const struct elf_backend_data *bed;
  struct elf_link_hash_table *htab;
  struct elf_link_hash_table *htab;
 
 
  /* When a shared library references a STT_GNU_IFUNC symbol defined
  /* When a shared library references a STT_GNU_IFUNC symbol defined
     in executable, the address of the resolved function may be used.
     in executable, the address of the resolved function may be used.
     But in non-shared executable, the address of its .plt slot may
     But in non-shared executable, the address of its .plt slot may
     be used.  Pointer equality may not work correctly.  PIE should
     be used.  Pointer equality may not work correctly.  PIE should
     be used if pointer equality is required here.  */
     be used if pointer equality is required here.  */
  if (!info->shared
  if (!info->shared
      && (h->dynindx != -1
      && (h->dynindx != -1
          || info->export_dynamic)
          || info->export_dynamic)
      && h->pointer_equality_needed)
      && h->pointer_equality_needed)
    {
    {
      info->callbacks->einfo
      info->callbacks->einfo
        (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
        (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
           "equality in `%B' can not be used when making an "
           "equality in `%B' can not be used when making an "
           "executable; recompile with -fPIE and relink with -pie\n"),
           "executable; recompile with -fPIE and relink with -pie\n"),
         h->root.root.string,
         h->root.root.string,
         h->root.u.def.section->owner);
         h->root.u.def.section->owner);
      bfd_set_error (bfd_error_bad_value);
      bfd_set_error (bfd_error_bad_value);
      return FALSE;
      return FALSE;
    }
    }
 
 
  htab = elf_hash_table (info);
  htab = elf_hash_table (info);
 
 
  /* Support garbage collection against STT_GNU_IFUNC symbols.  */
  /* Support garbage collection against STT_GNU_IFUNC symbols.  */
  if (h->plt.refcount <= 0 && h->got.refcount <= 0)
  if (h->plt.refcount <= 0 && h->got.refcount <= 0)
    {
    {
      /* When building shared library, we need to handle the case
      /* When building shared library, we need to handle the case
         where it is marked with regular reference, but not non-GOT
         where it is marked with regular reference, but not non-GOT
         reference.  It may happen if we didn't see STT_GNU_IFUNC
         reference.  It may happen if we didn't see STT_GNU_IFUNC
         symbol at the time when checking relocations.  */
         symbol at the time when checking relocations.  */
      if (info->shared
      if (info->shared
          && !h->non_got_ref
          && !h->non_got_ref
          && h->ref_regular)
          && h->ref_regular)
        for (p = *head; p != NULL; p = p->next)
        for (p = *head; p != NULL; p = p->next)
          if (p->count)
          if (p->count)
            {
            {
              h->non_got_ref = 1;
              h->non_got_ref = 1;
              goto keep;
              goto keep;
            }
            }
 
 
      h->got = htab->init_got_offset;
      h->got = htab->init_got_offset;
      h->plt = htab->init_plt_offset;
      h->plt = htab->init_plt_offset;
      *head = NULL;
      *head = NULL;
      return TRUE;
      return TRUE;
    }
    }
 
 
  /* Return and discard space for dynamic relocations against it if
  /* Return and discard space for dynamic relocations against it if
     it is never referenced in a non-shared object.  */
     it is never referenced in a non-shared object.  */
  if (!h->ref_regular)
  if (!h->ref_regular)
    {
    {
      if (h->plt.refcount > 0
      if (h->plt.refcount > 0
          || h->got.refcount > 0)
          || h->got.refcount > 0)
        abort ();
        abort ();
      h->got = htab->init_got_offset;
      h->got = htab->init_got_offset;
      h->plt = htab->init_plt_offset;
      h->plt = htab->init_plt_offset;
      *head = NULL;
      *head = NULL;
      return TRUE;
      return TRUE;
    }
    }
 
 
keep:
keep:
  bed = get_elf_backend_data (info->output_bfd);
  bed = get_elf_backend_data (info->output_bfd);
  if (bed->rela_plts_and_copies_p)
  if (bed->rela_plts_and_copies_p)
    sizeof_reloc = bed->s->sizeof_rela;
    sizeof_reloc = bed->s->sizeof_rela;
  else
  else
    sizeof_reloc = bed->s->sizeof_rel;
    sizeof_reloc = bed->s->sizeof_rel;
 
 
  /* When building a static executable, use .iplt, .igot.plt and
  /* When building a static executable, use .iplt, .igot.plt and
     .rel[a].iplt sections for STT_GNU_IFUNC symbols.  */
     .rel[a].iplt sections for STT_GNU_IFUNC symbols.  */
  if (htab->splt != NULL)
  if (htab->splt != NULL)
    {
    {
      plt = htab->splt;
      plt = htab->splt;
      gotplt = htab->sgotplt;
      gotplt = htab->sgotplt;
      relplt = htab->srelplt;
      relplt = htab->srelplt;
 
 
      /* If this is the first .plt entry, make room for the special
      /* If this is the first .plt entry, make room for the special
         first entry.  */
         first entry.  */
      if (plt->size == 0)
      if (plt->size == 0)
        plt->size += plt_entry_size;
        plt->size += plt_entry_size;
    }
    }
  else
  else
    {
    {
      plt = htab->iplt;
      plt = htab->iplt;
      gotplt = htab->igotplt;
      gotplt = htab->igotplt;
      relplt = htab->irelplt;
      relplt = htab->irelplt;
    }
    }
 
 
  /* Don't update value of STT_GNU_IFUNC symbol to PLT.  We need
  /* Don't update value of STT_GNU_IFUNC symbol to PLT.  We need
     the original value for R_*_IRELATIVE.  */
     the original value for R_*_IRELATIVE.  */
  h->plt.offset = plt->size;
  h->plt.offset = plt->size;
 
 
  /* Make room for this entry in the .plt/.iplt section.  */
  /* Make room for this entry in the .plt/.iplt section.  */
  plt->size += plt_entry_size;
  plt->size += plt_entry_size;
 
 
  /* We also need to make an entry in the .got.plt/.got.iplt section,
  /* We also need to make an entry in the .got.plt/.got.iplt section,
     which will be placed in the .got section by the linker script.  */
     which will be placed in the .got section by the linker script.  */
  gotplt->size += got_entry_size;
  gotplt->size += got_entry_size;
 
 
  /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
  /* We also need to make an entry in the .rel[a].plt/.rel[a].iplt
     section.  */
     section.  */
  relplt->size += sizeof_reloc;
  relplt->size += sizeof_reloc;
  relplt->reloc_count++;
  relplt->reloc_count++;
 
 
  /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
  /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
     there is a non-GOT reference in a shared object.  */
     there is a non-GOT reference in a shared object.  */
  if (!info->shared
  if (!info->shared
      || !h->non_got_ref)
      || !h->non_got_ref)
    *head = NULL;
    *head = NULL;
 
 
  /* Finally, allocate space.  */
  /* Finally, allocate space.  */
  p = *head;
  p = *head;
  if (p != NULL)
  if (p != NULL)
    {
    {
      bfd_size_type count = 0;
      bfd_size_type count = 0;
      do
      do
        {
        {
          count += p->count;
          count += p->count;
          p = p->next;
          p = p->next;
        }
        }
      while (p != NULL);
      while (p != NULL);
      htab->irelifunc->size += count * sizeof_reloc;
      htab->irelifunc->size += count * sizeof_reloc;
    }
    }
 
 
  /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
  /* For STT_GNU_IFUNC symbol, .got.plt has the real function address
     and .got has the PLT entry adddress.  We will load the GOT entry
     and .got has the PLT entry adddress.  We will load the GOT entry
     with the PLT entry in finish_dynamic_symbol if it is used.  For
     with the PLT entry in finish_dynamic_symbol if it is used.  For
     branch, it uses .got.plt.  For symbol value,
     branch, it uses .got.plt.  For symbol value,
     1. Use .got.plt in a shared object if it is forced local or not
     1. Use .got.plt in a shared object if it is forced local or not
     dynamic.
     dynamic.
     2. Use .got.plt in a non-shared object if pointer equality isn't
     2. Use .got.plt in a non-shared object if pointer equality isn't
     needed.
     needed.
     3. Use .got.plt in PIE.
     3. Use .got.plt in PIE.
     4. Use .got.plt if .got isn't used.
     4. Use .got.plt if .got isn't used.
     5. Otherwise use .got so that it can be shared among different
     5. Otherwise use .got so that it can be shared among different
     objects at run-time.
     objects at run-time.
     We only need to relocate .got entry in shared object.  */
     We only need to relocate .got entry in shared object.  */
  if ((info->shared
  if (h->got.refcount <= 0
 
      || (info->shared
       && (h->dynindx == -1
       && (h->dynindx == -1
           || h->forced_local))
              || h->forced_local))
      || (!info->shared
      || (!info->shared
          && !h->pointer_equality_needed)
          && !h->pointer_equality_needed)
      || (info->executable && info->shared)
      || (info->executable && info->shared)
      || htab->sgot == NULL)
      || htab->sgot == NULL)
    {
    {
      /* Use .got.plt.  */
      /* Use .got.plt.  */
      h->got.offset = (bfd_vma) -1;
      h->got.offset = (bfd_vma) -1;
    }
    }
  else
  else
    {
    {
      h->got.offset = htab->sgot->size;
      h->got.offset = htab->sgot->size;
      htab->sgot->size += got_entry_size;
      htab->sgot->size += got_entry_size;
      if (info->shared)
      if (info->shared)
        htab->srelgot->size += sizeof_reloc;
        htab->srelgot->size += sizeof_reloc;
    }
    }
 
 
  return TRUE;
  return TRUE;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.