Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ldemul.c -- clearing house for ld emulation states |
b3adc24a | 2 | Copyright (C) 1991-2020 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 | ||
62 | void | |
62b635b6 | 63 | ldemul_after_open (void) |
252b5132 RH |
64 | { |
65 | ld_emulation->after_open (); | |
66 | } | |
67 | ||
5c3261b0 L |
68 | void |
69 | ldemul_after_check_relocs (void) | |
70 | { | |
71 | ld_emulation->after_check_relocs (); | |
72 | } | |
73 | ||
9b538ba7 L |
74 | void |
75 | ldemul_before_place_orphans (void) | |
76 | { | |
77 | ld_emulation->before_place_orphans (); | |
78 | } | |
79 | ||
4de2d33d | 80 | void |
62b635b6 | 81 | ldemul_after_allocation (void) |
252b5132 | 82 | { |
4de2d33d | 83 | ld_emulation->after_allocation (); |
252b5132 RH |
84 | } |
85 | ||
4de2d33d | 86 | void |
62b635b6 | 87 | ldemul_before_allocation (void) |
252b5132 | 88 | { |
8423293d | 89 | ld_emulation->before_allocation (); |
252b5132 RH |
90 | } |
91 | ||
252b5132 | 92 | void |
62b635b6 | 93 | ldemul_set_output_arch (void) |
252b5132 | 94 | { |
4de2d33d | 95 | ld_emulation->set_output_arch (); |
252b5132 RH |
96 | } |
97 | ||
98 | void | |
62b635b6 | 99 | ldemul_finish (void) |
252b5132 | 100 | { |
1e035701 | 101 | ld_emulation->finish (); |
252b5132 RH |
102 | } |
103 | ||
104 | void | |
62b635b6 | 105 | ldemul_set_symbols (void) |
252b5132 RH |
106 | { |
107 | if (ld_emulation->set_symbols) | |
4de2d33d | 108 | ld_emulation->set_symbols (); |
252b5132 RH |
109 | } |
110 | ||
111 | void | |
62b635b6 | 112 | ldemul_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 | ||
118 | char * | |
62b635b6 | 119 | ldemul_get_script (int *isfile) |
252b5132 | 120 | { |
4de2d33d | 121 | return ld_emulation->get_script (isfile); |
252b5132 RH |
122 | } |
123 | ||
b34976b6 | 124 | bfd_boolean |
62b635b6 KH |
125 | ldemul_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 | 133 | lang_output_section_statement_type * |
8a99a385 | 134 | ldemul_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 | 141 | void |
62b635b6 KH |
142 | ldemul_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 | ||
151 | bfd_boolean | |
62b635b6 | 152 | ldemul_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 | ||
159 | bfd_boolean | |
62b635b6 | 160 | ldemul_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 | 170 | bfd_boolean |
62b635b6 | 171 | ldemul_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 | 180 | bfd_boolean |
62b635b6 | 181 | ldemul_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 | ||
188 | char * | |
62b635b6 | 189 | ldemul_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 | ||
197 | char * | |
62b635b6 | 198 | ldemul_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 | 217 | void |
62b635b6 | 218 | after_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 | } |
702d1671 AM |
235 | if (config.maxpagesize == 0) |
236 | config.maxpagesize = bfd_emul_get_maxpagesize (default_target); | |
237 | if (config.commonpagesize == 0) | |
238 | config.commonpagesize = bfd_emul_get_commonpagesize (default_target, | |
239 | link_info.relro); | |
252b5132 RH |
240 | } |
241 | ||
242 | void | |
62b635b6 | 243 | after_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 |
270 | void |
271 | after_check_relocs_default (void) | |
272 | { | |
273 | } | |
274 | ||
9b538ba7 L |
275 | void |
276 | before_place_orphans_default (void) | |
277 | { | |
278 | } | |
279 | ||
252b5132 | 280 | void |
62b635b6 | 281 | after_allocation_default (void) |
252b5132 | 282 | { |
eaeb0a9d | 283 | lang_relax_sections (FALSE); |
252b5132 RH |
284 | } |
285 | ||
286 | void | |
62b635b6 | 287 | before_allocation_default (void) |
252b5132 | 288 | { |
0e1862bb | 289 | if (!bfd_link_relocatable (&link_info)) |
8423293d | 290 | strip_excluded_output_sections (); |
252b5132 RH |
291 | } |
292 | ||
5e797c2c | 293 | void |
1e035701 AM |
294 | finish_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 | ||
300 | void | |
62b635b6 | 301 | set_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); |
8be573a7 AM |
306 | |
307 | bfd_emul_set_maxpagesize (output_target, config.maxpagesize); | |
308 | bfd_emul_set_commonpagesize (output_target, config.commonpagesize); | |
252b5132 RH |
309 | } |
310 | ||
252b5132 | 311 | void |
62b635b6 | 312 | syslib_default (char *ignore ATTRIBUTE_UNUSED) |
252b5132 | 313 | { |
c1c8c1ef | 314 | info_msg (_("%pS SYSLIB ignored\n"), NULL); |
252b5132 RH |
315 | } |
316 | ||
252b5132 | 317 | void |
62b635b6 | 318 | hll_default (char *ignore ATTRIBUTE_UNUSED) |
252b5132 | 319 | { |
c1c8c1ef | 320 | info_msg (_("%pS HLL ignored\n"), NULL); |
252b5132 RH |
321 | } |
322 | ||
323 | ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST }; | |
324 | ||
325 | void | |
62b635b6 | 326 | ldemul_choose_mode (char *target) |
252b5132 | 327 | { |
4de2d33d KH |
328 | ld_emulation_xfer_type **eptr = ld_emulations; |
329 | /* Ignore "gld" prefix. */ | |
330 | if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd') | |
331 | target += 3; | |
332 | for (; *eptr; eptr++) | |
333 | { | |
334 | if (strcmp (target, (*eptr)->emulation_name) == 0) | |
335 | { | |
336 | ld_emulation = *eptr; | |
337 | return; | |
338 | } | |
339 | } | |
340 | einfo (_("%P: unrecognised emulation mode: %s\n"), target); | |
341 | einfo (_("Supported emulations: ")); | |
342 | ldemul_list_emulations (stderr); | |
343 | einfo ("%F\n"); | |
252b5132 RH |
344 | } |
345 | ||
346 | void | |
62b635b6 | 347 | ldemul_list_emulations (FILE *f) |
252b5132 RH |
348 | { |
349 | ld_emulation_xfer_type **eptr = ld_emulations; | |
b34976b6 | 350 | bfd_boolean first = TRUE; |
252b5132 RH |
351 | |
352 | for (; *eptr; eptr++) | |
353 | { | |
354 | if (first) | |
b34976b6 | 355 | first = FALSE; |
252b5132 RH |
356 | else |
357 | fprintf (f, " "); | |
358 | fprintf (f, "%s", (*eptr)->emulation_name); | |
359 | } | |
360 | } | |
361 | ||
362 | void | |
62b635b6 | 363 | ldemul_list_emulation_options (FILE *f) |
252b5132 | 364 | { |
4de2d33d | 365 | ld_emulation_xfer_type **eptr; |
252b5132 | 366 | int options_found = 0; |
4de2d33d KH |
367 | |
368 | for (eptr = ld_emulations; *eptr; eptr++) | |
252b5132 | 369 | { |
4de2d33d KH |
370 | ld_emulation_xfer_type *emul = *eptr; |
371 | ||
252b5132 RH |
372 | if (emul->list_options) |
373 | { | |
374 | fprintf (f, "%s: \n", emul->emulation_name); | |
4de2d33d | 375 | |
252b5132 RH |
376 | emul->list_options (f); |
377 | ||
378 | options_found = 1; | |
379 | } | |
380 | } | |
4de2d33d | 381 | |
0aa7f586 | 382 | if (!options_found) |
252b5132 RH |
383 | fprintf (f, _(" no emulation specific options.\n")); |
384 | } | |
344a211f NC |
385 | |
386 | int | |
62b635b6 | 387 | ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry) |
344a211f NC |
388 | { |
389 | if (ld_emulation->find_potential_libraries) | |
390 | return ld_emulation->find_potential_libraries (name, entry); | |
391 | ||
392 | return 0; | |
393 | } | |
fac1652d AM |
394 | |
395 | struct bfd_elf_version_expr * | |
62b635b6 | 396 | ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry) |
fac1652d AM |
397 | { |
398 | if (ld_emulation->new_vers_pattern) | |
399 | entry = (*ld_emulation->new_vers_pattern) (entry); | |
400 | return entry; | |
401 | } | |
7a2f2d82 DD |
402 | |
403 | void | |
404 | ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf) | |
405 | { | |
406 | if (ld_emulation->extra_map_file_text) | |
407 | ld_emulation->extra_map_file_text (abfd, info, mapf); | |
408 | } | |
1ff6de03 NA |
409 | |
410 | int | |
411 | ldemul_emit_ctf_early (void) | |
412 | { | |
413 | if (ld_emulation->emit_ctf_early) | |
414 | return ld_emulation->emit_ctf_early (); | |
415 | /* If the emulation doesn't know if it wants to emit CTF early, it is going | |
416 | to do so. */ | |
417 | return 1; | |
418 | } | |
419 | ||
420 | void | |
421 | ldemul_examine_strtab_for_ctf (struct ctf_file *ctf_output, | |
422 | struct elf_sym_strtab *syms, | |
423 | bfd_size_type symcount, | |
424 | struct elf_strtab_hash *symstrtab) | |
425 | ||
426 | { | |
427 | if (ld_emulation->examine_strtab_for_ctf) | |
428 | ld_emulation->examine_strtab_for_ctf (ctf_output, syms, | |
429 | symcount, symstrtab); | |
430 | } | |
3edf7b9f DR |
431 | |
432 | bfd_boolean | |
433 | ldemul_print_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) | |
434 | { | |
435 | if (ld_emulation->print_symbol) | |
436 | return ld_emulation->print_symbol (hash_entry, ptr); | |
437 | return print_one_symbol (hash_entry, ptr); | |
438 | } |