]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldemul.c
bfd: new functions for getting strings out of a strtab
[thirdparty/binutils-gdb.git] / ld / ldemul.c
CommitLineData
252b5132 1/* ldemul.c -- clearing house for ld emulation states
82704155 2 Copyright (C) 1991-2019 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"
252b5132
RH
25
26#include "ld.h"
252b5132
RH
27#include "ldmisc.h"
28#include "ldexp.h"
29#include "ldlang.h"
30#include "ldfile.h"
b71e2778 31#include "ldemul.h"
252b5132
RH
32#include "ldmain.h"
33#include "ldemul-list.h"
34
279e75dc 35static ld_emulation_xfer_type *ld_emulation;
252b5132
RH
36
37void
62b635b6 38ldemul_hll (char *name)
252b5132 39{
4de2d33d 40 ld_emulation->hll (name);
252b5132
RH
41}
42
4de2d33d 43void
62b635b6 44ldemul_syslib (char *name)
252b5132 45{
4de2d33d 46 ld_emulation->syslib (name);
252b5132
RH
47}
48
49void
62b635b6 50ldemul_after_parse (void)
252b5132 51{
4de2d33d 52 ld_emulation->after_parse ();
252b5132
RH
53}
54
55void
62b635b6 56ldemul_before_parse (void)
252b5132 57{
4de2d33d 58 ld_emulation->before_parse ();
252b5132
RH
59}
60
61void
62b635b6 62ldemul_after_open (void)
252b5132
RH
63{
64 ld_emulation->after_open ();
65}
66
5c3261b0
L
67void
68ldemul_after_check_relocs (void)
69{
70 ld_emulation->after_check_relocs ();
71}
72
4de2d33d 73void
62b635b6 74ldemul_after_allocation (void)
252b5132 75{
4de2d33d 76 ld_emulation->after_allocation ();
252b5132
RH
77}
78
4de2d33d 79void
62b635b6 80ldemul_before_allocation (void)
252b5132 81{
8423293d 82 ld_emulation->before_allocation ();
252b5132
RH
83}
84
252b5132 85void
62b635b6 86ldemul_set_output_arch (void)
252b5132 87{
4de2d33d 88 ld_emulation->set_output_arch ();
252b5132
RH
89}
90
91void
62b635b6 92ldemul_finish (void)
252b5132 93{
1e035701 94 ld_emulation->finish ();
252b5132
RH
95}
96
97void
62b635b6 98ldemul_set_symbols (void)
252b5132
RH
99{
100 if (ld_emulation->set_symbols)
4de2d33d 101 ld_emulation->set_symbols ();
252b5132
RH
102}
103
104void
62b635b6 105ldemul_create_output_section_statements (void)
252b5132
RH
106{
107 if (ld_emulation->create_output_section_statements)
4de2d33d 108 ld_emulation->create_output_section_statements ();
252b5132
RH
109}
110
111char *
62b635b6 112ldemul_get_script (int *isfile)
252b5132 113{
4de2d33d 114 return ld_emulation->get_script (isfile);
252b5132
RH
115}
116
b34976b6 117bfd_boolean
62b635b6
KH
118ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
119 lang_input_statement_type *entry)
252b5132
RH
120{
121 if (ld_emulation->open_dynamic_archive)
122 return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
b34976b6 123 return FALSE;
252b5132
RH
124}
125
c2edb4b8 126lang_output_section_statement_type *
8a99a385 127ldemul_place_orphan (asection *s, const char *name, int constraint)
252b5132
RH
128{
129 if (ld_emulation->place_orphan)
8a99a385 130 return (*ld_emulation->place_orphan) (s, name, constraint);
c2edb4b8 131 return NULL;
252b5132
RH
132}
133
3bcf5557 134void
62b635b6
KH
135ldemul_add_options (int ns, char **shortopts, int nl,
136 struct option **longopts, int nrl,
137 struct option **really_longopts)
3bcf5557
AM
138{
139 if (ld_emulation->add_options)
140 (*ld_emulation->add_options) (ns, shortopts, nl, longopts,
141 nrl, really_longopts);
142}
143
144bfd_boolean
62b635b6 145ldemul_handle_option (int optc)
3bcf5557
AM
146{
147 if (ld_emulation->handle_option)
148 return (*ld_emulation->handle_option) (optc);
149 return FALSE;
150}
151
152bfd_boolean
62b635b6 153ldemul_parse_args (int argc, char **argv)
252b5132 154{
4de2d33d 155 /* Try and use the emulation parser if there is one. */
252b5132 156 if (ld_emulation->parse_args)
3bcf5557 157 return (*ld_emulation->parse_args) (argc, argv);
3aa97c58 158 return FALSE;
252b5132
RH
159}
160
161/* Let the emulation code handle an unrecognized file. */
162
b34976b6 163bfd_boolean
62b635b6 164ldemul_unrecognized_file (lang_input_statement_type *entry)
252b5132
RH
165{
166 if (ld_emulation->unrecognized_file)
167 return (*ld_emulation->unrecognized_file) (entry);
b34976b6 168 return FALSE;
252b5132
RH
169}
170
171/* Let the emulation code handle a recognized file. */
172
b34976b6 173bfd_boolean
62b635b6 174ldemul_recognized_file (lang_input_statement_type *entry)
252b5132
RH
175{
176 if (ld_emulation->recognized_file)
177 return (*ld_emulation->recognized_file) (entry);
b34976b6 178 return FALSE;
252b5132
RH
179}
180
181char *
62b635b6 182ldemul_choose_target (int argc, char **argv)
252b5132 183{
742aeb63 184 return ld_emulation->choose_target (argc, argv);
252b5132
RH
185}
186
742aeb63 187
252b5132
RH
188/* The default choose_target function. */
189
190char *
62b635b6 191ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
252b5132
RH
192{
193 char *from_outside = getenv (TARGET_ENVIRON);
4de2d33d 194 if (from_outside != (char *) NULL)
252b5132
RH
195 return from_outside;
196 return ld_emulation->target_name;
197}
198
97b11f40
AM
199/* If the entry point was not specified as an address, then add the
200 symbol as undefined. This will cause ld to extract an archive
201 element defining the entry if ld is linking against such an archive.
202
203 We don't do this when generating shared libraries unless given -e
204 on the command line, because most shared libs are not designed to
205 be run as an executable. However, some are, eg. glibc ld.so and
206 may rely on the default linker script supplying ENTRY. So we can't
207 remove the ENTRY from the script, but would rather not insert
208 undefined _start syms. */
209
4de2d33d 210void
62b635b6 211after_parse_default (void)
252b5132 212{
97b11f40 213 if (entry_symbol.name != NULL
0e1862bb 214 && (bfd_link_executable (&link_info) || entry_from_cmdline))
97b11f40
AM
215 {
216 bfd_boolean is_vma = FALSE;
217
218 if (entry_from_cmdline)
219 {
220 const char *send;
221
222 bfd_scan_vma (entry_symbol.name, &send, 0);
223 is_vma = *send == '\0';
224 }
225 if (!is_vma)
24898b70 226 ldlang_add_undef (entry_symbol.name, entry_from_cmdline);
97b11f40 227 }
702d1671
AM
228 if (config.maxpagesize == 0)
229 config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
230 if (config.commonpagesize == 0)
231 config.commonpagesize = bfd_emul_get_commonpagesize (default_target,
232 link_info.relro);
252b5132
RH
233}
234
235void
62b635b6 236after_open_default (void)
252b5132
RH
237{
238}
239
5c3261b0
L
240void
241after_check_relocs_default (void)
242{
243}
244
252b5132 245void
62b635b6 246after_allocation_default (void)
252b5132 247{
eaeb0a9d 248 lang_relax_sections (FALSE);
252b5132
RH
249}
250
251void
62b635b6 252before_allocation_default (void)
252b5132 253{
0e1862bb 254 if (!bfd_link_relocatable (&link_info))
8423293d 255 strip_excluded_output_sections ();
252b5132
RH
256}
257
5e797c2c 258void
1e035701
AM
259finish_default (void)
260{
0e1862bb 261 if (!bfd_link_relocatable (&link_info))
f13a99db 262 _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info);
1e035701
AM
263}
264
265void
62b635b6 266set_output_arch_default (void)
252b5132 267{
4de2d33d 268 /* Set the output architecture and machine if possible. */
f13a99db 269 bfd_set_arch_mach (link_info.output_bfd,
4de2d33d 270 ldfile_output_architecture, ldfile_output_machine);
8be573a7
AM
271
272 bfd_emul_set_maxpagesize (output_target, config.maxpagesize);
273 bfd_emul_set_commonpagesize (output_target, config.commonpagesize);
252b5132
RH
274}
275
252b5132 276void
62b635b6 277syslib_default (char *ignore ATTRIBUTE_UNUSED)
252b5132 278{
c1c8c1ef 279 info_msg (_("%pS SYSLIB ignored\n"), NULL);
252b5132
RH
280}
281
252b5132 282void
62b635b6 283hll_default (char *ignore ATTRIBUTE_UNUSED)
252b5132 284{
c1c8c1ef 285 info_msg (_("%pS HLL ignored\n"), NULL);
252b5132
RH
286}
287
288ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
289
290void
62b635b6 291ldemul_choose_mode (char *target)
252b5132 292{
4de2d33d
KH
293 ld_emulation_xfer_type **eptr = ld_emulations;
294 /* Ignore "gld" prefix. */
295 if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd')
296 target += 3;
297 for (; *eptr; eptr++)
298 {
299 if (strcmp (target, (*eptr)->emulation_name) == 0)
300 {
301 ld_emulation = *eptr;
302 return;
303 }
304 }
305 einfo (_("%P: unrecognised emulation mode: %s\n"), target);
306 einfo (_("Supported emulations: "));
307 ldemul_list_emulations (stderr);
308 einfo ("%F\n");
252b5132
RH
309}
310
311void
62b635b6 312ldemul_list_emulations (FILE *f)
252b5132
RH
313{
314 ld_emulation_xfer_type **eptr = ld_emulations;
b34976b6 315 bfd_boolean first = TRUE;
252b5132
RH
316
317 for (; *eptr; eptr++)
318 {
319 if (first)
b34976b6 320 first = FALSE;
252b5132
RH
321 else
322 fprintf (f, " ");
323 fprintf (f, "%s", (*eptr)->emulation_name);
324 }
325}
326
327void
62b635b6 328ldemul_list_emulation_options (FILE *f)
252b5132 329{
4de2d33d 330 ld_emulation_xfer_type **eptr;
252b5132 331 int options_found = 0;
4de2d33d
KH
332
333 for (eptr = ld_emulations; *eptr; eptr++)
252b5132 334 {
4de2d33d
KH
335 ld_emulation_xfer_type *emul = *eptr;
336
252b5132
RH
337 if (emul->list_options)
338 {
339 fprintf (f, "%s: \n", emul->emulation_name);
4de2d33d 340
252b5132
RH
341 emul->list_options (f);
342
343 options_found = 1;
344 }
345 }
4de2d33d 346
0aa7f586 347 if (!options_found)
252b5132
RH
348 fprintf (f, _(" no emulation specific options.\n"));
349}
344a211f
NC
350
351int
62b635b6 352ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
344a211f
NC
353{
354 if (ld_emulation->find_potential_libraries)
355 return ld_emulation->find_potential_libraries (name, entry);
356
357 return 0;
358}
fac1652d
AM
359
360struct bfd_elf_version_expr *
62b635b6 361ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
fac1652d
AM
362{
363 if (ld_emulation->new_vers_pattern)
364 entry = (*ld_emulation->new_vers_pattern) (entry);
365 return entry;
366}
7a2f2d82
DD
367
368void
369ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
370{
371 if (ld_emulation->extra_map_file_text)
372 ld_emulation->extra_map_file_text (abfd, info, mapf);
373}