gdb: fix vfork with multiple threads
[deliverable/binutils-gdb.git] / bfd / elf32-spu.c
index 94de8fadf59bcd3c55da01ac239dabe53ea3fb3e..c3ce2c16d8063ccc6f008e910adc1fe0ac9b983f 100644 (file)
@@ -1,6 +1,6 @@
 /* SPU specific support for 32-bit ELF
 
-   Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2006-2021 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -27,6 +27,9 @@
 #include "elf/spu.h"
 #include "elf32-spu.h"
 
+/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
+#define OCTETS_PER_BYTE(ABFD, SEC) 1
+
 /* We use RELA style relocs.  Don't define USE_REL.  */
 
 static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
@@ -37,57 +40,60 @@ static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
    array, so it must be declared in the order of that type.  */
 
 static reloc_howto_type elf_howto_table[] = {
-  HOWTO (R_SPU_NONE,       0, 0,  0, FALSE,  0, complain_overflow_dont,
+  HOWTO (R_SPU_NONE,      0, 3,  0, false,  0, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_NONE",
-        FALSE, 0, 0x00000000, FALSE),
-  HOWTO (R_SPU_ADDR10,     4, 2, 10, FALSE, 14, complain_overflow_bitfield,
+        false, 0, 0x00000000, false),
+  HOWTO (R_SPU_ADDR10,    4, 2, 10, false, 14, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_ADDR10",
-        FALSE, 0, 0x00ffc000, FALSE),
-  HOWTO (R_SPU_ADDR16,     2, 2, 16, FALSE,  7, complain_overflow_bitfield,
+        false, 0, 0x00ffc000, false),
+  HOWTO (R_SPU_ADDR16,    2, 2, 16, false,  7, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_ADDR16",
-        FALSE, 0, 0x007fff80, FALSE),
-  HOWTO (R_SPU_ADDR16_HI, 16, 2, 16, FALSE,  7, complain_overflow_bitfield,
+        false, 0, 0x007fff80, false),
+  HOWTO (R_SPU_ADDR16_HI, 16, 2, 16, false,  7, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_ADDR16_HI",
-        FALSE, 0, 0x007fff80, FALSE),
-  HOWTO (R_SPU_ADDR16_LO,  0, 2, 16, FALSE,  7, complain_overflow_dont,
+        false, 0, 0x007fff80, false),
+  HOWTO (R_SPU_ADDR16_LO,  0, 2, 16, false,  7, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_ADDR16_LO",
-        FALSE, 0, 0x007fff80, FALSE),
-  HOWTO (R_SPU_ADDR18,     0, 2, 18, FALSE,  7, complain_overflow_bitfield,
+        false, 0, 0x007fff80, false),
+  HOWTO (R_SPU_ADDR18,    0, 2, 18, false,  7, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_ADDR18",
-        FALSE, 0, 0x01ffff80, FALSE),
-  HOWTO (R_SPU_ADDR32,     0, 2, 32, FALSE,  0, complain_overflow_dont,
+        false, 0, 0x01ffff80, false),
+  HOWTO (R_SPU_ADDR32,    0, 2, 32, false,  0, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_ADDR32",
-        FALSE, 0, 0xffffffff, FALSE),
-  HOWTO (R_SPU_REL16,      2, 2, 16,  TRUE,  7, complain_overflow_bitfield,
+        false, 0, 0xffffffff, false),
+  HOWTO (R_SPU_REL16,     2, 2, 16,  true,  7, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_REL16",
-        FALSE, 0, 0x007fff80, TRUE),
-  HOWTO (R_SPU_ADDR7,      0, 2,  7, FALSE, 14, complain_overflow_dont,
+        false, 0, 0x007fff80, true),
+  HOWTO (R_SPU_ADDR7,     0, 2,  7, false, 14, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_ADDR7",
-        FALSE, 0, 0x001fc000, FALSE),
-  HOWTO (R_SPU_REL9,       2, 2,  9,  TRUE,  0, complain_overflow_signed,
-        spu_elf_rel9,          "SPU_REL9",
-        FALSE, 0, 0x0180007f, TRUE),
-  HOWTO (R_SPU_REL9I,      2, 2,  9,  TRUE,  0, complain_overflow_signed,
-        spu_elf_rel9,          "SPU_REL9I",
-        FALSE, 0, 0x0000c07f, TRUE),
-  HOWTO (R_SPU_ADDR10I,    0, 2, 10, FALSE, 14, complain_overflow_signed,
+        false, 0, 0x001fc000, false),
+  HOWTO (R_SPU_REL9,      2, 2,  9,  true,  0, complain_overflow_signed,
+        spu_elf_rel9,          "SPU_REL9",
+        false, 0, 0x0180007f, true),
+  HOWTO (R_SPU_REL9I,     2, 2,  9,  true,  0, complain_overflow_signed,
+        spu_elf_rel9,          "SPU_REL9I",
+        false, 0, 0x0000c07f, true),
+  HOWTO (R_SPU_ADDR10I,           0, 2, 10, false, 14, complain_overflow_signed,
         bfd_elf_generic_reloc, "SPU_ADDR10I",
-        FALSE, 0, 0x00ffc000, FALSE),
-  HOWTO (R_SPU_ADDR16I,    0, 2, 16, FALSE,  7, complain_overflow_signed,
+        false, 0, 0x00ffc000, false),
+  HOWTO (R_SPU_ADDR16I,           0, 2, 16, false,  7, complain_overflow_signed,
         bfd_elf_generic_reloc, "SPU_ADDR16I",
-        FALSE, 0, 0x007fff80, FALSE),
-  HOWTO (R_SPU_REL32,      0, 2, 32, TRUE,  0, complain_overflow_dont,
+        false, 0, 0x007fff80, false),
+  HOWTO (R_SPU_REL32,     0, 2, 32, true,  0, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_REL32",
-        FALSE, 0, 0xffffffff, TRUE),
-  HOWTO (R_SPU_ADDR16X,    0, 2, 16, FALSE,  7, complain_overflow_bitfield,
+        false, 0, 0xffffffff, true),
+  HOWTO (R_SPU_ADDR16X,           0, 2, 16, false,  7, complain_overflow_bitfield,
         bfd_elf_generic_reloc, "SPU_ADDR16X",
-        FALSE, 0, 0x007fff80, FALSE),
-  HOWTO (R_SPU_PPU32,      0, 2, 32, FALSE,  0, complain_overflow_dont,
+        false, 0, 0x007fff80, false),
+  HOWTO (R_SPU_PPU32,     0, 2, 32, false,  0, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_PPU32",
-        FALSE, 0, 0xffffffff, FALSE),
-  HOWTO (R_SPU_PPU64,      0, 4, 64, FALSE,  0, complain_overflow_dont,
+        false, 0, 0xffffffff, false),
+  HOWTO (R_SPU_PPU64,     0, 4, 64, false,  0, complain_overflow_dont,
         bfd_elf_generic_reloc, "SPU_PPU64",
-        FALSE, 0, -1, FALSE),
+        false, 0, -1, false),
+  HOWTO (R_SPU_ADD_PIC,             0, 0, 0, false,  0, complain_overflow_dont,
+        bfd_elf_generic_reloc, "SPU_ADD_PIC",
+        false, 0, 0x00000000, false),
 };
 
 static struct bfd_elf_special_section const spu_elf_special_sections[] = {
@@ -102,6 +108,8 @@ spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
   switch (code)
     {
     default:
+      return (enum elf_spu_reloc_type) -1;
+    case BFD_RELOC_NONE:
       return R_SPU_NONE;
     case BFD_RELOC_SPU_IMM10W:
       return R_SPU_ADDR10;
@@ -135,19 +143,30 @@ spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
       return R_SPU_PPU32;
     case BFD_RELOC_SPU_PPU64:
       return R_SPU_PPU64;
+    case BFD_RELOC_SPU_ADD_PIC:
+      return R_SPU_ADD_PIC;
     }
 }
 
-static void
-spu_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
+static bool
+spu_elf_info_to_howto (bfd *abfd,
                       arelent *cache_ptr,
                       Elf_Internal_Rela *dst)
 {
   enum elf_spu_reloc_type r_type;
 
   r_type = (enum elf_spu_reloc_type) ELF32_R_TYPE (dst->r_info);
-  BFD_ASSERT (r_type < R_SPU_max);
+  /* PR 17512: file: 90c2a92e.  */
+  if (r_type >= R_SPU_max)
+    {
+      /* xgettext:c-format */
+      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
+                         abfd, r_type);
+      bfd_set_error (bfd_error_bad_value);
+      return false;
+    }
   cache_ptr->howto = &elf_howto_table[(int) r_type];
+  return true;
 }
 
 static reloc_howto_type *
@@ -156,7 +175,7 @@ spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 {
   enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code);
 
-  if (r_type == R_SPU_NONE)
+  if (r_type == (enum elf_spu_reloc_type) -1)
     return NULL;
 
   return elf_howto_table + r_type;
@@ -196,7 +215,7 @@ spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
 
   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
     return bfd_reloc_outofrange;
-  octets = reloc_entry->address * bfd_octets_per_byte (abfd);
+  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
 
   /* Get symbol value.  */
   val = 0;
@@ -225,7 +244,7 @@ spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   return bfd_reloc_ok;
 }
 
-static bfd_boolean
+static bool
 spu_elf_new_section_hook (bfd *abfd, asection *sec)
 {
   if (!sec->used_by_bfd)
@@ -234,7 +253,7 @@ spu_elf_new_section_hook (bfd *abfd, asection *sec)
 
       sdata = bfd_zalloc (abfd, sizeof (*sdata));
       if (sdata == NULL)
-       return FALSE;
+       return false;
       sec->used_by_bfd = sdata;
     }
 
@@ -243,7 +262,7 @@ spu_elf_new_section_hook (bfd *abfd, asection *sec)
 
 /* Set up overlay info for executables.  */
 
-static bfd_boolean
+static bool
 spu_elf_object_p (bfd *abfd)
 {
   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
@@ -267,7 +286,8 @@ spu_elf_object_p (bfd *abfd)
              {
                Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[j];
 
-               if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (shdr, phdr))
+               if (ELF_SECTION_SIZE (shdr, phdr) != 0
+                   && ELF_SECTION_IN_SEGMENT (shdr, phdr))
                  {
                    asection *sec = shdr->bfd_section;
                    spu_elf_section_data (sec)->u.o.ovl_index = num_ovl;
@@ -276,7 +296,7 @@ spu_elf_object_p (bfd *abfd)
              }
          }
     }
-  return TRUE;
+  return true;
 }
 
 /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
@@ -287,7 +307,7 @@ spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
 {
   if (sym->name != NULL
       && sym->section != bfd_abs_section_ptr
-      && strncmp (sym->name, "_EAR_", 5) == 0)
+      && startswith (sym->name, "_EAR_"))
     sym->flags |= BSF_KEEP;
 }
 
@@ -311,9 +331,7 @@ struct spu_link_hash_table
   /* The stub section for each overlay section.  */
   asection **stub_sec;
 
-  struct elf_link_hash_entry *ovly_load;
-  struct elf_link_hash_entry *ovly_return;
-  unsigned long ovly_load_r_symndx;
+  struct elf_link_hash_entry *ovly_entry[2];
 
   /* Number of overlay buffers.  */
   unsigned int num_buf;
@@ -324,22 +342,17 @@ struct spu_link_hash_table
   /* For soft icache.  */
   unsigned int line_size_log2;
   unsigned int num_lines_log2;
+  unsigned int fromelem_size_log2;
 
   /* How much memory we have.  */
   unsigned int local_store;
-  /* Local store --auto-overlay should reserve for non-overlay
-     functions and data.  */
-  unsigned int overlay_fixed;
-  /* Local store --auto-overlay should reserve for stack and heap.  */
-  unsigned int reserved;
-  /* If reserved is not specified, stack analysis will calculate a value
-     for the stack.  This parameter adjusts that value to allow for
-     negative sp access (the ABI says 2000 bytes below sp are valid,
-     and the overlay manager uses some of this area).  */
-  int extra_stack_space;
+
   /* Count of overlay stubs needed in non-overlay area.  */
   unsigned int non_ovly_stub;
 
+  /* Pointer to the fixup section */
+  asection *sfixup;
+
   /* Set on error.  */
   unsigned int stub_err : 1;
 };
@@ -358,7 +371,9 @@ struct got_entry
 };
 
 #define spu_hash_table(p) \
-  ((struct spu_link_hash_table *) ((p)->hash))
+  ((is_elf_hash_table ((p)->hash)                                      \
+    && elf_hash_table_id (elf_hash_table (p)) == SPU_ELF_DATA)         \
+   ? (struct spu_link_hash_table *) (p)->hash : NULL)
 
 struct call_info
 {
@@ -368,6 +383,7 @@ struct call_info
   unsigned int max_depth;
   unsigned int is_tail : 1;
   unsigned int is_pasted : 1;
+  unsigned int broken_cycle : 1;
   unsigned int priority : 13;
 };
 
@@ -438,21 +454,19 @@ spu_elf_link_hash_table_create (bfd *abfd)
 {
   struct spu_link_hash_table *htab;
 
-  htab = bfd_malloc (sizeof (*htab));
+  htab = bfd_zmalloc (sizeof (*htab));
   if (htab == NULL)
     return NULL;
 
   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd,
                                      _bfd_elf_link_hash_newfunc,
-                                     sizeof (struct elf_link_hash_entry)))
+                                     sizeof (struct elf_link_hash_entry),
+                                     SPU_ELF_DATA))
     {
       free (htab);
       return NULL;
     }
 
-  memset (&htab->ovtab, 0,
-         sizeof (*htab) - offsetof (struct spu_link_hash_table, ovtab));
-
   htab->elf.init_got_refcount.refcount = 0;
   htab->elf.init_got_refcount.glist = NULL;
   htab->elf.init_got_offset.offset = 0;
@@ -463,17 +477,25 @@ spu_elf_link_hash_table_create (bfd *abfd)
 void
 spu_elf_setup (struct bfd_link_info *info, struct spu_elf_params *params)
 {
+  bfd_vma max_branch_log2;
+
   struct spu_link_hash_table *htab = spu_hash_table (info);
   htab->params = params;
   htab->line_size_log2 = bfd_log2 (htab->params->line_size);
   htab->num_lines_log2 = bfd_log2 (htab->params->num_lines);
+
+  /* For the software i-cache, we provide a "from" list whose size
+     is a power-of-two number of quadwords, big enough to hold one
+     byte per outgoing branch.  Compute this number here.  */
+  max_branch_log2 = bfd_log2 (htab->params->max_branch);
+  htab->fromelem_size_log2 = max_branch_log2 > 4 ? max_branch_log2 - 4 : 0;
 }
 
 /* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
    to (hash, NULL) for global symbols, and (NULL, sym) for locals.  Set
    *SYMSECP to the symbol's section.  *LOCSYMSP caches local syms.  */
 
-static bfd_boolean
+static bool
 get_sym_h (struct elf_link_hash_entry **hp,
           Elf_Internal_Sym **symp,
           asection **symsecp,
@@ -521,7 +543,7 @@ get_sym_h (struct elf_link_hash_entry **hp,
                                            symtab_hdr->sh_info,
                                            0, NULL, NULL, NULL);
          if (locsyms == NULL)
-           return FALSE;
+           return false;
          *locsymsp = locsyms;
        }
       sym = locsyms + r_symndx;
@@ -536,18 +558,19 @@ get_sym_h (struct elf_link_hash_entry **hp,
        *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
     }
 
-  return TRUE;
+  return true;
 }
 
 /* Create the note section if not already present.  This is done early so
    that the linker maps the sections to the right place in the output.  */
 
-bfd_boolean
+bool
 spu_elf_create_sections (struct bfd_link_info *info)
 {
+  struct spu_link_hash_table *htab = spu_hash_table (info);
   bfd *ibfd;
 
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL)
       break;
 
@@ -561,22 +584,27 @@ spu_elf_create_sections (struct bfd_link_info *info)
       flagword flags;
 
       ibfd = info->input_bfds;
+      /* This should really be SEC_LINKER_CREATED, but then we'd need
+        to write out the section ourselves.  */
       flags = SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
       s = bfd_make_section_anyway_with_flags (ibfd, SPU_PTNOTE_SPUNAME, flags);
       if (s == NULL
-         || !bfd_set_section_alignment (ibfd, s, 4))
-       return FALSE;
+         || !bfd_set_section_alignment (s, 4))
+       return false;
+      /* Because we didn't set SEC_LINKER_CREATED we need to set the
+        proper section type.  */
+      elf_section_type (s) = SHT_NOTE;
 
       name_len = strlen (bfd_get_filename (info->output_bfd)) + 1;
       size = 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4);
       size += (name_len + 3) & -4;
 
-      if (!bfd_set_section_size (ibfd, s, size))
-       return FALSE;
+      if (!bfd_set_section_size (s, size))
+       return false;
 
       data = bfd_zalloc (ibfd, size);
       if (data == NULL)
-       return FALSE;
+       return false;
 
       bfd_put_32 (ibfd, sizeof (SPU_PLUGIN_NAME), data + 0);
       bfd_put_32 (ibfd, name_len, data + 4);
@@ -587,7 +615,23 @@ spu_elf_create_sections (struct bfd_link_info *info)
       s->contents = data;
     }
 
-  return TRUE;
+  if (htab->params->emit_fixups)
+    {
+      asection *s;
+      flagword flags;
+
+      if (htab->elf.dynobj == NULL)
+       htab->elf.dynobj = ibfd;
+      ibfd = htab->elf.dynobj;
+      flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY | SEC_HAS_CONTENTS
+              | SEC_IN_MEMORY | SEC_LINKER_CREATED);
+      s = bfd_make_section_anyway_with_flags (ibfd, ".fixup", flags);
+      if (s == NULL || !bfd_set_section_alignment (s, 2))
+       return false;
+      htab->sfixup = s;
+    }
+
+  return true;
 }
 
 /* qsort predicate to sort sections by vma.  */
@@ -605,9 +649,10 @@ sort_sections (const void *a, const void *b)
   return (*s1)->index - (*s2)->index;
 }
 
-/* Identify overlays in the output bfd, and number them.  */
+/* Identify overlays in the output bfd, and number them.
+   Returns 0 on error, 1 if no overlays, 2 if overlays.  */
 
