update copyright dates
[external/binutils.git] / ld / ldemul.c
1 /* ldemul.c -- clearing house for ld emulation states
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003
4    Free Software Foundation, Inc.
5
6 This file is part of GLD, the Gnu Linker.
7
8 GLD 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, or (at your option)
11 any later version.
12
13 GLD 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 GLD; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "getopt.h"
26
27 #include "ld.h"
28 #include "ldmisc.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldfile.h"
32 #include "ldemul.h"
33 #include "ldmain.h"
34 #include "ldemul-list.h"
35
36 ld_emulation_xfer_type *ld_emulation;
37
38 void
39 ldemul_hll (char *name)
40 {
41   ld_emulation->hll (name);
42 }
43
44 void
45 ldemul_syslib (char *name)
46 {
47   ld_emulation->syslib (name);
48 }
49
50 void
51 ldemul_after_parse (void)
52 {
53   ld_emulation->after_parse ();
54 }
55
56 void
57 ldemul_before_parse (void)
58 {
59   ld_emulation->before_parse ();
60 }
61
62 void
63 ldemul_after_open (void)
64 {
65   ld_emulation->after_open ();
66 }
67
68 void
69 ldemul_after_allocation (void)
70 {
71   ld_emulation->after_allocation ();
72 }
73
74 void
75 ldemul_before_allocation (void)
76 {
77   if (ld_emulation->before_allocation)
78     ld_emulation->before_allocation ();
79 }
80
81 void
82 ldemul_set_output_arch (void)
83 {
84   ld_emulation->set_output_arch ();
85 }
86
87 void
88 ldemul_finish (void)
89 {
90   if (ld_emulation->finish)
91     ld_emulation->finish ();
92 }
93
94 void
95 ldemul_set_symbols (void)
96 {
97   if (ld_emulation->set_symbols)
98     ld_emulation->set_symbols ();
99 }
100
101 void
102 ldemul_create_output_section_statements (void)
103 {
104   if (ld_emulation->create_output_section_statements)
105     ld_emulation->create_output_section_statements ();
106 }
107
108 char *
109 ldemul_get_script (int *isfile)
110 {
111   return ld_emulation->get_script (isfile);
112 }
113
114 bfd_boolean
115 ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
116                              lang_input_statement_type *entry)
117 {
118   if (ld_emulation->open_dynamic_archive)
119     return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
120   return FALSE;
121 }
122
123 bfd_boolean
124 ldemul_place_orphan (lang_input_statement_type *file, asection *s)
125 {
126   if (ld_emulation->place_orphan)
127     return (*ld_emulation->place_orphan) (file, s);
128   return FALSE;
129 }
130
131 void
132 ldemul_add_options (int ns, char **shortopts, int nl,
133                     struct option **longopts, int nrl,
134                     struct option **really_longopts)
135 {
136   if (ld_emulation->add_options)
137     (*ld_emulation->add_options) (ns, shortopts, nl, longopts,
138                                   nrl, really_longopts);
139 }
140
141 bfd_boolean
142 ldemul_handle_option (int optc)
143 {
144   if (ld_emulation->handle_option)
145     return (*ld_emulation->handle_option) (optc);
146   return FALSE;
147 }
148
149 bfd_boolean
150 ldemul_parse_args (int argc, char **argv)
151 {
152   /* Try and use the emulation parser if there is one.  */
153   if (ld_emulation->parse_args)
154     return (*ld_emulation->parse_args) (argc, argv);
155   return FALSE;
156 }
157
158 /* Let the emulation code handle an unrecognized file.  */
159
160 bfd_boolean
161 ldemul_unrecognized_file (lang_input_statement_type *entry)
162 {
163   if (ld_emulation->unrecognized_file)
164     return (*ld_emulation->unrecognized_file) (entry);
165   return FALSE;
166 }
167
168 /* Let the emulation code handle a recognized file.  */
169
170 bfd_boolean
171 ldemul_recognized_file (lang_input_statement_type *entry)
172 {
173   if (ld_emulation->recognized_file)
174     return (*ld_emulation->recognized_file) (entry);
175   return FALSE;
176 }
177
178 char *
179 ldemul_choose_target (int argc, char **argv)
180 {
181   return ld_emulation->choose_target (argc, argv);
182 }
183
184
185 /* The default choose_target function.  */
186
187 char *
188 ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
189 {
190   char *from_outside = getenv (TARGET_ENVIRON);
191   if (from_outside != (char *) NULL)
192     return from_outside;
193   return ld_emulation->target_name;
194 }
195
196 void
197 after_parse_default (void)
198 {
199 }
200
201 void
202 after_open_default (void)
203 {
204 }
205
206 void
207 after_allocation_default (void)
208 {
209 }
210
211 void
212 before_allocation_default (void)
213 {
214 }
215
216 void
217 set_output_arch_default (void)
218 {
219   /* Set the output architecture and machine if possible.  */
220   bfd_set_arch_mach (output_bfd,
221                      ldfile_output_architecture, ldfile_output_machine);
222 }
223
224 void
225 syslib_default (char *ignore ATTRIBUTE_UNUSED)
226 {
227   info_msg (_("%S SYSLIB ignored\n"));
228 }
229
230 void
231 hll_default (char *ignore ATTRIBUTE_UNUSED)
232 {
233   info_msg (_("%S HLL ignored\n"));
234 }
235
236 ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
237
238 void
239 ldemul_choose_mode (char *target)
240 {
241   ld_emulation_xfer_type **eptr = ld_emulations;
242   /* Ignore "gld" prefix.  */
243   if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd')
244     target += 3;
245   for (; *eptr; eptr++)
246     {
247       if (strcmp (target, (*eptr)->emulation_name) == 0)
248         {
249           ld_emulation = *eptr;
250           return;
251         }
252     }
253   einfo (_("%P: unrecognised emulation mode: %s\n"), target);
254   einfo (_("Supported emulations: "));
255   ldemul_list_emulations (stderr);
256   einfo ("%F\n");
257 }
258
259 void
260 ldemul_list_emulations (FILE *f)
261 {
262   ld_emulation_xfer_type **eptr = ld_emulations;
263   bfd_boolean first = TRUE;
264
265   for (; *eptr; eptr++)
266     {
267       if (first)
268         first = FALSE;
269       else
270         fprintf (f, " ");
271       fprintf (f, "%s", (*eptr)->emulation_name);
272     }
273 }
274
275 void
276 ldemul_list_emulation_options (FILE *f)
277 {
278   ld_emulation_xfer_type **eptr;
279   int options_found = 0;
280
281   for (eptr = ld_emulations; *eptr; eptr++)
282     {
283       ld_emulation_xfer_type *emul = *eptr;
284
285       if (emul->list_options)
286         {
287           fprintf (f, "%s: \n", emul->emulation_name);
288
289           emul->list_options (f);
290
291           options_found = 1;
292         }
293     }
294
295   if (! options_found)
296     fprintf (f, _("  no emulation specific options.\n"));
297 }
298
299 int
300 ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
301 {
302   if (ld_emulation->find_potential_libraries)
303     return ld_emulation->find_potential_libraries (name, entry);
304
305   return 0;
306 }
307
308 struct bfd_elf_version_expr *
309 ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
310 {
311   if (ld_emulation->new_vers_pattern)
312     entry = (*ld_emulation->new_vers_pattern) (entry);
313   return entry;
314 }