Check file exists before completion tests
[deliverable/binutils-gdb.git] / bfd / elf32-avr.c
index 9fb72f583f5be7503da68762cd6bad85b433014c..425e2d1c1ed071d3bc71075c08049f285a20ba3a 100644 (file)
@@ -1,7 +1,5 @@
 /* AVR-specific support for 32-bit ELF
-   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
-   2010, 2011, 2012
-   Free Software Foundation, Inc.
+   Copyright (C) 1999-2014 Free Software Foundation, Inc.
    Contributed by Denis Chertykov <denisc@overta.ru>
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -34,6 +32,15 @@ static bfd_boolean debug_relax = FALSE;
 /* Enable debugging printout at stdout with this variable.  */
 static bfd_boolean debug_stubs = FALSE;
 
+static bfd_reloc_status_type
+bfd_elf_avr_diff_reloc (bfd *abfd,
+              arelent *reloc_entry,
+              asymbol *symbol,
+              void *data,
+              asection *input_section,
+              bfd *output_bfd,
+              char **error_message);
+
 /* Hash table initialization and handling.  Code is taken from the hppa port
    and adapted to the needs of AVR.  */
 
@@ -517,6 +524,87 @@ static reloc_howto_type elf_avr_howto_table[] =
         0x000000ff,            /* src_mask */
         0x000000ff,            /* dst_mask */
         FALSE),                /* pcrel_offset */
+  /* lo8-part to use in  .byte lo8(sym).  */
+  HOWTO (R_AVR_8_LO8,          /* type */
+        0,                     /* rightshift */
+        0,                     /* size (0 = byte, 1 = short, 2 = long) */
+        8,                     /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont,/* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_AVR_8_LO8",         /* name */
+        FALSE,                 /* partial_inplace */
+        0xffffff,              /* src_mask */
+        0xffffff,              /* dst_mask */
+        FALSE),                /* pcrel_offset */
+  /* hi8-part to use in  .byte hi8(sym).  */
+  HOWTO (R_AVR_8_HI8,          /* type */
+        8,                     /* rightshift */
+        0,                     /* size (0 = byte, 1 = short, 2 = long) */
+        8,                     /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont,/* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_AVR_8_HI8",         /* name */
+        FALSE,                 /* partial_inplace */
+        0xffffff,              /* src_mask */
+        0xffffff,              /* dst_mask */
+        FALSE),                /* pcrel_offset */
+  /* hlo8-part to use in  .byte hlo8(sym).  */
+  HOWTO (R_AVR_8_HLO8,         /* type */
+        16,                    /* rightshift */
+        0,                     /* size (0 = byte, 1 = short, 2 = long) */
+        8,                     /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont,/* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_AVR_8_HLO8",        /* name */
+        FALSE,                 /* partial_inplace */
+        0xffffff,              /* src_mask */
+        0xffffff,              /* dst_mask */
+        FALSE),                /* pcrel_offset */
+  HOWTO (R_AVR_DIFF8,      /* type */
+     0,             /* rightshift */
+     0,                        /* size (0 = byte, 1 = short, 2 = long) */
+     8,                        /* bitsize */
+     FALSE,         /* pc_relative */
+     0,             /* bitpos */
+     complain_overflow_bitfield, /* complain_on_overflow */
+     bfd_elf_avr_diff_reloc, /* special_function */
+     "R_AVR_DIFF8",     /* name */
+     FALSE,         /* partial_inplace */
+     0,             /* src_mask */
+     0xff,          /* dst_mask */
+     FALSE),        /* pcrel_offset */
+  HOWTO (R_AVR_DIFF16,     /* type */
+     0,             /* rightshift */
+     1,                        /* size (0 = byte, 1 = short, 2 = long) */
+     16,                       /* bitsize */
+     FALSE,         /* pc_relative */
+     0,             /* bitpos */
+     complain_overflow_bitfield, /* complain_on_overflow */
+     bfd_elf_avr_diff_reloc, /* special_function */
+     "R_AVR_DIFF16",     /* name */
+     FALSE,         /* partial_inplace */
+     0,             /* src_mask */
+     0xffff,        /* dst_mask */
+     FALSE),        /* pcrel_offset */
+  HOWTO (R_AVR_DIFF32,    /* type */
+     0,             /* rightshift */
+     2,         /* size (0 = byte, 1 = short, 2 = long) */
+     32,        /* bitsize */
+     FALSE,         /* pc_relative */
+     0,             /* bitpos */
+     complain_overflow_bitfield, /* complain_on_overflow */
+     bfd_elf_avr_diff_reloc, /* special_function */
+     "R_AVR_DIFF32",     /* name */
+     FALSE,         /* partial_inplace */
+     0,             /* src_mask */
+     0xffffffff,    /* dst_mask */ 
+     FALSE)         /* pcrel_offset */
 };
 
 /* Map BFD reloc types to AVR ELF reloc types.  */