-bfd_boolean
+int
 spu_elf_find_overlays (struct bfd_link_info *info)
 {
   struct spu_link_hash_table *htab = spu_hash_table (info);
@@ -615,15 +660,18 @@ spu_elf_find_overlays (struct bfd_link_info *info)
   unsigned int i, n, ovl_index, num_buf;
   asection *s;
   bfd_vma ovl_end;
-  const char *ovly_mgr_entry;
+  static const char *const entry_names[2][2] = {
+    { "__ovly_load", "__icache_br_handler" },
+    { "__ovly_return", "__icache_call_handler" }
+  };
 
   if (info->output_bfd->section_count < 2)
-    return FALSE;
+    return 1;
 
   alloc_sec
     = bfd_malloc (info->output_bfd->section_count * sizeof (*alloc_sec));
   if (alloc_sec == NULL)
-    return FALSE;
+    return 0;
 
   /* Pick out all the alloced sections.  */
   for (n = 0, s = info->output_bfd->sections; s != NULL; s = s->next)
@@ -635,7 +683,7 @@ spu_elf_find_overlays (struct bfd_link_info *info)
   if (n == 0)
     {
       free (alloc_sec);
-      return FALSE;
+      return 1;
     }
 
   /* Sort them by vma.  */
@@ -644,9 +692,10 @@ spu_elf_find_overlays (struct bfd_link_info *info)
   ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size;
   if (htab->params->ovly_flavour == ovly_soft_icache)
     {
+      unsigned int prev_buf = 0, set_id = 0;
+
       /* Look for an overlapping vma to find the first overlay section.  */
       bfd_vma vma_start = 0;
-      bfd_vma lma_start = 0;
 
       for (i = 1; i < n; i++)
        {
@@ -655,7 +704,6 @@ spu_elf_find_overlays (struct bfd_link_info *info)
            {
              asection *s0 = alloc_sec[i - 1];
              vma_start = s0->vma;
-             lma_start = s0->lma;
              ovl_end = (s0->vma
                         + ((bfd_vma) 1
                            << (htab->num_lines_log2 + htab->line_size_log2)));
@@ -677,28 +725,32 @@ spu_elf_find_overlays (struct bfd_link_info *info)
             an overlay, in the sense that it might be loaded in
             by the overlay manager, but rather the initial
             section contents for the overlay buffer.  */
-         if (strncmp (s->name, ".ovl.init", 9) != 0)
+         if (!startswith (s->name, ".ovl.init"))
            {
              num_buf = ((s->vma - vma_start) >> htab->line_size_log2) + 1;
-             if (((s->vma - vma_start) & (htab->params->line_size - 1))
-                 || ((s->lma - lma_start) & (htab->params->line_size - 1)))
+             set_id = (num_buf == prev_buf)? set_id + 1 : 0;
+             prev_buf = num_buf;
+
+             if ((s->vma - vma_start) & (htab->params->line_size - 1))
                {
-                 info->callbacks->einfo (_("%X%P: overlay section %A "
-                                           "does not start on a cache line.\n"),
+                 info->callbacks->einfo (_("%X%P: overlay section %pA "
+                                           "does not start on a cache line\n"),
                                          s);
-                 return FALSE;
+                 bfd_set_error (bfd_error_bad_value);
+                 return 0;
                }
              else if (s->size > htab->params->line_size)
                {
-                 info->callbacks->einfo (_("%X%P: overlay section %A "
-                                           "is larger than a cache line.\n"),
+                 info->callbacks->einfo (_("%X%P: overlay section %pA "
+                                           "is larger than a cache line\n"),
                                          s);
-                 return FALSE;
+                 bfd_set_error (bfd_error_bad_value);
+                 return 0;
                }
 
              alloc_sec[ovl_index++] = s;
              spu_elf_section_data (s)->u.o.ovl_index
-               = ((s->lma - lma_start) >>  htab->line_size_log2) + 1;
+               = (set_id << htab->num_lines_log2) + num_buf;
              spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
            }
        }
@@ -709,10 +761,11 @@ spu_elf_find_overlays (struct bfd_link_info *info)
          s = alloc_sec[i];
          if (s->vma < ovl_end)
            {
-             info->callbacks->einfo (_("%X%P: overlay section %A "
-                                       "is not in cache area.\n"),
+             info->callbacks->einfo (_("%X%P: overlay section %pA "
+                                       "is not in cache area\n"),
                                      alloc_sec[i-1]);
-             return FALSE;
+             bfd_set_error (bfd_error_bad_value);
+             return 0;
            }
          else
            ovl_end = s->vma + s->size;
@@ -732,7 +785,7 @@ spu_elf_find_overlays (struct bfd_link_info *info)
              if (spu_elf_section_data (s0)->u.o.ovl_index == 0)
                {
                  ++num_buf;
-                 if (strncmp (s0->name, ".ovl.init", 9) != 0)
+                 if (!startswith (s0->name, ".ovl.init"))
                    {
                      alloc_sec[ovl_index] = s0;
                      spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index;
@@ -741,18 +794,20 @@ spu_elf_find_overlays (struct bfd_link_info *info)
                  else
                    ovl_end = s->vma + s->size;
                }
-             if (strncmp (s->name, ".ovl.init", 9) != 0)
+             if (!startswith (s->name, ".ovl.init"))
                {
                  alloc_sec[ovl_index] = s;
                  spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index;
                  spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
                  if (s0->vma != s->vma)
                    {
-                     info->callbacks->einfo (_("%X%P: overlay sections %A "
-                                               "and %A do not start at the "
-                                               "same address.\n"),
+                     /* xgettext:c-format */
+                     info->callbacks->einfo (_("%X%P: overlay sections %pA "
+                                               "and %pA do not start at the "
+                                               "same address\n"),
                                              s0, s);
-                     return FALSE;
+                     bfd_set_error (bfd_error_bad_value);
+                     return 0;
                    }
                  if (ovl_end < s->vma + s->size)
                    ovl_end = s->vma + s->size;
@@ -766,15 +821,31 @@ spu_elf_find_overlays (struct bfd_link_info *info)
   htab->num_overlays = ovl_index;
   htab->num_buf = num_buf;
   htab->ovl_sec = alloc_sec;
-  ovly_mgr_entry = "__ovly_load";
-  if (htab->params->ovly_flavour == ovly_soft_icache)
-    ovly_mgr_entry = "__icache_br_handler";
-  htab->ovly_load = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry,
-                                         FALSE, FALSE, FALSE);
-  if (htab->params->ovly_flavour != ovly_soft_icache)
-    htab->ovly_return = elf_link_hash_lookup (&htab->elf, "__ovly_return",
-                                             FALSE, FALSE, FALSE);
-  return ovl_index != 0;
+
+  if (ovl_index == 0)
+    return 1;
+
+  for (i = 0; i < 2; i++)
+    {
+      const char *name;
+      struct elf_link_hash_entry *h;
+
+      name = entry_names[i][htab->params->ovly_flavour];
+      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
+      if (h == NULL)
+       return 0;
+
+      if (h->root.type == bfd_link_hash_new)
+       {
+         h->root.type = bfd_link_hash_undefined;
+         h->ref_regular = 1;
+         h->ref_regular_nonweak = 1;
+         h->non_elf = 0;
+       }
+      htab->ovly_entry[i] = h;
+    }
+
+  return 2;
 }
 
 /* Non-zero to use bra in overlay stubs rather than br.  */
@@ -798,7 +869,7 @@ spu_elf_find_overlays (struct bfd_link_info *info)
    brhz  00100010 0..
    brhnz 00100011 0..  */
 
-static bfd_boolean
+static bool
 is_branch (const unsigned char *insn)
 {
   return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0;
@@ -814,7 +885,7 @@ is_branch (const unsigned char *insn)
    bihz   00100101 010
    bihnz  00100101 011  */
 
-static bfd_boolean
+static bool
 is_indirect_branch (const unsigned char *insn)
 {
   return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0;
@@ -824,7 +895,7 @@ is_indirect_branch (const unsigned char *insn)
    hbra  0001000..
    hbrr  0001001..  */
 
-static bfd_boolean
+static bool
 is_hint (const unsigned char *insn)
 {
   return (insn[0] & 0xfc) == 0x10;
@@ -832,22 +903,22 @@ is_hint (const unsigned char *insn)
 
 /* True if INPUT_SECTION might need overlay stubs.  */
 
-static bfd_boolean
+static bool
 maybe_needs_stubs (asection *input_section)
 {
   /* No stubs for debug sections and suchlike.  */
   if ((input_section->flags & SEC_ALLOC) == 0)
-    return FALSE;
+    return false;
 
   /* No stubs for link-once sections that will be discarded.  */
   if (input_section->output_section == bfd_abs_section_ptr)
-    return FALSE;
+    return false;
 
   /* Don't create stubs for .eh_frame references.  */
   if (strcmp (input_section->name, ".eh_frame") == 0)
-    return FALSE;
+    return false;
 
-  return TRUE;
+  return true;
 }
 
 enum _stub_type
@@ -881,7 +952,7 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
   struct spu_link_hash_table *htab = spu_hash_table (info);
   enum elf_spu_reloc_type r_type;
   unsigned int sym_type;
-  bfd_boolean branch, hint, call;
+  bool branch, hint, call;
   enum _stub_type ret = no_stub;
   bfd_byte insn[4];
 
@@ -893,13 +964,13 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
   if (h != NULL)
     {
       /* Ensure no stubs for user supplied overlay manager syms.  */
-      if (h == htab->ovly_load || h == htab->ovly_return)
+      if (h == htab->ovly_entry[0] || h == htab->ovly_entry[1])
        return ret;
 
       /* setjmp always goes via an overlay stub, because then the return
         and hence the longjmp goes via __ovly_return.  That magically
         makes setjmp/longjmp between overlays work.  */
-      if (strncmp (h->root.root.string, "setjmp", 6) == 0
+      if (startswith (h->root.root.string, "setjmp")
          && (h->root.root.string[6] == '\0' || h->root.root.string[6] == '@'))
        ret = call_ovl_stub;
     }
@@ -910,9 +981,9 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
     sym_type = ELF_ST_TYPE (sym->st_info);
 
   r_type = ELF32_R_TYPE (irela->r_info);
-  branch = FALSE;
-  hint = FALSE;
-  call = FALSE;
+  branch = false;
+  hint = false;
+  call = false;
   if (r_type == R_SPU_REL16 || r_type == R_SPU_ADDR16)
     {
       if (contents == NULL)
@@ -955,9 +1026,10 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
                                               sym,
                                               sym_sec);
                }
-             (*_bfd_error_handler) (_("warning: call to non-function"
-                                      " symbol %s defined in %B"),
-                                    sym_sec->owner, sym_name);
+             _bfd_error_handler
+               /* xgettext:c-format */
+               (_("warning: call to non-function symbol %s defined in %pB"),
+                sym_name, sym_sec->owner);
 
            }
        }
@@ -979,18 +1051,14 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
   if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index
        != spu_elf_section_data (input_section->output_section)->u.o.ovl_index)
     {
-      if (call || sym_type == STT_FUNC)
+      unsigned int lrlive = 0;
+      if (branch)
+       lrlive = (contents[1] & 0x70) >> 4;
+
+      if (!lrlive && (call || sym_type == STT_FUNC))
        ret = call_ovl_stub;
       else
-       {
-         ret = br000_ovl_stub;
-
-         if (branch)
-           {
-             unsigned int lrlive = (contents[1] & 0x70) >> 4;
-             ret += lrlive;
-           }
-       }
+       ret = br000_ovl_stub + lrlive;
     }
 
   /* If this insn isn't a branch then we are possibly taking the
@@ -1004,7 +1072,7 @@ needs_ovl_stub (struct elf_link_hash_entry *h,
   return ret;
 }
 
-static bfd_boolean
+static bool
 count_stub (struct spu_link_hash_table *htab,
            bfd *ibfd,
            asection *isec,
@@ -1034,7 +1102,7 @@ count_stub (struct spu_link_hash_table *htab,
                               * sizeof (*elf_local_got_ents (ibfd)));
          elf_local_got_ents (ibfd) = bfd_zmalloc (amt);
          if (elf_local_got_ents (ibfd) == NULL)
-           return FALSE;
+           return false;
        }
       head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
     }
@@ -1042,7 +1110,7 @@ count_stub (struct spu_link_hash_table *htab,
   if (htab->params->ovly_flavour == ovly_soft_icache)
     {
       htab->stub_count[ovl] += 1;
-      return TRUE;
+      return true;
     }
 
   addend = 0;
@@ -1082,7 +1150,7 @@ count_stub (struct spu_link_hash_table *htab,
     {
       g = bfd_malloc (sizeof *g);
       if (g == NULL)
-       return FALSE;
+       return false;
       g->ovl = ovl;
       g->addend = addend;
       g->stub_addr = (bfd_vma) -1;
@@ -1092,16 +1160,23 @@ count_stub (struct spu_link_hash_table *htab,
       htab->stub_count[ovl] += 1;
     }
 
-  return TRUE;
+  return true;
 }
 
 /* Support two sizes of overlay stubs, a slower more compact stub of two
-   intructions, and a faster stub of four instructions.  */
+   instructions, and a faster stub of four instructions.
+   Soft-icache stubs are four or eight words.  */
+
+static unsigned int
+ovl_stub_size (struct spu_elf_params *params)
+{
+  return 16 << params->ovly_flavour >> params->compact_stub;
+}
 
 static unsigned int
-ovl_stub_size (enum _ovly_flavour ovly_flavour)
+ovl_stub_size_log2 (struct spu_elf_params *params)
 {
-  return 8 << ovly_flavour;
+  return 4 + params->ovly_flavour - params->compact_stub;
 }
 
 /* Two instruction overlay stubs look like:
@@ -1128,7 +1203,7 @@ ovl_stub_size (enum _ovly_flavour ovly_flavour)
    .quad xor_pattern
 */
 
-static bfd_boolean
+static bool
 build_stub (struct bfd_link_info *info,
            bfd *ibfd,
            asection *isec,
@@ -1162,7 +1237,7 @@ build_stub (struct bfd_link_info *info,
     {
       g = bfd_malloc (sizeof *g);
       if (g == NULL)
-       return FALSE;
+       return false;
       g->ovl = ovl;
       g->br_addr = 0;
       if (irela != NULL)
@@ -1181,30 +1256,30 @@ build_stub (struct bfd_link_info *info,
        abort ();
 
       if (g->ovl == 0 && ovl != 0)
-       return TRUE;
+       return true;
 
       if (g->stub_addr != (bfd_vma) -1)
-       return TRUE;
+       return true;
     }
 
   sec = htab->stub_sec[ovl];
   dest += dest_sec->output_offset + dest_sec->output_section->vma;
   from = sec->size + sec->output_offset + sec->output_section->vma;
   g->stub_addr = from;
-  to = (htab->ovly_load->root.u.def.value
-       + htab->ovly_load->root.u.def.section->output_offset
-       + htab->ovly_load->root.u.def.section->output_section->vma);
+  to = (htab->ovly_entry[0]->root.u.def.value
+       + htab->ovly_entry[0]->root.u.def.section->output_offset
+       + htab->ovly_entry[0]->root.u.def.section->output_section->vma);
 
   if (((dest | to | from) & 3) != 0)
     {
       htab->stub_err = 1;
-      return FALSE;
+      return false;
     }
   dest_ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index;
 
-  switch (htab->params->ovly_flavour)
+  if (htab->params->ovly_flavour == ovly_normal
+      && !htab->params->compact_stub)
     {
-    case ovly_normal:
       bfd_put_32 (sec->owner, ILA + ((dest_ovl << 7) & 0x01ffff80) + 78,
                  sec->contents + sec->size);
       bfd_put_32 (sec->owner, LNOP,
@@ -1217,9 +1292,10 @@ build_stub (struct bfd_link_info *info,
       else
        bfd_put_32 (sec->owner, BRA + ((to << 5) & 0x007fff80),
                    sec->contents + sec->size + 12);
-      break;
-
-    case ovly_compact:
+    }
+  else if (htab->params->ovly_flavour == ovly_normal
+          && htab->params->compact_stub)
+    {
       if (!BRA_STUBS)
        bfd_put_32 (sec->owner, BRSL + (((to - from) << 5) & 0x007fff80) + 75,
                    sec->contents + sec->size);
@@ -1228,9 +1304,10 @@ build_stub (struct bfd_link_info *info,
                    sec->contents + sec->size);
       bfd_put_32 (sec->owner, (dest & 0x3ffff) | (dest_ovl << 18),
                  sec->contents + sec->size + 4);
-      break;
-
-    case ovly_soft_icache:
+    }
+  else if (htab->params->ovly_flavour == ovly_soft_icache
+          && htab->params->compact_stub)
+    {
       lrlive = 0;
       if (stub_type == nonovl_stub)
        ;
@@ -1300,7 +1377,8 @@ build_stub (struct bfd_link_info *info,
 
          if (stub_type != br000_ovl_stub
              && lrlive != stub_type - br000_ovl_stub)
-           info->callbacks->einfo (_("%A:0x%v lrlive .brinfo (%u) differs "
+           /* xgettext:c-format */
+           info->callbacks->einfo (_("%pA:0x%v lrlive .brinfo (%u) differs "
                                      "from analysis (%u)\n"),
                                    isec, irela->r_offset, lrlive,
                                    stub_type - br000_ovl_stub);
@@ -1310,10 +1388,15 @@ build_stub (struct bfd_link_info *info,
       if (stub_type > br000_ovl_stub)
        lrlive = stub_type - br000_ovl_stub;
 
-      /* The branch that uses this stub goes to stub_addr + 12.  We'll
-         set up an xor pattern that can be used by the icache manager
+      if (ovl == 0)
+       to = (htab->ovly_entry[1]->root.u.def.value
+             + htab->ovly_entry[1]->root.u.def.section->output_offset
+             + htab->ovly_entry[1]->root.u.def.section->output_section->vma);
+
+      /* The branch that uses this stub goes to stub_addr + 4.  We'll
+        set up an xor pattern that can be used by the icache manager
         to modify this branch to go directly to its destination.  */
-      g->stub_addr += 12;
+      g->stub_addr += 4;
       br_dest = g->stub_addr;
       if (irela == NULL)
        {
@@ -1324,29 +1407,27 @@ build_stub (struct bfd_link_info *info,
          br_dest = to;
        }
 
-      bfd_put_32 (sec->owner, dest_ovl - 1,
-                 sec->contents + sec->size + 0);
-      set_id = (dest_ovl - 1) >> htab->num_lines_log2;
+      set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1;
       bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff),
+                 sec->contents + sec->size);
+      bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
                  sec->contents + sec->size + 4);
       bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff),
                  sec->contents + sec->size + 8);
-      bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
-                 sec->contents + sec->size + 12);
       patt = dest ^ br_dest;
       if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16)
        patt = (dest - g->br_addr) ^ (br_dest - g->br_addr);
       bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80,
-                 sec->contents + sec->size + 16 + (g->br_addr & 0xf));
+                 sec->contents + sec->size + 12);
+
       if (ovl == 0)
        /* Extra space for linked list entries.  */
        sec->size += 16;
-      break;
-
-    default:
-      abort ();
     }
-  sec->size += ovl_stub_size (htab->params->ovly_flavour);
+  else
+    abort ();
+
+  sec->size += ovl_stub_size (htab->params);
 
   if (htab->params->emit_stub_syms)
     {
@@ -1364,9 +1445,9 @@ build_stub (struct bfd_link_info *info,
        add = (int) irela->r_addend & 0xffffffff;
       if (add != 0)
        len += 1 + 8;
-      name = bfd_malloc (len);
+      name = bfd_malloc (len + 1);
       if (name == NULL)
-       return FALSE;
+       return false;
 
       sprintf (name, "%08x.ovl_call.", g->ovl);
       if (h != NULL)
@@ -1378,15 +1459,15 @@ build_stub (struct bfd_link_info *info,
       if (add != 0)
        sprintf (name + len - 9, "+%x", add);
 
-      h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
+      h = elf_link_hash_lookup (&htab->elf, name, true, true, false);
       free (name);
       if (h == NULL)
-       return FALSE;
+       return false;
       if (h->root.type == bfd_link_hash_new)
        {
          h->root.type = bfd_link_hash_defined;
          h->root.u.def.section = sec;
-         h->size = ovl_stub_size (htab->params->ovly_flavour);
+         h->size = ovl_stub_size (htab->params);
          h->root.u.def.value = sec->size - h->size;
          h->type = STT_FUNC;
          h->ref_regular = 1;
@@ -1397,13 +1478,13 @@ build_stub (struct bfd_link_info *info,
        }
     }
 
-  return TRUE;
+  return true;
 }
 
 /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
    symbols.  */
 
-static bfd_boolean
+static bool
 allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
 {
   /* Symbols starting with _SPUEAR_ need a stub because they may be
@@ -1415,7 +1496,7 @@ allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
   if ((h->root.type == bfd_link_hash_defined
        || h->root.type == bfd_link_hash_defweak)
       && h->def_regular
-      && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
+      && startswith (h->root.root.string, "_SPUEAR_")
       && (sym_sec = h->root.u.def.section) != NULL
       && sym_sec->output_section != bfd_abs_section_ptr
       && spu_elf_section_data (sym_sec->output_section) != NULL
@@ -1424,11 +1505,11 @@ allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
     {
       return count_stub (htab, NULL, NULL, nonovl_stub, h, NULL);
     }
-  
-  return TRUE;
+
+  return true;
 }
 
-static bfd_boolean
+static bool
 build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
 {
   /* Symbols starting with _SPUEAR_ need a stub because they may be
@@ -1440,7 +1521,7 @@ build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
   if ((h->root.type == bfd_link_hash_defined
        || h->root.type == bfd_link_hash_defweak)
       && h->def_regular
-      && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
+      && startswith (h->root.root.string, "_SPUEAR_")
       && (sym_sec = h->root.u.def.section) != NULL
       && sym_sec->output_section != bfd_abs_section_ptr
       && spu_elf_section_data (sym_sec->output_section) != NULL
@@ -1450,26 +1531,26 @@ build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
       return build_stub (info, NULL, NULL, nonovl_stub, h, NULL,
                         h->root.u.def.value, sym_sec);
     }
-  
-  return TRUE;
+
+  return true;
 }
 
 /* Size or build stubs.  */
 
-static bfd_boolean
-process_stubs (struct bfd_link_info *info, bfd_boolean build)
+static bool
+process_stubs (struct bfd_link_info *info, bool build)
 {
   struct spu_link_hash_table *htab = spu_hash_table (info);
   bfd *ibfd;
 
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       Elf_Internal_Shdr *symtab_hdr;
       asection *isec;
       Elf_Internal_Sym *local_syms = NULL;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       /* We'll need the symbol table in a second.  */
@@ -1518,11 +1599,9 @@ process_stubs (struct bfd_link_info *info, bfd_boolean build)
                  if (elf_section_data (isec)->relocs != internal_relocs)
                    free (internal_relocs);
                error_ret_free_local:
-                 if (local_syms != NULL
-                     && (symtab_hdr->contents
-                         != (unsigned char *) local_syms))
+                 if (symtab_hdr->contents != (unsigned char *) local_syms)
                    free (local_syms);
-                 return FALSE;
+                 return false;
                }
 
              /* Determine the reloc target section.  */
@@ -1580,10 +1659,11 @@ process_stubs (struct bfd_link_info *info, bfd_boolean build)
        }
     }
 
-  return TRUE;
+  return true;
 }
 
-/* Allocate space for overlay call and return stubs.  */
+/* Allocate space for overlay call and return stubs.
+   Return 0 on error, 1 if no overlays, 2 otherwise.  */
 
 int
 spu_elf_size_stubs (struct bfd_link_info *info)
@@ -1594,9 +1674,8 @@ spu_elf_size_stubs (struct bfd_link_info *info)
   flagword flags;
   unsigned int i;
   asection *stub;
-  const char *ovout;
 
-  if (!process_stubs (info, FALSE))
+  if (!process_stubs (info, false))
     return 0;
 
   htab = spu_hash_table (info);
@@ -1604,68 +1683,68 @@ spu_elf_size_stubs (struct bfd_link_info *info)
   if (htab->stub_err)
     return 0;
 
-  if (htab->stub_count == NULL)
-    return 1;
-
   ibfd = info->input_bfds;
-  amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
-  htab->stub_sec = bfd_zmalloc (amt);
-  if (htab->stub_sec == NULL)
-    return 0;
-
-  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
-          | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
-  stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
-  htab->stub_sec[0] = stub;
-  if (stub == NULL
-      || !bfd_set_section_alignment (ibfd, stub,
-                                    htab->params->ovly_flavour + 3))
-    return 0;
-  stub->size = htab->stub_count[0] * ovl_stub_size (htab->params->ovly_flavour);
-  if (htab->params->ovly_flavour == ovly_soft_icache)
-    /* Extra space for linked list entries.  */
-    stub->size += htab->stub_count[0] * 16;
-  (*htab->params->place_spu_section) (stub, NULL, ".text");
-
-  for (i = 0; i < htab->num_overlays; ++i)
+  if (htab->stub_count != NULL)
     {
-      asection *osec = htab->ovl_sec[i];
-      unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
+      amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
+      htab->stub_sec = bfd_zmalloc (amt);
+      if (htab->stub_sec == NULL)
+       return 0;
+
+      flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
+              | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
       stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
-      htab->stub_sec[ovl] = stub;
+      htab->stub_sec[0] = stub;
       if (stub == NULL
-         || !bfd_set_section_alignment (ibfd, stub,
-                                        htab->params->ovly_flavour + 3))
+         || !bfd_set_section_alignment (stub,
+                                        ovl_stub_size_log2 (htab->params)))
        return 0;
-      stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params->ovly_flavour);
-      (*htab->params->place_spu_section) (stub, osec, NULL);
-    }
+      stub->size = htab->stub_count[0] * ovl_stub_size (htab->params);
+      if (htab->params->ovly_flavour == ovly_soft_icache)
+       /* Extra space for linked list entries.  */
+       stub->size += htab->stub_count[0] * 16;
 
-  flags = (SEC_ALLOC | SEC_LOAD
-          | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
-  htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
-  if (htab->ovtab == NULL
-      || !bfd_set_section_alignment (ibfd, htab->ovtab, 4))
-    return 0;
+      for (i = 0; i < htab->num_overlays; ++i)
+       {
+         asection *osec = htab->ovl_sec[i];
+         unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
+         stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
+         htab->stub_sec[ovl] = stub;
+         if (stub == NULL
+             || !bfd_set_section_alignment (stub,
+                                            ovl_stub_size_log2 (htab->params)))
+           return 0;
+         stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params);
+       }
+    }
 
   if (htab->params->ovly_flavour == ovly_soft_icache)
     {
       /* Space for icache manager tables.
         a) Tag array, one quadword per cache line.
-        b) Linked list elements, max_branch per line quadwords.
-        c) Indirect branch descriptors, 8 quadwords.  */
-      htab->ovtab->size = 16 * (((1 + htab->params->max_branch)
-                                << htab->num_lines_log2)
-                               + 8);
+        b) Rewrite "to" list, one quadword per cache line.
+        c) Rewrite "from" list, one byte per outgoing branch (rounded up to
+           a power-of-two number of full quadwords) per cache line.  */
+
+      flags = SEC_ALLOC;
+      htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
+      if (htab->ovtab == NULL
+         || !bfd_set_section_alignment (htab->ovtab, 4))
+       return 0;
+
+      htab->ovtab->size = (16 + 16 + (16 << htab->fromelem_size_log2))
+                         << htab->num_lines_log2;
 
+      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
       htab->init = bfd_make_section_anyway_with_flags (ibfd, ".ovini", flags);
       if (htab->init == NULL
-         || !bfd_set_section_alignment (ibfd, htab->init, 4))
+         || !bfd_set_section_alignment (htab->init, 4))
        return 0;
 
       htab->init->size = 16;
-      (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init");
     }
+  else if (htab->stub_count == NULL)
+    return 1;
   else
     {
       /* htab->ovtab consists of two arrays.
@@ -1681,23 +1760,62 @@ spu_elf_size_stubs (struct bfd_link_info *info)
         .      } _ovly_buf_table[];
         .  */
 
+      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
+      htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
+      if (htab->ovtab == NULL
+         || !bfd_set_section_alignment (htab->ovtab, 4))
+       return 0;
+
       htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
     }
-  ovout = ".data";
-  if (htab->params->ovly_flavour == ovly_soft_icache)
-    ovout = ".data.icache";
-  (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout);
 
   htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC);
   if (htab->toe == NULL
-      || !bfd_set_section_alignment (ibfd, htab->toe, 4))
+      || !bfd_set_section_alignment (htab->toe, 4))
     return 0;
-  htab->toe->size = htab->params->ovly_flavour == ovly_soft_icache ? 256 : 16;
-  (*htab->params->place_spu_section) (htab->toe, NULL, ".toe");
+  htab->toe->size = 16;
 
   return 2;
 }
 
