]>
Commit | Line | Data |
---|---|---|
1 | /* Main program of GNU linker. | |
2 | Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc. | |
3 | Written by Steve Chamberlain [email protected] | |
4 | ||
5 | This file is part of GLD, the Gnu Linker. | |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GLD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | ||
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include <stdio.h> | |
25 | #include "libiberty.h" | |
26 | #include "bfdlink.h" | |
27 | ||
28 | #include "config.h" | |
29 | #include "ld.h" | |
30 | #include "ldmain.h" | |
31 | #include "ldmisc.h" | |
32 | #include "ldwrite.h" | |
33 | #include "ldgram.h" | |
34 | #include "ldexp.h" | |
35 | #include "ldlang.h" | |
36 | #include "ldemul.h" | |
37 | #include "ldlex.h" | |
38 | #include "ldfile.h" | |
39 | #include "ldctor.h" | |
40 | ||
41 | /* Somewhere above, sys/stat.h got included . . . . */ | |
42 | #if !defined(S_ISDIR) && defined(S_IFDIR) | |
43 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
44 | #endif | |
45 | ||
46 | #include <string.h> | |
47 | ||
48 | static char *get_emulation PARAMS ((int, char **)); | |
49 | static void set_scripts_dir PARAMS ((void)); | |
50 | ||
51 | /* EXPORTS */ | |
52 | ||
53 | char *default_target; | |
54 | const char *output_filename = "a.out"; | |
55 | ||
56 | /* Name this program was invoked by. */ | |
57 | char *program_name; | |
58 | ||
59 | /* The file that we're creating */ | |
60 | bfd *output_bfd = 0; | |
61 | ||
62 | /* Set by -G argument, for MIPS ECOFF target. */ | |
63 | int g_switch_value = 8; | |
64 | ||
65 | /* Nonzero means print names of input files as processed. */ | |
66 | boolean trace_files; | |
67 | ||
68 | /* Nonzero means same, but note open failures, too. */ | |
69 | boolean trace_file_tries; | |
70 | ||
71 | /* Nonzero means version number was printed, so exit successfully | |
72 | instead of complaining if no input files are given. */ | |
73 | boolean version_printed; | |
74 | ||
75 | /* Nonzero means link in every member of an archive. */ | |
76 | boolean whole_archive; | |
77 | ||
78 | args_type command_line; | |
79 | ||
80 | ld_config_type config; | |
81 | ||
82 | static boolean check_for_scripts_dir PARAMS ((char *dir)); | |
83 | static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *, | |
84 | const char *)); | |
85 | static boolean multiple_definition PARAMS ((struct bfd_link_info *, | |
86 | const char *, | |
87 | bfd *, asection *, bfd_vma, | |
88 | bfd *, asection *, bfd_vma)); | |
89 | static boolean multiple_common PARAMS ((struct bfd_link_info *, | |
90 | const char *, bfd *, | |
91 | enum bfd_link_hash_type, bfd_vma, | |
92 | bfd *, enum bfd_link_hash_type, | |
93 | bfd_vma)); | |
94 | static boolean add_to_set PARAMS ((struct bfd_link_info *, | |
95 | struct bfd_link_hash_entry *, | |
96 | bfd_reloc_code_real_type, | |
97 | bfd *, asection *, bfd_vma)); | |
98 | static boolean constructor_callback PARAMS ((struct bfd_link_info *, | |
99 | boolean constructor, | |
100 | const char *name, | |
101 | bfd *, asection *, bfd_vma)); | |
102 | static boolean warning_callback PARAMS ((struct bfd_link_info *, | |
103 | const char *)); | |
104 | static boolean undefined_symbol PARAMS ((struct bfd_link_info *, | |
105 | const char *, bfd *, | |
106 | asection *, bfd_vma)); | |
107 | static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, | |
108 | const char *, bfd_vma, | |
109 | bfd *, asection *, bfd_vma)); | |
110 | static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *, | |
111 | bfd *, asection *, bfd_vma)); | |
112 | static boolean unattached_reloc PARAMS ((struct bfd_link_info *, | |
113 | const char *, bfd *, asection *, | |
114 | bfd_vma)); | |
115 | static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *, | |
116 | bfd *, asection *, bfd_vma)); | |
117 | ||
118 | static struct bfd_link_callbacks link_callbacks = | |
119 | { | |
120 | add_archive_element, | |
121 | multiple_definition, | |
122 | multiple_common, | |
123 | add_to_set, | |
124 | constructor_callback, | |
125 | warning_callback, | |
126 | undefined_symbol, | |
127 | reloc_overflow, | |
128 | reloc_dangerous, | |
129 | unattached_reloc, | |
130 | notice_ysym | |
131 | }; | |
132 | ||
133 | struct bfd_link_info link_info; | |
134 | \f | |
135 | static void | |
136 | remove_output () | |
137 | { | |
138 | if (output_filename) | |
139 | { | |
140 | if (output_bfd && output_bfd->iostream) | |
141 | fclose((FILE *)(output_bfd->iostream)); | |
142 | if (delete_output_file_on_failure) | |
143 | unlink (output_filename); | |
144 | } | |
145 | } | |
146 | ||
147 | int | |
148 | main (argc, argv) | |
149 | int argc; | |
150 | char **argv; | |
151 | { | |
152 | char *emulation; | |
153 | long start_time = get_run_time (); | |
154 | ||
155 | program_name = argv[0]; | |
156 | xmalloc_set_program_name (program_name); | |
157 | ||
158 | bfd_init (); | |
159 | ||
160 | xatexit (remove_output); | |
161 | ||
162 | /* Initialize the data about options. */ | |
163 | trace_files = trace_file_tries = version_printed = false; | |
164 | whole_archive = false; | |
165 | config.traditional_format = false; | |
166 | config.build_constructors = true; | |
167 | config.dynamic_link = false; | |
168 | command_line.force_common_definition = false; | |
169 | command_line.interpreter = NULL; | |
170 | command_line.rpath = NULL; | |
171 | ||
172 | link_info.callbacks = &link_callbacks; | |
173 | link_info.relocateable = false; | |
174 | link_info.shared = false; | |
175 | link_info.strip = strip_none; | |
176 | link_info.discard = discard_none; | |
177 | link_info.lprefix_len = 1; | |
178 | link_info.lprefix = "L"; | |
179 | link_info.keep_memory = true; | |
180 | link_info.input_bfds = NULL; | |
181 | link_info.create_object_symbols_section = NULL; | |
182 | link_info.hash = NULL; | |
183 | link_info.keep_hash = NULL; | |
184 | link_info.notice_hash = NULL; | |
185 | ||
186 | ldfile_add_arch (""); | |
187 | ||
188 | config.make_executable = true; | |
189 | force_make_executable = false; | |
190 | config.magic_demand_paged = true; | |
191 | config.text_read_only = true; | |
192 | config.make_executable = true; | |
193 | ||
194 | emulation = get_emulation (argc, argv); | |
195 | ldemul_choose_mode (emulation); | |
196 | default_target = ldemul_choose_target (); | |
197 | lang_init (); | |
198 | ldemul_before_parse (); | |
199 | lang_has_input_file = false; | |
200 | parse_args (argc, argv); | |
201 | ||
202 | if (link_info.relocateable) | |
203 | { | |
204 | if (command_line.relax) | |
205 | einfo ("%P%F: -relax and -r may not be used together\n"); | |
206 | if (config.dynamic_link) | |
207 | einfo ("%P%F: -r and -call_shared may not be used together\n"); | |
208 | if (link_info.shared) | |
209 | einfo ("%P%F: -r and -shared may not be used together\n"); | |
210 | } | |
211 | ||
212 | /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I | |
213 | don't see how else this can be handled, since in this case we | |
214 | must preserve all externally visible symbols. */ | |
215 | if (link_info.relocateable && link_info.strip == strip_all) | |
216 | { | |
217 | link_info.strip = strip_debugger; | |
218 | if (link_info.discard == discard_none) | |
219 | link_info.discard = discard_all; | |
220 | } | |
221 | ||
222 | /* This essentially adds another -L directory so this must be done after | |
223 | the -L's in argv have been processed. */ | |
224 | set_scripts_dir (); | |
225 | ||
226 | if (had_script == false) | |
227 | { | |
228 | /* Read the emulation's appropriate default script. */ | |
229 | int isfile; | |
230 | char *s = ldemul_get_script (&isfile); | |
231 | ||
232 | if (isfile) | |
233 | ldfile_open_command_file (s); | |
234 | else | |
235 | { | |
236 | if (trace_file_tries) | |
237 | { | |
238 | info_msg ("using internal linker script:\n"); | |
239 | info_msg ("==================================================\n"); | |
240 | info_msg (s); | |
241 | info_msg ("\n==================================================\n"); | |
242 | } | |
243 | lex_redirect (s); | |
244 | } | |
245 | parser_input = input_script; | |
246 | yyparse (); | |
247 | } | |
248 | ||
249 | lang_final (); | |
250 | ||
251 | if (lang_has_input_file == false) | |
252 | { | |
253 | if (version_printed) | |
254 | xexit (0); | |
255 | einfo ("%P%F: no input files\n"); | |
256 | } | |
257 | ||
258 | if (trace_files) | |
259 | { | |
260 | info_msg ("%P: mode %s\n", emulation); | |
261 | } | |
262 | ||
263 | ldemul_after_parse (); | |
264 | ||
265 | ||
266 | if (config.map_filename) | |
267 | { | |
268 | if (strcmp (config.map_filename, "-") == 0) | |
269 | { | |
270 | config.map_file = stdout; | |
271 | } | |
272 | else | |
273 | { | |
274 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
275 | if (config.map_file == (FILE *) NULL) | |
276 | { | |
277 | einfo ("%P%F: cannot open map file %s: %E\n", | |
278 | config.map_filename); | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | ||
284 | lang_process (); | |
285 | ||
286 | /* Print error messages for any missing symbols, for any warning | |
287 | symbols, and possibly multiple definitions */ | |
288 | ||
289 | ||
290 | if (config.text_read_only) | |
291 | { | |
292 | /* Look for a text section and mark the readonly attribute in it */ | |
293 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); | |
294 | ||
295 | if (found != (asection *) NULL) | |
296 | { | |
297 | found->flags |= SEC_READONLY; | |
298 | } | |
299 | } | |
300 | ||
301 | if (link_info.relocateable || link_info.shared) | |
302 | output_bfd->flags &= ~EXEC_P; | |
303 | else | |
304 | output_bfd->flags |= EXEC_P; | |
305 | ||
306 | ldwrite (); | |
307 | ||
308 | /* Even if we're producing relocateable output, some non-fatal errors should | |
309 | be reported in the exit status. (What non-fatal errors, if any, do we | |
310 | want to ignore for relocateable output?) */ | |
311 | ||
312 | if (config.make_executable == false && force_make_executable == false) | |
313 | { | |
314 | if (trace_files == true) | |
315 | { | |
316 | einfo ("%P: link errors found, deleting executable `%s'\n", | |
317 | output_filename); | |
318 | } | |
319 | ||
320 | if (output_bfd->iostream) | |
321 | fclose ((FILE *) (output_bfd->iostream)); | |
322 | ||
323 | unlink (output_filename); | |
324 | xexit (1); | |
325 | } | |
326 | else | |
327 | { | |
328 | if (! bfd_close (output_bfd)) | |
329 | einfo ("%F%B: final close failed: %E\n", output_bfd); | |
330 | } | |
331 | ||
332 | if (config.stats) | |
333 | { | |
334 | extern char **environ; | |
335 | char *lim = (char *) sbrk (0); | |
336 | long run_time = get_run_time () - start_time; | |
337 | ||
338 | fprintf (stderr, "%s: total time in link: %ld.%06ld\n", | |
339 | program_name, run_time / 1000000, run_time % 1000000); | |
340 | fprintf (stderr, "%s: data size %ld\n", program_name, | |
341 | (long) (lim - (char *) &environ)); | |
342 | } | |
343 | ||
344 | /* Prevent remove_output from doing anything, after a successful link. */ | |
345 | output_filename = NULL; | |
346 | ||
347 | xexit (0); | |
348 | return 0; | |
349 | } | |
350 | ||
351 | /* We need to find any explicitly given emulation in order to initialize the | |
352 | state that's needed by the lex&yacc argument parser (parse_args). */ | |
353 | ||
354 | static char * | |
355 | get_emulation (argc, argv) | |
356 | int argc; | |
357 | char **argv; | |
358 | { | |
359 | char *emulation; | |
360 | int i; | |
361 | ||
362 | emulation = (char *) getenv (EMULATION_ENVIRON); | |
363 | if (emulation == NULL) | |
364 | emulation = DEFAULT_EMULATION; | |
365 | ||
366 | for (i = 1; i < argc; i++) | |
367 | { | |
368 | if (!strncmp (argv[i], "-m", 2)) | |
369 | { | |
370 | if (argv[i][2] == '\0') | |
371 | { | |
372 | /* -m EMUL */ | |
373 | if (i < argc - 1) | |
374 | { | |
375 | emulation = argv[i + 1]; | |
376 | i++; | |
377 | } | |
378 | else | |
379 | { | |
380 | einfo("%P%F: missing argument to -m\n"); | |
381 | } | |
382 | } | |
383 | else if (strcmp (argv[i], "-mips1") == 0 | |
384 | || strcmp (argv[i], "-mips2") == 0 | |
385 | || strcmp (argv[i], "-mips3") == 0) | |
386 | { | |
387 | /* FIXME: The arguments -mips1, -mips2 and -mips3 are | |
388 | passed to the linker by some MIPS compilers. They | |
389 | generally tell the linker to use a slightly different | |
390 | library path. Perhaps someday these should be | |
391 | implemented as emulations; until then, we just ignore | |
392 | the arguments and hope that nobody ever creates | |
393 | emulations named ips1, ips2 or ips3. */ | |
394 | } | |
395 | else if (strcmp (argv[i], "-m486") == 0) | |
396 | { | |
397 | /* FIXME: The argument -m486 is passed to the linker on | |
398 | some Linux systems. Hope that nobody creates an | |
399 | emulation named 486. */ | |
400 | } | |
401 | else | |
402 | { | |
403 | /* -mEMUL */ | |
404 | emulation = &argv[i][2]; | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | return emulation; | |
410 | } | |
411 | ||
412 | /* If directory DIR contains an "ldscripts" subdirectory, | |
413 | add DIR to the library search path and return true, | |
414 | else return false. */ | |
415 | ||
416 | static boolean | |
417 | check_for_scripts_dir (dir) | |
418 | char *dir; | |
419 | { | |
420 | size_t dirlen; | |
421 | char *buf; | |
422 | struct stat s; | |
423 | boolean res; | |
424 | ||
425 | dirlen = strlen (dir); | |
426 | /* sizeof counts the terminating NUL. */ | |
427 | buf = (char *) xmalloc (dirlen + sizeof("/ldscripts")); | |
428 | sprintf (buf, "%s/ldscripts", dir); | |
429 | ||
430 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
431 | free (buf); | |
432 | if (res) | |
433 | ldfile_add_library_path (dir, false); | |
434 | return res; | |
435 | } | |
436 | ||
437 | /* Set the default directory for finding script files. | |
438 | Libraries will be searched for here too, but that's ok. | |
439 | We look for the "ldscripts" directory in: | |
440 | ||
441 | SCRIPTDIR (passed from Makefile) | |
442 | the dir where this program is (for using it from the build tree) | |
443 | the dir where this program is/../lib (for installing the tool suite elsewhere) */ | |
444 | ||
445 | static void | |
446 | set_scripts_dir () | |
447 | { | |
448 | char *end, *dir; | |
449 | size_t dirlen; | |
450 | ||
451 | if (check_for_scripts_dir (SCRIPTDIR)) | |
452 | return; /* We've been installed normally. */ | |
453 | ||
454 | /* Look for "ldscripts" in the dir where our binary is. */ | |
455 | end = strrchr (program_name, '/'); | |
456 | if (end) | |
457 | { | |
458 | dirlen = end - program_name; | |
459 | /* Make a copy of program_name in dir. | |
460 | Leave room for later "/../lib". */ | |
461 | dir = (char *) xmalloc (dirlen + 8); | |
462 | strncpy (dir, program_name, dirlen); | |
463 | dir[dirlen] = '\0'; | |
464 | } | |
465 | else | |
466 | { | |
467 | dirlen = 1; | |
468 | dir = (char *) xmalloc (dirlen + 8); | |
469 | strcpy (dir, "."); | |
470 | } | |
471 | ||
472 | if (check_for_scripts_dir (dir)) | |
473 | return; /* Don't free dir. */ | |
474 | ||
475 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ | |
476 | strcpy (dir + dirlen, "/../lib"); | |
477 | if (check_for_scripts_dir (dir)) | |
478 | return; | |
479 | ||
480 | free (dir); /* Well, we tried. */ | |
481 | } | |
482 | ||
483 | void | |
484 | add_ysym (name) | |
485 | const char *name; | |
486 | { | |
487 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) | |
488 | { | |
489 | link_info.notice_hash = ((struct bfd_hash_table *) | |
490 | xmalloc (sizeof (struct bfd_hash_table))); | |
491 | if (! bfd_hash_table_init_n (link_info.notice_hash, | |
492 | bfd_hash_newfunc, | |
493 | 61)) | |
494 | einfo ("%P%F: bfd_hash_table_init failed: %E\n"); | |
495 | } | |
496 | ||
497 | if (bfd_hash_lookup (link_info.notice_hash, name, true, true) | |
498 | == (struct bfd_hash_entry *) NULL) | |
499 | einfo ("%P%F: bfd_hash_lookup failed: %E\n"); | |
500 | } | |
501 | ||
502 | /* Handle the -retain-symbols-file option. */ | |
503 | ||
504 | void | |
505 | add_keepsyms_file (filename) | |
506 | const char *filename; | |
507 | { | |
508 | FILE *file; | |
509 | char *buf; | |
510 | size_t bufsize; | |
511 | int c; | |
512 | ||
513 | if (link_info.strip == strip_some) | |
514 | einfo ("%X%P: error: duplicate retain-symbols-file\n"); | |
515 | ||
516 | file = fopen (filename, "r"); | |
517 | if (file == (FILE *) NULL) | |
518 | { | |
519 | bfd_set_error (bfd_error_system_call); | |
520 | einfo ("%X%P: %s: %E", filename); | |
521 | return; | |
522 | } | |
523 | ||
524 | link_info.keep_hash = ((struct bfd_hash_table *) | |
525 | xmalloc (sizeof (struct bfd_hash_table))); | |
526 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) | |
527 | einfo ("%P%F: bfd_hash_table_init failed: %E\n"); | |
528 | ||
529 | bufsize = 100; | |
530 | buf = (char *) xmalloc (bufsize); | |
531 | ||
532 | c = getc (file); | |
533 | while (c != EOF) | |
534 | { | |
535 | while (isspace (c)) | |
536 | c = getc (file); | |
537 | ||
538 | if (c != EOF) | |
539 | { | |
540 | size_t len = 0; | |
541 | ||
542 | while (! isspace (c) && c != EOF) | |
543 | { | |
544 | buf[len] = c; | |
545 | ++len; | |
546 | if (len >= bufsize) | |
547 | { | |
548 | bufsize *= 2; | |
549 | buf = xrealloc (buf, bufsize); | |
550 | } | |
551 | c = getc (file); | |
552 | } | |
553 | ||
554 | buf[len] = '\0'; | |
555 | ||
556 | if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) | |
557 | == (struct bfd_hash_entry *) NULL) | |
558 | einfo ("%P%F: bfd_hash_lookup for insertion failed: %E"); | |
559 | } | |
560 | } | |
561 | ||
562 | if (link_info.strip != strip_none) | |
563 | einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"); | |
564 | ||
565 | link_info.strip = strip_some; | |
566 | } | |
567 | \f | |
568 | /* Callbacks from the BFD linker routines. */ | |
569 | ||
570 | /* This is called when BFD has decided to include an archive member in | |
571 | a link. */ | |
572 | ||
573 | /*ARGSUSED*/ | |
574 | static boolean | |
575 | add_archive_element (info, abfd, name) | |
576 | struct bfd_link_info *info; | |
577 | bfd *abfd; | |
578 | const char *name; | |
579 | { | |
580 | lang_input_statement_type *input; | |
581 | ||
582 | input = ((lang_input_statement_type *) | |
583 | xmalloc ((bfd_size_type) sizeof (lang_input_statement_type))); | |
584 | input->filename = abfd->filename; | |
585 | input->local_sym_name = abfd->filename; | |
586 | input->the_bfd = abfd; | |
587 | input->asymbols = NULL; | |
588 | input->next = NULL; | |
589 | input->just_syms_flag = false; | |
590 | input->loaded = false; | |
591 | input->search_dirs_flag = false; | |
592 | ||
593 | /* FIXME: The following fields are not set: header.next, | |
594 | header.type, closed, passive_position, symbol_count, | |
595 | next_real_file, is_archive, target, real, common_section, | |
596 | common_output_section, complained. This bit of code is from the | |
597 | old decode_library_subfile function. I don't know whether any of | |
598 | those fields matters. */ | |
599 | ||
600 | ldlang_add_file (input); | |
601 | ||
602 | if (config.map_file != (FILE *) NULL) | |
603 | minfo ("%s needed due to %T\n", abfd->filename, name); | |
604 | ||
605 | if (trace_files || trace_file_tries) | |
606 | info_msg ("%I\n", input); | |
607 | ||
608 | return true; | |
609 | } | |
610 | ||
611 | /* This is called when BFD has discovered a symbol which is defined | |
612 | multiple times. */ | |
613 | ||
614 | /*ARGSUSED*/ | |
615 | static boolean | |
616 | multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) | |
617 | struct bfd_link_info *info; | |
618 | const char *name; | |
619 | bfd *obfd; | |
620 | asection *osec; | |
621 | bfd_vma oval; | |
622 | bfd *nbfd; | |
623 | asection *nsec; | |
624 | bfd_vma nval; | |
625 | { | |
626 | einfo ("%X%C: multiple definition of `%T'\n", | |
627 | nbfd, nsec, nval, name); | |
628 | if (obfd != (bfd *) NULL) | |
629 | einfo ("%D: first defined here\n", obfd, osec, oval); | |
630 | return true; | |
631 | } | |
632 | ||
633 | /* This is called when there is a definition of a common symbol, or | |
634 | when a common symbol is found for a symbol that is already defined, | |
635 | or when two common symbols are found. We only do something if | |
636 | -warn-common was used. */ | |
637 | ||
638 | /*ARGSUSED*/ | |
639 | static boolean | |
640 | multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) | |
641 | struct bfd_link_info *info; | |
642 | const char *name; | |
643 | bfd *obfd; | |
644 | enum bfd_link_hash_type otype; | |
645 | bfd_vma osize; | |
646 | bfd *nbfd; | |
647 | enum bfd_link_hash_type ntype; | |
648 | bfd_vma nsize; | |
649 | { | |
650 | if (! config.warn_common) | |
651 | return true; | |
652 | ||
653 | if (ntype == bfd_link_hash_defined | |
654 | || ntype == bfd_link_hash_defweak | |
655 | || ntype == bfd_link_hash_indirect) | |
656 | { | |
657 | ASSERT (otype == bfd_link_hash_common); | |
658 | einfo ("%B: warning: definition of `%T' overriding common\n", | |
659 | nbfd, name); | |
660 | if (obfd != NULL) | |
661 | einfo ("%B: warning: common is here\n", obfd); | |
662 | } | |
663 | else if (otype == bfd_link_hash_defined | |
664 | || otype == bfd_link_hash_defweak | |
665 | || otype == bfd_link_hash_indirect) | |
666 | { | |
667 | ASSERT (ntype == bfd_link_hash_common); | |
668 | einfo ("%B: warning: common of `%T' overridden by definition\n", | |
669 | nbfd, name); | |
670 | if (obfd != NULL) | |
671 | einfo ("%B: warning: defined here\n", obfd); | |
672 | } | |
673 | else | |
674 | { | |
675 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
676 | if (osize > nsize) | |
677 | { | |
678 | einfo ("%B: warning: common of `%T' overridden by larger common\n", | |
679 | nbfd, name); | |
680 | if (obfd != NULL) | |
681 | einfo ("%B: warning: larger common is here\n", obfd); | |
682 | } | |
683 | else if (nsize > osize) | |
684 | { | |
685 | einfo ("%B: warning: common of `%T' overriding smaller common\n", | |
686 | nbfd, name); | |
687 | if (obfd != NULL) | |
688 | einfo ("%B: warning: smaller common is here\n", obfd); | |
689 | } | |
690 | else | |
691 | { | |
692 | einfo ("%B: warning: multiple common of `%T'\n", nbfd, name); | |
693 | if (obfd != NULL) | |
694 | einfo ("%B: warning: previous common is here\n", obfd); | |
695 | } | |
696 | } | |
697 | ||
698 | return true; | |
699 | } | |
700 | ||
701 | /* This is called when BFD has discovered a set element. H is the | |
702 | entry in the linker hash table for the set. SECTION and VALUE | |
703 | represent a value which should be added to the set. */ | |
704 | ||
705 | /*ARGSUSED*/ | |
706 | static boolean | |
707 | add_to_set (info, h, reloc, abfd, section, value) | |
708 | struct bfd_link_info *info; | |
709 | struct bfd_link_hash_entry *h; | |
710 | bfd_reloc_code_real_type reloc; | |
711 | bfd *abfd; | |
712 | asection *section; | |
713 | bfd_vma value; | |
714 | { | |
715 | if (! config.build_constructors) | |
716 | return true; | |
717 | ||
718 | ldctor_add_set_entry (h, reloc, section, value); | |
719 | ||
720 | if (h->type == bfd_link_hash_new) | |
721 | { | |
722 | h->type = bfd_link_hash_undefined; | |
723 | h->u.undef.abfd = abfd; | |
724 | /* We don't call bfd_link_add_undef to add this to the list of | |
725 | undefined symbols because we are going to define it | |
726 | ourselves. */ | |
727 | } | |
728 | ||
729 | return true; | |
730 | } | |
731 | ||
732 | /* This is called when BFD has discovered a constructor. This is only | |
733 | called for some object file formats--those which do not handle | |
734 | constructors in some more clever fashion. This is similar to | |
735 | adding an element to a set, but less general. */ | |
736 | ||
737 | static boolean | |
738 | constructor_callback (info, constructor, name, abfd, section, value) | |
739 | struct bfd_link_info *info; | |
740 | boolean constructor; | |
741 | const char *name; | |
742 | bfd *abfd; | |
743 | asection *section; | |
744 | bfd_vma value; | |
745 | { | |
746 | char *set_name; | |
747 | char *s; | |
748 | struct bfd_link_hash_entry *h; | |
749 | ||
750 | if (! config.build_constructors) | |
751 | return true; | |
752 | ||
753 | /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a | |
754 | useful error message. */ | |
755 | if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL) | |
756 | einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported"); | |
757 | ||
758 | set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__"); | |
759 | s = set_name; | |
760 | if (bfd_get_symbol_leading_char (abfd) != '\0') | |
761 | *s++ = bfd_get_symbol_leading_char (abfd); | |
762 | if (constructor) | |
763 | strcpy (s, "__CTOR_LIST__"); | |
764 | else | |
765 | strcpy (s, "__DTOR_LIST__"); | |
766 | ||
767 | if (config.map_file != (FILE *) NULL) | |
768 | fprintf (config.map_file, | |
769 | "Adding %s to constructor/destructor set %s\n", name, set_name); | |
770 | ||
771 | h = bfd_link_hash_lookup (info->hash, set_name, true, true, true); | |
772 | if (h == (struct bfd_link_hash_entry *) NULL) | |
773 | einfo ("%P%F: bfd_link_hash_lookup failed: %E"); | |
774 | if (h->type == bfd_link_hash_new) | |
775 | { | |
776 | h->type = bfd_link_hash_undefined; | |
777 | h->u.undef.abfd = abfd; | |
778 | /* We don't call bfd_link_add_undef to add this to the list of | |
779 | undefined symbols because we are going to define it | |
780 | ourselves. */ | |
781 | } | |
782 | ||
783 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value); | |
784 | return true; | |
785 | } | |
786 | ||
787 | /* This is called when there is a reference to a warning symbol. */ | |
788 | ||
789 | /*ARGSUSED*/ | |
790 | static boolean | |
791 | warning_callback (info, warning) | |
792 | struct bfd_link_info *info; | |
793 | const char *warning; | |
794 | { | |
795 | einfo ("%P: %s\n", warning); | |
796 | return true; | |
797 | } | |
798 | ||
799 | /* This is called when an undefined symbol is found. */ | |
800 | ||
801 | /*ARGSUSED*/ | |
802 | static boolean | |
803 | undefined_symbol (info, name, abfd, section, address) | |
804 | struct bfd_link_info *info; | |
805 | const char *name; | |
806 | bfd *abfd; | |
807 | asection *section; | |
808 | bfd_vma address; | |
809 | { | |
810 | static char *error_name; | |
811 | static unsigned int error_count; | |
812 | ||
813 | #define MAX_ERRORS_IN_A_ROW 5 | |
814 | ||
815 | if (config.warn_once) | |
816 | { | |
817 | static struct bfd_hash_table *hash; | |
818 | ||
819 | /* Only warn once about a particular undefined symbol. */ | |
820 | ||
821 | if (hash == NULL) | |
822 | { | |
823 | hash = ((struct bfd_hash_table *) | |
824 | xmalloc (sizeof (struct bfd_hash_table))); | |
825 | if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) | |
826 | einfo ("%F%P: bfd_hash_table_init failed: %E\n"); | |
827 | } | |
828 | ||
829 | if (bfd_hash_lookup (hash, name, false, false) != NULL) | |
830 | return true; | |
831 | ||
832 | if (bfd_hash_lookup (hash, name, true, true) == NULL) | |
833 | einfo ("%F%P: bfd_hash_lookup failed: %E\n"); | |
834 | } | |
835 | ||
836 | /* We never print more than a reasonable number of errors in a row | |
837 | for a single symbol. */ | |
838 | if (error_name != (char *) NULL | |
839 | && strcmp (name, error_name) == 0) | |
840 | ++error_count; | |
841 | else | |
842 | { | |
843 | error_count = 0; | |
844 | if (error_name != (char *) NULL) | |
845 | free (error_name); | |
846 | error_name = buystring (name); | |
847 | } | |
848 | ||
849 | if (section != NULL) | |
850 | { | |
851 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
852 | einfo ("%X%C: undefined reference to `%T'\n", | |
853 | abfd, section, address, name); | |
854 | else if (error_count == MAX_ERRORS_IN_A_ROW) | |
855 | einfo ("%D: more undefined references to `%T' follow\n", | |
856 | abfd, section, address, name); | |
857 | } | |
858 | else | |
859 | { | |
860 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
861 | einfo ("%X%B: undefined reference to `%T'\n", | |
862 | abfd, name); | |
863 | else if (error_count == MAX_ERRORS_IN_A_ROW) | |
864 | einfo ("%B: more undefined references to `%T' follow\n", | |
865 | abfd, name); | |
866 | } | |
867 | ||
868 | return true; | |
869 | } | |
870 | ||
871 | /* This is called when a reloc overflows. */ | |
872 | ||
873 | /*ARGSUSED*/ | |
874 | static boolean | |
875 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) | |
876 | struct bfd_link_info *info; | |
877 | const char *name; | |
878 | const char *reloc_name; | |
879 | bfd_vma addend; | |
880 | bfd *abfd; | |
881 | asection *section; | |
882 | bfd_vma address; | |
883 | { | |
884 | if (abfd == (bfd *) NULL) | |
885 | einfo ("%P%X: generated"); | |
886 | else | |
887 | einfo ("%X%C:", abfd, section, address); | |
888 | einfo (" relocation truncated to fit: %s %T", reloc_name, name); | |
889 | if (addend != 0) | |
890 | einfo ("+%v", addend); | |
891 | einfo ("\n"); | |
892 | return true; | |
893 | } | |
894 | ||
895 | /* This is called when a dangerous relocation is made. */ | |
896 | ||
897 | /*ARGSUSED*/ | |
898 | static boolean | |
899 | reloc_dangerous (info, message, abfd, section, address) | |
900 | struct bfd_link_info *info; | |
901 | const char *message; | |
902 | bfd *abfd; | |
903 | asection *section; | |
904 | bfd_vma address; | |
905 | { | |
906 | if (abfd == (bfd *) NULL) | |
907 | einfo ("%P%X: generated"); | |
908 | else | |
909 | einfo ("%X%C:", abfd, section, address); | |
910 | einfo ("dangerous relocation: %s\n", message); | |
911 | return true; | |
912 | } | |
913 | ||
914 | /* This is called when a reloc is being generated attached to a symbol | |
915 | that is not being output. */ | |
916 | ||
917 | /*ARGSUSED*/ | |
918 | static boolean | |
919 | unattached_reloc (info, name, abfd, section, address) | |
920 | struct bfd_link_info *info; | |
921 | const char *name; | |
922 | bfd *abfd; | |
923 | asection *section; | |
924 | bfd_vma address; | |
925 | { | |
926 | if (abfd == (bfd *) NULL) | |
927 | einfo ("%P%X: generated"); | |
928 | else | |
929 | einfo ("%X%C:", abfd, section, address); | |
930 | einfo (" reloc refers to symbol `%T' which is not being output\n", name); | |
931 | return true; | |
932 | } | |
933 | ||
934 | /* This is called when a symbol in notice_hash is found. Symbols are | |
935 | put in notice_hash using the -y option. */ | |
936 | ||
937 | /*ARGSUSED*/ | |
938 | static boolean | |
939 | notice_ysym (info, name, abfd, section, value) | |
940 | struct bfd_link_info *info; | |
941 | const char *name; | |
942 | bfd *abfd; | |
943 | asection *section; | |
944 | bfd_vma value; | |
945 | { | |
946 | einfo ("%B: %s %s\n", abfd, | |
947 | bfd_is_und_section (section) ? "reference to" : "definition of", | |
948 | name); | |
949 | return true; | |
950 | } |