# This shell script emits a C file. -*- C -*-
# Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
#
# This file is part of GLD, the Gnu Linker.
#
# 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# This file is sourced from elf32.em and mmo.em, used to define
# linker MMIX-specifics common to ELF and MMO.
cat >>e${EMULATION_NAME}.c <<EOF
#define gldmmo_before_allocation before_allocation_default
EOF
. ${srcdir}/emultempl/mmix-elfnmmo.em
cat >>e${EMULATION_NAME}.c <<EOF
static asection *
output_prev_sec_find (lang_output_section_statement_type *os)
{
asection *s = NULL;
lang_statement_union_type *u;
lang_output_section_statement_type *lookup;
for (u = lang_output_section_statement.head;
u != (lang_statement_union_type *) NULL;
u = lookup->next)
{
lookup = &u->output_section_statement;
if (lookup->constraint == -1)
continue;
if (lookup == os)
break;
if (lookup->bfd_section != NULL
&& lookup->bfd_section != bfd_abs_section_ptr
&& lookup->bfd_section != bfd_com_section_ptr
&& lookup->bfd_section != bfd_und_section_ptr)
s = lookup->bfd_section;
}
if (u == NULL)
return NULL;
return s;
}
struct orphan_save {
lang_output_section_statement_type *os;
asection **section;
lang_statement_union_type **stmt;
};
#define HAVE_SECTION(hold, name) \
(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
static bfd_boolean
mmo_place_orphan (lang_input_statement_type *file, asection *s)
{
static struct orphan_save hold_text;
struct orphan_save *place;
lang_output_section_statement_type *os;
lang_statement_list_type *old;
lang_statement_list_type add;
asection *snew, **pps, *bfd_section;
if (link_info.relocatable
|| (bfd_get_section_flags (s->owner, s)
& (SEC_EXCLUDE | SEC_LOAD)) != SEC_LOAD)
return FALSE;
os = lang_output_section_find (bfd_get_section_name (s->owner, s));
if (os != NULL)
{
lang_add_section (&os->children, s, os, file);
return TRUE;
}
if ((bfd_get_section_flags (s->owner, s) & (SEC_CODE | SEC_READONLY)) == 0)
return FALSE;
if (hold_text.os == NULL)
hold_text.os = lang_output_section_find (MMO_TEXT_SECTION_NAME);
place = &hold_text;
old = stat_ptr;
stat_ptr = &add;
lang_list_init (stat_ptr);
os = lang_enter_output_section_statement (bfd_get_section_name (s->owner,
s),
NULL, 0,
(etree_type *) NULL,
(etree_type *) NULL,
(etree_type *) NULL, 0);
lang_add_section (&os->children, s, os, file);
lang_leave_output_section_statement
((bfd_vma) 0, "*default*",
(struct lang_output_section_phdr_list *) NULL, NULL);
stat_ptr = old;
snew = os->bfd_section;
if (snew == NULL)
return TRUE;
if (hold_text.os == NULL)
{
if (os == NULL)
return FALSE;
hold_text.os = os;
}
bfd_section = place->os->bfd_section;
if (place->section == NULL && bfd_section == NULL)
bfd_section = output_prev_sec_find (place->os);
if (place->section != NULL
|| (bfd_section != NULL
&& bfd_section != snew))
{
if (place->section == NULL)
place->section = &bfd_section->next;
for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
;
bfd_section_list_remove (output_bfd, pps);
bfd_section_list_insert (output_bfd, place->section, snew);
}
place->section = &snew->next;
if (add.head != NULL)
{
if (place->stmt == NULL)
{
*add.tail = place->os->header.next;
place->os->header.next = add.head;
}
else
{
*add.tail = *place->stmt;
*place->stmt = add.head;
}
if (*old->tail == add.head)
old->tail = add.tail;
place->stmt = add.tail;
}
return TRUE;
}
static void
mmo_wipe_sec_reloc_flag (bfd *abfd, asection *sec, void *ptr ATTRIBUTE_UNUSED)
{
bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec) & ~SEC_RELOC);
}
static void
mmo_finish (void)
{
bfd_map_over_sections (output_bfd, mmo_wipe_sec_reloc_flag, NULL);
}
static void
mmo_after_open (void)
{
if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour)
{
LANG_FOR_EACH_INPUT_STATEMENT (is)
{
if (bfd_get_flavour (is->the_bfd) == bfd_target_elf_flavour
&& !_bfd_mmix_check_all_relocs (is->the_bfd, &link_info))
einfo ("%X%P: Internal problems scanning %B after opening it",
is->the_bfd);
}
}
}
EOF
LDEMUL_PLACE_ORPHAN=mmo_place_orphan
LDEMUL_FINISH=mmo_finish
LDEMUL_AFTER_OPEN=mmo_after_open