Back to index

cell-binutils  2.17cvs20070401
coff-z80.c
Go to the documentation of this file.
00001 /* BFD back-end for Zilog Z80 COFF binaries.
00002    Copyright 2005, 2007 Free Software Foundation, Inc.
00003    Contributed by Arnold Metselaar <arnold_m@operamail.com>
00004 
00005    This file is part of BFD, the Binary File Descriptor library.
00006 
00007    This program is free software; you can redistribute it and/or modify 
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
00020    MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "libbfd.h"
00025 #include "bfdlink.h"
00026 #include "coff/z80.h"
00027 #include "coff/internal.h"
00028 #include "libcoff.h"
00029 
00030 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 0
00031 
00032 static reloc_howto_type r_imm32 =
00033 HOWTO (R_IMM32, 0, 1, 32, FALSE, 0,
00034        complain_overflow_dont, 0, "r_imm32", TRUE, 0xffffffff, 0xffffffff,
00035        FALSE);
00036 
00037 static reloc_howto_type r_imm24 =
00038 HOWTO (R_IMM24, 0, 1, 24, FALSE, 0,
00039        complain_overflow_dont, 0, "r_imm24", TRUE, 0x00ffffff, 0x00ffffff,
00040        FALSE);
00041 
00042 static reloc_howto_type r_imm16 =
00043 HOWTO (R_IMM16, 0, 1, 16, FALSE, 0,
00044        complain_overflow_dont, 0, "r_imm16", TRUE, 0x0000ffff, 0x0000ffff,
00045        FALSE);
00046 
00047 static reloc_howto_type r_imm8 =
00048 HOWTO (R_IMM8, 0, 0, 8, FALSE, 0,
00049        complain_overflow_bitfield, 0, "r_imm8", TRUE, 0x000000ff, 0x000000ff,
00050        FALSE);
00051 
00052 static reloc_howto_type r_jr =
00053 HOWTO (R_JR, 0, 0, 8, TRUE, 0, 
00054        complain_overflow_signed, 0, "r_jr", FALSE, 0, 0xFF,
00055        FALSE);
00056 
00057 static reloc_howto_type r_off8 =
00058 HOWTO (R_OFF8, 0, 0, 8, FALSE, 0, 
00059        complain_overflow_signed, 0,"r_off8", FALSE, 0, 0xff,
00060        FALSE);
00061 
00062 
00063 #define BADMAG(x) Z80BADMAG(x)
00064 #define Z80 1               /* Customize coffcode.h.  */
00065 #define __A_MAGIC_SET__
00066 
00067 /* Code to swap in the reloc.  */
00068 
00069 #define SWAP_IN_RELOC_OFFSET       H_GET_32
00070 #define SWAP_OUT_RELOC_OFFSET      H_PUT_32
00071 
00072 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
00073   dst->r_stuff[0] = 'S'; \
00074   dst->r_stuff[1] = 'C';
00075 
00076 /* Code to turn a r_type into a howto ptr, uses the above howto table.  */
00077 
00078 static void
00079 rtype2howto (arelent *internal, struct internal_reloc *dst)
00080 {
00081   switch (dst->r_type)
00082     {
00083     default:
00084       abort ();
00085       break;
00086     case R_IMM8:
00087       internal->howto = &r_imm8;
00088       break;
00089     case R_IMM16:
00090       internal->howto = &r_imm16;
00091       break;
00092     case R_IMM24:
00093       internal->howto = &r_imm24;
00094       break;
00095     case R_IMM32:
00096       internal->howto = &r_imm32;
00097       break;
00098     case R_JR:
00099       internal->howto = &r_jr;
00100       break;
00101     case R_OFF8:
00102       internal->howto = &r_off8;
00103       break;
00104     }
00105 }
00106 
00107 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
00108 
00109 static reloc_howto_type *
00110 coff_z80_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00111                          bfd_reloc_code_real_type code)
00112 {
00113   switch (code)
00114     {
00115     case BFD_RELOC_8:              return & r_imm8;
00116     case BFD_RELOC_16:             return & r_imm16;
00117     case BFD_RELOC_24:             return & r_imm24;
00118     case BFD_RELOC_32:             return & r_imm32;
00119     case BFD_RELOC_8_PCREL: return & r_jr;
00120     case BFD_RELOC_Z80_DISP8:      return & r_off8;
00121     default:                BFD_FAIL ();
00122       return NULL;
00123     }
00124 }
00125 
00126 static reloc_howto_type *
00127 coff_z80_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00128                          const char *r_name)
00129 {
00130   if (strcasecmp (r_imm8.name, r_name) == 0)
00131     return &r_imm8;
00132   if (strcasecmp (r_imm16.name, r_name) == 0)
00133     return &r_imm16;
00134   if (strcasecmp (r_imm24.name, r_name) == 0)
00135     return &r_imm24;
00136   if (strcasecmp (r_imm32.name, r_name) == 0)
00137     return &r_imm32;
00138   if (strcasecmp (r_jr.name, r_name) == 0)
00139     return &r_jr;
00140   if (strcasecmp (r_off8.name, r_name) == 0)
00141     return &r_off8;
00142 
00143   return NULL;
00144 }
00145 
00146 /* Perform any necessary magic to the addend in a reloc entry.  */
00147 
00148 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
00149  cache_ptr->addend =  ext_reloc.r_offset;
00150 
00151 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
00152  reloc_processing(relent, reloc, symbols, abfd, section)
00153 
00154 static void
00155 reloc_processing (arelent *relent,
00156                   struct internal_reloc *reloc,
00157                   asymbol **symbols,
00158                   bfd *abfd,
00159                   asection *section)
00160 {
00161   relent->address = reloc->r_vaddr;
00162   rtype2howto (relent, reloc);
00163 
00164   if (reloc->r_symndx > 0)
00165     relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
00166   else
00167     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
00168 
00169   relent->addend = reloc->r_offset;
00170   relent->address -= section->vma;
00171 }
00172 
00173 static void
00174 extra_case (bfd *in_abfd,
00175             struct bfd_link_info *link_info,
00176             struct bfd_link_order *link_order,
00177             arelent *reloc,
00178             bfd_byte *data,
00179             unsigned int *src_ptr,
00180             unsigned int *dst_ptr)
00181 {
00182   asection * input_section = link_order->u.indirect.section;
00183   int val;
00184 
00185   switch (reloc->howto->type)
00186     {
00187     case R_OFF8:
00188        val = bfd_coff_reloc16_get_value (reloc, link_info,
00189                                       input_section);
00190        if (val>127 || val<-128) /* Test for overflow.  */
00191          {
00192            if (! ((*link_info->callbacks->reloc_overflow)
00193                  (link_info, NULL,
00194                   bfd_asymbol_name (*reloc->sym_ptr_ptr),
00195                   reloc->howto->name, reloc->addend, input_section->owner,
00196                   input_section, reloc->address)))
00197              abort ();
00198          }
00199        bfd_put_8 (in_abfd, val, data + *dst_ptr);
00200        (*dst_ptr) += 1;
00201        (*src_ptr) += 1;
00202       break;
00203 
00204     case R_IMM8:
00205       val = bfd_get_8 ( in_abfd, data+*src_ptr)
00206        + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
00207       bfd_put_8 (in_abfd, val, data + *dst_ptr);
00208       (*dst_ptr) += 1;
00209       (*src_ptr) += 1;
00210       break;
00211 
00212     case R_IMM16:
00213       val = bfd_get_16 ( in_abfd, data+*src_ptr)
00214        + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
00215       bfd_put_16 (in_abfd, val, data + *dst_ptr);
00216       (*dst_ptr) += 2;
00217       (*src_ptr) += 2;
00218       break;
00219 
00220     case R_IMM24:
00221       val = bfd_get_16 ( in_abfd, data+*src_ptr)
00222        + (bfd_get_8 ( in_abfd, data+*src_ptr+2) << 16)
00223        + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
00224       bfd_put_16 (in_abfd, val, data + *dst_ptr);
00225       bfd_put_8 (in_abfd, val >> 16, data + *dst_ptr+2);
00226       (*dst_ptr) += 3;
00227       (*src_ptr) += 3;
00228       break;
00229 
00230     case R_IMM32:
00231       val = bfd_get_32 ( in_abfd, data+*src_ptr)
00232        + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
00233       bfd_put_32 (in_abfd, val, data + *dst_ptr);
00234       (*dst_ptr) += 4;
00235       (*src_ptr) += 4;
00236       break;
00237 
00238     case R_JR:
00239       {
00240        bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
00241                                             input_section);
00242        bfd_vma dot = (*dst_ptr
00243                      + input_section->output_offset
00244                      + input_section->output_section->vma);
00245        int gap = dst - dot - 1;  /* -1, Since the offset is relative
00246                                  to the value of PC after reading
00247                                  the offset.  */
00248 
00249        if (gap >= 128 || gap < -128)
00250          {
00251            if (! ((*link_info->callbacks->reloc_overflow)
00252                  (link_info, NULL,
00253                   bfd_asymbol_name (*reloc->sym_ptr_ptr),
00254                   reloc->howto->name, reloc->addend, input_section->owner,
00255                   input_section, reloc->address)))
00256              abort ();
00257          }
00258        bfd_put_8 (in_abfd, gap, data + *dst_ptr);
00259        (*dst_ptr)++;
00260        (*src_ptr)++;
00261        break;
00262       }
00263 
00264     default:
00265       abort ();
00266     }
00267 }
00268 
00269 #define coff_reloc16_extra_cases    extra_case
00270 #define coff_bfd_reloc_type_lookup  coff_z80_reloc_type_lookup
00271 #define coff_bfd_reloc_name_lookup coff_z80_reloc_name_lookup
00272 
00273 #include "coffcode.h"
00274 
00275 #undef  coff_bfd_get_relocated_section_contents
00276 #define coff_bfd_get_relocated_section_contents \
00277   bfd_coff_reloc16_get_relocated_section_contents
00278 
00279 #undef  coff_bfd_relax_section
00280 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
00281 
00282 CREATE_LITTLE_COFF_TARGET_VEC (z80coff_vec, "coff-z80", 0, 0, '\0', NULL, 
00283                             COFF_SWAP_TABLE)
00284