1 /* ldemul.c -- clearing house for ld emulation states
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
34 #include "ldemul-list.h"
36 static ld_emulation_xfer_type
*ld_emulation
;
39 ldemul_hll (char *name
)
41 ld_emulation
->hll (name
);
45 ldemul_syslib (char *name
)
47 ld_emulation
->syslib (name
);
51 ldemul_after_parse (void)
53 ld_emulation
->after_parse ();
57 ldemul_before_parse (void)
59 ld_emulation
->before_parse ();
63 ldemul_after_open (void)
65 ld_emulation
->after_open ();
69 ldemul_after_check_relocs (void)
71 ld_emulation
->after_check_relocs ();
75 ldemul_after_allocation (void)
77 ld_emulation
->after_allocation ();
81 ldemul_before_allocation (void)
83 ld_emulation
->before_allocation ();
87 ldemul_set_output_arch (void)
89 ld_emulation
->set_output_arch ();
95 ld_emulation
->finish ();
99 ldemul_set_symbols (void)
101 if (ld_emulation
->set_symbols
)
102 ld_emulation
->set_symbols ();
106 ldemul_create_output_section_statements (void)
108 if (ld_emulation
->create_output_section_statements
)
109 ld_emulation
->create_output_section_statements ();
113 ldemul_get_script (int *isfile
)
115 return ld_emulation
->get_script (isfile
);
119 ldemul_open_dynamic_archive (const char *arch
, search_dirs_type
*search
,
120 lang_input_statement_type
*entry
)
122 if (ld_emulation
->open_dynamic_archive
)
123 return (*ld_emulation
->open_dynamic_archive
) (arch
, search
, entry
);
127 lang_output_section_statement_type
*
128 ldemul_place_orphan (asection
*s
, const char *name
, int constraint
)
130 if (ld_emulation
->place_orphan
)
131 return (*ld_emulation
->place_orphan
) (s
, name
, constraint
);
136 ldemul_add_options (int ns
, char **shortopts
, int nl
,
137 struct option
**longopts
, int nrl
,
138 struct option
**really_longopts
)
140 if (ld_emulation
->add_options
)
141 (*ld_emulation
->add_options
) (ns
, shortopts
, nl
, longopts
,
142 nrl
, really_longopts
);
146 ldemul_handle_option (int optc
)
148 if (ld_emulation
->handle_option
)
149 return (*ld_emulation
->handle_option
) (optc
);
154 ldemul_parse_args (int argc
, char **argv
)
156 /* Try and use the emulation parser if there is one. */
157 if (ld_emulation
->parse_args
)
158 return (*ld_emulation
->parse_args
) (argc
, argv
);
162 /* Let the emulation code handle an unrecognized file. */
165 ldemul_unrecognized_file (lang_input_statement_type
*entry
)
167 if (ld_emulation
->unrecognized_file
)
168 return (*ld_emulation
->unrecognized_file
) (entry
);
172 /* Let the emulation code handle a recognized file. */
175 ldemul_recognized_file (lang_input_statement_type
*entry
)
177 if (ld_emulation
->recognized_file
)
178 return (*ld_emulation
->recognized_file
) (entry
);
183 ldemul_choose_target (int argc
, char **argv
)
185 return ld_emulation
->choose_target (argc
, argv
);
189 /* The default choose_target function. */
192 ldemul_default_target (int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
)
194 char *from_outside
= getenv (TARGET_ENVIRON
);
195 if (from_outside
!= (char *) NULL
)
197 return ld_emulation
->target_name
;
200 /* If the entry point was not specified as an address, then add the
201 symbol as undefined. This will cause ld to extract an archive
202 element defining the entry if ld is linking against such an archive.
204 We don't do this when generating shared libraries unless given -e
205 on the command line, because most shared libs are not designed to
206 be run as an executable. However, some are, eg. glibc ld.so and
207 may rely on the default linker script supplying ENTRY. So we can't
208 remove the ENTRY from the script, but would rather not insert
209 undefined _start syms. */
212 after_parse_default (void)
214 if (entry_symbol
.name
!= NULL
215 && (bfd_link_executable (&link_info
) || entry_from_cmdline
))
217 bfd_boolean is_vma
= FALSE
;
219 if (entry_from_cmdline
)
223 bfd_scan_vma (entry_symbol
.name
, &send
, 0);
224 is_vma
= *send
== '\0';
227 ldlang_add_undef (entry_symbol
.name
, entry_from_cmdline
);
229 if (config
.maxpagesize
== 0)
230 config
.maxpagesize
= bfd_emul_get_maxpagesize (default_target
);
231 if (config
.commonpagesize
== 0)
232 config
.commonpagesize
= bfd_emul_get_commonpagesize (default_target
,
237 after_open_default (void)
239 link_info
.big_endian
= TRUE
;
241 if (bfd_big_endian (link_info
.output_bfd
))
243 else if (bfd_little_endian (link_info
.output_bfd
))
244 link_info
.big_endian
= FALSE
;
247 if (command_line
.endian
== ENDIAN_BIG
)
249 else if (command_line
.endian
== ENDIAN_LITTLE
)
250 link_info
.big_endian
= FALSE
;
251 else if (command_line
.endian
== ENDIAN_UNSET
)
253 LANG_FOR_EACH_INPUT_STATEMENT (s
)
254 if (s
->the_bfd
!= NULL
)
256 if (bfd_little_endian (s
->the_bfd
))
257 link_info
.big_endian
= FALSE
;
265 after_check_relocs_default (void)
270 after_allocation_default (void)
272 lang_relax_sections (FALSE
);
276 before_allocation_default (void)
278 if (!bfd_link_relocatable (&link_info
))
279 strip_excluded_output_sections ();
283 finish_default (void)
285 if (!bfd_link_relocatable (&link_info
))
286 _bfd_fix_excluded_sec_syms (link_info
.output_bfd
, &link_info
);
290 set_output_arch_default (void)
292 /* Set the output architecture and machine if possible. */
293 bfd_set_arch_mach (link_info
.output_bfd
,
294 ldfile_output_architecture
, ldfile_output_machine
);
296 bfd_emul_set_maxpagesize (output_target
, config
.maxpagesize
);
297 bfd_emul_set_commonpagesize (output_target
, config
.commonpagesize
);
301 syslib_default (char *ignore ATTRIBUTE_UNUSED
)
303 info_msg (_("%pS SYSLIB ignored\n"), NULL
);
307 hll_default (char *ignore ATTRIBUTE_UNUSED
)
309 info_msg (_("%pS HLL ignored\n"), NULL
);
312 ld_emulation_xfer_type
*ld_emulations
[] = { EMULATION_LIST
};
315 ldemul_choose_mode (char *target
)
317 ld_emulation_xfer_type
**eptr
= ld_emulations
;
318 /* Ignore "gld" prefix. */
319 if (target
[0] == 'g' && target
[1] == 'l' && target
[2] == 'd')
321 for (; *eptr
; eptr
++)
323 if (strcmp (target
, (*eptr
)->emulation_name
) == 0)
325 ld_emulation
= *eptr
;
329 einfo (_("%P: unrecognised emulation mode: %s\n"), target
);
330 einfo (_("Supported emulations: "));
331 ldemul_list_emulations (stderr
);
336 ldemul_list_emulations (FILE *f
)
338 ld_emulation_xfer_type
**eptr
= ld_emulations
;
339 bfd_boolean first
= TRUE
;
341 for (; *eptr
; eptr
++)
347 fprintf (f
, "%s", (*eptr
)->emulation_name
);
352 ldemul_list_emulation_options (FILE *f
)
354 ld_emulation_xfer_type
**eptr
;
355 int options_found
= 0;
357 for (eptr
= ld_emulations
; *eptr
; eptr
++)
359 ld_emulation_xfer_type
*emul
= *eptr
;
361 if (emul
->list_options
)
363 fprintf (f
, "%s: \n", emul
->emulation_name
);
365 emul
->list_options (f
);
372 fprintf (f
, _(" no emulation specific options.\n"));
376 ldemul_find_potential_libraries (char *name
, lang_input_statement_type
*entry
)
378 if (ld_emulation
->find_potential_libraries
)
379 return ld_emulation
->find_potential_libraries (name
, entry
);
384 struct bfd_elf_version_expr
*
385 ldemul_new_vers_pattern (struct bfd_elf_version_expr
*entry
)
387 if (ld_emulation
->new_vers_pattern
)
388 entry
= (*ld_emulation
->new_vers_pattern
) (entry
);
393 ldemul_extra_map_file_text (bfd
*abfd
, struct bfd_link_info
*info
, FILE *mapf
)
395 if (ld_emulation
->extra_map_file_text
)
396 ld_emulation
->extra_map_file_text (abfd
, info
, mapf
);
400 ldemul_emit_ctf_early (void)
402 if (ld_emulation
->emit_ctf_early
)
403 return ld_emulation
->emit_ctf_early ();
404 /* If the emulation doesn't know if it wants to emit CTF early, it is going
410 ldemul_examine_strtab_for_ctf (struct ctf_file
*ctf_output
,
411 struct elf_sym_strtab
*syms
,
412 bfd_size_type symcount
,
413 struct elf_strtab_hash
*symstrtab
)
416 if (ld_emulation
->examine_strtab_for_ctf
)
417 ld_emulation
->examine_strtab_for_ctf (ctf_output
, syms
,
418 symcount
, symstrtab
);