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