home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / bfd / coff-z8k.c < prev    next >
C/C++ Source or Header  |  1995-12-15  |  8KB  |  282 lines

  1. /* BFD back-end for Zilog Z800n COFF binaries.
  2.    Copyright 1992, 1993, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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/z8k.h"
  28. #include "coff/internal.h"
  29. #include "libcoff.h"
  30.  
  31. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
  32.  
  33. static reloc_howto_type r_imm32 =
  34. HOWTO (R_IMM32, 0, 1, 32, false, 0,
  35.        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff,
  36.        0xffffffff, false);
  37.  
  38. static reloc_howto_type r_imm4l =
  39. HOWTO (R_IMM4L, 0, 1, 4, false, 0,
  40.        complain_overflow_bitfield, 0, "r_imm4l", true, 0xf, 0xf, false);
  41.  
  42. static reloc_howto_type r_da =
  43. HOWTO (R_IMM16, 0, 1, 16, false, 0,
  44.        complain_overflow_bitfield, 0, "r_da", true, 0x0000ffff, 0x0000ffff,
  45.        false);
  46.  
  47. static reloc_howto_type r_imm8 =
  48. HOWTO (R_IMM8, 0, 1, 8, false, 0,
  49.        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff,
  50.        false);
  51.  
  52. static reloc_howto_type r_jr =
  53. HOWTO (R_JR, 0, 1, 8, true, 0, complain_overflow_signed, 0,
  54.        "r_jr", true, 0, 0, true);
  55.  
  56. /* Turn a howto into a reloc number */
  57.  
  58. static int 
  59. coff_z8k_select_reloc (howto)
  60.      reloc_howto_type *howto;
  61. {
  62.   return howto->type;
  63. }
  64.  
  65. #define SELECT_RELOC(x,howto) x.r_type = coff_z8k_select_reloc(howto)
  66.  
  67.  
  68. #define BADMAG(x) Z8KBADMAG(x)
  69. #define Z8K 1            /* Customize coffcode.h */
  70. #define __A_MAGIC_SET__
  71.  
  72.  
  73.  
  74. /* Code to swap in the reloc */
  75. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  76. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  77. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  78.   dst->r_stuff[0] = 'S'; \
  79.   dst->r_stuff[1] = 'C';
  80.  
  81. /* Code to turn a r_type into a howto ptr, uses the above howto table
  82.    */
  83.  
  84. static void
  85. rtype2howto (internal, dst)
  86.      arelent * internal;
  87.      struct internal_reloc *dst;
  88. {
  89.   switch (dst->r_type)
  90.     {
  91.     default:
  92.       abort ();
  93.       break;
  94.     case R_IMM8:
  95.       internal->howto = &r_imm8;
  96.       break;
  97.      case R_IMM16:
  98.       internal->howto = &r_da;
  99.       break;
  100.     case R_JR:
  101.       internal->howto = &r_jr;
  102.       break;
  103.     case R_IMM32:
  104.       internal->howto = &r_imm32;
  105.       break;
  106.     case R_IMM4L:
  107.       internal->howto = &r_imm4l;
  108.       break;
  109.     }
  110. }
  111.  
  112. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  113.  
  114.  
  115. /* Perform any necessaru magic to the addend in a reloc entry */
  116.  
  117.  
  118. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  119.  cache_ptr->addend =  ext_reloc.r_offset;
  120.  
  121.  
  122. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  123.  reloc_processing(relent, reloc, symbols, abfd, section)
  124.  
  125. static void 
  126. reloc_processing (relent, reloc, symbols, abfd, section)
  127.      arelent * relent;
  128.      struct internal_reloc *reloc;
  129.      asymbol ** symbols;
  130.      bfd * abfd;
  131.      asection * section;
  132. {
  133.   relent->address = reloc->r_vaddr;
  134.   rtype2howto (relent, reloc);
  135.  
  136.   if (reloc->r_symndx > 0)
  137.     {
  138.       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  139.     }
  140.   else
  141.     {
  142.       relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  143.     }
  144.  
  145.  
  146.   relent->addend = reloc->r_offset;
  147.   relent->address -= section->vma;
  148. }
  149.  
  150. static void
  151. extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
  152.      bfd *in_abfd;
  153.      struct bfd_link_info *link_info;
  154.      struct bfd_link_order *link_order;
  155.      arelent *reloc;
  156.      bfd_byte *data;
  157.      unsigned int *src_ptr;
  158.      unsigned int *dst_ptr;
  159. {
  160.   asection *input_section = link_order->u.indirect.section;
  161.  
  162.   switch (reloc->howto->type)
  163.     {
  164.     case R_IMM8:
  165.       bfd_put_8 (in_abfd,
  166.          bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  167.          data + *dst_ptr);
  168.       (*dst_ptr) += 1;
  169.       (*src_ptr) += 1;
  170.       break;
  171.  
  172.     case R_IMM32:
  173.       bfd_put_32 (in_abfd,
  174.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  175.           data + *dst_ptr);
  176.       (*dst_ptr) += 4;
  177.       (*src_ptr) += 4;
  178.       break;
  179.  
  180.     case R_IMM4L:
  181.       bfd_put_8 (in_abfd,
  182.          ((bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0) 
  183.           | (0x0f
  184.              & bfd_coff_reloc16_get_value (reloc, link_info,
  185.                            input_section))),
  186.          data + *dst_ptr);
  187.       (*dst_ptr) += 1;
  188.       (*src_ptr) += 1;
  189.       break;
  190.  
  191.     case R_IMM16:
  192.       bfd_put_16 (in_abfd,
  193.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  194.           data + *dst_ptr);
  195.       (*dst_ptr) += 2;
  196.       (*src_ptr) += 2;
  197.       break;
  198.  
  199.     case R_JR:
  200.       {
  201.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
  202.                           input_section);
  203.     bfd_vma dot = (link_order->offset
  204.                + *dst_ptr
  205.                + input_section->output_section->vma);
  206.     int gap = dst - dot - 1;/* -1 since were in the odd byte of the
  207.                     word and the pc's been incremented */
  208.  
  209.     if (gap & 1)
  210.       abort ();
  211.     gap /= 2;
  212.     if (gap > 128 || gap < -128)
  213.       {
  214.         if (! ((*link_info->callbacks->reloc_overflow)
  215.            (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
  216.             reloc->howto->name, reloc->addend, input_section->owner,
  217.             input_section, reloc->address)))
  218.           abort ();
  219.       }
  220.     bfd_put_8 (in_abfd, gap, data + *dst_ptr);
  221.     (*dst_ptr)++;
  222.     (*src_ptr)++;
  223.     break;
  224.       }
  225.     default:
  226.       abort ();
  227.     }
  228. }
  229.  
  230. #define coff_reloc16_extra_cases extra_case
  231.  
  232. #include "coffcode.h"
  233.  
  234.  
  235. #undef  coff_bfd_get_relocated_section_contents
  236. #undef coff_bfd_relax_section
  237. #define coff_bfd_get_relocated_section_contents \
  238.   bfd_coff_reloc16_get_relocated_section_contents
  239. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  240.  
  241. const bfd_target z8kcoff_vec =
  242. {
  243.   "coff-z8k",            /* name */
  244.   bfd_target_coff_flavour,
  245.   BFD_ENDIAN_BIG,        /* data byte order is big */
  246.   BFD_ENDIAN_BIG,        /* header byte order is big */
  247.  
  248.   (HAS_RELOC | EXEC_P |        /* object flags */
  249.    HAS_LINENO | HAS_DEBUG |
  250.    HAS_SYMS | HAS_LOCALS | WP_TEXT),
  251.  
  252.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  253.   '_',                /* leading symbol underscore */
  254.   '/',                /* ar_pad_char */
  255.   15,                /* ar_max_namelen */
  256.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  257.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  258.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  259.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  260.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  261.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  262.  
  263.   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
  264.    bfd_generic_archive_p, _bfd_dummy_target},
  265.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
  266.    bfd_false},
  267.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  268.    _bfd_write_archive_contents, bfd_false},
  269.  
  270.      BFD_JUMP_TABLE_GENERIC (coff),
  271.      BFD_JUMP_TABLE_COPY (coff),
  272.      BFD_JUMP_TABLE_CORE (_bfd_nocore),
  273.      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  274.      BFD_JUMP_TABLE_SYMBOLS (coff),
  275.      BFD_JUMP_TABLE_RELOCS (coff),
  276.      BFD_JUMP_TABLE_WRITE (coff),
  277.      BFD_JUMP_TABLE_LINK (coff),
  278.      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  279.  
  280.   COFF_SWAP_TABLE,
  281. };
  282.