+/* Called from ld to place overlay manager data sections.  This is done
+   after the overlay manager itself is loaded, mainly so that the
+   linker's htab->init section is placed after any other .ovl.init
+   sections.  */
+
+void
+spu_elf_place_overlay_data (struct bfd_link_info *info)
+{
+  struct spu_link_hash_table *htab = spu_hash_table (info);
+  unsigned int i;
+
+  if (htab->stub_sec != NULL)
+    {
+      (*htab->params->place_spu_section) (htab->stub_sec[0], NULL, ".text");
+
+      for (i = 0; i < htab->num_overlays; ++i)
+       {
+         asection *osec = htab->ovl_sec[i];
+         unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
+         (*htab->params->place_spu_section) (htab->stub_sec[ovl], osec, NULL);
+       }
+    }
+
+  if (htab->params->ovly_flavour == ovly_soft_icache)
+    (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init");
+
+  if (htab->ovtab != NULL)
+    {
+      const char *ovout = ".data";
+      if (htab->params->ovly_flavour == ovly_soft_icache)
+       ovout = ".bss";
+      (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout);
+    }
+
+  if (htab->toe != NULL)
+    (*htab->params->place_spu_section) (htab->toe, NULL, ".toe");
+}
+
 /* Functions to handle embedded spu_ovl.o object.  */
 
 static void *
@@ -1731,7 +1849,19 @@ ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED,
   return count;
 }
 
-bfd_boolean
+static int
+ovl_mgr_stat (struct bfd *abfd ATTRIBUTE_UNUSED,
+             void *stream,
+             struct stat *sb)
+{
+  struct _ovl_stream *os = (struct _ovl_stream *) stream;
+
+  memset (sb, 0, sizeof (*sb));
+  sb->st_size = (const char *) os->end - (const char *) os->start;
+  return 0;
+}
+
+bool
 spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
 {
   *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr",
@@ -1740,7 +1870,7 @@ spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
                              (void *) stream,
                              ovl_mgr_pread,
                              NULL,
-                             NULL);
+                             ovl_mgr_stat);
   return *ovl_bfd != NULL;
 }
 
@@ -1760,7 +1890,7 @@ define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
 {
   struct elf_link_hash_entry *h;
 
-  h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
+  h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
   if (h == NULL)
     return NULL;
 
@@ -1777,16 +1907,17 @@ define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
     }
   else if (h->root.u.def.section->owner != NULL)
     {
-      (*_bfd_error_handler) (_("%B is not allowed to define %s"),
-                            h->root.u.def.section->owner,
-                            h->root.root.string);
+      /* xgettext:c-format */
+      _bfd_error_handler (_("%pB is not allowed to define %s"),
+                         h->root.u.def.section->owner,
+                         h->root.root.string);
       bfd_set_error (bfd_error_bad_value);
       return NULL;
     }
   else
     {
-      (*_bfd_error_handler) (_("you are not allowed to define %s in a script"),
-                            h->root.root.string);
+      _bfd_error_handler (_("you are not allowed to define %s in a script"),
+                         h->root.root.string);
       bfd_set_error (bfd_error_bad_value);
       return NULL;
     }
@@ -1796,7 +1927,7 @@ define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
 
 /* Fill in all stubs and the overlay tables.  */
 
-static bfd_boolean
+static bool
 spu_elf_build_stubs (struct bfd_link_info *info)
 {
   struct spu_link_hash_table *htab = spu_hash_table (info);
@@ -1806,165 +1937,176 @@ spu_elf_build_stubs (struct bfd_link_info *info)
   bfd *obfd;
   unsigned int i;
 
-  if (htab->stub_count == NULL)
-    return TRUE;
-
-  for (i = 0; i <= htab->num_overlays; i++)
-    if (htab->stub_sec[i]->size != 0)
-      {
-       htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
-                                                 htab->stub_sec[i]->size);
-       if (htab->stub_sec[i]->contents == NULL)
-         return FALSE;
-       htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
-       htab->stub_sec[i]->size = 0;
-      }
-
-  h = htab->ovly_load;
-  if (h == NULL)
+  if (htab->num_overlays != 0)
     {
-      const char *ovly_mgr_entry = "__ovly_load";
-
-      if (htab->params->ovly_flavour == ovly_soft_icache)
-       ovly_mgr_entry = "__icache_br_handler";
-      h = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry,
-                               FALSE, FALSE, FALSE);
-      htab->ovly_load = h;
-    }
-  BFD_ASSERT (h != NULL
+      for (i = 0; i < 2; i++)
+       {
+         h = htab->ovly_entry[i];
+         if (h != NULL
              && (h->root.type == bfd_link_hash_defined
                  || h->root.type == bfd_link_hash_defweak)
-             && h->def_regular);
-
-  s = h->root.u.def.section->output_section;
-  if (spu_elf_section_data (s)->u.o.ovl_index)
-    {
-      (*_bfd_error_handler) (_("%s in overlay section"),
-                            h->root.root.string);
-      bfd_set_error (bfd_error_bad_value);
-      return FALSE;
+             && h->def_regular)
+           {
+             s = h->root.u.def.section->output_section;
+             if (spu_elf_section_data (s)->u.o.ovl_index)
+               {
+                 _bfd_error_handler (_("%s in overlay section"),
+                                     h->root.root.string);
+                 bfd_set_error (bfd_error_bad_value);
+                 return false;
+               }
+           }
+       }
     }
 
-  h = htab->ovly_return;
-  if (h == NULL && htab->params->ovly_flavour != ovly_soft_icache)
+  if (htab->stub_sec != NULL)
     {
-      h = elf_link_hash_lookup (&htab->elf, "__ovly_return",
-                               FALSE, FALSE, FALSE);
-      htab->ovly_return = h;
-    }
-
-  /* Fill in all the stubs.  */
-  process_stubs (info, TRUE);
-  if (!htab->stub_err)
-    elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info);
+      for (i = 0; i <= htab->num_overlays; i++)
+       if (htab->stub_sec[i]->size != 0)
+         {
+           htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
+                                                     htab->stub_sec[i]->size);
+           if (htab->stub_sec[i]->contents == NULL)
+             return false;
+           htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
+           htab->stub_sec[i]->size = 0;
+         }
 
-  if (htab->stub_err)
-    {
-      (*_bfd_error_handler) (_("overlay stub relocation overflow"));
-      bfd_set_error (bfd_error_bad_value);
-      return FALSE;
-    }
+      /* Fill in all the stubs.  */
+      process_stubs (info, true);
+      if (!htab->stub_err)
+       elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info);
 
-  for (i = 0; i <= htab->num_overlays; i++)
-    {
-      if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
+      if (htab->stub_err)
        {
-         (*_bfd_error_handler)  (_("stubs don't match calculated size"));
+         _bfd_error_handler (_("overlay stub relocation overflow"));
          bfd_set_error (bfd_error_bad_value);
-         return FALSE;
+         return false;
+       }
+
+      for (i = 0; i <= htab->num_overlays; i++)
+       {
+         if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
+           {
+             _bfd_error_handler  (_("stubs don't match calculated size"));
+             bfd_set_error (bfd_error_bad_value);
+             return false;
+           }
+         htab->stub_sec[i]->rawsize = 0;
        }
-      htab->stub_sec[i]->rawsize = 0;
     }
 
   if (htab->ovtab == NULL || htab->ovtab->size == 0)
-    return TRUE;
+    return true;
 
   htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
   if (htab->ovtab->contents == NULL)
