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