home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / reloc16.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-21  |  7.3 KB  |  285 lines

  1. /* 8 and 16 bit COFF relocation functions, for BFD.
  2.    Copyright 1990, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, 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. #include "bfd.h"
  30. #include "sysdep.h"
  31. #include "obstack.h"
  32. #include "libbfd.h"
  33. #include "bfdlink.h"
  34. #include "genlink.h"
  35. #include "coff/internal.h"
  36. #include "libcoff.h"
  37.  
  38. bfd_vma
  39. bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  40.      arelent *reloc;
  41.      struct bfd_link_info *link_info;
  42.      asection *input_section;
  43. {
  44.   bfd_vma value;
  45.   asymbol *symbol = *(reloc->sym_ptr_ptr);
  46.   /* A symbol holds a pointer to a section, and an offset from the
  47.      base of the section.  To relocate, we find where the section will
  48.      live in the output and add that in */
  49.  
  50.   if (bfd_is_und_section (symbol->section))
  51.     {
  52.       struct bfd_link_hash_entry *h;
  53.  
  54.       /* The symbol is undefined in this BFD.  Look it up in the
  55.      global linker hash table.  FIXME: This should be changed when
  56.      we convert this stuff to use a specific final_link function
  57.      and change the interface to bfd_relax_section to not require
  58.      the generic symbols.  */
  59.       h = bfd_link_hash_lookup (link_info->hash, bfd_asymbol_name (symbol),
  60.                 false, false, true);
  61.       if (h != (struct bfd_link_hash_entry *) NULL
  62.       && h->type == bfd_link_hash_defined)
  63.     value = (h->u.def.value
  64.          + h->u.def.section->output_section->vma
  65.          + h->u.def.section->output_offset);
  66.       else if (h != (struct bfd_link_hash_entry *) NULL
  67.            && h->type == bfd_link_hash_common)
  68.     value = h->u.c.size;
  69.       else
  70.     {
  71.       if (! ((*link_info->callbacks->undefined_symbol)
  72.          (link_info, bfd_asymbol_name (symbol),
  73.           input_section->owner, input_section, reloc->address)))
  74.         abort ();
  75.       value = 0;
  76.     }
  77.     }
  78.   else 
  79.     {
  80.       value = symbol->value +
  81.     symbol->section->output_offset +
  82.       symbol->section->output_section->vma;
  83.     }
  84.   
  85.   /* Add the value contained in the relocation */
  86.   value += reloc->addend;
  87.   
  88.   return value;
  89. }
  90.  
  91. void
  92. bfd_perform_slip(abfd, slip, input_section, value)
  93.      bfd *abfd;
  94.      unsigned int slip;
  95.      asection *input_section;
  96.      bfd_vma value;
  97. {
  98.   asymbol **s;
  99.  
  100.   s = _bfd_generic_link_get_symbols (abfd);
  101.   BFD_ASSERT (s != (asymbol **) NULL);
  102.  
  103.   /* Find all symbols past this point, and make them know
  104.      what's happened */
  105.   while (*s) 
  106.     {
  107.       asymbol *p = *s;
  108.       if (p->section == input_section) 
  109.     {
  110.       /* This was pointing into this section, so mangle it */
  111.       if (p->value > value)
  112.         {
  113.           p->value -= slip;
  114.           if (p->udata != NULL)
  115.         {
  116.           struct generic_link_hash_entry *h;
  117.  
  118.           h = (struct generic_link_hash_entry *) p->udata;
  119.           BFD_ASSERT (h->root.type == bfd_link_hash_defined);
  120.           h->root.u.def.value -= slip;
  121.           BFD_ASSERT (h->root.u.def.value == p->value);
  122.         }
  123.         }
  124.     }
  125.       s++;
  126.     }    
  127. }
  128.  
  129. boolean 
  130. bfd_coff_reloc16_relax_section (abfd, i, link_info, again)
  131.      bfd *abfd;
  132.      asection *i;
  133.      struct bfd_link_info *link_info;
  134.      boolean *again;
  135. {
  136.   /* Get enough memory to hold the stuff */
  137.   bfd *input_bfd = i->owner;
  138.   asection *input_section = i;
  139.   int shrink = 0 ;
  140.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  141.   arelent **reloc_vector = NULL;
  142.   long reloc_count;
  143.  
  144.   /* We only run this relaxation once.  It might work to run it more
  145.      often, but it hasn't been tested.  */
  146.   *again = false;
  147.  
  148.   if (reloc_size < 0)
  149.     return false;
  150.  
  151.   reloc_vector = (arelent **) malloc (reloc_size);
  152.   if (!reloc_vector && reloc_size > 0)
  153.     {
  154.       bfd_set_error (bfd_error_no_memory);
  155.       return false;
  156.     }
  157.  
  158.   /* Get the relocs and think about them */
  159.   reloc_count =
  160.     bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
  161.                 _bfd_generic_link_get_symbols (input_bfd));
  162.   if (reloc_count < 0)
  163.     {
  164.       free (reloc_vector);
  165.       return false;
  166.     }
  167.  
  168.   if (reloc_count > 0)
  169.     {
  170.       arelent **parent;
  171.       for (parent = reloc_vector; *parent; parent++) 
  172.     {
  173.       shrink = bfd_coff_reloc16_estimate (abfd, input_section,
  174.                           *parent, shrink, link_info);
  175.     }
  176.     }
  177.  
  178.   input_section->_cooked_size -= shrink;  
  179.   free((char *)reloc_vector);
  180.   return true;
  181. }
  182.  
  183. bfd_byte *
  184. bfd_coff_reloc16_get_relocated_section_contents(in_abfd,
  185.                         link_info,
  186.                         link_order,
  187.                         data,
  188.                         relocateable,
  189.                         symbols)
  190.      bfd *in_abfd;
  191.      struct bfd_link_info *link_info;
  192.      struct bfd_link_order *link_order;
  193.      bfd_byte *data;
  194.      boolean relocateable;
  195.      asymbol **symbols;
  196. {
  197.   /* Get enough memory to hold the stuff */
  198.   bfd *input_bfd = link_order->u.indirect.section->owner;
  199.   asection *input_section = link_order->u.indirect.section;
  200.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  201.   arelent **reloc_vector;
  202.   long reloc_count;
  203.  
  204.   if (reloc_size < 0)
  205.     return NULL;
  206.  
  207.   /* If producing relocateable output, don't bother to relax.  */
  208.   if (relocateable)
  209.     return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
  210.                                link_order,
  211.                                data, relocateable,
  212.                                symbols);
  213.  
  214.   /* read in the section */
  215.   if (! bfd_get_section_contents(input_bfd,
  216.                  input_section,
  217.                  data,
  218.                  0,
  219.                  input_section->_raw_size))
  220.     return NULL;
  221.   
  222.   
  223.   reloc_vector = (arelent **)malloc((size_t) reloc_size);
  224.   if (!reloc_vector && reloc_size != 0)
  225.     {
  226.       bfd_set_error (bfd_error_no_memory);
  227.       return NULL;
  228.     }
  229.   
  230.   reloc_count = bfd_canonicalize_reloc (input_bfd, 
  231.                     input_section,
  232.                     reloc_vector,
  233.                     symbols);
  234.   if (reloc_count < 0)
  235.     {
  236.       free (reloc_vector);
  237.       return NULL;
  238.     }
  239.     
  240.   if (reloc_count > 0)
  241.     {
  242.       arelent **parent = reloc_vector;
  243.       arelent *reloc ;
  244.       unsigned int dst_address = 0;
  245.       unsigned int src_address = 0;
  246.       unsigned int run;
  247.       unsigned int idx;
  248.     
  249.       /* Find how long a run we can do */
  250.       while (dst_address < link_order->size) 
  251.     {
  252.       reloc = *parent;
  253.       if (reloc) 
  254.         {
  255.           /* Note that the relaxing didn't tie up the addresses in the
  256.          relocation, so we use the original address to work out the
  257.          run of non-relocated data */
  258.           run = reloc->address - src_address;
  259.           parent++;
  260.         }
  261.       else 
  262.         {
  263.           run = link_order->size - dst_address;
  264.         }
  265.       /* Copy the bytes */
  266.       for (idx = 0; idx < run; idx++)
  267.         {
  268.           data[dst_address++] = data[src_address++];
  269.         }
  270.     
  271.       /* Now do the relocation */
  272.     
  273.       if (reloc) 
  274.         {
  275.           bfd_coff_reloc16_extra_cases (in_abfd, link_info, link_order,
  276.                         reloc, data, &src_address,
  277.                         &dst_address);
  278.         }    
  279.     }
  280.     }
  281.   free((char *)reloc_vector);
  282.   return data;
  283. }
  284.  
  285.