@@ -555,7 +643,13 @@ static const struct avr_reloc_map avr_reloc_map[] =
   { BFD_RELOC_AVR_LDI,              R_AVR_LDI  },
   { BFD_RELOC_AVR_6,                R_AVR_6    },
   { BFD_RELOC_AVR_6_ADIW,           R_AVR_6_ADIW },
-  { BFD_RELOC_8,                    R_AVR_8 }
+  { BFD_RELOC_8,                    R_AVR_8 },
+  { BFD_RELOC_AVR_8_LO,             R_AVR_8_LO8 },
+  { BFD_RELOC_AVR_8_HI,             R_AVR_8_HI8 },
+  { BFD_RELOC_AVR_8_HLO,            R_AVR_8_HLO8 },
+  { BFD_RELOC_AVR_DIFF8,            R_AVR_DIFF8 },
+  { BFD_RELOC_AVR_DIFF16,           R_AVR_DIFF16 },
+  { BFD_RELOC_AVR_DIFF32,           R_AVR_DIFF32 }
 };
 
 /* Meant to be filled one day with the wrap around address for the
@@ -627,7 +721,7 @@ elf32_avr_link_hash_table_create (bfd *abfd)
   struct elf32_avr_link_hash_table *htab;
   bfd_size_type amt = sizeof (*htab);
 
-  htab = bfd_malloc (amt);
+  htab = bfd_zmalloc (amt);
   if (htab == NULL)
     return NULL;
 
@@ -645,15 +739,6 @@ elf32_avr_link_hash_table_create (bfd *abfd)
                             sizeof (struct elf32_avr_stub_hash_entry)))
     return NULL;
 
-  htab->stub_bfd = NULL;
-  htab->stub_sec = NULL;
-
-  /* Initialize the address mapping table.  */
-  htab->amt_stub_offsets = NULL;
-  htab->amt_destination_addr = NULL;
-  htab->amt_entry_cnt = 0;
-  htab->amt_max_entry_cnt = 0;
-
   return &htab->etab.root;
 }
 
@@ -672,7 +757,7 @@ elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
     free (htab->amt_destination_addr);
 
   bfd_hash_table_free (&htab->bstab);
-  _bfd_generic_link_hash_table_free (btab);
+  _bfd_elf_link_hash_table_free (btab);
 }
 
 /* Calculates the effective distance of a pc relative jump/call.  */
@@ -763,6 +848,22 @@ avr_get_stub_addr (bfd_vma srel,
   return 0x020000;
 }
 
+/* Perform a diff relocation. Nothing to do, as the difference value is already
+   written into the section's contents. */
+
+static bfd_reloc_status_type
+bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
+                     arelent *reloc_entry ATTRIBUTE_UNUSED,
+              asymbol *symbol ATTRIBUTE_UNUSED,
+              void *data ATTRIBUTE_UNUSED,
+              asection *input_section ATTRIBUTE_UNUSED,
+              bfd *output_bfd ATTRIBUTE_UNUSED,
+              char **error_message ATTRIBUTE_UNUSED)
+{
+  return bfd_reloc_ok;
+}
+
+
 /* Perform a single relocation.  By default we use the standard BFD
    routines, but a few relocs, we have to do them ourselves.  */
 
