home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / elflink.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  12KB  |  378 lines

  1. /* ELF linking support for BFD.
  2.    Copyright 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #define ARCH_SIZE 0
  25. #include "elf-bfd.h"
  26.  
  27. boolean
  28. _bfd_elf_create_got_section (abfd, info)
  29.      bfd *abfd;
  30.      struct bfd_link_info *info;
  31. {
  32.   flagword flags;
  33.   register asection *s;
  34.   struct elf_link_hash_entry *h;
  35.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  36.  
  37.   /* This function may be called more than once.  */
  38.   if (bfd_get_section_by_name (abfd, ".got") != NULL)
  39.     return true;
  40.  
  41.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  42.  
  43.   s = bfd_make_section (abfd, ".got");
  44.   if (s == NULL
  45.       || !bfd_set_section_flags (abfd, s, flags)
  46.       || !bfd_set_section_alignment (abfd, s, 2))
  47.     return false;
  48.  
  49.   if (bed->want_got_plt)
  50.     {
  51.       s = bfd_make_section (abfd, ".got.plt");
  52.       if (s == NULL
  53.       || !bfd_set_section_flags (abfd, s, flags)
  54.       || !bfd_set_section_alignment (abfd, s, 2))
  55.     return false;
  56.     }
  57.  
  58.   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  59.      (or .got.plt) section.  We don't do this in the linker script
  60.      because we don't want to define the symbol if we are not creating
  61.      a global offset table.  */
  62.   h = NULL;
  63.   if (!(_bfd_generic_link_add_one_symbol
  64.     (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  65.      (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  66.      (struct bfd_link_hash_entry **) &h)))
  67.     return false;
  68.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  69.   h->type = STT_OBJECT;
  70.  
  71.   if (info->shared
  72.       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
  73.     return false;
  74.  
  75.   elf_hash_table (info)->hgot = h;
  76.  
  77.   /* The first three global offset table entries are reserved.  */
  78.   s->_raw_size += 3 * 4;
  79.  
  80.   return true;
  81. }
  82.  
  83.  
  84. /* Create dynamic sections when linking against a dynamic object.  */
  85.  
  86. boolean
  87. _bfd_elf_create_dynamic_sections (abfd, info)
  88.      bfd *abfd;
  89.      struct bfd_link_info *info;
  90. {
  91.   flagword flags;
  92.   register asection *s;
  93.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  94.  
  95.   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  96.      .rel[a].bss sections.  */
  97.  
  98.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  99.  
  100.   s = bfd_make_section (abfd, ".plt");
  101.   if (s == NULL
  102.       || ! bfd_set_section_flags (abfd, s,
  103.                   (flags | SEC_CODE
  104.                    | (bed->plt_readonly ? SEC_READONLY : 0)))
  105.       || ! bfd_set_section_alignment (abfd, s, 2))
  106.     return false;
  107.  
  108.   if (bed->want_plt_sym)
  109.     {
  110.       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  111.      .plt section.  */
  112.       struct elf_link_hash_entry *h = NULL;
  113.       if (! (_bfd_generic_link_add_one_symbol
  114.          (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
  115.           (bfd_vma) 0, (const char *) NULL, false,
  116.           get_elf_backend_data (abfd)->collect,
  117.           (struct bfd_link_hash_entry **) &h)))
  118.     return false;
  119.       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  120.       h->type = STT_OBJECT;
  121.  
  122.       if (info->shared
  123.       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
  124.     return false;
  125.     }
  126.  
  127.   s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.plt" : ".rel.plt");
  128.   if (s == NULL
  129.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  130.       || ! bfd_set_section_alignment (abfd, s, 2))
  131.     return false;
  132.  
  133.   if (! _bfd_elf_create_got_section (abfd, info))
  134.     return false;
  135.  
  136.   /* The .dynbss section is a place to put symbols which are defined
  137.      by dynamic objects, are referenced by regular objects, and are
  138.      not functions.  We must allocate space for them in the process
  139.      image and use a R_*_COPY reloc to tell the dynamic linker to
  140.      initialize them at run time.  The linker script puts the .dynbss
  141.      section into the .bss section of the final image.  */
  142.   s = bfd_make_section (abfd, ".dynbss");
  143.   if (s == NULL
  144.       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
  145.     return false;
  146.  
  147.   /* The .rel[a].bss section holds copy relocs.  This section is not
  148.      normally needed.  We need to create it here, though, so that the
  149.      linker will map it to an output section.  We can't just create it
  150.      only if we need it, because we will not know whether we need it
  151.      until we have seen all the input files, and the first time the
  152.      main linker code calls BFD after examining all the input files
  153.      (size_dynamic_sections) the input sections have already been
  154.      mapped to the output sections.  If the section turns out not to
  155.      be needed, we can discard it later.  We will never need this
  156.      section when generating a shared object, since they do not use
  157.      copy relocs.  */
  158.   if (! info->shared)
  159.     {
  160.       s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.bss" : ".rel.bss");
  161.       if (s == NULL
  162.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  163.       || ! bfd_set_section_alignment (abfd, s, 2))
  164.     return false;
  165.     }
  166.  
  167.   return true;
  168. }
  169.  
  170.  
  171. /* Record a new dynamic symbol.  We record the dynamic symbols as we
  172.    read the input files, since we need to have a list of all of them
  173.    before we can determine the final sizes of the output sections.
  174.    Note that we may actually call this function even though we are not
  175.    going to output any dynamic symbols; in some cases we know that a
  176.    symbol should be in the dynamic symbol table, but only if there is
  177.    one.  */
  178.  
  179. boolean
  180. _bfd_elf_link_record_dynamic_symbol (info, h)
  181.      struct bfd_link_info *info;
  182.      struct elf_link_hash_entry *h;
  183. {
  184.   if (h->dynindx == -1)
  185.     {
  186.       struct bfd_strtab_hash *dynstr;
  187.  
  188.       h->dynindx = elf_hash_table (info)->dynsymcount;
  189.       ++elf_hash_table (info)->dynsymcount;
  190.  
  191.       dynstr = elf_hash_table (info)->dynstr;
  192.       if (dynstr == NULL)
  193.     {
  194.       /* Create a strtab to hold the dynamic symbol names.  */
  195.       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
  196.       if (dynstr == NULL)
  197.         return false;
  198.     }
  199.  
  200.       h->dynstr_index = ((unsigned long)
  201.              _bfd_stringtab_add (dynstr, h->root.root.string,
  202.                          true, false));
  203.       if (h->dynstr_index == (unsigned long) -1)
  204.     return false;
  205.     }
  206.  
  207.   return true;
  208. }
  209.  
  210. /* Create a special linker section, or return a pointer to a linker section already created  */
  211.  
  212. elf_linker_section_t *
  213. _bfd_elf_create_linker_section (abfd, info, which, defaults)
  214.      bfd *abfd;
  215.      struct bfd_link_info *info;
  216.      enum elf_linker_section_enum which;
  217.      elf_linker_section_t *defaults;
  218. {
  219.   bfd *dynobj = elf_hash_table (info)->dynobj;
  220.   elf_linker_section_t *lsect;
  221.  
  222.   /* Record the first bfd section that needs the special section */
  223.   if (!dynobj)
  224.     dynobj = elf_hash_table (info)->dynobj = abfd;
  225.  
  226.   /* If this is the first time, create the section */
  227.   lsect = elf_linker_section (dynobj, which);
  228.   if (!lsect)
  229.     {
  230.       asection *s;
  231.  
  232.       lsect = (elf_linker_section_t *)
  233.     bfd_alloc (dynobj, sizeof (elf_linker_section_t));
  234.  
  235.       *lsect = *defaults;
  236.       elf_linker_section (dynobj, which) = lsect;
  237.       lsect->which = which;
  238.       lsect->hole_written_p = false;
  239.  
  240.       /* See if the sections already exist */
  241.       lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
  242.       if (!s)
  243.     {
  244.       lsect->section = s = bfd_make_section (dynobj, lsect->name);
  245.  
  246.       if (s == NULL)
  247.         return (elf_linker_section_t *)0;
  248.  
  249.       bfd_set_section_flags (dynobj, s, defaults->flags);
  250.       bfd_set_section_alignment (dynobj, s, lsect->alignment);
  251.     }
  252.       else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
  253.     bfd_set_section_alignment (dynobj, s, lsect->alignment);
  254.  
  255.       s->_raw_size = align_power (s->_raw_size, lsect->alignment);
  256.  
  257.       /* Is there a hole we have to provide?  If so check whether the segment is
  258.      too big already */
  259.       if (lsect->hole_size)
  260.     {
  261.       lsect->hole_offset = s->_raw_size;
  262.       s->_raw_size += lsect->hole_size;
  263.       if (lsect->hole_offset > lsect->max_hole_offset)
  264.         {
  265.           (*_bfd_error_handler) ("%s: Section %s is already to large to put hole of %ld bytes in",
  266.                      bfd_get_filename (abfd),
  267.                      lsect->name,
  268.                      (long)lsect->hole_size);
  269.  
  270.           bfd_set_error (bfd_error_bad_value);
  271.           return (elf_linker_section_t *)0;
  272.         }
  273.     }
  274.  
  275. #ifdef DEBUG
  276.       fprintf (stderr, "Creating section %s, current size = %ld\n",
  277.            lsect->name, (long)s->_raw_size);
  278. #endif
  279.  
  280.       if (lsect->sym_name)
  281.     {
  282.       struct elf_link_hash_entry *h = NULL;
  283. #ifdef DEBUG
  284.       fprintf (stderr, "Adding %s to section %s\n",
  285.            lsect->sym_name,
  286.            lsect->name);
  287. #endif
  288.       h = (struct elf_link_hash_entry *)
  289.         bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false);
  290.  
  291.       if ((h == NULL || h->root.type == bfd_link_hash_undefined)
  292.           && !(_bfd_generic_link_add_one_symbol (info,
  293.                              abfd,
  294.                              lsect->sym_name,
  295.                              BSF_GLOBAL,
  296.                              s,
  297.                              ((lsect->hole_size)
  298.                               ? s->_raw_size - lsect->hole_size + lsect->sym_offset
  299.                               : lsect->sym_offset),
  300.                              (const char *) NULL,
  301.                              false,
  302.                              get_elf_backend_data (abfd)->collect,
  303.                              (struct bfd_link_hash_entry **) &h)))
  304.         return (elf_linker_section_t *)0;
  305.  
  306.       if ((defaults->which != LINKER_SECTION_SDATA)
  307.           && (defaults->which != LINKER_SECTION_SDATA2))
  308.         h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
  309.  
  310.       h->type = STT_OBJECT;
  311.       lsect->sym_hash = h;
  312.  
  313.       if (info->shared
  314.           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
  315.         return (elf_linker_section_t *)0;
  316.     }
  317.     }
  318.  
  319.   /* Find the related sections if they have been created */
  320.   if (lsect->bss_name && !lsect->bss_section)
  321.     lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
  322.  
  323.   if (lsect->rel_name && !lsect->rel_section)
  324.     lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
  325.  
  326.   return lsect;
  327. }
  328.  
  329.  
  330. /* Find a linker generated pointer with a given addend and type.  */
  331.  
  332. elf_linker_section_pointers_t *
  333. _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
  334.      elf_linker_section_pointers_t *linker_pointers;
  335.      bfd_signed_vma addend;
  336.      elf_linker_section_enum_t which;
  337. {
  338.   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
  339.     {
  340.       if (which == linker_pointers->which && addend == linker_pointers->addend)
  341.     return linker_pointers;
  342.     }
  343.  
  344.   return (elf_linker_section_pointers_t *)0;
  345. }
  346.  
  347.  
  348. /* Make the .rela section corresponding to the generated linker section.  */
  349.  
  350. boolean
  351. _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
  352.      bfd *dynobj;
  353.      elf_linker_section_t *lsect;
  354.      int alignment;
  355. {
  356.   if (lsect->rel_section)
  357.     return true;
  358.  
  359.   lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
  360.   if (lsect->rel_section == NULL)
  361.     {
  362.       lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
  363.       if (lsect->rel_section == NULL
  364.       || ! bfd_set_section_flags (dynobj,
  365.                       lsect->rel_section,
  366.                       (SEC_ALLOC
  367.                        | SEC_LOAD
  368.                        | SEC_HAS_CONTENTS
  369.                        | SEC_IN_MEMORY
  370.                        | SEC_READONLY))
  371.       || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
  372.     return false;
  373.     }
  374.  
  375.   return true;
  376. }
  377.  
  378.