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