@@ -1115,6 +1216,13 @@ avr_final_link_relocate (reloc_howto_type *                 howto,
       bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
       break;
 
+    case R_AVR_DIFF8:
+    case R_AVR_DIFF16:
+    case R_AVR_DIFF32:
+      /* Nothing to do here, as contents already contains the diff value. */
+      r = bfd_reloc_ok;
+      break;
+
     default:
       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
                                    contents, rel->r_offset,
@@ -1180,12 +1288,12 @@ elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
        }
       else
        {
-         bfd_boolean unresolved_reloc, warned;
+         bfd_boolean unresolved_reloc, warned, ignored;
 
          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
                                   r_symndx, symtab_hdr, sym_hashes,
                                   h, sec, relocation,
-                                  unresolved_reloc, warned);
+                                  unresolved_reloc, warned, ignored);
 
          name = h->root.root.string;
        }
@@ -1423,6 +1531,104 @@ elf32_avr_object_p (bfd *abfd)
                                    e_set);
 }
 
+/* Returns whether the relocation type passed is a diff reloc. */
+
+static bfd_boolean
+elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
+{
+  return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
+          ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
+          || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
+}
+
+/* Reduce the diff value written in the section by count if the shrinked 
+   insn address happens to fall between the two symbols for which this 
+   diff reloc was emitted. */
+
+static void
+elf32_avr_adjust_diff_reloc_value (bfd *abfd,
+                                   struct bfd_section *isec,
+                                   Elf_Internal_Rela *irel,
+                                   bfd_vma symval,
+                                   bfd_vma shrinked_insn_address,
+                                   int count)
+{
+  unsigned char *reloc_contents = NULL;
+  unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
+  if (isec_contents == NULL)
+  {
+    if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
+      return;
+
+    elf_section_data (isec)->this_hdr.contents = isec_contents;
+  }
+
+  reloc_contents = isec_contents + irel->r_offset;
+
+  /* Read value written in object file. */
+ bfd_vma x = 0;
+  switch (ELF32_R_TYPE (irel->r_info))
+  {
+  case R_AVR_DIFF8:
+    {
+      x = *reloc_contents;
+      break;
+    }
+  case R_AVR_DIFF16:
+    {
+      x = bfd_get_16 (abfd, reloc_contents);
+      break;
+    }
+  case R_AVR_DIFF32:
+    {
+      x = bfd_get_32 (abfd, reloc_contents);
+      break;
+    }
+  default:
+    {
+      BFD_FAIL();
+    }
+  }
+
+  /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
+     into the object file at the reloc offset. sym2's logical value is
+     symval (<start_of_section>) + reloc addend. Compute the start and end
+     addresses and check if the shrinked insn falls between sym1 and sym2. */
+
+  bfd_vma end_address = symval + irel->r_addend;
+  bfd_vma start_address = end_address - x;
+
+  /* Reduce the diff value by count bytes and write it back into section 
+    contents. */
+
+  if (shrinked_insn_address >= start_address && 
+      shrinked_insn_address <= end_address)
+  {
+    switch (ELF32_R_TYPE (irel->r_info))
+    {
+    case R_AVR_DIFF8:
+      {
+        *reloc_contents = (x - count);
+        break;
+      }
+    case R_AVR_DIFF16:
+      {
+        bfd_put_16 (abfd, (x - count) & 0xFFFF, reloc_contents);
+        break;
+      }
+    case R_AVR_DIFF32:
+      {
+        bfd_put_32 (abfd, (x - count) & 0xFFFFFFFF, reloc_contents);
+        break;
+      }
+    default:
+      {
+        BFD_FAIL();
+      }
+    }
+
+  }
+}
 
 /* Delete some bytes from a section while changing the size of an instruction.
    The parameter "addr" denotes the section-relative offset pointing just
@@ -1513,7 +1719,7 @@ elf32_avr_relax_delete_bytes (bfd *abfd,
        irel = elf_section_data (isec)->relocs;
        /* PR 12161: Read in the relocs for this section if necessary.  */
        if (irel == NULL)
