Back to index

cell-binutils  2.17cvs20070401
Defines | Functions | Variables
reloc.c File Reference
#include "bfd.h"
#include "sysdep.h"
#include "bfdlink.h"
#include "libbfd.h"

Go to the source code of this file.

Defines

#define _BFD_MAKE_TABLE_bfd_reloc_code_real
#define N_ONES(n)   (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
#define DOIT(x)   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
#define DOIT(x)   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))

Functions

unsigned int bfd_get_reloc_size (reloc_howto_type *howto)
bfd_reloc_status_type bfd_check_overflow (enum complain_overflow how, unsigned int bitsize, unsigned int rightshift, unsigned int addrsize, bfd_vma relocation)
bfd_reloc_status_type bfd_perform_relocation (bfd *abfd, arelent *reloc_entry, void *data, asection *input_section, bfd *output_bfd, char **error_message)
bfd_reloc_status_type bfd_install_relocation (bfd *abfd, arelent *reloc_entry, void *data_start, bfd_vma data_start_offset, asection *input_section, char **error_message)
bfd_reloc_status_type _bfd_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd, asection *input_section, bfd_byte *contents, bfd_vma address, bfd_vma value, bfd_vma addend)
bfd_reloc_status_type _bfd_relocate_contents (reloc_howto_type *howto, bfd *input_bfd, bfd_vma relocation, bfd_byte *location)
void _bfd_clear_contents (reloc_howto_type *howto, bfd *input_bfd, bfd_byte *location)
reloc_howto_type * bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
reloc_howto_type * bfd_reloc_name_lookup (bfd *abfd, const char *reloc_name)
reloc_howto_type * bfd_default_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
const char * bfd_get_reloc_code_name (bfd_reloc_code_real_type code)
bfd_boolean bfd_generic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *section ATTRIBUTE_UNUSED, struct bfd_link_info *link_info ATTRIBUTE_UNUSED, bfd_boolean *again)
bfd_boolean bfd_generic_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info ATTRIBUTE_UNUSED)
bfd_boolean bfd_generic_merge_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
bfd_bytebfd_generic_get_relocated_section_contents (bfd *abfd, struct bfd_link_info *link_info, struct bfd_link_order *link_order, bfd_byte *data, bfd_boolean relocatable, asymbol **symbols)

Variables

static reloc_howto_type bfd_howto_32

Define Documentation

Definition at line 48 of file reloc.c.

#define DOIT (   x)    x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
#define DOIT (   x)    x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
#define N_ONES (   n)    (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)

Definition at line 467 of file reloc.c.


Function Documentation

void _bfd_clear_contents ( reloc_howto_type *  howto,
bfd input_bfd,
bfd_byte location 
)

Definition at line 1553 of file reloc.c.

{
  int size;
  bfd_vma x = 0;

  /* Get the value we are going to relocate.  */
  size = bfd_get_reloc_size (howto);
  switch (size)
    {
    default:
    case 0:
      abort ();
    case 1:
      x = bfd_get_8 (input_bfd, location);
      break;
    case 2:
      x = bfd_get_16 (input_bfd, location);
      break;
    case 4:
      x = bfd_get_32 (input_bfd, location);
      break;
    case 8:
#ifdef BFD64
      x = bfd_get_64 (input_bfd, location);
#else
      abort ();
#endif
      break;
    }

  /* Zero out the unwanted bits of X.  */
  x &= ~howto->dst_mask;

  /* Put the relocated value back in the object file.  */
  switch (size)
    {
    default:
    case 0:
      abort ();
    case 1:
      bfd_put_8 (input_bfd, x, location);
      break;
    case 2:
      bfd_put_16 (input_bfd, x, location);
      break;
    case 4:
      bfd_put_32 (input_bfd, x, location);
      break;
    case 8:
#ifdef BFD64
      bfd_put_64 (input_bfd, x, location);
#else
      abort ();
#endif
      break;
    }
}

Here is the call graph for this function:

bfd_reloc_status_type _bfd_final_link_relocate ( reloc_howto_type *  howto,
bfd input_bfd,
asection input_section,
bfd_byte contents,
bfd_vma  address,
bfd_vma  value,
bfd_vma  addend 
)

Definition at line 1334 of file reloc.c.

{
  bfd_vma relocation;

  /* Sanity check the address.  */
  if (address > bfd_get_section_limit (input_bfd, input_section))
    return bfd_reloc_outofrange;

  /* This function assumes that we are dealing with a basic relocation
     against a symbol.  We want to compute the value of the symbol to
     relocate to.  This is just VALUE, the value of the symbol, plus
     ADDEND, any addend associated with the reloc.  */
  relocation = value + addend;

  /* If the relocation is PC relative, we want to set RELOCATION to
     the distance between the symbol (currently in RELOCATION) and the
     location we are relocating.  Some targets (e.g., i386-aout)
     arrange for the contents of the section to be the negative of the
     offset of the location within the section; for such targets
     pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
     simply leave the contents of the section as zero; for such
     targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
     need to subtract out the offset of the location within the
     section (which is just ADDRESS).  */
  if (howto->pc_relative)
    {
      relocation -= (input_section->output_section->vma
                   + input_section->output_offset);
      if (howto->pcrel_offset)
       relocation -= address;
    }

  return _bfd_relocate_contents (howto, input_bfd, relocation,
                             contents + address);
}

