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