]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Main program of GNU linker. |
4e5bae56 | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
cdc0d45e | 3 | 2002, 2003 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Steve Chamberlain steve@cygnus.com | |
6 | ||
2fa9fc65 | 7 | This file is part of GLD, the Gnu Linker. |
252b5132 | 8 | |
2fa9fc65 NC |
9 | GLD is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
252b5132 | 13 | |
2fa9fc65 NC |
14 | GLD is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
252b5132 | 18 | |
2fa9fc65 NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with GLD; see the file COPYING. If not, write to the Free | |
21 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
22 | 02111-1307, USA. */ | |
252b5132 RH |
23 | |
24 | #include "bfd.h" | |
25 | #include "sysdep.h" | |
26 | #include <stdio.h> | |
3882b010 | 27 | #include "safe-ctype.h" |
252b5132 RH |
28 | #include "libiberty.h" |
29 | #include "progress.h" | |
30 | #include "bfdlink.h" | |
5af11cab | 31 | #include "filenames.h" |
252b5132 RH |
32 | |
33 | #include "ld.h" | |
34 | #include "ldmain.h" | |
35 | #include "ldmisc.h" | |
36 | #include "ldwrite.h" | |
252b5132 RH |
37 | #include "ldexp.h" |
38 | #include "ldlang.h" | |
df2a7313 | 39 | #include <ldgram.h> |
252b5132 RH |
40 | #include "ldlex.h" |
41 | #include "ldfile.h" | |
b71e2778 | 42 | #include "ldemul.h" |
252b5132 RH |
43 | #include "ldctor.h" |
44 | ||
0125cdf2 | 45 | /* Somewhere above, sys/stat.h got included. */ |
252b5132 RH |
46 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
47 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
48 | #endif | |
49 | ||
50 | #include <string.h> | |
51 | ||
52 | #ifdef HAVE_SBRK | |
53 | #ifdef NEED_DECLARATION_SBRK | |
54 | extern PTR sbrk (); | |
55 | #endif | |
56 | #endif | |
57 | ||
9c8ebd6a DJ |
58 | #ifndef TARGET_SYSTEM_ROOT |
59 | #define TARGET_SYSTEM_ROOT "" | |
60 | #endif | |
61 | ||
4e5bae56 DJ |
62 | int main PARAMS ((int, char **)); |
63 | ||
252b5132 RH |
64 | /* EXPORTS */ |
65 | ||
66 | char *default_target; | |
67 | const char *output_filename = "a.out"; | |
68 | ||
69 | /* Name this program was invoked by. */ | |
70 | char *program_name; | |
71 | ||
9c8ebd6a DJ |
72 | /* The prefix for system library directories. */ |
73 | char *ld_sysroot; | |
74 | ||
6d5e62f8 | 75 | /* The file that we're creating. */ |
252b5132 RH |
76 | bfd *output_bfd = 0; |
77 | ||
78 | /* Set by -G argument, for MIPS ECOFF target. */ | |
79 | int g_switch_value = 8; | |
80 | ||
81 | /* Nonzero means print names of input files as processed. */ | |
b34976b6 | 82 | bfd_boolean trace_files; |
252b5132 RH |
83 | |
84 | /* Nonzero means same, but note open failures, too. */ | |
b34976b6 | 85 | bfd_boolean trace_file_tries; |
252b5132 RH |
86 | |
87 | /* Nonzero means version number was printed, so exit successfully | |
88 | instead of complaining if no input files are given. */ | |
b34976b6 | 89 | bfd_boolean version_printed; |
252b5132 RH |
90 | |
91 | /* Nonzero means link in every member of an archive. */ | |
b34976b6 | 92 | bfd_boolean whole_archive; |
252b5132 | 93 | |
b34976b6 AM |
94 | /* TRUE if we should demangle symbol names. */ |
95 | bfd_boolean demangling; | |
252b5132 RH |
96 | |
97 | args_type command_line; | |
98 | ||
99 | ld_config_type config; | |
100 | ||
0125cdf2 NC |
101 | static char *get_emulation |
102 | PARAMS ((int, char **)); | |
103 | static void set_scripts_dir | |
104 | PARAMS ((void)); | |
105 | static void remove_output | |
106 | PARAMS ((void)); | |
107 | static bfd_boolean check_for_scripts_dir | |
108 | PARAMS ((char *)); | |
109 | static bfd_boolean add_archive_element | |
110 | PARAMS ((struct bfd_link_info *, bfd *, const char *)); | |
111 | static bfd_boolean multiple_definition | |
112 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, | |
113 | bfd *, asection *, bfd_vma)); | |
114 | static bfd_boolean multiple_common | |
115 | PARAMS ((struct bfd_link_info *, const char *, bfd *, | |
116 | enum bfd_link_hash_type, bfd_vma, bfd *, enum bfd_link_hash_type, | |
117 | bfd_vma)); | |
118 | static bfd_boolean add_to_set | |
119 | PARAMS ((struct bfd_link_info *, struct bfd_link_hash_entry *, | |
120 | bfd_reloc_code_real_type, bfd *, asection *, bfd_vma)); | |
121 | static bfd_boolean constructor_callback | |
122 | PARAMS ((struct bfd_link_info *, bfd_boolean, const char *, bfd *, | |
123 | asection *, bfd_vma)); | |
124 | static bfd_boolean warning_callback | |
125 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *, | |
126 | asection *, bfd_vma)); | |
127 | static void warning_find_reloc | |
128 | PARAMS ((bfd *, asection *, PTR)); | |
129 | static bfd_boolean undefined_symbol | |
130 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, | |
131 | bfd_boolean)); | |
132 | static bfd_boolean reloc_overflow | |
133 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma, | |
134 | bfd *, asection *, bfd_vma)); | |
135 | static bfd_boolean reloc_dangerous | |
136 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
137 | static bfd_boolean unattached_reloc | |
138 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
139 | static bfd_boolean notice | |
140 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); | |
141 | ||
142 | static struct bfd_link_callbacks link_callbacks = | |
143 | { | |
252b5132 RH |
144 | add_archive_element, |
145 | multiple_definition, | |
146 | multiple_common, | |
147 | add_to_set, | |
148 | constructor_callback, | |
149 | warning_callback, | |
150 | undefined_symbol, | |
151 | reloc_overflow, | |
152 | reloc_dangerous, | |
153 | unattached_reloc, | |
154 | notice | |
155 | }; | |
156 | ||
157 | struct bfd_link_info link_info; | |
158 | \f | |
159 | static void | |
160 | remove_output () | |
161 | { | |
6d5e62f8 | 162 | if (output_filename) |
252b5132 RH |
163 | { |
164 | if (output_bfd && output_bfd->iostream) | |
6d5e62f8 | 165 | fclose ((FILE *) (output_bfd->iostream)); |
252b5132 RH |
166 | if (delete_output_file_on_failure) |
167 | unlink (output_filename); | |
168 | } | |
169 | } | |
170 | ||
171 | int | |
172 | main (argc, argv) | |
173 | int argc; | |
174 | char **argv; | |
175 | { | |
176 | char *emulation; | |
177 | long start_time = get_run_time (); | |
178 | ||
179 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
180 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
181 | #endif |
182 | #if defined (HAVE_SETLOCALE) | |
183 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
184 | #endif |
185 | bindtextdomain (PACKAGE, LOCALEDIR); | |
186 | textdomain (PACKAGE); | |
187 | ||
188 | program_name = argv[0]; | |
189 | xmalloc_set_program_name (program_name); | |
190 | ||
191 | START_PROGRESS (program_name, 0); | |
192 | ||
193 | bfd_init (); | |
194 | ||
195 | bfd_set_error_program_name (program_name); | |
196 | ||
197 | xatexit (remove_output); | |
198 | ||
9c8ebd6a DJ |
199 | #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE |
200 | ld_sysroot = make_relative_prefix (program_name, BINDIR, | |
201 | TARGET_SYSTEM_ROOT); | |
0125cdf2 | 202 | |
9c8ebd6a DJ |
203 | if (ld_sysroot) |
204 | { | |
205 | struct stat s; | |
206 | int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); | |
0125cdf2 | 207 | |
9c8ebd6a DJ |
208 | if (!res) |
209 | { | |
210 | free (ld_sysroot); | |
0125cdf2 | 211 | ld_sysroot = NULL; |
9c8ebd6a DJ |
212 | } |
213 | } | |
0125cdf2 NC |
214 | |
215 | if (! ld_sysroot) | |
216 | { | |
217 | ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR, | |
218 | TARGET_SYSTEM_ROOT); | |
219 | ||
220 | if (ld_sysroot) | |
221 | { | |
222 | struct stat s; | |
223 | int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); | |
224 | ||
225 | if (!res) | |
226 | { | |
227 | free (ld_sysroot); | |
228 | ld_sysroot = NULL; | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
233 | if (! ld_sysroot) | |
9c8ebd6a | 234 | #endif |
0125cdf2 | 235 | ld_sysroot = TARGET_SYSTEM_ROOT; |
9c8ebd6a | 236 | |
252b5132 RH |
237 | /* Set the default BFD target based on the configured target. Doing |
238 | this permits the linker to be configured for a particular target, | |
239 | and linked against a shared BFD library which was configured for | |
240 | a different target. The macro TARGET is defined by Makefile. */ | |
241 | if (! bfd_set_default_target (TARGET)) | |
242 | { | |
243 | einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); | |
244 | xexit (1); | |
245 | } | |
246 | ||
b47c4208 AM |
247 | #if YYDEBUG |
248 | { | |
249 | extern int yydebug; | |
250 | yydebug = 1; | |
251 | } | |
252 | #endif | |
253 | ||
252b5132 | 254 | /* Initialize the data about options. */ |
b34976b6 AM |
255 | trace_files = trace_file_tries = version_printed = FALSE; |
256 | whole_archive = FALSE; | |
257 | config.build_constructors = TRUE; | |
258 | config.dynamic_link = FALSE; | |
259 | config.has_shared = FALSE; | |
a854a4a7 AM |
260 | config.split_by_reloc = (unsigned) -1; |
261 | config.split_by_file = (bfd_size_type) -1; | |
b34976b6 AM |
262 | command_line.force_common_definition = FALSE; |
263 | command_line.inhibit_common_definition = FALSE; | |
252b5132 RH |
264 | command_line.interpreter = NULL; |
265 | command_line.rpath = NULL; | |
b34976b6 AM |
266 | command_line.warn_mismatch = TRUE; |
267 | command_line.check_section_addresses = TRUE; | |
312b768e | 268 | command_line.accept_unknown_input_arch = FALSE; |
252b5132 RH |
269 | |
270 | /* We initialize DEMANGLING based on the environment variable | |
271 | COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the | |
272 | output of the linker, unless COLLECT_NO_DEMANGLE is set in the | |
273 | environment. Acting the same way here lets us provide the same | |
274 | interface by default. */ | |
275 | demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; | |
276 | ||
b34976b6 AM |
277 | link_info.relocateable = FALSE; |
278 | link_info.emitrelocations = FALSE; | |
b5067602 | 279 | link_info.task_link = FALSE; |
b34976b6 AM |
280 | link_info.shared = FALSE; |
281 | link_info.symbolic = FALSE; | |
282 | link_info.export_dynamic = FALSE; | |
283 | link_info.static_link = FALSE; | |
284 | link_info.traditional_format = FALSE; | |
285 | link_info.optimize = FALSE; | |
286 | link_info.no_undefined = FALSE; | |
ae9a127f | 287 | link_info.allow_shlib_undefined = TRUE; |
b34976b6 AM |
288 | link_info.allow_multiple_definition = FALSE; |
289 | link_info.allow_undefined_version = TRUE; | |
b5067602 AM |
290 | link_info.keep_memory = TRUE; |
291 | link_info.notice_all = FALSE; | |
292 | link_info.nocopyreloc = FALSE; | |
293 | link_info.new_dtags = FALSE; | |
294 | link_info.combreloc = TRUE; | |
295 | link_info.eh_frame_hdr = FALSE; | |
d5cd3933 | 296 | link_info.strip_discarded = TRUE; |
252b5132 | 297 | link_info.strip = strip_none; |
f5fa8ca2 | 298 | link_info.discard = discard_sec_merge; |
b5067602 AM |
299 | link_info.common_skip_ar_aymbols = bfd_link_common_skip_none; |
300 | link_info.callbacks = &link_callbacks; | |
252b5132 RH |
301 | link_info.hash = NULL; |
302 | link_info.keep_hash = NULL; | |
252b5132 RH |
303 | link_info.notice_hash = NULL; |
304 | link_info.wrap_hash = NULL; | |
b5067602 AM |
305 | link_info.input_bfds = NULL; |
306 | link_info.create_object_symbols_section = NULL; | |
307 | link_info.gc_sym_list = NULL; | |
308 | link_info.base_file = NULL; | |
3dbf70a2 MM |
309 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init |
310 | and _fini symbols. We are compatible. */ | |
311 | link_info.init_function = "_init"; | |
312 | link_info.fini_function = "_fini"; | |
b5067602 | 313 | link_info.mpc860c0 = 0; |
e0076ab3 | 314 | link_info.pei386_auto_import = -1; |
b34976b6 | 315 | link_info.pei386_runtime_pseudo_reloc = FALSE; |
db6751f2 | 316 | link_info.spare_dynamic_tags = 5; |
b5067602 AM |
317 | link_info.flags = (bfd_vma) 0; |
318 | link_info.flags_1 = (bfd_vma) 0; | |
3dbf70a2 | 319 | |
252b5132 RH |
320 | ldfile_add_arch (""); |
321 | ||
b34976b6 AM |
322 | config.make_executable = TRUE; |
323 | force_make_executable = FALSE; | |
324 | config.magic_demand_paged = TRUE; | |
325 | config.text_read_only = TRUE; | |
252b5132 RH |
326 | |
327 | emulation = get_emulation (argc, argv); | |
328 | ldemul_choose_mode (emulation); | |
742aeb63 | 329 | default_target = ldemul_choose_target (argc, argv); |
252b5132 RH |
330 | lang_init (); |
331 | ldemul_before_parse (); | |
b34976b6 | 332 | lang_has_input_file = FALSE; |
252b5132 RH |
333 | parse_args (argc, argv); |
334 | ||
335 | ldemul_set_symbols (); | |
336 | ||
337 | if (link_info.relocateable) | |
338 | { | |
339 | if (command_line.gc_sections) | |
340 | einfo ("%P%F: --gc-sections and -r may not be used together\n"); | |
341 | if (link_info.mpc860c0) | |
342 | einfo (_("%P%F: -r and --mpc860c0 may not be used together\n")); | |
343 | else if (command_line.relax) | |
344 | einfo (_("%P%F: --relax and -r may not be used together\n")); | |
345 | if (link_info.shared) | |
346 | einfo (_("%P%F: -r and -shared may not be used together\n")); | |
347 | } | |
348 | ||
7333607e JJ |
349 | if (! link_info.shared) |
350 | { | |
351 | if (command_line.filter_shlib) | |
e97cb84f | 352 | einfo (_("%P%F: -F may not be used without -shared\n")); |
7333607e | 353 | if (command_line.auxiliary_filters) |
e97cb84f | 354 | einfo (_("%P%F: -f may not be used without -shared\n")); |
7333607e JJ |
355 | } |
356 | ||
252b5132 RH |
357 | /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I |
358 | don't see how else this can be handled, since in this case we | |
359 | must preserve all externally visible symbols. */ | |
360 | if (link_info.relocateable && link_info.strip == strip_all) | |
361 | { | |
362 | link_info.strip = strip_debugger; | |
f5fa8ca2 | 363 | if (link_info.discard == discard_sec_merge) |
252b5132 RH |
364 | link_info.discard = discard_all; |
365 | } | |
366 | ||
367 | /* This essentially adds another -L directory so this must be done after | |
368 | the -L's in argv have been processed. */ | |
369 | set_scripts_dir (); | |
370 | ||
b9a8de1e NC |
371 | /* If we have not already opened and parsed a linker script |
372 | read the emulation's appropriate default script. */ | |
b90e1c6f | 373 | if (saved_script_handle == NULL) |
252b5132 | 374 | { |
252b5132 | 375 | int isfile; |
b7a26f91 | 376 | char *s = ldemul_get_script (&isfile); |
252b5132 RH |
377 | |
378 | if (isfile) | |
379 | ldfile_open_command_file (s); | |
380 | else | |
b7a26f91 | 381 | { |
252b5132 RH |
382 | lex_string = s; |
383 | lex_redirect (s); | |
384 | } | |
385 | parser_input = input_script; | |
386 | yyparse (); | |
387 | lex_string = NULL; | |
388 | } | |
389 | ||
b9a8de1e NC |
390 | if (trace_file_tries) |
391 | { | |
9cb61eab AM |
392 | if (saved_script_handle) |
393 | info_msg (_("using external linker script:")); | |
394 | else | |
395 | info_msg (_("using internal linker script:")); | |
396 | info_msg ("\n==================================================\n"); | |
b9a8de1e NC |
397 | |
398 | if (saved_script_handle) | |
399 | { | |
74699268 | 400 | static const int ld_bufsz = 8193; |
b9a8de1e | 401 | size_t n; |
74699268 | 402 | char *buf = xmalloc (ld_bufsz); |
b9a8de1e NC |
403 | |
404 | rewind (saved_script_handle); | |
74699268 | 405 | while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0) |
b9a8de1e | 406 | { |
b7a26f91 | 407 | buf[n] = 0; |
b9a8de1e NC |
408 | info_msg (buf); |
409 | } | |
410 | rewind (saved_script_handle); | |
411 | free (buf); | |
412 | } | |
413 | else | |
414 | { | |
415 | int isfile; | |
416 | ||
b7a26f91 | 417 | info_msg (ldemul_get_script (&isfile)); |
b9a8de1e | 418 | } |
b7a26f91 | 419 | |
b9a8de1e NC |
420 | info_msg ("\n==================================================\n"); |
421 | } | |
422 | ||
252b5132 RH |
423 | lang_final (); |
424 | ||
b34976b6 | 425 | if (!lang_has_input_file) |
252b5132 RH |
426 | { |
427 | if (version_printed) | |
428 | xexit (0); | |
429 | einfo (_("%P%F: no input files\n")); | |
430 | } | |
431 | ||
432 | if (trace_files) | |
0125cdf2 | 433 | info_msg (_("%P: mode %s\n"), emulation); |
252b5132 RH |
434 | |
435 | ldemul_after_parse (); | |
436 | ||
252b5132 RH |
437 | if (config.map_filename) |
438 | { | |
439 | if (strcmp (config.map_filename, "-") == 0) | |
440 | { | |
441 | config.map_file = stdout; | |
442 | } | |
443 | else | |
444 | { | |
445 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
446 | if (config.map_file == (FILE *) NULL) | |
447 | { | |
448 | bfd_set_error (bfd_error_system_call); | |
449 | einfo (_("%P%F: cannot open map file %s: %E\n"), | |
450 | config.map_filename); | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
252b5132 RH |
455 | lang_process (); |
456 | ||
457 | /* Print error messages for any missing symbols, for any warning | |
6d5e62f8 | 458 | symbols, and possibly multiple definitions. */ |
252b5132 RH |
459 | if (link_info.relocateable) |
460 | output_bfd->flags &= ~EXEC_P; | |
461 | else | |
462 | output_bfd->flags |= EXEC_P; | |
463 | ||
464 | ldwrite (); | |
465 | ||
466 | if (config.map_file != NULL) | |
467 | lang_map (); | |
468 | if (command_line.cref) | |
469 | output_cref (config.map_file != NULL ? config.map_file : stdout); | |
470 | if (nocrossref_list != NULL) | |
471 | check_nocrossrefs (); | |
472 | ||
473 | /* Even if we're producing relocateable output, some non-fatal errors should | |
474 | be reported in the exit status. (What non-fatal errors, if any, do we | |
475 | want to ignore for relocateable output?) */ | |
b34976b6 | 476 | if (!config.make_executable && !force_make_executable) |
252b5132 | 477 | { |
b34976b6 | 478 | if (trace_files) |
0125cdf2 NC |
479 | einfo (_("%P: link errors found, deleting executable `%s'\n"), |
480 | output_filename); | |
252b5132 RH |
481 | |
482 | /* The file will be removed by remove_output. */ | |
252b5132 RH |
483 | xexit (1); |
484 | } | |
485 | else | |
486 | { | |
487 | if (! bfd_close (output_bfd)) | |
488 | einfo (_("%F%B: final close failed: %E\n"), output_bfd); | |
489 | ||
490 | /* If the --force-exe-suffix is enabled, and we're making an | |
6d5e62f8 KH |
491 | executable file and it doesn't end in .exe, copy it to one |
492 | which does. */ | |
252b5132 RH |
493 | if (! link_info.relocateable && command_line.force_exe_suffix) |
494 | { | |
495 | int len = strlen (output_filename); | |
0125cdf2 | 496 | |
6d5e62f8 | 497 | if (len < 4 |
252b5132 RH |
498 | || (strcasecmp (output_filename + len - 4, ".exe") != 0 |
499 | && strcasecmp (output_filename + len - 4, ".dll") != 0)) | |
500 | { | |
501 | FILE *src; | |
502 | FILE *dst; | |
503 | const int bsize = 4096; | |
504 | char *buf = xmalloc (bsize); | |
505 | int l; | |
506 | char *dst_name = xmalloc (len + 5); | |
0125cdf2 | 507 | |
252b5132 RH |
508 | strcpy (dst_name, output_filename); |
509 | strcat (dst_name, ".exe"); | |
510 | src = fopen (output_filename, FOPEN_RB); | |
511 | dst = fopen (dst_name, FOPEN_WB); | |
512 | ||
513 | if (!src) | |
514 | einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename); | |
515 | if (!dst) | |
516 | einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name); | |
517 | while ((l = fread (buf, 1, bsize, src)) > 0) | |
518 | { | |
519 | int done = fwrite (buf, 1, l, dst); | |
0125cdf2 | 520 | |
252b5132 | 521 | if (done != l) |
0125cdf2 | 522 | einfo (_("%P: Error writing file `%s'\n"), dst_name); |
252b5132 | 523 | } |
0125cdf2 | 524 | |
252b5132 RH |
525 | fclose (src); |
526 | if (fclose (dst) == EOF) | |
0125cdf2 | 527 | einfo (_("%P: Error closing file `%s'\n"), dst_name); |
252b5132 RH |
528 | free (dst_name); |
529 | free (buf); | |
530 | } | |
531 | } | |
532 | } | |
533 | ||
534 | END_PROGRESS (program_name); | |
535 | ||
536 | if (config.stats) | |
537 | { | |
538 | #ifdef HAVE_SBRK | |
539 | char *lim = (char *) sbrk (0); | |
540 | #endif | |
541 | long run_time = get_run_time () - start_time; | |
542 | ||
543 | fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), | |
544 | program_name, run_time / 1000000, run_time % 1000000); | |
545 | #ifdef HAVE_SBRK | |
546 | fprintf (stderr, _("%s: data size %ld\n"), program_name, | |
547 | (long) (lim - (char *) &environ)); | |
548 | #endif | |
549 | } | |
550 | ||
551 | /* Prevent remove_output from doing anything, after a successful link. */ | |
552 | output_filename = NULL; | |
553 | ||
554 | xexit (0); | |
555 | return 0; | |
556 | } | |
557 | ||
558 | /* We need to find any explicitly given emulation in order to initialize the | |
559 | state that's needed by the lex&yacc argument parser (parse_args). */ | |
560 | ||
561 | static char * | |
562 | get_emulation (argc, argv) | |
563 | int argc; | |
564 | char **argv; | |
565 | { | |
566 | char *emulation; | |
567 | int i; | |
568 | ||
569 | emulation = getenv (EMULATION_ENVIRON); | |
570 | if (emulation == NULL) | |
571 | emulation = DEFAULT_EMULATION; | |
572 | ||
573 | for (i = 1; i < argc; i++) | |
574 | { | |
575 | if (!strncmp (argv[i], "-m", 2)) | |
576 | { | |
577 | if (argv[i][2] == '\0') | |
578 | { | |
579 | /* -m EMUL */ | |
580 | if (i < argc - 1) | |
581 | { | |
582 | emulation = argv[i + 1]; | |
583 | i++; | |
584 | } | |
585 | else | |
0125cdf2 | 586 | einfo (_("%P%F: missing argument to -m\n")); |
252b5132 RH |
587 | } |
588 | else if (strcmp (argv[i], "-mips1") == 0 | |
589 | || strcmp (argv[i], "-mips2") == 0 | |
590 | || strcmp (argv[i], "-mips3") == 0 | |
d1cf510e | 591 | || strcmp (argv[i], "-mips4") == 0 |
6f33421c CD |
592 | || strcmp (argv[i], "-mips5") == 0 |
593 | || strcmp (argv[i], "-mips32") == 0 | |
594 | || strcmp (argv[i], "-mips32r2") == 0 | |
595 | || strcmp (argv[i], "-mips64") == 0) | |
252b5132 | 596 | { |
31d677f9 | 597 | /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are |
252b5132 RH |
598 | passed to the linker by some MIPS compilers. They |
599 | generally tell the linker to use a slightly different | |
600 | library path. Perhaps someday these should be | |
601 | implemented as emulations; until then, we just ignore | |
602 | the arguments and hope that nobody ever creates | |
603 | emulations named ips1, ips2 or ips3. */ | |
604 | } | |
605 | else if (strcmp (argv[i], "-m486") == 0) | |
606 | { | |
607 | /* FIXME: The argument -m486 is passed to the linker on | |
608 | some Linux systems. Hope that nobody creates an | |
609 | emulation named 486. */ | |
610 | } | |
611 | else | |
612 | { | |
613 | /* -mEMUL */ | |
614 | emulation = &argv[i][2]; | |
615 | } | |
616 | } | |
617 | } | |
618 | ||
619 | return emulation; | |
620 | } | |
621 | ||
622 | /* If directory DIR contains an "ldscripts" subdirectory, | |
b34976b6 AM |
623 | add DIR to the library search path and return TRUE, |
624 | else return FALSE. */ | |
252b5132 | 625 | |
b34976b6 | 626 | static bfd_boolean |
252b5132 RH |
627 | check_for_scripts_dir (dir) |
628 | char *dir; | |
629 | { | |
630 | size_t dirlen; | |
631 | char *buf; | |
632 | struct stat s; | |
b34976b6 | 633 | bfd_boolean res; |
252b5132 RH |
634 | |
635 | dirlen = strlen (dir); | |
636 | /* sizeof counts the terminating NUL. */ | |
6d5e62f8 | 637 | buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts")); |
252b5132 RH |
638 | sprintf (buf, "%s/ldscripts", dir); |
639 | ||
640 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
641 | free (buf); | |
642 | if (res) | |
b34976b6 | 643 | ldfile_add_library_path (dir, FALSE); |
252b5132 RH |
644 | return res; |
645 | } | |
646 | ||
647 | /* Set the default directory for finding script files. | |
648 | Libraries will be searched for here too, but that's ok. | |
649 | We look for the "ldscripts" directory in: | |
650 | ||
cdc0d45e DJ |
651 | SCRIPTDIR (passed from Makefile) |
652 | (adjusted according to the current location of the binary) | |
252b5132 RH |
653 | SCRIPTDIR (passed from Makefile) |
654 | the dir where this program is (for using it from the build tree) | |
cdc0d45e | 655 | the dir where this program is/../lib |
0125cdf2 | 656 | (for installing the tool suite elsewhere). */ |
252b5132 RH |
657 | |
658 | static void | |
659 | set_scripts_dir () | |
660 | { | |
661 | char *end, *dir; | |
662 | size_t dirlen; | |
663 | ||
cdc0d45e DJ |
664 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); |
665 | if (dir && check_for_scripts_dir (dir)) | |
666 | /* Success. Don't free dir. */ | |
667 | return; | |
668 | ||
669 | if (dir) | |
670 | free (dir); | |
671 | ||
4ca1b790 AO |
672 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); |
673 | if (dir && check_for_scripts_dir (dir)) | |
674 | /* Success. Don't free dir. */ | |
675 | return; | |
676 | ||
677 | if (dir) | |
678 | free (dir); | |
679 | ||
252b5132 | 680 | if (check_for_scripts_dir (SCRIPTDIR)) |
6d5e62f8 KH |
681 | /* We've been installed normally. */ |
682 | return; | |
252b5132 RH |
683 | |
684 | /* Look for "ldscripts" in the dir where our binary is. */ | |
685 | end = strrchr (program_name, '/'); | |
5af11cab AM |
686 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
687 | { | |
688 | /* We could have \foo\bar, or /foo\bar. */ | |
689 | char *bslash = strrchr (program_name, '\\'); | |
0125cdf2 | 690 | |
2ab47eed | 691 | if (end == NULL || (bslash != NULL && bslash > end)) |
5af11cab AM |
692 | end = bslash; |
693 | } | |
694 | #endif | |
252b5132 RH |
695 | |
696 | if (end == NULL) | |
0125cdf2 NC |
697 | /* Don't look for ldscripts in the current directory. There is |
698 | too much potential for confusion. */ | |
699 | return; | |
252b5132 RH |
700 | |
701 | dirlen = end - program_name; | |
702 | /* Make a copy of program_name in dir. | |
703 | Leave room for later "/../lib". */ | |
704 | dir = (char *) xmalloc (dirlen + 8); | |
705 | strncpy (dir, program_name, dirlen); | |
706 | dir[dirlen] = '\0'; | |
707 | ||
708 | if (check_for_scripts_dir (dir)) | |
6d5e62f8 KH |
709 | /* Don't free dir. */ |
710 | return; | |
252b5132 RH |
711 | |
712 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ | |
713 | strcpy (dir + dirlen, "/../lib"); | |
714 | if (check_for_scripts_dir (dir)) | |
715 | return; | |
716 | ||
6d5e62f8 KH |
717 | /* Well, we tried. */ |
718 | free (dir); | |
252b5132 RH |
719 | } |
720 | ||
721 | void | |
722 | add_ysym (name) | |
723 | const char *name; | |
724 | { | |
725 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) | |
726 | { | |
727 | link_info.notice_hash = ((struct bfd_hash_table *) | |
728 | xmalloc (sizeof (struct bfd_hash_table))); | |
729 | if (! bfd_hash_table_init_n (link_info.notice_hash, | |
730 | bfd_hash_newfunc, | |
731 | 61)) | |
732 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
6d5e62f8 | 733 | } |
252b5132 | 734 | |
b34976b6 | 735 | if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) |
252b5132 RH |
736 | == (struct bfd_hash_entry *) NULL) |
737 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); | |
738 | } | |
739 | ||
740 | /* Record a symbol to be wrapped, from the --wrap option. */ | |
741 | ||
742 | void | |
743 | add_wrap (name) | |
744 | const char *name; | |
745 | { | |
746 | if (link_info.wrap_hash == NULL) | |
747 | { | |
748 | link_info.wrap_hash = ((struct bfd_hash_table *) | |
749 | xmalloc (sizeof (struct bfd_hash_table))); | |
750 | if (! bfd_hash_table_init_n (link_info.wrap_hash, | |
751 | bfd_hash_newfunc, | |
752 | 61)) | |
753 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
754 | } | |
0125cdf2 | 755 | |
b34976b6 | 756 | if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL) |
252b5132 RH |
757 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); |
758 | } | |
759 | ||
760 | /* Handle the -retain-symbols-file option. */ | |
761 | ||
762 | void | |
763 | add_keepsyms_file (filename) | |
764 | const char *filename; | |
765 | { | |
766 | FILE *file; | |
767 | char *buf; | |
768 | size_t bufsize; | |
769 | int c; | |
770 | ||
771 | if (link_info.strip == strip_some) | |
772 | einfo (_("%X%P: error: duplicate retain-symbols-file\n")); | |
773 | ||
774 | file = fopen (filename, "r"); | |
775 | if (file == (FILE *) NULL) | |
776 | { | |
777 | bfd_set_error (bfd_error_system_call); | |
778 | einfo ("%X%P: %s: %E\n", filename); | |
779 | return; | |
780 | } | |
781 | ||
782 | link_info.keep_hash = ((struct bfd_hash_table *) | |
783 | xmalloc (sizeof (struct bfd_hash_table))); | |
784 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) | |
785 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); | |
786 | ||
787 | bufsize = 100; | |
788 | buf = (char *) xmalloc (bufsize); | |
789 | ||
790 | c = getc (file); | |
791 | while (c != EOF) | |
792 | { | |
3882b010 | 793 | while (ISSPACE (c)) |
252b5132 RH |
794 | c = getc (file); |
795 | ||
796 | if (c != EOF) | |
797 | { | |
798 | size_t len = 0; | |
799 | ||
3882b010 | 800 | while (! ISSPACE (c) && c != EOF) |
252b5132 RH |
801 | { |
802 | buf[len] = c; | |
803 | ++len; | |
804 | if (len >= bufsize) | |
805 | { | |
806 | bufsize *= 2; | |
807 | buf = xrealloc (buf, bufsize); | |
808 | } | |
809 | c = getc (file); | |
810 | } | |
811 | ||
812 | buf[len] = '\0'; | |
813 | ||
b34976b6 | 814 | if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) |
252b5132 RH |
815 | == (struct bfd_hash_entry *) NULL) |
816 | einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); | |
817 | } | |
818 | } | |
819 | ||
820 | if (link_info.strip != strip_none) | |
821 | einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); | |
822 | ||
823 | link_info.strip = strip_some; | |
824 | } | |
825 | \f | |
826 | /* Callbacks from the BFD linker routines. */ | |
827 | ||
828 | /* This is called when BFD has decided to include an archive member in | |
829 | a link. */ | |
830 | ||
b34976b6 | 831 | static bfd_boolean |
252b5132 | 832 | add_archive_element (info, abfd, name) |
87f2a346 | 833 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
834 | bfd *abfd; |
835 | const char *name; | |
836 | { | |
837 | lang_input_statement_type *input; | |
838 | ||
839 | input = ((lang_input_statement_type *) | |
840 | xmalloc (sizeof (lang_input_statement_type))); | |
841 | input->filename = abfd->filename; | |
842 | input->local_sym_name = abfd->filename; | |
843 | input->the_bfd = abfd; | |
844 | input->asymbols = NULL; | |
845 | input->next = NULL; | |
b34976b6 AM |
846 | input->just_syms_flag = FALSE; |
847 | input->loaded = FALSE; | |
848 | input->search_dirs_flag = FALSE; | |
252b5132 RH |
849 | |
850 | /* FIXME: The following fields are not set: header.next, | |
851 | header.type, closed, passive_position, symbol_count, | |
852 | next_real_file, is_archive, target, real. This bit of code is | |
853 | from the old decode_library_subfile function. I don't know | |
854 | whether any of those fields matters. */ | |
855 | ||
856 | ldlang_add_file (input); | |
857 | ||
858 | if (config.map_file != (FILE *) NULL) | |
859 | { | |
b34976b6 | 860 | static bfd_boolean header_printed; |
252b5132 RH |
861 | struct bfd_link_hash_entry *h; |
862 | bfd *from; | |
863 | int len; | |
864 | ||
b34976b6 | 865 | h = bfd_link_hash_lookup (link_info.hash, name, FALSE, FALSE, TRUE); |
252b5132 RH |
866 | |
867 | if (h == NULL) | |
868 | from = NULL; | |
869 | else | |
870 | { | |
871 | switch (h->type) | |
872 | { | |
873 | default: | |
874 | from = NULL; | |
875 | break; | |
876 | ||
877 | case bfd_link_hash_defined: | |
878 | case bfd_link_hash_defweak: | |
879 | from = h->u.def.section->owner; | |
880 | break; | |
881 | ||
882 | case bfd_link_hash_undefined: | |
883 | case bfd_link_hash_undefweak: | |
884 | from = h->u.undef.abfd; | |
885 | break; | |
886 | ||
887 | case bfd_link_hash_common: | |
888 | from = h->u.c.p->section->owner; | |
889 | break; | |
890 | } | |
891 | } | |
892 | ||
893 | if (! header_printed) | |
894 | { | |
895 | char buf[100]; | |
896 | ||
e3c8793a | 897 | sprintf (buf, _("Archive member included because of file (symbol)\n\n")); |
252b5132 | 898 | minfo ("%s", buf); |
b34976b6 | 899 | header_printed = TRUE; |
252b5132 RH |
900 | } |
901 | ||
902 | if (bfd_my_archive (abfd) == NULL) | |
903 | { | |
904 | minfo ("%s", bfd_get_filename (abfd)); | |
905 | len = strlen (bfd_get_filename (abfd)); | |
906 | } | |
907 | else | |
908 | { | |
909 | minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), | |
910 | bfd_get_filename (abfd)); | |
911 | len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) | |
912 | + strlen (bfd_get_filename (abfd)) | |
913 | + 2); | |
914 | } | |
915 | ||
916 | if (len >= 29) | |
917 | { | |
918 | print_nl (); | |
919 | len = 0; | |
920 | } | |
921 | while (len < 30) | |
922 | { | |
923 | print_space (); | |
924 | ++len; | |
925 | } | |
926 | ||
927 | if (from != NULL) | |
928 | minfo ("%B ", from); | |
929 | if (h != NULL) | |
930 | minfo ("(%T)\n", h->root.string); | |
931 | else | |
932 | minfo ("(%s)\n", name); | |
933 | } | |
934 | ||
935 | if (trace_files || trace_file_tries) | |
936 | info_msg ("%I\n", input); | |
937 | ||
b34976b6 | 938 | return TRUE; |
252b5132 RH |
939 | } |
940 | ||
941 | /* This is called when BFD has discovered a symbol which is defined | |
942 | multiple times. */ | |
943 | ||
b34976b6 | 944 | static bfd_boolean |
252b5132 | 945 | multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) |
87f2a346 | 946 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
947 | const char *name; |
948 | bfd *obfd; | |
949 | asection *osec; | |
950 | bfd_vma oval; | |
951 | bfd *nbfd; | |
952 | asection *nsec; | |
953 | bfd_vma nval; | |
954 | { | |
955 | /* If either section has the output_section field set to | |
956 | bfd_abs_section_ptr, it means that the section is being | |
957 | discarded, and this is not really a multiple definition at all. | |
958 | FIXME: It would be cleaner to somehow ignore symbols defined in | |
959 | sections which are being discarded. */ | |
960 | if ((osec->output_section != NULL | |
961 | && ! bfd_is_abs_section (osec) | |
962 | && bfd_is_abs_section (osec->output_section)) | |
963 | || (nsec->output_section != NULL | |
964 | && ! bfd_is_abs_section (nsec) | |
965 | && bfd_is_abs_section (nsec->output_section))) | |
b34976b6 | 966 | return TRUE; |
252b5132 RH |
967 | |
968 | einfo (_("%X%C: multiple definition of `%T'\n"), | |
969 | nbfd, nsec, nval, name); | |
970 | if (obfd != (bfd *) NULL) | |
971 | einfo (_("%D: first defined here\n"), obfd, osec, oval); | |
9b14b192 NC |
972 | |
973 | if (command_line.relax) | |
974 | { | |
975 | einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n")); | |
976 | command_line.relax = 0; | |
977 | } | |
6d5e62f8 | 978 | |
b34976b6 | 979 | return TRUE; |
252b5132 RH |
980 | } |
981 | ||
982 | /* This is called when there is a definition of a common symbol, or | |
983 | when a common symbol is found for a symbol that is already defined, | |
984 | or when two common symbols are found. We only do something if | |
985 | -warn-common was used. */ | |
986 | ||
b34976b6 | 987 | static bfd_boolean |
252b5132 | 988 | multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) |
87f2a346 | 989 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
990 | const char *name; |
991 | bfd *obfd; | |
992 | enum bfd_link_hash_type otype; | |
993 | bfd_vma osize; | |
994 | bfd *nbfd; | |
995 | enum bfd_link_hash_type ntype; | |
996 | bfd_vma nsize; | |
997 | { | |
998 | if (! config.warn_common) | |
b34976b6 | 999 | return TRUE; |
252b5132 RH |
1000 | |
1001 | if (ntype == bfd_link_hash_defined | |
1002 | || ntype == bfd_link_hash_defweak | |
1003 | || ntype == bfd_link_hash_indirect) | |
1004 | { | |
1005 | ASSERT (otype == bfd_link_hash_common); | |
1006 | einfo (_("%B: warning: definition of `%T' overriding common\n"), | |
1007 | nbfd, name); | |
1008 | if (obfd != NULL) | |
1009 | einfo (_("%B: warning: common is here\n"), obfd); | |
1010 | } | |
1011 | else if (otype == bfd_link_hash_defined | |
1012 | || otype == bfd_link_hash_defweak | |
1013 | || otype == bfd_link_hash_indirect) | |
1014 | { | |
1015 | ASSERT (ntype == bfd_link_hash_common); | |
1016 | einfo (_("%B: warning: common of `%T' overridden by definition\n"), | |
1017 | nbfd, name); | |
1018 | if (obfd != NULL) | |
1019 | einfo (_("%B: warning: defined here\n"), obfd); | |
1020 | } | |
1021 | else | |
1022 | { | |
1023 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
1024 | if (osize > nsize) | |
1025 | { | |
1026 | einfo (_("%B: warning: common of `%T' overridden by larger common\n"), | |
1027 | nbfd, name); | |
1028 | if (obfd != NULL) | |
1029 | einfo (_("%B: warning: larger common is here\n"), obfd); | |
1030 | } | |
1031 | else if (nsize > osize) | |
1032 | { | |
1033 | einfo (_("%B: warning: common of `%T' overriding smaller common\n"), | |
1034 | nbfd, name); | |
1035 | if (obfd != NULL) | |
1036 | einfo (_("%B: warning: smaller common is here\n"), obfd); | |
1037 | } | |
1038 | else | |
1039 | { | |
1040 | einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); | |
1041 | if (obfd != NULL) | |
1042 | einfo (_("%B: warning: previous common is here\n"), obfd); | |
1043 | } | |
1044 | } | |
1045 | ||
b34976b6 | 1046 | return TRUE; |
252b5132 RH |
1047 | } |
1048 | ||
1049 | /* This is called when BFD has discovered a set element. H is the | |
1050 | entry in the linker hash table for the set. SECTION and VALUE | |
1051 | represent a value which should be added to the set. */ | |
1052 | ||
b34976b6 | 1053 | static bfd_boolean |
252b5132 | 1054 | add_to_set (info, h, reloc, abfd, section, value) |
87f2a346 | 1055 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1056 | struct bfd_link_hash_entry *h; |
1057 | bfd_reloc_code_real_type reloc; | |
1058 | bfd *abfd; | |
1059 | asection *section; | |
1060 | bfd_vma value; | |
1061 | { | |
1062 | if (config.warn_constructors) | |
1063 | einfo (_("%P: warning: global constructor %s used\n"), | |
1064 | h->root.string); | |
1065 | ||
1066 | if (! config.build_constructors) | |
b34976b6 | 1067 | return TRUE; |
252b5132 RH |
1068 | |
1069 | ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value); | |
1070 | ||
1071 | if (h->type == bfd_link_hash_new) | |
1072 | { | |
1073 | h->type = bfd_link_hash_undefined; | |
1074 | h->u.undef.abfd = abfd; | |
1075 | /* We don't call bfd_link_add_undef to add this to the list of | |
1076 | undefined symbols because we are going to define it | |
1077 | ourselves. */ | |
1078 | } | |
1079 | ||
b34976b6 | 1080 | return TRUE; |
252b5132 RH |
1081 | } |
1082 | ||
1083 | /* This is called when BFD has discovered a constructor. This is only | |
1084 | called for some object file formats--those which do not handle | |
1085 | constructors in some more clever fashion. This is similar to | |
1086 | adding an element to a set, but less general. */ | |
1087 | ||
b34976b6 | 1088 | static bfd_boolean |
252b5132 RH |
1089 | constructor_callback (info, constructor, name, abfd, section, value) |
1090 | struct bfd_link_info *info; | |
b34976b6 | 1091 | bfd_boolean constructor; |
252b5132 RH |
1092 | const char *name; |
1093 | bfd *abfd; | |
1094 | asection *section; | |
1095 | bfd_vma value; | |
1096 | { | |
1097 | char *s; | |
1098 | struct bfd_link_hash_entry *h; | |
1099 | char set_name[1 + sizeof "__CTOR_LIST__"]; | |
1100 | ||
1101 | if (config.warn_constructors) | |
1102 | einfo (_("%P: warning: global constructor %s used\n"), name); | |
1103 | ||
1104 | if (! config.build_constructors) | |
b34976b6 | 1105 | return TRUE; |
252b5132 RH |
1106 | |
1107 | /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a | |
1108 | useful error message. */ | |
1109 | if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL | |
1110 | && (link_info.relocateable | |
1111 | || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) | |
1112 | einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); | |
1113 | ||
1114 | s = set_name; | |
1115 | if (bfd_get_symbol_leading_char (abfd) != '\0') | |
1116 | *s++ = bfd_get_symbol_leading_char (abfd); | |
1117 | if (constructor) | |
1118 | strcpy (s, "__CTOR_LIST__"); | |
1119 | else | |
1120 | strcpy (s, "__DTOR_LIST__"); | |
1121 | ||
b34976b6 | 1122 | h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE); |
252b5132 RH |
1123 | if (h == (struct bfd_link_hash_entry *) NULL) |
1124 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); | |
1125 | if (h->type == bfd_link_hash_new) | |
1126 | { | |
1127 | h->type = bfd_link_hash_undefined; | |
1128 | h->u.undef.abfd = abfd; | |
1129 | /* We don't call bfd_link_add_undef to add this to the list of | |
1130 | undefined symbols because we are going to define it | |
1131 | ourselves. */ | |
1132 | } | |
1133 | ||
1134 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); | |
b34976b6 | 1135 | return TRUE; |
252b5132 RH |
1136 | } |
1137 | ||
1138 | /* A structure used by warning_callback to pass information through | |
1139 | bfd_map_over_sections. */ | |
1140 | ||
0125cdf2 NC |
1141 | struct warning_callback_info |
1142 | { | |
b34976b6 | 1143 | bfd_boolean found; |
252b5132 RH |
1144 | const char *warning; |
1145 | const char *symbol; | |
1146 | asymbol **asymbols; | |
1147 | }; | |
1148 | ||
1149 | /* This is called when there is a reference to a warning symbol. */ | |
1150 | ||
b34976b6 | 1151 | static bfd_boolean |
252b5132 | 1152 | warning_callback (info, warning, symbol, abfd, section, address) |
87f2a346 | 1153 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1154 | const char *warning; |
1155 | const char *symbol; | |
1156 | bfd *abfd; | |
1157 | asection *section; | |
1158 | bfd_vma address; | |
1159 | { | |
1160 | /* This is a hack to support warn_multiple_gp. FIXME: This should | |
1161 | have a cleaner interface, but what? */ | |
1162 | if (! config.warn_multiple_gp | |
1163 | && strcmp (warning, "using multiple gp values") == 0) | |
b34976b6 | 1164 | return TRUE; |
252b5132 RH |
1165 | |
1166 | if (section != NULL) | |
1167 | einfo ("%C: %s\n", abfd, section, address, warning); | |
1168 | else if (abfd == NULL) | |
1169 | einfo ("%P: %s\n", warning); | |
1170 | else if (symbol == NULL) | |
1171 | einfo ("%B: %s\n", abfd, warning); | |
1172 | else | |
1173 | { | |
1174 | lang_input_statement_type *entry; | |
1175 | asymbol **asymbols; | |
1176 | struct warning_callback_info info; | |
1177 | ||
1178 | /* Look through the relocs to see if we can find a plausible | |
1179 | address. */ | |
252b5132 RH |
1180 | entry = (lang_input_statement_type *) abfd->usrdata; |
1181 | if (entry != NULL && entry->asymbols != NULL) | |
1182 | asymbols = entry->asymbols; | |
1183 | else | |
1184 | { | |
1185 | long symsize; | |
1186 | long symbol_count; | |
1187 | ||
1188 | symsize = bfd_get_symtab_upper_bound (abfd); | |
1189 | if (symsize < 0) | |
1190 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); | |
1191 | asymbols = (asymbol **) xmalloc (symsize); | |
1192 | symbol_count = bfd_canonicalize_symtab (abfd, asymbols); | |
1193 | if (symbol_count < 0) | |
1194 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); | |
1195 | if (entry != NULL) | |
1196 | { | |
1197 | entry->asymbols = asymbols; | |
1198 | entry->symbol_count = symbol_count; | |
1199 | } | |
1200 | } | |
1201 | ||
b34976b6 | 1202 | info.found = FALSE; |
252b5132 RH |
1203 | info.warning = warning; |
1204 | info.symbol = symbol; | |
1205 | info.asymbols = asymbols; | |
1206 | bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info); | |
1207 | ||
1208 | if (! info.found) | |
1209 | einfo ("%B: %s\n", abfd, warning); | |
1210 | ||
1211 | if (entry == NULL) | |
1212 | free (asymbols); | |
1213 | } | |
1214 | ||
b34976b6 | 1215 | return TRUE; |
252b5132 RH |
1216 | } |
1217 | ||
1218 | /* This is called by warning_callback for each section. It checks the | |
1219 | relocs of the section to see if it can find a reference to the | |
1220 | symbol which triggered the warning. If it can, it uses the reloc | |
1221 | to give an error message with a file and line number. */ | |
1222 | ||
1223 | static void | |
1224 | warning_find_reloc (abfd, sec, iarg) | |
1225 | bfd *abfd; | |
1226 | asection *sec; | |
1227 | PTR iarg; | |
1228 | { | |
1229 | struct warning_callback_info *info = (struct warning_callback_info *) iarg; | |
1230 | long relsize; | |
1231 | arelent **relpp; | |
1232 | long relcount; | |
1233 | arelent **p, **pend; | |
1234 | ||
1235 | if (info->found) | |
1236 | return; | |
1237 | ||
1238 | relsize = bfd_get_reloc_upper_bound (abfd, sec); | |
1239 | if (relsize < 0) | |
1240 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); | |
1241 | if (relsize == 0) | |
1242 | return; | |
1243 | ||
1244 | relpp = (arelent **) xmalloc (relsize); | |
1245 | relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); | |
1246 | if (relcount < 0) | |
1247 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); | |
1248 | ||
1249 | p = relpp; | |
1250 | pend = p + relcount; | |
1251 | for (; p < pend && *p != NULL; p++) | |
1252 | { | |
1253 | arelent *q = *p; | |
1254 | ||
1255 | if (q->sym_ptr_ptr != NULL | |
1256 | && *q->sym_ptr_ptr != NULL | |
1257 | && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) | |
1258 | { | |
1259 | /* We found a reloc for the symbol we are looking for. */ | |
1260 | einfo ("%C: %s\n", abfd, sec, q->address, info->warning); | |
b34976b6 | 1261 | info->found = TRUE; |
252b5132 RH |
1262 | break; |
1263 | } | |
1264 | } | |
1265 | ||
1266 | free (relpp); | |
1267 | } | |
1268 | ||
1269 | /* This is called when an undefined symbol is found. */ | |
1270 | ||
b34976b6 | 1271 | static bfd_boolean |
b6f29aaa | 1272 | undefined_symbol (info, name, abfd, section, address, fatal) |
87f2a346 | 1273 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1274 | const char *name; |
1275 | bfd *abfd; | |
1276 | asection *section; | |
1277 | bfd_vma address; | |
b34976b6 | 1278 | bfd_boolean fatal ATTRIBUTE_UNUSED; |
252b5132 RH |
1279 | { |
1280 | static char *error_name; | |
1281 | static unsigned int error_count; | |
1282 | ||
1283 | #define MAX_ERRORS_IN_A_ROW 5 | |
1284 | ||
1285 | if (config.warn_once) | |
1286 | { | |
1287 | static struct bfd_hash_table *hash; | |
1288 | ||
1289 | /* Only warn once about a particular undefined symbol. */ | |
252b5132 RH |
1290 | if (hash == NULL) |
1291 | { | |
1292 | hash = ((struct bfd_hash_table *) | |
1293 | xmalloc (sizeof (struct bfd_hash_table))); | |
1294 | if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) | |
1295 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); | |
1296 | } | |
1297 | ||
b34976b6 AM |
1298 | if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL) |
1299 | return TRUE; | |
252b5132 | 1300 | |
b34976b6 | 1301 | if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL) |
252b5132 RH |
1302 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
1303 | } | |
1304 | ||
1305 | /* We never print more than a reasonable number of errors in a row | |
1306 | for a single symbol. */ | |
1307 | if (error_name != (char *) NULL | |
1308 | && strcmp (name, error_name) == 0) | |
1309 | ++error_count; | |
1310 | else | |
1311 | { | |
1312 | error_count = 0; | |
1313 | if (error_name != (char *) NULL) | |
1314 | free (error_name); | |
d1b2b2dc | 1315 | error_name = xstrdup (name); |
252b5132 RH |
1316 | } |
1317 | ||
1318 | if (section != NULL) | |
1319 | { | |
1320 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
b6f29aaa L |
1321 | { |
1322 | einfo (_("%C: undefined reference to `%T'\n"), | |
1323 | abfd, section, address, name); | |
1324 | if (fatal) | |
1325 | einfo ("%X"); | |
1326 | } | |
252b5132 RH |
1327 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
1328 | einfo (_("%D: more undefined references to `%T' follow\n"), | |
1329 | abfd, section, address, name); | |
1330 | } | |
1331 | else | |
1332 | { | |
1333 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
b6f29aaa L |
1334 | { |
1335 | einfo (_("%B: undefined reference to `%T'\n"), | |
1336 | abfd, name); | |
1337 | if (fatal) | |
1338 | einfo ("%X"); | |
1339 | } | |
252b5132 RH |
1340 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
1341 | einfo (_("%B: more undefined references to `%T' follow\n"), | |
1342 | abfd, name); | |
1343 | } | |
1344 | ||
b34976b6 | 1345 | return TRUE; |
252b5132 RH |
1346 | } |
1347 | ||
1348 | /* This is called when a reloc overflows. */ | |
1349 | ||
b34976b6 | 1350 | static bfd_boolean |
252b5132 | 1351 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) |
87f2a346 | 1352 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1353 | const char *name; |
1354 | const char *reloc_name; | |
1355 | bfd_vma addend; | |
1356 | bfd *abfd; | |
1357 | asection *section; | |
1358 | bfd_vma address; | |
1359 | { | |
1360 | if (abfd == (bfd *) NULL) | |
1361 | einfo (_("%P%X: generated")); | |
1362 | else | |
1363 | einfo ("%X%C:", abfd, section, address); | |
1364 | einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); | |
1365 | if (addend != 0) | |
1366 | einfo ("+%v", addend); | |
1367 | einfo ("\n"); | |
b34976b6 | 1368 | return TRUE; |
252b5132 RH |
1369 | } |
1370 | ||
1371 | /* This is called when a dangerous relocation is made. */ | |
1372 | ||
b34976b6 | 1373 | static bfd_boolean |
252b5132 | 1374 | reloc_dangerous (info, message, abfd, section, address) |
87f2a346 | 1375 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1376 | const char *message; |
1377 | bfd *abfd; | |
1378 | asection *section; | |
1379 | bfd_vma address; | |
1380 | { | |
1381 | if (abfd == (bfd *) NULL) | |
1382 | einfo (_("%P%X: generated")); | |
1383 | else | |
1384 | einfo ("%X%C:", abfd, section, address); | |
1385 | einfo (_("dangerous relocation: %s\n"), message); | |
b34976b6 | 1386 | return TRUE; |
252b5132 RH |
1387 | } |
1388 | ||
1389 | /* This is called when a reloc is being generated attached to a symbol | |
1390 | that is not being output. */ | |
1391 | ||
b34976b6 | 1392 | static bfd_boolean |
252b5132 | 1393 | unattached_reloc (info, name, abfd, section, address) |
87f2a346 | 1394 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1395 | const char *name; |
1396 | bfd *abfd; | |
1397 | asection *section; | |
1398 | bfd_vma address; | |
1399 | { | |
1400 | if (abfd == (bfd *) NULL) | |
1401 | einfo (_("%P%X: generated")); | |
1402 | else | |
1403 | einfo ("%X%C:", abfd, section, address); | |
1404 | einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name); | |
b34976b6 | 1405 | return TRUE; |
252b5132 RH |
1406 | } |
1407 | ||
1408 | /* This is called if link_info.notice_all is set, or when a symbol in | |
1409 | link_info.notice_hash is found. Symbols are put in notice_hash | |
1410 | using the -y option. */ | |
1411 | ||
b34976b6 | 1412 | static bfd_boolean |
252b5132 RH |
1413 | notice (info, name, abfd, section, value) |
1414 | struct bfd_link_info *info; | |
1415 | const char *name; | |
1416 | bfd *abfd; | |
1417 | asection *section; | |
1418 | bfd_vma value; | |
1419 | { | |
1420 | if (! info->notice_all | |
1421 | || (info->notice_hash != NULL | |
b34976b6 | 1422 | && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)) |
252b5132 RH |
1423 | { |
1424 | if (bfd_is_und_section (section)) | |
1425 | einfo ("%B: reference to %s\n", abfd, name); | |
1426 | else | |
1427 | einfo ("%B: definition of %s\n", abfd, name); | |
1428 | } | |
1429 | ||
1430 | if (command_line.cref || nocrossref_list != NULL) | |
1431 | add_cref (name, abfd, section, value); | |
1432 | ||
b34976b6 | 1433 | return TRUE; |
252b5132 | 1434 | } |