Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/bfd/elflink.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* ELF linking support for BFD.
    2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
     2   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
    33   Free Software Foundation, Inc.
    44
     
    2626#include "elf-bfd.h"
    2727
    28 boolean
     28bfd_boolean
    2929_bfd_elf_create_got_section (abfd, info)
    3030     bfd *abfd;
     
    3232{
    3333  flagword flags;
    34   register asection *s;
     34  asection *s;
    3535  struct elf_link_hash_entry *h;
     36  struct bfd_link_hash_entry *bh;
    3637  struct elf_backend_data *bed = get_elf_backend_data (abfd);
    3738  int ptralign;
    3839
    3940  /* This function may be called more than once.  */
    40   if (bfd_get_section_by_name (abfd, ".got") != NULL)
    41     return true;
     41  s = bfd_get_section_by_name (abfd, ".got");
     42  if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
     43    return TRUE;
    4244
    4345  switch (bed->s->arch_size)
     
    5355    default:
    5456      bfd_set_error (bfd_error_bad_value);
    55       return false;
     57      return FALSE;
    5658    }
    5759
     
    6365      || !bfd_set_section_flags (abfd, s, flags)
    6466      || !bfd_set_section_alignment (abfd, s, ptralign))
    65     return false;
     67    return FALSE;
    6668
    6769  if (bed->want_got_plt)
     
    7173          || !bfd_set_section_flags (abfd, s, flags)
    7274          || !bfd_set_section_alignment (abfd, s, ptralign))
    73         return false;
    74     }
    75 
    76   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    77      (or .got.plt) section.  We don't do this in the linker script
    78      because we don't want to define the symbol if we are not creating
    79      a global offset table.  */
    80   h = NULL;
    81   if (!(_bfd_generic_link_add_one_symbol
    82         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
    83          bed->got_symbol_offset, (const char *) NULL, false,
    84          bed->collect, (struct bfd_link_hash_entry **) &h)))
    85     return false;
    86   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
    87   h->type = STT_OBJECT;
    88 
    89   if (info->shared
    90       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
    91     return false;
    92 
    93   elf_hash_table (info)->hgot = h;
     75        return FALSE;
     76    }
     77
     78  if (bed->want_got_sym)
     79    {
     80      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
     81         (or .got.plt) section.  We don't do this in the linker script
     82         because we don't want to define the symbol if we are not creating
     83         a global offset table.  */
     84      bh = NULL;
     85      if (!(_bfd_generic_link_add_one_symbol
     86            (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
     87             bed->got_symbol_offset, (const char *) NULL, FALSE,
     88             bed->collect, &bh)))
     89        return FALSE;
     90      h = (struct elf_link_hash_entry *) bh;
     91      h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
     92      h->type = STT_OBJECT;
     93
     94      if (info->shared
     95          && ! _bfd_elf_link_record_dynamic_symbol (info, h))
     96        return FALSE;
     97
     98      elf_hash_table (info)->hgot = h;
     99    }
    94100
    95101  /* The first bit of the global offset table is the header.  */
    96102  s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
    97103
    98   return true;
     104  return TRUE;
    99105}
    100106
     
    102108/* Create dynamic sections when linking against a dynamic object.  */
    103109
    104 boolean
     110bfd_boolean
    105111_bfd_elf_create_dynamic_sections (abfd, info)
    106112     bfd *abfd;
     
    108114{
    109115  flagword flags, pltflags;
    110   register asection *s;
     116  asection *s;
    111117  struct elf_backend_data *bed = get_elf_backend_data (abfd);
    112118  int ptralign;
     
    124130    default:
    125131      bfd_set_error (bfd_error_bad_value);
    126       return false;
     132      return FALSE;
    127133    }
    128134
     
    136142  pltflags |= SEC_CODE;
    137143  if (bed->plt_not_loaded)
    138     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
     144    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
    139145  if (bed->plt_readonly)
    140146    pltflags |= SEC_READONLY;
     
    144150      || ! bfd_set_section_flags (abfd, s, pltflags)
    145151      || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    146     return false;
     152    return FALSE;
    147153
    148154  if (bed->want_plt_sym)
     
    150156      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    151157         .plt section.  */
    152       struct elf_link_hash_entry *h = NULL;
     158      struct elf_link_hash_entry *h;
     159      struct bfd_link_hash_entry *bh = NULL;
     160
    153161      if (! (_bfd_generic_link_add_one_symbol
    154162             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
    155               (bfd_vma) 0, (const char *) NULL, false,
    156               get_elf_backend_data (abfd)->collect,
    157               (struct bfd_link_hash_entry **) &h)))
    158         return false;
     163              (bfd_vma) 0, (const char *) NULL, FALSE,
     164              get_elf_backend_data (abfd)->collect, &bh)))
     165        return FALSE;
     166      h = (struct elf_link_hash_entry *) bh;
    159167      h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
    160168      h->type = STT_OBJECT;
     
    162170      if (info->shared
    163171          && ! _bfd_elf_link_record_dynamic_symbol (info, h))
    164         return false;
     172        return FALSE;
    165173    }
    166174
     
    170178      || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
    171179      || ! bfd_set_section_alignment (abfd, s, ptralign))
    172     return false;
     180    return FALSE;
    173181
    174182  if (! _bfd_elf_create_got_section (abfd, info))
    175     return false;
     183    return FALSE;
    176184
    177185  if (bed->want_dynbss)
     
    186194      if (s == NULL
    187195          || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
    188         return false;
     196        return FALSE;
    189197
    190198      /* The .rel[a].bss section holds copy relocs.  This section is not
     
    207215              || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
    208216              || ! bfd_set_section_alignment (abfd, s, ptralign))
    209             return false;
    210         }
    211     }
    212 
    213   return true;
     217            return FALSE;
     218        }
     219    }
     220
     221  return TRUE;
    214222}
    215223
     
    223231   one.  */
    224232
    225 boolean
     233bfd_boolean
    226234_bfd_elf_link_record_dynamic_symbol (info, h)
    227235     struct bfd_link_info *info;
     
    230238  if (h->dynindx == -1)
    231239    {
    232       struct bfd_strtab_hash *dynstr;
     240      struct elf_strtab_hash *dynstr;
    233241      char *p, *alc;
    234242      const char *name;
    235       boolean copy;
     243      bfd_boolean copy;
    236244      bfd_size_type indx;
    237245
     
    248256            {
    249257              h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
    250               return true;
     258              return TRUE;
    251259            }
    252260
     
    262270        {
    263271          /* Create a strtab to hold the dynamic symbol names.  */
    264           elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
     272          elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
    265273          if (dynstr == NULL)
    266             return false;
     274            return FALSE;
    267275        }
    268276
    269277      /* We don't put any version information in the dynamic string
    270         table.  */
     278        table.  */
    271279      name = h->root.root.string;
    272280      p = strchr (name, ELF_VER_CHR);
     
    274282        {
    275283          alc = NULL;
    276           copy = false;
     284          copy = FALSE;
    277285        }
    278286      else
    279287        {
    280           alc = bfd_malloc (p - name + 1);
     288          size_t len = p - name + 1;
     289
     290          alc = bfd_malloc ((bfd_size_type) len);
    281291          if (alc == NULL)
    282             return false;
    283           strncpy (alc, name, p - name);
    284           alc[p - name] = '\0';
     292            return FALSE;
     293          memcpy (alc, name, len - 1);
     294          alc[len - 1] = '\0';
    285295          name = alc;
    286           copy = true;
    287         }
    288 
    289       indx = _bfd_stringtab_add (dynstr, name, true, copy);
     296          copy = TRUE;
     297        }
     298
     299      indx = _bfd_elf_strtab_add (dynstr, name, copy);
    290300
    291301      if (alc != NULL)
     
    293303
    294304      if (indx == (bfd_size_type) -1)
    295         return false;
     305        return FALSE;
    296306      h->dynstr_index = indx;
    297307    }
    298308
    299   return true;
     309  return TRUE;
     310}
     311
     312/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
     313   success, and 2 on a failure caused by attempting to record a symbol
     314   in a discarded section, eg. a discarded link-once section symbol.  */
     315
     316int
     317elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
     318     struct bfd_link_info *info;
     319     bfd *input_bfd;
     320     long input_indx;
     321{
     322  bfd_size_type amt;
     323  struct elf_link_local_dynamic_entry *entry;
     324  struct elf_link_hash_table *eht;
     325  struct elf_strtab_hash *dynstr;
     326  unsigned long dynstr_index;
     327  char *name;
     328  Elf_External_Sym_Shndx eshndx;
     329  char esym[sizeof (Elf64_External_Sym)];
     330
     331  if (! is_elf_hash_table (info))
     332    return 0;
     333
     334  /* See if the entry exists already.  */
     335  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
     336    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
     337      return 1;
     338
     339  amt = sizeof (*entry);
     340  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
     341  if (entry == NULL)
     342    return 0;
     343
     344  /* Go find the symbol, so that we can find it's name.  */
     345  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
     346                             (size_t) 1, (size_t) input_indx,
     347                             &entry->isym, esym, &eshndx))
     348    {
     349      bfd_release (input_bfd, entry);
     350      return 0;
     351    }
     352
     353  if (entry->isym.st_shndx != SHN_UNDEF
     354      && (entry->isym.st_shndx < SHN_LORESERVE
     355          || entry->isym.st_shndx > SHN_HIRESERVE))
     356    {
     357      asection *s;
     358
     359      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
     360      if (s == NULL || bfd_is_abs_section (s->output_section))
     361        {
     362          /* We can still bfd_release here as nothing has done another
     363             bfd_alloc.  We can't do this later in this function.  */
     364          bfd_release (input_bfd, entry);
     365          return 2;
     366        }
     367    }
     368
     369  name = (bfd_elf_string_from_elf_section
     370          (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
     371           entry->isym.st_name));
     372
     373  dynstr = elf_hash_table (info)->dynstr;
     374  if (dynstr == NULL)
     375    {
     376      /* Create a strtab to hold the dynamic symbol names.  */
     377      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
     378      if (dynstr == NULL)
     379        return 0;
     380    }
     381
     382  dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
     383  if (dynstr_index == (unsigned long) -1)
     384    return 0;
     385  entry->isym.st_name = dynstr_index;
     386
     387  eht = elf_hash_table (info);
     388
     389  entry->next = eht->dynlocal;
     390  eht->dynlocal = entry;
     391  entry->input_bfd = input_bfd;
     392  entry->input_indx = input_indx;
     393  eht->dynsymcount++;
     394
     395  /* Whatever binding the symbol had before, it's now local.  */
     396  entry->isym.st_info
     397    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
     398
     399  /* The dynindx will be set at the end of size_dynamic_sections.  */
     400
     401  return 1;
    300402}
    301403
     
    320422   via elf_link_hash_traverse.  */
    321423
    322 static boolean elf_link_renumber_hash_table_dynsyms
     424static bfd_boolean elf_link_renumber_hash_table_dynsyms
    323425  PARAMS ((struct elf_link_hash_entry *, PTR));
    324426
    325 static boolean
     427static bfd_boolean
    326428elf_link_renumber_hash_table_dynsyms (h, data)
    327429     struct elf_link_hash_entry *h;
     
    330432  size_t *count = (size_t *) data;
    331433
     434  if (h->root.type == bfd_link_hash_warning)
     435    h = (struct elf_link_hash_entry *) h->root.u.i.link;
     436
    332437  if (h->dynindx != -1)
    333438    h->dynindx = ++(*count);
    334439
    335   return true;
     440  return TRUE;
    336441}
    337442
     
    352457      asection *p;
    353458      for (p = output_bfd->sections; p ; p = p->next)
    354         elf_section_data (p)->dynindx = ++dynsymcount;
     459        if ((p->flags & SEC_EXCLUDE) == 0)
     460          elf_section_data (p)->dynindx = ++dynsymcount;
    355461    }
    356462
     
    398504    {
    399505      asection *s;
    400 
    401       lsect = (elf_linker_section_t *)
    402         bfd_alloc (dynobj, sizeof (elf_linker_section_t));
     506      bfd_size_type amt = sizeof (elf_linker_section_t);
     507
     508      lsect = (elf_linker_section_t *) bfd_alloc (dynobj, amt);
    403509
    404510      *lsect = *defaults;
    405511      elf_linker_section (dynobj, which) = lsect;
    406512      lsect->which = which;
    407       lsect->hole_written_p = false;
     513      lsect->hole_written_p = FALSE;
    408514
    409515      /* See if the sections already exist */
     
    424530      s->_raw_size = align_power (s->_raw_size, lsect->alignment);
    425531
    426       /* Is there a hole we have to provide?  If so check whether the segment is
    427          too big already */
     532      /* Is there a hole we have to provide?  If so check whether the
     533         segment is too big already */
    428534      if (lsect->hole_size)
    429535        {
     
    432538          if (lsect->hole_offset > lsect->max_hole_offset)
    433539            {
    434               (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"),
    435                                      bfd_get_filename (abfd),
    436                                      lsect->name,
    437                                      (long)lsect->hole_size);
     540              (*_bfd_error_handler)
     541                (_("%s: Section %s is too large to add hole of %ld bytes"),
     542                 bfd_get_filename (abfd),
     543                 lsect->name,
     544                 (long) lsect->hole_size);
    438545
    439546              bfd_set_error (bfd_error_bad_value);
     
    449556      if (lsect->sym_name)
    450557        {
    451           struct elf_link_hash_entry *h = NULL;
     558          struct elf_link_hash_entry *h;
     559          struct bfd_link_hash_entry *bh;
     560
    452561#ifdef DEBUG
    453562          fprintf (stderr, "Adding %s to section %s\n",
     
    455564                   lsect->name);
    456565#endif
    457           h = (struct elf_link_hash_entry *)
    458             bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false);
    459 
    460           if ((h == NULL || h->root.type == bfd_link_hash_undefined)
    461               && !(_bfd_generic_link_add_one_symbol (info,
    462                                                      abfd,
    463                                                      lsect->sym_name,
    464                                                      BSF_GLOBAL,
    465                                                      s,
    466                                                      ((lsect->hole_size)
    467                                                       ? s->_raw_size - lsect->hole_size + lsect->sym_offset
    468                                                       : lsect->sym_offset),
    469                                                      (const char *) NULL,
    470                                                      false,
    471                                                      get_elf_backend_data (abfd)->collect,
    472                                                      (struct bfd_link_hash_entry **) &h)))
    473             return (elf_linker_section_t *)0;
     566          bh = bfd_link_hash_lookup (info->hash, lsect->sym_name,
     567                                     FALSE, FALSE, FALSE);
     568
     569          if ((bh == NULL || bh->type == bfd_link_hash_undefined)
     570              && !(_bfd_generic_link_add_one_symbol
     571                   (info, abfd, lsect->sym_name, BSF_GLOBAL, s,
     572                    (lsect->hole_size
     573                     ? s->_raw_size - lsect->hole_size + lsect->sym_offset
     574                     : lsect->sym_offset),
     575                    (const char *) NULL, FALSE,
     576                    get_elf_backend_data (abfd)->collect, &bh)))
     577            return (elf_linker_section_t *) 0;
     578          h = (struct elf_link_hash_entry *) bh;
    474579
    475580          if ((defaults->which != LINKER_SECTION_SDATA)
     
    482587          if (info->shared
    483588              && ! _bfd_elf_link_record_dynamic_symbol (info, h))
    484             return (elf_linker_section_t *)0;
     589            return (elf_linker_section_t *) 0;
    485590        }
    486591    }
     
    508613_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
    509614     elf_linker_section_pointers_t *linker_pointers;
    510      bfd_signed_vma addend;
     615     bfd_vma addend;
    511616     elf_linker_section_enum_t which;
    512617{
     
    523628/* Make the .rela section corresponding to the generated linker section.  */
    524629
    525 boolean
     630bfd_boolean
    526631_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
    527632     bfd *dynobj;
     
    530635{
    531636  if (lsect->rel_section)
    532     return true;
     637    return TRUE;
    533638
    534639  lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
     
    546651                                       | SEC_READONLY))
    547652          || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
    548         return false;
    549     }
    550 
    551   return true;
    552 }
     653        return FALSE;
     654    }
     655
     656  return TRUE;
     657}
Note: See TracChangeset for help on using the changeset viewer.