2001-05-24 H.J. Lu <hjl@gnu.org>
[deliverable/binutils-gdb.git] / ld / emultempl / mipsecoff.em
CommitLineData
252b5132
RH
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
86af25fe
L
3if [ -z "$MACHINE" ]; then
4 OUTPUT_ARCH=${ARCH}
5else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7fi
252b5132
RH
8cat >e${EMULATION_NAME}.c <<EOF
9/* This file is is generated by a shell script. DO NOT EDIT! */
10
11/* Handle embedded relocs for MIPS.
a2b64bed 12 Copyright 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
252b5132
RH
13 Written by Ian Lance Taylor <ian@cygnus.com> based on generic.em.
14
15This file is part of GLD, the Gnu Linker.
16
17This program is free software; you can redistribute it and/or modify
18it under the terms of the GNU General Public License as published by
19the Free Software Foundation; either version 2 of the License, or
20(at your option) any later version.
21
22This program is distributed in the hope that it will be useful,
23but WITHOUT ANY WARRANTY; without even the implied warranty of
24MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25GNU General Public License for more details.
26
27You should have received a copy of the GNU General Public License
28along with this program; if not, write to the Free Software
29Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30
31#define TARGET_IS_${EMULATION_NAME}
32
33#include "bfd.h"
34#include "sysdep.h"
35#include "bfdlink.h"
36
37#include "ld.h"
38#include "ldmain.h"
252b5132
RH
39#include "ldmisc.h"
40
b71e2778
AM
41#include "ldexp.h"
42#include "ldlang.h"
43#include "ldfile.h"
44#include "ldemul.h"
45
252b5132
RH
46static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
47static void gld${EMULATION_NAME}_after_open PARAMS ((void));
48static void check_sections PARAMS ((bfd *, asection *, PTR));
49static void gld${EMULATION_NAME}_after_allocation PARAMS ((void));
50static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
51
52static void
53gld${EMULATION_NAME}_before_parse()
54{
55#ifndef TARGET_ /* I.e., if not generic. */
86af25fe
L
56 const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
57 if (arch)
58 {
59 ldfile_output_architecture = arch->arch;
60 ldfile_output_machine = arch->mach;
61 ldfile_output_machine_name = arch->printable_name;
62 }
63 else
64 ldfile_output_architecture = bfd_arch_${ARCH};
252b5132
RH
65#endif /* not TARGET_ */
66}
67
68/* This function is run after all the input files have been opened.
69 We create a .rel.sdata section for each input file with a non zero
70 .sdata section. The BFD backend will fill in these sections with
71 magic numbers which can be used to relocate the data section at run
72 time. This will only do the right thing if all the input files
73 have been compiled using -membedded-pic. */
74
75static void
76gld${EMULATION_NAME}_after_open ()
77{
78 bfd *abfd;
79
80 if (! command_line.embedded_relocs
81 || link_info.relocateable)
82 return;
83
84 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
85 {
86 asection *datasec;
87
57a6fd07
DB
88 /* As first-order business, make sure that each input BFD is ECOFF. It
89 better be, as we are directly calling an ECOFF backend function. */
90 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour)
91 einfo ("%F%B: all input objects must be ECOFF for --embedded-relocs\n");
92
252b5132
RH
93 datasec = bfd_get_section_by_name (abfd, ".sdata");
94
95 /* Note that we assume that the reloc_count field has already
96 been set up. We could call bfd_get_reloc_upper_bound, but
97 that returns the size of a memory buffer rather than a reloc
98 count. We do not want to call bfd_canonicalize_reloc,
99 because although it would always work it would force us to
100 read in the relocs into BFD canonical form, which would waste
101 a significant amount of time and memory. */
102 if (datasec != NULL && datasec->reloc_count > 0)
103 {
104 asection *relsec;
105
106 relsec = bfd_make_section (abfd, ".rel.sdata");
107 if (relsec == NULL
108 || ! bfd_set_section_flags (abfd, relsec,
109 (SEC_ALLOC
110 | SEC_LOAD
111 | SEC_HAS_CONTENTS
112 | SEC_IN_MEMORY))
113 || ! bfd_set_section_alignment (abfd, relsec, 2)
114 || ! bfd_set_section_size (abfd, relsec,
115 datasec->reloc_count * 4))
116 einfo ("%F%B: can not create .rel.sdata section: %E\n");
117 }
118
119 /* Double check that all other data sections are empty, as is
120 required for embedded PIC code. */
121 bfd_map_over_sections (abfd, check_sections, (PTR) datasec);
122 }
123}
124
125/* Check that of the data sections, only the .sdata section has
126 relocs. This is called via bfd_map_over_sections. */
127
128static void
129check_sections (abfd, sec, sdatasec)
130 bfd *abfd;
131 asection *sec;
132 PTR sdatasec;
133{
134 if ((bfd_get_section_flags (abfd, sec) & SEC_CODE) == 0
135 && sec != (asection *) sdatasec
136 && sec->reloc_count != 0)
57a6fd07 137 einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
252b5132
RH
138 abfd, bfd_get_section_name (abfd, sec));
139}
140
141/* This function is called after the section sizes and offsets have
142 been set. If we are generating embedded relocs, it calls a special
143 BFD backend routine to do the work. */
144
145static void
146gld${EMULATION_NAME}_after_allocation ()
147{
148 bfd *abfd;
149
150 if (! command_line.embedded_relocs
151 || link_info.relocateable)
152 return;
153
154 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
155 {
156 asection *datasec, *relsec;
157 char *errmsg;
158
159 datasec = bfd_get_section_by_name (abfd, ".sdata");
160
161 if (datasec == NULL || datasec->reloc_count == 0)
162 continue;
163
164 relsec = bfd_get_section_by_name (abfd, ".rel.sdata");
165 ASSERT (relsec != NULL);
166
167 if (! bfd_mips_ecoff_create_embedded_relocs (abfd, &link_info,
168 datasec, relsec,
169 &errmsg))
170 {
171 if (errmsg == NULL)
172 einfo ("%B%X: can not create runtime reloc information: %E\n",
173 abfd);
174 else
175 einfo ("%X%B: can not create runtime reloc information: %s\n",
176 abfd, errmsg);
177 }
178 }
179}
180
181static char *
182gld${EMULATION_NAME}_get_script(isfile)
183 int *isfile;
184EOF
185
186if test -n "$COMPILE_IN"
187then
188# Scripts compiled in.
189
190# sed commands to quote an ld script as a C string.
597e2591 191sc="-f stringify.sed"
252b5132
RH
192
193cat >>e${EMULATION_NAME}.c <<EOF
194{
195 *isfile = 0;
196
197 if (link_info.relocateable == true && config.build_constructors == true)
198 return
199EOF
200sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
201echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
202sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
203echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
204sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
205echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
206sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
207echo ' ; else return' >> e${EMULATION_NAME}.c
208sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
209echo '; }' >> e${EMULATION_NAME}.c
210
211else
212# Scripts read from the filesystem.
213
214cat >>e${EMULATION_NAME}.c <<EOF
215{
216 *isfile = 1;
217
218 if (link_info.relocateable == true && config.build_constructors == true)
219 return "ldscripts/${EMULATION_NAME}.xu";
220 else if (link_info.relocateable == true)
221 return "ldscripts/${EMULATION_NAME}.xr";
222 else if (!config.text_read_only)
223 return "ldscripts/${EMULATION_NAME}.xbn";
224 else if (!config.magic_demand_paged)
225 return "ldscripts/${EMULATION_NAME}.xn";
226 else
227 return "ldscripts/${EMULATION_NAME}.x";
228}
229EOF
230
231fi
232
233cat >>e${EMULATION_NAME}.c <<EOF
234
235struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
236{
237 gld${EMULATION_NAME}_before_parse,
238 syslib_default,
239 hll_default,
240 after_parse_default,
241 gld${EMULATION_NAME}_after_open,
242 gld${EMULATION_NAME}_after_allocation,
243 set_output_arch_default,
244 ldemul_default_target,
245 before_allocation_default,
246 gld${EMULATION_NAME}_get_script,
247 "${EMULATION_NAME}",
e1c47aa4
AM
248 "${OUTPUT_FORMAT}",
249 NULL, /* finish */
250 NULL, /* create output section statements */
251 NULL, /* open dynamic archive */
252 NULL, /* place orphan */
253 NULL, /* set symbols */
254 NULL, /* parse args */
255 NULL, /* unrecognized file */
256 NULL, /* list options */
40d109bf
AM
257 NULL, /* recognized file */
258 NULL /* find_potential_libraries */
252b5132
RH
259};
260EOF
This page took 0.087464 seconds and 4 git commands to generate.