Here is the call graph for this function:

bfd_reloc_status_type _bfd_relocate_contents ( reloc_howto_type *  howto,
bfd input_bfd,
bfd_vma  relocation,
bfd_byte location 
)

Definition at line 1379 of file reloc.c.

{
  int size;
  bfd_vma x = 0;
  bfd_reloc_status_type flag;
  unsigned int rightshift = howto->rightshift;
  unsigned int bitpos = howto->bitpos;

  /* If the size is negative, negate RELOCATION.  This isn't very
     general.  */
  if (howto->size < 0)
    relocation = -relocation;

  /* Get the value we are going to relocate.  */
  size = bfd_get_reloc_size (howto);
  switch (size)
    {
    default:
    case 0:
      abort ();
    case 1:
      x = bfd_get_8 (input_bfd, location);
      break;
    case 2:
      x = bfd_get_16 (input_bfd, location);
      break;
    case 4:
      x = bfd_get_32 (input_bfd, location);
      break;
    case 8:
#ifdef BFD64
      x = bfd_get_64 (input_bfd, location);
#else
      abort ();
#endif
      break;
    }

  /* Check for overflow.  FIXME: We may drop bits during the addition
     which we don't check for.  We must either check at every single
     operation, which would be tedious, or we must do the computations
     in a type larger than bfd_vma, which would be inefficient.  */
  flag = bfd_reloc_ok;
  if (howto->complain_on_overflow != complain_overflow_dont)
    {
      bfd_vma addrmask, fieldmask, signmask, ss;
      bfd_vma a, b, sum;

      /* Get the values to be added together.  For signed and unsigned
         relocations, we assume that all values should be truncated to
         the size of an address.  For bitfields, all the bits matter.
         See also bfd_check_overflow.  */
      fieldmask = N_ONES (howto->bitsize);
      signmask = ~fieldmask;
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
      a = (relocation & addrmask) >> rightshift;
      b = (x & howto->src_mask & addrmask) >> bitpos;

      switch (howto->complain_on_overflow)
       {
       case complain_overflow_signed:
         /* If any sign bits are set, all sign bits must be set.
            That is, A must be a valid negative address after
            shifting.  */
         signmask = ~(fieldmask >> 1);
         /* Fall thru */

       case complain_overflow_bitfield:
         /* Much like the signed check, but for a field one bit
            wider.  We allow a bitfield to represent numbers in the
            range -2**n to 2**n-1, where n is the number of bits in the
            field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
            can't overflow, which is exactly what we want.  */
         ss = a & signmask;
         if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
           flag = bfd_reloc_overflow;

         /* We only need this next bit of code if the sign bit of B
             is below the sign bit of A.  This would only happen if
             SRC_MASK had fewer bits than BITSIZE.  Note that if
             SRC_MASK has more bits than BITSIZE, we can get into
             trouble; we would need to verify that B is in range, as
             we do for A above.  */
         ss = ((~howto->src_mask) >> 1) & howto->src_mask;
         ss >>= bitpos;

         /* Set all the bits above the sign bit.  */
         b = (b ^ ss) - ss;

         /* Now we can do the addition.  */
         sum = a + b;

         /* See if the result has the correct sign.  Bits above the
             sign bit are junk now; ignore them.  If the sum is
             positive, make sure we did not have all negative inputs;
             if the sum is negative, make sure we did not have all
             positive inputs.  The test below looks only at the sign
             bits, and it really just
                SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)

            We mask with addrmask here to explicitly allow an address
            wrap-around.  The Linux kernel relies on it, and it is
            the only way to write assembler code which can run when
            loaded at a location 0x80000000 away from the location at
            which it is linked.  */
         if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
           flag = bfd_reloc_overflow;
         break;

       case complain_overflow_unsigned:
         /* Checking for an unsigned overflow is relatively easy:
             trim the addresses and add, and trim the result as well.
             Overflow is normally indicated when the result does not
             fit in the field.  However, we also need to consider the
             case when, e.g., fieldmask is 0x7fffffff or smaller, an
             input is 0x80000000, and bfd_vma is only 32 bits; then we
             will get sum == 0, but there is an overflow, since the
             inputs did not fit in the field.  Instead of doing a
             separate test, we can check for this by or-ing in the
             operands when testing for the sum overflowing its final
             field.  */
         sum = (a + b) & addrmask;
         if ((a | b | sum) & signmask)
           flag = bfd_reloc_overflow;
         break;

       default:
         abort ();
       }
    }

  /* Put RELOCATION in the right bits.  */
  relocation >>= (bfd_vma) rightshift;
  relocation <<= (bfd_vma) bitpos;

  /* Add RELOCATION to the right bits of X.  */
  x = ((x & ~howto->dst_mask)
       | (((x & howto->src_mask) + relocation) & howto->dst_mask));

  /* Put the relocated value back in the object file.  */
  switch (size)
    {
    default:
      abort ();
    case 1:
      bfd_put_8 (input_bfd, x, location);
      break;
    case 2:
      bfd_put_16 (input_bfd, x, location);
      break;
    case 4:
      bfd_put_32 (input_bfd, x, location);
      break;
    case 8:
#ifdef BFD64
      bfd_put_64 (input_bfd, x, location);
#else
      abort ();
#endif
      break;
    }

  return flag;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bfd_reloc_status_type bfd_check_overflow ( enum complain_overflow  how,
unsigned int  bitsize,
unsigned int  rightshift,
unsigned int  addrsize,
bfd_vma  relocation 
)

Definition at line 491 of file reloc.c.

{
  bfd_vma fieldmask, addrmask, signmask, ss, a;
  bfd_reloc_status_type flag = bfd_reloc_ok;

  /* Note: BITSIZE should always be <= ADDRSIZE, but in case it's not,
     we'll be permissive: extra bits in the field mask will
     automatically extend the address mask for purposes of the
     overflow check.  */
  fieldmask = N_ONES (bitsize);
  signmask = ~fieldmask;
  addrmask = N_ONES (addrsize) | fieldmask;
  a = (relocation & addrmask) >> rightshift;;

  switch (how)
    {
    case complain_overflow_dont:
      break;

    case complain_overflow_signed:
      /* If any sign bits are set, all sign bits must be set.  That
         is, A must be a valid negative address after shifting.  */
      signmask = ~ (fieldmask >> 1);
      /* Fall thru */

    case complain_overflow_bitfield:
      /* Bitfields are sometimes signed, sometimes unsigned.  We
        explicitly allow an address wrap too, which means a bitfield
        of n bits is allowed to store -2**n to 2**n-1.  Thus overflow
        if the value has some, but not all, bits set outside the
        field.  */
      ss = a & signmask;
      if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
       flag = bfd_reloc_overflow;
      break;

    case complain_overflow_unsigned:
      /* We have an overflow if the address does not fit in the field.  */
      if ((a & signmask) != 0)
       flag = bfd_reloc_overflow;
      break;

    default:
      abort ();
    }

  return flag;
}

Here is the caller graph for this function:

reloc_howto_type* bfd_default_reloc_type_lookup ( bfd abfd,
bfd_reloc_code_real_type  code 
)

Definition at line 4989 of file reloc.c.

{
  switch (code)
    {
    case BFD_RELOC_CTOR:
      /* The type of reloc used in a ctor, which will be as wide as the
        address - so either a 64, 32, or 16 bitter.  */
      switch (bfd_get_arch_info (abfd)->bits_per_address)
       {
       case 64:
         BFD_FAIL ();
       case 32:
         return &bfd_howto_32;
       case 16:
         BFD_FAIL ();
       default:
         BFD_FAIL ();
       }
    default:
      BFD_FAIL ();
    }
  return NULL;
}

Here is the call graph for this function:

bfd_boolean bfd_generic_gc_sections ( bfd *abfd  ATTRIBUTE_UNUSED,
struct bfd_link_info *info  ATTRIBUTE_UNUSED 
)

Definition at line 5073 of file reloc.c.

{
  return TRUE;
}
bfd_byte* bfd_generic_get_relocated_section_contents ( bfd abfd,
struct bfd_link_info link_info,
struct bfd_link_order link_order,
bfd_byte data,
bfd_boolean  relocatable,
asymbol **  symbols 
)

Definition at line 5119 of file reloc.c.

{
  /* Get enough memory to hold the stuff.  */
  bfd *input_bfd = link_order->u.indirect.section->owner;
  asection *input_section = link_order->u.indirect.section;

  long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  arelent **reloc_vector = NULL;
  long reloc_count;
  bfd_size_type sz;

  if (reloc_size < 0)
    goto error_return;

  reloc_vector = bfd_malloc (reloc_size);
  if (reloc_vector == NULL && reloc_size != 0)
    goto error_return;

  /* Read in the section.  */
  sz = input_section->rawsize ? input_section->rawsize : input_section->size;
  if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
    goto error_return;

  reloc_count = bfd_canonicalize_reloc (input_bfd,
                                   input_section,
                                   reloc_vector,
                                   symbols);
  if (reloc_count < 0)
    goto error_return;

  if (reloc_count > 0)
    {
      arelent **parent;
      for (parent = reloc_vector; *parent != NULL; parent++)
       {
         char *error_message = NULL;
         asymbol *symbol;
         bfd_reloc_status_type r;

         symbol = *(*parent)->sym_ptr_ptr;
         if (symbol->section && elf_discarded_section (symbol->section))
           {
             bfd_byte *p;
             static reloc_howto_type none_howto
              = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
                      "unused", FALSE, 0, 0, FALSE);

             p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
             _bfd_clear_contents ((*parent)->howto, input_bfd, p);
             (*parent)->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
             (*parent)->addend = 0;
             (*parent)->howto = &none_howto;
             r = bfd_reloc_ok;
           }
         else
           r = bfd_perform_relocation (input_bfd,
                                   *parent,
                                   data,
                                   input_section,
                                   relocatable ? abfd : NULL,
                                   &error_message);

         if (relocatable)
           {
             asection *os = input_section->output_section;

             /* A partial link, so keep the relocs.  */
             os->orelocation[os->reloc_count] = *parent;
             os->reloc_count++;
           }

         if (r != bfd_reloc_ok)
           {
             switch (r)
              {
              case bfd_reloc_undefined:
                if (!((*link_info->callbacks->undefined_symbol)
                     (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
                      input_bfd, input_section, (*parent)->address,
                      TRUE)))
                  goto error_return;
                break;
              case bfd_reloc_dangerous:
                BFD_ASSERT (error_message != NULL);
                if (!((*link_info->callbacks->reloc_dangerous)
                     (link_info, error_message, input_bfd, input_section,
                      (*parent)->address)))
                  goto error_return;
                break;
              case bfd_reloc_overflow:
                if (!((*link_info->callbacks->reloc_overflow)
                     (link_info, NULL,
                      bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
                      (*parent)->howto->name, (*parent)->addend,
                      input_bfd, input_section, (*parent)->address)))
                  goto error_return;
                break;
              case bfd_reloc_outofrange:
              default:
                abort ();
                break;
              }

           }
       }
    }
  if (reloc_vector != NULL)
    free (reloc_vector);
  return data;

error_return:
  if (reloc_vector != NULL)
    free (reloc_vector);
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bfd_boolean bfd_generic_merge_sections ( bfd *abfd  ATTRIBUTE_UNUSED,
struct bfd_link_info *link_info  ATTRIBUTE_UNUSED 
)

Definition at line 5093 of file reloc.c.

{
  return TRUE;
}
bfd_boolean bfd_generic_relax_section ( bfd *abfd  ATTRIBUTE_UNUSED,
asection *section  ATTRIBUTE_UNUSED,
struct bfd_link_info *link_info  ATTRIBUTE_UNUSED,
bfd_boolean again 
)

Definition at line 5050 of file reloc.c.

{
  *again = FALSE;
  return TRUE;
}

Definition at line 5026 of file reloc.c.

{
  if (code > BFD_RELOC_UNUSED)
    return 0;
  return bfd_reloc_code_real_names[code];
}

Here is the caller graph for this function:

unsigned int bfd_get_reloc_size ( reloc_howto_type *  howto)

Definition at line 434 of file reloc.c.

{
  switch (howto->size)
    {
    case 0: return 1;
    case 1: return 2;
    case 2: return 4;
    case 3: return 0;
    case 4: return 8;
    case 8: return 16;
    case -2: return 4;
    default: abort ();
    }
}

Here is the caller graph for this function:

bfd_reloc_status_type bfd_install_relocation ( bfd abfd,
arelent reloc_entry,
void *  data_start,
bfd_vma  data_start_offset,
asection input_section,
char **  error_message 
)

Definition at line 966 of file reloc.c.

{
  bfd_vma relocation;
  bfd_reloc_status_type flag = bfd_reloc_ok;
  bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
  bfd_vma output_base = 0;
  reloc_howto_type *howto = reloc_entry->howto;
  asection *reloc_target_output_section;
  asymbol *symbol;
  bfd_byte *data;

  symbol = *(reloc_entry->sym_ptr_ptr);
  if (bfd_is_abs_section (symbol->section))
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  /* If there is a function supplied to handle this relocation type,
     call it.  It'll return `bfd_reloc_continue' if further processing
     can be done.  */
  if (howto->special_function)
    {
      bfd_reloc_status_type cont;

      /* XXX - The special_function calls haven't been fixed up to deal
        with creating new relocations and section contents.  */
      cont = howto->special_function (abfd, reloc_entry, symbol,
                                  /* XXX - Non-portable! */
                                  ((bfd_byte *) data_start
                                   - data_start_offset),
                                  input_section, abfd, error_message);
      if (cont != bfd_reloc_continue)
       return cont;
    }

  /* Is the address of the relocation really within the section?  */
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
    return bfd_reloc_outofrange;

  /* Work out which section the relocation is targeted at and the
     initial relocation command value.  */

  /* Get symbol value.  (Common symbols are special.)  */
  if (bfd_is_com_section (symbol->section))
    relocation = 0;
  else
    relocation = symbol->value;

  reloc_target_output_section = symbol->section->output_section;

  /* Convert input-section-relative symbol value to absolute.  */
  if (! howto->partial_inplace)
    output_base = 0;
  else
    output_base = reloc_target_output_section->vma;

  relocation += output_base + symbol->section->output_offset;

  /* Add in supplied addend.  */
  relocation += reloc_entry->addend;

  /* Here the variable relocation holds the final address of the
     symbol we are relocating against, plus any addend.  */

  if (howto->pc_relative)
    {
      /* This is a PC relative relocation.  We want to set RELOCATION
        to the distance between the address of the symbol and the
        location.  RELOCATION is already the address of the symbol.

        We start by subtracting the address of the section containing
        the location.

        If pcrel_offset is set, we must further subtract the position
        of the location within the section.  Some targets arrange for
        the addend to be the negative of the position of the location
        within the section; for example, i386-aout does this.  For
        i386-aout, pcrel_offset is FALSE.  Some other targets do not
        include the position of the location; for example, m88kbcs,
        or ELF.  For those targets, pcrel_offset is TRUE.

        If we are producing relocatable output, then we must ensure
        that this reloc will be correctly computed when the final
        relocation is done.  If pcrel_offset is FALSE we want to wind
        up with the negative of the location within the section,
        which means we must adjust the existing addend by the change
        in the location within the section.  If pcrel_offset is TRUE
        we do not want to adjust the existing addend at all.

        FIXME: This seems logical to me, but for the case of
        producing relocatable output it is not what the code
        actually does.  I don't want to change it, because it seems
        far too likely that something will break.  */

      relocation -=
       input_section->output_section->vma + input_section->output_offset;

      if (howto->pcrel_offset && howto->partial_inplace)
       relocation -= reloc_entry->address;
    }

  if (! howto->partial_inplace)
    {
      /* This is a partial relocation, and we want to apply the relocation
        to the reloc entry rather than the raw data. Modify the reloc
        inplace to reflect what we now know.  */
      reloc_entry->addend = relocation;
      reloc_entry->address += input_section->output_offset;
      return flag;
    }
  else
    {
      /* This is a partial relocation, but inplace, so modify the
        reloc record a bit.

        If we've relocated with a symbol with a section, change
        into a ref to the section belonging to the symbol.  */
      reloc_entry->address += input_section->output_offset;

      /* WTF?? */
      if (abfd->xvec->flavour == bfd_target_coff_flavour
         && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
         && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
       {

         /* For m68k-coff, the addend was being subtracted twice during
            relocation with -r.  Removing the line below this comment
            fixes that problem; see PR 2953.

However, Ian wrote the following, regarding removing the line below,
which explains why it is still enabled:  --djm

If you put a patch like that into BFD you need to check all the COFF
linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
SCO); see coff_i386_reloc in coff-i386.c where I worked around the
problem in a different way.  There may very well be a reason that the
code works as it does.

Hmmm.  The first obvious point is that bfd_install_relocation should
not have any tests that depend upon the flavour.  It's seem like
entirely the wrong place for such a thing.  The second obvious point
is that the current code ignores the reloc addend when producing
relocatable output for COFF.  That's peculiar.  In fact, I really
have no idea what the point of the line you want to remove is.

A typical COFF reloc subtracts the old value of the symbol and adds in
the new value to the location in the object file (if it's a pc
relative reloc it adds the difference between the symbol value and the
location).  When relocating we need to preserve that property.

BFD handles this by setting the addend to the negative of the old
value of the symbol.  Unfortunately it handles common symbols in a
non-standard way (it doesn't subtract the old value) but that's a
different story (we can't change it without losing backward
compatibility with old object files) (coff-i386 does subtract the old
value, to be compatible with existing coff-i386 targets, like SCO).

So everything works fine when not producing relocatable output.  When
we are producing relocatable output, logically we should do exactly
what we do when not producing relocatable output.  Therefore, your
patch is correct.  In fact, it should probably always just set
reloc_entry->addend to 0 for all cases, since it is, in fact, going to
add the value into the object file.  This won't hurt the COFF code,
which doesn't use the addend; I'm not sure what it will do to other
formats (the thing to check for would be whether any formats both use
the addend and set partial_inplace).

When I wanted to make coff-i386 produce relocatable output, I ran
into the problem that you are running into: I wanted to remove that
line.  Rather than risk it, I made the coff-i386 relocs use a special
function; it's coff_i386_reloc in coff-i386.c.  The function
specifically adds the addend field into the object file, knowing that
bfd_install_relocation is not going to.  If you remove that line, then
coff-i386.c will wind up adding the addend field in twice.  It's
trivial to fix; it just needs to be done.

The problem with removing the line is just that it may break some
working code.  With BFD it's hard to be sure of anything.  The right
way to deal with this is simply to build and test at least all the
supported COFF targets.  It should be straightforward if time and disk
space consuming.  For each target:
    1) build the linker
    2) generate some executable, and link it using -r (I would
       probably use paranoia.o and link against newlib/libc.a, which
       for all the supported targets would be available in
       /usr/cygnus/progressive/H-host/target/lib/libc.a).
    3) make the change to reloc.c
    4) rebuild the linker
    5) repeat step 2
    6) if the resulting object files are the same, you have at least
       made it no worse
    7) if they are different you have to figure out which version is
       right.  */
         relocation -= reloc_entry->addend;
         /* FIXME: There should be no target specific code here...  */
         if (strcmp (abfd->xvec->name, "coff-z8k") != 0)
           reloc_entry->addend = 0;
       }
      else
       {
         reloc_entry->addend = relocation;
       }
    }

  /* FIXME: This overflow checking is incomplete, because the value
     might have overflowed before we get here.  For a correct check we
     need to compute the value in a size larger than bitsize, but we
     can't reasonably do that for a reloc the same size as a host
     machine word.
     FIXME: We should also do overflow checking on the result after
     adding in the value contained in the object file.  */
  if (howto->complain_on_overflow != complain_overflow_dont)
    flag = bfd_check_overflow (howto->complain_on_overflow,
                            howto->bitsize,
                            howto->rightshift,
                            bfd_arch_bits_per_address (abfd),
                            relocation);

  /* Either we are relocating all the way, or we don't want to apply
     the relocation to the reloc entry (probably because there isn't
     any room in the output format to describe addends to relocs).  */

  /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
     (OSF version 1.3, compiler version 3.11).  It miscompiles the
     following program:

     struct str
     {
       unsigned int i0;
     } s = { 0 };

     int
     main ()
     {
       unsigned long x;

       x = 0x100000000;
       x <<= (unsigned long) s.i0;
       if (x == 0)
        printf ("failed\n");
       else
        printf ("succeeded (%lx)\n", x);
     }
     */

  relocation >>= (bfd_vma) howto->rightshift;

  /* Shift everything up to where it's going to be used.  */
  relocation <<= (bfd_vma) howto->bitpos;

  /* Wait for the day when all have the mask in them.  */

  /* What we do:
     i instruction to be left alone
     o offset within instruction
     r relocation offset to apply
     S src mask
     D dst mask
     N ~dst mask
     A part 1
     B part 2
     R result

     Do this:
     ((  i i i i i o o o o o  from bfd_get<size>
     and           S S S S S) to get the size offset we want
     +   r r r r r r r r r r) to get the final value to place
     and           D D D D D  to chop to right size
     -----------------------
     =             A A A A A
     And this:
     (   i i i i i o o o o o  from bfd_get<size>
     and N N N N N          ) get instruction
     -----------------------
     =   B B B B B

     And then:
     (   B B B B B
     or            A A A A A)
     -----------------------
     =   R R R R R R R R R R  put into bfd_put<size>
     */

#define DOIT(x) \
  x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))

  data = (bfd_byte *) data_start + (octets - data_start_offset);

  switch (howto->size)
    {
    case 0:
      {
       char x = bfd_get_8 (abfd, data);
       DOIT (x);
       bfd_put_8 (abfd, x, data);
      }
      break;

    case 1:
      {
       short x = bfd_get_16 (abfd, data);
       DOIT (x);
       bfd_put_16 (abfd, (bfd_vma) x, data);
      }
      break;
    case 2:
      {
       long x = bfd_get_32 (abfd, data);
       DOIT (x);
       bfd_put_32 (abfd, (bfd_vma) x, data);
      }
      break;
    case -2:
      {
       long x = bfd_get_32 (abfd, data);
       relocation = -relocation;
       DOIT (x);
       bfd_put_32 (abfd, (bfd_vma) x, data);
      }
      break;

    case 3:
      /* Do nothing */
      break;

    case 4:
      {
       bfd_vma x = bfd_get_64 (abfd, data);
       DOIT (x);
       bfd_put_64 (abfd, x, data);
      }
      break;
    default:
      return bfd_reloc_other;
    }

  return flag;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bfd_reloc_status_type bfd_perform_relocation ( bfd abfd,
arelent reloc_entry,
void *  data,
asection input_section,
bfd output_bfd,
char **  error_message 
)

Definition at line 576 of file reloc.c.

{
  bfd_vma relocation;
  bfd_reloc_status_type flag = bfd_reloc_ok;
  bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
  bfd_vma output_base = 0;
  reloc_howto_type *howto = reloc_entry->howto;
  asection *reloc_target_output_section;
  asymbol *symbol;

  symbol = *(reloc_entry->sym_ptr_ptr);
  if (bfd_is_abs_section (symbol->section)
      && output_bfd != NULL)
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  /* If we are not producing relocatable output, return an error if
     the symbol is not defined.  An undefined weak symbol is
     considered to have a value of zero (SVR4 ABI, p. 4-27).  */
  if (bfd_is_und_section (symbol->section)
      && (symbol->flags & BSF_WEAK) == 0
      && output_bfd == NULL)
    flag = bfd_reloc_undefined;

  /* If there is a function supplied to handle this relocation type,
     call it.  It'll return `bfd_reloc_continue' if further processing
     can be done.  */
  if (howto->special_function)
    {
      bfd_reloc_status_type cont;
      cont = howto->special_function (abfd, reloc_entry, symbol, data,
                                  input_section, output_bfd,
                                  error_message);
      if (cont != bfd_reloc_continue)
       return cont;
    }

  /* Is the address of the relocation really within the section?  */
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
    return bfd_reloc_outofrange;

  /* Work out which section the relocation is targeted at and the
     initial relocation command value.  */

  /* Get symbol value.  (Common symbols are special.)  */
  if (bfd_is_com_section (symbol->section))
    relocation = 0;
  else
    relocation = symbol->value;

  reloc_target_output_section = symbol->section->output_section;

  /* Convert input-section-relative symbol value to absolute.  */
  if ((output_bfd && ! howto->partial_inplace)
      || reloc_target_output_section == NULL)
    output_base = 0;
  else
    output_base = reloc_target_output_section->vma;

  relocation += output_base + symbol->section->output_offset;

  /* Add in supplied addend.  */
  relocation += reloc_entry->addend;

  /* Here the variable relocation holds the final address of the
     symbol we are relocating against, plus any addend.  */

  if (howto->pc_relative)
    {
      /* This is a PC relative relocation.  We want to set RELOCATION
        to the distance between the address of the symbol and the
        location.  RELOCATION is already the address of the symbol.

        We start by subtracting the address of the section containing
        the location.

        If pcrel_offset is set, we must further subtract the position
        of the location within the section.  Some targets arrange for
        the addend to be the negative of the position of the location
        within the section; for example, i386-aout does this.  For
        i386-aout, pcrel_offset is FALSE.  Some other targets do not
        include the position of the location; for example, m88kbcs,
        or ELF.  For those targets, pcrel_offset is TRUE.

        If we are producing relocatable output, then we must ensure
        that this reloc will be correctly computed when the final
        relocation is done.  If pcrel_offset is FALSE we want to wind
        up with the negative of the location within the section,
        which means we must adjust the existing addend by the change
        in the location within the section.  If pcrel_offset is TRUE
        we do not want to adjust the existing addend at all.

        FIXME: This seems logical to me, but for the case of
        producing relocatable output it is not what the code
        actually does.  I don't want to change it, because it seems
        far too likely that something will break.  */

      relocation -=
       input_section->output_section->vma + input_section->output_offset;

      if (howto->pcrel_offset)
       relocation -= reloc_entry->address;
    }

  if (output_bfd != NULL)
    {
      if (! howto->partial_inplace)
       {
         /* This is a partial relocation, and we want to apply the relocation
            to the reloc entry rather than the raw data. Modify the reloc
            inplace to reflect what we now know.  */
         reloc_entry->addend = relocation;
         reloc_entry->address += input_section->output_offset;
         return flag;
       }
      else
       {
         /* This is a partial relocation, but inplace, so modify the
            reloc record a bit.

            If we've relocated with a symbol with a section, change
            into a ref to the section belonging to the symbol.  */

         reloc_entry->address += input_section->output_offset;

         /* WTF?? */
         if (abfd->xvec->flavour == bfd_target_coff_flavour
             && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
             && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
           {
             /* For m68k-coff, the addend was being subtracted twice during
               relocation with -r.  Removing the line below this comment
               fixes that problem; see PR 2953.

However, Ian wrote the following, regarding removing the line below,
which explains why it is still enabled:  --djm

If you put a patch like that into BFD you need to check all the COFF
linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
SCO); see coff_i386_reloc in coff-i386.c where I worked around the
problem in a different way.  There may very well be a reason that the
code works as it does.

Hmmm.  The first obvious point is that bfd_perform_relocation should
not have any tests that depend upon the flavour.  It's seem like
entirely the wrong place for such a thing.  The second obvious point
is that the current code ignores the reloc addend when producing
relocatable output for COFF.  That's peculiar.  In fact, I really
have no idea what the point of the line you want to remove is.

A typical COFF reloc subtracts the old value of the symbol and adds in
the new value to the location in the object file (if it's a pc
relative reloc it adds the difference between the symbol value and the
location).  When relocating we need to preserve that property.

BFD handles this by setting the addend to the negative of the old
value of the symbol.  Unfortunately it handles common symbols in a
non-standard way (it doesn't subtract the old value) but that's a
different story (we can't change it without losing backward
compatibility with old object files) (coff-i386 does subtract the old
value, to be compatible with existing coff-i386 targets, like SCO).

So everything works fine when not producing relocatable output.  When
we are producing relocatable output, logically we should do exactly
what we do when not producing relocatable output.  Therefore, your
patch is correct.  In fact, it should probably always just set
reloc_entry->addend to 0 for all cases, since it is, in fact, going to
add the value into the object file.  This won't hurt the COFF code,
which doesn't use the addend; I'm not sure what it will do to other
formats (the thing to check for would be whether any formats both use
the addend and set partial_inplace).

When I wanted to make coff-i386 produce relocatable output, I ran
into the problem that you are running into: I wanted to remove that
line.  Rather than risk it, I made the coff-i386 relocs use a special
function; it's coff_i386_reloc in coff-i386.c.  The function
specifically adds the addend field into the object file, knowing that
bfd_perform_relocation is not going to.  If you remove that line, then
coff-i386.c will wind up adding the addend field in twice.  It's
trivial to fix; it just needs to be done.

The problem with removing the line is just that it may break some
working code.  With BFD it's hard to be sure of anything.  The right
way to deal with this is simply to build and test at least all the
supported COFF targets.  It should be straightforward if time and disk
space consuming.  For each target:
    1) build the linker
    2) generate some executable, and link it using -r (I would
       probably use paranoia.o and link against newlib/libc.a, which
       for all the supported targets would be available in
       /usr/cygnus/progressive/H-host/target/lib/libc.a).
    3) make the change to reloc.c
    4) rebuild the linker
    5) repeat step 2
    6) if the resulting object files are the same, you have at least
       made it no worse
    7) if they are different you have to figure out which version is
       right
*/
             relocation -= reloc_entry->addend;
             reloc_entry->addend = 0;
           }
         else
           {
             reloc_entry->addend = relocation;
           }
       }
    }
  else
    {
      reloc_entry->addend = 0;
    }

  /* FIXME: This overflow checking is incomplete, because the value
     might have overflowed before we get here.  For a correct check we
     need to compute the value in a size larger than bitsize, but we
     can't reasonably do that for a reloc the same size as a host
     machine word.
     FIXME: We should also do overflow checking on the result after
     adding in the value contained in the object file.  */
  if (howto->complain_on_overflow != complain_overflow_dont
      && flag == bfd_reloc_ok)
    flag = bfd_check_overflow (howto->complain_on_overflow,
                            howto->bitsize,
                            howto->rightshift,
                            bfd_arch_bits_per_address (abfd),
                            relocation);

  /* Either we are relocating all the way, or we don't want to apply
     the relocation to the reloc entry (probably because there isn't
     any room in the output format to describe addends to relocs).  */

  /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
     (OSF version 1.3, compiler version 3.11).  It miscompiles the
     following program:

     struct str
     {
       unsigned int i0;
     } s = { 0 };

     int
     main ()
     {
       unsigned long x;

       x = 0x100000000;
       x <<= (unsigned long) s.i0;
       if (x == 0)
        printf ("failed\n");
       else
        printf ("succeeded (%lx)\n", x);
     }
     */

  relocation >>= (bfd_vma) howto->rightshift;

  /* Shift everything up to where it's going to be used.  */
  relocation <<= (bfd_vma) howto->bitpos;

  /* Wait for the day when all have the mask in them.  */

  /* What we do:
     i instruction to be left alone
     o offset within instruction
     r relocation offset to apply
     S src mask
     D dst mask
     N ~dst mask
     A part 1
     B part 2
     R result

     Do this:
     ((  i i i i i o o o o o  from bfd_get<size>
     and           S S S S S) to get the size offset we want
     +   r r r r r r r r r r) to get the final value to place
     and           D D D D D  to chop to right size
     -----------------------
     =             A A A A A
     And this:
     (   i i i i i o o o o o  from bfd_get<size>
     and N N N N N          ) get instruction
     -----------------------
     =   B B B B B

     And then:
     (   B B B B B
     or            A A A A A)
     -----------------------
     =   R R R R R R R R R R  put into bfd_put<size>
     */

#define DOIT(x) \
  x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))

  switch (howto->size)
    {
    case 0:
      {
       char x = bfd_get_8 (abfd, (char *) data + octets);
       DOIT (x);
       bfd_put_8 (abfd, x, (unsigned char *) data + octets);
      }
      break;

    case 1:
      {
       short x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
       DOIT (x);
       bfd_put_16 (abfd, (bfd_vma) x, (unsigned char *) data + octets);
      }
      break;
    case 2:
      {
       long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
       DOIT (x);
       bfd_put_32 (abfd, (bfd_vma) x, (bfd_byte *) data + octets);
      }
      break;
    case -2:
      {
       long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
       relocation = -relocation;
       DOIT (x);
       bfd_put_32 (abfd, (bfd_vma) x, (bfd_byte *) data + octets);
      }
      break;

    case -1:
      {
       long x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
       relocation = -relocation;
       DOIT (x);
       bfd_put_16 (abfd, (bfd_vma) x, (bfd_byte *) data + octets);
      }
      break;

    case 3:
      /* Do nothing */
      break;

    case 4:
#ifdef BFD64
      {
       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + octets);
       DOIT (x);
       bfd_put_64 (abfd, x, (bfd_byte *) data + octets);
      }
#else
      abort ();
#endif
      break;
    default:
      return bfd_reloc_other;
    }

  return flag;
}

Here is the call graph for this function:

Here is the caller graph for this function:

reloc_howto_type* bfd_reloc_name_lookup ( bfd abfd,
const char *  reloc_name 
)

Definition at line 4967 of file reloc.c.

{
  return BFD_SEND (abfd, reloc_name_lookup, (abfd, reloc_name));
}

Here is the call graph for this function:

reloc_howto_type* bfd_reloc_type_lookup ( bfd abfd,
bfd_reloc_code_real_type  code 
)

Definition at line 4961 of file reloc.c.

{
  return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
}

Here is the call graph for this function:


Variable Documentation

reloc_howto_type bfd_howto_32 [static]
Initial value:
HOWTO (0, 00, 2, 32, FALSE, 0, complain_overflow_dont, 0, "VRT32", FALSE, 0xffffffff, 0xffffffff, TRUE)

Definition at line 4972 of file reloc.c.