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 | ||
4de2d33d | 74 | void |
62b635b6 | 75 | ldemul_after_allocation (void) |
252b5132 | 76 | { |
4de2d33d | 77 | ld_emulation->after_allocation (); |
252b5132 RH |
78 | } |
79 | ||
4de2d33d | 80 | void |
62b635b6 | 81 | ldemul_before_allocation (void) |
252b5132 | 82 | { |
8423293d | 83 | ld_emulation->before_allocation (); |
252b5132 RH |
84 | } |
85 | ||
252b5132 | 86 | void |
62b635b6 | 87 | ldemul_set_output_arch (void) |
252b5132 | 88 | { |
4de2d33d | 89 | ld_emulation->set_output_arch (); |
252b5132 RH |
90 | } |
91 | ||
92 | void | |
62b635b6 | 93 | ldemul_finish (void) |
252b5132 | 94 | { |
1e035701 | 95 | ld_emulation->finish (); |
252b5132 RH |
96 | } |
97 | ||
98 | void | |
62b635b6 | 99 | ldemul_set_symbols (void) |
252b5132 RH |
100 | { |
101 | if (ld_emulation->set_symbols) | |
4de2d33d | 102 | ld_emulation->set_symbols (); |
252b5132 RH |
103 | } |
104 | ||
105 | void | |
62b635b6 | 106 | ldemul_create_output_section_statements (void) |
252b5132 RH |
107 | { |
108 | if (ld_emulation->create_output_section_statements) | |
4de2d33d | 109 | ld_emulation->create_output_section_statements (); |
252b5132 RH |
110 | } |
111 | ||
112 | char * | |
62b635b6 | 113 | ldemul_get_script (int *isfile) |
252b5132 | 114 | { |
4de2d33d | 115 | return ld_emulation->get_script (isfile); |
252b5132 RH |
116 | } |
117 | ||
b34976b6 | 118 | bfd_boolean |
62b635b6 KH |
119 | ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search, |
120 | lang_input_statement_type *entry) | |
252b5132 RH |
121 | { |
122 | if (ld_emulation->open_dynamic_archive) | |
123 | return (*ld_emulation->open_dynamic_archive) (arch, search, entry); | |
b34976b6 | 124 | return FALSE; |
252b5132 RH |
125 | } |
126 | ||
c2edb4b8 | 127 | lang_output_section_statement_type * |
8a99a385 | 128 | ldemul_place_orphan (asection *s, const char *name, int constraint) |
252b5132 RH |
129 | { |
130 | if (ld_emulation->place_orphan) | |
8a99a385 | 131 | return (*ld_emulation->place_orphan) (s, name, constraint); |
c2edb4b8 | 132 | return NULL; |
252b5132 RH |
133 | } |
134 | ||
3bcf5557 | 135 | void |
62b635b6 KH |
136 | ldemul_add_options (int ns, char **shortopts, int nl, |
137 | struct option **longopts, int nrl, | |
138 | struct option **really_longopts) | |
3bcf5557 AM |
139 | { |
140 | if (ld_emulation->add_options) | |
141 | (*ld_emulation->add_options) (ns, shortopts, nl, longopts, | |
142 | nrl, really_longopts); | |
143 | } | |
144 | ||
145 | bfd_boolean | |
62b635b6 | 146 | ldemul_handle_option (int optc) |
3bcf5557 AM |
147 | { |
148 | if (ld_emulation->handle_option) | |
149 | return (*ld_emulation->handle_option) (optc); | |
150 | return FALSE; | |
151 | } | |
152 | ||
153 | bfd_boolean | |
62b635b6 | 154 | ldemul_parse_args (int argc, char **argv) |
252b5132 | 155 | { |
4de2d33d | 156 | /* Try and use the emulation parser if there is one. */ |
252b5132 | 157 | if (ld_emulation->parse_args) |
3bcf5557 | 158 | return (*ld_emulation->parse_args) (argc, argv); |
3aa97c58 | 159 | return FALSE; |
252b5132 RH |
160 | } |
161 | ||
162 | /* Let the emulation code handle an unrecognized file. */ | |
163 | ||
b34976b6 | 164 | bfd_boolean |
62b635b6 | 165 | ldemul_unrecognized_file (lang_input_statement_type *entry) |
252b5132 RH |
166 | { |
167 | if (ld_emulation->unrecognized_file) | |
168 | return (*ld_emulation->unrecognized_file) (entry); | |
b34976b6 | 169 | return FALSE; |
252b5132 RH |
170 | } |
171 | ||
172 | /* Let the emulation code handle a recognized file. */ | |
173 | ||
b34976b6 | 174 | bfd_boolean |
62b635b6 | 175 | ldemul_recognized_file (lang_input_statement_type *entry) |
252b5132 RH |
176 | { |
177 | if (ld_emulation->recognized_file) | |
178 | return (*ld_emulation->recognized_file) (entry); | |
b34976b6 | 179 | return FALSE; |
252b5132 RH |
180 | } |
181 | ||
182 | char * | |
62b635b6 | 183 | ldemul_choose_target (int argc, char **argv) |
252b5132 | 184 | { |
742aeb63 | 185 | return ld_emulation->choose_target (argc, argv); |
252b5132 RH |
186 | } |
187 | ||
742aeb63 | 188 | |
252b5132 RH |
189 | /* The default choose_target function. */ |
190 | ||
191 | char * | |
62b635b6 | 192 | ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
252b5132 RH |
193 | { |
194 | char *from_outside = getenv (TARGET_ENVIRON); | |
4de2d33d | 195 | if (from_outside != (char *) NULL) |
252b5132 RH |
196 | return from_outside; |
197 | return ld_emulation->target_name; | |
198 | } | |
199 | ||
97b11f40 AM |
200 | /* If the entry point was not specified as an address, then add the |
201 | symbol as undefined. This will cause ld to extract an archive | |
202 | element defining the entry if ld is linking against such an archive. | |
203 | ||
204 | We don't do this when generating shared libraries unless given -e | |
205 | on the command line, because most shared libs are not designed to | |
206 | be run as an executable. However, some are, eg. glibc ld.so and | |
207 | may rely on the default linker script supplying ENTRY. So we can't | |
208 | remove the ENTRY from the script, but would rather not insert | |
209 | undefined _start syms. */ | |
210 | ||
4de2d33d | 211 | void |
62b635b6 | 212 | after_parse_default (void) |
252b5132 | 213 | { |
97b11f40 | 214 | if (entry_symbol.name != NULL |
0e1862bb | 215 | && (bfd_link_executable (&link_info) || entry_from_cmdline)) |
97b11f40 AM |
216 | { |
217 | bfd_boolean is_vma = FALSE; | |
218 | ||
219 | if (entry_from_cmdline) | |
220 | { | |
221 | const char *send; | |
222 | ||
223 | bfd_scan_vma (entry_symbol.name, &send, 0); | |
224 | is_vma = *send == '\0'; | |
225 | } | |
226 | if (!is_vma) | |
24898b70 | 227 | ldlang_add_undef (entry_symbol.name, entry_from_cmdline); |
97b11f40 | 228 | } |
702d1671 AM |
229 | if (config.maxpagesize == 0) |
230 | config.maxpagesize = bfd_emul_get_maxpagesize (default_target); | |
231 | if (config.commonpagesize == 0) | |
232 | config.commonpagesize = bfd_emul_get_commonpagesize (default_target, | |
233 | link_info.relro); | |
252b5132 RH |
234 | } |
235 | ||
236 | void | |
62b635b6 | 237 | after_open_default (void) |
252b5132 | 238 | { |
22216541 AM |
239 | link_info.big_endian = TRUE; |
240 | ||
241 | if (bfd_big_endian (link_info.output_bfd)) | |
242 | ; | |
243 | else if (bfd_little_endian (link_info.output_bfd)) | |
244 | link_info.big_endian = FALSE; | |
245 | else | |
246 | { | |
247 | if (command_line.endian == ENDIAN_BIG) | |
248 | ; | |
249 | else if (command_line.endian == ENDIAN_LITTLE) | |
250 | link_info.big_endian = FALSE; | |
251 | else if (command_line.endian == ENDIAN_UNSET) | |
252 | { | |
253 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
254 | if (s->the_bfd != NULL) | |
255 | { | |
256 | if (bfd_little_endian (s->the_bfd)) | |
257 | link_info.big_endian = FALSE; | |
258 | break; | |
259 | } | |
260 | } | |
261 | } | |
252b5132 RH |
262 | } |
263 | ||
5c3261b0 L |
264 | void |
265 | after_check_relocs_default (void) | |
266 | { | |
267 | } | |
268 | ||
252b5132 | 269 | void |
62b635b6 | 270 | after_allocation_default (void) |
252b5132 | 271 | { |
eaeb0a9d | 272 | lang_relax_sections (FALSE); |
252b5132 RH |
273 | } |
274 | ||
275 | void | |
62b635b6 | 276 | before_allocation_default (void) |
252b5132 | 277 | { |
0e1862bb | 278 | if (!bfd_link_relocatable (&link_info)) |
8423293d | 279 | strip_excluded_output_sections (); |
252b5132 RH |
280 | } |
281 | ||
5e797c2c | 282 | void |
1e035701 AM |
283 | finish_default (void) |
284 | { | |
0e1862bb | 285 | if (!bfd_link_relocatable (&link_info)) |
f13a99db | 286 | _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info); |
1e035701 AM |
287 | } |
288 | ||
289 | void | |
62b635b6 | 290 | set_output_arch_default (void) |
252b5132 | 291 | { |
4de2d33d | 292 | /* Set the output architecture and machine if possible. */ |
f13a99db | 293 | bfd_set_arch_mach (link_info.output_bfd, |
4de2d33d | 294 | ldfile_output_architecture, ldfile_output_machine); |
8be573a7 AM |
295 | |
296 | bfd_emul_set_maxpagesize (output_target, config.maxpagesize); | |
297 | bfd_emul_set_commonpagesize (output_target, config.commonpagesize); | |
252b5132 RH |
298 | } |
299 | ||
252b5132 | 300 | void |
62b635b6 | 301 | syslib_default (char *ignore ATTRIBUTE_UNUSED) |
252b5132 | 302 | { |
c1c8c1ef | 303 | info_msg (_("%pS SYSLIB ignored\n"), NULL); |
252b5132 RH |
304 | } |
305 | ||
252b5132 | 306 | void |
62b635b6 | 307 | hll_default (char *ignore ATTRIBUTE_UNUSED) |
252b5132 | 308 | { |
c1c8c1ef | 309 | info_msg (_("%pS HLL ignored\n"), NULL); |
252b5132 RH |
310 | } |
311 | ||
312 | ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST }; | |
313 | ||
314 | void | |
62b635b6 | 315 | ldemul_choose_mode (char *target) |
252b5132 | 316 | { |
4de2d33d KH |
317 | ld_emulation_xfer_type **eptr = ld_emulations; |
318 | /* Ignore "gld" prefix. */ | |
319 | if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd') | |
320 | target += 3; | |
321 | for (; *eptr; eptr++) | |
322 | { | |
323 | if (strcmp (target, (*eptr)->emulation_name) == 0) | |
324 | { | |
325 | ld_emulation = *eptr; | |
326 | return; | |
327 | } | |
328 | } | |
329 | einfo (_("%P: unrecognised emulation mode: %s\n"), target); | |
330 | einfo (_("Supported emulations: ")); | |
331 | ldemul_list_emulations (stderr); | |
332 | einfo ("%F\n"); | |
252b5132 RH |
333 | } |
334 | ||
335 | void | |
62b635b6 | 336 | ldemul_list_emulations (FILE *f) |
252b5132 RH |
337 | { |
338 | ld_emulation_xfer_type **eptr = ld_emulations; | |
b34976b6 | 339 | bfd_boolean first = TRUE; |
252b5132 RH |
340 | |
341 | for (; *eptr; eptr++) | |
342 | { | |
343 | if (first) | |
b34976b6 | 344 | first = FALSE; |
252b5132 RH |
345 | else |
346 | fprintf (f, " "); | |
347 | fprintf (f, "%s", (*eptr)->emulation_name); | |
348 | } | |
349 | } | |
350 | ||
351 | void | |
62b635b6 | 352 | ldemul_list_emulation_options (FILE *f) |
252b5132 | 353 | { |
4de2d33d | 354 | ld_emulation_xfer_type **eptr; |
252b5132 | 355 | int options_found = 0; |
4de2d33d KH |
356 | |
357 | for (eptr = ld_emulations; *eptr; eptr++) | |
252b5132 | 358 | { |
4de2d33d KH |
359 | ld_emulation_xfer_type *emul = *eptr; |
360 | ||
252b5132 RH |
361 | if (emul->list_options) |
362 | { | |
363 | fprintf (f, "%s: \n", emul->emulation_name); | |
4de2d33d | 364 | |
252b5132 RH |
365 | emul->list_options (f); |
366 | ||
367 | options_found = 1; | |
368 | } | |
369 | } | |
4de2d33d | 370 | |
0aa7f586 | 371 | if (!options_found) |
252b5132 RH |
372 | fprintf (f, _(" no emulation specific options.\n")); |
373 | } | |
344a211f NC |
374 | |
375 | int | |
62b635b6 | 376 | ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry) |
344a211f NC |
377 | { |
378 | if (ld_emulation->find_potential_libraries) | |
379 | return ld_emulation->find_potential_libraries (name, entry); | |
380 | ||
381 | return 0; | |
382 | } | |
fac1652d AM |
383 | |
384 | struct bfd_elf_version_expr * | |
62b635b6 | 385 | ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry) |
fac1652d AM |
386 | { |
387 | if (ld_emulation->new_vers_pattern) | |
388 | entry = (*ld_emulation->new_vers_pattern) (entry); | |
389 | return entry; | |
390 | } | |
7a2f2d82 DD |
391 | |
392 | void | |
393 | ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf) | |
394 | { | |
395 | if (ld_emulation->extra_map_file_text) | |
396 | ld_emulation->extra_map_file_text (abfd, info, mapf); | |
397 | } | |
1ff6de03 NA |
398 | |
399 | int | |
400 | ldemul_emit_ctf_early (void) | |
401 | { | |
402 | if (ld_emulation->emit_ctf_early) | |
403 | return ld_emulation->emit_ctf_early (); | |
404 | /* If the emulation doesn't know if it wants to emit CTF early, it is going | |
405 | to do so. */ | |
406 | return 1; | |
407 | } | |
408 | ||
409 | void | |
410 | ldemul_examine_strtab_for_ctf (struct ctf_file *ctf_output, | |
411 | struct elf_sym_strtab *syms, | |
412 | bfd_size_type symcount, | |
413 | struct elf_strtab_hash *symstrtab) | |
414 | ||
415 | { | |
416 | if (ld_emulation->examine_strtab_for_ctf) | |
417 | ld_emulation->examine_strtab_for_ctf (ctf_output, syms, | |
418 | symcount, symstrtab); | |
419 | } |