]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldemul.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / ldemul.c
1 /* ldemul.c -- clearing house for ld emulation states
2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
3
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. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "getopt.h"
24 #include "bfdlink.h"
25 #include "ctf-api.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 static 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_check_relocs (void)
70 {
71 ld_emulation->after_check_relocs ();
72 }
73
74 void
75 ldemul_before_place_orphans (void)
76 {
77 ld_emulation->before_place_orphans ();
78 }
79
80 void
81 ldemul_after_allocation (void)
82 {
83 ld_emulation->after_allocation ();
84 }
85
86 void
87 ldemul_before_allocation (void)
88 {
89 ld_emulation->before_allocation ();
90 }
91
92 void
93 ldemul_set_output_arch (void)
94 {
95 ld_emulation->set_output_arch ();
96 }
97
98 void
99 ldemul_finish (void)
100 {
101 ld_emulation->finish ();
102 }
103
104 void
105 ldemul_set_symbols (void)
106 {
107 if (ld_emulation->set_symbols)
108 ld_emulation->set_symbols ();
109 }
110
111 void
112 ldemul_create_output_section_statements (void)
113 {
114 if (ld_emulation->create_output_section_statements)
115 ld_emulation->create_output_section_statements ();
116 }
117
118 char *
119 ldemul_get_script (int *isfile)
120 {
121 return ld_emulation->get_script (isfile);
122 }
123
124 bfd_boolean
125 ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
126 lang_input_statement_type *entry)
127 {
128 if (ld_emulation->open_dynamic_archive)
129 return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
130 return FALSE;
131 }
132
133 lang_output_section_statement_type *
134 ldemul_place_orphan (asection *s, const char *name, int constraint)
135 {
136 if (ld_emulation->place_orphan)
137 return (*ld_emulation->place_orphan) (s, name, constraint);
138 return NULL;
139 }
140
141 void
142 ldemul_add_options (int ns, char **shortopts, int nl,
143 struct option **longopts, int nrl,
144 struct option **really_longopts)
145 {
146 if (ld_emulation->add_options)
147 (*ld_emulation->add_options) (ns, shortopts, nl, longopts,
148 nrl, really_longopts);
149 }
150
151 bfd_boolean
152 ldemul_handle_option (int optc)
153 {
154 if (ld_emulation->handle_option)
155 return (*ld_emulation->handle_option) (optc);
156 return FALSE;
157 }
158
159 bfd_boolean
160 ldemul_parse_args (int argc, char **argv)
161 {
162 /* Try and use the emulation parser if there is one. */
163 if (ld_emulation->parse_args)
164 return (*ld_emulation->parse_args) (argc, argv);
165 return FALSE;
166 }
167
168 /* Let the emulation code handle an unrecognized file. */
169
170 bfd_boolean
171 ldemul_unrecognized_file (lang_input_statement_type *entry)
172 {
173 if (ld_emulation->unrecognized_file)
174 return (*ld_emulation->unrecognized_file) (entry);
175 return FALSE;
176 }
177
178 /* Let the emulation code handle a recognized file. */
179
180 bfd_boolean
181 ldemul_recognized_file (lang_input_statement_type *entry)
182 {
183 if (ld_emulation->recognized_file)
184 return (*ld_emulation->recognized_file) (entry);
185 return FALSE;
186 }
187
188 char *
189 ldemul_choose_target (int argc, char **argv)
190 {
191 return ld_emulation->choose_target (argc, argv);
192 }
193
194
195 /* The default choose_target function. */
196
197 char *
198 ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
199 {
200 char *from_outside = getenv (TARGET_ENVIRON);
201 if (from_outside != (char *) NULL)
202 return from_outside;
203 return ld_emulation->target_name;
204 }
205
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
217 void
218 after_parse_default (void)
219 {
220 if (entry_symbol.name != NULL
221 && (bfd_link_executable (&link_info) || entry_from_cmdline))
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)
233 ldlang_add_undef (entry_symbol.name, entry_from_cmdline);
234 }
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);
240 }
241
242 void
243 after_open_default (void)
244 {
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 }
268 }
269
270 void
271 after_check_relocs_default (void)
272 {
273 }
274
275 void
276 before_place_orphans_default (void)
277 {
278 }
279
280 void
281 after_allocation_default (void)
282 {
283 lang_relax_sections (FALSE);
284 }
285
286 void
287 before_allocation_default (void)
288 {
289 if (!bfd_link_relocatable (&link_info))
290 strip_excluded_output_sections ();
291 }
292
293 void
294 finish_default (void)
295 {
296 if (!bfd_link_relocatable (&link_info))
297 _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info);
298 }
299
300 void
301 set_output_arch_default (void)
302 {
303 /* Set the output architecture and machine if possible. */
304 bfd_set_arch_mach (link_info.output_bfd,
305 ldfile_output_architecture, ldfile_output_machine);
306 }
307
308 void
309 syslib_default (char *ignore ATTRIBUTE_UNUSED)
310 {
311 info_msg (_("%pS SYSLIB ignored\n"), NULL);
312 }
313
314 void
315 hll_default (char *ignore ATTRIBUTE_UNUSED)
316 {
317 info_msg (_("%pS HLL ignored\n"), NULL);
318 }
319
320 ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
321
322 void
323 ldemul_choose_mode (char *target)
324 {
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");
341 }
342
343 void
344 ldemul_list_emulations (FILE *f)
345 {
346 ld_emulation_xfer_type **eptr = ld_emulations;
347 bfd_boolean first = TRUE;
348
349 for (; *eptr; eptr++)
350 {
351 if (first)
352 first = FALSE;
353 else
354 fprintf (f, " ");
355 fprintf (f, "%s", (*eptr)->emulation_name);
356 }
357 }
358
359 void
360 ldemul_list_emulation_options (FILE *f)
361 {
362 ld_emulation_xfer_type **eptr;
363 int options_found = 0;
364
365 for (eptr = ld_emulations; *eptr; eptr++)
366 {
367 ld_emulation_xfer_type *emul = *eptr;
368
369 if (emul->list_options)
370 {
371 fprintf (f, "%s: \n", emul->emulation_name);
372
373 emul->list_options (f);
374
375 options_found = 1;
376 }
377 }
378
379 if (!options_found)
380 fprintf (f, _(" no emulation specific options.\n"));
381 }
382
383 int
384 ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
385 {
386 if (ld_emulation->find_potential_libraries)
387 return ld_emulation->find_potential_libraries (name, entry);
388
389 return 0;
390 }
391
392 struct bfd_elf_version_expr *
393 ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
394 {
395 if (ld_emulation->new_vers_pattern)
396 entry = (*ld_emulation->new_vers_pattern) (entry);
397 return entry;
398 }
399
400 void
401 ldemul_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 }
406
407 int
408 ldemul_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
417 void
418 ldemul_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 }
424
425 void
426 ldemul_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
427 struct elf_internal_sym *sym)
428 {
429 if (ld_emulation->new_dynsym_for_ctf)
430 ld_emulation->new_dynsym_for_ctf (ctf_output, symidx, sym);
431 }
432
433 bfd_boolean
434 ldemul_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 }