xref: /netbsd/external/gpl3/gdb/dist/ld/ldemul.c (revision 1424dfb3)
1 /* ldemul.c -- clearing house for ld emulation states
2    Copyright (C) 1991-2020 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
ldemul_hll(char * name)39 ldemul_hll (char *name)
40 {
41   ld_emulation->hll (name);
42 }
43 
44 void
ldemul_syslib(char * name)45 ldemul_syslib (char *name)
46 {
47   ld_emulation->syslib (name);
48 }
49 
50 void
ldemul_after_parse(void)51 ldemul_after_parse (void)
52 {
53   ld_emulation->after_parse ();
54 }
55 
56 void
ldemul_before_parse(void)57 ldemul_before_parse (void)
58 {
59   ld_emulation->before_parse ();
60 }
61 
62 void
ldemul_after_open(void)63 ldemul_after_open (void)
64 {
65   ld_emulation->after_open ();
66 }
67 
68 void
ldemul_after_check_relocs(void)69 ldemul_after_check_relocs (void)
70 {
71   ld_emulation->after_check_relocs ();
72 }
73 
74 void
ldemul_before_place_orphans(void)75 ldemul_before_place_orphans (void)
76 {
77   ld_emulation->before_place_orphans ();
78 }
79 
80 void
ldemul_after_allocation(void)81 ldemul_after_allocation (void)
82 {
83   ld_emulation->after_allocation ();
84 }
85 
86 void
ldemul_before_allocation(void)87 ldemul_before_allocation (void)
88 {
89   ld_emulation->before_allocation ();
90 }
91 
92 void
ldemul_set_output_arch(void)93 ldemul_set_output_arch (void)
94 {
95   ld_emulation->set_output_arch ();
96 }
97 
98 void
ldemul_finish(void)99 ldemul_finish (void)
100 {
101   ld_emulation->finish ();
102 }
103 
104 void
ldemul_set_symbols(void)105 ldemul_set_symbols (void)
106 {
107   if (ld_emulation->set_symbols)
108     ld_emulation->set_symbols ();
109 }
110 
111 void
ldemul_create_output_section_statements(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 *
ldemul_get_script(int * isfile)119 ldemul_get_script (int *isfile)
120 {
121   return ld_emulation->get_script (isfile);
122 }
123 
124 bfd_boolean
ldemul_open_dynamic_archive(const char * arch,search_dirs_type * search,lang_input_statement_type * entry)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 *
ldemul_place_orphan(asection * s,const char * name,int constraint)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
ldemul_add_options(int ns,char ** shortopts,int nl,struct option ** longopts,int nrl,struct option ** really_longopts)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
ldemul_handle_option(int optc)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
ldemul_parse_args(int argc,char ** argv)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
ldemul_unrecognized_file(lang_input_statement_type * entry)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
ldemul_recognized_file(lang_input_statement_type * entry)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 *
ldemul_choose_target(int argc,char ** argv)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 *
ldemul_default_target(int argc ATTRIBUTE_UNUSED,char ** argv ATTRIBUTE_UNUSED)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
after_parse_default(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 (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);
240 }
241 
242 void
after_open_default(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
after_check_relocs_default(void)271 after_check_relocs_default (void)
272 {
273 }
274 
275 void
before_place_orphans_default(void)276 before_place_orphans_default (void)
277 {
278 }
279 
280 void
after_allocation_default(void)281 after_allocation_default (void)
282 {
283   lang_relax_sections (FALSE);
284 }
285 
286 void
before_allocation_default(void)287 before_allocation_default (void)
288 {
289   if (!bfd_link_relocatable (&link_info))
290     strip_excluded_output_sections ();
291 }
292 
293 void
finish_default(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
set_output_arch_default(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   bfd_emul_set_maxpagesize (output_target, config.maxpagesize);
308   bfd_emul_set_commonpagesize (output_target, config.commonpagesize);
309 }
310 
311 void
syslib_default(char * ignore ATTRIBUTE_UNUSED)312 syslib_default (char *ignore ATTRIBUTE_UNUSED)
313 {
314   info_msg (_("%pS SYSLIB ignored\n"), NULL);
315 }
316 
317 void
hll_default(char * ignore ATTRIBUTE_UNUSED)318 hll_default (char *ignore ATTRIBUTE_UNUSED)
319 {
320   info_msg (_("%pS HLL ignored\n"), NULL);
321 }
322 
323 ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
324 
325 void
ldemul_choose_mode(char * target)326 ldemul_choose_mode (char *target)
327 {
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");
344 }
345 
346 void
ldemul_list_emulations(FILE * f)347 ldemul_list_emulations (FILE *f)
348 {
349   ld_emulation_xfer_type **eptr = ld_emulations;
350   bfd_boolean first = TRUE;
351 
352   for (; *eptr; eptr++)
353     {
354       if (first)
355 	first = FALSE;
356       else
357 	fprintf (f, " ");
358       fprintf (f, "%s", (*eptr)->emulation_name);
359     }
360 }
361 
362 void
ldemul_list_emulation_options(FILE * f)363 ldemul_list_emulation_options (FILE *f)
364 {
365   ld_emulation_xfer_type **eptr;
366   int options_found = 0;
367 
368   for (eptr = ld_emulations; *eptr; eptr++)
369     {
370       ld_emulation_xfer_type *emul = *eptr;
371 
372       if (emul->list_options)
373 	{
374 	  fprintf (f, "%s: \n", emul->emulation_name);
375 
376 	  emul->list_options (f);
377 
378 	  options_found = 1;
379 	}
380     }
381 
382   if (!options_found)
383     fprintf (f, _("  no emulation specific options.\n"));
384 }
385 
386 int
ldemul_find_potential_libraries(char * name,lang_input_statement_type * entry)387 ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
388 {
389   if (ld_emulation->find_potential_libraries)
390     return ld_emulation->find_potential_libraries (name, entry);
391 
392   return 0;
393 }
394 
395 struct bfd_elf_version_expr *
ldemul_new_vers_pattern(struct bfd_elf_version_expr * entry)396 ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
397 {
398   if (ld_emulation->new_vers_pattern)
399     entry = (*ld_emulation->new_vers_pattern) (entry);
400   return entry;
401 }
402 
403 void
ldemul_extra_map_file_text(bfd * abfd,struct bfd_link_info * info,FILE * mapf)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 }
409 
410 int
ldemul_emit_ctf_early(void)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
ldemul_examine_strtab_for_ctf(struct ctf_file * ctf_output,struct elf_sym_strtab * syms,bfd_size_type symcount,struct elf_strtab_hash * symstrtab)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 }
431 
432 bfd_boolean
ldemul_print_symbol(struct bfd_link_hash_entry * hash_entry,void * ptr)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 }
439