-    return FALSE;
+    return false;
 
   p = htab->ovtab->contents;
   if (htab->params->ovly_flavour == ovly_soft_icache)
     {
-#define BI_HANDLER "__icache_ptr___icache_bi_handler0"
-      char name[sizeof (BI_HANDLER)];
-      bfd_vma off, icache_base, linklist, bihand;
+      bfd_vma off;
 
-      h = define_ovtab_symbol (htab, "__icache_tagbase");
+      h = define_ovtab_symbol (htab, "__icache_tag_array");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = 0;
       h->size = 16 << htab->num_lines_log2;
       off = h->size;
-      icache_base = htab->ovl_sec[0]->vma;
-      linklist = (htab->ovtab->output_section->vma
-                 + htab->ovtab->output_offset
-                 + off);
-      for (i = 0; i < htab->params->num_lines; i++)
-       {
-         bfd_vma line_end = icache_base + ((i + 1) << htab->line_size_log2);
-         bfd_vma stub_base = line_end - htab->params->max_branch * 32;
-         bfd_vma link_elem = linklist + i * htab->params->max_branch * 16;
-         bfd_vma locator = link_elem - stub_base / 2;
-
-         bfd_put_32 (htab->ovtab->owner, locator, p + 4);
-         bfd_put_16 (htab->ovtab->owner, link_elem, p + 8);
-         bfd_put_16 (htab->ovtab->owner, link_elem, p + 10);
-         bfd_put_16 (htab->ovtab->owner, link_elem, p + 12);
-         bfd_put_16 (htab->ovtab->owner, link_elem, p + 14);
-         p += 16;
-       }
 
-      h = define_ovtab_symbol (htab, "__icache_linked_list");
+      h = define_ovtab_symbol (htab, "__icache_tag_array_size");
       if (h == NULL)
-       return FALSE;
+       return false;
+      h->root.u.def.value = 16 << htab->num_lines_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_rewrite_to");
+      if (h == NULL)
+       return false;
       h->root.u.def.value = off;
-      h->size = htab->params->max_branch << (htab->num_lines_log2 + 4);
+      h->size = 16 << htab->num_lines_log2;
       off += h->size;
-      p += h->size;
 
-      h = elf_link_hash_lookup (&htab->elf, "__icache_bi_handler",
-                                FALSE, FALSE, FALSE);
-      bihand = 0;
-      if (h != NULL
-         && (h->root.type == bfd_link_hash_defined
-             || h->root.type == bfd_link_hash_defweak)
-         && h->def_regular)
-       bihand = (h->root.u.def.value
-                 + h->root.u.def.section->output_offset
-                 + h->root.u.def.section->output_section->vma);
-      memcpy (name, BI_HANDLER, sizeof (BI_HANDLER));
-      for (i = 0; i < 8; i++)
-       {
-         name[sizeof (BI_HANDLER) - 2] = '0' + i;
-         h = define_ovtab_symbol (htab, name);
-         if (h == NULL)
-           return FALSE;
-         h->root.u.def.value = off;
-         h->size = 16;
-         bfd_put_32 (htab->ovtab->owner, bihand, p);
-         bfd_put_32 (htab->ovtab->owner, i << 28, p + 8);
-         p += 16;
-         off += 16;
-       }
+      h = define_ovtab_symbol (htab, "__icache_rewrite_to_size");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = 16 << htab->num_lines_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_rewrite_from");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = off;
+      h->size = 16 << (htab->fromelem_size_log2 + htab->num_lines_log2);
+      off += h->size;
+
+      h = define_ovtab_symbol (htab, "__icache_rewrite_from_size");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = 16 << (htab->fromelem_size_log2
+                                  + htab->num_lines_log2);
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_log2_fromelemsize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = htab->fromelem_size_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
 
       h = define_ovtab_symbol (htab, "__icache_base");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = htab->ovl_sec[0]->vma;
       h->root.u.def.section = bfd_abs_section_ptr;
       h->size = htab->num_buf << htab->line_size_log2;
 
+      h = define_ovtab_symbol (htab, "__icache_linesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = 1 << htab->line_size_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_log2_linesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = htab->line_size_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_neg_log2_linesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = -htab->line_size_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_cachesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = 1 << (htab->num_lines_log2 + htab->line_size_log2);
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_log2_cachesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = htab->num_lines_log2 + htab->line_size_log2;
+      h->root.u.def.section = bfd_abs_section_ptr;
+
+      h = define_ovtab_symbol (htab, "__icache_neg_log2_cachesize");
+      if (h == NULL)
+       return false;
+      h->root.u.def.value = -(htab->num_lines_log2 + htab->line_size_log2);
+      h->root.u.def.section = bfd_abs_section_ptr;
+
       if (htab->init != NULL && htab->init->size != 0)
        {
          htab->init->contents = bfd_zalloc (htab->init->owner,
                                             htab->init->size);
          if (htab->init->contents == NULL)
-           return FALSE;
+           return false;
 
          h = define_ovtab_symbol (htab, "__icache_fileoff");
          if (h == NULL)
-           return FALSE;
+           return false;
          h->root.u.def.value = 0;
          h->root.u.def.section = htab->init;
          h->size = 8;
@@ -1988,44 +2130,44 @@ spu_elf_build_stubs (struct bfd_link_info *info)
              bfd_put_32 (htab->ovtab->owner, s->vma, p + off);
              bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16,
                          p + off + 4);
-             /* file_off written later in spu_elf_modify_program_headers.  */
+             /* file_off written later in spu_elf_modify_headers.  */
              bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12);
            }
        }
 
       h = define_ovtab_symbol (htab, "_ovly_table");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = 16;
       h->size = htab->num_overlays * 16;
 
       h = define_ovtab_symbol (htab, "_ovly_table_end");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = htab->num_overlays * 16 + 16;
       h->size = 0;
 
       h = define_ovtab_symbol (htab, "_ovly_buf_table");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = htab->num_overlays * 16 + 16;
       h->size = htab->num_buf * 4;
 
       h = define_ovtab_symbol (htab, "_ovly_buf_table_end");
       if (h == NULL)
-       return FALSE;
+       return false;
       h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
       h->size = 0;
     }
 
   h = define_ovtab_symbol (htab, "_EAR_");
   if (h == NULL)
-    return FALSE;
+    return false;
   h->root.u.def.section = htab->toe;
   h->root.u.def.value = 0;
-  h->size = htab->params->ovly_flavour == ovly_soft_icache ? 16 * 16 : 16;
+  h->size = 16;
 
-  return TRUE;
+  return true;
 }
 
 /* Check that all loadable section VMAs lie in the range
@@ -2043,7 +2185,7 @@ spu_elf_check_vma (struct bfd_link_info *info)
 
   htab->local_store = hi + 1 - lo;
 
-  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
+  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
     if (m->p_type == PT_LOAD)
       for (i = 0; i < m->count; i++)
        if (m->sections[i]->size != 0
@@ -2052,9 +2194,6 @@ spu_elf_check_vma (struct bfd_link_info *info)
                || m->sections[i]->vma + m->sections[i]->size - 1 > hi))
          return m->sections[i];
 
-  /* No need for overlays if it all fits.  */
-  if (htab->params->ovly_flavour != ovly_soft_icache)
-    htab->params->auto_overlay = 0;
   return NULL;
 }
 
@@ -2070,14 +2209,14 @@ find_function_stack_adjust (asection *sec,
                            bfd_vma *lr_store,
                            bfd_vma *sp_adjust)
 {
-  int reg[128];
+  int32_t reg[128];
 
   memset (reg, 0, sizeof (reg));
   for ( ; offset + 4 <= sec->size; offset += 4)
     {
       unsigned char buf[4];
       int rt, ra;
-      int imm;
+      uint32_t imm;
 
       /* Assume no relocs on stack adjusing insns.  */
       if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4))
@@ -2123,6 +2262,19 @@ find_function_stack_adjust (asection *sec,
              return reg[rt];
            }
        }
+      else if (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */)
+       {
+         int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
+
+         reg[rt] = reg[rb] - reg[ra];
+         if (rt == 1)
+           {
+             if (reg[rt] > 0)
+               break;
+             *sp_adjust = offset;
+             return reg[rt];
+           }
+       }
       else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
        {
          if (buf[0] >= 0x42 /* ila */)
@@ -2240,8 +2392,8 @@ alloc_stack_info (asection *sec, int max_fun)
 static struct function_info *
 maybe_insert_function (asection *sec,
                       void *sym_h,
-                      bfd_boolean global,
-                      bfd_boolean is_func)
+                      bool global,
+                      bool is_func)
 {
   struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
   struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
@@ -2281,11 +2433,11 @@ maybe_insert_function (asection *sec,
          /* Prefer globals over local syms.  */
          if (global && !sinfo->fun[i].global)
            {
-             sinfo->fun[i].global = TRUE;
+             sinfo->fun[i].global = true;
              sinfo->fun[i].u.h = sym_h;
            }
          if (is_func)
-           sinfo->fun[i].is_func = TRUE;
+           sinfo->fun[i].is_func = true;
          return &sinfo->fun[i];
        }
       /* Ignore a zero-size symbol inside an existing function.  */
@@ -2363,25 +2515,25 @@ func_name (struct function_info *fun)
 /* Read the instruction at OFF in SEC.  Return true iff the instruction
    is a nop, lnop, or stop 0 (all zero insn).  */
 
-static bfd_boolean
+static bool
 is_nop (asection *sec, bfd_vma off)
 {
   unsigned char insn[4];
 
   if (off + 4 > sec->size
       || !bfd_get_section_contents (sec->owner, sec, insn, off, 4))
-    return FALSE;
+    return false;
   if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20)
-    return TRUE;
+    return true;
   if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0)
-    return TRUE;
-  return FALSE;
+    return true;
+  return false;
 }
 
 /* Extend the range of FUN to cover nop padding up to LIMIT.
    Return TRUE iff some instruction other than a NOP was found.  */
 
-static bfd_boolean
+static bool
 insns_at_end (struct function_info *fun, bfd_vma limit)
 {
   bfd_vma off = (fun->hi + 3) & -4;
@@ -2391,25 +2543,25 @@ insns_at_end (struct function_info *fun, bfd_vma limit)
   if (off < limit)
     {
       fun->hi = off;
-      return TRUE;
+      return true;
     }
   fun->hi = limit;
-  return FALSE;
+  return false;
 }
 
 /* Check and fix overlapping function ranges.  Return TRUE iff there
    are gaps in the current info we have about functions in SEC.  */
 
-static bfd_boolean
+static bool
 check_function_ranges (asection *sec, struct bfd_link_info *info)
 {
   struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
   struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
   int i;
-  bfd_boolean gaps = FALSE;
+  bool gaps = false;
 
   if (sinfo == NULL)
-    return FALSE;
+    return false;
 
   for (i = 1; i < sinfo->num_fun; i++)
     if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo)
@@ -2418,18 +2570,19 @@ check_function_ranges (asection *sec, struct bfd_link_info *info)
        const char *f1 = func_name (&sinfo->fun[i - 1]);
        const char *f2 = func_name (&sinfo->fun[i]);
 
+       /* xgettext:c-format */
        info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2);
        sinfo->fun[i - 1].hi = sinfo->fun[i].lo;
       }
     else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo))
-      gaps = TRUE;
+      gaps = true;
 
   if (sinfo->num_fun == 0)
-    gaps = TRUE;
+    gaps = true;
   else
     {
       if (sinfo->fun[0].lo != 0)
-       gaps = TRUE;
+       gaps = true;
       if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size)
        {
          const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]);
@@ -2438,7 +2591,7 @@ check_function_ranges (asection *sec, struct bfd_link_info *info)
          sinfo->fun[sinfo->num_fun - 1].hi = sec->size;
        }
       else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size))
-       gaps = TRUE;
+       gaps = true;
     }
   return gaps;
 }
@@ -2465,8 +2618,10 @@ find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
       else
        return &sinfo->fun[mid];
     }
-  info->callbacks->einfo (_("%A:0x%v not found in function table\n"),
+  /* xgettext:c-format */
+  info->callbacks->einfo (_("%pA:0x%v not found in function table\n"),
                          sec, offset);
+  bfd_set_error (bfd_error_bad_value);
   return NULL;
 }
 
@@ -2474,7 +2629,7 @@ find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
    if CALLEE was new.  If this function return FALSE, CALLEE should
    be freed.  */
 
-static bfd_boolean
+static bool
 insert_callee (struct function_info *caller, struct call_info *callee)
 {
   struct call_info **pp, *p;
@@ -2488,40 +2643,39 @@ insert_callee (struct function_info *caller, struct call_info *callee)
        if (!p->is_tail)
          {
            p->fun->start = NULL;
-           p->fun->is_func = TRUE;
+           p->fun->is_func = true;
          }
-       p->count += 1;
+       p->count += callee->count;
        /* Reorder list so most recent call is first.  */
        *pp = p->next;
        p->next = caller->call_list;
        caller->call_list = p;
-       return FALSE;
+       return false;
       }
   callee->next = caller->call_list;
-  callee->count += 1;
   caller->call_list = callee;
-  return TRUE;
+  return true;
 }
 
 /* Copy CALL and insert the copy into CALLER.  */
 
-static bfd_boolean
+static bool
 copy_callee (struct function_info *caller, const struct call_info *call)
 {
   struct call_info *callee;
   callee = bfd_malloc (sizeof (*callee));
   if (callee == NULL)
-    return FALSE;
+    return false;
   *callee = *call;
   if (!insert_callee (caller, callee))
     free (callee);
-  return TRUE;
+  return true;
 }
 
 /* We're only interested in code sections.  Testing SEC_IN_MEMORY excludes
    overlay stub sections.  */
 
-static bfd_boolean
+static bool
 interesting_section (asection *s)
 {
   return (s->output_section != bfd_abs_section_ptr
@@ -2536,7 +2690,7 @@ interesting_section (asection *s)
    look at branches, which may be tail calls or go to hot/cold
    section part of same function.  */
 
-static bfd_boolean
+static bool
 mark_functions_via_relocs (asection *sec,
                           struct bfd_link_info *info,
                           int call_tree)
@@ -2545,16 +2699,16 @@ mark_functions_via_relocs (asection *sec,
   Elf_Internal_Shdr *symtab_hdr;
   void *psyms;
   unsigned int priority = 0;
-  static bfd_boolean warned;
+  static bool warned;
 
   if (!interesting_section (sec)
       || sec->reloc_count == 0)
-    return TRUE;
+    return true;
 
   internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
                                               info->keep_memory);
   if (internal_relocs == NULL)
-    return FALSE;
+    return false;
 
   symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr;
   psyms = &symtab_hdr->contents;
@@ -2568,36 +2722,29 @@ mark_functions_via_relocs (asection *sec,
       Elf_Internal_Sym *sym;
       struct elf_link_hash_entry *h;
       bfd_vma val;
-      bfd_boolean reject, is_call;
+      bool nonbranch, is_call;
       struct function_info *caller;
       struct call_info *callee;
 
-      reject = FALSE;
       r_type = ELF32_R_TYPE (irela->r_info);
-      if (r_type != R_SPU_REL16
-         && r_type != R_SPU_ADDR16)
-       {
-         reject = TRUE;
-         if (!(call_tree && spu_hash_table (info)->params->auto_overlay))
-           continue;
-       }
+      nonbranch = r_type != R_SPU_REL16 && r_type != R_SPU_ADDR16;
 
       r_indx = ELF32_R_SYM (irela->r_info);
       if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner))
-       return FALSE;
+       return false;
 
       if (sym_sec == NULL
          || sym_sec->output_section == bfd_abs_section_ptr)
        continue;
 
-      is_call = FALSE;
-      if (!reject)
+      is_call = false;
+      if (!nonbranch)
        {
          unsigned char insn[4];
 
          if (!bfd_get_section_contents (sec->owner, sec, insn,
                                         irela->r_offset, 4))
-           return FALSE;
+           return false;
          if (is_branch (insn))
            {
              is_call = (insn[0] & 0xfd) == 0x31;
@@ -2612,24 +2759,24 @@ mark_functions_via_relocs (asection *sec,
                {
                  if (!warned)
                    info->callbacks->einfo
-                     (_("%B(%A+0x%v): call to non-code section"
-                        " %B(%A), analysis incomplete\n"),
+                     /* xgettext:c-format */
+                     (_("%pB(%pA+0x%v): call to non-code section"
+                        " %pB(%pA), analysis incomplete\n"),
                       sec->owner, sec, irela->r_offset,
                       sym_sec->owner, sym_sec);
-                 warned = TRUE;
+                 warned = true;
                  continue;
                }
            }
          else
            {
-             reject = TRUE;
-             if (!(call_tree && spu_hash_table (info)->params->auto_overlay)
-                 || is_hint (insn))
+             nonbranch = true;
+             if (is_hint (insn))
                continue;
            }
        }
 
-      if (reject)
+      if (nonbranch)
        {
          /* For --auto-overlay, count possible stubs we need for
             function pointer references.  */
@@ -2639,8 +2786,20 @@ mark_functions_via_relocs (asection *sec,
          else
            sym_type = ELF_ST_TYPE (sym->st_info);
          if (sym_type == STT_FUNC)
-           spu_hash_table (info)->non_ovly_stub += 1;
-         continue;
+           {
+             if (call_tree && spu_hash_table (info)->params->auto_overlay)
+               spu_hash_table (info)->non_ovly_stub += 1;
+             /* If the symbol type is STT_FUNC then this must be a
+                function pointer initialisation.  */
+             continue;
+           }
+         /* Ignore data references.  */
+         if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
+             != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
+           continue;
+         /* Otherwise we probably have a jump table reloc for
+            a switch statement or some other reference to a
+            code label.  */
        }
 
       if (h)
@@ -2657,18 +2816,18 @@ mark_functions_via_relocs (asection *sec,
            {
              Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake));
              if (fake == NULL)
-               return FALSE;
+               return false;
              fake->st_value = val;
              fake->st_shndx
                = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec);
              sym = fake;
            }
          if (sym)
-           fun = maybe_insert_function (sym_sec, sym, FALSE, is_call);
+           fun = maybe_insert_function (sym_sec, sym, false, is_call);
          else
-           fun = maybe_insert_function (sym_sec, h, TRUE, is_call);
+           fun = maybe_insert_function (sym_sec, h, true, is_call);
          if (fun == NULL)
-           return FALSE;
+           return false;
          if (irela->r_addend != 0
              && fun->u.sym != sym)
            free (sym);
@@ -2677,18 +2836,19 @@ mark_functions_via_relocs (asection *sec,
 
       caller = find_function (sec, irela->r_offset, info);
       if (caller == NULL)
-       return FALSE;
+       return false;
       callee = bfd_malloc (sizeof *callee);
       if (callee == NULL)
-       return FALSE;
+       return false;
 
       callee->fun = find_function (sym_sec, val, info);
       if (callee->fun == NULL)
-       return FALSE;
+       return false;
       callee->is_tail = !is_call;
-      callee->is_pasted = FALSE;
+      callee->is_pasted = false;
+      callee->broken_cycle = false;
       callee->priority = priority;
-      callee->count = 0;
+      callee->count = nonbranch? 0 : 1;
       if (callee->fun->last_caller != sec)
        {
          callee->fun->last_caller = sec;
@@ -2708,10 +2868,17 @@ mark_functions_via_relocs (asection *sec,
          if (sec->owner != sym_sec->owner)
            {
              callee->fun->start = NULL;
-             callee->fun->is_func = TRUE;
+             callee->fun->is_func = true;
            }
          else if (callee->fun->start == NULL)
-           callee->fun->start = caller;
+           {
+             struct function_info *caller_start = caller;
+             while (caller_start->start)
+               caller_start = caller_start->start;
+
+             if (caller_start != callee->fun)
+               callee->fun->start = caller_start;
+           }
          else
            {
              struct function_info *callee_start;
@@ -2725,20 +2892,20 @@ mark_functions_via_relocs (asection *sec,
              if (caller_start != callee_start)
                {
                  callee->fun->start = NULL;
-                 callee->fun->is_func = TRUE;
+                 callee->fun->is_func = true;
                }
            }
        }
     }
 
-  return TRUE;
+  return true;
 }
 
 /* Handle something like .init or .fini, which has a piece of a function.
    These sections are pasted together to form a single function.  */
 
-static bfd_boolean
-pasted_function (asection *sec, struct bfd_link_info *info)
+static bool
+pasted_function (asection *sec)
 {
   struct bfd_link_order *l;
   struct _spu_elf_section_data *sec_data;
@@ -2748,14 +2915,14 @@ pasted_function (asection *sec, struct bfd_link_info *info)
 
   fake = bfd_zmalloc (sizeof (*fake));
   if (fake == NULL)
-    return FALSE;
+    return false;
   fake->st_value = 0;
   fake->st_size = sec->size;
   fake->st_shndx
     = _bfd_elf_section_from_bfd_section (sec->owner, sec);
-  fun = maybe_insert_function (sec, fake, FALSE, FALSE);
+  fun = maybe_insert_function (sec, fake, false, false);
   if (!fun)
-    return FALSE;
+    return false;
 
   /* Find a function immediately preceding this section.  */
   fun_start = NULL;
@@ -2767,16 +2934,18 @@ pasted_function (asection *sec, struct bfd_link_info *info)
            {
              struct call_info *callee = bfd_malloc (sizeof *callee);
              if (callee == NULL)
-               return FALSE;
+               return false;
 
              fun->start = fun_start;
              callee->fun = fun;
-             callee->is_tail = TRUE;
-             callee->is_pasted = TRUE;
-             callee->count = 0;
+             callee->is_tail = true;
+             callee->is_pasted = true;
+             callee->broken_cycle = false;
+             callee->priority = 0;
+             callee->count = 1;
              if (!insert_callee (fun_start, callee))
                free (callee);
-             return TRUE;
+             return true;
            }
          break;
        }
@@ -2787,45 +2956,45 @@ pasted_function (asection *sec, struct bfd_link_info *info)
        fun_start = &sinfo->fun[sinfo->num_fun - 1];
     }
 
-  info->callbacks->einfo (_("%A link_order not found\n"), sec);
-  return FALSE;
+  /* Don't return an error if we did not find a function preceding this
+     section.  The section may have incorrect flags.  */
+  return true;
 }
 
 /* Map address ranges in code sections to functions.  */
 
-static bfd_boolean
+static bool
 discover_functions (struct bfd_link_info *info)
 {
   bfd *ibfd;
   int bfd_idx;
   Elf_Internal_Sym ***psym_arr;
   asection ***sec_arr;
-  bfd_boolean gaps = FALSE;
+  bool gaps = false;
 
   bfd_idx = 0;
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     bfd_idx++;
 
   psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr));
   if (psym_arr == NULL)
