linux.em revision 130562
11573Srgrimes# This shell script emits a C file. -*- C -*-
21573Srgrimes# It does some substitutions.
31573Srgrimesif [ -z "$MACHINE" ]; then
41573Srgrimes  OUTPUT_ARCH=${ARCH}
51573Srgrimeselse
61573Srgrimes  OUTPUT_ARCH=${ARCH}:${MACHINE}
71573Srgrimesfi
81573Srgrimescat >e${EMULATION_NAME}.c <<EOF
91573Srgrimes/* This file is is generated by a shell script.  DO NOT EDIT! */
101573Srgrimes
111573Srgrimes/* Linux a.out emulation code for ${EMULATION_NAME}
121573Srgrimes   Copyright 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003, 2004
131573Srgrimes   Free Software Foundation, Inc.
141573Srgrimes   Written by Steve Chamberlain <sac@cygnus.com>
151573Srgrimes   Linux support by Eric Youngdale <ericy@cais.cais.com>
16148834Sstefanf
171573SrgrimesThis file is part of GLD, the Gnu Linker.
181573Srgrimes
191573SrgrimesThis program is free software; you can redistribute it and/or modify
201573Srgrimesit under the terms of the GNU General Public License as published by
211573Srgrimesthe Free Software Foundation; either version 2 of the License, or
221573Srgrimes(at your option) any later version.
231573Srgrimes
241573SrgrimesThis program is distributed in the hope that it will be useful,
251573Srgrimesbut WITHOUT ANY WARRANTY; without even the implied warranty of
261573SrgrimesMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
271573SrgrimesGNU General Public License for more details.
281573Srgrimes
291573SrgrimesYou should have received a copy of the GNU General Public License
301573Srgrimesalong with this program; if not, write to the Free Software
3184260SobrienFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
32237448Spfg
331573Srgrimes#define TARGET_IS_${EMULATION_NAME}
341573Srgrimes
351573Srgrimes#include "bfd.h"
361573Srgrimes#include "sysdep.h"
371573Srgrimes#include "bfdlink.h"
3884260Sobrien
3984260Sobrien#include "ld.h"
401573Srgrimes#include "ldmain.h"
411573Srgrimes#include "ldmisc.h"
421573Srgrimes#include "ldexp.h"
431573Srgrimes#include "ldlang.h"
441573Srgrimes#include "ldfile.h"
451573Srgrimes#include "ldemul.h"
4626926Smsmith
473285Sachestatic void
4826926Smsmithgld${EMULATION_NAME}_before_parse (void)
491573Srgrimes{
501573Srgrimes  ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
511573Srgrimes  config.dynamic_link = TRUE;
521573Srgrimes  config.has_shared = TRUE;
531573Srgrimes}
541573Srgrimes
551573Srgrimes/* Try to open a dynamic archive.  This is where we know that Linux
5684260Sobrien   dynamic libraries have an extension of .sa.  */
571573Srgrimes
581573Srgrimesstatic bfd_boolean
591573Srgrimesgld${EMULATION_NAME}_open_dynamic_archive
601573Srgrimes  (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
611573Srgrimes{
621573Srgrimes  char *string;
631573Srgrimes
6484260Sobrien  if (! entry->is_archive)
651573Srgrimes    return FALSE;
6684260Sobrien
6784260Sobrien  string = (char *) xmalloc (strlen (search->name)
6884260Sobrien			     + strlen (entry->filename)
6984260Sobrien			     + strlen (arch)
7084260Sobrien			     + sizeof "/lib.sa");
7184260Sobrien
7284260Sobrien  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
73148834Sstefanf
74148834Sstefanf  if (! ldfile_try_open_bfd (string, entry))
7584260Sobrien    {
761573Srgrimes      free (string);
771573Srgrimes      return FALSE;
781573Srgrimes    }
791573Srgrimes
801573Srgrimes  entry->filename = string;
811573Srgrimes
821573Srgrimes  return TRUE;
8384260Sobrien}
841573Srgrimes
8584260Sobrien/* This is called by the create_output_section_statements routine via
8684260Sobrien   lang_for_each_statement.  It locates any address assignment to
8784260Sobrien   .text, and modifies it to include the size of the headers.  This
881573Srgrimes   causes -Ttext to mean the starting address of the header, rather
891573Srgrimes   than the starting address of .text, which is compatible with other
9084260Sobrien   Linux tools.  */
911573Srgrimes
921573Srgrimesstatic void
931573Srgrimesgld${EMULATION_NAME}_find_address_statement (lang_statement_union_type *s)
941573Srgrimes{
958870Srgrimes  if (s->header.type == lang_address_statement_enum
961573Srgrimes      && strcmp (s->address_statement.section_name, ".text") == 0)
9784260Sobrien    {
981573Srgrimes      ASSERT (s->address_statement.address->type.node_class == etree_value);
991573Srgrimes      s->address_statement.address->value.value += 0x20;
1001573Srgrimes    }
1011573Srgrimes}
10284260Sobrien
1031573Srgrimes/* This is called before opening the input BFD's.  */
1041573Srgrimes
1051573Srgrimesstatic void
1061573Srgrimesgld${EMULATION_NAME}_create_output_section_statements (void)
10784260Sobrien{
1081573Srgrimes  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
10926926Smsmith}
11084260Sobrien
11184260Sobrien/* This is called after the sections have been attached to output
11226926Smsmith   sections, but before any sizes or addresses have been set.  */
11384260Sobrien
11484260Sobrienstatic void
11584260Sobriengld${EMULATION_NAME}_before_allocation (void)
11684260Sobrien{
11784260Sobrien  if (link_info.relocatable)
1181573Srgrimes    return;
1191573Srgrimes
12084260Sobrien  /* Let the backend work out the sizes of any sections required by
12184260Sobrien     dynamic linking.  */
12226926Smsmith  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
12326926Smsmith    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
12484260Sobrien}
12584260Sobrien
12684260Sobrienstatic char *
12784260Sobriengld${EMULATION_NAME}_get_script (int *isfile)
12884260SobrienEOF
12984260Sobrien
13084260Sobrienif test -n "$COMPILE_IN"
13126926Smsmiththen
13284260Sobrien# Scripts compiled in.
13384260Sobrien
13484260Sobrien# sed commands to quote an ld script as a C string.
13584260Sobriensc="-f stringify.sed"
13684260Sobrien
13784260Sobriencat >>e${EMULATION_NAME}.c <<EOF
13884260Sobrien{
1393285Sache  *isfile = 0;
14084260Sobrien
14184260Sobrien  if (link_info.relocatable && config.build_constructors)
14284260Sobrien    return
14384260SobrienEOF
1441573Srgrimessed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
1451573Srgrimesecho '  ; else if (link_info.relocatable) return'     >> e${EMULATION_NAME}.c
1461573Srgrimessed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
1471573Srgrimesecho '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
1481573Srgrimessed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
1491573Srgrimesecho '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1501573Srgrimessed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
1511573Srgrimesecho '  ; else return'                                 >> e${EMULATION_NAME}.c
15284260Sobriensed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
1531573Srgrimesecho '; }'                                             >> e${EMULATION_NAME}.c
1541573Srgrimes
15584260Sobrienelse
15684260Sobrien# Scripts read from the filesystem.
1571573Srgrimes
1588870Srgrimescat >>e${EMULATION_NAME}.c <<EOF
15984260Sobrien{
1601573Srgrimes  *isfile = 1;
1611573Srgrimes
1621573Srgrimes  if (link_info.relocatable && config.build_constructors)
1638870Srgrimes    return "ldscripts/${EMULATION_NAME}.xu";
1641573Srgrimes  else if (link_info.relocatable)
1651573Srgrimes    return "ldscripts/${EMULATION_NAME}.xr";
1661573Srgrimes  else if (!config.text_read_only)
16784260Sobrien    return "ldscripts/${EMULATION_NAME}.xbn";
1681573Srgrimes  else if (!config.magic_demand_paged)
16984260Sobrien    return "ldscripts/${EMULATION_NAME}.xn";
17084260Sobrien  else
17184260Sobrien    return "ldscripts/${EMULATION_NAME}.x";
17284260Sobrien}
17384260SobrienEOF
17484260Sobrien
17584260Sobrienfi
17684260Sobrien
17784260Sobriencat >>e${EMULATION_NAME}.c <<EOF
17884260Sobrien
17984260Sobrienstruct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1801573Srgrimes{
1811573Srgrimes  gld${EMULATION_NAME}_before_parse,
18284260Sobrien  syslib_default,
18384260Sobrien  hll_default,
18484260Sobrien  after_parse_default,
18584260Sobrien  after_open_default,
18684260Sobrien  after_allocation_default,
18784260Sobrien  set_output_arch_default,
18884260Sobrien  ldemul_default_target,
18984260Sobrien  gld${EMULATION_NAME}_before_allocation,
1901573Srgrimes  gld${EMULATION_NAME}_get_script,
1911573Srgrimes  "${EMULATION_NAME}",
1921573Srgrimes  "${OUTPUT_FORMAT}",
1931573Srgrimes  NULL,	/* finish */
1941573Srgrimes  gld${EMULATION_NAME}_create_output_section_statements,
1951573Srgrimes  gld${EMULATION_NAME}_open_dynamic_archive,
1961573Srgrimes  NULL,	/* place orphan */
1971573Srgrimes  NULL,	/* set symbols */
19884260Sobrien  NULL,	/* parse args */
1991573Srgrimes  NULL,	/* add_options */
20084260Sobrien  NULL,	/* handle_option */
20184260Sobrien  NULL,	/* unrecognized file */
20284260Sobrien  NULL,	/* list options */
20384260Sobrien  NULL,	/* recognized file */
20484260Sobrien  NULL,	/* find_potential_libraries */
20584260Sobrien  NULL	/* new_vers_pattern */
2061573Srgrimes};
20784260SobrienEOF
2081573Srgrimes