-        irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, FALSE);
+         irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
 
        for (irelend = irel + isec->reloc_count;
             irel < irelend;
@@ -1561,6 +1767,14 @@ elf32_avr_relax_delete_bytes (bfd *abfd,
                    if (symval <= shrinked_insn_address
                        && (symval + irel->r_addend) > shrinked_insn_address)
                      {
+                       if (elf32_avr_is_diff_reloc (irel))
+                         {
+                           elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
+                                                         symval,
+                                                         shrinked_insn_address,
+                                                        count);
+                         }
+
                        irel->r_addend -= count;
 
                        if (debug_relax)
@@ -1572,9 +1786,6 @@ elf32_avr_relax_delete_bytes (bfd *abfd,
           /* else...Reference symbol is extern.  No need for adjusting
              the addend.  */
         }
-
-       if (elf_section_data (isec)->relocs == NULL)
-        free (irelend - isec->reloc_count);
      }
   }
 
@@ -1661,8 +1872,8 @@ elf32_avr_relax_section (bfd *abfd,
   struct elf32_avr_link_hash_table *htab;
 
   /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
-     relaxing. Such shrinking can cause issues for the sections such 
-     as .vectors and .jumptables. Instead the unused bytes should be 
+     relaxing. Such shrinking can cause issues for the sections such
+     as .vectors and .jumptables. Instead the unused bytes should be
      filled with nop instructions. */
   bfd_boolean shrinkable = TRUE;
 
@@ -1833,7 +2044,7 @@ elf32_avr_relax_section (bfd *abfd,
               distance_short_enough = 1;
             /* If shrinkable, then we can check for a range of distance which
                is two bytes farther on both the directions because the call
-               or jump target will be closer by two bytes after the 
+               or jump target will be closer by two bytes after the
                relaxation. */
             else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097))
               distance_short_enough = 1;
@@ -1910,7 +2121,7 @@ elf32_avr_relax_section (bfd *abfd,
                                              R_AVR_13_PCREL);
 
                 /* We should not modify the ordering if 'shrinkable' is
-                   FALSE. */ 
+                   FALSE. */
                 if (!shrinkable)
                   {
                     /* Let's insert a nop.  */
@@ -2105,6 +2316,7 @@ elf32_avr_relax_section (bfd *abfd,
                          irel->r_offset + insn_size;
                         Elf_Internal_Sym *isym, *isymend;
                         unsigned int sec_shndx;
+                       struct bfd_section *isec;
 
                         sec_shndx =
                          _bfd_elf_section_from_bfd_section (abfd, sec);
@@ -2155,80 +2367,85 @@ elf32_avr_relax_section (bfd *abfd,
                                }
                            }
                        }
