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