# This shell script emits a C file. -*- C -*-
# Copyright (C) 2006-2025 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# This file is sourced from elf.em, and defines extra spu specific
# features.
#
fragment <<EOF
#include "ldctor.h"
#include "elf32-spu.h"
static unsigned int no_overlays = 0;
static unsigned int num_lines_set = 0;
static unsigned int line_size_set = 0;
static char *auto_overlay_file = 0;
int my_argc;
char **my_argv;
static const char ovl_mgr[] = {
EOF
if ! cat ${srcdir}/emultempl/spu_ovl.o_c >> e${EMULATION_NAME}.c
then
echo >&2 "Missing ${srcdir}/emultempl/spu_ovl.o_c"
echo >&2 "You must build gas/as-new with --target=spu"
exit 1
fi
fragment <<EOF
};
static const char icache_mgr[] = {
EOF
if ! cat ${srcdir}/emultempl/spu_icache.o_c >> e${EMULATION_NAME}.c
then
echo >&2 "Missing ${srcdir}/emultempl/spu_icache.o_c"
echo >&2 "You must build gas/as-new with --target=spu"
exit 1
fi
static void
spu_after_open (void)
{
if (is_spu_target ())
{
/* Pass params to backend. */
if ((params.auto_overlay & AUTO_OVERLAY) == 0)
params.auto_overlay = 0;
params.emit_stub_syms |= link_info.emitrelocations;
spu_elf_setup (&link_info, ¶ms);
if (bfd_link_relocatable (&link_info))
lang_add_unique (".text.ia.*");
if (!bfd_link_relocatable (&link_info)
&& link_info.input_bfds != NULL
&& !spu_elf_create_sections (&link_info))
einfo (_("%X%P: can not create note section: %E\n"));
}
gld${EMULATION_NAME}_after_open ();
}
/* If O is NULL, add section S at the end of output section OUTPUT_NAME.
If O is not NULL, add section S at the beginning of output section O,
except for soft-icache which adds to the end.
Really, we should be duplicating ldlang.c map_input_to_output_sections
logic here, ie. using the linker script to find where the section
goes. That's rather a lot of code, and we don't want to run
map_input_to_output_sections again because most sections are already
mapped. So cheat, and put the section in a fixed place, ignoring any
attempt via a linker script to put .stub, .ovtab, and built-in
overlay manager code somewhere else. */
if (o != NULL)
os = lang_output_section_get (o);
else
os = lang_output_section_find (output_name);
if (os == NULL)
{
os = ldelf_place_orphan (s, output_name, 0);
os->addr_tree = NULL;
}
else if (params.ovly_flavour != ovly_soft_icache
&& o != NULL && os->children.head != NULL)
{
lang_statement_list_type add;
lang_list_init (&add);
lang_add_section (&add, s, NULL, NULL, os);
*add.tail = os->children.head;
os->children.head = add.head;
}
else
{
if (params.ovly_flavour == ovly_soft_icache && o != NULL)
{
/* Pad this stub section so that it finishes at the
end of the icache line. */
etree_type *e_size;
/* Go find if we need to do anything special for overlays. */
static void
spu_before_allocation (void)
{
if (is_spu_target ()
&& !bfd_link_relocatable (&link_info)
&& !no_overlays)
{
int ret;
/* Size the sections. This is premature, but we need to know the
rough layout so that overlays can be found. */
expld.phase = lang_mark_phase_enum;
expld.dataseg.phase = exp_seg_none;
one_lang_size_sections_pass (NULL, true);
/* Find overlays by inspecting section vmas. */
ret = spu_elf_find_overlays (&link_info);
if (ret == 0)
einfo (_("%X%P: can not find overlays: %E\n"));
else if (ret == 2)
{
lang_output_section_statement_type *os;
if (params.auto_overlay != 0)
{
einfo (_("%P: --auto-overlay ignored with user overlay script\n"));
params.auto_overlay = 0;
}
/* Ensure alignment of overlay sections is sufficient. */
for (os = (void *) lang_os_list.head;
os != NULL;
os = os->next)
if (os->bfd_section != NULL
&& spu_elf_section_data (os->bfd_section) != NULL
&& spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0)
{
if (os->bfd_section->alignment_power < 4)
os->bfd_section->alignment_power = 4;
static void
gld${EMULATION_NAME}_finish (void)
{
if (is_spu_target ())
{
if (params.local_store_lo < params.local_store_hi)
{
asection *s;
s = spu_elf_check_vma (&link_info);
if (s != NULL && !params.auto_overlay)
einfo (_("%X%P: %pA exceeds local store range\n"), s);
}
else if (params.auto_overlay)
einfo (_("%P: --auto-overlay ignored with zero local store range\n"));
}
/* This function is called when building a ppc32 or ppc64 executable
to handle embedded spu images. */
extern bool embedded_spu_file (lang_input_statement_type *, const char *);
bool
embedded_spu_file (lang_input_statement_type *entry, const char *flags)
{
const char *cmd[6];
const char *pex_return;
const char *sym;
char *handle, *p;
char *oname;
int fd;
int status;
union lang_statement_union **old_stat_tail;
union lang_statement_union **old_file_tail;
union lang_statement_union *new_ent;
lang_input_statement_type *search;
/* lang_add_input_file puts the new list entry at the end of the statement
and input file lists. Move it to just after the current entry. */
new_ent = *old_stat_tail;
*old_stat_tail = NULL;
stat_ptr->tail = old_stat_tail;
*old_file_tail = NULL;
input_file_chain.tail = old_file_tail;
new_ent->header.next = entry->header.next;
entry->header.next = new_ent;
new_ent->input_statement.next_real_file = entry->next_real_file;
entry->next_real_file = &new_ent->input_statement;
/* Ensure bfd sections are excluded from the output. */
bfd_section_list_clear (entry->the_bfd);
entry->flags.loaded = true;
return true;
}
PARSE_AND_LIST_OPTIONS='
fprintf (file, _("\
--plugin Make SPU plugin\n"));
fprintf (file, _("\
--no-overlays No overlay handling\n"));
fprintf (file, _("\
--compact-stubs Use smaller and possibly slower call stubs\n"));
fprintf (file, _("\
--emit-stub-syms Add symbols on overlay call stubs\n"));
fprintf (file, _("\
--extra-overlay-stubs Add stubs on all calls out of overlay regions\n"));
fprintf (file, _("\
--local-store=lo:hi Valid address range\n"));
fprintf (file, _("\
--stack-analysis Estimate maximum stack requirement\n"));
fprintf (file, _("\
--emit-stack-syms Add sym giving stack needed for each func\n"));
fprintf (file, _("\
--auto-overlay [=filename] Create an overlay script in filename if\n\
executable does not fit in local store\n"));
fprintf (file, _("\
--auto-relink Rerun linker using auto-overlay script\n"));
fprintf (file, _("\
--overlay-rodata Place read-only data with associated function\n\
code in overlays\n"));
fprintf (file, _("\
--num-regions Number of overlay buffers (default 1)\n"));
fprintf (file, _("\
--region-size Size of overlay buffers (default 0, auto)\n"));
fprintf (file, _("\
--fixed-space=bytes Local store for non-overlay code and data\n"));
fprintf (file, _("\
--reserved-space=bytes Local store for stack and heap. If not specified\n\
ld will estimate stack size and assume no heap\n"));
fprintf (file, _("\
--extra-stack-space=bytes Space for negative sp access (default 2000) if\n\
--reserved-space not given\n"));
fprintf (file, _("\
--soft-icache Generate software icache overlays\n"));
fprintf (file, _("\
--num-lines Number of soft-icache lines (default 32)\n"));
fprintf (file, _("\
--line-size Size of soft-icache lines (default 1k)\n"));
fprintf (file, _("\
--non-ia-text Allow non-icache code in icache lines\n"));
fprintf (file, _("\
--lrlive-analysis Scan function prologue for lr liveness\n"));
'
PARSE_AND_LIST_ARGS_CASES='
case OPTION_SPU_PLUGIN:
spu_elf_plugin (1);
break;
case OPTION_SPU_NO_OVERLAYS:
no_overlays = 1;
break;
case OPTION_SPU_COMPACT_STUBS:
params.compact_stub = 1;
break;
case OPTION_SPU_STUB_SYMS:
params.emit_stub_syms = 1;
break;
case OPTION_SPU_NON_OVERLAY_STUBS:
params.non_overlay_stubs = 1;
break;
case OPTION_SPU_LOCAL_STORE:
{
char *end;
params.local_store_lo = strtoul (optarg, &end, 0);
if (*end == '\'':'\'')
{
params.local_store_hi = strtoul (end + 1, &end, 0);
if (*end == 0)
break;
}
fatal (_("%P: invalid --local-store address range `%s'\''\n"), optarg);
}
break;
case OPTION_SPU_STACK_ANALYSIS:
params.stack_analysis = 1;
break;
case OPTION_SPU_STACK_SYMS:
params.emit_stack_syms = 1;
break;
case OPTION_SPU_AUTO_OVERLAY:
params.auto_overlay |= 1;
if (optarg != NULL)
{
auto_overlay_file = optarg;
break;
}
/* Fallthru */
case OPTION_SPU_AUTO_RELINK:
params.auto_overlay |= 2;
break;
case OPTION_SPU_OVERLAY_RODATA:
params.auto_overlay |= 4;
break;
case OPTION_SPU_SOFT_ICACHE:
params.ovly_flavour = ovly_soft_icache;
/* Software i-cache stubs are always "compact". */
params.compact_stub = 1;
if (!num_lines_set)
params.num_lines = 32;
else if ((params.num_lines & -params.num_lines) != params.num_lines)
fatal (_("%P: invalid --num-lines/--num-regions `%u'\''\n"),
params.num_lines);
if (!line_size_set)
params.line_size = 1024;
else if ((params.line_size & -params.line_size) != params.line_size)
fatal (_("%P: invalid --line-size/--region-size `%u'\''\n"),
params.line_size);
break;
case OPTION_SPU_LRLIVE:
params.lrlive_analysis = 1;
break;
case OPTION_SPU_NON_IA_TEXT:
params.non_ia_text = 1;
break;