-                       /* Now we check for relocations pointing to ret.  */
-                       {
-                         Elf_Internal_Rela *rel;
-                         Elf_Internal_Rela *relend;
 
-                         relend = elf_section_data (sec)->relocs
-                           + sec->reloc_count;
+                       /* Now we check for relocations pointing to ret.  */
+                       for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
+                         {
+                           Elf_Internal_Rela *rel;
+                           Elf_Internal_Rela *relend;
+              
+                           rel = elf_section_data (isec)->relocs;
+                           if (rel == NULL)
+                             rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
 
-                         for (rel = elf_section_data (sec)->relocs;
-                              rel < relend; rel++)
-                           {
-                             bfd_vma reloc_target = 0;
+                           relend = rel + isec->reloc_count;
 
-                             /* Read this BFD's local symbols if we haven't
-                                done so already.  */
-                             if (isymbuf == NULL && symtab_hdr->sh_info != 0)
-                               {
-                                 isymbuf = (Elf_Internal_Sym *)
-                                   symtab_hdr->contents;
-                                 if (isymbuf == NULL)
-                                   isymbuf = bfd_elf_get_elf_syms
-                                     (abfd,
-                                      symtab_hdr,
-                                      symtab_hdr->sh_info, 0,
-                                      NULL, NULL, NULL);
-                                 if (isymbuf == NULL)
+                           for (; rel && rel < relend; rel++)
+                             {
+                               bfd_vma reloc_target = 0;
+
+                               /* Read this BFD's local symbols if we haven't
+                                  done so already.  */
+                               if (isymbuf == NULL && symtab_hdr->sh_info != 0)
+                                 {
+                                   isymbuf = (Elf_Internal_Sym *)
+                                     symtab_hdr->contents;
+                                   if (isymbuf == NULL)
+                                     isymbuf = bfd_elf_get_elf_syms
+                                       (abfd,
+                                        symtab_hdr,
+                                        symtab_hdr->sh_info, 0,
+                                        NULL, NULL, NULL);
+                                   if (isymbuf == NULL)
+                                     break;
+                                 }
+
+                               /* Get the value of the symbol referred to
+                                  by the reloc.  */
+                               if (ELF32_R_SYM (rel->r_info)
+                                   < symtab_hdr->sh_info)
+                                 {
+                                   /* A local symbol.  */
+                                   asection *sym_sec;
+
+                                   isym = isymbuf
+                                     + ELF32_R_SYM (rel->r_info);
+                                   sym_sec = bfd_section_from_elf_index
+                                     (abfd, isym->st_shndx);
+                                   symval = isym->st_value;
+
+                                   /* If the reloc is absolute, it will not
+                                      have a symbol or section associated
+                                      with it.  */
+
+                                   if (sym_sec)
+                                     {
+                                       symval +=
+                                         sym_sec->output_section->vma
+                                         + sym_sec->output_offset;
+                                       reloc_target = symval + rel->r_addend;
+                                     }
+                                   else
+                                     {
+                                       reloc_target = symval + rel->r_addend;
+                                       /* Reference symbol is absolute.  */
+                                     }
+                                 }
+                               /* else ... reference symbol is extern.  */
+
+                               if (address_of_ret == reloc_target)
+                                 {
+                                   deleting_ret_is_safe = 0;
+                                   if (debug_relax)
+                                     printf ("ret from "
+                                             "rjmp/jmp ret sequence at address"
+                                             " 0x%x could not be deleted. ret"
+                                             " is target of a relocation.\n",
+                                             (int) address_of_ret);
                                    break;
-                               }
-
-                             /* Get the value of the symbol referred to
-                                by the reloc.  */
-                             if (ELF32_R_SYM (rel->r_info)
-                                 < symtab_hdr->sh_info)
-                               {
-                                 /* A local symbol.  */
-                                 asection *sym_sec;
-
-                                 isym = isymbuf
-                                   + ELF32_R_SYM (rel->r_info);
-                                 sym_sec = bfd_section_from_elf_index
-                                   (abfd, isym->st_shndx);
-                                 symval = isym->st_value;
-
-                                 /* If the reloc is absolute, it will not
-                                    have a symbol or section associated
-                                    with it.  */
-
-                                 if (sym_sec)
-                                   {
-                                     symval +=
-                                       sym_sec->output_section->vma
-                                       + sym_sec->output_offset;
-                                     reloc_target = symval + rel->r_addend;
-                                   }
-                                 else
-                                   {
-                                     reloc_target = symval + rel->r_addend;
-                                     /* Reference symbol is absolute.  */
-                                   }
-                               }
-                             /* else ... reference symbol is extern.  */
-
-                             if (address_of_ret == reloc_target)
-                               {
-                                 deleting_ret_is_safe = 0;
-                                 if (debug_relax)
-                                   printf ("ret from "
-                                           "rjmp/jmp ret sequence at address"
-                                           " 0x%x could not be deleted. ret"
-                                           " is target of a relocation.\n",
-                                           (int) address_of_ret);
-                               }
-                           }
-                       }
+                                 }
+                             }
+                         }
 
                        if (deleting_ret_is_safe)
                          {
@@ -2249,7 +2466,6 @@ elf32_avr_relax_section (bfd *abfd,
                            break;
                          }
                       }
-
                   }
               }
             break;
This page took 0.029164 seconds and 4 git commands to generate.