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 / coff-h8500.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  9KB  |  343 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 "obstack.h"
  25. #include "libbfd.h"
  26. #include "bfdlink.h"
  27. #include "coff/h8500.h"
  28. #include "coff/internal.h"
  29. #include "libcoff.h"
  30.  
  31. static reloc_howto_type r_imm8 =
  32. HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0,
  33.        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
  34.  
  35. static reloc_howto_type r_imm16 =
  36. HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0,
  37.        complain_overflow_bitfield, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false);
  38.  
  39. static reloc_howto_type r_imm24 =
  40. HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0,
  41.        complain_overflow_bitfield, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false);
  42.  
  43. static reloc_howto_type r_imm32 =
  44. HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0,
  45.        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
  46.  
  47.  
  48. static reloc_howto_type r_high8 =
  49. HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0,
  50.        complain_overflow_bitfield, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
  51.  
  52. static reloc_howto_type r_low16 =
  53. HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0,
  54.        complain_overflow_bitfield, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
  55.  
  56. static reloc_howto_type r_pcrel8 =
  57. HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, complain_overflow_signed, 0, "r_pcrel8", true, 0, 0, true);
  58.  
  59.  
  60. static reloc_howto_type r_pcrel16 =
  61. HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, complain_overflow_signed, 0, "r_pcrel16", true, 0, 0, true);
  62.  
  63. static reloc_howto_type r_high16 =
  64. HOWTO (R_H8500_HIGH16, 0, 1, 8, false, 0,
  65.        complain_overflow_bitfield, 0, "r_high16", true, 0x000ffff, 0x0000ffff, false);
  66.  
  67.  
  68. /* Turn a howto into a reloc number */
  69.  
  70. static int 
  71. coff_h8500_select_reloc (howto)
  72.      reloc_howto_type *howto;
  73. {
  74.   return howto->type;
  75. }
  76.  
  77. #define SELECT_RELOC(x,howto) x= coff_h8500_select_reloc(howto)
  78.  
  79.  
  80. #define BADMAG(x) H8500BADMAG(x)
  81. #define H8500 1            /* Customize coffcode.h */
  82.  
  83. #define __A_MAGIC_SET__
  84.  
  85. /* Code to swap in the reloc */
  86. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  87. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  88. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  89.   dst->r_stuff[0] = 'S'; \
  90.   dst->r_stuff[1] = 'C';
  91.  
  92. /* Code to turn a r_type into a howto ptr, uses the above howto table
  93.    */
  94.  
  95. static void
  96. rtype2howto(internal, dst)
  97.      arelent * internal;
  98.      struct internal_reloc *dst;
  99. {
  100.   switch (dst->r_type)
  101.     {
  102.     default:
  103.       fprintf (stderr, "BAD 0x%x\n", dst->r_type);
  104.     case R_H8500_IMM8:
  105.       internal->howto = &r_imm8;
  106.       break;
  107.     case R_H8500_IMM16:
  108.       internal->howto = &r_imm16;
  109.       break;
  110.     case R_H8500_IMM24:
  111.       internal->howto = &r_imm24;
  112.       break;
  113.     case R_H8500_IMM32:
  114.       internal->howto = &r_imm32;
  115.       break;
  116.     case R_H8500_PCREL8:
  117.       internal->howto = &r_pcrel8;
  118.       break;
  119.     case R_H8500_PCREL16:
  120.       internal->howto = &r_pcrel16;
  121.       break;
  122.     case R_H8500_HIGH8:
  123.       internal->howto = &r_high8;
  124.       break;
  125.     case R_H8500_HIGH16:
  126.       internal->howto = &r_high16;
  127.       break;
  128.     case R_H8500_LOW16:
  129.       internal->howto = &r_low16;
  130.       break;
  131.     }
  132. }
  133.  
  134. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  135.  
  136.  
  137. /* Perform any necessaru magic to the addend in a reloc entry */
  138.  
  139.  
  140. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  141.  cache_ptr->addend =  ext_reloc.r_offset;
  142.  
  143.  
  144. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  145.  reloc_processing(relent, reloc, symbols, abfd, section)
  146.  
  147. static void reloc_processing (relent, reloc, symbols, abfd, section)
  148.      arelent * relent;
  149.      struct internal_reloc *reloc;
  150.      asymbol ** symbols;
  151.      bfd * abfd;
  152.      asection * section;
  153. {
  154.   relent->address = reloc->r_vaddr;
  155.   rtype2howto (relent, reloc);
  156.  
  157.   if (reloc->r_symndx > 0)
  158.     {
  159.       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  160.     }
  161.   else
  162.     {
  163.       relent->sym_ptr_ptr = &(bfd_abs_symbol);
  164.     }
  165.  
  166.  
  167.   relent->addend = reloc->r_offset;
  168.   relent->address -= section->vma;
  169. }
  170.  
  171. static void
  172. extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
  173.      bfd *in_abfd;
  174.      struct bfd_link_info *link_info;
  175.      struct bfd_link_order *link_order;
  176.      arelent *reloc;
  177.      bfd_byte *data;
  178.      unsigned int *src_ptr;
  179.      unsigned int *dst_ptr;
  180. {
  181.   bfd_byte *d = data+*dst_ptr;
  182.   asection *input_section = link_order->u.indirect.section;
  183.   switch (reloc->howto->type)
  184.     {
  185.     case R_H8500_IMM8:
  186.       bfd_put_8 (in_abfd,
  187.          bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  188.          d);
  189.       (*dst_ptr) += 1;
  190.       (*src_ptr) += 1;
  191.       break;
  192.  
  193.     case R_H8500_HIGH8:
  194.       bfd_put_8 (in_abfd,
  195.          (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  196.           >> 16),
  197.          d );
  198.       (*dst_ptr) += 1;
  199.       (*src_ptr) += 1;
  200.       break;
  201.  
  202.     case R_H8500_IMM16:
  203.       bfd_put_16 (in_abfd,
  204.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  205.           d  );
  206.       (*dst_ptr) += 2;
  207.       (*src_ptr) += 2;
  208.       break;
  209.  
  210.     case R_H8500_LOW16:
  211.       bfd_put_16 (in_abfd,
  212.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  213.           d);
  214.  
  215.       (*dst_ptr) += 2;
  216.       (*src_ptr) += 2;
  217.       break;
  218.       
  219.     case R_H8500_HIGH16:
  220.       bfd_put_16 (in_abfd,
  221.           (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  222.            >>16),
  223.           d);
  224.  
  225.       (*dst_ptr) += 2;
  226.       (*src_ptr) += 2;
  227.       break;
  228.  
  229.     case R_H8500_IMM24:
  230.       {
  231.     int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
  232.     int o = bfd_get_32(in_abfd, data+ *dst_ptr -1);
  233.     v = (v & 0x00ffffff) | (o & 0xff00000);
  234.     bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
  235.     (*dst_ptr) +=3;
  236.     (*src_ptr)+=3;;
  237.       }
  238.       break;
  239.     case R_H8500_IMM32:
  240.       {
  241.     int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
  242.     bfd_put_32 (in_abfd, v, data  + *dst_ptr);
  243.     (*dst_ptr) +=4;
  244.     (*src_ptr)+=4;;
  245.       }
  246.       break;
  247.  
  248.  
  249.     case R_H8500_PCREL8:
  250.       {
  251.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
  252.                           input_section);
  253.     bfd_vma dot = link_order->offset
  254.       + *dst_ptr
  255.         + link_order->u.indirect.section->output_section->vma;
  256.     int gap = dst - dot - 1; /* -1 since were in the odd byte of the
  257.                     word and the pc's been incremented */
  258.  
  259.     if (gap > 128 || gap < -128)
  260.       {
  261.         if (! ((*link_info->callbacks->reloc_overflow)
  262.            (link_info, input_section->owner, input_section,
  263.             reloc->address)))
  264.           abort ();
  265.       }
  266.     bfd_put_8 (in_abfd, gap, data + *dst_ptr);
  267.     (*dst_ptr)++;
  268.     (*src_ptr)++;
  269.     break;
  270.       }
  271.     case R_H8500_PCREL16:
  272.       {
  273.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
  274.                           input_section);
  275.     bfd_vma dot = link_order->offset
  276.       + *dst_ptr
  277.         + link_order->u.indirect.section->output_section->vma;
  278.     int gap = dst - dot - 1; /* -1 since were in the odd byte of the
  279.                     word and the pc's been incremented */
  280.  
  281.     if (gap > 32767 || gap < -32768)
  282.       {
  283.         if (! ((*link_info->callbacks->reloc_overflow)
  284.            (link_info, input_section->owner, input_section,
  285.             reloc->address)))
  286.           abort ();
  287.       }
  288.     bfd_put_16 (in_abfd, gap, data + *dst_ptr);
  289.     (*dst_ptr)+=2;
  290.     (*src_ptr)+=2;
  291.     break;
  292.       }
  293.  
  294.     default:
  295.       abort ();
  296.     }
  297. }
  298.  
  299. #define coff_reloc16_extra_cases extra_case
  300.  
  301. #include "coffcode.h"
  302.  
  303.  
  304. #undef  coff_bfd_get_relocated_section_contents
  305. #undef coff_bfd_relax_section
  306. #define coff_bfd_get_relocated_section_contents \
  307.   bfd_coff_reloc16_get_relocated_section_contents
  308. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  309.  
  310. bfd_target h8500coff_vec =
  311. {
  312.   "coff-h8500",            /* name */
  313.   bfd_target_coff_flavour,
  314.   true,                /* data byte order is big */
  315.   true,                /* header byte order is big */
  316.  
  317.   (HAS_RELOC | EXEC_P |        /* object flags */
  318.    HAS_LINENO | HAS_DEBUG |
  319.    HAS_SYMS | HAS_LOCALS | WP_TEXT),
  320.  
  321.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  322.   '_',                /* leading symbol underscore */
  323.   '/',                /* ar_pad_char */
  324.   15,                /* ar_max_namelen */
  325.   1,                /* minimum section alignment */
  326.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  327.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  328.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  329.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  330.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  331.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  332.  
  333.   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
  334.    bfd_generic_archive_p, _bfd_dummy_target},
  335.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
  336.    bfd_false},
  337.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  338.    _bfd_write_archive_contents, bfd_false},
  339.  
  340.   JUMP_TABLE (coff),
  341.   COFF_SWAP_TABLE,
  342. };
  343.