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