+ struct call_info **pp, *p;
+
+ /* This section fits. Mark it as non-overlay. */
+ lib_sections[2 * i]->linker_mark = 0;
+ if (lib_sections[2 * i + 1])
+ lib_sections[2 * i + 1]->linker_mark = 0;
+ lib_size -= tmp + stub_size;
+ /* Call stubs to the section we just added are no longer
+ needed. */
+ pp = &dummy_caller.call_list;
+ while ((p = *pp) != NULL)
+ if (!p->fun->sec->linker_mark)
+ {
+ lib_size += ovl_stub_size (htab->params);
+ *pp = p->next;
+ free (p);
+ }
+ else
+ pp = &p->next;
+ /* Add new call stubs to dummy_caller. */
+ if ((sec_data = spu_elf_section_data (sec)) != NULL
+ && (sinfo = sec_data->u.i.stack_info) != NULL)
+ {
+ int k;
+ struct call_info *call;
+
+ for (k = 0; k < sinfo->num_fun; ++k)
+ for (call = sinfo->fun[k].call_list;
+ call;
+ call = call->next)
+ if (call->fun->sec->linker_mark)
+ {
+ struct call_info *callee;
+ callee = bfd_malloc (sizeof (*callee));
+ if (callee == NULL)
+ return (unsigned int) -1;
+ *callee = *call;
+ if (!insert_callee (&dummy_caller, callee))
+ free (callee);
+ }
+ }
+ }
+ }
+ while (dummy_caller.call_list != NULL)
+ {
+ struct call_info *call = dummy_caller.call_list;
+ dummy_caller.call_list = call->next;
+ free (call);
+ }
+ for (i = 0; i < 2 * lib_count; i++)
+ if (lib_sections[i])
+ lib_sections[i]->gc_mark = 1;
+ free (lib_sections);
+ return lib_size;
+}
+
+/* Build an array of overlay sections. The deepest node's section is
+ added first, then its parent node's section, then everything called
+ from the parent section. The idea being to group sections to
+ minimise calls between different overlays. */
+
+static bfd_boolean
+collect_overlays (struct function_info *fun,
+ struct bfd_link_info *info,
+ void *param)
+{
+ struct call_info *call;
+ bfd_boolean added_fun;
+ asection ***ovly_sections = param;
+
+ if (fun->visit7)
+ return TRUE;
+
+ fun->visit7 = TRUE;
+ for (call = fun->call_list; call != NULL; call = call->next)
+ if (!call->is_pasted && !call->broken_cycle)
+ {
+ if (!collect_overlays (call->fun, info, ovly_sections))
+ return FALSE;
+ break;
+ }
+
+ added_fun = FALSE;
+ if (fun->sec->linker_mark && fun->sec->gc_mark)
+ {
+ fun->sec->gc_mark = 0;
+ *(*ovly_sections)++ = fun->sec;
+ if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark)
+ {
+ fun->rodata->gc_mark = 0;
+ *(*ovly_sections)++ = fun->rodata;
+ }
+ else
+ *(*ovly_sections)++ = NULL;
+ added_fun = TRUE;
+
+ /* Pasted sections must stay with the first section. We don't
+ put pasted sections in the array, just the first section.
+ Mark subsequent sections as already considered. */
+ if (fun->sec->segment_mark)
+ {
+ struct function_info *call_fun = fun;
+ do
+ {
+ for (call = call_fun->call_list; call != NULL; call = call->next)
+ if (call->is_pasted)
+ {
+ call_fun = call->fun;
+ call_fun->sec->gc_mark = 0;
+ if (call_fun->rodata)
+ call_fun->rodata->gc_mark = 0;
+ break;
+ }
+ if (call == NULL)
+ abort ();
+ }
+ while (call_fun->sec->segment_mark);
+ }
+ }
+
+ for (call = fun->call_list; call != NULL; call = call->next)
+ if (!call->broken_cycle
+ && !collect_overlays (call->fun, info, ovly_sections))
+ return FALSE;
+
+ if (added_fun)
+ {
+ struct _spu_elf_section_data *sec_data;
+ struct spu_elf_stack_info *sinfo;
+
+ if ((sec_data = spu_elf_section_data (fun->sec)) != NULL
+ && (sinfo = sec_data->u.i.stack_info) != NULL)
+ {
+ int i;
+ for (i = 0; i < sinfo->num_fun; ++i)
+ if (!collect_overlays (&sinfo->fun[i], info, ovly_sections))
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+struct _sum_stack_param {
+ size_t cum_stack;
+ size_t overall_stack;
+ bfd_boolean emit_stack_syms;
+};
+
+/* Descend the call graph for FUN, accumulating total stack required. */
+
+static bfd_boolean
+sum_stack (struct function_info *fun,
+ struct bfd_link_info *info,
+ void *param)
+{
+ struct call_info *call;
+ struct function_info *max;
+ size_t stack, cum_stack;
+ const char *f1;
+ bfd_boolean 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;
+
+ 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;
+ if (!sum_stack (call->fun, info, sum_stack_param))
+ 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
+ this function. */
+ if (!call->is_tail || call->is_pasted || call->fun->start != NULL)
+ stack += fun->stack;
+ if (cum_stack < stack)
+ {
+ cum_stack = stack;
+ max = call->fun;
+ }
+ }
+
+ sum_stack_param->cum_stack = cum_stack;
+ stack = fun->stack;
+ /* Now fun->stack holds cumulative stack. */
+ fun->stack = cum_stack;
+ fun->visit3 = TRUE;
+
+ if (!fun->non_root
+ && sum_stack_param->overall_stack < cum_stack)
+ sum_stack_param->overall_stack = cum_stack;
+
+ htab = spu_hash_table (info);
+ if (htab->params->auto_overlay)
+ 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"),
+ 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 && !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);
+ }
+ }
+ }
+
+ if (sum_stack_param->emit_stack_syms)
+ {
+ char *name = bfd_malloc (18 + strlen (f1));
+ struct elf_link_hash_entry *h;
+
+ if (name == NULL)
+ 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);
+ free (name);
+ if (h != NULL
+ && (h->root.type == bfd_link_hash_new
+ || h->root.type == bfd_link_hash_undefined
+ || h->root.type == bfd_link_hash_undefweak))
+ {
+ h->root.type = bfd_link_hash_defined;
+ h->root.u.def.section = bfd_abs_section_ptr;
+ h->root.u.def.value = cum_stack;
+ h->size = 0;
+ h->type = 0;
+ h->ref_regular = 1;
+ h->def_regular = 1;
+ h->ref_regular_nonweak = 1;
+ h->forced_local = 1;
+ h->non_elf = 0;
+ }
+ }
+
+ return TRUE;
+}
+
+/* SEC is part of a pasted function. Return the call_info for the
+ next section of this function. */
+
+static struct call_info *
+find_pasted_call (asection *sec)
+{
+ struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
+ struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
+ struct call_info *call;
+ int k;
+
+ for (k = 0; k < sinfo->num_fun; ++k)
+ for (call = sinfo->fun[k].call_list; call != NULL; call = call->next)
+ if (call->is_pasted)
+ return call;
+ abort ();
+ return 0;
+}
+
+/* qsort predicate to sort bfds by file name. */
+
+static int
+sort_bfds (const void *a, const void *b)
+{
+ bfd *const *abfd1 = a;
+ bfd *const *abfd2 = b;
+
+ return filename_cmp ((*abfd1)->filename, (*abfd2)->filename);
+}
+
+static unsigned int
+print_one_overlay_section (FILE *script,
+ unsigned int base,
+ unsigned int count,
+ unsigned int ovlynum,
+ unsigned int *ovly_map,
+ asection **ovly_sections,
+ 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 : ""),
+ info->path_separator,
+ sec->owner->filename,
+ sec->name) <= 0)
+ return -1;
+ if (sec->segment_mark)
+ {
+ struct call_info *call = find_pasted_call (sec);
+ while (call != NULL)
+ {
+ struct function_info *call_fun = call->fun;
+ sec = call_fun->sec;
+ if (fprintf (script, " %s%c%s (%s)\n",
+ (sec->owner->my_archive != NULL
+ ? sec->owner->my_archive->filename : ""),
+ info->path_separator,
+ sec->owner->filename,
+ sec->name) <= 0)
+ return -1;
+ for (call = call_fun->call_list; call; call = call->next)
+ if (call->is_pasted)
+ break;
+ }
+ }
+ }
+
+ for (j = base; j < count && ovly_map[j] == ovlynum; j++)
+ {
+ asection *sec = ovly_sections[2 * j + 1];
+ if (sec != NULL
+ && fprintf (script, " %s%c%s (%s)\n",
+ (sec->owner->my_archive != NULL
+ ? sec->owner->my_archive->filename : ""),
+ info->path_separator,
+ sec->owner->filename,
+ sec->name) <= 0)
+ return -1;
+
+ sec = ovly_sections[2 * j];
+ if (sec->segment_mark)
+ {
+ struct call_info *call = find_pasted_call (sec);
+ while (call != NULL)
+ {
+ struct function_info *call_fun = call->fun;
+ sec = call_fun->rodata;
+ if (sec != NULL
+ && fprintf (script, " %s%c%s (%s)\n",
+ (sec->owner->my_archive != NULL
+ ? sec->owner->my_archive->filename : ""),
+ info->path_separator,
+ sec->owner->filename,
+ sec->name) <= 0)
+ return -1;
+ for (call = call_fun->call_list; call; call = call->next)
+ if (call->is_pasted)
+ break;
+ }
+ }
+ }
+
+ return j;
+}
+
+/* Handle --auto-overlay. */
+
+static void
+spu_elf_auto_overlay (struct bfd_link_info *info)
+{
+ bfd *ibfd;
+ 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;
+ asection **ovly_sections, **ovly_p;
+ unsigned int *ovly_map;
+ FILE *script;
+ unsigned int total_overlay_size, overlay_size;
+ const char *ovly_mgr_entry;
+ struct elf_link_hash_entry *h;
+ struct _mos_param mos_param;
+ struct _uos_param uos_param;
+ struct function_info dummy_caller;
+
+ /* 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)
+ if (m->p_type == PT_LOAD)
+ for (i = 0; i < m->count; i++)
+ if (m->sections[i]->size != 0)
+ {
+ if (m->sections[i]->vma < lo)
+ lo = m->sections[i]->vma;
+ if (m->sections[i]->vma + m->sections[i]->size - 1 > hi)
+ hi = m->sections[i]->vma + m->sections[i]->size - 1;
+ }
+ fixed_size = hi + 1 - lo;
+
+ if (!discover_functions (info))
+ goto err_exit;
+
+ 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");
+
+ 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);
+ if (h != NULL
+ && (h->root.type == bfd_link_hash_defined
+ || h->root.type == bfd_link_hash_defweak)
+ && h->def_regular)
+ {
+ /* We have a user supplied overlay manager. */
+ uos_param.exclude_input_section = h->root.u.def.section;
+ }
+ else
+ {
+ /* If no user overlay manager, spu_elf_load_ovl_mgr will add our
+ builtin version to .text, and will adjust .text size. */
+ fixed_size += (*htab->params->spu_elf_load_ovl_mgr) ();
+ }
+
+ /* 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))
+ goto err_exit;
+
+ /* We can't put the overlay manager or interrupt routines in
+ overlays. */
+ 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))
+ goto err_exit;
+
+ bfd_count = 0;
+ 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)
+ goto err_exit;
+
+ /* Count overlay sections, and subtract their sizes from "fixed_size". */
+ count = 0;
+ bfd_count = 0;
+ total_overlay_size = 0;
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+ {
+ extern const bfd_target bfd_elf32_spu_vec;
+ asection *sec;
+ unsigned int old_count;
+
+ if (ibfd->xvec != &bfd_elf32_spu_vec)
+ continue;
+
+ old_count = count;
+ for (sec = ibfd->sections; sec != NULL; sec = sec->next)
+ if (sec->linker_mark)
+ {
+ if ((sec->flags & SEC_CODE) != 0)
+ count += 1;
+ fixed_size -= sec->size;
+ total_overlay_size += sec->size;
+ }
+ 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)
+ fixed_size -= sec->size;
+ if (count != old_count)
+ bfd_arr[bfd_count++] = ibfd;
+ }
+
+ /* Since the overlay link script selects sections by file name and
+ section name, ensure that file names are unique. */
+ if (bfd_count > 1)
+ {
+ bfd_boolean ok = TRUE;
+
+ qsort (bfd_arr, bfd_count, sizeof (*bfd_arr), sort_bfds);
+ for (i = 1; i < bfd_count; ++i)
+ if (filename_cmp (bfd_arr[i - 1]->filename, bfd_arr[i]->filename) == 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)
+ info->callbacks->einfo (_("%s duplicated in %s\n"),
+ bfd_arr[i]->filename,
+ bfd_arr[i]->my_archive->filename);
+ else
+ info->callbacks->einfo (_("%s duplicated\n"),
+ bfd_arr[i]->filename);
+ ok = FALSE;
+ }
+ }
+ if (!ok)
+ {
+ info->callbacks->einfo (_("sorry, no support for duplicate "
+ "object files in auto-overlay script\n"));
+ bfd_set_error (bfd_error_bad_value);
+ goto err_exit;
+ }
+ }
+ free (bfd_arr);
+
+ 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)
+ {
+ /* Stubs in the non-icache area are bigger. */
+ 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. */
+ fixed_size += 16 << htab->num_lines_log2;
+ /* b) Rewrite "to" list, one quadword per cache line. */
+ fixed_size += 16 << htab->num_lines_log2;
+ /* 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
+ {
+ /* Guess number of overlays. Assuming overlay buffer is on
+ average only half full should be conservative. */
+ ovlynum = (total_overlay_size * 2 * htab->params->num_lines
+ / (htab->local_store - fixed_size));
+ /* Space for _ovly_table[], _ovly_buf_table[] and toe. */
+ fixed_size += ovlynum * 16 + 16 + 4 + 16;
+ }
+ }
+
+ if (fixed_size + mos_param.max_overlay_size > htab->local_store)
+ 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->params->auto_overlay_fixed)
+ {
+ unsigned int max_fixed, lib_size;
+
+ max_fixed = htab->local_store - mos_param.max_overlay_size;
+ 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)
+ goto err_exit;
+ fixed_size = max_fixed - lib_size;
+ }
+
+ /* Build an array of sections, suitably sorted to place into
+ overlays. */
+ ovly_sections = bfd_malloc (2 * count * sizeof (*ovly_sections));
+ if (ovly_sections == NULL)
+ goto err_exit;
+ ovly_p = ovly_sections;
+ 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));
+ if (ovly_map == NULL)
+ goto err_exit;
+
+ memset (&dummy_caller, 0, sizeof (dummy_caller));
+ overlay_size = (htab->local_store - fixed_size) / htab->params->num_lines;
+ if (htab->params->line_size != 0)
+ overlay_size = htab->params->line_size;
+ base = 0;
+ ovlynum = 0;
+ while (base < count)
+ {
+ unsigned int size = 0, rosize = 0, roalign = 0;
+
+ for (i = base; i < count; i++)
+ {
+ asection *sec, *rosec;
+ unsigned int tmp, rotmp;
+ unsigned int num_stubs;
+ struct call_info *call, *pasty;