]>
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 [email protected] |
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 | ||
f2f55b16 ILT |
75 | /* Nonzero means link in every member of an archive. */ |
76 | boolean whole_archive; | |
77 | ||
2fa0b342 | 78 | args_type command_line; |
173a0c3d | 79 | |
2fa0b342 | 80 | ld_config_type config; |
9f629407 ILT |
81 | |
82 | static boolean check_for_scripts_dir PARAMS ((char *dir)); | |
b9395be3 ILT |
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 *, | |
2a9fa50c | 96 | bfd_reloc_code_real_type, |
b9395be3 ILT |
97 | bfd *, asection *, bfd_vma)); |
98 | static boolean constructor_callback PARAMS ((struct bfd_link_info *, | |
99 | boolean constructor, | |
b9395be3 ILT |
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)); | |
5dad4c97 ILT |
107 | static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, |
108 | const char *, bfd_vma, | |
109 | bfd *, asection *, bfd_vma)); | |
b9395be3 ILT |
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; | |
173a0c3d | 134 | \f |
0b2f8d2e DM |
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 | ||
9f629407 | 147 | int |
2fa0b342 | 148 | main (argc, argv) |
2fa0b342 | 149 | int argc; |
9f629407 | 150 | char **argv; |
2fa0b342 DHW |
151 | { |
152 | char *emulation; | |
8594f568 | 153 | long start_time = get_run_time (); |
e47bfa63 | 154 | |
2fa0b342 | 155 | program_name = argv[0]; |
5bcb7f28 | 156 | xmalloc_set_program_name (program_name); |
99fe4553 | 157 | |
e47bfa63 | 158 | bfd_init (); |
bfbdc80f | 159 | |
5bcb7f28 | 160 | xatexit (remove_output); |
0b2f8d2e | 161 | |
2fa0b342 | 162 | /* Initialize the data about options. */ |
7b40f2b1 | 163 | trace_files = trace_file_tries = version_printed = false; |
f2f55b16 | 164 | whole_archive = false; |
c96386c4 | 165 | config.traditional_format = false; |
b9395be3 | 166 | config.build_constructors = true; |
2a9fa50c | 167 | config.dynamic_link = false; |
2fa0b342 | 168 | command_line.force_common_definition = false; |
7fb9ca5f | 169 | command_line.interpreter = NULL; |
cc23cc69 | 170 | command_line.rpath = NULL; |
2fa0b342 | 171 | |
b9395be3 ILT |
172 | link_info.callbacks = &link_callbacks; |
173 | link_info.relocateable = false; | |
64887de2 | 174 | link_info.shared = false; |
b9395be3 ILT |
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 | ||
e47bfa63 | 186 | ldfile_add_arch (""); |
a72f4e5f | 187 | |
2fa0b342 DHW |
188 | config.make_executable = true; |
189 | force_make_executable = false; | |
ce4d59e2 SC |
190 | config.magic_demand_paged = true; |
191 | config.text_read_only = true; | |
2fa0b342 | 192 | config.make_executable = true; |
1418c83b | 193 | |
d723cd17 | 194 | emulation = get_emulation (argc, argv); |
e47bfa63 SC |
195 | ldemul_choose_mode (emulation); |
196 | default_target = ldemul_choose_target (); | |
197 | lang_init (); | |
198 | ldemul_before_parse (); | |
2fa0b342 | 199 | lang_has_input_file = false; |
e47bfa63 | 200 | parse_args (argc, argv); |
f3739bc3 | 201 | |
2a9fa50c ILT |
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"); | |
64887de2 ILT |
208 | if (link_info.shared) |
209 | einfo ("%P%F: -r and -shared may not be used together\n"); | |
2a9fa50c ILT |
210 | } |
211 | ||
8ed88239 ILT |
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 | ||
973e421e ILT |
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 | ||
bbd2521f DM |
226 | if (had_script == false) |
227 | { | |
228 | /* Read the emulation's appropriate default script. */ | |
2a28d8b0 DM |
229 | int isfile; |
230 | char *s = ldemul_get_script (&isfile); | |
231 | ||
232 | if (isfile) | |
d4e5e3c3 | 233 | ldfile_open_command_file (s); |
2a28d8b0 | 234 | else |
22c41f00 ILT |
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 | } | |
d4e5e3c3 DM |
245 | parser_input = input_script; |
246 | yyparse (); | |
bbd2521f DM |
247 | } |
248 | ||
e47bfa63 | 249 | lang_final (); |
9d1fe8a4 | 250 | |
e47bfa63 SC |
251 | if (lang_has_input_file == false) |
252 | { | |
7b40f2b1 | 253 | if (version_printed) |
0b2f8d2e | 254 | xexit (0); |
973e421e | 255 | einfo ("%P%F: no input files\n"); |
870f54b2 | 256 | } |
2fa0b342 | 257 | |
bbd2521f DM |
258 | if (trace_files) |
259 | { | |
973e421e | 260 | info_msg ("%P: mode %s\n", emulation); |
bbd2521f DM |
261 | } |
262 | ||
e47bfa63 | 263 | ldemul_after_parse (); |
870f54b2 | 264 | |
9d1fe8a4 | 265 | |
e47bfa63 | 266 | if (config.map_filename) |
870f54b2 | 267 | { |
e47bfa63 | 268 | if (strcmp (config.map_filename, "-") == 0) |
2e2bf962 | 269 | { |
e47bfa63 | 270 | config.map_file = stdout; |
2e2bf962 | 271 | } |
e47bfa63 SC |
272 | else |
273 | { | |
274 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
275 | if (config.map_file == (FILE *) NULL) | |
276 | { | |
bbd2521f | 277 | einfo ("%P%F: cannot open map file %s: %E\n", |
e47bfa63 SC |
278 | config.map_filename); |
279 | } | |
280 | } | |
281 | } | |
870f54b2 | 282 | |
2e2bf962 | 283 | |
e47bfa63 | 284 | lang_process (); |
2fa0b342 | 285 | |
2fa0b342 DHW |
286 | /* Print error messages for any missing symbols, for any warning |
287 | symbols, and possibly multiple definitions */ | |
288 | ||
2fa0b342 | 289 | |
e47bfa63 SC |
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 | } | |
3e4c643d | 299 | } |
2fa0b342 | 300 | |
22c41f00 | 301 | if (link_info.relocateable || link_info.shared) |
f3739bc3 | 302 | output_bfd->flags &= ~EXEC_P; |
a72f4e5f | 303 | else |
f3739bc3 | 304 | output_bfd->flags |= EXEC_P; |
b257477f | 305 | |
f3739bc3 | 306 | ldwrite (); |
b257477f | 307 | |
f3739bc3 SC |
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?) */ | |
a72f4e5f | 311 | |
f3739bc3 SC |
312 | if (config.make_executable == false && force_make_executable == false) |
313 | { | |
314 | if (trace_files == true) | |
e47bfa63 | 315 | { |
973e421e | 316 | einfo ("%P: link errors found, deleting executable `%s'\n", |
f3739bc3 | 317 | output_filename); |
e47bfa63 | 318 | } |
a72f4e5f | 319 | |
f3739bc3 SC |
320 | if (output_bfd->iostream) |
321 | fclose ((FILE *) (output_bfd->iostream)); | |
a72f4e5f | 322 | |
f3739bc3 | 323 | unlink (output_filename); |
0b2f8d2e | 324 | xexit (1); |
f3739bc3 SC |
325 | } |
326 | else | |
327 | { | |
2a9fa50c ILT |
328 | if (! bfd_close (output_bfd)) |
329 | einfo ("%F%B: final close failed: %E\n", output_bfd); | |
f3739bc3 | 330 | } |
2fa0b342 | 331 | |
b9395be3 ILT |
332 | if (config.stats) |
333 | { | |
334 | extern char **environ; | |
335 | char *lim = (char *) sbrk (0); | |
8594f568 | 336 | long run_time = get_run_time () - start_time; |
b9395be3 | 337 | |
5bcb7f28 | 338 | fprintf (stderr, "%s: total time in link: %ld.%06ld\n", |
8594f568 | 339 | program_name, run_time / 1000000, run_time % 1000000); |
b9395be3 ILT |
340 | fprintf (stderr, "%s: data size %ld\n", program_name, |
341 | (long) (lim - (char *) &environ)); | |
342 | } | |
343 | ||
52a8ebfe DM |
344 | /* Prevent remove_output from doing anything, after a successful link. */ |
345 | output_filename = NULL; | |
346 | ||
0b2f8d2e | 347 | xexit (0); |
9f629407 | 348 | return 0; |
d723cd17 DM |
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 | ||
d723cd17 DM |
362 | emulation = (char *) getenv (EMULATION_ENVIRON); |
363 | if (emulation == NULL) | |
364 | emulation = DEFAULT_EMULATION; | |
d723cd17 DM |
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 | { | |
973e421e | 380 | einfo("%P%F: missing argument to -m\n"); |
d723cd17 DM |
381 | } |
382 | } | |
973e421e ILT |
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 | } | |
cbbf9608 ILT |
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 | } | |
d723cd17 DM |
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. */ | |
0b2f8d2e | 427 | buf = (char *) xmalloc (dirlen + sizeof("/ldscripts")); |
d723cd17 DM |
428 | sprintf (buf, "%s/ldscripts", dir); |
429 | ||
430 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
f4208462 | 431 | free (buf); |
d723cd17 | 432 | if (res) |
0cd82d00 | 433 | ldfile_add_library_path (dir, false); |
d723cd17 DM |
434 | return res; |
435 | } | |
436 | ||
437 | /* Set the default directory for finding script files. | |
f4208462 DM |
438 | Libraries will be searched for here too, but that's ok. |
439 | We look for the "ldscripts" directory in: | |
440 | ||
f4208462 | 441 | SCRIPTDIR (passed from Makefile) |
bbd2521f DM |
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) */ | |
d723cd17 DM |
444 | |
445 | static void | |
446 | set_scripts_dir () | |
447 | { | |
f4208462 DM |
448 | char *end, *dir; |
449 | size_t dirlen; | |
450 | ||
d723cd17 | 451 | if (check_for_scripts_dir (SCRIPTDIR)) |
f4208462 | 452 | return; /* We've been installed normally. */ |
d723cd17 DM |
453 | |
454 | /* Look for "ldscripts" in the dir where our binary is. */ | |
455 | end = strrchr (program_name, '/'); | |
bbd2521f DM |
456 | if (end) |
457 | { | |
458 | dirlen = end - program_name; | |
459 | /* Make a copy of program_name in dir. | |
460 | Leave room for later "/../lib". */ | |
0b2f8d2e | 461 | dir = (char *) xmalloc (dirlen + 8); |
bbd2521f DM |
462 | strncpy (dir, program_name, dirlen); |
463 | dir[dirlen] = '\0'; | |
464 | } | |
465 | else | |
466 | { | |
467 | dirlen = 1; | |
0b2f8d2e | 468 | dir = (char *) xmalloc (dirlen + 8); |
bbd2521f DM |
469 | strcpy (dir, "."); |
470 | } | |
f4208462 | 471 | |
f4208462 DM |
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. */ | |
d723cd17 | 481 | } |
2fa0b342 | 482 | |
e47bfa63 | 483 | void |
b9395be3 ILT |
484 | add_ysym (name) |
485 | const char *name; | |
2fa0b342 | 486 | { |
b9395be3 ILT |
487 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) |
488 | { | |
489 | link_info.notice_hash = ((struct bfd_hash_table *) | |
0b2f8d2e | 490 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 ILT |
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"); | |
2fa0b342 | 500 | } |
e47bfa63 | 501 | |
b9395be3 | 502 | /* Handle the -retain-symbols-file option. */ |
2fa0b342 | 503 | |
2fa0b342 | 504 | void |
b9395be3 ILT |
505 | add_keepsyms_file (filename) |
506 | const char *filename; | |
2fa0b342 | 507 | { |
b9395be3 ILT |
508 | FILE *file; |
509 | char *buf; | |
510 | size_t bufsize; | |
511 | int c; | |
2fa0b342 | 512 | |
b9395be3 ILT |
513 | if (link_info.strip == strip_some) |
514 | einfo ("%X%P: error: duplicate retain-symbols-file\n"); | |
3e4c643d | 515 | |
b9395be3 ILT |
516 | file = fopen (filename, "r"); |
517 | if (file == (FILE *) NULL) | |
e47bfa63 | 518 | { |
5bcb7f28 | 519 | bfd_set_error (bfd_error_system_call); |
b9395be3 ILT |
520 | einfo ("%X%P: %s: %E", filename); |
521 | return; | |
be1627d3 | 522 | } |
be1627d3 | 523 | |
b9395be3 | 524 | link_info.keep_hash = ((struct bfd_hash_table *) |
0b2f8d2e | 525 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 ILT |
526 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) |
527 | einfo ("%P%F: bfd_hash_table_init failed: %E\n"); | |
2fa0b342 | 528 | |
b9395be3 | 529 | bufsize = 100; |
0b2f8d2e | 530 | buf = (char *) xmalloc (bufsize); |
c611e285 | 531 | |
b9395be3 ILT |
532 | c = getc (file); |
533 | while (c != EOF) | |
29f33467 | 534 | { |
b9395be3 ILT |
535 | while (isspace (c)) |
536 | c = getc (file); | |
e47bfa63 | 537 | |
b9395be3 | 538 | if (c != EOF) |
29f33467 | 539 | { |
b9395be3 | 540 | size_t len = 0; |
8a045e50 | 541 | |
b9395be3 | 542 | while (! isspace (c) && c != EOF) |
29f33467 | 543 | { |
b9395be3 ILT |
544 | buf[len] = c; |
545 | ++len; | |
546 | if (len >= bufsize) | |
29f33467 | 547 | { |
b9395be3 | 548 | bufsize *= 2; |
0b2f8d2e | 549 | buf = xrealloc (buf, bufsize); |
29f33467 | 550 | } |
b9395be3 | 551 | c = getc (file); |
29f33467 | 552 | } |
81016051 | 553 | |
b9395be3 ILT |
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"); | |
29f33467 | 559 | } |
e47bfa63 | 560 | } |
2fa0b342 | 561 | |
b9395be3 ILT |
562 | if (link_info.strip != strip_none) |
563 | einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"); | |
8a045e50 | 564 | |
b9395be3 ILT |
565 | link_info.strip = strip_some; |
566 | } | |
567 | \f | |
568 | /* Callbacks from the BFD linker routines. */ | |
2fa0b342 | 569 | |
b9395be3 ILT |
570 | /* This is called when BFD has decided to include an archive member in |
571 | a link. */ | |
2fa0b342 | 572 | |
b9395be3 ILT |
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; | |
2fa0b342 | 579 | { |
b9395be3 ILT |
580 | lang_input_statement_type *input; |
581 | ||
582 | input = ((lang_input_statement_type *) | |
0b2f8d2e | 583 | xmalloc ((bfd_size_type) sizeof (lang_input_statement_type))); |
b9395be3 ILT |
584 | input->filename = abfd->filename; |
585 | input->local_sym_name = abfd->filename; | |
586 | input->the_bfd = abfd; | |
587 | input->asymbols = NULL; | |
b9395be3 ILT |
588 | input->next = NULL; |
589 | input->just_syms_flag = false; | |
590 | input->loaded = false; | |
210c52ac | 591 | input->search_dirs_flag = false; |
b9395be3 ILT |
592 | |
593 | /* FIXME: The following fields are not set: header.next, | |
210c52ac ILT |
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. */ | |
b9395be3 ILT |
599 | |
600 | ldlang_add_file (input); | |
601 | ||
2a9fa50c ILT |
602 | if (config.map_file != (FILE *) NULL) |
603 | minfo ("%s needed due to %T\n", abfd->filename, name); | |
b9395be3 | 604 | |
5dad4c97 ILT |
605 | if (trace_files || trace_file_tries) |
606 | info_msg ("%I\n", input); | |
607 | ||
b9395be3 ILT |
608 | return true; |
609 | } | |
2fa0b342 | 610 | |
b9395be3 ILT |
611 | /* This is called when BFD has discovered a symbol which is defined |
612 | multiple times. */ | |
2fa0b342 | 613 | |
b9395be3 ILT |
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) | |
cc23cc69 | 629 | einfo ("%D: first defined here\n", obfd, osec, oval); |
b9395be3 | 630 | return true; |
2fa0b342 DHW |
631 | } |
632 | ||
b9395be3 ILT |
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; | |
99fe4553 | 649 | { |
b9395be3 ILT |
650 | if (! config.warn_common) |
651 | return true; | |
99fe4553 | 652 | |
8ed88239 ILT |
653 | if (ntype == bfd_link_hash_defined |
654 | || ntype == bfd_link_hash_defweak | |
655 | || ntype == bfd_link_hash_indirect) | |
e47bfa63 | 656 | { |
b9395be3 ILT |
657 | ASSERT (otype == bfd_link_hash_common); |
658 | einfo ("%B: warning: definition of `%T' overriding common\n", | |
659 | nbfd, name); | |
8ed88239 ILT |
660 | if (obfd != NULL) |
661 | einfo ("%B: warning: common is here\n", obfd); | |
e47bfa63 | 662 | } |
8ed88239 ILT |
663 | else if (otype == bfd_link_hash_defined |
664 | || otype == bfd_link_hash_defweak | |
665 | || otype == bfd_link_hash_indirect) | |
2fa0b342 | 666 | { |
b9395be3 ILT |
667 | ASSERT (ntype == bfd_link_hash_common); |
668 | einfo ("%B: warning: common of `%T' overridden by definition\n", | |
669 | nbfd, name); | |
8ed88239 ILT |
670 | if (obfd != NULL) |
671 | einfo ("%B: warning: defined here\n", obfd); | |
b9395be3 ILT |
672 | } |
673 | else | |
674 | { | |
675 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
676 | if (osize > nsize) | |
2fa0b342 | 677 | { |
b9395be3 ILT |
678 | einfo ("%B: warning: common of `%T' overridden by larger common\n", |
679 | nbfd, name); | |
8ed88239 ILT |
680 | if (obfd != NULL) |
681 | einfo ("%B: warning: larger common is here\n", obfd); | |
2fa0b342 | 682 | } |
b9395be3 | 683 | else if (nsize > osize) |
2fa0b342 | 684 | { |
b9395be3 ILT |
685 | einfo ("%B: warning: common of `%T' overriding smaller common\n", |
686 | nbfd, name); | |
8ed88239 ILT |
687 | if (obfd != NULL) |
688 | einfo ("%B: warning: smaller common is here\n", obfd); | |
2fa0b342 | 689 | } |
e47bfa63 | 690 | else |
2fa0b342 | 691 | { |
b9395be3 | 692 | einfo ("%B: warning: multiple common of `%T'\n", nbfd, name); |
8ed88239 ILT |
693 | if (obfd != NULL) |
694 | einfo ("%B: warning: previous common is here\n", obfd); | |
2fa0b342 DHW |
695 | } |
696 | } | |
697 | ||
b9395be3 | 698 | return true; |
2fa0b342 DHW |
699 | } |
700 | ||
b9395be3 ILT |
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. */ | |
2fa0b342 | 704 | |
b9395be3 ILT |
705 | /*ARGSUSED*/ |
706 | static boolean | |
2a9fa50c | 707 | add_to_set (info, h, reloc, abfd, section, value) |
b9395be3 ILT |
708 | struct bfd_link_info *info; |
709 | struct bfd_link_hash_entry *h; | |
2a9fa50c | 710 | bfd_reloc_code_real_type reloc; |
b9395be3 ILT |
711 | bfd *abfd; |
712 | asection *section; | |
713 | bfd_vma value; | |
2fa0b342 | 714 | { |
2a9fa50c ILT |
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 | ||
b9395be3 ILT |
729 | return true; |
730 | } | |
1418c83b | 731 | |
b9395be3 ILT |
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. */ | |
2fa0b342 | 736 | |
b9395be3 | 737 | static boolean |
2a9fa50c | 738 | constructor_callback (info, constructor, name, abfd, section, value) |
b9395be3 ILT |
739 | struct bfd_link_info *info; |
740 | boolean constructor; | |
b9395be3 ILT |
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 | ||
2a9fa50c ILT |
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 | ||
b9395be3 ILT |
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__"); | |
29f33467 | 764 | else |
b9395be3 | 765 | strcpy (s, "__DTOR_LIST__"); |
2fa0b342 | 766 | |
2a9fa50c ILT |
767 | if (config.map_file != (FILE *) NULL) |
768 | fprintf (config.map_file, | |
769 | "Adding %s to constructor/destructor set %s\n", name, set_name); | |
e47bfa63 | 770 | |
b9395be3 ILT |
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) | |
e47bfa63 | 775 | { |
b9395be3 ILT |
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. */ | |
2fa0b342 | 781 | } |
2fa0b342 | 782 | |
2a9fa50c | 783 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value); |
b9395be3 | 784 | return true; |
2fa0b342 DHW |
785 | } |
786 | ||
b9395be3 | 787 | /* This is called when there is a reference to a warning symbol. */ |
2fa0b342 | 788 | |
b9395be3 ILT |
789 | /*ARGSUSED*/ |
790 | static boolean | |
791 | warning_callback (info, warning) | |
792 | struct bfd_link_info *info; | |
793 | const char *warning; | |
2fa0b342 | 794 | { |
b9395be3 ILT |
795 | einfo ("%P: %s\n", warning); |
796 | return true; | |
2fa0b342 DHW |
797 | } |
798 | ||
b9395be3 | 799 | /* This is called when an undefined symbol is found. */ |
2fa0b342 | 800 | |
b9395be3 ILT |
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; | |
29f33467 | 812 | |
b9395be3 | 813 | #define MAX_ERRORS_IN_A_ROW 5 |
8a045e50 | 814 | |
809ee7e0 ILT |
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 | ||
b9395be3 ILT |
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 | |
e47bfa63 | 842 | { |
b9395be3 ILT |
843 | error_count = 0; |
844 | if (error_name != (char *) NULL) | |
845 | free (error_name); | |
846 | error_name = buystring (name); | |
847 | } | |
2fa0b342 | 848 | |
23244cd6 ILT |
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 | } | |
2fa0b342 | 867 | |
b9395be3 | 868 | return true; |
2fa0b342 DHW |
869 | } |
870 | ||
b9395be3 | 871 | /* This is called when a reloc overflows. */ |
2fa0b342 | 872 | |
b9395be3 | 873 | /*ARGSUSED*/ |
9f629407 | 874 | static boolean |
5dad4c97 | 875 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) |
b9395be3 | 876 | struct bfd_link_info *info; |
5dad4c97 ILT |
877 | const char *name; |
878 | const char *reloc_name; | |
879 | bfd_vma addend; | |
b9395be3 ILT |
880 | bfd *abfd; |
881 | asection *section; | |
882 | bfd_vma address; | |
2fa0b342 | 883 | { |
2a9fa50c ILT |
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); | |
5dad4c97 ILT |
889 | if (addend != 0) |
890 | einfo ("+%v", addend); | |
891 | einfo ("\n"); | |
b9395be3 ILT |
892 | return true; |
893 | } | |
2fa0b342 | 894 | |
b9395be3 | 895 | /* This is called when a dangerous relocation is made. */ |
3e4c643d | 896 | |
b9395be3 ILT |
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 | { | |
2a9fa50c ILT |
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); | |
b9395be3 ILT |
911 | return true; |
912 | } | |
973e421e | 913 | |
b9395be3 ILT |
914 | /* This is called when a reloc is being generated attached to a symbol |
915 | that is not being output. */ | |
2fa0b342 | 916 | |
b9395be3 ILT |
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 | { | |
2a9fa50c ILT |
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); | |
b9395be3 | 931 | return true; |
2fa0b342 | 932 | } |
8a045e50 | 933 | |
b9395be3 ILT |
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; | |
8a045e50 | 945 | { |
b9395be3 | 946 | einfo ("%B: %s %s\n", abfd, |
cc23cc69 | 947 | bfd_is_und_section (section) ? "reference to" : "definition of", |
b9395be3 ILT |
948 | name); |
949 | return true; | |
8a045e50 | 950 | } |