home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / reloc16.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  6KB  |  220 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 "coff/internal.h"
  35. #include "libcoff.h"
  36.  
  37. bfd_vma
  38. bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  39.      arelent *reloc;
  40.      struct bfd_link_info *link_info;
  41.      asection *input_section;
  42. {
  43.   bfd_vma value;
  44.   asymbol *symbol = *(reloc->sym_ptr_ptr);
  45.   /* A symbol holds a pointer to a section, and an offset from the
  46.      base of the section.  To relocate, we find where the section will
  47.      live in the output and add that in */
  48.  
  49.   if (symbol->section == &bfd_und_section)
  50.     {
  51.       /* Ouch, this is an undefined symbol.. */
  52.       if (! ((*link_info->callbacks->undefined_symbol)
  53.          (link_info, bfd_asymbol_name (symbol),
  54.           input_section->owner, input_section, reloc->address)))
  55.     abort ();
  56.       value = symbol->value;
  57.     }
  58.   else 
  59.     {
  60.       value = symbol->value +
  61.     symbol->section->output_offset +
  62.       symbol->section->output_section->vma;
  63.     }
  64.   
  65.   /* Add the value contained in the relocation */
  66.   value += reloc->addend;
  67.   
  68.   return value;
  69. }
  70.  
  71. void
  72. bfd_perform_slip(s, slip, input_section, value)
  73.      asymbol **s;
  74.      unsigned int slip;
  75.      asection *input_section;
  76.      bfd_vma value;
  77. {
  78.   /* Find all symbols past this point, and make them know
  79.      what's happened */
  80.   while (*s) 
  81.     {
  82.       asymbol *p = *s;
  83.       if (p->section == input_section) 
  84.     {
  85.       /* This was pointing into this section, so mangle it */
  86.       if (p->value > value)
  87.         {
  88.           p->value -= slip;
  89.         }
  90.     }
  91.       s++;
  92.     }    
  93. }
  94.  
  95. boolean 
  96. bfd_coff_reloc16_relax_section (abfd, i, link_info, symbols)
  97.      bfd *abfd;
  98.      asection *i;
  99.      struct bfd_link_info *link_info;
  100.      asymbol **symbols;
  101. {
  102.   /* Get enough memory to hold the stuff */
  103.   bfd *input_bfd = i->owner;
  104.   asection *input_section = i;
  105.   int shrink = 0 ;
  106.   boolean new = false;
  107.   
  108.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
  109.                                input_section);
  110.   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
  111.  
  112.   /* Get the relocs and think about them */
  113.   if (bfd_canonicalize_reloc(input_bfd, 
  114.                  input_section,
  115.                  reloc_vector,
  116.                  symbols))
  117.     {
  118.       arelent **parent;
  119.       for (parent = reloc_vector; *parent; parent++) 
  120.     {
  121.       shrink = bfd_coff_reloc16_estimate (abfd, input_section, symbols,
  122.                           *parent, shrink, link_info);
  123.     }
  124.     }
  125.  
  126.   input_section->_cooked_size -= shrink;  
  127.   free((char *)reloc_vector);
  128.   return new;
  129. }
  130.  
  131. bfd_byte *
  132. bfd_coff_reloc16_get_relocated_section_contents(in_abfd,
  133.                         link_info,
  134.                         link_order,
  135.                         data,
  136.                         relocateable,
  137.                         symbols)
  138.      bfd *in_abfd;
  139.      struct bfd_link_info *link_info;
  140.      struct bfd_link_order *link_order;
  141.      bfd_byte *data;
  142.      boolean relocateable;
  143.      asymbol **symbols;
  144. {
  145.   /* Get enough memory to hold the stuff */
  146.   bfd *input_bfd = link_order->u.indirect.section->owner;
  147.   asection *input_section = link_order->u.indirect.section;
  148.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
  149.                                input_section);
  150.   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
  151.   
  152.   /* If producing relocateable output, don't bother to relax.  */
  153.   if (relocateable)
  154.     return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
  155.                                link_order,
  156.                                data, relocateable,
  157.                                symbols);
  158.  
  159.   /* read in the section */
  160.   bfd_get_section_contents(input_bfd,
  161.                input_section,
  162.                data,
  163.                0,
  164.                input_section->_raw_size);
  165.   
  166.   
  167.   if (bfd_canonicalize_reloc(input_bfd, 
  168.                  input_section,
  169.                  reloc_vector,
  170.                  symbols) )
  171.     {
  172.       arelent **parent = reloc_vector;
  173.       arelent *reloc ;
  174.     
  175.  
  176.  
  177.       unsigned int dst_address = 0;
  178.       unsigned int src_address = 0;
  179.       unsigned int run;
  180.       unsigned int idx;
  181.     
  182.       /* Find how long a run we can do */
  183.       while (dst_address < link_order->size) 
  184.     {
  185.       
  186.       reloc = *parent;
  187.       if (reloc) 
  188.         {
  189.           /* Note that the relaxing didn't tie up the addresses in the
  190.          relocation, so we use the original address to work out the
  191.          run of non-relocated data */
  192.           run = reloc->address - src_address;
  193.           parent++;
  194.     
  195.         }
  196.       else 
  197.         {
  198.           run = link_order->size - dst_address;
  199.         }
  200.       /* Copy the bytes */
  201.       for (idx = 0; idx < run; idx++)
  202.         {
  203.           data[dst_address++] = data[src_address++];
  204.         }
  205.     
  206.       /* Now do the relocation */
  207.     
  208.       if (reloc) 
  209.         {
  210.           bfd_coff_reloc16_extra_cases (in_abfd, link_info, link_order,
  211.                         reloc, data, &src_address,
  212.                         &dst_address);
  213.         }    
  214.     }
  215.     }
  216.   free((char *)reloc_vector);
  217.   return data;
  218. }
  219.  
  220.