linux.em revision 89857
133965Sjdp# This shell script emits a C file. -*- C -*-
233965Sjdp# It does some substitutions.
389857Sobrienif [ -z "$MACHINE" ]; then 
489857Sobrien  OUTPUT_ARCH=${ARCH}
589857Sobrienelse
689857Sobrien  OUTPUT_ARCH=${ARCH}:${MACHINE}
789857Sobrienfi
833965Sjdpcat >e${EMULATION_NAME}.c <<EOF
933965Sjdp/* This file is is generated by a shell script.  DO NOT EDIT! */
1033965Sjdp
1133965Sjdp/* Linux a.out emulation code for ${EMULATION_NAME}
1278828Sobrien   Copyright 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000
1377298Sobrien   Free Software Foundation, Inc.
1433965Sjdp   Written by Steve Chamberlain <sac@cygnus.com>
1533965Sjdp   Linux support by Eric Youngdale <ericy@cais.cais.com>
1633965Sjdp
1733965SjdpThis file is part of GLD, the Gnu Linker.
1833965Sjdp
1933965SjdpThis program is free software; you can redistribute it and/or modify
2033965Sjdpit under the terms of the GNU General Public License as published by
2133965Sjdpthe Free Software Foundation; either version 2 of the License, or
2233965Sjdp(at your option) any later version.
2333965Sjdp
2433965SjdpThis program is distributed in the hope that it will be useful,
2533965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
2633965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2733965SjdpGNU General Public License for more details.
2833965Sjdp
2933965SjdpYou should have received a copy of the GNU General Public License
3033965Sjdpalong with this program; if not, write to the Free Software
3133965SjdpFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
3233965Sjdp
3333965Sjdp#define TARGET_IS_${EMULATION_NAME}
3433965Sjdp
3533965Sjdp#include "bfd.h"
3633965Sjdp#include "sysdep.h"
3733965Sjdp#include "bfdlink.h"
3833965Sjdp
3933965Sjdp#include "ld.h"
4033965Sjdp#include "ldmain.h"
4133965Sjdp#include "ldmisc.h"
4233965Sjdp#include "ldexp.h"
4333965Sjdp#include "ldlang.h"
4477298Sobrien#include "ldfile.h"
4577298Sobrien#include "ldemul.h"
4633965Sjdp
4733965Sjdpstatic void gld${EMULATION_NAME}_before_parse PARAMS ((void));
4833965Sjdpstatic boolean gld${EMULATION_NAME}_open_dynamic_archive
4933965Sjdp  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
5033965Sjdpstatic void gld${EMULATION_NAME}_find_address_statement
5133965Sjdp  PARAMS ((lang_statement_union_type *));
5233965Sjdpstatic void gld${EMULATION_NAME}_create_output_section_statements
5333965Sjdp  PARAMS ((void));
5433965Sjdpstatic void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
5533965Sjdpstatic char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
5633965Sjdp
5733965Sjdpstatic void
5833965Sjdpgld${EMULATION_NAME}_before_parse()
5933965Sjdp{
6089857Sobrien  const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
6189857Sobrien  if (arch)
6289857Sobrien    {
6389857Sobrien      ldfile_output_architecture = arch->arch;
6489857Sobrien      ldfile_output_machine = arch->mach;
6589857Sobrien      ldfile_output_machine_name = arch->printable_name;
6689857Sobrien    }
6789857Sobrien  else
6889857Sobrien    ldfile_output_architecture = bfd_arch_${ARCH};
6933965Sjdp  config.dynamic_link = true;
7060484Sobrien  config.has_shared = true;
7133965Sjdp}
7233965Sjdp
7333965Sjdp/* Try to open a dynamic archive.  This is where we know that Linux
7433965Sjdp   dynamic libraries have an extension of .sa.  */
7533965Sjdp
7633965Sjdpstatic boolean
7733965Sjdpgld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
7833965Sjdp     const char *arch;
7933965Sjdp     search_dirs_type *search;
8033965Sjdp     lang_input_statement_type *entry;
8133965Sjdp{
8233965Sjdp  char *string;
8333965Sjdp
8433965Sjdp  if (! entry->is_archive)
8533965Sjdp    return false;
8633965Sjdp
8733965Sjdp  string = (char *) xmalloc (strlen (search->name)
8833965Sjdp			     + strlen (entry->filename)
8933965Sjdp			     + strlen (arch)
9033965Sjdp			     + sizeof "/lib.sa");
9133965Sjdp
9233965Sjdp  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
9333965Sjdp
9433965Sjdp  if (! ldfile_try_open_bfd (string, entry))
9533965Sjdp    {
9633965Sjdp      free (string);
9733965Sjdp      return false;
9833965Sjdp    }
9933965Sjdp
10033965Sjdp  entry->filename = string;
10133965Sjdp
10233965Sjdp  return true;
10333965Sjdp}
10433965Sjdp
10533965Sjdp/* This is called by the create_output_section_statements routine via
10633965Sjdp   lang_for_each_statement.  It locates any address assignment to
10733965Sjdp   .text, and modifies it to include the size of the headers.  This
10833965Sjdp   causes -Ttext to mean the starting address of the header, rather
10933965Sjdp   than the starting address of .text, which is compatible with other
11033965Sjdp   Linux tools.  */
11133965Sjdp
11233965Sjdpstatic void
11333965Sjdpgld${EMULATION_NAME}_find_address_statement (s)
11433965Sjdp     lang_statement_union_type *s;
11533965Sjdp{
11633965Sjdp  if (s->header.type == lang_address_statement_enum
11733965Sjdp      && strcmp (s->address_statement.section_name, ".text") == 0)
11833965Sjdp    {
11933965Sjdp      ASSERT (s->address_statement.address->type.node_class == etree_value);
12033965Sjdp      s->address_statement.address->value.value += 0x20;
12133965Sjdp    }
12233965Sjdp}
12333965Sjdp
12433965Sjdp/* This is called before opening the input BFD's.  */
12533965Sjdp
12633965Sjdpstatic void
12733965Sjdpgld${EMULATION_NAME}_create_output_section_statements ()
12833965Sjdp{
12933965Sjdp  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
13033965Sjdp}
13133965Sjdp
13233965Sjdp/* This is called after the sections have been attached to output
13333965Sjdp   sections, but before any sizes or addresses have been set.  */
13433965Sjdp
13533965Sjdpstatic void
13633965Sjdpgld${EMULATION_NAME}_before_allocation ()
13733965Sjdp{
13833965Sjdp  if (link_info.relocateable)
13933965Sjdp    return;
14033965Sjdp
14133965Sjdp  /* Let the backend work out the sizes of any sections required by
14233965Sjdp     dynamic linking.  */
14333965Sjdp  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
14433965Sjdp    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
14533965Sjdp}
14633965Sjdp
14733965Sjdpstatic char *
14833965Sjdpgld${EMULATION_NAME}_get_script(isfile)
14933965Sjdp     int *isfile;
15033965SjdpEOF
15133965Sjdp
15233965Sjdpif test -n "$COMPILE_IN"
15333965Sjdpthen
15433965Sjdp# Scripts compiled in.
15533965Sjdp
15633965Sjdp# sed commands to quote an ld script as a C string.
15760484Sobriensc="-f stringify.sed"
15833965Sjdp
15933965Sjdpcat >>e${EMULATION_NAME}.c <<EOF
16033965Sjdp{			     
16133965Sjdp  *isfile = 0;
16233965Sjdp
16333965Sjdp  if (link_info.relocateable == true && config.build_constructors == true)
16460484Sobrien    return
16533965SjdpEOF
16660484Sobriensed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
16760484Sobrienecho '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
16860484Sobriensed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
16960484Sobrienecho '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
17060484Sobriensed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
17160484Sobrienecho '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
17260484Sobriensed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
17360484Sobrienecho '  ; else return'                                     >> e${EMULATION_NAME}.c
17460484Sobriensed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
17560484Sobrienecho '; }'                                                 >> e${EMULATION_NAME}.c
17633965Sjdp
17733965Sjdpelse
17833965Sjdp# Scripts read from the filesystem.
17933965Sjdp
18033965Sjdpcat >>e${EMULATION_NAME}.c <<EOF
18133965Sjdp{			     
18233965Sjdp  *isfile = 1;
18333965Sjdp
18433965Sjdp  if (link_info.relocateable == true && config.build_constructors == true)
18533965Sjdp    return "ldscripts/${EMULATION_NAME}.xu";
18633965Sjdp  else if (link_info.relocateable == true)
18733965Sjdp    return "ldscripts/${EMULATION_NAME}.xr";
18833965Sjdp  else if (!config.text_read_only)
18933965Sjdp    return "ldscripts/${EMULATION_NAME}.xbn";
19033965Sjdp  else if (!config.magic_demand_paged)
19133965Sjdp    return "ldscripts/${EMULATION_NAME}.xn";
19233965Sjdp  else
19333965Sjdp    return "ldscripts/${EMULATION_NAME}.x";
19433965Sjdp}
19533965SjdpEOF
19633965Sjdp
19733965Sjdpfi
19833965Sjdp
19933965Sjdpcat >>e${EMULATION_NAME}.c <<EOF
20033965Sjdp
20133965Sjdpstruct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
20233965Sjdp{
20333965Sjdp  gld${EMULATION_NAME}_before_parse,
20433965Sjdp  syslib_default,
20533965Sjdp  hll_default,
20633965Sjdp  after_parse_default,
20733965Sjdp  after_open_default,
20833965Sjdp  after_allocation_default,
20933965Sjdp  set_output_arch_default,
21033965Sjdp  ldemul_default_target,
21133965Sjdp  gld${EMULATION_NAME}_before_allocation,
21233965Sjdp  gld${EMULATION_NAME}_get_script,
21333965Sjdp  "${EMULATION_NAME}",
21433965Sjdp  "${OUTPUT_FORMAT}",
21560484Sobrien  NULL,	/* finish */
21633965Sjdp  gld${EMULATION_NAME}_create_output_section_statements,
21760484Sobrien  gld${EMULATION_NAME}_open_dynamic_archive,
21860484Sobrien  NULL,	/* place orphan */
21960484Sobrien  NULL,	/* set symbols */
22060484Sobrien  NULL,	/* parse args */
22160484Sobrien  NULL,	/* unrecognized file */
22260484Sobrien  NULL,	/* list options */
22360484Sobrien  NULL,	/* recognized file */
22460484Sobrien  NULL 	/* find_potential_libraries */
22533965Sjdp};
22633965SjdpEOF
227