Home | History | Annotate | Line # | Download | only in bfd
cpu-ns32k.c revision 1.1.1.1.6.1
      1          1.1  skrll /* BFD support for the ns32k architecture.
      2          1.1  skrll    Copyright 1990, 1991, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003,
      3  1.1.1.1.6.1   yamt    2004, 2005, 2007, 2012 Free Software Foundation, Inc.
      4          1.1  skrll    Almost totally rewritten by Ian Dall from initial work
      5          1.1  skrll    by Andrew Cagney.
      6          1.1  skrll 
      7          1.1  skrll    This file is part of BFD, the Binary File Descriptor library.
      8          1.1  skrll 
      9          1.1  skrll    This program is free software; you can redistribute it and/or modify
     10          1.1  skrll    it under the terms of the GNU General Public License as published by
     11          1.1  skrll    the Free Software Foundation; either version 3 of the License, or
     12          1.1  skrll    (at your option) any later version.
     13          1.1  skrll 
     14          1.1  skrll    This program is distributed in the hope that it will be useful,
     15          1.1  skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16          1.1  skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17          1.1  skrll    GNU General Public License for more details.
     18          1.1  skrll 
     19          1.1  skrll    You should have received a copy of the GNU General Public License
     20          1.1  skrll    along with this program; if not, write to the Free Software
     21          1.1  skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     22          1.1  skrll    MA 02110-1301, USA.  */
     23          1.1  skrll 
     24          1.1  skrll #include "sysdep.h"
     25          1.1  skrll #include "bfd.h"
     26          1.1  skrll #include "libbfd.h"
     27          1.1  skrll #include "ns32k.h"
     28          1.1  skrll 
     29          1.1  skrll #define N(machine, printable, d, next)  \
     30  1.1.1.1.6.1   yamt {  32, 32, 8, bfd_arch_ns32k, machine, "ns32k",printable,3,d, \
     31  1.1.1.1.6.1   yamt    bfd_default_compatible,bfd_default_scan,bfd_arch_default_fill,next, }
     32          1.1  skrll 
     33          1.1  skrll static const bfd_arch_info_type arch_info_struct[] =
     34          1.1  skrll {
     35          1.1  skrll   N(32532,"ns32k:32532",TRUE, 0), /* The word ns32k will match this too.  */
     36          1.1  skrll };
     37          1.1  skrll 
     38          1.1  skrll const bfd_arch_info_type bfd_ns32k_arch =
     39          1.1  skrll   N(32032,"ns32k:32032",FALSE, &arch_info_struct[0]);
     40          1.1  skrll 
     41          1.1  skrll bfd_vma
     42  1.1.1.1.6.1   yamt _bfd_ns32k_get_displacement (bfd_byte *buffer, int size)
     43          1.1  skrll {
     44          1.1  skrll   bfd_signed_vma value;
     45          1.1  skrll 
     46          1.1  skrll   switch (size)
     47          1.1  skrll     {
     48          1.1  skrll     case 1:
     49          1.1  skrll       value = ((*buffer & 0x7f) ^ 0x40) - 0x40;
     50          1.1  skrll       break;
     51          1.1  skrll 
     52          1.1  skrll     case 2:
     53          1.1  skrll       value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
     54          1.1  skrll       value = (value << 8) | (0xff & *buffer);
     55          1.1  skrll       break;
     56          1.1  skrll 
     57          1.1  skrll     case 4:
     58          1.1  skrll       value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
     59          1.1  skrll       value = (value << 8) | (0xff & *buffer++);
     60          1.1  skrll       value = (value << 8) | (0xff & *buffer++);
     61          1.1  skrll       value = (value << 8) | (0xff & *buffer);
     62          1.1  skrll       break;
     63          1.1  skrll 
     64          1.1  skrll     default:
     65          1.1  skrll       abort ();
     66          1.1  skrll       return 0;
     67          1.1  skrll     }
     68          1.1  skrll 
     69          1.1  skrll   return value;
     70          1.1  skrll }
     71          1.1  skrll 
     72          1.1  skrll void
     73  1.1.1.1.6.1   yamt _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size)
     74          1.1  skrll {
     75          1.1  skrll   switch (size)
     76          1.1  skrll     {
     77          1.1  skrll     case 1:
     78          1.1  skrll       value &= 0x7f;
     79          1.1  skrll       *buffer++ = value;
     80          1.1  skrll       break;
     81          1.1  skrll 
     82          1.1  skrll     case 2:
     83          1.1  skrll       value &= 0x3fff;
     84          1.1  skrll       value |= 0x8000;
     85          1.1  skrll       *buffer++ = (value >> 8);
     86          1.1  skrll       *buffer++ = value;
     87          1.1  skrll       break;
     88          1.1  skrll 
     89          1.1  skrll     case 4:
     90          1.1  skrll       value |= (bfd_vma) 0xc0000000;
     91          1.1  skrll       *buffer++ = (value >> 24);
     92          1.1  skrll       *buffer++ = (value >> 16);
     93          1.1  skrll       *buffer++ = (value >> 8);
     94          1.1  skrll       *buffer++ = value;
     95          1.1  skrll       break;
     96          1.1  skrll   }
     97          1.1  skrll   return;
     98          1.1  skrll }
     99          1.1  skrll 
    100          1.1  skrll bfd_vma
    101  1.1.1.1.6.1   yamt _bfd_ns32k_get_immediate (bfd_byte *buffer, int size)
    102          1.1  skrll {
    103          1.1  skrll   bfd_vma value = 0;
    104          1.1  skrll 
    105          1.1  skrll   switch (size)
    106          1.1  skrll     {
    107          1.1  skrll     case 4:
    108          1.1  skrll       value = (value << 8) | (*buffer++ & 0xff);
    109          1.1  skrll       value = (value << 8) | (*buffer++ & 0xff);
    110          1.1  skrll     case 2:
    111          1.1  skrll       value = (value << 8) | (*buffer++ & 0xff);
    112          1.1  skrll     case 1:
    113          1.1  skrll       value = (value << 8) | (*buffer++ & 0xff);
    114          1.1  skrll       break;
    115          1.1  skrll     default:
    116          1.1  skrll       abort ();
    117          1.1  skrll     }
    118          1.1  skrll   return value;
    119          1.1  skrll }
    120          1.1  skrll 
    121          1.1  skrll void
    122  1.1.1.1.6.1   yamt _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size)
    123          1.1  skrll {
    124          1.1  skrll   buffer += size - 1;
    125          1.1  skrll   switch (size)
    126          1.1  skrll     {
    127          1.1  skrll     case 4:
    128          1.1  skrll       *buffer-- = (value & 0xff); value >>= 8;
    129          1.1  skrll       *buffer-- = (value & 0xff); value >>= 8;
    130          1.1  skrll     case 2:
    131          1.1  skrll       *buffer-- = (value & 0xff); value >>= 8;
    132          1.1  skrll     case 1:
    133          1.1  skrll       *buffer-- = (value & 0xff); value >>= 8;
    134          1.1  skrll     }
    135          1.1  skrll }
    136          1.1  skrll 
    137          1.1  skrll /* This is just like the standard perform_relocation except we
    138          1.1  skrll    use get_data and put_data which know about the ns32k storage
    139          1.1  skrll    methods.  This is probably a lot more complicated than it
    140          1.1  skrll    needs to be!  */
    141          1.1  skrll 
    142          1.1  skrll static bfd_reloc_status_type
    143  1.1.1.1.6.1   yamt do_ns32k_reloc (bfd *      abfd,
    144  1.1.1.1.6.1   yamt 		arelent *  reloc_entry,
    145  1.1.1.1.6.1   yamt 		struct bfd_symbol * symbol,
    146  1.1.1.1.6.1   yamt 		void *     data,
    147  1.1.1.1.6.1   yamt 		asection * input_section,
    148  1.1.1.1.6.1   yamt 		bfd *      output_bfd,
    149  1.1.1.1.6.1   yamt 		char **    error_message ATTRIBUTE_UNUSED,
    150  1.1.1.1.6.1   yamt 		bfd_vma (* get_data) (bfd_byte *, int),
    151  1.1.1.1.6.1   yamt 		void (*    put_data) (bfd_vma, bfd_byte *, int))
    152          1.1  skrll {
    153          1.1  skrll   int overflow = 0;
    154          1.1  skrll   bfd_vma relocation;
    155          1.1  skrll   bfd_reloc_status_type flag = bfd_reloc_ok;
    156          1.1  skrll   bfd_size_type addr = reloc_entry->address;
    157          1.1  skrll   bfd_vma output_base = 0;
    158          1.1  skrll   reloc_howto_type *howto = reloc_entry->howto;
    159          1.1  skrll   asection *reloc_target_output_section;
    160          1.1  skrll   bfd_byte *location;
    161          1.1  skrll 
    162  1.1.1.1.6.1   yamt   if (bfd_is_abs_section (symbol->section)
    163          1.1  skrll       && output_bfd != (bfd *) NULL)
    164          1.1  skrll     {
    165          1.1  skrll       reloc_entry->address += input_section->output_offset;
    166          1.1  skrll       return bfd_reloc_ok;
    167          1.1  skrll     }
    168          1.1  skrll 
    169          1.1  skrll   /* If we are not producing relocatable output, return an error if
    170          1.1  skrll      the symbol is not defined.  An undefined weak symbol is
    171          1.1  skrll      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
    172  1.1.1.1.6.1   yamt   if (bfd_is_und_section (symbol->section)
    173          1.1  skrll       && (symbol->flags & BSF_WEAK) == 0
    174          1.1  skrll       && output_bfd == (bfd *) NULL)
    175          1.1  skrll     flag = bfd_reloc_undefined;
    176          1.1  skrll 
    177          1.1  skrll   /* Is the address of the relocation really within the section?  */
    178          1.1  skrll   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
    179          1.1  skrll     return bfd_reloc_outofrange;
    180          1.1  skrll 
    181          1.1  skrll   /* Work out which section the relocation is targeted at and the
    182          1.1  skrll      initial relocation command value.  */
    183          1.1  skrll 
    184          1.1  skrll   /* Get symbol value.  (Common symbols are special.)  */
    185          1.1  skrll   if (bfd_is_com_section (symbol->section))
    186          1.1  skrll     relocation = 0;
    187          1.1  skrll   else
    188          1.1  skrll     relocation = symbol->value;
    189          1.1  skrll 
    190          1.1  skrll   reloc_target_output_section = symbol->section->output_section;
    191          1.1  skrll 
    192          1.1  skrll   /* Convert input-section-relative symbol value to absolute.  */
    193          1.1  skrll   if (output_bfd != NULL && ! howto->partial_inplace)
    194          1.1  skrll     output_base = 0;
    195          1.1  skrll   else
    196          1.1  skrll     output_base = reloc_target_output_section->vma;
    197          1.1  skrll 
    198          1.1  skrll   relocation += output_base + symbol->section->output_offset;
    199          1.1  skrll 
    200          1.1  skrll   /* Add in supplied addend.  */
    201          1.1  skrll   relocation += reloc_entry->addend;
    202          1.1  skrll 
    203          1.1  skrll   /* Here the variable relocation holds the final address of the
    204          1.1  skrll      symbol we are relocating against, plus any addend.  */
    205          1.1  skrll 
    206          1.1  skrll   if (howto->pc_relative)
    207          1.1  skrll     {
    208          1.1  skrll       /* This is a PC relative relocation.  We want to set RELOCATION
    209          1.1  skrll 	 to the distance between the address of the symbol and the
    210          1.1  skrll 	 location.  RELOCATION is already the address of the symbol.
    211          1.1  skrll 
    212          1.1  skrll 	 We start by subtracting the address of the section containing
    213          1.1  skrll 	 the location.
    214          1.1  skrll 
    215          1.1  skrll 	 If pcrel_offset is set, we must further subtract the position
    216          1.1  skrll 	 of the location within the section.  Some targets arrange for
    217          1.1  skrll 	 the addend to be the negative of the position of the location
    218          1.1  skrll 	 within the section; for example, i386-aout does this.  For
    219          1.1  skrll 	 i386-aout, pcrel_offset is FALSE.  Some other targets do not
    220          1.1  skrll 	 include the position of the location; for example, m88kbcs,
    221          1.1  skrll 	 or ELF.  For those targets, pcrel_offset is TRUE.
    222          1.1  skrll 
    223          1.1  skrll 	 If we are producing relocatable output, then we must ensure
    224          1.1  skrll 	 that this reloc will be correctly computed when the final
    225          1.1  skrll 	 relocation is done.  If pcrel_offset is FALSE we want to wind
    226          1.1  skrll 	 up with the negative of the location within the section,
    227          1.1  skrll 	 which means we must adjust the existing addend by the change
    228          1.1  skrll 	 in the location within the section.  If pcrel_offset is TRUE
    229          1.1  skrll 	 we do not want to adjust the existing addend at all.
    230          1.1  skrll 
    231          1.1  skrll 	 FIXME: This seems logical to me, but for the case of
    232          1.1  skrll 	 producing relocatable output it is not what the code
    233          1.1  skrll 	 actually does.  I don't want to change it, because it seems
    234          1.1  skrll 	 far too likely that something will break.  */
    235          1.1  skrll       relocation -=
    236          1.1  skrll 	input_section->output_section->vma + input_section->output_offset;
    237          1.1  skrll 
    238          1.1  skrll       if (howto->pcrel_offset)
    239          1.1  skrll 	relocation -= reloc_entry->address;
    240          1.1  skrll     }
    241          1.1  skrll 
    242          1.1  skrll   if (output_bfd != (bfd *) NULL)
    243          1.1  skrll     {
    244          1.1  skrll       if (! howto->partial_inplace)
    245          1.1  skrll 	{
    246          1.1  skrll 	  /* This is a partial relocation, and we want to apply the relocation
    247          1.1  skrll 	     to the reloc entry rather than the raw data. Modify the reloc
    248          1.1  skrll 	     inplace to reflect what we now know.  */
    249          1.1  skrll 	  reloc_entry->addend = relocation;
    250          1.1  skrll 	  reloc_entry->address += input_section->output_offset;
    251          1.1  skrll 	  return flag;
    252          1.1  skrll 	}
    253          1.1  skrll       else
    254          1.1  skrll 	{
    255          1.1  skrll 	  /* This is a partial relocation, but inplace, so modify the
    256          1.1  skrll 	     reloc record a bit.
    257          1.1  skrll 
    258          1.1  skrll 	     If we've relocated with a symbol with a section, change
    259          1.1  skrll 	     into a ref to the section belonging to the symbol.  */
    260          1.1  skrll 
    261          1.1  skrll 	  reloc_entry->address += input_section->output_offset;
    262          1.1  skrll 
    263          1.1  skrll 	  /* WTF?? */
    264          1.1  skrll 	  if (abfd->xvec->flavour == bfd_target_coff_flavour)
    265          1.1  skrll 	    {
    266          1.1  skrll 	      /* For m68k-coff, the addend was being subtracted twice during
    267          1.1  skrll 		 relocation with -r.  Removing the line below this comment
    268          1.1  skrll 		 fixes that problem; see PR 2953.
    269          1.1  skrll 
    270          1.1  skrll 		 However, Ian wrote the following, regarding removing the line
    271          1.1  skrll 		 below, which explains why it is still enabled:  --djm
    272          1.1  skrll 
    273          1.1  skrll 		 If you put a patch like that into BFD you need to check all
    274          1.1  skrll 		 the COFF linkers.  I am fairly certain that patch will break
    275          1.1  skrll 		 coff-i386 (e.g., SCO); see coff_i386_reloc in coff-i386.c
    276          1.1  skrll 		 where I worked around the problem in a different way.  There
    277          1.1  skrll 		 may very well be a reason that the code works as it does.
    278          1.1  skrll 
    279          1.1  skrll 		 Hmmm.  The first obvious point is that bfd_perform_relocation
    280          1.1  skrll 		 should not have any tests that depend upon the flavour.  It's
    281          1.1  skrll 		 seem like entirely the wrong place for such a thing.  The
    282          1.1  skrll 		 second obvious point is that the current code ignores the
    283          1.1  skrll 		 reloc addend when producing relocatable output for COFF.
    284          1.1  skrll 		 That's peculiar.  In fact, I really have no idea what the
    285          1.1  skrll 		 point of the line you want to remove is.
    286          1.1  skrll 
    287          1.1  skrll 		 A typical COFF reloc subtracts the old value of the symbol
    288          1.1  skrll 		 and adds in the new value to the location in the object file
    289          1.1  skrll 		 (if it's a pc relative reloc it adds the difference between
    290          1.1  skrll 		 the symbol value and the location).  When relocating we need
    291          1.1  skrll 		 to preserve that property.
    292          1.1  skrll 
    293          1.1  skrll 		 BFD handles this by setting the addend to the negative of the
    294          1.1  skrll 		 old value of the symbol.  Unfortunately it handles common
    295          1.1  skrll 		 symbols in a non-standard way (it doesn't subtract the old
    296          1.1  skrll 		 value) but that's a different story (we can't change it
    297          1.1  skrll 		 without losing backward compatibility with old object files)
    298          1.1  skrll 		 (coff-i386 does subtract the old value, to be compatible with
    299          1.1  skrll 		 existing coff-i386 targets, like SCO).
    300          1.1  skrll 
    301          1.1  skrll 		 So everything works fine when not producing relocatable
    302          1.1  skrll 		 output.  When we are producing relocatable output, logically
    303          1.1  skrll 		 we should do exactly what we do when not producing
    304          1.1  skrll 		 relocatable output.  Therefore, your patch is correct.  In
    305          1.1  skrll 		 fact, it should probably always just set reloc_entry->addend
    306          1.1  skrll 		 to 0 for all cases, since it is, in fact, going to add the
    307          1.1  skrll 		 value into the object file.  This won't hurt the COFF code,
    308          1.1  skrll 		 which doesn't use the addend; I'm not sure what it will do
    309          1.1  skrll 		 to other formats (the thing to check for would be whether
    310          1.1  skrll 		 any formats both use the addend and set partial_inplace).
    311          1.1  skrll 
    312          1.1  skrll 		 When I wanted to make coff-i386 produce relocatable output,
    313          1.1  skrll 		 I ran into the problem that you are running into: I wanted
    314          1.1  skrll 		 to remove that line.  Rather than risk it, I made the
    315          1.1  skrll 		 coff-i386 relocs use a special function; it's coff_i386_reloc
    316          1.1  skrll 		 in coff-i386.c.  The function specifically adds the addend
    317          1.1  skrll 		 field into the object file, knowing that bfd_perform_relocation
    318          1.1  skrll 		 is not going to.  If you remove that line, then coff-i386.c
    319          1.1  skrll 		 will wind up adding the addend field in twice.  It's trivial
    320          1.1  skrll 		 to fix; it just needs to be done.
    321          1.1  skrll 
    322          1.1  skrll 		 The problem with removing the line is just that it may break
    323          1.1  skrll 		 some working code.  With BFD it's hard to be sure of anything.
    324          1.1  skrll 		 The right way to deal with this is simply to build and test at
    325          1.1  skrll 		 least all the supported COFF targets.  It should be
    326          1.1  skrll 		 straightforward if time and disk space consuming.  For each
    327          1.1  skrll 		 target:
    328          1.1  skrll 		   1) build the linker
    329          1.1  skrll 		   2) generate some executable, and link it using -r (I would
    330          1.1  skrll 		      probably use paranoia.o and link against newlib/libc.a,
    331          1.1  skrll 		      which for all the supported targets would be available in
    332          1.1  skrll 		      /usr/cygnus/progressive/H-host/target/lib/libc.a).
    333          1.1  skrll 		   3) make the change to reloc.c
    334          1.1  skrll 		   4) rebuild the linker
    335          1.1  skrll 		   5) repeat step 2
    336          1.1  skrll 		   6) if the resulting object files are the same, you have at
    337          1.1  skrll 		      least made it no worse
    338          1.1  skrll 		   7) if they are different you have to figure out which
    339          1.1  skrll 		      version is right.  */
    340          1.1  skrll 	      relocation -= reloc_entry->addend;
    341          1.1  skrll 	      reloc_entry->addend = 0;
    342          1.1  skrll 	    }
    343          1.1  skrll 	  else
    344          1.1  skrll 	    {
    345          1.1  skrll 	      reloc_entry->addend = relocation;
    346          1.1  skrll 	    }
    347          1.1  skrll 	}
    348          1.1  skrll     }
    349          1.1  skrll   else
    350          1.1  skrll     {
    351          1.1  skrll       reloc_entry->addend = 0;
    352          1.1  skrll     }
    353          1.1  skrll 
    354          1.1  skrll   /* FIXME: This overflow checking is incomplete, because the value
    355          1.1  skrll      might have overflowed before we get here.  For a correct check we
    356          1.1  skrll      need to compute the value in a size larger than bitsize, but we
    357          1.1  skrll      can't reasonably do that for a reloc the same size as a host
    358          1.1  skrll      machine word.
    359          1.1  skrll      FIXME: We should also do overflow checking on the result after
    360          1.1  skrll      adding in the value contained in the object file.  */
    361          1.1  skrll   if (howto->complain_on_overflow != complain_overflow_dont)
    362          1.1  skrll     {
    363          1.1  skrll       bfd_vma check;
    364          1.1  skrll 
    365          1.1  skrll       /* Get the value that will be used for the relocation, but
    366          1.1  skrll 	 starting at bit position zero.  */
    367          1.1  skrll       if (howto->rightshift > howto->bitpos)
    368          1.1  skrll 	check = relocation >> (howto->rightshift - howto->bitpos);
    369          1.1  skrll       else
    370          1.1  skrll 	check = relocation << (howto->bitpos - howto->rightshift);
    371          1.1  skrll       switch (howto->complain_on_overflow)
    372          1.1  skrll 	{
    373          1.1  skrll 	case complain_overflow_signed:
    374          1.1  skrll 	  {
    375          1.1  skrll 	    /* Assumes two's complement.  */
    376          1.1  skrll 	    bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
    377          1.1  skrll 	    bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
    378          1.1  skrll 
    379          1.1  skrll 	    /* The above right shift is incorrect for a signed value.
    380          1.1  skrll 	       Fix it up by forcing on the upper bits.  */
    381          1.1  skrll 	    if (howto->rightshift > howto->bitpos
    382          1.1  skrll 		&& (bfd_signed_vma) relocation < 0)
    383          1.1  skrll 	      check |= ((bfd_vma) - 1
    384          1.1  skrll 			& ~((bfd_vma) - 1
    385          1.1  skrll 			    >> (howto->rightshift - howto->bitpos)));
    386          1.1  skrll 	    if ((bfd_signed_vma) check > reloc_signed_max
    387          1.1  skrll 		|| (bfd_signed_vma) check < reloc_signed_min)
    388          1.1  skrll 	      flag = bfd_reloc_overflow;
    389          1.1  skrll 	  }
    390          1.1  skrll 	  break;
    391          1.1  skrll 	case complain_overflow_unsigned:
    392          1.1  skrll 	  {
    393          1.1  skrll 	    /* Assumes two's complement.  This expression avoids
    394          1.1  skrll 	       overflow if howto->bitsize is the number of bits in
    395          1.1  skrll 	       bfd_vma.  */
    396          1.1  skrll 	    bfd_vma reloc_unsigned_max =
    397          1.1  skrll 	    (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
    398          1.1  skrll 
    399          1.1  skrll 	    if ((bfd_vma) check > reloc_unsigned_max)
    400          1.1  skrll 	      flag = bfd_reloc_overflow;
    401          1.1  skrll 	  }
    402          1.1  skrll 	  break;
    403          1.1  skrll 	case complain_overflow_bitfield:
    404          1.1  skrll 	  {
    405          1.1  skrll 	    /* Assumes two's complement.  This expression avoids
    406          1.1  skrll 	       overflow if howto->bitsize is the number of bits in
    407          1.1  skrll 	       bfd_vma.  */
    408          1.1  skrll 	    bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
    409          1.1  skrll 
    410          1.1  skrll 	    if (((bfd_vma) check & ~reloc_bits) != 0
    411          1.1  skrll 		&& (((bfd_vma) check & ~reloc_bits)
    412          1.1  skrll 		    != (-(bfd_vma) 1 & ~reloc_bits)))
    413          1.1  skrll 	      {
    414          1.1  skrll 		/* The above right shift is incorrect for a signed
    415          1.1  skrll 		   value.  See if turning on the upper bits fixes the
    416          1.1  skrll 		   overflow.  */
    417          1.1  skrll 		if (howto->rightshift > howto->bitpos
    418          1.1  skrll 		    && (bfd_signed_vma) relocation < 0)
    419          1.1  skrll 		  {
    420          1.1  skrll 		    check |= ((bfd_vma) - 1
    421          1.1  skrll 			      & ~((bfd_vma) - 1
    422          1.1  skrll 				  >> (howto->rightshift - howto->bitpos)));
    423          1.1  skrll 		    if (((bfd_vma) check & ~reloc_bits)
    424          1.1  skrll 			!= (-(bfd_vma) 1 & ~reloc_bits))
    425          1.1  skrll 		      flag = bfd_reloc_overflow;
    426          1.1  skrll 		  }
    427          1.1  skrll 		else
    428          1.1  skrll 		  flag = bfd_reloc_overflow;
    429          1.1  skrll 	      }
    430          1.1  skrll 	  }
    431          1.1  skrll 	  break;
    432          1.1  skrll 	default:
    433          1.1  skrll 	  abort ();
    434          1.1  skrll 	}
    435          1.1  skrll     }
    436          1.1  skrll 
    437          1.1  skrll   /* Either we are relocating all the way, or we don't want to apply
    438          1.1  skrll      the relocation to the reloc entry (probably because there isn't
    439          1.1  skrll      any room in the output format to describe addends to relocs).  */
    440          1.1  skrll 
    441          1.1  skrll   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
    442          1.1  skrll      (OSF version 1.3, compiler version 3.11).  It miscompiles the
    443          1.1  skrll      following program:
    444          1.1  skrll 
    445          1.1  skrll      struct str
    446          1.1  skrll      {
    447          1.1  skrll        unsigned int i0;
    448          1.1  skrll      } s = { 0 };
    449          1.1  skrll 
    450          1.1  skrll      int
    451          1.1  skrll      main ()
    452          1.1  skrll      {
    453          1.1  skrll        unsigned long x;
    454          1.1  skrll 
    455          1.1  skrll        x = 0x100000000;
    456          1.1  skrll        x <<= (unsigned long) s.i0;
    457          1.1  skrll        if (x == 0)
    458          1.1  skrll 	 printf ("failed\n");
    459          1.1  skrll        else
    460          1.1  skrll 	 printf ("succeeded (%lx)\n", x);
    461          1.1  skrll      }
    462          1.1  skrll      */
    463          1.1  skrll 
    464          1.1  skrll   relocation >>= (bfd_vma) howto->rightshift;
    465          1.1  skrll 
    466          1.1  skrll   /* Shift everything up to where it's going to be used.  */
    467          1.1  skrll   relocation <<= (bfd_vma) howto->bitpos;
    468          1.1  skrll 
    469          1.1  skrll   /* Wait for the day when all have the mask in them.  */
    470          1.1  skrll 
    471          1.1  skrll   /* What we do:
    472          1.1  skrll      i instruction to be left alone
    473          1.1  skrll      o offset within instruction
    474          1.1  skrll      r relocation offset to apply
    475          1.1  skrll      S src mask
    476          1.1  skrll      D dst mask
    477          1.1  skrll      N ~dst mask
    478          1.1  skrll      A part 1
    479          1.1  skrll      B part 2
    480          1.1  skrll      R result
    481          1.1  skrll 
    482          1.1  skrll      Do this:
    483          1.1  skrll      i i i i i o o o o o        from bfd_get<size>
    484          1.1  skrll      and           S S S S S    to get the size offset we want
    485          1.1  skrll      +   r r r r r r r r r r  to get the final value to place
    486          1.1  skrll      and           D D D D D  to chop to right size
    487          1.1  skrll      -----------------------
    488          1.1  skrll      A A A A A
    489          1.1  skrll      And this:
    490          1.1  skrll      ...   i i i i i o o o o o  from bfd_get<size>
    491          1.1  skrll      and   N N N N N            get instruction
    492          1.1  skrll      -----------------------
    493          1.1  skrll      ...   B B B B B
    494          1.1  skrll 
    495          1.1  skrll      And then:
    496          1.1  skrll      B B B B B
    497          1.1  skrll      or              A A A A A
    498          1.1  skrll      -----------------------
    499          1.1  skrll      R R R R R R R R R R        put into bfd_put<size>.  */
    500          1.1  skrll 
    501          1.1  skrll #define DOIT(x) \
    502          1.1  skrll   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
    503          1.1  skrll 
    504          1.1  skrll   location = (bfd_byte *) data + addr;
    505          1.1  skrll   switch (howto->size)
    506          1.1  skrll     {
    507          1.1  skrll     case 0:
    508          1.1  skrll       {
    509          1.1  skrll 	bfd_vma x = get_data (location, 1);
    510          1.1  skrll 	DOIT (x);
    511          1.1  skrll 	put_data ((bfd_vma) x, location, 1);
    512          1.1  skrll       }
    513          1.1  skrll       break;
    514          1.1  skrll 
    515          1.1  skrll     case 1:
    516          1.1  skrll       if (relocation)
    517          1.1  skrll 	{
    518          1.1  skrll 	  bfd_vma x = get_data (location, 2);
    519          1.1  skrll 	  DOIT (x);
    520          1.1  skrll 	  put_data ((bfd_vma) x, location, 2);
    521          1.1  skrll 	}
    522          1.1  skrll       break;
    523          1.1  skrll     case 2:
    524          1.1  skrll       if (relocation)
    525          1.1  skrll 	{
    526          1.1  skrll 	  bfd_vma x = get_data (location, 4);
    527          1.1  skrll 	  DOIT (x);
    528          1.1  skrll 	  put_data ((bfd_vma) x, location, 4);
    529          1.1  skrll 	}
    530          1.1  skrll       break;
    531          1.1  skrll     case -2:
    532          1.1  skrll       {
    533          1.1  skrll 	bfd_vma x = get_data (location, 4);
    534          1.1  skrll 	relocation = -relocation;
    535          1.1  skrll 	DOIT(x);
    536          1.1  skrll 	put_data ((bfd_vma) x, location, 4);
    537          1.1  skrll       }
    538          1.1  skrll       break;
    539          1.1  skrll 
    540          1.1  skrll     case 3:
    541          1.1  skrll       /* Do nothing.  */
    542          1.1  skrll       break;
    543          1.1  skrll 
    544          1.1  skrll     case 4:
    545          1.1  skrll #ifdef BFD64
    546          1.1  skrll       if (relocation)
    547          1.1  skrll 	{
    548          1.1  skrll 	  bfd_vma x = get_data (location, 8);
    549          1.1  skrll 	  DOIT (x);
    550          1.1  skrll 	  put_data (x, location, 8);
    551          1.1  skrll 	}
    552          1.1  skrll #else
    553          1.1  skrll       abort ();
    554          1.1  skrll #endif
    555          1.1  skrll       break;
    556          1.1  skrll     default:
    557          1.1  skrll       return bfd_reloc_other;
    558          1.1  skrll     }
    559          1.1  skrll   if ((howto->complain_on_overflow != complain_overflow_dont) && overflow)
    560          1.1  skrll     return bfd_reloc_overflow;
    561          1.1  skrll 
    562          1.1  skrll   return flag;
    563          1.1  skrll }
    564          1.1  skrll 
    565          1.1  skrll /* Relocate a given location using a given value and howto.  */
    566          1.1  skrll 
    567          1.1  skrll bfd_reloc_status_type
    568  1.1.1.1.6.1   yamt _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto,
    569  1.1.1.1.6.1   yamt 			      bfd *input_bfd ATTRIBUTE_UNUSED,
    570  1.1.1.1.6.1   yamt 			      bfd_vma relocation,
    571  1.1.1.1.6.1   yamt 			      bfd_byte *location,
    572  1.1.1.1.6.1   yamt 			      bfd_vma (*get_data) (bfd_byte *, int),
    573  1.1.1.1.6.1   yamt 			      void (*put_data) (bfd_vma, bfd_byte *, int))
    574          1.1  skrll {
    575          1.1  skrll   int size;
    576          1.1  skrll   bfd_vma x;
    577          1.1  skrll   bfd_boolean overflow;
    578          1.1  skrll 
    579          1.1  skrll   /* If the size is negative, negate RELOCATION.  This isn't very
    580          1.1  skrll      general.  */
    581          1.1  skrll   if (howto->size < 0)
    582          1.1  skrll     relocation = -relocation;
    583          1.1  skrll 
    584          1.1  skrll   /* Get the value we are going to relocate.  */
    585          1.1  skrll   size = bfd_get_reloc_size (howto);
    586          1.1  skrll   switch (size)
    587          1.1  skrll     {
    588          1.1  skrll     default:
    589          1.1  skrll     case 0:
    590          1.1  skrll       abort ();
    591          1.1  skrll     case 1:
    592          1.1  skrll     case 2:
    593          1.1  skrll     case 4:
    594          1.1  skrll #ifdef BFD64
    595          1.1  skrll     case 8:
    596          1.1  skrll #endif
    597          1.1  skrll       x = get_data (location, size);
    598          1.1  skrll       break;
    599          1.1  skrll     }
    600          1.1  skrll 
    601          1.1  skrll   /* Check for overflow.  FIXME: We may drop bits during the addition
    602          1.1  skrll      which we don't check for.  We must either check at every single
    603          1.1  skrll      operation, which would be tedious, or we must do the computations
    604          1.1  skrll      in a type larger than bfd_vma, which would be inefficient.  */
    605          1.1  skrll   overflow = FALSE;
    606          1.1  skrll   if (howto->complain_on_overflow != complain_overflow_dont)
    607          1.1  skrll     {
    608          1.1  skrll       bfd_vma check;
    609          1.1  skrll       bfd_signed_vma signed_check;
    610          1.1  skrll       bfd_vma add;
    611          1.1  skrll       bfd_signed_vma signed_add;
    612          1.1  skrll 
    613          1.1  skrll       if (howto->rightshift == 0)
    614          1.1  skrll 	{
    615          1.1  skrll 	  check = relocation;
    616          1.1  skrll 	  signed_check = (bfd_signed_vma) relocation;
    617          1.1  skrll 	}
    618          1.1  skrll       else
    619          1.1  skrll 	{
    620          1.1  skrll 	  /* Drop unwanted bits from the value we are relocating to.  */
    621          1.1  skrll 	  check = relocation >> howto->rightshift;
    622          1.1  skrll 
    623          1.1  skrll 	  /* If this is a signed value, the rightshift just dropped
    624          1.1  skrll 	     leading 1 bits (assuming twos complement).  */
    625          1.1  skrll 	  if ((bfd_signed_vma) relocation >= 0)
    626          1.1  skrll 	    signed_check = check;
    627          1.1  skrll 	  else
    628          1.1  skrll 	    signed_check = (check
    629          1.1  skrll 			    | ((bfd_vma) - 1
    630          1.1  skrll 			       & ~((bfd_vma) - 1 >> howto->rightshift)));
    631          1.1  skrll 	}
    632          1.1  skrll 
    633          1.1  skrll       /* Get the value from the object file.  */
    634          1.1  skrll       add = x & howto->src_mask;
    635          1.1  skrll 
    636          1.1  skrll       /* Get the value from the object file with an appropriate sign.
    637          1.1  skrll 	 The expression involving howto->src_mask isolates the upper
    638          1.1  skrll 	 bit of src_mask.  If that bit is set in the value we are
    639          1.1  skrll 	 adding, it is negative, and we subtract out that number times
    640          1.1  skrll 	 two.  If src_mask includes the highest possible bit, then we
    641          1.1  skrll 	 can not get the upper bit, but that does not matter since
    642          1.1  skrll 	 signed_add needs no adjustment to become negative in that
    643          1.1  skrll 	 case.  */
    644          1.1  skrll       signed_add = add;
    645          1.1  skrll       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
    646          1.1  skrll 	signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
    647          1.1  skrll 
    648          1.1  skrll       /* Add the value from the object file, shifted so that it is a
    649          1.1  skrll 	 straight number.  */
    650          1.1  skrll       if (howto->bitpos == 0)
    651          1.1  skrll 	{
    652          1.1  skrll 	  check += add;
    653          1.1  skrll 	  signed_check += signed_add;
    654          1.1  skrll 	}
    655          1.1  skrll       else
    656          1.1  skrll 	{
    657          1.1  skrll 	  check += add >> howto->bitpos;
    658          1.1  skrll 
    659          1.1  skrll 	  /* For the signed case we use ADD, rather than SIGNED_ADD,
    660          1.1  skrll 	     to avoid warnings from SVR4 cc.  This is OK since we
    661          1.1  skrll 	     explicitly handle the sign bits.  */
    662          1.1  skrll 	  if (signed_add >= 0)
    663          1.1  skrll 	    signed_check += add >> howto->bitpos;
    664          1.1  skrll 	  else
    665          1.1  skrll 	    signed_check += ((add >> howto->bitpos)
    666          1.1  skrll 			     | ((bfd_vma) - 1
    667          1.1  skrll 				& ~((bfd_vma) - 1 >> howto->bitpos)));
    668          1.1  skrll 	}
    669          1.1  skrll 
    670          1.1  skrll       switch (howto->complain_on_overflow)
    671          1.1  skrll 	{
    672          1.1  skrll 	case complain_overflow_signed:
    673          1.1  skrll 	  {
    674          1.1  skrll 	    /* Assumes two's complement.  */
    675          1.1  skrll 	    bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
    676          1.1  skrll 	    bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
    677          1.1  skrll 
    678          1.1  skrll 	    if (signed_check > reloc_signed_max
    679          1.1  skrll 		|| signed_check < reloc_signed_min)
    680          1.1  skrll 	      overflow = TRUE;
    681          1.1  skrll 	  }
    682          1.1  skrll 	  break;
    683          1.1  skrll 	case complain_overflow_unsigned:
    684          1.1  skrll 	  {
    685          1.1  skrll 	    /* Assumes two's complement.  This expression avoids
    686          1.1  skrll 	       overflow if howto->bitsize is the number of bits in
    687          1.1  skrll 	       bfd_vma.  */
    688          1.1  skrll 	    bfd_vma reloc_unsigned_max =
    689          1.1  skrll 	    (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
    690          1.1  skrll 
    691          1.1  skrll 	    if (check > reloc_unsigned_max)
    692          1.1  skrll 	      overflow = TRUE;
    693          1.1  skrll 	  }
    694          1.1  skrll 	  break;
    695          1.1  skrll 	case complain_overflow_bitfield:
    696          1.1  skrll 	  {
    697          1.1  skrll 	    /* Assumes two's complement.  This expression avoids
    698          1.1  skrll 	       overflow if howto->bitsize is the number of bits in
    699          1.1  skrll 	       bfd_vma.  */
    700          1.1  skrll 	    bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
    701          1.1  skrll 
    702          1.1  skrll 	    if ((check & ~reloc_bits) != 0
    703          1.1  skrll 		&& (((bfd_vma) signed_check & ~reloc_bits)
    704          1.1  skrll 		    != (-(bfd_vma) 1 & ~reloc_bits)))
    705          1.1  skrll 	      overflow = TRUE;
    706          1.1  skrll 	  }
    707          1.1  skrll 	  break;
    708          1.1  skrll 	default:
    709          1.1  skrll 	  abort ();
    710          1.1  skrll 	}
    711          1.1  skrll     }
    712          1.1  skrll 
    713          1.1  skrll   /* Put RELOCATION in the right bits.  */
    714          1.1  skrll   relocation >>= (bfd_vma) howto->rightshift;
    715          1.1  skrll   relocation <<= (bfd_vma) howto->bitpos;
    716          1.1  skrll 
    717          1.1  skrll   /* Add RELOCATION to the right bits of X.  */
    718          1.1  skrll   x = ((x & ~howto->dst_mask)
    719          1.1  skrll        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
    720          1.1  skrll 
    721          1.1  skrll   /* Put the relocated value back in the object file.  */
    722          1.1  skrll   switch (size)
    723          1.1  skrll     {
    724          1.1  skrll     default:
    725          1.1  skrll     case 0:
    726          1.1  skrll       abort ();
    727          1.1  skrll     case 1:
    728          1.1  skrll     case 2:
    729          1.1  skrll     case 4:
    730          1.1  skrll #ifdef BFD64
    731          1.1  skrll     case 8:
    732          1.1  skrll #endif
    733          1.1  skrll       put_data (x, location, size);
    734          1.1  skrll       break;
    735          1.1  skrll     }
    736          1.1  skrll 
    737          1.1  skrll   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
    738          1.1  skrll }
    739          1.1  skrll 
    740          1.1  skrll bfd_reloc_status_type
    741  1.1.1.1.6.1   yamt _bfd_ns32k_reloc_disp (bfd *abfd,
    742  1.1.1.1.6.1   yamt 		       arelent *reloc_entry,
    743  1.1.1.1.6.1   yamt 		       struct bfd_symbol *symbol,
    744  1.1.1.1.6.1   yamt 		       void * data,
    745  1.1.1.1.6.1   yamt 		       asection *input_section,
    746  1.1.1.1.6.1   yamt 		       bfd *output_bfd,
    747  1.1.1.1.6.1   yamt 		       char **error_message)
    748          1.1  skrll {
    749          1.1  skrll   return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section,
    750          1.1  skrll 			 output_bfd, error_message,
    751          1.1  skrll 			 _bfd_ns32k_get_displacement,
    752          1.1  skrll 			 _bfd_ns32k_put_displacement);
    753          1.1  skrll }
    754          1.1  skrll 
    755          1.1  skrll bfd_reloc_status_type
    756  1.1.1.1.6.1   yamt _bfd_ns32k_reloc_imm (bfd *abfd,
    757  1.1.1.1.6.1   yamt 		      arelent *reloc_entry,
    758  1.1.1.1.6.1   yamt 		      struct bfd_symbol *symbol,
    759  1.1.1.1.6.1   yamt 		      void * data,
    760  1.1.1.1.6.1   yamt 		      asection *input_section,
    761  1.1.1.1.6.1   yamt 		      bfd *output_bfd,
    762  1.1.1.1.6.1   yamt 		      char **error_message)
    763          1.1  skrll {
    764          1.1  skrll   return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section,
    765          1.1  skrll 			 output_bfd, error_message, _bfd_ns32k_get_immediate,
    766          1.1  skrll 			 _bfd_ns32k_put_immediate);
    767          1.1  skrll }
    768          1.1  skrll 
    769          1.1  skrll bfd_reloc_status_type
    770  1.1.1.1.6.1   yamt _bfd_ns32k_final_link_relocate (reloc_howto_type *howto,
    771  1.1.1.1.6.1   yamt 				bfd *input_bfd,
    772  1.1.1.1.6.1   yamt 				asection *input_section,
    773  1.1.1.1.6.1   yamt 				bfd_byte *contents,
    774  1.1.1.1.6.1   yamt 				bfd_vma address,
    775  1.1.1.1.6.1   yamt 				bfd_vma value,
    776  1.1.1.1.6.1   yamt 				bfd_vma addend)
    777          1.1  skrll {
    778          1.1  skrll   bfd_vma relocation;
    779          1.1  skrll 
    780          1.1  skrll   /* Sanity check the address.  */
    781          1.1  skrll   if (address > bfd_get_section_limit (input_bfd, input_section))
    782          1.1  skrll     return bfd_reloc_outofrange;
    783          1.1  skrll 
    784          1.1  skrll   /* This function assumes that we are dealing with a basic relocation
    785          1.1  skrll      against a symbol.  We want to compute the value of the symbol to
    786          1.1  skrll      relocate to.  This is just VALUE, the value of the symbol, plus
    787          1.1  skrll      ADDEND, any addend associated with the reloc.  */
    788          1.1  skrll   relocation = value + addend;
    789          1.1  skrll 
    790          1.1  skrll   /* If the relocation is PC relative, we want to set RELOCATION to
    791          1.1  skrll      the distance between the symbol (currently in RELOCATION) and the
    792          1.1  skrll      location we are relocating.  Some targets (e.g., i386-aout)
    793          1.1  skrll      arrange for the contents of the section to be the negative of the
    794          1.1  skrll      offset of the location within the section; for such targets
    795          1.1  skrll      pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
    796          1.1  skrll      simply leave the contents of the section as zero; for such
    797          1.1  skrll      targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
    798          1.1  skrll      need to subtract out the offset of the location within the
    799          1.1  skrll      section (which is just ADDRESS).  */
    800          1.1  skrll   if (howto->pc_relative)
    801          1.1  skrll     {
    802          1.1  skrll       relocation -= (input_section->output_section->vma
    803          1.1  skrll 		     + input_section->output_offset);
    804          1.1  skrll       if (howto->pcrel_offset)
    805          1.1  skrll 	relocation -= address;
    806          1.1  skrll     }
    807          1.1  skrll 
    808          1.1  skrll   return _bfd_ns32k_relocate_contents (howto, input_bfd, relocation,
    809          1.1  skrll 				       contents + address);
    810          1.1  skrll }
    811