1 /* ldemul.c -- clearing house for ld emulation states
2 Copyright (C) 1991-2025 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_before_plugin_all_symbols_read (void)
65 if (ld_emulation
->before_plugin_all_symbols_read
)
66 ld_emulation
->before_plugin_all_symbols_read ();
70 ldemul_after_open (void)
72 ld_emulation
->after_open ();
76 ldemul_after_check_relocs (void)
78 ld_emulation
->after_check_relocs ();
82 ldemul_before_place_orphans (void)
84 ld_emulation
->before_place_orphans ();
88 ldemul_after_allocation (void)
90 ld_emulation
->after_allocation ();
94 ldemul_before_allocation (void)
96 ld_emulation
->before_allocation ();
100 ldemul_set_output_arch (void)
102 ld_emulation
->set_output_arch ();
108 ld_emulation
->finish ();
112 ldemul_set_symbols (void)
114 if (ld_emulation
->set_symbols
)
115 ld_emulation
->set_symbols ();
119 ldemul_create_output_section_statements (void)
121 if (ld_emulation
->create_output_section_statements
)
122 ld_emulation
->create_output_section_statements ();
126 ldemul_get_script (int *isfile
)
128 return ld_emulation
->get_script (isfile
);
132 ldemul_open_dynamic_archive (const char *arch
, search_dirs_type
*search
,
133 lang_input_statement_type
*entry
)
135 if (ld_emulation
->open_dynamic_archive
)
136 return (*ld_emulation
->open_dynamic_archive
) (arch
, search
, entry
);
140 lang_output_section_statement_type
*
141 ldemul_place_orphan (asection
*s
, const char *name
, int constraint
)
143 if (ld_emulation
->place_orphan
)
144 return (*ld_emulation
->place_orphan
) (s
, name
, constraint
);
149 ldemul_add_options (int ns
, char **shortopts
, int nl
,
150 struct option
**longopts
, int nrl
,
151 struct option
**really_longopts
)
153 if (ld_emulation
->add_options
)
154 (*ld_emulation
->add_options
) (ns
, shortopts
, nl
, longopts
,
155 nrl
, really_longopts
);
159 ldemul_handle_option (int optc
)
161 if (ld_emulation
->handle_option
)
162 return (*ld_emulation
->handle_option
) (optc
);
167 ldemul_parse_args (int argc
, char **argv
)
169 /* Try and use the emulation parser if there is one. */
170 if (ld_emulation
->parse_args
)
171 return (*ld_emulation
->parse_args
) (argc
, argv
);
175 /* Let the emulation code handle an unrecognized file. */
178 ldemul_unrecognized_file (lang_input_statement_type
*entry
)
180 if (ld_emulation
->unrecognized_file
)
181 return (*ld_emulation
->unrecognized_file
) (entry
);
185 /* Let the emulation code handle a recognized file. */
188 ldemul_recognized_file (lang_input_statement_type
*entry
)
190 if (ld_emulation
->recognized_file
)
191 return (*ld_emulation
->recognized_file
) (entry
);
196 ldemul_choose_target (int argc
, char **argv
)
198 return ld_emulation
->choose_target (argc
, argv
);
202 /* The default choose_target function. */
205 ldemul_default_target (int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
)
207 char *from_outside
= getenv (TARGET_ENVIRON
);
208 if (from_outside
!= (char *) NULL
)
210 return ld_emulation
->target_name
;
213 /* If the entry point was not specified as an address, then add the
214 symbol as undefined. This will cause ld to extract an archive
215 element defining the entry if ld is linking against such an archive.
217 We don't do this when generating shared libraries unless given -e
218 on the command line, because most shared libs are not designed to
219 be run as an executable. However, some are, eg. glibc ld.so and
220 may rely on the default linker script supplying ENTRY. So we can't
221 remove the ENTRY from the script, but would rather not insert
222 undefined _start syms. */
225 after_parse_default (void)
227 if (entry_symbol
.name
!= NULL
228 && (bfd_link_executable (&link_info
) || entry_from_cmdline
))
232 if (entry_from_cmdline
)
236 bfd_scan_vma (entry_symbol
.name
, &send
, 0);
237 is_vma
= *send
== '\0';
240 ldlang_add_undef (entry_symbol
.name
, entry_from_cmdline
);
242 if (link_info
.maxpagesize
== 0)
243 link_info
.maxpagesize
= bfd_emul_get_maxpagesize (default_target
);
244 if (link_info
.commonpagesize
== 0)
245 link_info
.commonpagesize
= bfd_emul_get_commonpagesize (default_target
);
249 after_open_default (void)
251 link_info
.big_endian
= true;
253 if (bfd_big_endian (link_info
.output_bfd
))
255 else if (bfd_little_endian (link_info
.output_bfd
))
256 link_info
.big_endian
= false;
259 if (command_line
.endian
== ENDIAN_BIG
)
261 else if (command_line
.endian
== ENDIAN_LITTLE
)
262 link_info
.big_endian
= false;
263 else if (command_line
.endian
== ENDIAN_UNSET
)
265 LANG_FOR_EACH_INPUT_STATEMENT (s
)
266 if (s
->the_bfd
!= NULL
)
268 if (bfd_little_endian (s
->the_bfd
))
269 link_info
.big_endian
= false;
277 after_check_relocs_default (void)
282 before_place_orphans_default (void)
287 after_allocation_default (void)
289 lang_relax_sections (false);
293 before_allocation_default (void)
295 if (!bfd_link_relocatable (&link_info
))
296 strip_excluded_output_sections ();
300 finish_default (void)
302 lang_output_section_statement_type
*os
;
303 for (os
= (void *) lang_os_list
.head
; os
!= NULL
; os
= os
->next
)
308 if (!bfd_link_relocatable (&link_info
))
309 _bfd_fix_excluded_sec_syms (link_info
.output_bfd
, &link_info
);
313 set_output_arch_default (void)
315 /* Set the output architecture and machine if possible. */
316 bfd_set_arch_mach (link_info
.output_bfd
,
317 ldfile_output_architecture
, ldfile_output_machine
);
321 syslib_default (char *ignore ATTRIBUTE_UNUSED
)
323 info_msg (_("%pS SYSLIB ignored\n"), NULL
);
327 hll_default (char *ignore ATTRIBUTE_UNUSED
)
329 info_msg (_("%pS HLL ignored\n"), NULL
);
332 ld_emulation_xfer_type
*ld_emulations
[] = { EMULATION_LIST
};
335 ldemul_choose_mode (char *target
)
337 ld_emulation_xfer_type
**eptr
= ld_emulations
;
338 /* Ignore "gld" prefix. */
339 if (target
[0] == 'g' && target
[1] == 'l' && target
[2] == 'd')
341 for (; *eptr
; eptr
++)
343 if (strcmp (target
, (*eptr
)->emulation_name
) == 0)
345 ld_emulation
= *eptr
;
349 einfo (_("%P: unrecognised emulation mode: %s\n"), target
);
350 einfo (_("Supported emulations: "));
351 ldemul_list_emulations (stderr
);
356 ldemul_list_emulations (FILE *f
)
358 ld_emulation_xfer_type
**eptr
= ld_emulations
;
361 for (; *eptr
; eptr
++)
367 fprintf (f
, "%s", (*eptr
)->emulation_name
);
372 ldemul_list_emulation_options (FILE *f
)
374 ld_emulation_xfer_type
**eptr
;
375 int options_found
= 0;
377 for (eptr
= ld_emulations
; *eptr
; eptr
++)
379 ld_emulation_xfer_type
*emul
= *eptr
;
381 if (emul
->list_options
)
383 fprintf (f
, "%s: \n", emul
->emulation_name
);
385 emul
->list_options (f
);
392 fprintf (f
, _(" no emulation specific options.\n"));
396 ldemul_find_potential_libraries (char *name
, lang_input_statement_type
*entry
)
398 if (ld_emulation
->find_potential_libraries
)
399 return ld_emulation
->find_potential_libraries (name
, entry
);
404 struct bfd_elf_version_expr
*
405 ldemul_new_vers_pattern (struct bfd_elf_version_expr
*entry
)
407 if (ld_emulation
->new_vers_pattern
)
408 entry
= (*ld_emulation
->new_vers_pattern
) (entry
);
413 ldemul_extra_map_file_text (bfd
*abfd
, struct bfd_link_info
*info
, FILE *mapf
)
415 if (ld_emulation
->extra_map_file_text
)
416 ld_emulation
->extra_map_file_text (abfd
, info
, mapf
);
420 ldemul_emit_ctf_early (void)
422 if (ld_emulation
->emit_ctf_early
)
423 return ld_emulation
->emit_ctf_early ();
424 /* If the emulation doesn't know if it wants to emit CTF early, it is going
430 ldemul_acquire_strings_for_ctf (struct ctf_dict
*ctf_output
,
431 struct elf_strtab_hash
*symstrtab
)
433 if (ld_emulation
->acquire_strings_for_ctf
)
434 ld_emulation
->acquire_strings_for_ctf (ctf_output
, symstrtab
);
438 ldemul_new_dynsym_for_ctf (struct ctf_dict
*ctf_output
, int symidx
,
439 struct elf_internal_sym
*sym
)
441 if (ld_emulation
->new_dynsym_for_ctf
)
442 ld_emulation
->new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
446 ldemul_print_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
448 if (ld_emulation
->print_symbol
)
449 return ld_emulation
->print_symbol (hash_entry
, ptr
);
450 return print_one_symbol (hash_entry
, ptr
);