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