home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / cpu-ns32k.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  25KB  |  844 lines

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