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

  1. /* ldctor.c -- constructor support routines
  2.    Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    By Steve Chamberlain <sac@cygnus.com>
  4.    
  5. This file is part of GLD, the Gnu Linker.
  6.  
  7. GLD is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GLD is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GLD; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "bfdlink.h"
  24.  
  25. #include "ld.h"
  26. #include "ldexp.h"
  27. #include "ldlang.h"
  28. #include "ldmisc.h"
  29. #include "ldgram.h"
  30. #include "ldmain.h"
  31. #include "ldctor.h"
  32.  
  33. /* The list of statements needed to handle constructors.  These are
  34.    invoked by the command CONSTRUCTORS in the linker script.  */
  35. lang_statement_list_type constructor_list;
  36.  
  37. /* The sets we have seen.  */
  38. struct set_info *sets;
  39.  
  40. /* Add an entry to a set.  H is the entry in the linker hash table.
  41.    RELOC is the relocation to use for an entry in the set.  SECTION
  42.    and VALUE are the value to add.  This is called during the first
  43.    phase of the link, when we are still gathering symbols together.
  44.    We just record the information now.  The ldctor_find_constructors
  45.    function will construct the sets.  */
  46.  
  47. void
  48. ldctor_add_set_entry (h, reloc, name, section, value)
  49.      struct bfd_link_hash_entry *h;
  50.      bfd_reloc_code_real_type reloc;
  51.      const char *name;
  52.      asection *section;
  53.      bfd_vma value;
  54. {
  55.   struct set_info *p;
  56.   struct set_element *e;
  57.   struct set_element **epp;
  58.  
  59.   for (p = sets; p != (struct set_info *) NULL; p = p->next)
  60.     if (p->h == h)
  61.       break;
  62.  
  63.   if (p == (struct set_info *) NULL)
  64.     {
  65.       p = (struct set_info *) xmalloc (sizeof (struct set_info));
  66.       p->next = sets;
  67.       sets = p;
  68.       p->h = h;
  69.       p->reloc = reloc;
  70.       p->count = 0;
  71.       p->elements = NULL;
  72.     }
  73.   else
  74.     {
  75.       if (p->reloc != reloc)
  76.     {
  77.       einfo ("%P%X: Different relocs used in set %s\n", h->root.string);
  78.       return;
  79.     }
  80.  
  81.       /* Don't permit a set to be constructed from different object
  82.          file formats.  The same reloc may have different results.  We
  83.          actually could sometimes handle this, but the case is
  84.          unlikely to ever arise.  Sometimes constructor symbols are in
  85.          unusual sections, such as the absolute section--this appears
  86.          to be the case in Linux a.out--and in such cases we just
  87.          assume everything is OK.  */
  88.       if (p->elements != NULL
  89.       && section->owner != NULL
  90.       && p->elements->section->owner != NULL
  91.       && strcmp (bfd_get_target (section->owner),
  92.              bfd_get_target (p->elements->section->owner)) != 0)
  93.     {
  94.       einfo ("%P%X: Different object file formats composing set %s\n",
  95.          h->root.string);
  96.       return;
  97.     }
  98.     }
  99.  
  100.   e = (struct set_element *) xmalloc (sizeof (struct set_element));
  101.   e->next = NULL;
  102.   e->name = name;
  103.   e->section = section;
  104.   e->value = value;
  105.  
  106.   for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
  107.     ;
  108.   *epp = e;
  109.  
  110.   ++p->count;
  111. }
  112.  
  113. /* This function is called after the first phase of the link and
  114.    before the second phase.  At this point all set information has
  115.    been gathered.  We now put the statements to build the sets
  116.    themselves into constructor_list.  */
  117.  
  118. void
  119. ldctor_build_sets ()
  120. {
  121.   static boolean called;
  122.   lang_statement_list_type *old;
  123.   boolean header_printed;
  124.   struct set_info *p;
  125.  
  126.   /* The emulation code may call us directly, but we only want to do
  127.      this once.  */
  128.   if (called)
  129.     return;
  130.   called = true;
  131.  
  132.   old = stat_ptr;
  133.   stat_ptr = &constructor_list;
  134.  
  135.   lang_list_init (stat_ptr);
  136.  
  137.   header_printed = false;
  138.   for (p = sets; p != (struct set_info *) NULL; p = p->next)
  139.     {
  140.       struct set_element *e;
  141.       reloc_howto_type *howto;
  142.       int size;
  143.  
  144.       /* If the symbol is defined, we may have been invoked from
  145.      collect, and the sets may already have been built, so we do
  146.      not do anything.  */
  147.       /* dgv -- libnix v1.1 uses absolute sets that are also explicitly
  148.      defined in the library so that the sets need to be build even
  149.      if the symbol is defined */
  150.       if ((output_bfd->xvec->flavour != bfd_target_amiga_flavour) &&
  151.       (p->h->type == bfd_link_hash_defined
  152.       || p->h->type == bfd_link_hash_defweak))
  153.     continue;
  154.       /* For each set we build:
  155.        set:
  156.          .long number_of_elements
  157.          .long element0
  158.          ...
  159.          .long elementN
  160.          .long 0
  161.      except that we use the right size instead of .long.  When
  162.      generating relocateable output, we generate relocs instead of
  163.      addresses.  */
  164.       howto = bfd_reloc_type_lookup (output_bfd, p->reloc);
  165.       if (howto == (reloc_howto_type *) NULL)
  166.     {
  167.       if (link_info.relocateable)
  168.         {
  169.           einfo ("%P%X: %s does not support reloc %s for set %s\n",
  170.              bfd_get_target (output_bfd),
  171.              bfd_get_reloc_code_name (p->reloc),
  172.              p->h->root.string);
  173.           continue;
  174.         }
  175.  
  176.       /* If this is not a relocateable link, all we need is the
  177.          size, which we can get from the input BFD.  */
  178.       howto = bfd_reloc_type_lookup (p->elements->section->owner,
  179.                      p->reloc);
  180.       if (howto == NULL)
  181.         {
  182.           einfo ("%P%X: %s does not support reloc %s for set %s\n",
  183.              bfd_get_target (p->elements->section->owner),
  184.              bfd_get_reloc_code_name (p->reloc),
  185.              p->h->root.string);
  186.           continue;
  187.         }
  188.     }
  189.  
  190.       switch (bfd_get_reloc_size (howto))
  191.     {
  192.     case 1: size = BYTE; break;
  193.     case 2: size = SHORT; break;
  194.     case 4: size = LONG; break;
  195.     case 8: size = QUAD; break;
  196.     default:
  197.       einfo ("%P%X: Unsupported size %d for set %s\n",
  198.          bfd_get_reloc_size (howto), p->h->root.string);
  199.       size = LONG;
  200.       break;
  201.     }
  202.  
  203.       lang_add_assignment (exp_assop ('=', p->h->root.string,
  204.                       exp_nameop (NAME, ".")));
  205.       lang_add_data (size, exp_intop ((bfd_vma) p->count));
  206.  
  207.       for (e = p->elements; e != (struct set_element *) NULL; e = e->next)
  208.     {
  209.       if (config.map_file != NULL)
  210.         {
  211.           int len;
  212.  
  213.           if (! header_printed)
  214.         {
  215.           minfo ("\nSet                 Symbol\n\n");
  216.           header_printed = true;
  217.         }
  218.  
  219.           minfo ("%s", p->h->root.string);
  220.           len = strlen (p->h->root.string);
  221.  
  222.           if (len >= 19)
  223.         {
  224.           print_nl ();
  225.           len = 0;
  226.         }
  227.           while (len < 20)
  228.         {
  229.           print_space ();
  230.           ++len;
  231.         }
  232.  
  233.           if (e->name != NULL)
  234.         minfo ("%T\n", e->name);
  235.           else
  236.         minfo ("%G\n", e->section->owner, e->section, e->value);
  237.         }
  238.  
  239.       /* dgv -- on the amiga, we want the constructors to be relocateable
  240.          objects. However, this should be arranged somewhere else (FIXME) */
  241.       if (link_info.relocateable ||
  242.           (output_bfd->xvec->flavour == bfd_target_amiga_flavour &&
  243.            e->section != bfd_abs_section_ptr))
  244.         lang_add_reloc (p->reloc, howto, e->section, e->name,
  245.                 exp_intop (e->value));
  246.       else
  247.         lang_add_data (size, exp_relop (e->section, e->value));
  248.     }
  249.  
  250.       lang_add_data (size, exp_intop (0));
  251.     }
  252.  
  253.   stat_ptr = old;
  254. }
  255.