home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / coff-h85.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  8.4 KB  |  318 lines

  1. /* BFD back-end for Hitachi H8/500 COFF binaries.
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.    Written by Steve Chamberlain, <sac@cygnus.com>.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "libbfd.h"
  25. #include "obstack.h"
  26. #include "coff/h8500.h"
  27. #include "coff/internal.h"
  28. #include "libcoff.h"
  29. #include "seclet.h"
  30.  
  31. extern bfd_error_vector_type bfd_error_vector;
  32.  
  33. static reloc_howto_type r_imm8 =
  34. HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0, true,
  35.        true, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
  36.  
  37. static reloc_howto_type r_imm16 =
  38. HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0, true,
  39.        true, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false);
  40.  
  41. static reloc_howto_type r_imm24 =
  42. HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0, true,
  43.        true, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false);
  44.  
  45. static reloc_howto_type r_imm32 =
  46. HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0, true,
  47.        true, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
  48.  
  49.  
  50. static reloc_howto_type r_high8 =
  51. HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0, true,
  52.        true, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
  53.  
  54. static reloc_howto_type r_low16 =
  55. HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0, true,
  56.        true, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
  57.  
  58. static reloc_howto_type r_pcrel8 =
  59. HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, true, true, 0, "r_pcrel8", true, 0, 0, true);
  60.  
  61.  
  62. static reloc_howto_type r_pcrel16 =
  63. HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, true, true, 0, "r_pcrel16", true, 0, 0, true);
  64.  
  65.  
  66.  
  67. /* Turn a howto into a reloc number */
  68.  
  69. static int 
  70. coff_h8500_select_reloc (howto)
  71.      reloc_howto_type *howto;
  72. {
  73.   return howto->type;
  74. }
  75.  
  76. #define SELECT_RELOC(x,howto) x= coff_h8500_select_reloc(howto)
  77.  
  78.  
  79. #define BADMAG(x) H8500BADMAG(x)
  80. #define H8500 1            /* Customize coffcode.h */
  81.  
  82. #define __A_MAGIC_SET__
  83.  
  84. /* Code to swap in the reloc */
  85. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  86. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  87. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  88.   dst->r_stuff[0] = 'S'; \
  89.   dst->r_stuff[1] = 'C';
  90.  
  91. /* Code to turn a r_type into a howto ptr, uses the above howto table
  92.    */
  93.  
  94. static void
  95. DEFUN (rtype2howto, (internal, dst),
  96.        arelent * internal AND
  97.        struct internal_reloc *dst)
  98. {
  99.   switch (dst->r_type)
  100.     {
  101.       default:
  102.       printf ("BAD 0x%x\n", dst->r_type);
  103.     case R_H8500_IMM8:
  104.       internal->howto = &r_imm8;
  105.       break;
  106.     case R_H8500_IMM16:
  107.       internal->howto = &r_imm16;
  108.       break;
  109.     case R_H8500_IMM24:
  110.       internal->howto = &r_imm24;
  111.       break;
  112.     case R_H8500_IMM32:
  113.       internal->howto = &r_imm32;
  114.       break;
  115.     case R_H8500_PCREL8:
  116.       internal->howto = &r_pcrel8;
  117.       break;
  118.     case R_H8500_PCREL16:
  119.       internal->howto = &r_pcrel16;
  120.       break;
  121.     case R_H8500_HIGH8:
  122.       internal->howto = &r_high8;
  123.       break;
  124.     case R_H8500_LOW16:
  125.       internal->howto = &r_low16;
  126.       break;
  127.     }
  128. }
  129.  
  130. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  131.  
  132.  
  133. /* Perform any necessaru magic to the addend in a reloc entry */
  134.  
  135.  
  136. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  137.  cache_ptr->addend =  ext_reloc.r_offset;
  138.  
  139.  
  140. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  141.  reloc_processing(relent, reloc, symbols, abfd, section)
  142.  
  143. static void 
  144. DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
  145.        arelent * relent AND
  146.        struct internal_reloc *reloc AND
  147.        asymbol ** symbols AND
  148.        bfd * abfd AND
  149.        asection * section)
  150. {
  151.   relent->address = reloc->r_vaddr;
  152.   rtype2howto (relent, reloc);
  153.  
  154.   if (reloc->r_symndx > 0)
  155.     {
  156.       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  157.     }
  158.   else
  159.     {
  160.       relent->sym_ptr_ptr = &(bfd_abs_symbol);
  161.     }
  162.  
  163.  
  164.   relent->addend = reloc->r_offset;
  165.   relent->address -= section->vma;
  166. }
  167.  
  168. static void
  169. extra_case (in_abfd, seclet, reloc, data, src_ptr, dst_ptr)
  170.      bfd *in_abfd;
  171.      bfd_seclet_type *seclet;
  172.      arelent *reloc;
  173.      bfd_byte *data;
  174.      unsigned int *src_ptr;
  175.      unsigned int *dst_ptr;
  176. {
  177. bfd_byte *d = data+*dst_ptr;
  178.   switch (reloc->howto->type)
  179.     {
  180.     case R_H8500_IMM8:
  181.       bfd_put_8 (in_abfd,
  182.          bfd_coff_reloc16_get_value (reloc, seclet),
  183.          d);
  184.       (*dst_ptr) += 1;
  185.       (*src_ptr) += 1;
  186.       break;
  187.  
  188.     case R_H8500_HIGH8:
  189.       bfd_put_8 (in_abfd,
  190.          (bfd_coff_reloc16_get_value (reloc, seclet)>>16),
  191.          d );
  192.       (*dst_ptr) += 1;
  193.       (*src_ptr) += 1;
  194.       break;
  195.  
  196.     case R_H8500_IMM16:
  197.       bfd_put_16 (in_abfd,
  198.           bfd_coff_reloc16_get_value (reloc, seclet) ,
  199.           d  );
  200.       (*dst_ptr) += 2;
  201.       (*src_ptr) += 2;
  202.       break;
  203.  
  204.     case R_H8500_LOW16:
  205.       bfd_put_16 (in_abfd,
  206.           bfd_coff_reloc16_get_value (reloc, seclet) ,
  207.           d);
  208.  
  209.       (*dst_ptr) += 2;
  210.       (*src_ptr) += 2;
  211.       break;
  212.  
  213.     case R_H8500_IMM24:
  214.      {
  215.        int v = bfd_coff_reloc16_get_value(reloc, seclet);
  216.        int o = bfd_get_32(in_abfd, data+ *dst_ptr -1);
  217.        v = (v & 0x00ffffff) | (o & 0xff00000);
  218.        bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
  219.        (*dst_ptr) +=3;
  220.        (*src_ptr)+=3;;
  221.      }
  222.       break;
  223.     case R_H8500_IMM32:
  224.      {
  225.        int v = bfd_coff_reloc16_get_value(reloc, seclet);
  226.        bfd_put_32 (in_abfd, v, data  + *dst_ptr);
  227.        (*dst_ptr) +=4;
  228.        (*src_ptr)+=4;;
  229.      }
  230.       break;
  231.  
  232.  
  233.     case R_H8500_PCREL8:
  234.       {
  235.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, seclet);
  236.     bfd_vma dot = seclet->offset
  237.     + *dst_ptr
  238.     + seclet->u.indirect.section->output_section->vma;
  239.     int gap = dst - dot - 1;/* -1 since were in the odd byte of the
  240.                     word and the pc's been incremented */
  241.  
  242.     if (gap > 128 || gap < -128)
  243.       {
  244.         bfd_error_vector.reloc_value_truncated (reloc, seclet);
  245.       }
  246.     bfd_put_8 (in_abfd, gap, data + *dst_ptr);
  247.     (*dst_ptr)++;
  248.     (*src_ptr)++;
  249.     break;
  250.       }
  251.     case R_H8500_PCREL16:
  252.       {
  253.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, seclet);
  254.     bfd_vma dot = seclet->offset
  255.     + *dst_ptr
  256.     + seclet->u.indirect.section->output_section->vma;
  257.     int gap = dst - dot - 1;/* -1 since were in the odd byte of the
  258.                     word and the pc's been incremented */
  259.  
  260.     if (gap > 32767 || gap < -32768)
  261.       {
  262.         bfd_error_vector.reloc_value_truncated (reloc, seclet);
  263.       }
  264.     bfd_put_16 (in_abfd, gap, data + *dst_ptr);
  265.     (*dst_ptr)+=2;
  266.     (*src_ptr)+=2;
  267.     break;
  268.       }
  269.  
  270.     default:
  271.       abort ();
  272.     }
  273. }
  274.  
  275. #define coff_reloc16_extra_cases extra_case
  276.  
  277. #include "coffcode.h"
  278.  
  279.  
  280. #undef  coff_bfd_get_relocated_section_contents
  281. #undef coff_bfd_relax_section
  282. #define  coff_bfd_get_relocated_section_contents bfd_coff_reloc16_get_relocated_section_contents
  283. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  284.  
  285. bfd_target h8500coff_vec =
  286. {
  287.   "coff-h8500",            /* name */
  288.   bfd_target_coff_flavour,
  289.   true,                /* data byte order is big */
  290.   true,                /* header byte order is big */
  291.  
  292.   (HAS_RELOC | EXEC_P |        /* object flags */
  293.    HAS_LINENO | HAS_DEBUG |
  294.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  295.  
  296.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  297.   '_',                /* leading symbol underscore */
  298.   '/',                /* ar_pad_char */
  299.   15,                /* ar_max_namelen */
  300.   1,                /* minimum section alignment */
  301.   _do_getb64, _do_getb_signed_64, _do_putb64,
  302.      _do_getb32, _do_getb_signed_32, _do_putb32,
  303.      _do_getb16, _do_getb_signed_16, _do_putb16,    /* data */
  304.   _do_getb64, _do_getb_signed_64, _do_putb64,
  305.      _do_getb32, _do_getb_signed_32, _do_putb32,
  306.      _do_getb16, _do_getb_signed_16, _do_putb16,    /* hdrs */
  307.  
  308.   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
  309.    bfd_generic_archive_p, _bfd_dummy_target},
  310.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
  311.    bfd_false},
  312.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  313.    _bfd_write_archive_contents, bfd_false},
  314.  
  315.   JUMP_TABLE (coff),
  316.   COFF_SWAP_TABLE,
  317. };
  318.