-    return FALSE;
+    return false;
   sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr));
   if (sec_arr == NULL)
-    return FALSE;
+    return false;
 
-  
   for (ibfd = info->input_bfds, bfd_idx = 0;
        ibfd != NULL;
-       ibfd = ibfd->link_next, bfd_idx++)
+       ibfd = ibfd->link.next, bfd_idx++)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       Elf_Internal_Shdr *symtab_hdr;
       asection *sec;
       size_t symcount;
       Elf_Internal_Sym *syms, *sy, **psyms, **psy;
       asection **psecs, **p;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       /* Read all the symbols.  */
@@ -2837,38 +3006,34 @@ discover_functions (struct bfd_link_info *info)
            for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
              if (interesting_section (sec))
                {
-                 gaps = TRUE;
+                 gaps = true;
                  break;
                }
          continue;
        }
 
-      if (symtab_hdr->contents != NULL)
-       {
-         /* Don't use cached symbols since the generic ELF linker
-            code only reads local symbols, and we need globals too.  */ 
-         free (symtab_hdr->contents);
-         symtab_hdr->contents = NULL;
-       }
+      /* Don't use cached symbols since the generic ELF linker
+        code only reads local symbols, and we need globals too.  */
+      free (symtab_hdr->contents);
+      symtab_hdr->contents = NULL;
       syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
                                   NULL, NULL, NULL);
       symtab_hdr->contents = (void *) syms;
       if (syms == NULL)
-       return FALSE;
+       return false;
 
       /* Select defined function symbols that are going to be output.  */
       psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms));
       if (psyms == NULL)
-       return FALSE;
+       return false;
       psym_arr[bfd_idx] = psyms;
       psecs = bfd_malloc (symcount * sizeof (*psecs));
       if (psecs == NULL)
-       return FALSE;
+       return false;
       sec_arr[bfd_idx] = psecs;
       for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy)
        if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE
-           || ELF_ST_TYPE (sy->st_info) == STT_FUNC
-           || ELF_ST_TYPE (sy->st_info) == STT_SECTION)
+           || ELF_ST_TYPE (sy->st_info) == STT_FUNC)
          {
            asection *s;
 
@@ -2895,7 +3060,7 @@ discover_functions (struct bfd_link_info *info)
              break;
 
          if (!alloc_stack_info (s, psy2 - psy))
-           return FALSE;
+           return false;
          psy = psy2;
        }
 
@@ -2909,8 +3074,8 @@ discover_functions (struct bfd_link_info *info)
          if (ELF_ST_TYPE (sy->st_info) == STT_FUNC)
            {
              asection *s = psecs[sy - syms];
-             if (!maybe_insert_function (s, sy, FALSE, TRUE))
-               return FALSE;
+             if (!maybe_insert_function (s, sy, false, true))
+               return false;
            }
        }
 
@@ -2925,7 +3090,7 @@ discover_functions (struct bfd_link_info *info)
         relocations.  */
       for (ibfd = info->input_bfds, bfd_idx = 0;
           ibfd != NULL;
-          ibfd = ibfd->link_next, bfd_idx++)
+          ibfd = ibfd->link.next, bfd_idx++)
        {
          asection *sec;
 
@@ -2933,13 +3098,13 @@ discover_functions (struct bfd_link_info *info)
            continue;
 
          for (sec = ibfd->sections; sec != NULL; sec = sec->next)
-           if (!mark_functions_via_relocs (sec, info, FALSE))
-             return FALSE;
+           if (!mark_functions_via_relocs (sec, info, false))
+             return false;
        }
 
       for (ibfd = info->input_bfds, bfd_idx = 0;
           ibfd != NULL;
-          ibfd = ibfd->link_next, bfd_idx++)
+          ibfd = ibfd->link.next, bfd_idx++)
        {
          Elf_Internal_Shdr *symtab_hdr;
          asection *sec;
@@ -2954,7 +3119,7 @@ discover_functions (struct bfd_link_info *info)
          symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
          syms = (Elf_Internal_Sym *) symtab_hdr->contents;
 
-         gaps = FALSE;
+         gaps = false;
          for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
            if (interesting_section (sec))
              gaps |= check_function_ranges (sec, info);
@@ -2972,18 +3137,18 @@ discover_functions (struct bfd_link_info *info)
              if (ELF_ST_TYPE (sy->st_info) != STT_FUNC
                  && ELF_ST_BIND (sy->st_info) == STB_GLOBAL)
                {
-                 if (!maybe_insert_function (s, sy, FALSE, FALSE))
-                   return FALSE;
+                 if (!maybe_insert_function (s, sy, false, false))
+                   return false;
                }
            }
        }
 
-      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
        {
-         extern const bfd_target bfd_elf32_spu_vec;
+         extern const bfd_target spu_elf32_vec;
          asection *sec;
 
-         if (ibfd->xvec != &bfd_elf32_spu_vec)
+         if (ibfd->xvec != &spu_elf32_vec)
            continue;
 
          /* Some of the symbols we've installed as marking the
@@ -2998,7 +3163,7 @@ discover_functions (struct bfd_link_info *info)
 
                sec_data = spu_elf_section_data (sec);
                sinfo = sec_data->u.i.stack_info;
-               if (sinfo != NULL)
+               if (sinfo != NULL && sinfo->num_fun != 0)
                  {
                    int fun_idx;
                    bfd_vma hi = sec->size;
@@ -3008,18 +3173,20 @@ discover_functions (struct bfd_link_info *info)
                        sinfo->fun[fun_idx].hi = hi;
                        hi = sinfo->fun[fun_idx].lo;
                      }
+
+                   sinfo->fun[0].lo = 0;
                  }
                /* No symbols in this section.  Must be .init or .fini
                   or something similar.  */
-               else if (!pasted_function (sec, info))
-                 return FALSE;
+               else if (!pasted_function (sec))
+                 return false;
              }
        }
     }
 
   for (ibfd = info->input_bfds, bfd_idx = 0;
        ibfd != NULL;
-       ibfd = ibfd->link_next, bfd_idx++)
+       ibfd = ibfd->link.next, bfd_idx++)
     {
       if (psym_arr[bfd_idx] == NULL)
        continue;
@@ -3031,29 +3198,29 @@ discover_functions (struct bfd_link_info *info)
   free (psym_arr);
   free (sec_arr);
 
-  return TRUE;
+  return true;
 }
 
 /* Iterate over all function_info we have collected, calling DOIT on
    each node if ROOT_ONLY is false.  Only call DOIT on root nodes
    if ROOT_ONLY.  */
 
-static bfd_boolean
-for_each_node (bfd_boolean (*doit) (struct function_info *,
-                                   struct bfd_link_info *,
-                                   void *),
+static bool
+for_each_node (bool (*doit) (struct function_info *,
+                            struct bfd_link_info *,
+                            void *),
               struct bfd_link_info *info,
               void *param,
               int root_only)
 {
   bfd *ibfd;
 
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       asection *sec;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
@@ -3068,17 +3235,17 @@ for_each_node (bfd_boolean (*doit) (struct function_info *,
              for (i = 0; i < sinfo->num_fun; ++i)
                if (!root_only || !sinfo->fun[i].non_root)
                  if (!doit (&sinfo->fun[i], info, param))
-                   return FALSE;
+                   return false;
            }
        }
     }
-  return TRUE;
+  return true;
 }
 
 /* Transfer call info attached to struct function_info entries for
    all of a given function's sections to the first entry.  */
 
-static bfd_boolean
+static bool
 transfer_calls (struct function_info *fun,
                struct bfd_link_info *info ATTRIBUTE_UNUSED,
                void *param ATTRIBUTE_UNUSED)
@@ -3099,12 +3266,12 @@ transfer_calls (struct function_info *fun,
        }
       fun->call_list = NULL;
     }
-  return TRUE;
+  return true;
 }
 
 /* Mark nodes in the call graph that are called by some other node.  */
 
-static bfd_boolean
+static bool
 mark_non_root (struct function_info *fun,
               struct bfd_link_info *info ATTRIBUTE_UNUSED,
               void *param ATTRIBUTE_UNUSED)
@@ -3112,19 +3279,19 @@ mark_non_root (struct function_info *fun,
   struct call_info *call;
 
   if (fun->visit1)
-    return TRUE;
-  fun->visit1 = TRUE;
+    return true;
+  fun->visit1 = true;
   for (call = fun->call_list; call; call = call->next)
     {
-      call->fun->non_root = TRUE;
+      call->fun->non_root = true;
       mark_non_root (call->fun, 0, 0);
     }
-  return TRUE;
+  return true;
 }
 
 /* Remove cycles from the call graph.  Set depth of nodes.  */
 
-static bfd_boolean
+static bool
 remove_cycles (struct function_info *fun,
               struct bfd_link_info *info,
               void *param)
@@ -3134,8 +3301,8 @@ remove_cycles (struct function_info *fun,
   unsigned int max_depth = depth;
 
   fun->depth = depth;
-  fun->visit2 = TRUE;
-  fun->marking = TRUE;
+  fun->visit2 = true;
+  fun->marking = true;
 
   callp = &fun->call_list;
   while ((call = *callp) != NULL)
@@ -3144,7 +3311,7 @@ remove_cycles (struct function_info *fun,
       if (!call->fun->visit2)
        {
          if (!remove_cycles (call->fun, info, &call->max_depth))
-           return FALSE;
+           return false;
          if (max_depth < call->max_depth)
            max_depth = call->max_depth;
        }
@@ -3158,19 +3325,19 @@ remove_cycles (struct function_info *fun,
              const char *f1 = func_name (fun);
              const char *f2 = func_name (call->fun);
 
-             info->callbacks->info (_("Stack analysis will ignore the call "
+             /* xgettext:c-format */
+             info->callbacks->info (_("stack analysis will ignore the call "
                                       "from %s to %s\n"),
                                     f1, f2);
            }
-         *callp = call->next;
-         free (call);
-         continue;
+
+         call->broken_cycle = true;
        }
       callp = &call->next;
     }
-  fun->marking = FALSE;
+  fun->marking = false;
   *(unsigned int *) param = max_depth;
-  return TRUE;
+  return true;
 }
 
 /* Check that we actually visited all nodes in remove_cycles.  If we
@@ -3178,56 +3345,56 @@ remove_cycles (struct function_info *fun,
    any root node.  Arbitrarily choose a node in the cycle as a new
    root and break the cycle.  */
 
-static bfd_boolean
+static bool
 mark_detached_root (struct function_info *fun,
                    struct bfd_link_info *info,
                    void *param)
 {
   if (fun->visit2)
-    return TRUE;
-  fun->non_root = FALSE;
+    return true;
+  fun->non_root = false;
   *(unsigned int *) param = 0;
   return remove_cycles (fun, info, param);
 }
 
 /* Populate call_list for each function.  */
 
-static bfd_boolean
+static bool
 build_call_tree (struct bfd_link_info *info)
 {
   bfd *ibfd;
   unsigned int depth;
 
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       asection *sec;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
-       if (!mark_functions_via_relocs (sec, info, TRUE))
-         return FALSE;
+       if (!mark_functions_via_relocs (sec, info, true))
+         return false;
     }
 
   /* Transfer call info from hot/cold section part of function
      to main entry.  */
   if (!spu_hash_table (info)->params->auto_overlay
-      && !for_each_node (transfer_calls, info, 0, FALSE))
-    return FALSE;
+      && !for_each_node (transfer_calls, info, 0, false))
+    return false;
 
   /* Find the call graph root(s).  */
-  if (!for_each_node (mark_non_root, info, 0, FALSE))
-    return FALSE;
+  if (!for_each_node (mark_non_root, info, 0, false))
+    return false;
 
   /* Remove cycles from the call graph.  We start from the root node(s)
      so that we break cycles in a reasonable place.  */
   depth = 0;
-  if (!for_each_node (remove_cycles, info, &depth, TRUE))
-    return FALSE;
+  if (!for_each_node (remove_cycles, info, &depth, true))
+    return false;
 
-  return for_each_node (mark_detached_root, info, &depth, FALSE);
+  return for_each_node (mark_detached_root, info, &depth, false);
 }
 
 /* qsort predicate to sort calls by priority, max_depth then count.  */
@@ -3271,7 +3438,7 @@ struct _mos_param {
    Sort the call graph so that the deepest nodes will be visited
    first.  */
 
-static bfd_boolean
+static bool
 mark_overlay_section (struct function_info *fun,
                      struct bfd_link_info *info,
                      void *param)
@@ -3282,13 +3449,15 @@ mark_overlay_section (struct function_info *fun,
   struct spu_link_hash_table *htab = spu_hash_table (info);
 
   if (fun->visit4)
-    return TRUE;
+    return true;
 
-  fun->visit4 = TRUE;
+  fun->visit4 = true;
   if (!fun->sec->linker_mark
       && (htab->params->ovly_flavour != ovly_soft_icache
          || htab->params->non_ia_text
-         || strncmp (fun->sec->name, ".text.ia.", 9) == 0))
+         || startswith (fun->sec->name, ".text.ia.")
+         || strcmp (fun->sec->name, ".init") == 0
+         || strcmp (fun->sec->name, ".fini") == 0))
     {
       unsigned int size;
 
@@ -3311,24 +3480,24 @@ mark_overlay_section (struct function_info *fun,
            {
              name = bfd_malloc (sizeof (".rodata"));
              if (name == NULL)
-               return FALSE;
+               return false;
              memcpy (name, ".rodata", sizeof (".rodata"));
            }
-         else if (strncmp (fun->sec->name, ".text.", 6) == 0)
+         else if (startswith (fun->sec->name, ".text."))
            {
              size_t len = strlen (fun->sec->name);
              name = bfd_malloc (len + 3);
              if (name == NULL)
-               return FALSE;
+               return false;
              memcpy (name, ".rodata", sizeof (".rodata"));
              memcpy (name + 7, fun->sec->name + 5, len - 4);
            }
-         else if (strncmp (fun->sec->name, ".gnu.linkonce.t.", 16) == 0)
+         else if (startswith (fun->sec->name, ".gnu.linkonce.t."))
            {
              size_t len = strlen (fun->sec->name) + 1;
              name = bfd_malloc (len);
              if (name == NULL)
-               return FALSE;
+               return false;
              memcpy (name, fun->sec->name, len);
              name[14] = 'r';
            }
@@ -3380,7 +3549,7 @@ mark_overlay_section (struct function_info *fun,
     {
       struct call_info **calls = bfd_malloc (count * sizeof (*calls));
       if (calls == NULL)
-       return FALSE;
+       return false;
 
       for (count = 0, call = fun->call_list; call != NULL; call = call->next)
        calls[count++] = call;
@@ -3405,21 +3574,22 @@ mark_overlay_section (struct function_info *fun,
          BFD_ASSERT (!fun->sec->segment_mark);
          fun->sec->segment_mark = 1;
        }
-      if (!mark_overlay_section (call->fun, info, param))
-       return FALSE;
+      if (!call->broken_cycle
+         && !mark_overlay_section (call->fun, info, param))
+       return false;
     }
 
   /* Don't put entry code into an overlay.  The overlay manager needs
      a stack!  Also, don't mark .ovl.init as an overlay.  */
   if (fun->lo + fun->sec->output_offset + fun->sec->output_section->vma
       == info->output_bfd->start_address
-      || strncmp (fun->sec->output_section->name, ".ovl.init", 9) == 0)
+      || startswith (fun->sec->output_section->name, ".ovl.init"))
     {
       fun->sec->linker_mark = 0;
       if (fun->rodata != NULL)
        fun->rodata->linker_mark = 0;
     }
-  return TRUE;
+  return true;
 }
 
 /* If non-zero then unmark functions called from those within sections
@@ -3435,7 +3605,7 @@ struct _uos_param {
 
 /* Undo some of mark_overlay_section's work.  */
 
-static bfd_boolean
+static bool
 unmark_overlay_section (struct function_info *fun,
                        struct bfd_link_info *info,
                        void *param)
@@ -3445,9 +3615,9 @@ unmark_overlay_section (struct function_info *fun,
   unsigned int excluded = 0;
 
   if (fun->visit5)
-    return TRUE;
+    return true;
 
-  fun->visit5 = TRUE;
+  fun->visit5 = true;
 
   excluded = 0;
   if (fun->sec == uos_param->exclude_input_section
@@ -3465,12 +3635,13 @@ unmark_overlay_section (struct function_info *fun,
     }
 
   for (call = fun->call_list; call != NULL; call = call->next)
-    if (!unmark_overlay_section (call->fun, info, param))
-      return FALSE;
+    if (!call->broken_cycle
+       && !unmark_overlay_section (call->fun, info, param))
+      return false;
 
   if (RECURSE_UNMARK)
     uos_param->clearing -= excluded;
-  return TRUE;
+  return true;
 }
 
 struct _cl_param {
@@ -3482,7 +3653,7 @@ struct _cl_param {
    for consideration as non-overlay sections.  The array consist of
    pairs of sections, (text,rodata), for functions in the call graph.  */
 
-static bfd_boolean
+static bool
 collect_lib_sections (struct function_info *fun,
                      struct bfd_link_info *info,
                      void *param)
@@ -3492,11 +3663,11 @@ collect_lib_sections (struct function_info *fun,
   unsigned int size;
 
   if (fun->visit6)
-    return TRUE;
+    return true;
 
-  fun->visit6 = TRUE;
+  fun->visit6 = true;
   if (!fun->sec->linker_mark || !fun->sec->gc_mark || fun->sec->segment_mark)
-    return TRUE;
+    return true;
 
   size = fun->sec->size;
   if (fun->rodata)
@@ -3516,9 +3687,10 @@ collect_lib_sections (struct function_info *fun,
     }
 
   for (call = fun->call_list; call != NULL; call = call->next)
-    collect_lib_sections (call->fun, info, param);
+    if (!call->broken_cycle)
+      collect_lib_sections (call->fun, info, param);
 
-  return TRUE;
+  return true;
 }
 
 /* qsort predicate to sort sections by call count.  */
@@ -3570,12 +3742,12 @@ auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
 
   memset (&dummy_caller, 0, sizeof (dummy_caller));
   lib_count = 0;
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       asection *sec;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
@@ -3590,7 +3762,7 @@ auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
   collect_lib_param.lib_size = lib_size;
   collect_lib_param.lib_sections = lib_sections;
   if (!for_each_node (collect_lib_sections, info, &collect_lib_param,
-                     TRUE))
+                     true))
     return (unsigned int) -1;
   lib_count = (collect_lib_param.lib_sections - lib_sections) / 2;
 
@@ -3630,7 +3802,7 @@ auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
                    if (p->fun == call->fun)
                      break;
                  if (!p)
-                   stub_size += ovl_stub_size (htab->params->ovly_flavour);
+                   stub_size += ovl_stub_size (htab->params);
                }
        }
       if (tmp + stub_size < lib_size)
