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