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