* lib/ld-lib.exp (run_dump_test): Add -L$srcdir/$subdir.
[external/binutils.git] / ld / emultempl / mipsecoff.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then 
4   OUTPUT_ARCH=${ARCH}
5 else
6   OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
8 cat >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.
12    Copyright 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
13    Written by Ian Lance Taylor <ian@cygnus.com> based on generic.em.
14
15 This file is part of GLD, the Gnu Linker.
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, 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"
39 #include "ldmisc.h"
40
41 #include "ldexp.h"
42 #include "ldlang.h"
43 #include "ldfile.h"
44 #include "ldemul.h"
45
46 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
47 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
48 static void check_sections PARAMS ((bfd *, asection *, PTR));
49 static void gld${EMULATION_NAME}_after_allocation PARAMS ((void));
50 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
51
52 static void
53 gld${EMULATION_NAME}_before_parse()
54 {
55 #ifndef TARGET_                 /* I.e., if not generic.  */
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};
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
75 static void
76 gld${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
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
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
128 static void
129 check_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)
137     einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
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
145 static void
146 gld${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
181 static char *
182 gld${EMULATION_NAME}_get_script(isfile)
183      int *isfile;
184 EOF
185
186 if test -n "$COMPILE_IN"
187 then
188 # Scripts compiled in.
189
190 # sed commands to quote an ld script as a C string.
191 sc="-f stringify.sed"
192
193 cat >>e${EMULATION_NAME}.c <<EOF
194 {                            
195   *isfile = 0;
196
197   if (link_info.relocateable == true && config.build_constructors == true)
198     return
199 EOF
200 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
201 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
202 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
203 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
204 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
205 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
206 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
207 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
208 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
209 echo '; }'                                                 >> e${EMULATION_NAME}.c
210
211 else
212 # Scripts read from the filesystem.
213
214 cat >>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 }
229 EOF
230
231 fi
232
233 cat >>e${EMULATION_NAME}.c <<EOF
234
235 struct 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}",
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 */
257   NULL, /* recognized file */
258   NULL  /* find_potential_libraries */
259 };
260 EOF