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