]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldemul.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / ldemul.c
CommitLineData
252b5132 1/* ldemul.c -- clearing house for ld emulation states
250d07de 2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
252b5132 3
f96b4a7b
NC
4 This file is part of the GNU Binutils.
5
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.
10
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.
15
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. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
fcf65871 23#include "getopt.h"
8423293d 24#include "bfdlink.h"
1ff6de03 25#include "ctf-api.h"
252b5132
RH
26
27#include "ld.h"
252b5132
RH
28#include "ldmisc.h"
29#include "ldexp.h"
30#include "ldlang.h"
31#include "ldfile.h"
b71e2778 32#include "ldemul.h"
252b5132
RH
33#include "ldmain.h"
34#include "ldemul-list.h"
35
279e75dc 36static ld_emulation_xfer_type *ld_emulation;
252b5132
RH
37
38void
62b635b6 39ldemul_hll (char *name)
252b5132 40{
4de2d33d 41 ld_emulation->hll (name);
252b5132
RH
42}
43
4de2d33d 44void
62b635b6 45ldemul_syslib (char *name)
252b5132 46{
4de2d33d 47 ld_emulation->syslib (name);
252b5132
RH
48}
49
50void
62b635b6 51ldemul_after_parse (void)
252b5132 52{
4de2d33d 53 ld_emulation->after_parse ();
252b5132
RH
54}
55
56void
62b635b6 57ldemul_before_parse (void)
252b5132 58{
4de2d33d 59 ld_emulation->before_parse ();
252b5132
RH
60}
61
62void
62b635b6 63ldemul_after_open (void)
252b5132
RH
64{
65 ld_emulation->after_open ();
66}
67
5c3261b0
L
68void
69ldemul_after_check_relocs (void)
70{
71 ld_emulation->after_check_relocs ();
72}
73
9b538ba7
L
74void
75ldemul_before_place_orphans (void)
76{
77 ld_emulation->before_place_orphans ();
78}
79
4de2d33d 80void
62b635b6 81ldemul_after_allocation (void)
252b5132 82{
4de2d33d 83 ld_emulation->after_allocation ();
252b5132
RH
84}
85
4de2d33d 86void
62b635b6 87ldemul_before_allocation (void)
252b5132 88{
8423293d 89 ld_emulation->before_allocation ();
252b5132
RH
90}
91
252b5132 92void
62b635b6 93ldemul_set_output_arch (void)
252b5132 94{
4de2d33d 95 ld_emulation->set_output_arch ();
252b5132
RH
96}
97
98void
62b635b6 99ldemul_finish (void)
252b5132 100{
1e035701 101 ld_emulation->finish ();
252b5132
RH
102}
103
104void
62b635b6 105ldemul_set_symbols (void)
252b5132
RH
106{
107 if (ld_emulation->set_symbols)
4de2d33d 108 ld_emulation->set_symbols ();
252b5132
RH
109}
110
111void
62b635b6 112ldemul_create_output_section_statements (void)
252b5132
RH
113{
114 if (ld_emulation->create_output_section_statements)
4de2d33d 115 ld_emulation->create_output_section_statements ();
252b5132
RH
116}
117
118char *
62b635b6 119ldemul_get_script (int *isfile)
252b5132 120{
4de2d33d 121 return ld_emulation->get_script (isfile);
252b5132
RH
122}
123
b34976b6 124bfd_boolean
62b635b6
KH
125ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
126 lang_input_statement_type *entry)
252b5132
RH
127{
128 if (ld_emulation->open_dynamic_archive)
129 return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
b34976b6 130 return FALSE;
252b5132
RH
131}
132
c2edb4b8 133lang_output_section_statement_type *
8a99a385 134ldemul_place_orphan (asection *s, const char *name, int constraint)
252b5132
RH
135{
136 if (ld_emulation->place_orphan)
8a99a385 137 return (*ld_emulation->place_orphan) (s, name, constraint);
c2edb4b8 138 return NULL;
252b5132
RH
139}
140
3bcf5557 141void
62b635b6
KH
142ldemul_add_options (int ns, char **shortopts, int nl,
143 struct option **longopts, int nrl,
144 struct option **really_longopts)
3bcf5557
AM
145{
146 if (ld_emulation->add_options)
147 (*ld_emulation->add_options) (ns, shortopts, nl, longopts,
148 nrl, really_longopts);
149}
150
151bfd_boolean
62b635b6 152ldemul_handle_option (int optc)
3bcf5557
AM
153{
154 if (ld_emulation->handle_option)
155 return (*ld_emulation->handle_option) (optc);
156 return FALSE;
157}
158
159bfd_boolean
62b635b6 160ldemul_parse_args (int argc, char **argv)
252b5132 161{
4de2d33d 162 /* Try and use the emulation parser if there is one. */
252b5132 163 if (ld_emulation->parse_args)
3bcf5557 164 return (*ld_emulation->parse_args) (argc, argv);
3aa97c58 165 return FALSE;
252b5132
RH
166}
167
168/* Let the emulation code handle an unrecognized file. */
169
b34976b6 170bfd_boolean
62b635b6 171ldemul_unrecognized_file (lang_input_statement_type *entry)
252b5132
RH
172{
173 if (ld_emulation->unrecognized_file)
174 return (*ld_emulation->unrecognized_file) (entry);
b34976b6 175 return FALSE;
252b5132
RH
176}
177
178/* Let the emulation code handle a recognized file. */
179
b34976b6 180bfd_boolean
62b635b6 181ldemul_recognized_file (lang_input_statement_type *entry)
252b5132
RH
182{
183 if (ld_emulation->recognized_file)
184 return (*ld_emulation->recognized_file) (entry);
b34976b6 185 return FALSE;
252b5132
RH
186}
187
188char *
62b635b6 189ldemul_choose_target (int argc, char **argv)
252b5132 190{
742aeb63 191 return ld_emulation->choose_target (argc, argv);
252b5132
RH
192}
193
742aeb63 194
252b5132
RH
195/* The default choose_target function. */
196
197char *
62b635b6 198ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
252b5132
RH
199{
200 char *from_outside = getenv (TARGET_ENVIRON);
4de2d33d 201 if (from_outside != (char *) NULL)
252b5132
RH
202 return from_outside;
203 return ld_emulation->target_name;
204}
205
97b11f40
AM
206/* If the entry point was not specified as an address, then add the
207 symbol as undefined. This will cause ld to extract an archive
208 element defining the entry if ld is linking against such an archive.
209
210 We don't do this when generating shared libraries unless given -e
211 on the command line, because most shared libs are not designed to
212 be run as an executable. However, some are, eg. glibc ld.so and
213 may rely on the default linker script supplying ENTRY. So we can't
214 remove the ENTRY from the script, but would rather not insert
215 undefined _start syms. */
216
4de2d33d 217void
62b635b6 218after_parse_default (void)
252b5132 219{
97b11f40 220 if (entry_symbol.name != NULL
0e1862bb 221 && (bfd_link_executable (&link_info) || entry_from_cmdline))
97b11f40
AM
222 {
223 bfd_boolean is_vma = FALSE;
224
225 if (entry_from_cmdline)
226 {
227 const char *send;
228
229 bfd_scan_vma (entry_symbol.name, &send, 0);
230 is_vma = *send == '\0';
231 }
232 if (!is_vma)
24898b70 233 ldlang_add_undef (entry_symbol.name, entry_from_cmdline);
97b11f40 234 }
c410035d
AM
235 if (link_info.maxpagesize == 0)
236 link_info.maxpagesize = bfd_emul_get_maxpagesize (default_target);
237 if (link_info.commonpagesize == 0)
238 link_info.commonpagesize = bfd_emul_get_commonpagesize (default_target,
239 link_info.relro);
252b5132
RH
240}
241
242void
62b635b6 243after_open_default (void)
252b5132 244{
22216541
AM
245 link_info.big_endian = TRUE;
246
247 if (bfd_big_endian (link_info.output_bfd))
248 ;
249 else if (bfd_little_endian (link_info.output_bfd))
250 link_info.big_endian = FALSE;
251 else
252 {
253 if (command_line.endian == ENDIAN_BIG)
254 ;
255 else if (command_line.endian == ENDIAN_LITTLE)
256 link_info.big_endian = FALSE;
257 else if (command_line.endian == ENDIAN_UNSET)
258 {
259 LANG_FOR_EACH_INPUT_STATEMENT (s)
260 if (s->the_bfd != NULL)
261 {
262 if (bfd_little_endian (s->the_bfd))
263 link_info.big_endian = FALSE;
264 break;
265 }
266 }
267 }
252b5132
RH
268}
269
5c3261b0
L
270void
271after_check_relocs_default (void)
272{
273}
274
9b538ba7
L
275void
276before_place_orphans_default (void)
277{
278}
279
252b5132 280void
62b635b6 281after_allocation_default (void)
252b5132 282{
eaeb0a9d 283 lang_relax_sections (FALSE);
252b5132
RH
284}
285
286void
62b635b6 287before_allocation_default (void)
252b5132 288{
0e1862bb 289 if (!bfd_link_relocatable (&link_info))
8423293d 290 strip_excluded_output_sections ();
252b5132
RH
291}
292
5e797c2c 293void
1e035701
AM
294finish_default (void)
295{
0e1862bb 296 if (!bfd_link_relocatable (&link_info))
f13a99db 297 _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info);
1e035701
AM
298}
299
300void
62b635b6 301set_output_arch_default (void)
252b5132 302{
4de2d33d 303 /* Set the output architecture and machine if possible. */
f13a99db 304 bfd_set_arch_mach (link_info.output_bfd,
4de2d33d 305 ldfile_output_architecture, ldfile_output_machine);
252b5132
RH
306}
307
252b5132 308void
62b635b6 309syslib_default (char *ignore ATTRIBUTE_UNUSED)
252b5132 310{
c1c8c1ef 311 info_msg (_("%pS SYSLIB ignored\n"), NULL);
252b5132
RH
312}
313
252b5132 314void
62b635b6 315hll_default (char *ignore ATTRIBUTE_UNUSED)
252b5132 316{
c1c8c1ef 317 info_msg (_("%pS HLL ignored\n"), NULL);
252b5132
RH
318}
319
320ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
321
322void
62b635b6 323ldemul_choose_mode (char *target)
252b5132 324{
4de2d33d
KH
325 ld_emulation_xfer_type **eptr = ld_emulations;
326 /* Ignore "gld" prefix. */
327 if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd')
328 target += 3;
329 for (; *eptr; eptr++)
330 {
331 if (strcmp (target, (*eptr)->emulation_name) == 0)
332 {
333 ld_emulation = *eptr;
334 return;
335 }
336 }
337 einfo (_("%P: unrecognised emulation mode: %s\n"), target);
338 einfo (_("Supported emulations: "));
339 ldemul_list_emulations (stderr);
340 einfo ("%F\n");
252b5132
RH
341}
342
343void
62b635b6 344ldemul_list_emulations (FILE *f)
252b5132
RH
345{
346 ld_emulation_xfer_type **eptr = ld_emulations;
b34976b6 347 bfd_boolean first = TRUE;
252b5132
RH
348
349 for (; *eptr; eptr++)
350 {
351 if (first)
b34976b6 352 first = FALSE;
252b5132
RH
353 else
354 fprintf (f, " ");
355 fprintf (f, "%s", (*eptr)->emulation_name);
356 }
357}
358
359void
62b635b6 360ldemul_list_emulation_options (FILE *f)
252b5132 361{
4de2d33d 362 ld_emulation_xfer_type **eptr;
252b5132 363 int options_found = 0;
4de2d33d
KH
364
365 for (eptr = ld_emulations; *eptr; eptr++)
252b5132 366 {
4de2d33d
KH
367 ld_emulation_xfer_type *emul = *eptr;
368
252b5132
RH
369 if (emul->list_options)
370 {
371 fprintf (f, "%s: \n", emul->emulation_name);
4de2d33d 372
252b5132
RH
373 emul->list_options (f);
374
375 options_found = 1;
376 }
377 }
4de2d33d 378
0aa7f586 379 if (!options_found)
252b5132
RH
380 fprintf (f, _(" no emulation specific options.\n"));
381}
344a211f
NC
382
383int
62b635b6 384ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
344a211f
NC
385{
386 if (ld_emulation->find_potential_libraries)
387 return ld_emulation->find_potential_libraries (name, entry);
388
389 return 0;
390}
fac1652d
AM
391
392struct bfd_elf_version_expr *
62b635b6 393ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
fac1652d
AM
394{
395 if (ld_emulation->new_vers_pattern)
396 entry = (*ld_emulation->new_vers_pattern) (entry);
397 return entry;
398}
7a2f2d82
DD
399
400void
401ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
402{
403 if (ld_emulation->extra_map_file_text)
404 ld_emulation->extra_map_file_text (abfd, info, mapf);
405}
1ff6de03
NA
406
407int
408ldemul_emit_ctf_early (void)
409{
410 if (ld_emulation->emit_ctf_early)
411 return ld_emulation->emit_ctf_early ();
412 /* If the emulation doesn't know if it wants to emit CTF early, it is going
413 to do so. */
414 return 1;
415}
416
417void
3d16b64e
NA
418ldemul_acquire_strings_for_ctf (struct ctf_dict *ctf_output,
419 struct elf_strtab_hash *symstrtab)
420{
421 if (ld_emulation->acquire_strings_for_ctf)
422 ld_emulation->acquire_strings_for_ctf (ctf_output, symstrtab);
423}
1ff6de03 424
3d16b64e
NA
425void
426ldemul_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
427 struct elf_internal_sym *sym)
1ff6de03 428{
3d16b64e
NA
429 if (ld_emulation->new_dynsym_for_ctf)
430 ld_emulation->new_dynsym_for_ctf (ctf_output, symidx, sym);
1ff6de03 431}
3edf7b9f
DR
432
433bfd_boolean
434ldemul_print_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
435{
436 if (ld_emulation->print_symbol)
437 return ld_emulation->print_symbol (hash_entry, ptr);
438 return print_one_symbol (hash_entry, ptr);
439}