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