@@ -3648,7 +3820,7 @@ auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
          while ((p = *pp) != NULL)
            if (!p->fun->sec->linker_mark)
              {
-               lib_size += ovl_stub_size (htab->params->ovly_flavour);
+               lib_size += ovl_stub_size (htab->params);
                *pp = p->next;
                free (p);
              }
@@ -3696,28 +3868,28 @@ auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
    from the parent section.  The idea being to group sections to
    minimise calls between different overlays.  */
 
-static bfd_boolean
+static bool
 collect_overlays (struct function_info *fun,
                  struct bfd_link_info *info,
                  void *param)
 {
   struct call_info *call;
-  bfd_boolean added_fun;
+  bool added_fun;
   asection ***ovly_sections = param;
 
   if (fun->visit7)
-    return TRUE;
+    return true;
 
-  fun->visit7 = TRUE;
+  fun->visit7 = true;
   for (call = fun->call_list; call != NULL; call = call->next)
-    if (!call->is_pasted)
+    if (!call->is_pasted && !call->broken_cycle)
       {
        if (!collect_overlays (call->fun, info, ovly_sections))
-         return FALSE;
+         return false;
        break;
       }
 
-  added_fun = FALSE;
+  added_fun = false;
   if (fun->sec->linker_mark && fun->sec->gc_mark)
     {
       fun->sec->gc_mark = 0;
@@ -3729,7 +3901,7 @@ collect_overlays (struct function_info *fun,
        }
       else
        *(*ovly_sections)++ = NULL;
-      added_fun = TRUE;
+      added_fun = true;
 
       /* Pasted sections must stay with the first section.  We don't
         put pasted sections in the array, just the first section.
@@ -3756,8 +3928,9 @@ collect_overlays (struct function_info *fun,
     }
 
   for (call = fun->call_list; call != NULL; call = call->next)
-    if (!collect_overlays (call->fun, info, ovly_sections))
-      return FALSE;
+    if (!call->broken_cycle
+       && !collect_overlays (call->fun, info, ovly_sections))
+      return false;
 
   if (added_fun)
     {
@@ -3770,22 +3943,22 @@ collect_overlays (struct function_info *fun,
          int i;
          for (i = 0; i < sinfo->num_fun; ++i)
            if (!collect_overlays (&sinfo->fun[i], info, ovly_sections))
-             return FALSE;
+             return false;
        }
     }
 
-  return TRUE;
+  return true;
 }
 
 struct _sum_stack_param {
   size_t cum_stack;
   size_t overall_stack;
-  bfd_boolean emit_stack_syms;
+  bool emit_stack_syms;
 };
 
 /* Descend the call graph for FUN, accumulating total stack required.  */
 
-static bfd_boolean
+static bool
 sum_stack (struct function_info *fun,
           struct bfd_link_info *info,
           void *param)
@@ -3794,23 +3967,25 @@ sum_stack (struct function_info *fun,
   struct function_info *max;
   size_t stack, cum_stack;
   const char *f1;
-  bfd_boolean has_call;
+  bool has_call;
   struct _sum_stack_param *sum_stack_param = param;
   struct spu_link_hash_table *htab;
 
   cum_stack = fun->stack;
   sum_stack_param->cum_stack = cum_stack;
   if (fun->visit3)
-    return TRUE;
+    return true;
 
-  has_call = FALSE;
+  has_call = false;
   max = NULL;
   for (call = fun->call_list; call; call = call->next)
     {
+      if (call->broken_cycle)
+       continue;
       if (!call->is_pasted)
-       has_call = TRUE;
+       has_call = true;
       if (!sum_stack (call->fun, info, sum_stack_param))
-       return FALSE;
+       return false;
       stack = sum_stack_param->cum_stack;
       /* Include caller stack for normal calls, don't do so for
         tail calls.  fun->stack here is local stack usage for
@@ -3828,7 +4003,7 @@ sum_stack (struct function_info *fun,
   stack = fun->stack;
   /* Now fun->stack holds cumulative stack.  */
   fun->stack = cum_stack;
-  fun->visit3 = TRUE;
+  fun->visit3 = true;
 
   if (!fun->non_root
       && sum_stack_param->overall_stack < cum_stack)
@@ -3836,27 +4011,27 @@ sum_stack (struct function_info *fun,
 
   htab = spu_hash_table (info);
   if (htab->params->auto_overlay)
-    return TRUE;
+    return true;
 
   f1 = func_name (fun);
   if (htab->params->stack_analysis)
     {
       if (!fun->non_root)
-       info->callbacks->info (_("  %s: 0x%v\n"), f1, (bfd_vma) cum_stack);
-      info->callbacks->minfo (_("%s: 0x%v 0x%v\n"),
+       info->callbacks->info ("  %s: 0x%v\n", f1, (bfd_vma) cum_stack);
+      info->callbacks->minfo ("%s: 0x%v 0x%v\n",
                              f1, (bfd_vma) stack, (bfd_vma) cum_stack);
 
       if (has_call)
        {
          info->callbacks->minfo (_("  calls:\n"));
          for (call = fun->call_list; call; call = call->next)
-           if (!call->is_pasted)
+           if (!call->is_pasted && !call->broken_cycle)
              {
                const char *f2 = func_name (call->fun);
                const char *ann1 = call->fun == max ? "*" : " ";
                const char *ann2 = call->is_tail ? "t" : " ";
 
-               info->callbacks->minfo (_("   %s%s %s\n"), ann1, ann2, f2);
+               info->callbacks->minfo ("   %s%s %s\n", ann1, ann2, f2);
              }
        }
     }
@@ -3867,14 +4042,14 @@ sum_stack (struct function_info *fun,
       struct elf_link_hash_entry *h;
 
       if (name == NULL)
-       return FALSE;
+       return false;
 
       if (fun->global || ELF_ST_BIND (fun->u.sym->st_info) == STB_GLOBAL)
        sprintf (name, "__stack_%s", f1);
       else
        sprintf (name, "__stack_%x_%s", fun->sec->id & 0xffffffff, f1);
 
-      h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
+      h = elf_link_hash_lookup (&htab->elf, name, true, true, false);
       free (name);
       if (h != NULL
          && (h->root.type == bfd_link_hash_new
@@ -3894,7 +4069,7 @@ sum_stack (struct function_info *fun,
        }
     }
 
-  return TRUE;
+  return true;
 }
 
 /* SEC is part of a pasted function.  Return the call_info for the
@@ -3924,7 +4099,7 @@ sort_bfds (const void *a, const void *b)
   bfd *const *abfd1 = a;
   bfd *const *abfd2 = b;
 
-  return strcmp ((*abfd1)->filename, (*abfd2)->filename);
+  return filename_cmp (bfd_get_filename (*abfd1), bfd_get_filename (*abfd2));
 }
 
 static unsigned int
@@ -3937,16 +4112,16 @@ print_one_overlay_section (FILE *script,
                           struct bfd_link_info *info)
 {
   unsigned int j;
-         
+
   for (j = base; j < count && ovly_map[j] == ovlynum; j++)
     {
       asection *sec = ovly_sections[2 * j];
 
       if (fprintf (script, "   %s%c%s (%s)\n",
                   (sec->owner->my_archive != NULL
-                   ? sec->owner->my_archive->filename : ""),
+                   ? bfd_get_filename (sec->owner->my_archive) : ""),
                   info->path_separator,
-                  sec->owner->filename,
+                  bfd_get_filename (sec->owner),
                   sec->name) <= 0)
        return -1;
       if (sec->segment_mark)
@@ -3958,9 +4133,9 @@ print_one_overlay_section (FILE *script,
              sec = call_fun->sec;
              if (fprintf (script, "   %s%c%s (%s)\n",
                           (sec->owner->my_archive != NULL
-                           ? sec->owner->my_archive->filename : ""),
+                           ? bfd_get_filename (sec->owner->my_archive) : ""),
                           info->path_separator,
-                          sec->owner->filename,
+                          bfd_get_filename (sec->owner),
                           sec->name) <= 0)
                return -1;
              for (call = call_fun->call_list; call; call = call->next)
@@ -3976,9 +4151,9 @@ print_one_overlay_section (FILE *script,
       if (sec != NULL
          && fprintf (script, "   %s%c%s (%s)\n",
                      (sec->owner->my_archive != NULL
-                      ? sec->owner->my_archive->filename : ""),
+                      ? bfd_get_filename (sec->owner->my_archive) : ""),
                      info->path_separator,
-                     sec->owner->filename,
+                     bfd_get_filename (sec->owner),
                      sec->name) <= 0)
        return -1;
 
@@ -3993,9 +4168,9 @@ print_one_overlay_section (FILE *script,
              if (sec != NULL
                  && fprintf (script, "   %s%c%s (%s)\n",
                              (sec->owner->my_archive != NULL
-                              ? sec->owner->my_archive->filename : ""),
+                              ? bfd_get_filename (sec->owner->my_archive) : ""),
                              info->path_separator,
-                             sec->owner->filename,
+                             bfd_get_filename (sec->owner),
                              sec->name) <= 0)
                return -1;
              for (call = call_fun->call_list; call; call = call->next)
@@ -4010,9 +4185,6 @@ print_one_overlay_section (FILE *script,
 
 /* Handle --auto-overlay.  */
 
-static void spu_elf_auto_overlay (struct bfd_link_info *)
-     ATTRIBUTE_NORETURN;
-
 static void
 spu_elf_auto_overlay (struct bfd_link_info *info)
 {
@@ -4020,6 +4192,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   bfd **bfd_arr;
   struct elf_segment_map *m;
   unsigned int fixed_size, lo, hi;
+  unsigned int reserved;
   struct spu_link_hash_table *htab;
   unsigned int base, i, count, bfd_count;
   unsigned int region, ovlynum;
@@ -4036,7 +4209,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   /* Find the extents of our loadable image.  */
   lo = (unsigned int) -1;
   hi = 0;
-  for (m = elf_tdata (info->output_bfd)->segment_map; m != NULL; m = m->next)
+  for (m = elf_seg_map (info->output_bfd); m != NULL; m = m->next)
     if (m->p_type == PT_LOAD)
       for (i = 0; i < m->count; i++)
        if (m->sections[i]->size != 0)
@@ -4054,16 +4227,37 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   if (!build_call_tree (info))
     goto err_exit;
 
+  htab = spu_hash_table (info);
+  reserved = htab->params->auto_overlay_reserved;
+  if (reserved == 0)
+    {
+      struct _sum_stack_param sum_stack_param;
+
+      sum_stack_param.emit_stack_syms = 0;
+      sum_stack_param.overall_stack = 0;
+      if (!for_each_node (sum_stack, info, &sum_stack_param, true))
+       goto err_exit;
+      reserved = (sum_stack_param.overall_stack
+                 + htab->params->extra_stack_space);
+    }
+
+  /* No need for overlays if everything already fits.  */
+  if (fixed_size + reserved <= htab->local_store
+      && htab->params->ovly_flavour != ovly_soft_icache)
+    {
+      htab->params->auto_overlay = 0;
+      return;
+    }
+
   uos_param.exclude_input_section = 0;
   uos_param.exclude_output_section
     = bfd_get_section_by_name (info->output_bfd, ".interrupt");
 
-  htab = spu_hash_table (info);
   ovly_mgr_entry = "__ovly_load";
   if (htab->params->ovly_flavour == ovly_soft_icache)
     ovly_mgr_entry = "__icache_br_handler";
   h = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry,
-                           FALSE, FALSE, FALSE);
+                           false, false, false);
   if (h != NULL
       && (h->root.type == bfd_link_hash_defined
          || h->root.type == bfd_link_hash_defweak)
@@ -4081,7 +4275,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
 
   /* Mark overlay sections, and find max overlay section size.  */
   mos_param.max_overlay_size = 0;
-  if (!for_each_node (mark_overlay_section, info, &mos_param, TRUE))
+  if (!for_each_node (mark_overlay_section, info, &mos_param, true))
     goto err_exit;
 
   /* We can't put the overlay manager or interrupt routines in
@@ -4089,11 +4283,11 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   uos_param.clearing = 0;
   if ((uos_param.exclude_input_section
        || uos_param.exclude_output_section)
-      && !for_each_node (unmark_overlay_section, info, &uos_param, TRUE))
+      && !for_each_node (unmark_overlay_section, info, &uos_param, true))
     goto err_exit;
 
   bfd_count = 0;
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     ++bfd_count;
   bfd_arr = bfd_malloc (bfd_count * sizeof (*bfd_arr));
   if (bfd_arr == NULL)
@@ -4103,13 +4297,13 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   count = 0;
   bfd_count = 0;
   total_overlay_size = 0;
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
     {
-      extern const bfd_target bfd_elf32_spu_vec;
+      extern const bfd_target spu_elf32_vec;
       asection *sec;
       unsigned int old_count;
 
-      if (ibfd->xvec != &bfd_elf32_spu_vec)
+      if (ibfd->xvec != &spu_elf32_vec)
        continue;
 
       old_count = count;
@@ -4123,7 +4317,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
          }
        else if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD)
                 && sec->output_section->owner == info->output_bfd
-                && strncmp (sec->output_section->name, ".ovl.init", 9) == 0)
+                && startswith (sec->output_section->name, ".ovl.init"))
          fixed_size -= sec->size;
       if (count != old_count)
        bfd_arr[bfd_count++] = ibfd;
@@ -4133,22 +4327,24 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
      section name, ensure that file names are unique.  */
   if (bfd_count > 1)
     {
-      bfd_boolean ok = TRUE;
+      bool ok = true;
 
       qsort (bfd_arr, bfd_count, sizeof (*bfd_arr), sort_bfds);
       for (i = 1; i < bfd_count; ++i)
-       if (strcmp (bfd_arr[i - 1]->filename, bfd_arr[i]->filename) == 0)
+       if (filename_cmp (bfd_get_filename (bfd_arr[i - 1]),
+                         bfd_get_filename (bfd_arr[i])) == 0)
          {
            if (bfd_arr[i - 1]->my_archive == bfd_arr[i]->my_archive)
              {
                if (bfd_arr[i - 1]->my_archive && bfd_arr[i]->my_archive)
+                 /* xgettext:c-format */
                  info->callbacks->einfo (_("%s duplicated in %s\n"),
-                                         bfd_arr[i]->filename,
-                                         bfd_arr[i]->my_archive->filename);
+                                         bfd_get_filename (bfd_arr[i]),
+                                         bfd_get_filename (bfd_arr[i]->my_archive));
                else
                  info->callbacks->einfo (_("%s duplicated\n"),
-                                         bfd_arr[i]->filename);
-               ok = FALSE;
+                                         bfd_get_filename (bfd_arr[i]));
+               ok = false;
              }
          }
       if (!ok)
@@ -4161,18 +4357,8 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
     }
   free (bfd_arr);
 
-  if (htab->reserved == 0)
-    {
-      struct _sum_stack_param sum_stack_param;
-
-      sum_stack_param.emit_stack_syms = 0;
-      sum_stack_param.overall_stack = 0;
-      if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE))
-       goto err_exit;
-      htab->reserved = sum_stack_param.overall_stack + htab->extra_stack_space;
-    }
-  fixed_size += htab->reserved;
-  fixed_size += htab->non_ovly_stub * ovl_stub_size (htab->params->ovly_flavour);
+  fixed_size += reserved;
+  fixed_size += htab->non_ovly_stub * ovl_stub_size (htab->params);
   if (fixed_size + mos_param.max_overlay_size <= htab->local_store)
     {
       if (htab->params->ovly_flavour == ovly_soft_icache)
@@ -4181,16 +4367,16 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
          fixed_size += htab->non_ovly_stub * 16;
          /* Space for icache manager tables.
             a) Tag array, one quadword per cache line.
-            - word 0: ia address of present line, init to zero.
-            - word 1: link locator.  link_elem=stub_addr/2+locator
-            - halfwords 4-7: head/tail pointers for linked lists.  */
+            - word 0: ia address of present line, init to zero.  */
+         fixed_size += 16 << htab->num_lines_log2;
+         /* b) Rewrite "to" list, one quadword per cache line.  */
          fixed_size += 16 << htab->num_lines_log2;
-         /* b) Linked list elements, max_branch per line.  */
-         fixed_size += htab->params->max_branch << (htab->num_lines_log2 + 4);
-         /* c) Indirect branch descriptors, 8 quadwords.  */
-         fixed_size += 8 * 16;
-         /* d) Pointers to __ea backing store, 16 quadwords.  */
-         fixed_size += 16 * 16;
+         /* c) Rewrite "from" list, one byte per outgoing branch (rounded up
+               to a power-of-two number of full quadwords) per cache line.  */
+         fixed_size += 16 << (htab->fromelem_size_log2
+                              + htab->num_lines_log2);
+         /* d) Pointer to __ea backing store (toe), 1 quadword.  */
+         fixed_size += 16;
        }
       else
        {
@@ -4204,19 +4390,20 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
     }
 
   if (fixed_size + mos_param.max_overlay_size > htab->local_store)
+    /* xgettext:c-format */
     info->callbacks->einfo (_("non-overlay size of 0x%v plus maximum overlay "
                              "size of 0x%v exceeds local store\n"),
                            (bfd_vma) fixed_size,
                            (bfd_vma) mos_param.max_overlay_size);
 
   /* Now see if we should put some functions in the non-overlay area.  */
-  else if (fixed_size < htab->overlay_fixed)
+  else if (fixed_size < htab->params->auto_overlay_fixed)
     {
       unsigned int max_fixed, lib_size;
 
       max_fixed = htab->local_store - mos_param.max_overlay_size;
-      if (max_fixed > htab->overlay_fixed)
-       max_fixed = htab->overlay_fixed;
+      if (max_fixed > htab->params->auto_overlay_fixed)
+       max_fixed = htab->params->auto_overlay_fixed;
       lib_size = max_fixed - fixed_size;
       lib_size = auto_ovl_lib_functions (info, lib_size);
       if (lib_size == (unsigned int) -1)
@@ -4230,7 +4417,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   if (ovly_sections == NULL)
     goto err_exit;
   ovly_p = ovly_sections;
-  if (!for_each_node (collect_overlays, info, &ovly_p, TRUE))
+  if (!for_each_node (collect_overlays, info, &ovly_p, true))
     goto err_exit;
   count = (size_t) (ovly_p - ovly_sections) / 2;
   ovly_map = bfd_malloc (count * sizeof (*ovly_map));
@@ -4245,43 +4432,56 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
   ovlynum = 0;
   while (base < count)
     {
-      unsigned int size = 0;
+      unsigned int size = 0, rosize = 0, roalign = 0;
 
       for (i = base; i < count; i++)
        {
-         asection *sec;
-         unsigned int tmp;
+         asection *sec, *rosec;
+         unsigned int tmp, rotmp;
          unsigned int num_stubs;
          struct call_info *call, *pasty;
          struct _spu_elf_section_data *sec_data;
          struct spu_elf_stack_info *sinfo;
-         int k;
+         unsigned int k;
 
          /* See whether we can add this section to the current
             overlay without overflowing our overlay buffer.  */
          sec = ovly_sections[2 * i];
-         tmp = size + sec->size;
-         if (ovly_sections[2 * i + 1])
-           tmp += ovly_sections[2 * i + 1]->size;
-         if (tmp > overlay_size)
+         tmp = align_power (size, sec->alignment_power) + sec->size;
+         rotmp = rosize;
+         rosec = ovly_sections[2 * i + 1];
+         if (rosec != NULL)
+           {
+             rotmp = align_power (rotmp, rosec->alignment_power) + rosec->size;
+             if (roalign < rosec->alignment_power)
+               roalign = rosec->alignment_power;
+           }
+         if (align_power (tmp, roalign) + rotmp > overlay_size)
            break;
          if (sec->segment_mark)
            {
              /* Pasted sections must stay together, so add their
                 sizes too.  */
-             struct call_info *pasty = find_pasted_call (sec);
+             pasty = find_pasted_call (sec);
              while (pasty != NULL)
                {
                  struct function_info *call_fun = pasty->fun;
-                 tmp += call_fun->sec->size;
+                 tmp = (align_power (tmp, call_fun->sec->alignment_power)
+                        + call_fun->sec->size);
                  if (call_fun->rodata)
-                   tmp += call_fun->rodata->size;
+                   {
+                     rotmp = (align_power (rotmp,
+                                           call_fun->rodata->alignment_power)
+                              + call_fun->rodata->size);
+                     if (roalign < rosec->alignment_power)
+                       roalign = rosec->alignment_power;
+                   }
                  for (pasty = call_fun->call_list; pasty; pasty = pasty->next)
                    if (pasty->is_pasted)
                      break;
                }
            }
-         if (tmp > overlay_size)
+         if (align_power (tmp, roalign) + rotmp > overlay_size)
            break;
 
          /* If we add this section, we might need new overlay call
@@ -4289,7 +4489,7 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
          pasty = NULL;
          sec_data = spu_elf_section_data (sec);
          sinfo = sec_data->u.i.stack_info;
-         for (k = 0; k < sinfo->num_fun; ++k)
+         for (k = 0; k < (unsigned) sinfo->num_fun; ++k)
            for (call = sinfo->fun[k].call_list; call; call = call->next)
              if (call->is_pasted)
                {
@@ -4319,30 +4519,35 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
          num_stubs = 0;
          for (call = dummy_caller.call_list; call; call = call->next)
            {
-             unsigned int k;
+             unsigned int stub_delta = 1;
+
+             if (htab->params->ovly_flavour == ovly_soft_icache)
+               stub_delta = call->count;
+             num_stubs += stub_delta;
 
-             ++num_stubs;
              /* If the call is within this overlay, we won't need a
                 stub.  */
              for (k = base; k < i + 1; k++)
                if (call->fun->sec == ovly_sections[2 * k])
                  {
-                   --num_stubs;
+                   num_stubs -= stub_delta;
                    break;
                  }
            }
          if (htab->params->ovly_flavour == ovly_soft_icache
              && num_stubs > htab->params->max_branch)
            break;
-         if (tmp + num_stubs * ovl_stub_size (htab->params->ovly_flavour)
-             > overlay_size)
+         if (align_power (tmp, roalign) + rotmp
+             + num_stubs * ovl_stub_size (htab->params) > overlay_size)
            break;
          size = tmp;
+         rosize = rotmp;
        }
 
       if (i == base)
        {
-         info->callbacks->einfo (_("%B:%A%s exceeds overlay size\n"),
+         /* xgettext:c-format */
+         info->callbacks->einfo (_("%pB:%pA%s exceeds overlay size\n"),
                                  ovly_sections[2 * i]->owner,
                                  ovly_sections[2 * i],
                                  ovly_sections[2 * i + 1] ? " + rodata" : "");
@@ -4364,13 +4569,12 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
 
   script = htab->params->spu_elf_open_overlay_script ();
 
-  if (fprintf (script, "SECTIONS\n{\n") <= 0)
-    goto file_err;
-
   if (htab->params->ovly_flavour == ovly_soft_icache)
     {
+      if (fprintf (script, "SECTIONS\n{\n") <= 0)
+       goto file_err;
+
       if (fprintf (script,
-                  " .data.icache ALIGN (16) : { *(.ovtab) *(.data.icache) }\n"
                   " . = ALIGN (%u);\n"
                   " .ovl.init : { *(.ovl.init) }\n"
                   " . = ABSOLUTE (ADDR (.ovl.init));\n",
@@ -4385,10 +4589,10 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
          unsigned int vma, lma;
 
          vma = (indx & (htab->params->num_lines - 1)) << htab->line_size_log2;
-         lma = indx << htab->line_size_log2;
+         lma = vma + (((indx >> htab->num_lines_log2) + 1) << 18);
 
          if (fprintf (script, " .ovly%u ABSOLUTE (ADDR (.ovl.init)) + %u "
-                      ": AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16) + %u) {\n",
+                              ": AT (LOADADDR (.ovl.init) + %u) {\n",
                       ovlynum, vma, lma) <= 0)
            goto file_err;
 
@@ -4406,9 +4610,15 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
       if (fprintf (script, " . = ABSOLUTE (ADDR (.ovl.init)) + %u;\n",
                   1 << (htab->num_lines_log2 + htab->line_size_log2)) <= 0)
        goto file_err;
+
+      if (fprintf (script, "}\nINSERT AFTER .toe;\n") <= 0)
+       goto file_err;
     }
   else
     {
+      if (fprintf (script, "SECTIONS\n{\n") <= 0)
+       goto file_err;
+
       if (fprintf (script,
                   " . = ALIGN (16);\n"
                   " .ovl.init : { *(.ovl.init) }\n"
@@ -4460,13 +4670,13 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
            goto file_err;
        }
 
+      if (fprintf (script, "}\nINSERT BEFORE .text;\n") <= 0)
+       goto file_err;
     }
 
   free (ovly_map);
   free (ovly_sections);
 
-  if (fprintf (script, "}\nINSERT BEFORE .text;\n") <= 0)
-    goto file_err;
   if (fclose (script) != 0)
     goto file_err;
 
@@ -4478,23 +4688,23 @@ spu_elf_auto_overlay (struct bfd_link_info *info)
  file_err:
   bfd_set_error (bfd_error_system_call);
  err_exit:
-  info->callbacks->einfo ("%F%P: auto overlay error: %E\n");
+  info->callbacks->einfo (_("%F%P: auto overlay error: %E\n"));
   xexit (1);
 }
 
 /* Provide an estimate of total stack required.  */
 
-static bfd_boolean
+static bool
 spu_elf_stack_analysis (struct bfd_link_info *info)
 {
   struct spu_link_hash_table *htab;
   struct _sum_stack_param sum_stack_param;
 
   if (!discover_functions (info))
-    return FALSE;
+    return false;
 
   if (!build_call_tree (info))
-    return FALSE;
+    return false;
 
   htab = spu_hash_table (info);
   if (htab->params->stack_analysis)
@@ -4506,18 +4716,18 @@ spu_elf_stack_analysis (struct bfd_link_info *info)
 
   sum_stack_param.emit_stack_syms = htab->params->emit_stack_syms;
   sum_stack_param.overall_stack = 0;
-  if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE))
-    return FALSE;
+  if (!for_each_node (sum_stack, info, &sum_stack_param, true))
+    return false;
 
   if (htab->params->stack_analysis)
     info->callbacks->info (_("Maximum stack required is 0x%v\n"),
                           (bfd_vma) sum_stack_param.overall_stack);
-  return TRUE;
+  return true;
 }
 
 /* Perform a final link.  */
 
-static bfd_boolean
+static bool
 spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info)
 {
   struct spu_link_hash_table *htab = spu_hash_table (info);
@@ -4529,15 +4739,15 @@ spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info)
        || (htab->params->ovly_flavour == ovly_soft_icache
           && htab->params->lrlive_analysis))
       && !spu_elf_stack_analysis (info))
-    info->callbacks->einfo ("%X%P: stack/lrlive analysis error: %E\n");
+    info->callbacks->einfo (_("%X%P: stack/lrlive analysis error: %E\n"));
 
   if (!spu_elf_build_stubs (info))
-    info->callbacks->einfo ("%F%P: can not build overlay stubs: %E\n");
+    info->callbacks->einfo (_("%F%P: can not build overlay stubs: %E\n"));
 
   return bfd_elf_final_link (output_bfd, info);
 }
 
-/* Called when not normally emitting relocs, ie. !info->relocatable
+/* Called when not normally emitting relocs, ie. !bfd_link_relocatable (info)
    and !info->emitrelocations.  Returns a count of special relocs
    that need to be emitted.  */
 
@@ -4568,6 +4778,48 @@ spu_elf_count_relocs (struct bfd_link_info *info, asection *sec)
   return count;
 }
 
+/* Functions for adding fixup records to .fixup */
+
+#define FIXUP_RECORD_SIZE 4
+
+#define FIXUP_PUT(output_bfd,htab,index,addr) \
+         bfd_put_32 (output_bfd, addr, \
+                     htab->sfixup->contents + FIXUP_RECORD_SIZE * (index))
+#define FIXUP_GET(output_bfd,htab,index) \
+         bfd_get_32 (output_bfd, \
+                     htab->sfixup->contents + FIXUP_RECORD_SIZE * (index))
+
+/* Store OFFSET in .fixup.  This assumes it will be called with an
+   increasing OFFSET.  When this OFFSET fits with the last base offset,
+   it just sets a bit, otherwise it adds a new fixup record.  */
+static void
+spu_elf_emit_fixup (bfd * output_bfd, struct bfd_link_info *info,
+                   bfd_vma offset)
+{
+  struct spu_link_hash_table *htab = spu_hash_table (info);
+  asection *sfixup = htab->sfixup;
+  bfd_vma qaddr = offset & ~(bfd_vma) 15;
+  bfd_vma bit = ((bfd_vma) 8) >> ((offset & 15) >> 2);
+  if (sfixup->reloc_count == 0)
+    {
+      FIXUP_PUT (output_bfd, htab, 0, qaddr | bit);
+      sfixup->reloc_count++;
+    }
+  else
+    {
+      bfd_vma base = FIXUP_GET (output_bfd, htab, sfixup->reloc_count - 1);
+      if (qaddr != (base & ~(bfd_vma) 15))
+       {
+         if ((sfixup->reloc_count + 1) * FIXUP_RECORD_SIZE > sfixup->size)
+           _bfd_error_handler (_("fatal error while creating .fixup"));
+         FIXUP_PUT (output_bfd, htab, sfixup->reloc_count, qaddr | bit);
+         sfixup->reloc_count++;
+       }
+      else
+       FIXUP_PUT (output_bfd, htab, sfixup->reloc_count - 1, base | bit);
+    }
+}
+
 /* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD.  */
 
 static int
@@ -4585,10 +4837,10 @@ spu_elf_relocate_section (bfd *output_bfd,
   Elf_Internal_Rela *rel, *relend;
   struct spu_link_hash_table *htab;
   asection *ea;
-  int ret = TRUE;
-  bfd_boolean emit_these_relocs = FALSE;
-  bfd_boolean is_ea_sym;
-  bfd_boolean stubs;
+  int ret = true;
+  bool emit_these_relocs = false;
+  bool is_ea_sym;
+  bool stubs;
   unsigned int iovl = 0;
 
   htab = spu_hash_table (info);
@@ -4613,16 +4865,13 @@ spu_elf_relocate_section (bfd *output_bfd,
       bfd_vma relocation;
       bfd_vma addend;
       bfd_reloc_status_type r;
-      bfd_boolean unresolved_reloc;
-      bfd_boolean warned;
-      bfd_boolean overlay_encoded;
+      bool unresolved_reloc;
       enum _stub_type stub_type;
 
       r_symndx = ELF32_R_SYM (rel->r_info);
       r_type = ELF32_R_TYPE (rel->r_info);
       howto = elf_howto_table + r_type;
-      unresolved_reloc = FALSE;
-      warned = FALSE;
+      unresolved_reloc = false;
       h = NULL;
       sym = NULL;
       sec = NULL;
@@ -4636,10 +4885,15 @@ spu_elf_relocate_section (bfd *output_bfd,
       else
        {
          if (sym_hashes == NULL)
-           return FALSE;
+           return false;
 
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
 
+         if (info->wrap_hash != NULL
+             && (input_section->flags & SEC_DEBUGGING) != 0)
+           h = ((struct elf_link_hash_entry *)
+                unwrap_hash_lookup (info, input_bfd, &h->root));
+
          while (h->root.type == bfd_link_hash_indirect
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
@@ -4655,7 +4909,7 @@ spu_elf_relocate_section (bfd *output_bfd,
                   relocation value for this symbol.  output_section
                   is typically NULL for symbols satisfied by a shared
                   library.  */
-               unresolved_reloc = TRUE;
+               unresolved_reloc = true;
              else
                relocation = (h->root.u.def.value
                              + sec->output_section->vma
@@ -4666,41 +4920,43 @@ spu_elf_relocate_section (bfd *output_bfd,
          else if (info->unresolved_syms_in_objects == RM_IGNORE
                   && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
            ;
-         else if (!info->relocatable
+         else if (!bfd_link_relocatable (info)
                   && !(r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64))
            {
-             bfd_boolean err;
-             err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
-                    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT);
-             if (!info->callbacks->undefined_symbol (info,
-                                                     h->root.root.string,
-                                                     input_bfd,
-                                                     input_section,
-                                                     rel->r_offset, err))
-               return FALSE;
-             warned = TRUE;
+             bool err;
+
+             err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
+                    && !info->warn_unresolved_syms)
+               || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT;
+
+             info->callbacks->undefined_symbol
+               (info, h->root.root.string, input_bfd,
+                input_section, rel->r_offset, err);
            }
          sym_name = h->root.root.string;
        }
 
-      if (sec != NULL && elf_discarded_section (sec))
-       {
-         /* For relocs against symbols from removed linkonce sections,
-            or sections discarded by a linker script, we just want the
-            section contents zeroed.  Avoid any special processing.  */
-         _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
-         rel->r_info = 0;
-         rel->r_addend = 0;
-         continue;
-       }
+      if (sec != NULL && discarded_section (sec))
+       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
+                                        rel, 1, relend, howto, 0, contents);
 
-      if (info->relocatable)
+      if (bfd_link_relocatable (info))
        continue;
 
+      /* Change "a rt,ra,rb" to "ai rt,ra,0". */
+      if (r_type == R_SPU_ADD_PIC
+         && h != NULL
+         && !(h->def_regular || ELF_COMMON_DEF_P (h)))
+       {
+         bfd_byte *loc = contents + rel->r_offset;
+         loc[0] = 0x1c;
+         loc[1] = 0x00;
+         loc[2] &= 0x3f;
+       }
+
       is_ea_sym = (ea != NULL
                   && sec != NULL
                   && sec->output_section == ea);
-      overlay_encoded = FALSE;
 
       /* If this symbol is in an overlay area, we may need to relocate
         to the overlay stub.  */
@@ -4723,9 +4979,10 @@ spu_elf_relocate_section (bfd *output_bfd,
 
          for (g = *head; g != NULL; g = g->next)
            if (htab->params->ovly_flavour == ovly_soft_icache
-               ? g->br_addr == (rel->r_offset
-                                + input_section->output_offset
-                                + input_section->output_section->vma)
+               ? (g->ovl == ovl
+                  && g->br_addr == (rel->r_offset
+                                    + input_section->output_offset
+                                    + input_section->output_section->vma))
                : g->addend == addend && (g->ovl == ovl || g->ovl == 0))
              break;
          if (g == NULL)
@@ -4738,18 +4995,29 @@ spu_elf_relocate_section (bfd *output_bfd,
        {
          /* For soft icache, encode the overlay index into addresses.  */
          if (htab->params->ovly_flavour == ovly_soft_icache
+             && (r_type == R_SPU_ADDR16_HI
+                 || r_type == R_SPU_ADDR32 || r_type == R_SPU_REL32)
              && !is_ea_sym)
            {
              unsigned int ovl = overlay_index (sec);
              if (ovl != 0)
                {
-                 unsigned int set_id = (ovl - 1) >> htab->num_lines_log2;
+                 unsigned int set_id = ((ovl - 1) >> htab->num_lines_log2) + 1;
                  relocation += set_id << 18;
-                 overlay_encoded = set_id != 0;
                }
            }
        }
 
+      if (htab->params->emit_fixups && !bfd_link_relocatable (info)
+         && (input_section->flags & SEC_ALLOC) != 0
+         && r_type == R_SPU_ADDR32)
+       {
+         bfd_vma offset;
+         offset = rel->r_offset + input_section->output_section->vma
+                  + input_section->output_offset;
+         spu_elf_emit_fixup (output_bfd, info, offset);
+       }
+
       if (unresolved_reloc)
        ;
       else if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
@@ -4767,22 +5035,26 @@ spu_elf_relocate_section (bfd *output_bfd,
                                + elf_section_data (ea)->this_hdr.sh_offset);
              rel->r_info = ELF32_R_INFO (0, r_type);
            }
-         emit_these_relocs = TRUE;
+         emit_these_relocs = true;
          continue;
        }
       else if (is_ea_sym)
-       unresolved_reloc = TRUE;
+       unresolved_reloc = true;
 
-      if (unresolved_reloc)
+      if (unresolved_reloc
+         && _bfd_elf_section_offset (output_bfd, info, input_section,
+                                     rel->r_offset) != (bfd_vma) -1)
        {
-         (*_bfd_error_handler)
-           (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
+         _bfd_error_handler
+           /* xgettext:c-format */
+           (_("%pB(%s+%#" PRIx64 "): "
+              "unresolvable %s relocation against symbol `%s'"),
             input_bfd,
-            bfd_get_section_name (input_bfd, input_section),
-            (long) rel->r_offset,
+            bfd_section_name (input_section),
+            (uint64_t) rel->r_offset,
             howto->name,
             sym_name);
-         ret = FALSE;
+         ret = false;
        }
 
       r = _bfd_final_link_relocate (howto,
@@ -4798,22 +5070,14 @@ spu_elf_relocate_section (bfd *output_bfd,
          switch (r)
            {
            case bfd_reloc_overflow:
-             /* FIXME: We don't want to warn on most references
-                within an overlay to itself, but this may silence a
-                warning that should be reported.  */
-             if (overlay_encoded && sec == input_section)
-               break;
-             if (!((*info->callbacks->reloc_overflow)
-                   (info, (h ? &h->root : NULL), sym_name, howto->name,
-                    (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
-               return FALSE;
+             (*info->callbacks->reloc_overflow)
+               (info, (h ? &h->root : NULL), sym_name, howto->name,
+                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
              break;
 
            case bfd_reloc_undefined:
-             if (!((*info->callbacks->undefined_symbol)
-                   (info, sym_name, input_bfd, input_section,
-                    rel->r_offset, TRUE)))
-               return FALSE;
+             (*info->callbacks->undefined_symbol)
+               (info, sym_name, input_bfd, input_section, rel->r_offset, true);
              break;
 
            case bfd_reloc_outofrange:
@@ -4833,11 +5097,9 @@ spu_elf_relocate_section (bfd *output_bfd,
              /* fall through */
 
            common_error:
-             ret = FALSE;
-             if (!((*info->callbacks->warning)
-                   (info, msg, sym_name, input_bfd, input_section,
-                    rel->r_offset)))
-               return FALSE;
+             ret = false;
+             (*info->callbacks->warning) (info, msg, sym_name, input_bfd,
+                                          input_section, rel->r_offset);
              break;
            }
        }
@@ -4862,7 +5124,7 @@ spu_elf_relocate_section (bfd *output_bfd,
        }
       input_section->reloc_count = wrel - relocs;
       /* Backflips for _bfd_elf_link_output_relocs.  */
-      rel_hdr = &elf_section_data (input_section)->rel_hdr;
+      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
       rel_hdr->sh_size = input_section->reloc_count * rel_hdr->sh_entsize;
       ret = 2;
     }
@@ -4870,9 +5132,16 @@ spu_elf_relocate_section (bfd *output_bfd,
   return ret;
 }
 
+static bool
+spu_elf_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
+                                struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+  return true;
+}
+
 /* Adjust _SPUEAR_ syms to point at their overlay stubs.  */
 
-static bfd_boolean
+static int
 spu_elf_output_symbol_hook (struct bfd_link_info *info,
                            const char *sym_name ATTRIBUTE_UNUSED,
                            Elf_Internal_Sym *sym,
@@ -4881,13 +5150,13 @@ spu_elf_output_symbol_hook (struct bfd_link_info *info,
 {
   struct spu_link_hash_table *htab = spu_hash_table (info);
 
-  if (!info->relocatable
+  if (!bfd_link_relocatable (info)
       && htab->stub_sec != NULL
       && h != NULL
       && (h->root.type == bfd_link_hash_defined
          || h->root.type == bfd_link_hash_defweak)
       && h->def_regular
-      && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0)
+      && startswith (h->root.root.string, "_SPUEAR_"))
     {
       struct got_entry *g;
 
@@ -4904,7 +5173,7 @@ spu_elf_output_symbol_hook (struct bfd_link_info *info,
          }
     }
 
-  return TRUE;
+  return 1;
 }
 
 static int spu_plugin = 0;
@@ -4917,16 +5186,19 @@ spu_elf_plugin (int val)
 
 /* Set ELF header e_type for plugins.  */
 
-static void
-spu_elf_post_process_headers (bfd *abfd,
-                             struct bfd_link_info *info ATTRIBUTE_UNUSED)
+static bool
+spu_elf_init_file_header (bfd *abfd, struct bfd_link_info *info)
 {
+  if (!_bfd_elf_init_file_header (abfd, info))
+    return false;
+
   if (spu_plugin)
     {
       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
 
       i_ehdrp->e_type = ET_DYN;
     }
+  return true;
 }
 
 /* We may add an extra PT_LOAD segment for .toe.  We also need extra
@@ -4957,18 +5229,19 @@ spu_elf_additional_program_headers (bfd *abfd, struct bfd_link_info *info)
 /* Remove .toe section from other PT_LOAD segments and put it in
    a segment of its own.  Put overlays in separate segments too.  */
 
-static bfd_boolean
+static bool
 spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
 {
   asection *toe, *s;
-  struct elf_segment_map *m;
+  struct elf_segment_map *m, *m_overlay;
+  struct elf_segment_map **p, **p_overlay, **first_load;
   unsigned int i;
 
   if (info == NULL)
-    return TRUE;
+    return true;
 
   toe = bfd_get_section_by_name (abfd, ".toe");
-  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
+  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
     if (m->p_type == PT_LOAD && m->count > 1)
       for (i = 0; i < m->count; i++)
        if ((s = m->sections[i]) == toe
@@ -4983,7 +5256,7 @@ spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
                amt += (m->count - (i + 2)) * sizeof (m->sections[0]);
                m2 = bfd_zalloc (abfd, amt);
                if (m2 == NULL)
-                 return FALSE;
+                 return false;
                m2->count = m->count - (i + 1);
                memcpy (m2->sections, m->sections + i + 1,
                        m2->count * sizeof (m->sections[0]));
@@ -4998,7 +5271,7 @@ spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
                amt = sizeof (struct elf_segment_map);
                m2 = bfd_zalloc (abfd, amt);
                if (m2 == NULL)
-                 return FALSE;
+                 return false;
                m2->p_type = PT_LOAD;
                m2->count = 1;
                m2->sections[0] = s;
@@ -5008,130 +5281,250 @@ spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
            break;
          }
 
-  return TRUE;
+
+  /* Some SPU ELF loaders ignore the PF_OVERLAY flag and just load all
+     PT_LOAD segments.  This can cause the .ovl.init section to be
+     overwritten with the contents of some overlay segment.  To work
+     around this issue, we ensure that all PF_OVERLAY segments are
+     sorted first amongst the program headers; this ensures that even
+     with a broken loader, the .ovl.init section (which is not marked
+     as PF_OVERLAY) will be placed into SPU local store on startup.  */
+
+  /* Move all overlay segments onto a separate list.  */
+  p = &elf_seg_map (abfd);
+  p_overlay = &m_overlay;
+  m_overlay = NULL;
+  first_load = NULL;
+  while (*p != NULL)
+    {
+      if ((*p)->p_type == PT_LOAD)
+       {
+         if (!first_load)
+           first_load = p;
+         if ((*p)->count == 1
+             && spu_elf_section_data ((*p)->sections[0])->u.o.ovl_index != 0)
+           {
+             m = *p;
+             m->no_sort_lma = 1;
+             *p = m->next;
+             *p_overlay = m;
+             p_overlay = &m->next;
+             continue;
+           }
+       }
+      p = &((*p)->next);
+    }
+
+  /* Re-insert overlay segments at the head of the segment map.  */
+  if (m_overlay != NULL)
+    {
+      p = first_load;
+      if (*p != NULL && (*p)->p_type == PT_LOAD && (*p)->includes_filehdr)
+       /* It doesn't really make sense for someone to include the ELF
+          file header into an spu image, but if they do the code that
+          assigns p_offset needs to see the segment containing the
+          header first.  */
+       p = &(*p)->next;
+      *p_overlay = *p;
+      *p = m_overlay;
+    }
+
+  return true;
 }
 
 /* Tweak the section type of .note.spu_name.  */
 
-static bfd_boolean
+static bool
 spu_elf_fake_sections (bfd *obfd ATTRIBUTE_UNUSED,
                       Elf_Internal_Shdr *hdr,
                       asection *sec)
 {
   if (strcmp (sec->name, SPU_PTNOTE_SPUNAME) == 0)
     hdr->sh_type = SHT_NOTE;
-  return TRUE;
+  return true;
 }
 
 /* Tweak phdrs before writing them out.  */
 
-static int
-spu_elf_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
+static bool
+spu_elf_modify_headers (bfd *abfd, struct bfd_link_info *info)
 {
-  const struct elf_backend_data *bed;
-  struct elf_obj_tdata *tdata;
-  Elf_Internal_Phdr *phdr, *last;
-  struct spu_link_hash_table *htab;
-  unsigned int count;
-  unsigned int i;
+  if (info != NULL)
+    {
+      const struct elf_backend_data *bed;
+      struct elf_obj_tdata *tdata;
+      Elf_Internal_Phdr *phdr, *last;
+      struct spu_link_hash_table *htab;
+      unsigned int count;
+      unsigned int i;
+
+      bed = get_elf_backend_data (abfd);
+      tdata = elf_tdata (abfd);
+      phdr = tdata->phdr;
+      count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
+      htab = spu_hash_table (info);
+      if (htab->num_overlays != 0)
+       {
+         struct elf_segment_map *m;
+         unsigned int o;
 
-  if (info == NULL)
-    return TRUE;
+         for (i = 0, m = elf_seg_map (abfd); m; ++i, m = m->next)
+           if (m->count != 0
+               && ((o = spu_elf_section_data (m->sections[0])->u.o.ovl_index)
+                   != 0))
+             {
+               /* Mark this as an overlay header.  */
+               phdr[i].p_flags |= PF_OVERLAY;
 
-  bed = get_elf_backend_data (abfd);
-  tdata = elf_tdata (abfd);
-  phdr = tdata->phdr;
-  count = tdata->program_header_size / bed->s->sizeof_phdr;
-  htab = spu_hash_table (info);
-  if (htab->num_overlays != 0)
-    {
-      struct elf_segment_map *m;
-      unsigned int o;
+               if (htab->ovtab != NULL && htab->ovtab->size != 0
+                   && htab->params->ovly_flavour != ovly_soft_icache)
+                 {
+                   bfd_byte *p = htab->ovtab->contents;
+                   unsigned int off = o * 16 + 8;
+
+                   /* Write file_off into _ovly_table.  */
+                   bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off);
+                 }
+             }
+         /* Soft-icache has its file offset put in .ovl.init.  */
+         if (htab->init != NULL && htab->init->size != 0)
+           {
+             bfd_vma val
+               = elf_section_data (htab->ovl_sec[0])->this_hdr.sh_offset;
 
-      for (i = 0, m = elf_tdata (abfd)->segment_map; m; ++i, m = m->next)
-       if (m->count != 0
-           && (o = spu_elf_section_data (m->sections[0])->u.o.ovl_index) != 0)
+             bfd_put_32 (htab->init->owner, val, htab->init->contents + 4);
+           }
+       }
+
+      /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
+        of 16.  This should always be possible when using the standard
+        linker scripts, but don't create overlapping segments if
+        someone is playing games with linker scripts.  */
+      last = NULL;
+      for (i = count; i-- != 0; )
+       if (phdr[i].p_type == PT_LOAD)
          {
-           /* Mark this as an overlay header.  */
-           phdr[i].p_flags |= PF_OVERLAY;
+           unsigned adjust;
 
-           if (htab->ovtab != NULL && htab->ovtab->size != 0
-               && htab->params->ovly_flavour != ovly_soft_icache)
-             {
-               bfd_byte *p = htab->ovtab->contents;
-               unsigned int off = o * 16 + 8;
+           adjust = -phdr[i].p_filesz & 15;
+           if (adjust != 0
+               && last != NULL
+               && (phdr[i].p_offset + phdr[i].p_filesz
+                   > last->p_offset - adjust))
+             break;
 
-               /* Write file_off into _ovly_table.  */
-               bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off);
-             }
+           adjust = -phdr[i].p_memsz & 15;
+           if (adjust != 0
+               && last != NULL
+               && phdr[i].p_filesz != 0
+               && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust
+               && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr)
+             break;
+
+           if (phdr[i].p_filesz != 0)
+             last = &phdr[i];
          }
-      /* Soft-icache has its file offset put in .ovl.init.  */
-      if (htab->init != NULL && htab->init->size != 0)
-       {
-         bfd_vma val = elf_section_data (htab->ovl_sec[0])->this_hdr.sh_offset;
 
-         bfd_put_32 (htab->init->owner, val, htab->init->contents + 4);
-       }
-    }
+      if (i == (unsigned int) -1)
+       for (i = count; i-- != 0; )
+         if (phdr[i].p_type == PT_LOAD)
+           {
+             unsigned adjust;
 
-  /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
-     of 16.  This should always be possible when using the standard
-     linker scripts, but don't create overlapping segments if
-     someone is playing games with linker scripts.  */
-  last = NULL;
-  for (i = count; i-- != 0; )
-    if (phdr[i].p_type == PT_LOAD)
-      {
-       unsigned adjust;
+             adjust = -phdr[i].p_filesz & 15;
+             phdr[i].p_filesz += adjust;
 
-       adjust = -phdr[i].p_filesz & 15;
-       if (adjust != 0
-           && last != NULL
-           && phdr[i].p_offset + phdr[i].p_filesz > last->p_offset - adjust)
-         break;
+             adjust = -phdr[i].p_memsz & 15;
+             phdr[i].p_memsz += adjust;
+           }
+    }
 
-       adjust = -phdr[i].p_memsz & 15;
-       if (adjust != 0
-           && last != NULL
-           && phdr[i].p_filesz != 0
-           && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust
-           && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr)
-         break;
+  return _bfd_elf_modify_headers (abfd, info);
+}
 
-       if (phdr[i].p_filesz != 0)
-         last = &phdr[i];
-      }
+bool
+spu_elf_size_sections (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
+{
+  struct spu_link_hash_table *htab = spu_hash_table (info);
+  if (htab->params->emit_fixups)
+    {
+      asection *sfixup = htab->sfixup;
+      int fixup_count = 0;
+      bfd *ibfd;
+      size_t size;
 
-  if (i == (unsigned int) -1)
-    for (i = count; i-- != 0; )
-      if (phdr[i].p_type == PT_LOAD)
+      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
        {
-       unsigned adjust;
+         asection *isec;
 
-       adjust = -phdr[i].p_filesz & 15;
-       phdr[i].p_filesz += adjust;
+         if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
+           continue;
 
-       adjust = -phdr[i].p_memsz & 15;
-       phdr[i].p_memsz += adjust;
-      }
+         /* Walk over each section attached to the input bfd.  */
+         for (isec = ibfd->sections; isec != NULL; isec = isec->next)
+           {
+             Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
+             bfd_vma base_end;
+
+             /* If there aren't any relocs, then there's nothing more
+                to do.  */
+             if ((isec->flags & SEC_ALLOC) == 0
+                 || (isec->flags & SEC_RELOC) == 0
+                 || isec->reloc_count == 0)
+               continue;
 
-  return TRUE;
+             /* Get the relocs.  */
+             internal_relocs =
+               _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
+                                          info->keep_memory);
+             if (internal_relocs == NULL)
+               return false;
+
+             /* 1 quadword can contain up to 4 R_SPU_ADDR32
+                relocations.  They are stored in a single word by
+                saving the upper 28 bits of the address and setting the
+                lower 4 bits to a bit mask of the words that have the
+                relocation.  BASE_END keeps track of the next quadword. */
+             irela = internal_relocs;
+             irelaend = irela + isec->reloc_count;
+             base_end = 0;
+             for (; irela < irelaend; irela++)
+               if (ELF32_R_TYPE (irela->r_info) == R_SPU_ADDR32
+                   && irela->r_offset >= base_end)
+                 {
+                   base_end = (irela->r_offset & ~(bfd_vma) 15) + 16;
+                   fixup_count++;
+                 }
+           }
+       }
+
+      /* We always have a NULL fixup as a sentinel */
+      size = (fixup_count + 1) * FIXUP_RECORD_SIZE;
+      if (!bfd_set_section_size (sfixup, size))
+       return false;
+      sfixup->contents = (bfd_byte *) bfd_zalloc (info->input_bfds, size);
+      if (sfixup->contents == NULL)
+       return false;
+    }
+  return true;
 }
 
-#define TARGET_BIG_SYM         bfd_elf32_spu_vec
+#define TARGET_BIG_SYM         spu_elf32_vec
 #define TARGET_BIG_NAME                "elf32-spu"
 #define ELF_ARCH               bfd_arch_spu
+#define ELF_TARGET_ID          SPU_ELF_DATA
 #define ELF_MACHINE_CODE       EM_SPU
 /* This matches the alignment need for DMA.  */
 #define ELF_MAXPAGESIZE                0x80
-#define elf_backend_rela_normal         1
+#define elf_backend_rela_normal                1
 #define elf_backend_can_gc_sections    1
 
 #define bfd_elf32_bfd_reloc_type_lookup                spu_elf_reloc_type_lookup
-#define bfd_elf32_bfd_reloc_name_lookup        spu_elf_reloc_name_lookup
+#define bfd_elf32_bfd_reloc_name_lookup                spu_elf_reloc_name_lookup
 #define elf_info_to_howto                      spu_elf_info_to_howto
 #define elf_backend_count_relocs               spu_elf_count_relocs
 #define elf_backend_relocate_section           spu_elf_relocate_section
+#define elf_backend_finish_dynamic_sections    spu_elf_finish_dynamic_sections
 #define elf_backend_symbol_processing          spu_elf_backend_symbol_processing
 #define elf_backend_link_output_symbol_hook    spu_elf_output_symbol_hook
 #define elf_backend_object_p                   spu_elf_object_p
@@ -5140,8 +5533,8 @@ spu_elf_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
 
 #define elf_backend_additional_program_headers spu_elf_additional_program_headers
 #define elf_backend_modify_segment_map         spu_elf_modify_segment_map
-#define elf_backend_modify_program_headers     spu_elf_modify_program_headers
-#define elf_backend_post_process_headers        spu_elf_post_process_headers
+#define elf_backend_modify_headers             spu_elf_modify_headers
+#define elf_backend_init_file_header           spu_elf_init_file_header
 #define elf_backend_fake_sections              spu_elf_fake_sections
 #define elf_backend_special_sections           spu_elf_special_sections
 #define bfd_elf32_bfd_final_link               spu_elf_final_link
This page took 0.084231 seconds and 4 git commands to generate.