Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Main program of GNU linker. |
4e5bae56 | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
cdc0d45e | 3 | 2002, 2003 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Steve Chamberlain steve@cygnus.com | |
6 | ||
2fa9fc65 | 7 | This file is part of GLD, the Gnu Linker. |
252b5132 | 8 | |
2fa9fc65 NC |
9 | GLD is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
252b5132 | 13 | |
2fa9fc65 NC |
14 | GLD is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
252b5132 | 18 | |
2fa9fc65 NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with GLD; see the file COPYING. If not, write to the Free | |
21 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
22 | 02111-1307, USA. */ | |
252b5132 RH |
23 | |
24 | #include "bfd.h" | |
25 | #include "sysdep.h" | |
26 | #include <stdio.h> | |
3882b010 | 27 | #include "safe-ctype.h" |
252b5132 RH |
28 | #include "libiberty.h" |
29 | #include "progress.h" | |
30 | #include "bfdlink.h" | |
5af11cab | 31 | #include "filenames.h" |
252b5132 RH |
32 | |
33 | #include "ld.h" | |
34 | #include "ldmain.h" | |
35 | #include "ldmisc.h" | |
36 | #include "ldwrite.h" | |
252b5132 RH |
37 | #include "ldexp.h" |
38 | #include "ldlang.h" | |
df2a7313 | 39 | #include <ldgram.h> |
252b5132 RH |
40 | #include "ldlex.h" |
41 | #include "ldfile.h" | |
b71e2778 | 42 | #include "ldemul.h" |
252b5132 RH |
43 | #include "ldctor.h" |
44 | ||
0125cdf2 | 45 | /* Somewhere above, sys/stat.h got included. */ |
252b5132 RH |
46 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
47 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
48 | #endif | |
49 | ||
50 | #include <string.h> | |
51 | ||
52 | #ifdef HAVE_SBRK | |
53 | #ifdef NEED_DECLARATION_SBRK | |
54 | extern PTR sbrk (); | |
55 | #endif | |
56 | #endif | |
57 | ||
9c8ebd6a DJ |
58 | #ifndef TARGET_SYSTEM_ROOT |
59 | #define TARGET_SYSTEM_ROOT "" | |
60 | #endif | |
61 | ||
4e5bae56 DJ |
62 | int main PARAMS ((int, char **)); |
63 | ||
252b5132 RH |
64 | /* EXPORTS */ |
65 | ||
66 | char *default_target; | |
67 | const char *output_filename = "a.out"; | |
68 | ||
69 | /* Name this program was invoked by. */ | |
70 | char *program_name; | |
71 | ||
9c8ebd6a DJ |
72 | /* The prefix for system library directories. */ |
73 | char *ld_sysroot; | |
74 | ||
3fe38064 NC |
75 | /* The canonical representation of ld_sysroot. */ |
76 | char * ld_canon_sysroot; | |
77 | int ld_canon_sysroot_len; | |
78 | ||
6d5e62f8 | 79 | /* The file that we're creating. */ |
252b5132 RH |
80 | bfd *output_bfd = 0; |
81 | ||
82 | /* Set by -G argument, for MIPS ECOFF target. */ | |
83 | int g_switch_value = 8; | |
84 | ||
85 | /* Nonzero means print names of input files as processed. */ | |
b34976b6 | 86 | bfd_boolean trace_files; |
252b5132 RH |
87 | |
88 | /* Nonzero means same, but note open failures, too. */ | |
b34976b6 | 89 | bfd_boolean trace_file_tries; |
252b5132 RH |
90 | |
91 | /* Nonzero means version number was printed, so exit successfully | |
92 | instead of complaining if no input files are given. */ | |
b34976b6 | 93 | bfd_boolean version_printed; |
252b5132 RH |
94 | |
95 | /* Nonzero means link in every member of an archive. */ | |
b34976b6 | 96 | bfd_boolean whole_archive; |
252b5132 | 97 | |
b34976b6 AM |
98 | /* TRUE if we should demangle symbol names. */ |
99 | bfd_boolean demangling; | |
252b5132 RH |
100 | |
101 | args_type command_line; | |
102 | ||
103 | ld_config_type config; | |
104 | ||
0125cdf2 NC |
105 | static char *get_emulation |
106 | PARAMS ((int, char **)); | |
107 | static void set_scripts_dir | |
108 | PARAMS ((void)); | |
109 | static void remove_output | |
110 | PARAMS ((void)); | |
111 | static bfd_boolean check_for_scripts_dir | |
112 | PARAMS ((char *)); | |
113 | static bfd_boolean add_archive_element | |
114 | PARAMS ((struct bfd_link_info *, bfd *, const char *)); | |
115 | static bfd_boolean multiple_definition | |
116 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, | |
117 | bfd *, asection *, bfd_vma)); | |
118 | static bfd_boolean multiple_common | |
119 | PARAMS ((struct bfd_link_info *, const char *, bfd *, | |
120 | enum bfd_link_hash_type, bfd_vma, bfd *, enum bfd_link_hash_type, | |
121 | bfd_vma)); | |
122 | static bfd_boolean add_to_set | |
123 | PARAMS ((struct bfd_link_info *, struct bfd_link_hash_entry *, | |
124 | bfd_reloc_code_real_type, bfd *, asection *, bfd_vma)); | |
125 | static bfd_boolean constructor_callback | |
126 | PARAMS ((struct bfd_link_info *, bfd_boolean, const char *, bfd *, | |
127 | asection *, bfd_vma)); | |
128 | static bfd_boolean warning_callback | |
129 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *, | |
130 | asection *, bfd_vma)); | |
131 | static void warning_find_reloc | |
132 | PARAMS ((bfd *, asection *, PTR)); | |
133 | static bfd_boolean undefined_symbol | |
134 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, | |
135 | bfd_boolean)); | |
136 | static bfd_boolean reloc_overflow | |
137 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma, | |
138 | bfd *, asection *, bfd_vma)); | |
139 | static bfd_boolean reloc_dangerous | |
140 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
141 | static bfd_boolean unattached_reloc | |
142 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
143 | static bfd_boolean notice | |
144 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
145 | ||
146 | static struct bfd_link_callbacks link_callbacks = | |
147 | { | |
252b5132 RH |
148 | add_archive_element, |
149 | multiple_definition, | |
150 | multiple_common, | |
151 | add_to_set, | |
152 | constructor_callback, | |
153 | warning_callback, | |
154 | undefined_symbol, | |
155 | reloc_overflow, | |
156 | reloc_dangerous, | |
157 | unattached_reloc, | |
0f0569c4 L |
158 | notice, |
159 | error_handler | |
252b5132 RH |
160 | }; |
161 | ||
162 | struct bfd_link_info link_info; | |
163 | \f | |
164 | static void | |
165 | remove_output () | |
166 | { | |
6d5e62f8 | 167 | if (output_filename) |
252b5132 RH |
168 | { |
169 | if (output_bfd && output_bfd->iostream) | |
6d5e62f8 | 170 | fclose ((FILE *) (output_bfd->iostream)); |
252b5132 RH |
171 | if (delete_output_file_on_failure) |
172 | unlink (output_filename); | |
173 | } | |
174 | } | |
175 | ||
176 | int | |
177 | main (argc, argv) | |
178 | int argc; | |
179 | char **argv; | |
180 | { | |
181 | char *emulation; | |
182 | long start_time = get_run_time (); | |
183 | ||
184 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
185 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
186 | #endif |
187 | #if defined (HAVE_SETLOCALE) | |
188 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
189 | #endif |
190 | bindtextdomain (PACKAGE, LOCALEDIR); | |
191 | textdomain (PACKAGE); | |
192 | ||
193 | program_name = argv[0]; | |
194 | xmalloc_set_program_name (program_name); | |
195 | ||
196 | START_PROGRESS (program_name, 0); | |
197 | ||
198 | bfd_init (); | |
199 | ||
200 | bfd_set_error_program_name (program_name); | |
201 | ||
202 | xatexit (remove_output); | |
203 | ||
9c8ebd6a DJ |
204 | #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE |
205 | ld_sysroot = make_relative_prefix (program_name, BINDIR, | |
206 | TARGET_SYSTEM_ROOT); | |
0125cdf2 | 207 | |
9c8ebd6a DJ |
208 | if (ld_sysroot) |
209 | { | |
210 | struct stat s; | |
211 | int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); | |
0125cdf2 | 212 | |
9c8ebd6a DJ |
213 | if (!res) |
214 | { | |
215 | free (ld_sysroot); | |
0125cdf2 | 216 | ld_sysroot = NULL; |
9c8ebd6a DJ |
217 | } |
218 | } | |
0125cdf2 NC |
219 | |
220 | if (! ld_sysroot) | |
221 | { | |
222 | ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR, | |
223 | TARGET_SYSTEM_ROOT); | |
224 | ||
225 | if (ld_sysroot) | |
226 | { | |
227 | struct stat s; | |
228 | int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); | |
229 | ||
230 | if (!res) | |
231 | { | |
232 | free (ld_sysroot); | |
233 | ld_sysroot = NULL; | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | if (! ld_sysroot) | |
9c8ebd6a | 239 | #endif |
0125cdf2 | 240 | ld_sysroot = TARGET_SYSTEM_ROOT; |
9c8ebd6a | 241 | |
3fe38064 NC |
242 | if (ld_sysroot && *ld_sysroot) |
243 | ld_canon_sysroot = lrealpath (ld_sysroot); | |
244 | ||
245 | if (ld_canon_sysroot) | |
246 | ld_canon_sysroot_len = strlen (ld_canon_sysroot); | |
247 | else | |
248 | ld_canon_sysroot_len = -1; | |
249 | ||
252b5132 RH |
250 | /* Set the default BFD target based on the configured target. Doing |
251 | this permits the linker to be configured for a particular target, | |
252 | and linked against a shared BFD library which was configured for | |
253 | a different target. The macro TARGET is defined by Makefile. */ | |
254 | if (! bfd_set_default_target (TARGET)) | |
255 | { | |
256 | einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); | |
257 | xexit (1); | |
258 | } | |
259 | ||
b47c4208 AM |
260 | #if YYDEBUG |
261 | { | |
262 | extern int yydebug; | |
263 | yydebug = 1; | |
264 | } | |
265 | #endif | |
266 | ||
252b5132 | 267 | /* Initialize the data about options. */ |
b34976b6 AM |
268 | trace_files = trace_file_tries = version_printed = FALSE; |
269 | whole_archive = FALSE; | |
270 | config.build_constructors = TRUE; | |
271 | config.dynamic_link = FALSE; | |
272 | config.has_shared = FALSE; | |
a854a4a7 AM |
273 | config.split_by_reloc = (unsigned) -1; |
274 | config.split_by_file = (bfd_size_type) -1; | |
b34976b6 AM |
275 | command_line.force_common_definition = FALSE; |
276 | command_line.inhibit_common_definition = FALSE; | |
252b5132 RH |
277 | command_line.interpreter = NULL; |
278 | command_line.rpath = NULL; | |
b34976b6 AM |
279 | command_line.warn_mismatch = TRUE; |
280 | command_line.check_section_addresses = TRUE; | |
312b768e | 281 | command_line.accept_unknown_input_arch = FALSE; |
252b5132 RH |
282 | |
283 | /* We initialize DEMANGLING based on the environment variable | |
284 | COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the | |
285 | output of the linker, unless COLLECT_NO_DEMANGLE is set in the | |
286 | environment. Acting the same way here lets us provide the same | |
287 | interface by default. */ | |
288 | demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; | |
289 | ||
b34976b6 AM |
290 | link_info.relocateable = FALSE; |
291 | link_info.emitrelocations = FALSE; | |
b5067602 | 292 | link_info.task_link = FALSE; |
b34976b6 AM |
293 | link_info.shared = FALSE; |
294 | link_info.symbolic = FALSE; | |
295 | link_info.export_dynamic = FALSE; | |
296 | link_info.static_link = FALSE; | |
297 | link_info.traditional_format = FALSE; | |
298 | link_info.optimize = FALSE; | |
299 | link_info.no_undefined = FALSE; | |
ae9a127f | 300 | link_info.allow_shlib_undefined = TRUE; |
b34976b6 AM |
301 | link_info.allow_multiple_definition = FALSE; |
302 | link_info.allow_undefined_version = TRUE; | |
b5067602 AM |
303 | link_info.keep_memory = TRUE; |
304 | link_info.notice_all = FALSE; | |
305 | link_info.nocopyreloc = FALSE; | |
306 | link_info.new_dtags = FALSE; | |
307 | link_info.combreloc = TRUE; | |
308 | link_info.eh_frame_hdr = FALSE; | |
d5cd3933 | 309 | link_info.strip_discarded = TRUE; |
252b5132 | 310 | link_info.strip = strip_none; |
f5fa8ca2 | 311 | link_info.discard = discard_sec_merge; |
b5067602 AM |
312 | link_info.common_skip_ar_aymbols = bfd_link_common_skip_none; |
313 | link_info.callbacks = &link_callbacks; | |
252b5132 RH |
314 | link_info.hash = NULL; |
315 | link_info.keep_hash = NULL; | |
252b5132 RH |
316 | link_info.notice_hash = NULL; |
317 | link_info.wrap_hash = NULL; | |
b5067602 AM |
318 | link_info.input_bfds = NULL; |
319 | link_info.create_object_symbols_section = NULL; | |
320 | link_info.gc_sym_list = NULL; | |
321 | link_info.base_file = NULL; | |
3dbf70a2 MM |
322 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init |
323 | and _fini symbols. We are compatible. */ | |
324 | link_info.init_function = "_init"; | |
325 | link_info.fini_function = "_fini"; | |
b5067602 | 326 | link_info.mpc860c0 = 0; |
e0076ab3 | 327 | link_info.pei386_auto_import = -1; |
b34976b6 | 328 | link_info.pei386_runtime_pseudo_reloc = FALSE; |
db6751f2 | 329 | link_info.spare_dynamic_tags = 5; |
b5067602 AM |
330 | link_info.flags = (bfd_vma) 0; |
331 | link_info.flags_1 = (bfd_vma) 0; | |
c7996ad6 | 332 | link_info.relax_finalizing = FALSE; |
3dbf70a2 | 333 | |
252b5132 RH |
334 | ldfile_add_arch (""); |
335 | ||
b34976b6 AM |
336 | config.make_executable = TRUE; |
337 | force_make_executable = FALSE; | |
338 | config.magic_demand_paged = TRUE; | |
339 | config.text_read_only = TRUE; | |
252b5132 RH |
340 | |
341 | emulation = get_emulation (argc, argv); | |
342 | ldemul_choose_mode (emulation); | |
742aeb63 | 343 | default_target = ldemul_choose_target (argc, argv); |
252b5132 RH |
344 | lang_init (); |
345 | ldemul_before_parse (); | |
b34976b6 | 346 | lang_has_input_file = FALSE; |
252b5132 RH |
347 | parse_args (argc, argv); |
348 | ||
349 | ldemul_set_symbols (); | |
350 | ||
351 | if (link_info.relocateable) | |
352 | { | |
353 | if (command_line.gc_sections) | |
354 | einfo ("%P%F: --gc-sections and -r may not be used together\n"); | |
355 | if (link_info.mpc860c0) | |
356 | einfo (_("%P%F: -r and --mpc860c0 may not be used together\n")); | |
357 | else if (command_line.relax) | |
358 | einfo (_("%P%F: --relax and -r may not be used together\n")); | |
359 | if (link_info.shared) | |
360 | einfo (_("%P%F: -r and -shared may not be used together\n")); | |
361 | } | |
362 | ||
7333607e JJ |
363 | if (! link_info.shared) |
364 | { | |
365 | if (command_line.filter_shlib) | |
e97cb84f | 366 | einfo (_("%P%F: -F may not be used without -shared\n")); |
7333607e | 367 | if (command_line.auxiliary_filters) |
e97cb84f | 368 | einfo (_("%P%F: -f may not be used without -shared\n")); |
7333607e JJ |
369 | } |
370 | ||
252b5132 RH |
371 | /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I |
372 | don't see how else this can be handled, since in this case we | |
373 | must preserve all externally visible symbols. */ | |
374 | if (link_info.relocateable && link_info.strip == strip_all) | |
375 | { | |
376 | link_info.strip = strip_debugger; | |
f5fa8ca2 | 377 | if (link_info.discard == discard_sec_merge) |
252b5132 RH |
378 | link_info.discard = discard_all; |
379 | } | |
380 | ||
381 | /* This essentially adds another -L directory so this must be done after | |
382 | the -L's in argv have been processed. */ | |
383 | set_scripts_dir (); | |
384 | ||
b9a8de1e NC |
385 | /* If we have not already opened and parsed a linker script |
386 | read the emulation's appropriate default script. */ | |
b90e1c6f | 387 | if (saved_script_handle == NULL) |
252b5132 | 388 | { |
252b5132 | 389 | int isfile; |
b7a26f91 | 390 | char *s = ldemul_get_script (&isfile); |
252b5132 RH |
391 | |
392 | if (isfile) | |
393 | ldfile_open_command_file (s); | |
394 | else | |
b7a26f91 | 395 | { |
252b5132 RH |
396 | lex_string = s; |
397 | lex_redirect (s); | |
398 | } | |
399 | parser_input = input_script; | |
400 | yyparse (); | |
401 | lex_string = NULL; | |
402 | } | |
403 | ||
b9a8de1e NC |
404 | if (trace_file_tries) |
405 | { | |
9cb61eab AM |
406 | if (saved_script_handle) |
407 | info_msg (_("using external linker script:")); | |
408 | else | |
409 | info_msg (_("using internal linker script:")); | |
410 | info_msg ("\n==================================================\n"); | |
b9a8de1e NC |
411 | |
412 | if (saved_script_handle) | |
413 | { | |
74699268 | 414 | static const int ld_bufsz = 8193; |
b9a8de1e | 415 | size_t n; |
74699268 | 416 | char *buf = xmalloc (ld_bufsz); |
b9a8de1e NC |
417 | |
418 | rewind (saved_script_handle); | |
74699268 | 419 | while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0) |
b9a8de1e | 420 | { |
b7a26f91 | 421 | buf[n] = 0; |
b9a8de1e NC |
422 | info_msg (buf); |
423 | } | |
424 | rewind (saved_script_handle); | |
425 | free (buf); | |
426 | } | |
427 | else | |
428 | { | |
429 | int isfile; | |
430 | ||
b7a26f91 | 431 | info_msg (ldemul_get_script (&isfile)); |
b9a8de1e | 432 | } |
b7a26f91 | 433 | |
b9a8de1e NC |
434 | info_msg ("\n==================================================\n"); |
435 | } | |
436 | ||
252b5132 RH |
437 | lang_final (); |
438 | ||
b34976b6 | 439 | if (!lang_has_input_file) |
252b5132 RH |
440 | { |
441 | if (version_printed) | |
442 | xexit (0); | |
443 | einfo (_("%P%F: no input files\n")); | |
444 | } | |
445 | ||
446 | if (trace_files) | |
0125cdf2 | 447 | info_msg (_("%P: mode %s\n"), emulation); |
252b5132 RH |
448 | |
449 | ldemul_after_parse (); | |
450 | ||
252b5132 RH |
451 | if (config.map_filename) |
452 | { | |
453 | if (strcmp (config.map_filename, "-") == 0) | |
454 | { | |
455 | config.map_file = stdout; | |
456 | } | |
457 | else | |
458 | { | |
459 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
460 | if (config.map_file == (FILE *) NULL) | |
461 | { | |
462 | bfd_set_error (bfd_error_system_call); | |
463 | einfo (_("%P%F: cannot open map file %s: %E\n"), | |
464 | config.map_filename); | |
465 | } | |
466 | } | |
467 | } | |
468 | ||
252b5132 RH |
469 | lang_process (); |
470 | ||
471 | /* Print error messages for any missing symbols, for any warning | |
6d5e62f8 | 472 | symbols, and possibly multiple definitions. */ |
252b5132 RH |
473 | if (link_info.relocateable) |
474 | output_bfd->flags &= ~EXEC_P; | |
475 | else | |
476 | output_bfd->flags |= EXEC_P; | |
477 | ||
478 | ldwrite (); | |
479 | ||
480 | if (config.map_file != NULL) | |
481 | lang_map (); | |
482 | if (command_line.cref) | |
483 | output_cref (config.map_file != NULL ? config.map_file : stdout); | |
484 | if (nocrossref_list != NULL) | |
485 | check_nocrossrefs (); | |
486 | ||
487 | /* Even if we're producing relocateable output, some non-fatal errors should | |
488 | be reported in the exit status. (What non-fatal errors, if any, do we | |
489 | want to ignore for relocateable output?) */ | |
b34976b6 | 490 | if (!config.make_executable && !force_make_executable) |
252b5132 | 491 | { |
b34976b6 | 492 | if (trace_files) |
0125cdf2 NC |
493 | einfo (_("%P: link errors found, deleting executable `%s'\n"), |
494 | output_filename); | |
252b5132 RH |
495 | |
496 | /* The file will be removed by remove_output. */ | |
252b5132 RH |
497 | xexit (1); |
498 | } | |
499 | else | |
500 | { | |
501 | if (! bfd_close (output_bfd)) | |
502 | einfo (_("%F%B: final close failed: %E\n"), output_bfd); | |
503 | ||
504 | /* If the --force-exe-suffix is enabled, and we're making an | |
6d5e62f8 KH |
505 | executable file and it doesn't end in .exe, copy it to one |
506 | which does. */ | |
252b5132 RH |
507 | if (! link_info.relocateable && command_line.force_exe_suffix) |
508 | { | |
509 | int len = strlen (output_filename); | |
0125cdf2 | 510 | |
6d5e62f8 | 511 | if (len < 4 |
252b5132 RH |
512 | || (strcasecmp (output_filename + len - 4, ".exe") != 0 |
513 | && strcasecmp (output_filename + len - 4, ".dll") != 0)) | |
514 | { | |
515 | FILE *src; | |
516 | FILE *dst; | |
517 | const int bsize = 4096; | |
518 | char *buf = xmalloc (bsize); | |
519 | int l; | |
520 | char *dst_name = xmalloc (len + 5); | |
0125cdf2 | 521 | |
252b5132 RH |
522 | strcpy (dst_name, output_filename); |
523 | strcat (dst_name, ".exe"); | |
524 | src = fopen (output_filename, FOPEN_RB); | |
525 | dst = fopen (dst_name, FOPEN_WB); | |
526 | ||
527 | if (!src) | |
528 | einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename); | |
529 | if (!dst) | |
530 | einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name); | |
531 | while ((l = fread (buf, 1, bsize, src)) > 0) | |
532 | { | |
533 | int done = fwrite (buf, 1, l, dst); | |
0125cdf2 | 534 | |
252b5132 | 535 | if (done != l) |
0125cdf2 | 536 | einfo (_("%P: Error writing file `%s'\n"), dst_name); |
252b5132 | 537 | } |
0125cdf2 | 538 | |
252b5132 RH |
539 | fclose (src); |
540 | if (fclose (dst) == EOF) | |
0125cdf2 | 541 | einfo (_("%P: Error closing file `%s'\n"), dst_name); |
252b5132 RH |
542 | free (dst_name); |
543 | free (buf); | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | END_PROGRESS (program_name); | |
549 | ||
550 | if (config.stats) | |
551 | { | |
552 | #ifdef HAVE_SBRK | |
553 | char *lim = (char *) sbrk (0); | |
554 | #endif | |
555 | long run_time = get_run_time () - start_time; | |
556 | ||
557 | fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), | |
558 | program_name, run_time / 1000000, run_time % 1000000); | |
559 | #ifdef HAVE_SBRK | |
560 | fprintf (stderr, _("%s: data size %ld\n"), program_name, | |
561 | (long) (lim - (char *) &environ)); | |
562 | #endif | |
563 | } | |
564 | ||
565 | /* Prevent remove_output from doing anything, after a successful link. */ | |
566 | output_filename = NULL; | |
567 | ||
568 | xexit (0); | |
569 | return 0; | |
570 | } | |
571 | ||
572 | /* We need to find any explicitly given emulation in order to initialize the | |
573 | state that's needed by the lex&yacc argument parser (parse_args). */ | |
574 | ||
575 | static char * | |
576 | get_emulation (argc, argv) | |
577 | int argc; | |
578 | char **argv; | |
579 | { | |
580 | char *emulation; | |
581 | int i; | |
582 | ||
583 | emulation = getenv (EMULATION_ENVIRON); | |
584 | if (emulation == NULL) | |
585 | emulation = DEFAULT_EMULATION; | |
586 | ||
587 | for (i = 1; i < argc; i++) | |
588 | { | |
589 | if (!strncmp (argv[i], "-m", 2)) | |
590 | { | |
591 | if (argv[i][2] == '\0') | |
592 | { | |
593 | /* -m EMUL */ | |
594 | if (i < argc - 1) | |
595 | { | |
596 | emulation = argv[i + 1]; | |
597 | i++; | |
598 | } | |
599 | else | |
0125cdf2 | 600 | einfo (_("%P%F: missing argument to -m\n")); |
252b5132 RH |
601 | } |
602 | else if (strcmp (argv[i], "-mips1") == 0 | |
603 | || strcmp (argv[i], "-mips2") == 0 | |
604 | || strcmp (argv[i], "-mips3") == 0 | |
d1cf510e | 605 | || strcmp (argv[i], "-mips4") == 0 |
6f33421c CD |
606 | || strcmp (argv[i], "-mips5") == 0 |
607 | || strcmp (argv[i], "-mips32") == 0 | |
608 | || strcmp (argv[i], "-mips32r2") == 0 | |
609 | || strcmp (argv[i], "-mips64") == 0) | |
252b5132 | 610 | { |
31d677f9 | 611 | /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are |
252b5132 RH |
612 | passed to the linker by some MIPS compilers. They |
613 | generally tell the linker to use a slightly different | |
614 | library path. Perhaps someday these should be | |
615 | implemented as emulations; until then, we just ignore | |
616 | the arguments and hope that nobody ever creates | |
617 | emulations named ips1, ips2 or ips3. */ | |
618 | } | |
619 | else if (strcmp (argv[i], "-m486") == 0) | |
620 | { | |
621 | /* FIXME: The argument -m486 is passed to the linker on | |
622 | some Linux systems. Hope that nobody creates an | |
623 | emulation named 486. */ | |
624 | } | |
625 | else | |
626 | { | |
627 | /* -mEMUL */ | |
628 | emulation = &argv[i][2]; | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | return emulation; | |
634 | } | |
635 | ||
636 | /* If directory DIR contains an "ldscripts" subdirectory, | |
b34976b6 AM |
637 | add DIR to the library search path and return TRUE, |
638 | else return FALSE. */ | |
252b5132 | 639 | |
b34976b6 | 640 | static bfd_boolean |
252b5132 RH |
641 | check_for_scripts_dir (dir) |
642 | char *dir; | |
643 | { | |
644 | size_t dirlen; | |
645 | char *buf; | |
646 | struct stat s; | |
b34976b6 | 647 | bfd_boolean res; |
252b5132 RH |
648 | |
649 | dirlen = strlen (dir); | |
650 | /* sizeof counts the terminating NUL. */ | |
6d5e62f8 | 651 | buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts")); |
252b5132 RH |
652 | sprintf (buf, "%s/ldscripts", dir); |
653 | ||
654 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
655 | free (buf); | |
656 | if (res) | |
b34976b6 | 657 | ldfile_add_library_path (dir, FALSE); |
252b5132 RH |
658 | return res; |
659 | } | |
660 | ||
661 | /* Set the default directory for finding script files. | |
662 | Libraries will be searched for here too, but that's ok. | |
663 | We look for the "ldscripts" directory in: | |
664 | ||
cdc0d45e DJ |
665 | SCRIPTDIR (passed from Makefile) |
666 | (adjusted according to the current location of the binary) | |
252b5132 RH |
667 | SCRIPTDIR (passed from Makefile) |
668 | the dir where this program is (for using it from the build tree) | |
cdc0d45e | 669 | the dir where this program is/../lib |
0125cdf2 | 670 | (for installing the tool suite elsewhere). */ |
252b5132 RH |
671 | |
672 | static void | |
673 | set_scripts_dir () | |
674 | { | |
675 | char *end, *dir; | |
676 | size_t dirlen; | |
5ed6aba4 | 677 | bfd_boolean found; |
252b5132 | 678 | |
cdc0d45e | 679 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); |
cdc0d45e | 680 | if (dir) |
5ed6aba4 NC |
681 | { |
682 | found = check_for_scripts_dir (dir); | |
683 | free (dir); | |
684 | if (found) | |
685 | return; | |
686 | } | |
cdc0d45e | 687 | |
4ca1b790 | 688 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); |
4ca1b790 | 689 | if (dir) |
5ed6aba4 NC |
690 | { |
691 | found = check_for_scripts_dir (dir); | |
692 | free (dir); | |
693 | if (found) | |
694 | return; | |
695 | } | |
4ca1b790 | 696 | |
252b5132 | 697 | if (check_for_scripts_dir (SCRIPTDIR)) |
6d5e62f8 KH |
698 | /* We've been installed normally. */ |
699 | return; | |
252b5132 RH |
700 | |
701 | /* Look for "ldscripts" in the dir where our binary is. */ | |
702 | end = strrchr (program_name, '/'); | |
5af11cab AM |
703 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
704 | { | |
705 | /* We could have \foo\bar, or /foo\bar. */ | |
706 | char *bslash = strrchr (program_name, '\\'); | |
0125cdf2 | 707 | |
2ab47eed | 708 | if (end == NULL || (bslash != NULL && bslash > end)) |
5af11cab AM |
709 | end = bslash; |
710 | } | |
711 | #endif | |
252b5132 RH |
712 | |
713 | if (end == NULL) | |
0125cdf2 NC |
714 | /* Don't look for ldscripts in the current directory. There is |
715 | too much potential for confusion. */ | |
716 | return; | |
252b5132 RH |
717 | |
718 | dirlen = end - program_name; | |
719 | /* Make a copy of program_name in dir. | |
720 | Leave room for later "/../lib". */ | |
721 | dir = (char *) xmalloc (dirlen + 8); | |
722 | strncpy (dir, program_name, dirlen); | |
723 | dir[dirlen] = '\0'; | |
724 | ||
725 | if (check_for_scripts_dir (dir)) | |
5ed6aba4 NC |
726 | { |
727 | free (dir); | |
728 | return; | |
729 | } | |
252b5132 RH |
730 | |
731 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ | |
732 | strcpy (dir + dirlen, "/../lib"); | |
5ed6aba4 | 733 | check_for_scripts_dir (dir); |
6d5e62f8 | 734 | free (dir); |
252b5132 RH |
735 | } |
736 | ||
737 | void | |
738 | add_ysym (name) | |
739 | const char *name; | |
740 | { | |
741 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) | |
742 | { | |
743 | link_info.notice_hash = ((struct bfd_hash_table *) | |
744 | xmalloc (sizeof (struct bfd_hash_table))); | |
745 | if (! bfd_hash_table_init_n (link_info.notice_hash, | |
746 | bfd_hash_newfunc, | |
747 | 61)) | |
748 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
6d5e62f8 | 749 | } |
252b5132 | 750 | |
b34976b6 | 751 | if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) |
252b5132 RH |
752 | == (struct bfd_hash_entry *) NULL) |
753 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); | |
754 | } | |
755 | ||
756 | /* Record a symbol to be wrapped, from the --wrap option. */ | |
757 | ||
758 | void | |
759 | add_wrap (name) | |
760 | const char *name; | |
761 | { | |
762 | if (link_info.wrap_hash == NULL) | |
763 | { | |
764 | link_info.wrap_hash = ((struct bfd_hash_table *) | |
765 | xmalloc (sizeof (struct bfd_hash_table))); | |
766 | if (! bfd_hash_table_init_n (link_info.wrap_hash, | |
767 | bfd_hash_newfunc, | |
768 | 61)) | |
769 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
770 | } | |
0125cdf2 | 771 | |
b34976b6 | 772 | if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL) |
252b5132 RH |
773 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); |
774 | } | |
775 | ||
776 | /* Handle the -retain-symbols-file option. */ | |
777 | ||
778 | void | |
779 | add_keepsyms_file (filename) | |
780 | const char *filename; | |
781 | { | |
782 | FILE *file; | |
783 | char *buf; | |
784 | size_t bufsize; | |
785 | int c; | |
786 | ||
787 | if (link_info.strip == strip_some) | |
788 | einfo (_("%X%P: error: duplicate retain-symbols-file\n")); | |
789 | ||
790 | file = fopen (filename, "r"); | |
791 | if (file == (FILE *) NULL) | |
792 | { | |
793 | bfd_set_error (bfd_error_system_call); | |
794 | einfo ("%X%P: %s: %E\n", filename); | |
795 | return; | |
796 | } | |
797 | ||
798 | link_info.keep_hash = ((struct bfd_hash_table *) | |
799 | xmalloc (sizeof (struct bfd_hash_table))); | |
800 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) | |
801 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
802 | ||
803 | bufsize = 100; | |
804 | buf = (char *) xmalloc (bufsize); | |
805 | ||
806 | c = getc (file); | |
807 | while (c != EOF) | |
808 | { | |
3882b010 | 809 | while (ISSPACE (c)) |
252b5132 RH |
810 | c = getc (file); |
811 | ||
812 | if (c != EOF) | |
813 | { | |
814 | size_t len = 0; | |
815 | ||
3882b010 | 816 | while (! ISSPACE (c) && c != EOF) |
252b5132 RH |
817 | { |
818 | buf[len] = c; | |
819 | ++len; | |
820 | if (len >= bufsize) | |
821 | { | |
822 | bufsize *= 2; | |
823 | buf = xrealloc (buf, bufsize); | |
824 | } | |
825 | c = getc (file); | |
826 | } | |
827 | ||
828 | buf[len] = '\0'; | |
829 | ||
b34976b6 | 830 | if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) |
252b5132 RH |
831 | == (struct bfd_hash_entry *) NULL) |
832 | einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); | |
833 | } | |
834 | } | |
835 | ||
836 | if (link_info.strip != strip_none) | |
837 | einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); | |
838 | ||
5ed6aba4 | 839 | free (buf); |
252b5132 RH |
840 | link_info.strip = strip_some; |
841 | } | |
842 | \f | |
843 | /* Callbacks from the BFD linker routines. */ | |
844 | ||
845 | /* This is called when BFD has decided to include an archive member in | |
846 | a link. */ | |
847 | ||
b34976b6 | 848 | static bfd_boolean |
252b5132 | 849 | add_archive_element (info, abfd, name) |
87f2a346 | 850 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
851 | bfd *abfd; |
852 | const char *name; | |
853 | { | |
854 | lang_input_statement_type *input; | |
855 | ||
856 | input = ((lang_input_statement_type *) | |
857 | xmalloc (sizeof (lang_input_statement_type))); | |
858 | input->filename = abfd->filename; | |
859 | input->local_sym_name = abfd->filename; | |
860 | input->the_bfd = abfd; | |
861 | input->asymbols = NULL; | |
862 | input->next = NULL; | |
b34976b6 AM |
863 | input->just_syms_flag = FALSE; |
864 | input->loaded = FALSE; | |
865 | input->search_dirs_flag = FALSE; | |
252b5132 RH |
866 | |
867 | /* FIXME: The following fields are not set: header.next, | |
868 | header.type, closed, passive_position, symbol_count, | |
869 | next_real_file, is_archive, target, real. This bit of code is | |
870 | from the old decode_library_subfile function. I don't know | |
871 | whether any of those fields matters. */ | |
872 | ||
873 | ldlang_add_file (input); | |
874 | ||
875 | if (config.map_file != (FILE *) NULL) | |
876 | { | |
b34976b6 | 877 | static bfd_boolean header_printed; |
252b5132 RH |
878 | struct bfd_link_hash_entry *h; |
879 | bfd *from; | |
880 | int len; | |
881 | ||
b34976b6 | 882 | h = bfd_link_hash_lookup (link_info.hash, name, FALSE, FALSE, TRUE); |
252b5132 RH |
883 | |
884 | if (h == NULL) | |
885 | from = NULL; | |
886 | else | |
887 | { | |
888 | switch (h->type) | |
889 | { | |
890 | default: | |
891 | from = NULL; | |
892 | break; | |
893 | ||
894 | case bfd_link_hash_defined: | |
895 | case bfd_link_hash_defweak: | |
896 | from = h->u.def.section->owner; | |
897 | break; | |
898 | ||
899 | case bfd_link_hash_undefined: | |
900 | case bfd_link_hash_undefweak: | |
901 | from = h->u.undef.abfd; | |
902 | break; | |
903 | ||
904 | case bfd_link_hash_common: | |
905 | from = h->u.c.p->section->owner; | |
906 | break; | |
907 | } | |
908 | } | |
909 | ||
910 | if (! header_printed) | |
911 | { | |
912 | char buf[100]; | |
913 | ||
e3c8793a | 914 | sprintf (buf, _("Archive member included because of file (symbol)\n\n")); |
252b5132 | 915 | minfo ("%s", buf); |
b34976b6 | 916 | header_printed = TRUE; |
252b5132 RH |
917 | } |
918 | ||
919 | if (bfd_my_archive (abfd) == NULL) | |
920 | { | |
921 | minfo ("%s", bfd_get_filename (abfd)); | |
922 | len = strlen (bfd_get_filename (abfd)); | |
923 | } | |
924 | else | |
925 | { | |
926 | minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), | |
927 | bfd_get_filename (abfd)); | |
928 | len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) | |
929 | + strlen (bfd_get_filename (abfd)) | |
930 | + 2); | |
931 | } | |
932 | ||
933 | if (len >= 29) | |
934 | { | |
935 | print_nl (); | |
936 | len = 0; | |
937 | } | |
938 | while (len < 30) | |
939 | { | |
940 | print_space (); | |
941 | ++len; | |
942 | } | |
943 | ||
944 | if (from != NULL) | |
945 | minfo ("%B ", from); | |
946 | if (h != NULL) | |
947 | minfo ("(%T)\n", h->root.string); | |
948 | else | |
949 | minfo ("(%s)\n", name); | |
950 | } | |
951 | ||
952 | if (trace_files || trace_file_tries) | |
953 | info_msg ("%I\n", input); | |
954 | ||
b34976b6 | 955 | return TRUE; |
252b5132 RH |
956 | } |
957 | ||
958 | /* This is called when BFD has discovered a symbol which is defined | |
959 | multiple times. */ | |
960 | ||
b34976b6 | 961 | static bfd_boolean |
252b5132 | 962 | multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) |
87f2a346 | 963 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
964 | const char *name; |
965 | bfd *obfd; | |
966 | asection *osec; | |
967 | bfd_vma oval; | |
968 | bfd *nbfd; | |
969 | asection *nsec; | |
970 | bfd_vma nval; | |
971 | { | |
972 | /* If either section has the output_section field set to | |
973 | bfd_abs_section_ptr, it means that the section is being | |
974 | discarded, and this is not really a multiple definition at all. | |
975 | FIXME: It would be cleaner to somehow ignore symbols defined in | |
976 | sections which are being discarded. */ | |
977 | if ((osec->output_section != NULL | |
978 | && ! bfd_is_abs_section (osec) | |
979 | && bfd_is_abs_section (osec->output_section)) | |
980 | || (nsec->output_section != NULL | |
981 | && ! bfd_is_abs_section (nsec) | |
982 | && bfd_is_abs_section (nsec->output_section))) | |
b34976b6 | 983 | return TRUE; |
252b5132 RH |
984 | |
985 | einfo (_("%X%C: multiple definition of `%T'\n"), | |
986 | nbfd, nsec, nval, name); | |
987 | if (obfd != (bfd *) NULL) | |
988 | einfo (_("%D: first defined here\n"), obfd, osec, oval); | |
9b14b192 NC |
989 | |
990 | if (command_line.relax) | |
991 | { | |
992 | einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n")); | |
993 | command_line.relax = 0; | |
994 | } | |
6d5e62f8 | 995 | |
b34976b6 | 996 | return TRUE; |
252b5132 RH |
997 | } |
998 | ||
999 | /* This is called when there is a definition of a common symbol, or | |
1000 | when a common symbol is found for a symbol that is already defined, | |
1001 | or when two common symbols are found. We only do something if | |
1002 | -warn-common was used. */ | |
1003 | ||
b34976b6 | 1004 | static bfd_boolean |
252b5132 | 1005 | multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) |
87f2a346 | 1006 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1007 | const char *name; |
1008 | bfd *obfd; | |
1009 | enum bfd_link_hash_type otype; | |
1010 | bfd_vma osize; | |
1011 | bfd *nbfd; | |
1012 | enum bfd_link_hash_type ntype; | |
1013 | bfd_vma nsize; | |
1014 | { | |
1015 | if (! config.warn_common) | |
b34976b6 | 1016 | return TRUE; |
252b5132 RH |
1017 | |
1018 | if (ntype == bfd_link_hash_defined | |
1019 | || ntype == bfd_link_hash_defweak | |
1020 | || ntype == bfd_link_hash_indirect) | |
1021 | { | |
1022 | ASSERT (otype == bfd_link_hash_common); | |
1023 | einfo (_("%B: warning: definition of `%T' overriding common\n"), | |
1024 | nbfd, name); | |
1025 | if (obfd != NULL) | |
1026 | einfo (_("%B: warning: common is here\n"), obfd); | |
1027 | } | |
1028 | else if (otype == bfd_link_hash_defined | |
1029 | || otype == bfd_link_hash_defweak | |
1030 | || otype == bfd_link_hash_indirect) | |
1031 | { | |
1032 | ASSERT (ntype == bfd_link_hash_common); | |
1033 | einfo (_("%B: warning: common of `%T' overridden by definition\n"), | |
1034 | nbfd, name); | |
1035 | if (obfd != NULL) | |
1036 | einfo (_("%B: warning: defined here\n"), obfd); | |
1037 | } | |
1038 | else | |
1039 | { | |
1040 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
1041 | if (osize > nsize) | |
1042 | { | |
1043 | einfo (_("%B: warning: common of `%T' overridden by larger common\n"), | |
1044 | nbfd, name); | |
1045 | if (obfd != NULL) | |
1046 | einfo (_("%B: warning: larger common is here\n"), obfd); | |
1047 | } | |
1048 | else if (nsize > osize) | |
1049 | { | |
1050 | einfo (_("%B: warning: common of `%T' overriding smaller common\n"), | |
1051 | nbfd, name); | |
1052 | if (obfd != NULL) | |
1053 | einfo (_("%B: warning: smaller common is here\n"), obfd); | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); | |
1058 | if (obfd != NULL) | |
1059 | einfo (_("%B: warning: previous common is here\n"), obfd); | |
1060 | } | |
1061 | } | |
1062 | ||
b34976b6 | 1063 | return TRUE; |
252b5132 RH |
1064 | } |
1065 | ||
1066 | /* This is called when BFD has discovered a set element. H is the | |
1067 | entry in the linker hash table for the set. SECTION and VALUE | |
1068 | represent a value which should be added to the set. */ | |
1069 | ||
b34976b6 | 1070 | static bfd_boolean |
252b5132 | 1071 | add_to_set (info, h, reloc, abfd, section, value) |
87f2a346 | 1072 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1073 | struct bfd_link_hash_entry *h; |
1074 | bfd_reloc_code_real_type reloc; | |
1075 | bfd *abfd; | |
1076 | asection *section; | |
1077 | bfd_vma value; | |
1078 | { | |
1079 | if (config.warn_constructors) | |
1080 | einfo (_("%P: warning: global constructor %s used\n"), | |
1081 | h->root.string); | |
1082 | ||
1083 | if (! config.build_constructors) | |
b34976b6 | 1084 | return TRUE; |
252b5132 RH |
1085 | |
1086 | ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value); | |
1087 | ||
1088 | if (h->type == bfd_link_hash_new) | |
1089 | { | |
1090 | h->type = bfd_link_hash_undefined; | |
1091 | h->u.undef.abfd = abfd; | |
1092 | /* We don't call bfd_link_add_undef to add this to the list of | |
1093 | undefined symbols because we are going to define it | |
1094 | ourselves. */ | |
1095 | } | |
1096 | ||
b34976b6 | 1097 | return TRUE; |
252b5132 RH |
1098 | } |
1099 | ||
1100 | /* This is called when BFD has discovered a constructor. This is only | |
1101 | called for some object file formats--those which do not handle | |
1102 | constructors in some more clever fashion. This is similar to | |
1103 | adding an element to a set, but less general. */ | |
1104 | ||
b34976b6 | 1105 | static bfd_boolean |
252b5132 RH |
1106 | constructor_callback (info, constructor, name, abfd, section, value) |
1107 | struct bfd_link_info *info; | |
b34976b6 | 1108 | bfd_boolean constructor; |
252b5132 RH |
1109 | const char *name; |
1110 | bfd *abfd; | |
1111 | asection *section; | |
1112 | bfd_vma value; | |
1113 | { | |
1114 | char *s; | |
1115 | struct bfd_link_hash_entry *h; | |
1116 | char set_name[1 + sizeof "__CTOR_LIST__"]; | |
1117 | ||
1118 | if (config.warn_constructors) | |
1119 | einfo (_("%P: warning: global constructor %s used\n"), name); | |
1120 | ||
1121 | if (! config.build_constructors) | |
b34976b6 | 1122 | return TRUE; |
252b5132 RH |
1123 | |
1124 | /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a | |
1125 | useful error message. */ | |
1126 | if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL | |
1127 | && (link_info.relocateable | |
1128 | || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) | |
1129 | einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); | |
1130 | ||
1131 | s = set_name; | |
1132 | if (bfd_get_symbol_leading_char (abfd) != '\0') | |
1133 | *s++ = bfd_get_symbol_leading_char (abfd); | |
1134 | if (constructor) | |
1135 | strcpy (s, "__CTOR_LIST__"); | |
1136 | else | |
1137 | strcpy (s, "__DTOR_LIST__"); | |
1138 | ||
b34976b6 | 1139 | h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE); |
252b5132 RH |
1140 | if (h == (struct bfd_link_hash_entry *) NULL) |
1141 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); | |
1142 | if (h->type == bfd_link_hash_new) | |
1143 | { | |
1144 | h->type = bfd_link_hash_undefined; | |
1145 | h->u.undef.abfd = abfd; | |
1146 | /* We don't call bfd_link_add_undef to add this to the list of | |
1147 | undefined symbols because we are going to define it | |
1148 | ourselves. */ | |
1149 | } | |
1150 | ||
1151 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); | |
b34976b6 | 1152 | return TRUE; |
252b5132 RH |
1153 | } |
1154 | ||
1155 | /* A structure used by warning_callback to pass information through | |
1156 | bfd_map_over_sections. */ | |
1157 | ||
0125cdf2 NC |
1158 | struct warning_callback_info |
1159 | { | |
b34976b6 | 1160 | bfd_boolean found; |
252b5132 RH |
1161 | const char *warning; |
1162 | const char *symbol; | |
1163 | asymbol **asymbols; | |
1164 | }; | |
1165 | ||
1166 | /* This is called when there is a reference to a warning symbol. */ | |
1167 | ||
b34976b6 | 1168 | static bfd_boolean |
252b5132 | 1169 | warning_callback (info, warning, symbol, abfd, section, address) |
87f2a346 | 1170 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1171 | const char *warning; |
1172 | const char *symbol; | |
1173 | bfd *abfd; | |
1174 | asection *section; | |
1175 | bfd_vma address; | |
1176 | { | |
1177 | /* This is a hack to support warn_multiple_gp. FIXME: This should | |
1178 | have a cleaner interface, but what? */ | |
1179 | if (! config.warn_multiple_gp | |
1180 | && strcmp (warning, "using multiple gp values") == 0) | |
b34976b6 | 1181 | return TRUE; |
252b5132 RH |
1182 | |
1183 | if (section != NULL) | |
1184 | einfo ("%C: %s\n", abfd, section, address, warning); | |
1185 | else if (abfd == NULL) | |
1186 | einfo ("%P: %s\n", warning); | |
1187 | else if (symbol == NULL) | |
1188 | einfo ("%B: %s\n", abfd, warning); | |
1189 | else | |
1190 | { | |
1191 | lang_input_statement_type *entry; | |
1192 | asymbol **asymbols; | |
1193 | struct warning_callback_info info; | |
1194 | ||
1195 | /* Look through the relocs to see if we can find a plausible | |
1196 | address. */ | |
252b5132 RH |
1197 | entry = (lang_input_statement_type *) abfd->usrdata; |
1198 | if (entry != NULL && entry->asymbols != NULL) | |
1199 | asymbols = entry->asymbols; | |
1200 | else | |
1201 | { | |
1202 | long symsize; | |
1203 | long symbol_count; | |
1204 | ||
1205 | symsize = bfd_get_symtab_upper_bound (abfd); | |
1206 | if (symsize < 0) | |
1207 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); | |
1208 | asymbols = (asymbol **) xmalloc (symsize); | |
1209 | symbol_count = bfd_canonicalize_symtab (abfd, asymbols); | |
1210 | if (symbol_count < 0) | |
1211 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); | |
1212 | if (entry != NULL) | |
1213 | { | |
1214 | entry->asymbols = asymbols; | |
1215 | entry->symbol_count = symbol_count; | |
1216 | } | |
1217 | } | |
1218 | ||
b34976b6 | 1219 | info.found = FALSE; |
252b5132 RH |
1220 | info.warning = warning; |
1221 | info.symbol = symbol; | |
1222 | info.asymbols = asymbols; | |
1223 | bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info); | |
1224 | ||
1225 | if (! info.found) | |
1226 | einfo ("%B: %s\n", abfd, warning); | |
1227 | ||
1228 | if (entry == NULL) | |
1229 | free (asymbols); | |
1230 | } | |
1231 | ||
b34976b6 | 1232 | return TRUE; |
252b5132 RH |
1233 | } |
1234 | ||
1235 | /* This is called by warning_callback for each section. It checks the | |
1236 | relocs of the section to see if it can find a reference to the | |
1237 | symbol which triggered the warning. If it can, it uses the reloc | |
1238 | to give an error message with a file and line number. */ | |
1239 | ||
1240 | static void | |
1241 | warning_find_reloc (abfd, sec, iarg) | |
1242 | bfd *abfd; | |
1243 | asection *sec; | |
1244 | PTR iarg; | |
1245 | { | |
1246 | struct warning_callback_info *info = (struct warning_callback_info *) iarg; | |
1247 | long relsize; | |
1248 | arelent **relpp; | |
1249 | long relcount; | |
1250 | arelent **p, **pend; | |
1251 | ||
1252 | if (info->found) | |
1253 | return; | |
1254 | ||
1255 | relsize = bfd_get_reloc_upper_bound (abfd, sec); | |
1256 | if (relsize < 0) | |
1257 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); | |
1258 | if (relsize == 0) | |
1259 | return; | |
1260 | ||
1261 | relpp = (arelent **) xmalloc (relsize); | |
1262 | relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); | |
1263 | if (relcount < 0) | |
1264 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); | |
1265 | ||
1266 | p = relpp; | |
1267 | pend = p + relcount; | |
1268 | for (; p < pend && *p != NULL; p++) | |
1269 | { | |
1270 | arelent *q = *p; | |
1271 | ||
1272 | if (q->sym_ptr_ptr != NULL | |
1273 | && *q->sym_ptr_ptr != NULL | |
1274 | && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) | |
1275 | { | |
1276 | /* We found a reloc for the symbol we are looking for. */ | |
1277 | einfo ("%C: %s\n", abfd, sec, q->address, info->warning); | |
b34976b6 | 1278 | info->found = TRUE; |
252b5132 RH |
1279 | break; |
1280 | } | |
1281 | } | |
1282 | ||
1283 | free (relpp); | |
1284 | } | |
1285 | ||
1286 | /* This is called when an undefined symbol is found. */ | |
1287 | ||
b34976b6 | 1288 | static bfd_boolean |
b6f29aaa | 1289 | undefined_symbol (info, name, abfd, section, address, fatal) |
87f2a346 | 1290 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1291 | const char *name; |
1292 | bfd *abfd; | |
1293 | asection *section; | |
1294 | bfd_vma address; | |
b34976b6 | 1295 | bfd_boolean fatal ATTRIBUTE_UNUSED; |
252b5132 RH |
1296 | { |
1297 | static char *error_name; | |
1298 | static unsigned int error_count; | |
1299 | ||
1300 | #define MAX_ERRORS_IN_A_ROW 5 | |
1301 | ||
1302 | if (config.warn_once) | |
1303 | { | |
1304 | static struct bfd_hash_table *hash; | |
1305 | ||
1306 | /* Only warn once about a particular undefined symbol. */ | |
252b5132 RH |
1307 | if (hash == NULL) |
1308 | { | |
1309 | hash = ((struct bfd_hash_table *) | |
1310 | xmalloc (sizeof (struct bfd_hash_table))); | |
1311 | if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) | |
1312 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); | |
1313 | } | |
1314 | ||
b34976b6 AM |
1315 | if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL) |
1316 | return TRUE; | |
252b5132 | 1317 | |
b34976b6 | 1318 | if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL) |
252b5132 RH |
1319 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
1320 | } | |
1321 | ||
1322 | /* We never print more than a reasonable number of errors in a row | |
1323 | for a single symbol. */ | |
1324 | if (error_name != (char *) NULL | |
1325 | && strcmp (name, error_name) == 0) | |
1326 | ++error_count; | |
1327 | else | |
1328 | { | |
1329 | error_count = 0; | |
1330 | if (error_name != (char *) NULL) | |
1331 | free (error_name); | |
d1b2b2dc | 1332 | error_name = xstrdup (name); |
252b5132 RH |
1333 | } |
1334 | ||
1335 | if (section != NULL) | |
1336 | { | |
1337 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
b6f29aaa L |
1338 | { |
1339 | einfo (_("%C: undefined reference to `%T'\n"), | |
1340 | abfd, section, address, name); | |
1341 | if (fatal) | |
1342 | einfo ("%X"); | |
1343 | } | |
252b5132 RH |
1344 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
1345 | einfo (_("%D: more undefined references to `%T' follow\n"), | |
1346 | abfd, section, address, name); | |
1347 | } | |
1348 | else | |
1349 | { | |
1350 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
b6f29aaa L |
1351 | { |
1352 | einfo (_("%B: undefined reference to `%T'\n"), | |
1353 | abfd, name); | |
1354 | if (fatal) | |
1355 | einfo ("%X"); | |
1356 | } | |
252b5132 RH |
1357 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
1358 | einfo (_("%B: more undefined references to `%T' follow\n"), | |
1359 | abfd, name); | |
1360 | } | |
1361 | ||
b34976b6 | 1362 | return TRUE; |
252b5132 RH |
1363 | } |
1364 | ||
8aae64e6 AO |
1365 | /* Counter to limit the number of relocation overflow error messages |
1366 | to print. Errors are printed as it is decremented. When it's | |
1367 | called and the counter is zero, a final message is printed | |
1368 | indicating more relocations were omitted. When it gets to -1, no | |
1369 | such errors are printed. If it's initially set to a value less | |
1370 | than -1, all such errors will be printed (--verbose does this). */ | |
1371 | ||
1372 | int overflow_cutoff_limit = 10; | |
1373 | ||
252b5132 RH |
1374 | /* This is called when a reloc overflows. */ |
1375 | ||
b34976b6 | 1376 | static bfd_boolean |
252b5132 | 1377 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) |
87f2a346 | 1378 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1379 | const char *name; |
1380 | const char *reloc_name; | |
1381 | bfd_vma addend; | |
1382 | bfd *abfd; | |
1383 | asection *section; | |
1384 | bfd_vma address; | |
1385 | { | |
8aae64e6 AO |
1386 | if (overflow_cutoff_limit == -1) |
1387 | return TRUE; | |
1388 | ||
252b5132 RH |
1389 | if (abfd == (bfd *) NULL) |
1390 | einfo (_("%P%X: generated")); | |
1391 | else | |
1392 | einfo ("%X%C:", abfd, section, address); | |
8aae64e6 AO |
1393 | |
1394 | if (overflow_cutoff_limit >= 0 | |
1395 | && overflow_cutoff_limit-- == 0) | |
1396 | { | |
1397 | einfo (_(" additional relocation overflows omitted from the output\n")); | |
1398 | return TRUE; | |
1399 | } | |
1400 | ||
252b5132 RH |
1401 | einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); |
1402 | if (addend != 0) | |
1403 | einfo ("+%v", addend); | |
1404 | einfo ("\n"); | |
b34976b6 | 1405 | return TRUE; |
252b5132 RH |
1406 | } |
1407 | ||
1408 | /* This is called when a dangerous relocation is made. */ | |
1409 | ||
b34976b6 | 1410 | static bfd_boolean |
252b5132 | 1411 | reloc_dangerous (info, message, abfd, section, address) |
87f2a346 | 1412 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1413 | const char *message; |
1414 | bfd *abfd; | |
1415 | asection *section; | |
1416 | bfd_vma address; | |
1417 | { | |
1418 | if (abfd == (bfd *) NULL) | |
1419 | einfo (_("%P%X: generated")); | |
1420 | else | |
1421 | einfo ("%X%C:", abfd, section, address); | |
1422 | einfo (_("dangerous relocation: %s\n"), message); | |
b34976b6 | 1423 | return TRUE; |
252b5132 RH |
1424 | } |
1425 | ||
1426 | /* This is called when a reloc is being generated attached to a symbol | |
1427 | that is not being output. */ | |
1428 | ||
b34976b6 | 1429 | static bfd_boolean |
252b5132 | 1430 | unattached_reloc (info, name, abfd, section, address) |
87f2a346 | 1431 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1432 | const char *name; |
1433 | bfd *abfd; | |
1434 | asection *section; | |
1435 | bfd_vma address; | |
1436 | { | |
1437 | if (abfd == (bfd *) NULL) | |
1438 | einfo (_("%P%X: generated")); | |
1439 | else | |
1440 | einfo ("%X%C:", abfd, section, address); | |
1441 | einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name); | |
b34976b6 | 1442 | return TRUE; |
252b5132 RH |
1443 | } |
1444 | ||
1445 | /* This is called if link_info.notice_all is set, or when a symbol in | |
1446 | link_info.notice_hash is found. Symbols are put in notice_hash | |
1447 | using the -y option. */ | |
1448 | ||
b34976b6 | 1449 | static bfd_boolean |
252b5132 RH |
1450 | notice (info, name, abfd, section, value) |
1451 | struct bfd_link_info *info; | |
1452 | const char *name; | |
1453 | bfd *abfd; | |
1454 | asection *section; | |
1455 | bfd_vma value; | |
1456 | { | |
1457 | if (! info->notice_all | |
1458 | || (info->notice_hash != NULL | |
b34976b6 | 1459 | && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)) |
252b5132 RH |
1460 | { |
1461 | if (bfd_is_und_section (section)) | |
1462 | einfo ("%B: reference to %s\n", abfd, name); | |
1463 | else | |
1464 | einfo ("%B: definition of %s\n", abfd, name); | |
1465 | } | |
1466 | ||
1467 | if (command_line.cref || nocrossref_list != NULL) | |
1468 | add_cref (name, abfd, section, value); | |
1469 | ||
b34976b6 | 1470 | return TRUE; |
252b5132 | 1471 | } |