Automatic date update in version.in
[deliverable/binutils-gdb.git] / ld / ldemul.c
1 /* ldemul.c -- clearing house for ld emulation states
2 Copyright (C) 1991-2019 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_after_allocation (void)
76 {
77 ld_emulation->after_allocation ();
78 }
79
80 void
81 ldemul_before_allocation (void)
82 {
83 ld_emulation->before_allocation ();
84 }
85
86 void
87 ldemul_set_output_arch (void)
88 {
89 ld_emulation->set_output_arch ();
90 }
91
92 void
93 ldemul_finish (void)
94 {
95 ld_emulation->finish ();
96 }
97
98 void
99 ldemul_set_symbols (void)
100 {
101 if (ld_emulation->set_symbols)
102 ld_emulation->set_symbols ();
103 }
104
105 void
106 ldemul_create_output_section_statements (void)
107 {
108 if (ld_emulation->create_output_section_statements)
109 ld_emulation->create_output_section_statements ();
110 }
111
112 char *
113 ldemul_get_script (int *isfile)
114 {
115 return ld_emulation->get_script (isfile);
116 }
117
118 bfd_boolean
119 ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
120 lang_input_statement_type *entry)
121 {
122 if (ld_emulation->open_dynamic_archive)
123 return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
124 return FALSE;
125 }
126
127 lang_output_section_statement_type *
128 ldemul_place_orphan (asection *s, const char *name, int constraint)
129 {
130 if (ld_emulation->place_orphan)
131 return (*ld_emulation->place_orphan) (s, name, constraint);
132 return NULL;
133 }
134
135 void
136 ldemul_add_options (int ns, char **shortopts, int nl,
137 struct option **longopts, int nrl,
138 struct option **really_longopts)
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
146 ldemul_handle_option (int optc)
147 {
148 if (ld_emulation->handle_option)
149 return (*ld_emulation->handle_option) (optc);
150 return FALSE;
151 }
152
153 bfd_boolean
154 ldemul_parse_args (int argc, char **argv)
155 {
156 /* Try and use the emulation parser if there is one. */
157 if (ld_emulation->parse_args)
158 return (*ld_emulation->parse_args) (argc, argv);
159 return FALSE;
160 }
161
162 /* Let the emulation code handle an unrecognized file. */
163
164 bfd_boolean
165 ldemul_unrecognized_file (lang_input_statement_type *entry)
166 {
167 if (ld_emulation->unrecognized_file)
168 return (*ld_emulation->unrecognized_file) (entry);
169 return FALSE;
170 }
171
172 /* Let the emulation code handle a recognized file. */
173
174 bfd_boolean
175 ldemul_recognized_file (lang_input_statement_type *entry)
176 {
177 if (ld_emulation->recognized_file)
178 return (*ld_emulation->recognized_file) (entry);
179 return FALSE;
180 }
181
182 char *
183 ldemul_choose_target (int argc, char **argv)
184 {
185 return ld_emulation->choose_target (argc, argv);
186 }
187
188
189 /* The default choose_target function. */
190
191 char *
192 ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
193 {
194 char *from_outside = getenv (TARGET_ENVIRON);
195 if (from_outside != (char *) NULL)
196 return from_outside;
197 return ld_emulation->target_name;
198 }
199
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
211 void
212 after_parse_default (void)
213 {
214 if (entry_symbol.name != NULL
215 && (bfd_link_executable (&link_info) || entry_from_cmdline))
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)
227 ldlang_add_undef (entry_symbol.name, entry_from_cmdline);
228 }
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);
234 }
235
236 void
237 after_open_default (void)
238 {
239 }
240
241 void
242 after_check_relocs_default (void)
243 {
244 }
245
246 void
247 after_allocation_default (void)
248 {
249 lang_relax_sections (FALSE);
250 }
251
252 void
253 before_allocation_default (void)
254 {
255 if (!bfd_link_relocatable (&link_info))
256 strip_excluded_output_sections ();
257 }
258
259 void
260 finish_default (void)
261 {
262 if (!bfd_link_relocatable (&link_info))
263 _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info);
264 }
265
266 void
267 set_output_arch_default (void)
268 {
269 /* Set the output architecture and machine if possible. */
270 bfd_set_arch_mach (link_info.output_bfd,
271 ldfile_output_architecture, ldfile_output_machine);
272
273 bfd_emul_set_maxpagesize (output_target, config.maxpagesize);
274 bfd_emul_set_commonpagesize (output_target, config.commonpagesize);
275 }
276
277 void
278 syslib_default (char *ignore ATTRIBUTE_UNUSED)
279 {
280 info_msg (_("%pS SYSLIB ignored\n"), NULL);
281 }
282
283 void
284 hll_default (char *ignore ATTRIBUTE_UNUSED)
285 {
286 info_msg (_("%pS HLL ignored\n"), NULL);
287 }
288
289 ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
290
291 void
292 ldemul_choose_mode (char *target)
293 {
294 ld_emulation_xfer_type **eptr = ld_emulations;
295 /* Ignore "gld" prefix. */
296 if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd')
297 target += 3;
298 for (; *eptr; eptr++)
299 {
300 if (strcmp (target, (*eptr)->emulation_name) == 0)
301 {
302 ld_emulation = *eptr;
303 return;
304 }
305 }
306 einfo (_("%P: unrecognised emulation mode: %s\n"), target);
307 einfo (_("Supported emulations: "));
308 ldemul_list_emulations (stderr);
309 einfo ("%F\n");
310 }
311
312 void
313 ldemul_list_emulations (FILE *f)
314 {
315 ld_emulation_xfer_type **eptr = ld_emulations;
316 bfd_boolean first = TRUE;
317
318 for (; *eptr; eptr++)
319 {
320 if (first)
321 first = FALSE;
322 else
323 fprintf (f, " ");
324 fprintf (f, "%s", (*eptr)->emulation_name);
325 }
326 }
327
328 void
329 ldemul_list_emulation_options (FILE *f)
330 {
331 ld_emulation_xfer_type **eptr;
332 int options_found = 0;
333
334 for (eptr = ld_emulations; *eptr; eptr++)
335 {
336 ld_emulation_xfer_type *emul = *eptr;
337
338 if (emul->list_options)
339 {
340 fprintf (f, "%s: \n", emul->emulation_name);
341
342 emul->list_options (f);
343
344 options_found = 1;
345 }
346 }
347
348 if (!options_found)
349 fprintf (f, _(" no emulation specific options.\n"));
350 }
351
352 int
353 ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
354 {
355 if (ld_emulation->find_potential_libraries)
356 return ld_emulation->find_potential_libraries (name, entry);
357
358 return 0;
359 }
360
361 struct bfd_elf_version_expr *
362 ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
363 {
364 if (ld_emulation->new_vers_pattern)
365 entry = (*ld_emulation->new_vers_pattern) (entry);
366 return entry;
367 }
368
369 void
370 ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
371 {
372 if (ld_emulation->extra_map_file_text)
373 ld_emulation->extra_map_file_text (abfd, info, mapf);
374 }
375
376 int
377 ldemul_emit_ctf_early (void)
378 {
379 if (ld_emulation->emit_ctf_early)
380 return ld_emulation->emit_ctf_early ();
381 /* If the emulation doesn't know if it wants to emit CTF early, it is going
382 to do so. */
383 return 1;
384 }
385
386 void
387 ldemul_examine_strtab_for_ctf (struct ctf_file *ctf_output,
388 struct elf_sym_strtab *syms,
389 bfd_size_type symcount,
390 struct elf_strtab_hash *symstrtab)
391
392 {
393 if (ld_emulation->examine_strtab_for_ctf)
394 ld_emulation->examine_strtab_for_ctf (ctf_output, syms,
395 symcount, symstrtab);
396 }
This page took 0.044695 seconds and 4 git commands to generate.