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