home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / cpu-ns32k.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-14  |  23.2 KB  |  802 lines

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