* read.c (read_a_source_file): Conditionally allow matched "
[binutils.git] / ld / emultempl / mipsecoff.em
blobd1200c46d7041b1f6ecce42099a003bf85838fc5
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script.  DO NOT EDIT! */
6 /* Handle embedded relocs for MIPS.
7    Copyright 1994 Free Software Foundation, Inc.
8    Written by Ian Lance Taylor <ian@cygnus.com> based on generic.em.
10 This file is part of GLD, the Gnu Linker.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
26 #define TARGET_IS_${EMULATION_NAME}
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldemul.h"
35 #include "ldfile.h"
36 #include "ldmisc.h"
38 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
39 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
40 static void check_sections PARAMS ((bfd *, asection *, PTR));
41 static void gld${EMULATION_NAME}_after_allocation PARAMS ((void));
42 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
44 static void
45 gld${EMULATION_NAME}_before_parse()
47 #ifndef TARGET_                 /* I.e., if not generic.  */
48   ldfile_output_architecture = bfd_arch_${ARCH};
49 #endif /* not TARGET_ */
52 /* This function is run after all the input files have been opened.
53    We create a .rel.sdata section for each input file with a non zero
54    .sdata section.  The BFD backend will fill in these sections with
55    magic numbers which can be used to relocate the data section at run
56    time.  This will only do the right thing if all the input files
57    have been compiled using -membedded-pic.  */
59 static void
60 gld${EMULATION_NAME}_after_open ()
62   bfd *abfd;
64   if (! command_line.embedded_relocs
65       || link_info.relocateable)
66     return;
68   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
69     {
70       asection *datasec;
72       datasec = bfd_get_section_by_name (abfd, ".sdata");
74       /* Note that we assume that the reloc_count field has already
75          been set up.  We could call bfd_get_reloc_upper_bound, but
76          that returns the size of a memory buffer rather than a reloc
77          count.  We do not want to call bfd_canonicalize_reloc,
78          because although it would always work it would force us to
79          read in the relocs into BFD canonical form, which would waste
80          a significant amount of time and memory.  */
81       if (datasec != NULL && datasec->reloc_count > 0)
82         {
83           asection *relsec;
85           relsec = bfd_make_section (abfd, ".rel.sdata");
86           if (relsec == NULL
87               || ! bfd_set_section_flags (abfd, relsec,
88                                           (SEC_ALLOC
89                                            | SEC_LOAD
90                                            | SEC_HAS_CONTENTS
91                                            | SEC_IN_MEMORY))
92               || ! bfd_set_section_alignment (abfd, relsec, 2)
93               || ! bfd_set_section_size (abfd, relsec,
94                                          datasec->reloc_count * 4))
95             einfo ("%F%B: can not create .rel.sdata section: %E\n");
96         }
98       /* Double check that all other data sections are empty, as is
99          required for embedded PIC code.  */
100       bfd_map_over_sections (abfd, check_sections, (PTR) datasec);
101     }
104 /* Check that of the data sections, only the .sdata section has
105    relocs.  This is called via bfd_map_over_sections.  */
107 static void
108 check_sections (abfd, sec, sdatasec)
109      bfd *abfd;
110      asection *sec;
111      PTR sdatasec;
113   if ((bfd_get_section_flags (abfd, sec) & SEC_CODE) == 0
114       && sec != (asection *) sdatasec
115       && sec->reloc_count != 0)
116     einfo ("%P%X: section %s has relocs; can not use --embedded-relocs\n",
117            abfd, bfd_get_section_name (abfd, sec));
120 /* This function is called after the section sizes and offsets have
121    been set.  If we are generating embedded relocs, it calls a special
122    BFD backend routine to do the work.  */
124 static void
125 gld${EMULATION_NAME}_after_allocation ()
127   bfd *abfd;
129   if (! command_line.embedded_relocs
130       || link_info.relocateable)
131     return;
133   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
134     {
135       asection *datasec, *relsec;
136       char *errmsg;
138       datasec = bfd_get_section_by_name (abfd, ".sdata");
140       if (datasec == NULL || datasec->reloc_count == 0)
141         continue;
143       relsec = bfd_get_section_by_name (abfd, ".rel.sdata");
144       ASSERT (relsec != NULL);
146       if (! bfd_mips_ecoff_create_embedded_relocs (abfd, &link_info,
147                                                    datasec, relsec,
148                                                    &errmsg))
149         {
150           if (errmsg == NULL)
151             einfo ("%B%X: can not create runtime reloc information: %E\n",
152                    abfd);
153           else
154             einfo ("%X%B: can not create runtime reloc information: %s\n",
155                    abfd, errmsg);
156         }
157     }
160 static char *
161 gld${EMULATION_NAME}_get_script(isfile)
162      int *isfile;
165 if test -n "$COMPILE_IN"
166 then
167 # Scripts compiled in.
169 # sed commands to quote an ld script as a C string.
170 sc="-f ${srcdir}/emultempl/stringify.sed"
172 cat >>e${EMULATION_NAME}.c <<EOF
173 {                            
174   *isfile = 0;
176   if (link_info.relocateable == true && config.build_constructors == true)
177     return
179 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
180 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
181 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
182 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
183 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
184 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
185 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
186 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
187 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
188 echo '; }'                                                 >> e${EMULATION_NAME}.c
190 else
191 # Scripts read from the filesystem.
193 cat >>e${EMULATION_NAME}.c <<EOF
194 {                            
195   *isfile = 1;
197   if (link_info.relocateable == true && config.build_constructors == true)
198     return "ldscripts/${EMULATION_NAME}.xu";
199   else if (link_info.relocateable == true)
200     return "ldscripts/${EMULATION_NAME}.xr";
201   else if (!config.text_read_only)
202     return "ldscripts/${EMULATION_NAME}.xbn";
203   else if (!config.magic_demand_paged)
204     return "ldscripts/${EMULATION_NAME}.xn";
205   else
206     return "ldscripts/${EMULATION_NAME}.x";
212 cat >>e${EMULATION_NAME}.c <<EOF
214 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
216   gld${EMULATION_NAME}_before_parse,
217   syslib_default,
218   hll_default,
219   after_parse_default,
220   gld${EMULATION_NAME}_after_open,
221   gld${EMULATION_NAME}_after_allocation,
222   set_output_arch_default,
223   ldemul_default_target,
224   before_allocation_default,
225   gld${EMULATION_NAME}_get_script,
226   "${EMULATION_NAME}",
227   "${OUTPUT_FORMAT}"