1# This shell script emits a C file. -*- C -*-
2# Copyright 2002, 2003 Free Software Foundation, Inc.
3#   Written by Mitch Lichtenberg <mpl@broadcom.com> and
4#   Chris Demetriou <cgd@broadcom.com> based on m68kelf.em and mipsecoff.em.
5#
6# This file is part of GLD, the Gnu Linker.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22
23# This file is sourced from elf32.em, and defines some extra routines for m68k
24# embedded systems using ELF and for some other systems using m68k ELF.  While
25# it is sourced from elf32.em for all m68k ELF configurations, here we include
26# only the features we want depending on the configuration.
27
28case ${target} in
29  mips*-*-elf)
30    echo "#define SUPPORT_EMBEDDED_RELOCS" >>e${EMULATION_NAME}.c
31    ;;
32esac
33
34cat >>e${EMULATION_NAME}.c <<EOF
35
36#ifdef SUPPORT_EMBEDDED_RELOCS
37static void mips_elf${ELFSIZE}_check_sections (bfd *, asection *, void *);
38#endif
39
40/* This function is run after all the input files have been opened.  */
41
42static void
43mips_elf${ELFSIZE}_after_open (void)
44{
45  /* Call the standard elf routine.  */
46  gld${EMULATION_NAME}_after_open ();
47
48#ifdef SUPPORT_EMBEDDED_RELOCS
49  if (command_line.embedded_relocs && (! link_info.relocatable))
50    {
51      bfd *abfd;
52
53      /* In the embedded relocs mode we create a .rel.sdata section for
54	 each input file with a .sdata section which has has
55	 relocations.  The BFD backend will fill in these sections
56	 with magic numbers which can be used to relocate the data
57	 section at run time.  */
58      for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
59        {
60          asection *datasec;
61
62	  /* As first-order business, make sure that each input BFD is
63	     ELF.  We need to call a special BFD backend function to
64	     generate the embedded relocs, and we have that function
65	     only for ELF */
66
67          if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
68	    einfo ("%F%B: all input objects must be ELF for --embedded-relocs\n");
69
70	  if (bfd_get_arch_size (abfd) != ${ELFSIZE})
71	    einfo ("%F%B: all input objects must be ${ELFSIZE}-bit ELF for --embedded-relocs\n");
72
73          datasec = bfd_get_section_by_name (abfd, ".sdata");
74
75          /* Note that we assume that the reloc_count field has already
76             been set up.  We could call bfd_get_reloc_upper_bound, but
77             that returns the size of a memory buffer rather than a reloc
78             count.  We do not want to call bfd_canonicalize_reloc,
79             because although it would always work it would force us to
80             read in the relocs into BFD canonical form, which would waste
81             a significant amount of time and memory.  */
82
83          if (datasec != NULL && datasec->reloc_count > 0)
84 	    {
85              asection *relsec;
86
87	      relsec = bfd_make_section (abfd, ".rel.sdata");
88	      if (relsec == NULL
89	          || ! bfd_set_section_flags (abfd, relsec,
90					      (SEC_ALLOC
91					       | SEC_LOAD
92					       | SEC_HAS_CONTENTS
93					       | SEC_IN_MEMORY))
94                  || ! bfd_set_section_alignment (abfd, relsec,
95						  (${ELFSIZE} == 32) ? 2 : 3)
96	          || ! bfd_set_section_size (abfd, relsec,
97		  			     datasec->reloc_count
98					     * ((${ELFSIZE} / 8) + 8)))
99	        einfo ("%F%B: cannot create .rel.sdata section: %E\n");
100	    }
101
102          /* Double check that all other data sections have no relocs,
103             as is required for embedded PIC code.  */
104          bfd_map_over_sections (abfd, mips_elf${ELFSIZE}_check_sections,
105				 datasec);
106        }
107    }
108#endif /* SUPPORT_EMBEDDED_RELOCS */
109}
110
111#ifdef SUPPORT_EMBEDDED_RELOCS
112/* Check that of the data sections, only the .sdata section has
113   relocs.  This is called via bfd_map_over_sections.  */
114
115static void
116mips_elf${ELFSIZE}_check_sections (bfd *abfd, asection *sec, void *sdatasec)
117{
118  if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
119      && sec != sdatasec
120      && sec->reloc_count != 0)
121    einfo ("%B%X: section %s has relocs; cannot use --embedded-relocs\n",
122	   abfd, bfd_get_section_name (abfd, sec));
123}
124#endif /* SUPPORT_EMBEDDED_RELOCS */
125
126/* This function is called after the section sizes and offsets have
127   been set.  If we are generating embedded relocs, it calls a special
128   BFD backend routine to do the work.  */
129
130static void
131mips_elf${ELFSIZE}_after_allocation (void)
132{
133  /* Call the standard elf routine.  */
134  after_allocation_default ();
135
136#ifdef SUPPORT_EMBEDDED_RELOCS
137  if (command_line.embedded_relocs && (! link_info.relocatable))
138    {
139      bfd *abfd;
140
141      for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
142	{
143	  asection *datasec, *relsec;
144	  char *errmsg;
145
146	  datasec = bfd_get_section_by_name (abfd, ".sdata");
147
148	  if (datasec == NULL || datasec->reloc_count == 0)
149	    continue;
150
151	  relsec = bfd_get_section_by_name (abfd, ".rel.sdata");
152	  ASSERT (relsec != NULL);
153
154	  if (! bfd_mips_elf${ELFSIZE}_create_embedded_relocs (abfd,
155							       &link_info,
156							       datasec,
157							       relsec,
158							       &errmsg))
159	    {
160	      if (errmsg == NULL)
161		einfo ("%B%X: can not create runtime reloc information: %E\n",
162		       abfd);
163	      else
164		einfo ("%X%B: can not create runtime reloc information: %s\n",
165		       abfd, errmsg);
166	    }
167	}
168    }
169#endif /* SUPPORT_EMBEDDED_RELOCS */
170}
171
172EOF
173
174# We have our own after_open and after_allocation functions, but they call
175# the standard routines, so give them a different name.
176LDEMUL_AFTER_OPEN=mips_elf${ELFSIZE}_after_open
177LDEMUL_AFTER_ALLOCATION=mips_elf${ELFSIZE}_after_allocation
178