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 / reloc16.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  10KB  |  333 lines

  1. /* 8 and 16 bit COFF relocation functions, for BFD.
  2.    Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program 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 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program 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 this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* 
  22. Most of this hacked by  Steve Chamberlain,
  23.             sac@cygnus.com 
  24. */
  25.  
  26. /* These routines are used by coff-h8300 and coff-z8k to do
  27.    relocation.
  28.  
  29.    FIXME: This code should be rewritten to support the new COFF
  30.    linker.  Basically, they need to deal with COFF relocs rather than
  31.    BFD generic relocs.  They should store the relocs in some location
  32.    where coff_link_input_bfd can find them (and coff_link_input_bfd
  33.    should be changed to use this location rather than rereading the
  34.    file) (unless info->keep_memory is false, in which case they should
  35.    free up the relocs after dealing with them).  */
  36.  
  37. #include "bfd.h"
  38. #include "sysdep.h"
  39. #include "obstack.h"
  40. #include "libbfd.h"
  41. #include "bfdlink.h"
  42. #include "genlink.h"
  43. #include "coff/internal.h"
  44. #include "libcoff.h"
  45.  
  46. bfd_vma
  47. bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  48.      arelent *reloc;
  49.      struct bfd_link_info *link_info;
  50.      asection *input_section;
  51. {
  52.   bfd_vma value;
  53.   asymbol *symbol = *(reloc->sym_ptr_ptr);
  54.   /* A symbol holds a pointer to a section, and an offset from the
  55.      base of the section.  To relocate, we find where the section will
  56.      live in the output and add that in */
  57.  
  58.   if (bfd_is_und_section (symbol->section)
  59.       || bfd_is_com_section (symbol->section))
  60.     {
  61.       struct bfd_link_hash_entry *h;
  62.  
  63.       /* The symbol is undefined in this BFD.  Look it up in the
  64.      global linker hash table.  FIXME: This should be changed when
  65.      we convert this stuff to use a specific final_link function
  66.      and change the interface to bfd_relax_section to not require
  67.      the generic symbols.  */
  68.       h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
  69.                     bfd_asymbol_name (symbol),
  70.                     false, false, true);
  71.       if (h != (struct bfd_link_hash_entry *) NULL
  72.       && (h->type == bfd_link_hash_defined
  73.           || h->type == bfd_link_hash_defweak))
  74.     value = (h->u.def.value
  75.          + h->u.def.section->output_section->vma
  76.          + h->u.def.section->output_offset);
  77.       else if (h != (struct bfd_link_hash_entry *) NULL
  78.            && h->type == bfd_link_hash_common)
  79.     value = h->u.c.size;
  80.       else
  81.     {
  82.       if (! ((*link_info->callbacks->undefined_symbol)
  83.          (link_info, bfd_asymbol_name (symbol),
  84.           input_section->owner, input_section, reloc->address)))
  85.         abort ();
  86.       value = 0;
  87.     }
  88.     }
  89.   else 
  90.     {
  91.       value = symbol->value +
  92.     symbol->section->output_offset +
  93.       symbol->section->output_section->vma;
  94.     }
  95.   
  96.   /* Add the value contained in the relocation */
  97.   value += reloc->addend;
  98.   
  99.   return value;
  100. }
  101.  
  102. void
  103. bfd_perform_slip(abfd, slip, input_section, value)
  104.      bfd *abfd;
  105.      unsigned int slip;
  106.      asection *input_section;
  107.      bfd_vma value;
  108. {
  109.   asymbol **s;
  110.  
  111.   s = _bfd_generic_link_get_symbols (abfd);
  112.   BFD_ASSERT (s != (asymbol **) NULL);
  113.  
  114.   /* Find all symbols past this point, and make them know
  115.      what's happened */
  116.   while (*s) 
  117.     {
  118.       asymbol *p = *s;
  119.       if (p->section == input_section) 
  120.     {
  121.       /* This was pointing into this section, so mangle it */
  122.       if (p->value > value)
  123.         {
  124.           p->value -= slip;
  125.           if (p->udata.p != NULL)
  126.         {
  127.           struct generic_link_hash_entry *h;
  128.  
  129.           h = (struct generic_link_hash_entry *) p->udata.p;
  130.           BFD_ASSERT (h->root.type == bfd_link_hash_defined
  131.                   || h->root.type == bfd_link_hash_defweak);
  132.           h->root.u.def.value -= slip;
  133.           BFD_ASSERT (h->root.u.def.value == p->value);
  134.         }
  135.         }
  136.     }
  137.       s++;
  138.     }    
  139. }
  140.  
  141. boolean 
  142. bfd_coff_reloc16_relax_section (abfd, i, link_info, again)
  143.      bfd *abfd;
  144.      asection *i;
  145.      struct bfd_link_info *link_info;
  146.      boolean *again;
  147. {
  148.   /* Get enough memory to hold the stuff */
  149.   bfd *input_bfd = i->owner;
  150.   asection *input_section = i;
  151.   unsigned *shrinks;
  152.   int shrink = 0;
  153.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  154.   arelent **reloc_vector = NULL;
  155.   long reloc_count;
  156.  
  157.   /* We only do global relaxation once.  It is not safe to do it multiple
  158.      times (see discussion of the "shrinks" array below).  */
  159.   *again = false;
  160.  
  161.   if (reloc_size < 0)
  162.     return false;
  163.  
  164.   reloc_vector = (arelent **) bfd_malloc (reloc_size);
  165.   if (!reloc_vector && reloc_size > 0)
  166.     return false;
  167.  
  168.   /* Get the relocs and think about them */
  169.   reloc_count =
  170.     bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
  171.                 _bfd_generic_link_get_symbols (input_bfd));
  172.   if (reloc_count < 0)
  173.     {
  174.       free (reloc_vector);
  175.       return false;
  176.     }
  177.  
  178.   /* The reloc16.c and related relaxing code is very simple, the price
  179.      for that simplicity is we can only call this function once for
  180.      each section.
  181.  
  182.      So, to get the best results within that limitation, we do multiple
  183.      relaxing passes over each section here.  That involves keeping track
  184.      of the "shrink" at each reloc in the section.  This allows us to
  185.      accurately determine the relative location of two relocs within
  186.      this section.
  187.  
  188.      In theory, if we kept the "shrinks" array for each section for the
  189.      entire link, we could use the generic relaxing code in the linker
  190.      and get better results, particularly for jsr->bsr and 24->16 bit
  191.      memory reference relaxations.  */
  192.      
  193.   if (reloc_count > 0)
  194.     {
  195.       int another_pass = 0;
  196.  
  197.       /* Allocate and initialize the shrinks array for this section.  */
  198.       shrinks = (unsigned *)bfd_malloc (reloc_count * sizeof (unsigned));
  199.       memset (shrinks, 0, reloc_count * sizeof (unsigned));
  200.  
  201.       /* Loop until nothing changes in this section.  */
  202.       do {
  203.     arelent **parent;
  204.     unsigned int i, j;
  205.  
  206.     another_pass = 0;
  207.  
  208.     for (i = 0, parent = reloc_vector; *parent; parent++, i++) 
  209.       {
  210.         /* Let the target/machine dependent code examine each reloc
  211.            in this section and attempt to shrink it.  */
  212.         shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
  213.                         shrinks[i], link_info);
  214.  
  215.         /* If it shrunk, note it in the shrinks array and set up for
  216.            another pass.  */
  217.         if (shrink != shrinks[i])
  218.           {
  219.             another_pass = 1;
  220.         for (j = i + 1; j < reloc_count; j++)
  221.           shrinks[j] += shrink - shrinks[i];
  222.           }
  223.       }
  224.   
  225.       } while (another_pass);
  226.     }
  227.  
  228.   input_section->_cooked_size -= shrink;  
  229.   free((char *)reloc_vector);
  230.   free((char *)shrinks);
  231.   return true;
  232. }
  233.  
  234. bfd_byte *
  235. bfd_coff_reloc16_get_relocated_section_contents(in_abfd,
  236.                         link_info,
  237.                         link_order,
  238.                         data,
  239.                         relocateable,
  240.                         symbols)
  241.      bfd *in_abfd;
  242.      struct bfd_link_info *link_info;
  243.      struct bfd_link_order *link_order;
  244.      bfd_byte *data;
  245.      boolean relocateable;
  246.      asymbol **symbols;
  247. {
  248.   /* Get enough memory to hold the stuff */
  249.   bfd *input_bfd = link_order->u.indirect.section->owner;
  250.   asection *input_section = link_order->u.indirect.section;
  251.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  252.   arelent **reloc_vector;
  253.   long reloc_count;
  254.  
  255.   if (reloc_size < 0)
  256.     return NULL;
  257.  
  258.   /* If producing relocateable output, don't bother to relax.  */
  259.   if (relocateable)
  260.     return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
  261.                                link_order,
  262.                                data, relocateable,
  263.                                symbols);
  264.  
  265.   /* read in the section */
  266.   if (! bfd_get_section_contents(input_bfd,
  267.                  input_section,
  268.                  data,
  269.                  0,
  270.                  input_section->_raw_size))
  271.     return NULL;
  272.   
  273.   
  274.   reloc_vector = (arelent **) bfd_malloc((size_t) reloc_size);
  275.   if (!reloc_vector && reloc_size != 0)
  276.     return NULL;
  277.   
  278.   reloc_count = bfd_canonicalize_reloc (input_bfd, 
  279.                     input_section,
  280.                     reloc_vector,
  281.                     symbols);
  282.   if (reloc_count < 0)
  283.     {
  284.       free (reloc_vector);
  285.       return NULL;
  286.     }
  287.     
  288.   if (reloc_count > 0)
  289.     {
  290.       arelent **parent = reloc_vector;
  291.       arelent *reloc ;
  292.       unsigned int dst_address = 0;
  293.       unsigned int src_address = 0;
  294.       unsigned int run;
  295.       unsigned int idx;
  296.     
  297.       /* Find how long a run we can do */
  298.       while (dst_address < link_order->size) 
  299.     {
  300.       reloc = *parent;
  301.       if (reloc) 
  302.         {
  303.           /* Note that the relaxing didn't tie up the addresses in the
  304.          relocation, so we use the original address to work out the
  305.          run of non-relocated data */
  306.           run = reloc->address - src_address;
  307.           parent++;
  308.         }
  309.       else 
  310.         {
  311.           run = link_order->size - dst_address;
  312.         }
  313.       /* Copy the bytes */
  314.       for (idx = 0; idx < run; idx++)
  315.         {
  316.           data[dst_address++] = data[src_address++];
  317.         }
  318.     
  319.       /* Now do the relocation */
  320.     
  321.       if (reloc) 
  322.         {
  323.           bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
  324.                         reloc, data, &src_address,
  325.                         &dst_address);
  326.         }    
  327.     }
  328.     }
  329.   free((char *)reloc_vector);
  330.   return data;
  331. }
  332.  
  333.