| 1 | /* Main program of GNU linker. |
| 2 | Copyright (C) 1991-2020 Free Software Foundation, Inc. |
| 3 | Written by Steve Chamberlain steve@cygnus.com |
| 4 | |
| 5 | This file is part of the GNU Binutils. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 20 | MA 02110-1301, USA. */ |
| 21 | |
| 22 | #include "sysdep.h" |
| 23 | #include "bfd.h" |
| 24 | #include "safe-ctype.h" |
| 25 | #include "libiberty.h" |
| 26 | #include "progress.h" |
| 27 | #include "bfdlink.h" |
| 28 | #include "ctf-api.h" |
| 29 | #include "filenames.h" |
| 30 | |
| 31 | #include "ld.h" |
| 32 | #include "ldmain.h" |
| 33 | #include "ldmisc.h" |
| 34 | #include "ldwrite.h" |
| 35 | #include "ldexp.h" |
| 36 | #include "ldlang.h" |
| 37 | #include <ldgram.h> |
| 38 | #include "ldlex.h" |
| 39 | #include "ldfile.h" |
| 40 | #include "ldemul.h" |
| 41 | #include "ldctor.h" |
| 42 | #ifdef ENABLE_PLUGINS |
| 43 | #include "plugin.h" |
| 44 | #include "plugin-api.h" |
| 45 | #endif /* ENABLE_PLUGINS */ |
| 46 | |
| 47 | /* Somewhere above, sys/stat.h got included. */ |
| 48 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
| 49 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 50 | #endif |
| 51 | |
| 52 | #include <string.h> |
| 53 | |
| 54 | #ifndef TARGET_SYSTEM_ROOT |
| 55 | #define TARGET_SYSTEM_ROOT "" |
| 56 | #endif |
| 57 | |
| 58 | /* EXPORTS */ |
| 59 | |
| 60 | FILE *saved_script_handle = NULL; |
| 61 | FILE *previous_script_handle = NULL; |
| 62 | bfd_boolean force_make_executable = FALSE; |
| 63 | |
| 64 | char *default_target; |
| 65 | const char *output_filename = "a.out"; |
| 66 | |
| 67 | /* Name this program was invoked by. */ |
| 68 | char *program_name; |
| 69 | |
| 70 | /* The prefix for system library directories. */ |
| 71 | const char *ld_sysroot; |
| 72 | |
| 73 | /* The canonical representation of ld_sysroot. */ |
| 74 | char *ld_canon_sysroot; |
| 75 | int ld_canon_sysroot_len; |
| 76 | |
| 77 | /* Set by -G argument, for targets like MIPS ELF. */ |
| 78 | int g_switch_value = 8; |
| 79 | |
| 80 | /* Nonzero means print names of input files as processed. */ |
| 81 | unsigned int trace_files; |
| 82 | |
| 83 | /* Nonzero means report actions taken by the linker, and describe the linker script in use. */ |
| 84 | bfd_boolean verbose; |
| 85 | |
| 86 | /* Nonzero means version number was printed, so exit successfully |
| 87 | instead of complaining if no input files are given. */ |
| 88 | bfd_boolean version_printed; |
| 89 | |
| 90 | /* TRUE if we should demangle symbol names. */ |
| 91 | bfd_boolean demangling; |
| 92 | |
| 93 | args_type command_line; |
| 94 | |
| 95 | ld_config_type config; |
| 96 | |
| 97 | sort_type sort_section; |
| 98 | |
| 99 | static const char *get_sysroot |
| 100 | (int, char **); |
| 101 | static char *get_emulation |
| 102 | (int, char **); |
| 103 | static bfd_boolean add_archive_element |
| 104 | (struct bfd_link_info *, bfd *, const char *, bfd **); |
| 105 | static void multiple_definition |
| 106 | (struct bfd_link_info *, struct bfd_link_hash_entry *, |
| 107 | bfd *, asection *, bfd_vma); |
| 108 | static void multiple_common |
| 109 | (struct bfd_link_info *, struct bfd_link_hash_entry *, |
| 110 | bfd *, enum bfd_link_hash_type, bfd_vma); |
| 111 | static void add_to_set |
| 112 | (struct bfd_link_info *, struct bfd_link_hash_entry *, |
| 113 | bfd_reloc_code_real_type, bfd *, asection *, bfd_vma); |
| 114 | static void constructor_callback |
| 115 | (struct bfd_link_info *, bfd_boolean, const char *, bfd *, |
| 116 | asection *, bfd_vma); |
| 117 | static void warning_callback |
| 118 | (struct bfd_link_info *, const char *, const char *, bfd *, |
| 119 | asection *, bfd_vma); |
| 120 | static void warning_find_reloc |
| 121 | (bfd *, asection *, void *); |
| 122 | static void undefined_symbol |
| 123 | (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, |
| 124 | bfd_boolean); |
| 125 | static void reloc_overflow |
| 126 | (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *, |
| 127 | const char *, bfd_vma, bfd *, asection *, bfd_vma); |
| 128 | static void reloc_dangerous |
| 129 | (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma); |
| 130 | static void unattached_reloc |
| 131 | (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma); |
| 132 | static bfd_boolean notice |
| 133 | (struct bfd_link_info *, struct bfd_link_hash_entry *, |
| 134 | struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword); |
| 135 | |
| 136 | static struct bfd_link_callbacks link_callbacks = |
| 137 | { |
| 138 | add_archive_element, |
| 139 | multiple_definition, |
| 140 | multiple_common, |
| 141 | add_to_set, |
| 142 | constructor_callback, |
| 143 | warning_callback, |
| 144 | undefined_symbol, |
| 145 | reloc_overflow, |
| 146 | reloc_dangerous, |
| 147 | unattached_reloc, |
| 148 | notice, |
| 149 | einfo, |
| 150 | info_msg, |
| 151 | minfo, |
| 152 | ldlang_override_segment_assignment, |
| 153 | ldlang_ctf_apply_strsym, |
| 154 | ldlang_write_ctf_late |
| 155 | }; |
| 156 | |
| 157 | static bfd_assert_handler_type default_bfd_assert_handler; |
| 158 | static bfd_error_handler_type default_bfd_error_handler; |
| 159 | |
| 160 | struct bfd_link_info link_info; |
| 161 | \f |
| 162 | static void |
| 163 | ld_cleanup (void) |
| 164 | { |
| 165 | bfd_cache_close_all (); |
| 166 | #ifdef ENABLE_PLUGINS |
| 167 | plugin_call_cleanup (); |
| 168 | #endif |
| 169 | if (output_filename && delete_output_file_on_failure) |
| 170 | unlink_if_ordinary (output_filename); |
| 171 | } |
| 172 | |
| 173 | /* Hook to notice BFD assertions. */ |
| 174 | |
| 175 | static void |
| 176 | ld_bfd_assert_handler (const char *fmt, const char *bfdver, |
| 177 | const char *file, int line) |
| 178 | { |
| 179 | config.make_executable = FALSE; |
| 180 | (*default_bfd_assert_handler) (fmt, bfdver, file, line); |
| 181 | } |
| 182 | |
| 183 | /* Hook the bfd error/warning handler for --fatal-warnings. */ |
| 184 | |
| 185 | static void |
| 186 | ld_bfd_error_handler (const char *fmt, va_list ap) |
| 187 | { |
| 188 | if (config.fatal_warnings) |
| 189 | config.make_executable = FALSE; |
| 190 | (*default_bfd_error_handler) (fmt, ap); |
| 191 | } |
| 192 | |
| 193 | int |
| 194 | main (int argc, char **argv) |
| 195 | { |
| 196 | char *emulation; |
| 197 | long start_time = get_run_time (); |
| 198 | |
| 199 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) |
| 200 | setlocale (LC_MESSAGES, ""); |
| 201 | #endif |
| 202 | #if defined (HAVE_SETLOCALE) |
| 203 | setlocale (LC_CTYPE, ""); |
| 204 | #endif |
| 205 | bindtextdomain (PACKAGE, LOCALEDIR); |
| 206 | textdomain (PACKAGE); |
| 207 | |
| 208 | program_name = argv[0]; |
| 209 | xmalloc_set_program_name (program_name); |
| 210 | |
| 211 | START_PROGRESS (program_name, 0); |
| 212 | |
| 213 | expandargv (&argc, &argv); |
| 214 | |
| 215 | if (bfd_init () != BFD_INIT_MAGIC) |
| 216 | einfo (_("%F%P: fatal error: libbfd ABI mismatch\n")); |
| 217 | |
| 218 | bfd_set_error_program_name (program_name); |
| 219 | |
| 220 | /* We want to notice and fail on those nasty BFD assertions which are |
| 221 | likely to signal incorrect output being generated but otherwise may |
| 222 | leave no trace. */ |
| 223 | default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler); |
| 224 | |
| 225 | /* Also hook the bfd error/warning handler for --fatal-warnings. */ |
| 226 | default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler); |
| 227 | |
| 228 | xatexit (ld_cleanup); |
| 229 | |
| 230 | /* Set up the sysroot directory. */ |
| 231 | ld_sysroot = get_sysroot (argc, argv); |
| 232 | if (*ld_sysroot) |
| 233 | ld_canon_sysroot = lrealpath (ld_sysroot); |
| 234 | if (ld_canon_sysroot) |
| 235 | { |
| 236 | ld_canon_sysroot_len = strlen (ld_canon_sysroot); |
| 237 | |
| 238 | /* is_sysrooted_pathname() relies on no trailing dirsep. */ |
| 239 | if (ld_canon_sysroot_len > 0 |
| 240 | && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1])) |
| 241 | ld_canon_sysroot [--ld_canon_sysroot_len] = '\0'; |
| 242 | } |
| 243 | else |
| 244 | ld_canon_sysroot_len = -1; |
| 245 | |
| 246 | /* Set the default BFD target based on the configured target. Doing |
| 247 | this permits the linker to be configured for a particular target, |
| 248 | and linked against a shared BFD library which was configured for |
| 249 | a different target. The macro TARGET is defined by Makefile. */ |
| 250 | if (!bfd_set_default_target (TARGET)) |
| 251 | { |
| 252 | einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); |
| 253 | xexit (1); |
| 254 | } |
| 255 | |
| 256 | #if YYDEBUG |
| 257 | { |
| 258 | extern int yydebug; |
| 259 | yydebug = 1; |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | config.build_constructors = TRUE; |
| 264 | config.rpath_separator = ':'; |
| 265 | config.split_by_reloc = (unsigned) -1; |
| 266 | config.split_by_file = (bfd_size_type) -1; |
| 267 | config.make_executable = TRUE; |
| 268 | config.magic_demand_paged = TRUE; |
| 269 | config.text_read_only = TRUE; |
| 270 | config.print_map_discarded = TRUE; |
| 271 | link_info.disable_target_specific_optimizations = -1; |
| 272 | |
| 273 | command_line.warn_mismatch = TRUE; |
| 274 | command_line.warn_search_mismatch = TRUE; |
| 275 | command_line.check_section_addresses = -1; |
| 276 | |
| 277 | /* We initialize DEMANGLING based on the environment variable |
| 278 | COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the |
| 279 | output of the linker, unless COLLECT_NO_DEMANGLE is set in the |
| 280 | environment. Acting the same way here lets us provide the same |
| 281 | interface by default. */ |
| 282 | demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; |
| 283 | |
| 284 | link_info.allow_undefined_version = TRUE; |
| 285 | link_info.keep_memory = TRUE; |
| 286 | link_info.combreloc = TRUE; |
| 287 | link_info.strip_discarded = TRUE; |
| 288 | link_info.prohibit_multiple_definition_absolute = FALSE; |
| 289 | link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH; |
| 290 | link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH; |
| 291 | link_info.callbacks = &link_callbacks; |
| 292 | link_info.input_bfds_tail = &link_info.input_bfds; |
| 293 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init |
| 294 | and _fini symbols. We are compatible. */ |
| 295 | link_info.init_function = "_init"; |
| 296 | link_info.fini_function = "_fini"; |
| 297 | link_info.relax_pass = 1; |
| 298 | link_info.extern_protected_data = -1; |
| 299 | link_info.dynamic_undefined_weak = -1; |
| 300 | link_info.pei386_auto_import = -1; |
| 301 | link_info.spare_dynamic_tags = 5; |
| 302 | link_info.path_separator = ':'; |
| 303 | #ifdef DEFAULT_FLAG_COMPRESS_DEBUG |
| 304 | link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB; |
| 305 | #endif |
| 306 | #ifdef DEFAULT_NEW_DTAGS |
| 307 | link_info.new_dtags = DEFAULT_NEW_DTAGS; |
| 308 | #endif |
| 309 | |
| 310 | ldfile_add_arch (""); |
| 311 | emulation = get_emulation (argc, argv); |
| 312 | ldemul_choose_mode (emulation); |
| 313 | default_target = ldemul_choose_target (argc, argv); |
| 314 | lang_init (); |
| 315 | ldexp_init (); |
| 316 | ldemul_before_parse (); |
| 317 | lang_has_input_file = FALSE; |
| 318 | parse_args (argc, argv); |
| 319 | |
| 320 | if (config.hash_table_size != 0) |
| 321 | bfd_hash_set_default_size (config.hash_table_size); |
| 322 | |
| 323 | #ifdef ENABLE_PLUGINS |
| 324 | /* Now all the plugin arguments have been gathered, we can load them. */ |
| 325 | plugin_load_plugins (); |
| 326 | #endif /* ENABLE_PLUGINS */ |
| 327 | |
| 328 | ldemul_set_symbols (); |
| 329 | |
| 330 | /* If we have not already opened and parsed a linker script, |
| 331 | try the default script from command line first. */ |
| 332 | if (saved_script_handle == NULL |
| 333 | && command_line.default_script != NULL) |
| 334 | { |
| 335 | ldfile_open_script_file (command_line.default_script); |
| 336 | parser_input = input_script; |
| 337 | yyparse (); |
| 338 | } |
| 339 | |
| 340 | /* If we have not already opened and parsed a linker script |
| 341 | read the emulation's appropriate default script. */ |
| 342 | if (saved_script_handle == NULL) |
| 343 | { |
| 344 | int isfile; |
| 345 | char *s = ldemul_get_script (&isfile); |
| 346 | |
| 347 | if (isfile) |
| 348 | ldfile_open_default_command_file (s); |
| 349 | else |
| 350 | { |
| 351 | lex_string = s; |
| 352 | lex_redirect (s, _("built in linker script"), 1); |
| 353 | } |
| 354 | parser_input = input_script; |
| 355 | yyparse (); |
| 356 | lex_string = NULL; |
| 357 | } |
| 358 | |
| 359 | if (verbose) |
| 360 | { |
| 361 | if (saved_script_handle) |
| 362 | info_msg (_("using external linker script:")); |
| 363 | else |
| 364 | info_msg (_("using internal linker script:")); |
| 365 | info_msg ("\n==================================================\n"); |
| 366 | |
| 367 | if (saved_script_handle) |
| 368 | { |
| 369 | static const int ld_bufsz = 8193; |
| 370 | size_t n; |
| 371 | char *buf = (char *) xmalloc (ld_bufsz); |
| 372 | |
| 373 | rewind (saved_script_handle); |
| 374 | while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0) |
| 375 | { |
| 376 | buf[n] = 0; |
| 377 | info_msg ("%s", buf); |
| 378 | } |
| 379 | rewind (saved_script_handle); |
| 380 | free (buf); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | int isfile; |
| 385 | |
| 386 | info_msg (ldemul_get_script (&isfile)); |
| 387 | } |
| 388 | |
| 389 | info_msg ("\n==================================================\n"); |
| 390 | } |
| 391 | |
| 392 | if (command_line.force_group_allocation |
| 393 | || !bfd_link_relocatable (&link_info)) |
| 394 | link_info.resolve_section_groups = TRUE; |
| 395 | else |
| 396 | link_info.resolve_section_groups = FALSE; |
| 397 | |
| 398 | if (command_line.print_output_format) |
| 399 | info_msg ("%s\n", lang_get_output_target ()); |
| 400 | |
| 401 | lang_final (); |
| 402 | |
| 403 | /* If the only command line argument has been -v or --version or --verbose |
| 404 | then ignore any input files provided by linker scripts and exit now. |
| 405 | We do not want to create an output file when the linker is just invoked |
| 406 | to provide version information. */ |
| 407 | if (argc == 2 && version_printed) |
| 408 | xexit (0); |
| 409 | |
| 410 | if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info)) |
| 411 | einfo (_("%F%P: --no-define-common may not be used without -shared\n")); |
| 412 | |
| 413 | if (!lang_has_input_file) |
| 414 | { |
| 415 | if (version_printed || command_line.print_output_format) |
| 416 | xexit (0); |
| 417 | einfo (_("%F%P: no input files\n")); |
| 418 | } |
| 419 | |
| 420 | if (verbose) |
| 421 | info_msg (_("%P: mode %s\n"), emulation); |
| 422 | |
| 423 | ldemul_after_parse (); |
| 424 | |
| 425 | if (config.map_filename) |
| 426 | { |
| 427 | if (strcmp (config.map_filename, "-") == 0) |
| 428 | { |
| 429 | config.map_file = stdout; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | config.map_file = fopen (config.map_filename, FOPEN_WT); |
| 434 | if (config.map_file == (FILE *) NULL) |
| 435 | { |
| 436 | bfd_set_error (bfd_error_system_call); |
| 437 | einfo (_("%F%P: cannot open map file %s: %E\n"), |
| 438 | config.map_filename); |
| 439 | } |
| 440 | } |
| 441 | link_info.has_map_file = TRUE; |
| 442 | } |
| 443 | |
| 444 | lang_process (); |
| 445 | |
| 446 | /* Print error messages for any missing symbols, for any warning |
| 447 | symbols, and possibly multiple definitions. */ |
| 448 | if (bfd_link_relocatable (&link_info)) |
| 449 | link_info.output_bfd->flags &= ~EXEC_P; |
| 450 | else |
| 451 | link_info.output_bfd->flags |= EXEC_P; |
| 452 | |
| 453 | if ((link_info.compress_debug & COMPRESS_DEBUG)) |
| 454 | { |
| 455 | link_info.output_bfd->flags |= BFD_COMPRESS; |
| 456 | if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB) |
| 457 | link_info.output_bfd->flags |= BFD_COMPRESS_GABI; |
| 458 | } |
| 459 | |
| 460 | ldwrite (); |
| 461 | |
| 462 | if (config.map_file != NULL) |
| 463 | lang_map (); |
| 464 | if (command_line.cref) |
| 465 | output_cref (config.map_file != NULL ? config.map_file : stdout); |
| 466 | if (nocrossref_list != NULL) |
| 467 | check_nocrossrefs (); |
| 468 | if (command_line.print_memory_usage) |
| 469 | lang_print_memory_usage (); |
| 470 | #if 0 |
| 471 | { |
| 472 | struct bfd_link_hash_entry *h; |
| 473 | |
| 474 | h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1); |
| 475 | fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1); |
| 476 | } |
| 477 | #endif |
| 478 | ldexp_finish (); |
| 479 | lang_finish (); |
| 480 | |
| 481 | /* Even if we're producing relocatable output, some non-fatal errors should |
| 482 | be reported in the exit status. (What non-fatal errors, if any, do we |
| 483 | want to ignore for relocatable output?) */ |
| 484 | if (!config.make_executable && !force_make_executable) |
| 485 | { |
| 486 | if (verbose) |
| 487 | einfo (_("%P: link errors found, deleting executable `%s'\n"), |
| 488 | output_filename); |
| 489 | |
| 490 | /* The file will be removed by ld_cleanup. */ |
| 491 | xexit (1); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | if (!bfd_close (link_info.output_bfd)) |
| 496 | einfo (_("%F%P: %pB: final close failed: %E\n"), link_info.output_bfd); |
| 497 | |
| 498 | /* If the --force-exe-suffix is enabled, and we're making an |
| 499 | executable file and it doesn't end in .exe, copy it to one |
| 500 | which does. */ |
| 501 | if (!bfd_link_relocatable (&link_info) |
| 502 | && command_line.force_exe_suffix) |
| 503 | { |
| 504 | int len = strlen (output_filename); |
| 505 | |
| 506 | if (len < 4 |
| 507 | || (strcasecmp (output_filename + len - 4, ".exe") != 0 |
| 508 | && strcasecmp (output_filename + len - 4, ".dll") != 0)) |
| 509 | { |
| 510 | FILE *src; |
| 511 | FILE *dst; |
| 512 | const int bsize = 4096; |
| 513 | char *buf = (char *) xmalloc (bsize); |
| 514 | int l; |
| 515 | char *dst_name = (char *) xmalloc (len + 5); |
| 516 | |
| 517 | strcpy (dst_name, output_filename); |
| 518 | strcat (dst_name, ".exe"); |
| 519 | src = fopen (output_filename, FOPEN_RB); |
| 520 | dst = fopen (dst_name, FOPEN_WB); |
| 521 | |
| 522 | if (!src) |
| 523 | einfo (_("%F%P: unable to open for source of copy `%s'\n"), |
| 524 | output_filename); |
| 525 | if (!dst) |
| 526 | einfo (_("%F%P: unable to open for destination of copy `%s'\n"), |
| 527 | dst_name); |
| 528 | while ((l = fread (buf, 1, bsize, src)) > 0) |
| 529 | { |
| 530 | int done = fwrite (buf, 1, l, dst); |
| 531 | |
| 532 | if (done != l) |
| 533 | einfo (_("%P: error writing file `%s'\n"), dst_name); |
| 534 | } |
| 535 | |
| 536 | fclose (src); |
| 537 | if (fclose (dst) == EOF) |
| 538 | einfo (_("%P: error closing file `%s'\n"), dst_name); |
| 539 | free (dst_name); |
| 540 | free (buf); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | END_PROGRESS (program_name); |
| 546 | |
| 547 | if (config.stats) |
| 548 | { |
| 549 | long run_time = get_run_time () - start_time; |
| 550 | |
| 551 | fflush (stdout); |
| 552 | fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), |
| 553 | program_name, run_time / 1000000, run_time % 1000000); |
| 554 | fflush (stderr); |
| 555 | } |
| 556 | |
| 557 | /* Prevent ld_cleanup from doing anything, after a successful link. */ |
| 558 | output_filename = NULL; |
| 559 | |
| 560 | xexit (0); |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* If the configured sysroot is relocatable, try relocating it based on |
| 565 | default prefix FROM. Return the relocated directory if it exists, |
| 566 | otherwise return null. */ |
| 567 | |
| 568 | static char * |
| 569 | get_relative_sysroot (const char *from ATTRIBUTE_UNUSED) |
| 570 | { |
| 571 | #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE |
| 572 | char *path; |
| 573 | struct stat s; |
| 574 | |
| 575 | path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT); |
| 576 | if (path) |
| 577 | { |
| 578 | if (stat (path, &s) == 0 && S_ISDIR (s.st_mode)) |
| 579 | return path; |
| 580 | free (path); |
| 581 | } |
| 582 | #endif |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* Return the sysroot directory. Return "" if no sysroot is being used. */ |
| 587 | |
| 588 | static const char * |
| 589 | get_sysroot (int argc, char **argv) |
| 590 | { |
| 591 | int i; |
| 592 | const char *path = NULL; |
| 593 | |
| 594 | for (i = 1; i < argc; i++) |
| 595 | if (CONST_STRNEQ (argv[i], "--sysroot=")) |
| 596 | path = argv[i] + strlen ("--sysroot="); |
| 597 | |
| 598 | if (!path) |
| 599 | path = get_relative_sysroot (BINDIR); |
| 600 | |
| 601 | if (!path) |
| 602 | path = get_relative_sysroot (TOOLBINDIR); |
| 603 | |
| 604 | if (!path) |
| 605 | path = TARGET_SYSTEM_ROOT; |
| 606 | |
| 607 | if (IS_DIR_SEPARATOR (*path) && path[1] == 0) |
| 608 | path = ""; |
| 609 | |
| 610 | return path; |
| 611 | } |
| 612 | |
| 613 | /* We need to find any explicitly given emulation in order to initialize the |
| 614 | state that's needed by the lex&yacc argument parser (parse_args). */ |
| 615 | |
| 616 | static char * |
| 617 | get_emulation (int argc, char **argv) |
| 618 | { |
| 619 | char *emulation; |
| 620 | int i; |
| 621 | |
| 622 | emulation = getenv (EMULATION_ENVIRON); |
| 623 | if (emulation == NULL) |
| 624 | emulation = DEFAULT_EMULATION; |
| 625 | |
| 626 | for (i = 1; i < argc; i++) |
| 627 | { |
| 628 | if (CONST_STRNEQ (argv[i], "-m")) |
| 629 | { |
| 630 | if (argv[i][2] == '\0') |
| 631 | { |
| 632 | /* -m EMUL */ |
| 633 | if (i < argc - 1) |
| 634 | { |
| 635 | emulation = argv[i + 1]; |
| 636 | i++; |
| 637 | } |
| 638 | else |
| 639 | einfo (_("%F%P: missing argument to -m\n")); |
| 640 | } |
| 641 | else if (strcmp (argv[i], "-mips1") == 0 |
| 642 | || strcmp (argv[i], "-mips2") == 0 |
| 643 | || strcmp (argv[i], "-mips3") == 0 |
| 644 | || strcmp (argv[i], "-mips4") == 0 |
| 645 | || strcmp (argv[i], "-mips5") == 0 |
| 646 | || strcmp (argv[i], "-mips32") == 0 |
| 647 | || strcmp (argv[i], "-mips32r2") == 0 |
| 648 | || strcmp (argv[i], "-mips32r6") == 0 |
| 649 | || strcmp (argv[i], "-mips64") == 0 |
| 650 | || strcmp (argv[i], "-mips64r2") == 0 |
| 651 | || strcmp (argv[i], "-mips64r6") == 0) |
| 652 | { |
| 653 | /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are |
| 654 | passed to the linker by some MIPS compilers. They |
| 655 | generally tell the linker to use a slightly different |
| 656 | library path. Perhaps someday these should be |
| 657 | implemented as emulations; until then, we just ignore |
| 658 | the arguments and hope that nobody ever creates |
| 659 | emulations named ips1, ips2 or ips3. */ |
| 660 | } |
| 661 | else if (strcmp (argv[i], "-m486") == 0) |
| 662 | { |
| 663 | /* FIXME: The argument -m486 is passed to the linker on |
| 664 | some Linux systems. Hope that nobody creates an |
| 665 | emulation named 486. */ |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | /* -mEMUL */ |
| 670 | emulation = &argv[i][2]; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | return emulation; |
| 676 | } |
| 677 | |
| 678 | void |
| 679 | add_ysym (const char *name) |
| 680 | { |
| 681 | if (link_info.notice_hash == NULL) |
| 682 | { |
| 683 | link_info.notice_hash |
| 684 | = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table)); |
| 685 | if (!bfd_hash_table_init_n (link_info.notice_hash, |
| 686 | bfd_hash_newfunc, |
| 687 | sizeof (struct bfd_hash_entry), |
| 688 | 61)) |
| 689 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); |
| 690 | } |
| 691 | |
| 692 | if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL) |
| 693 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
| 694 | } |
| 695 | |
| 696 | void |
| 697 | add_ignoresym (struct bfd_link_info *info, const char *name) |
| 698 | { |
| 699 | if (info->ignore_hash == NULL) |
| 700 | { |
| 701 | info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table)); |
| 702 | if (!bfd_hash_table_init_n (info->ignore_hash, |
| 703 | bfd_hash_newfunc, |
| 704 | sizeof (struct bfd_hash_entry), |
| 705 | 61)) |
| 706 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); |
| 707 | } |
| 708 | |
| 709 | if (bfd_hash_lookup (info->ignore_hash, name, TRUE, TRUE) == NULL) |
| 710 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
| 711 | } |
| 712 | |
| 713 | /* Record a symbol to be wrapped, from the --wrap option. */ |
| 714 | |
| 715 | void |
| 716 | add_wrap (const char *name) |
| 717 | { |
| 718 | if (link_info.wrap_hash == NULL) |
| 719 | { |
| 720 | link_info.wrap_hash |
| 721 | = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table)); |
| 722 | if (!bfd_hash_table_init_n (link_info.wrap_hash, |
| 723 | bfd_hash_newfunc, |
| 724 | sizeof (struct bfd_hash_entry), |
| 725 | 61)) |
| 726 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); |
| 727 | } |
| 728 | |
| 729 | if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL) |
| 730 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
| 731 | } |
| 732 | |
| 733 | /* Handle the -retain-symbols-file option. */ |
| 734 | |
| 735 | void |
| 736 | add_keepsyms_file (const char *filename) |
| 737 | { |
| 738 | FILE *file; |
| 739 | char *buf; |
| 740 | size_t bufsize; |
| 741 | int c; |
| 742 | |
| 743 | if (link_info.strip == strip_some) |
| 744 | einfo (_("%X%P: error: duplicate retain-symbols-file\n")); |
| 745 | |
| 746 | file = fopen (filename, "r"); |
| 747 | if (file == NULL) |
| 748 | { |
| 749 | bfd_set_error (bfd_error_system_call); |
| 750 | einfo ("%X%P: %s: %E\n", filename); |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | link_info.keep_hash = (struct bfd_hash_table *) |
| 755 | xmalloc (sizeof (struct bfd_hash_table)); |
| 756 | if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc, |
| 757 | sizeof (struct bfd_hash_entry))) |
| 758 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); |
| 759 | |
| 760 | bufsize = 100; |
| 761 | buf = (char *) xmalloc (bufsize); |
| 762 | |
| 763 | c = getc (file); |
| 764 | while (c != EOF) |
| 765 | { |
| 766 | while (ISSPACE (c)) |
| 767 | c = getc (file); |
| 768 | |
| 769 | if (c != EOF) |
| 770 | { |
| 771 | size_t len = 0; |
| 772 | |
| 773 | while (!ISSPACE (c) && c != EOF) |
| 774 | { |
| 775 | buf[len] = c; |
| 776 | ++len; |
| 777 | if (len >= bufsize) |
| 778 | { |
| 779 | bufsize *= 2; |
| 780 | buf = (char *) xrealloc (buf, bufsize); |
| 781 | } |
| 782 | c = getc (file); |
| 783 | } |
| 784 | |
| 785 | buf[len] = '\0'; |
| 786 | |
| 787 | if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL) |
| 788 | einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n")); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | if (link_info.strip != strip_none) |
| 793 | einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); |
| 794 | |
| 795 | free (buf); |
| 796 | link_info.strip = strip_some; |
| 797 | fclose (file); |
| 798 | } |
| 799 | \f |
| 800 | /* Callbacks from the BFD linker routines. */ |
| 801 | |
| 802 | /* This is called when BFD has decided to include an archive member in |
| 803 | a link. */ |
| 804 | |
| 805 | static bfd_boolean |
| 806 | add_archive_element (struct bfd_link_info *info, |
| 807 | bfd *abfd, |
| 808 | const char *name, |
| 809 | bfd **subsbfd ATTRIBUTE_UNUSED) |
| 810 | { |
| 811 | lang_input_statement_type *input; |
| 812 | lang_input_statement_type *parent; |
| 813 | lang_input_statement_type orig_input; |
| 814 | |
| 815 | input = (lang_input_statement_type *) |
| 816 | xcalloc (1, sizeof (lang_input_statement_type)); |
| 817 | input->header.type = lang_input_statement_enum; |
| 818 | input->filename = abfd->filename; |
| 819 | input->local_sym_name = abfd->filename; |
| 820 | input->the_bfd = abfd; |
| 821 | |
| 822 | parent = bfd_usrdata (abfd->my_archive); |
| 823 | if (parent != NULL && !parent->flags.reload) |
| 824 | parent->next = input; |
| 825 | |
| 826 | /* Save the original data for trace files/tries below, as plugins |
| 827 | (if enabled) may possibly alter it to point to a replacement |
| 828 | BFD, but we still want to output the original BFD filename. */ |
| 829 | orig_input = *input; |
| 830 | #ifdef ENABLE_PLUGINS |
| 831 | if (link_info.lto_plugin_active) |
| 832 | { |
| 833 | /* We must offer this archive member to the plugins to claim. */ |
| 834 | plugin_maybe_claim (input); |
| 835 | if (input->flags.claimed) |
| 836 | { |
| 837 | if (no_more_claiming) |
| 838 | { |
| 839 | /* Don't claim new IR symbols after all IR symbols have |
| 840 | been claimed. */ |
| 841 | if (verbose) |
| 842 | info_msg ("%pI: no new IR symbols to claimi\n", |
| 843 | &orig_input); |
| 844 | input->flags.claimed = 0; |
| 845 | return FALSE; |
| 846 | } |
| 847 | input->flags.claim_archive = TRUE; |
| 848 | *subsbfd = input->the_bfd; |
| 849 | } |
| 850 | } |
| 851 | #endif /* ENABLE_PLUGINS */ |
| 852 | |
| 853 | ldlang_add_file (input); |
| 854 | |
| 855 | if (config.map_file != NULL) |
| 856 | { |
| 857 | static bfd_boolean header_printed; |
| 858 | struct bfd_link_hash_entry *h; |
| 859 | bfd *from; |
| 860 | int len; |
| 861 | |
| 862 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); |
| 863 | if (h == NULL |
| 864 | && info->pei386_auto_import |
| 865 | && CONST_STRNEQ (name, "__imp_")) |
| 866 | h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE); |
| 867 | |
| 868 | if (h == NULL) |
| 869 | from = NULL; |
| 870 | else |
| 871 | { |
| 872 | switch (h->type) |
| 873 | { |
| 874 | default: |
| 875 | from = NULL; |
| 876 | break; |
| 877 | |
| 878 | case bfd_link_hash_defined: |
| 879 | case bfd_link_hash_defweak: |
| 880 | from = h->u.def.section->owner; |
| 881 | break; |
| 882 | |
| 883 | case bfd_link_hash_undefined: |
| 884 | case bfd_link_hash_undefweak: |
| 885 | from = h->u.undef.abfd; |
| 886 | break; |
| 887 | |
| 888 | case bfd_link_hash_common: |
| 889 | from = h->u.c.p->section->owner; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (!header_printed) |
| 895 | { |
| 896 | minfo (_("Archive member included to satisfy reference by file (symbol)\n\n")); |
| 897 | header_printed = TRUE; |
| 898 | } |
| 899 | |
| 900 | if (abfd->my_archive == NULL |
| 901 | || bfd_is_thin_archive (abfd->my_archive)) |
| 902 | { |
| 903 | minfo ("%s", bfd_get_filename (abfd)); |
| 904 | len = strlen (bfd_get_filename (abfd)); |
| 905 | } |
| 906 | else |
| 907 | { |
| 908 | minfo ("%s(%s)", bfd_get_filename (abfd->my_archive), |
| 909 | bfd_get_filename (abfd)); |
| 910 | len = (strlen (bfd_get_filename (abfd->my_archive)) |
| 911 | + strlen (bfd_get_filename (abfd)) |
| 912 | + 2); |
| 913 | } |
| 914 | |
| 915 | if (len >= 29) |
| 916 | { |
| 917 | print_nl (); |
| 918 | len = 0; |
| 919 | } |
| 920 | while (len < 30) |
| 921 | { |
| 922 | print_space (); |
| 923 | ++len; |
| 924 | } |
| 925 | |
| 926 | if (from != NULL) |
| 927 | minfo ("%pB ", from); |
| 928 | if (h != NULL) |
| 929 | minfo ("(%pT)\n", h->root.string); |
| 930 | else |
| 931 | minfo ("(%s)\n", name); |
| 932 | } |
| 933 | |
| 934 | if (verbose |
| 935 | || trace_files > 1 |
| 936 | || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive))) |
| 937 | info_msg ("%pI\n", &orig_input); |
| 938 | return TRUE; |
| 939 | } |
| 940 | |
| 941 | /* This is called when BFD has discovered a symbol which is defined |
| 942 | multiple times. */ |
| 943 | |
| 944 | static void |
| 945 | multiple_definition (struct bfd_link_info *info, |
| 946 | struct bfd_link_hash_entry *h, |
| 947 | bfd *nbfd, |
| 948 | asection *nsec, |
| 949 | bfd_vma nval) |
| 950 | { |
| 951 | const char *name; |
| 952 | bfd *obfd; |
| 953 | asection *osec; |
| 954 | bfd_vma oval; |
| 955 | |
| 956 | if (info->allow_multiple_definition) |
| 957 | return; |
| 958 | |
| 959 | switch (h->type) |
| 960 | { |
| 961 | case bfd_link_hash_defined: |
| 962 | osec = h->u.def.section; |
| 963 | oval = h->u.def.value; |
| 964 | obfd = h->u.def.section->owner; |
| 965 | break; |
| 966 | case bfd_link_hash_indirect: |
| 967 | osec = bfd_ind_section_ptr; |
| 968 | oval = 0; |
| 969 | obfd = NULL; |
| 970 | break; |
| 971 | default: |
| 972 | abort (); |
| 973 | } |
| 974 | |
| 975 | /* Ignore a redefinition of an absolute symbol to the |
| 976 | same value; it's harmless. */ |
| 977 | if (h->type == bfd_link_hash_defined |
| 978 | && bfd_is_abs_section (osec) |
| 979 | && bfd_is_abs_section (nsec) |
| 980 | && nval == oval) |
| 981 | return; |
| 982 | |
| 983 | /* If either section has the output_section field set to |
| 984 | bfd_abs_section_ptr, it means that the section is being |
| 985 | discarded, and this is not really a multiple definition at all. |
| 986 | FIXME: It would be cleaner to somehow ignore symbols defined in |
| 987 | sections which are being discarded. */ |
| 988 | if (!info->prohibit_multiple_definition_absolute |
| 989 | && ((osec->output_section != NULL |
| 990 | && ! bfd_is_abs_section (osec) |
| 991 | && bfd_is_abs_section (osec->output_section)) |
| 992 | || (nsec->output_section != NULL |
| 993 | && !bfd_is_abs_section (nsec) |
| 994 | && bfd_is_abs_section (nsec->output_section)))) |
| 995 | return; |
| 996 | |
| 997 | name = h->root.string; |
| 998 | if (nbfd == NULL) |
| 999 | { |
| 1000 | nbfd = obfd; |
| 1001 | nsec = osec; |
| 1002 | nval = oval; |
| 1003 | obfd = NULL; |
| 1004 | } |
| 1005 | einfo (_("%X%P: %C: multiple definition of `%pT'"), |
| 1006 | nbfd, nsec, nval, name); |
| 1007 | if (obfd != NULL) |
| 1008 | einfo (_("; %D: first defined here"), obfd, osec, oval); |
| 1009 | einfo ("\n"); |
| 1010 | |
| 1011 | if (RELAXATION_ENABLED_BY_USER) |
| 1012 | { |
| 1013 | einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n")); |
| 1014 | DISABLE_RELAXATION; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* This is called when there is a definition of a common symbol, or |
| 1019 | when a common symbol is found for a symbol that is already defined, |
| 1020 | or when two common symbols are found. We only do something if |
| 1021 | -warn-common was used. */ |
| 1022 | |
| 1023 | static void |
| 1024 | multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1025 | struct bfd_link_hash_entry *h, |
| 1026 | bfd *nbfd, |
| 1027 | enum bfd_link_hash_type ntype, |
| 1028 | bfd_vma nsize) |
| 1029 | { |
| 1030 | const char *name; |
| 1031 | bfd *obfd; |
| 1032 | enum bfd_link_hash_type otype; |
| 1033 | bfd_vma osize; |
| 1034 | |
| 1035 | if (!config.warn_common) |
| 1036 | return; |
| 1037 | |
| 1038 | name = h->root.string; |
| 1039 | otype = h->type; |
| 1040 | if (otype == bfd_link_hash_common) |
| 1041 | { |
| 1042 | obfd = h->u.c.p->section->owner; |
| 1043 | osize = h->u.c.size; |
| 1044 | } |
| 1045 | else if (otype == bfd_link_hash_defined |
| 1046 | || otype == bfd_link_hash_defweak) |
| 1047 | { |
| 1048 | obfd = h->u.def.section->owner; |
| 1049 | osize = 0; |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | /* FIXME: It would nice if we could report the BFD which defined |
| 1054 | an indirect symbol, but we don't have anywhere to store the |
| 1055 | information. */ |
| 1056 | obfd = NULL; |
| 1057 | osize = 0; |
| 1058 | } |
| 1059 | |
| 1060 | if (ntype == bfd_link_hash_defined |
| 1061 | || ntype == bfd_link_hash_defweak |
| 1062 | || ntype == bfd_link_hash_indirect) |
| 1063 | { |
| 1064 | ASSERT (otype == bfd_link_hash_common); |
| 1065 | if (obfd != NULL) |
| 1066 | einfo (_("%P: %pB: warning: definition of `%pT' overriding common" |
| 1067 | " from %pB\n"), |
| 1068 | nbfd, name, obfd); |
| 1069 | else |
| 1070 | einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"), |
| 1071 | nbfd, name); |
| 1072 | } |
| 1073 | else if (otype == bfd_link_hash_defined |
| 1074 | || otype == bfd_link_hash_defweak |
| 1075 | || otype == bfd_link_hash_indirect) |
| 1076 | { |
| 1077 | ASSERT (ntype == bfd_link_hash_common); |
| 1078 | if (obfd != NULL) |
| 1079 | einfo (_("%P: %pB: warning: common of `%pT' overridden by definition" |
| 1080 | " from %pB\n"), |
| 1081 | nbfd, name, obfd); |
| 1082 | else |
| 1083 | einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"), |
| 1084 | nbfd, name); |
| 1085 | } |
| 1086 | else |
| 1087 | { |
| 1088 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); |
| 1089 | if (osize > nsize) |
| 1090 | { |
| 1091 | if (obfd != NULL) |
| 1092 | einfo (_("%P: %pB: warning: common of `%pT' overridden" |
| 1093 | " by larger common from %pB\n"), |
| 1094 | nbfd, name, obfd); |
| 1095 | else |
| 1096 | einfo (_("%P: %pB: warning: common of `%pT' overridden" |
| 1097 | " by larger common\n"), |
| 1098 | nbfd, name); |
| 1099 | } |
| 1100 | else if (nsize > osize) |
| 1101 | { |
| 1102 | if (obfd != NULL) |
| 1103 | einfo (_("%P: %pB: warning: common of `%pT' overriding" |
| 1104 | " smaller common from %pB\n"), |
| 1105 | nbfd, name, obfd); |
| 1106 | else |
| 1107 | einfo (_("%P: %pB: warning: common of `%pT' overriding" |
| 1108 | " smaller common\n"), |
| 1109 | nbfd, name); |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | if (obfd != NULL) |
| 1114 | einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"), |
| 1115 | nbfd, obfd, name); |
| 1116 | else |
| 1117 | einfo (_("%P: %pB: warning: multiple common of `%pT'\n"), |
| 1118 | nbfd, name); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* This is called when BFD has discovered a set element. H is the |
| 1124 | entry in the linker hash table for the set. SECTION and VALUE |
| 1125 | represent a value which should be added to the set. */ |
| 1126 | |
| 1127 | static void |
| 1128 | add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1129 | struct bfd_link_hash_entry *h, |
| 1130 | bfd_reloc_code_real_type reloc, |
| 1131 | bfd *abfd, |
| 1132 | asection *section, |
| 1133 | bfd_vma value) |
| 1134 | { |
| 1135 | if (config.warn_constructors) |
| 1136 | einfo (_("%P: warning: global constructor %s used\n"), |
| 1137 | h->root.string); |
| 1138 | |
| 1139 | if (!config.build_constructors) |
| 1140 | return; |
| 1141 | |
| 1142 | ldctor_add_set_entry (h, reloc, NULL, section, value); |
| 1143 | |
| 1144 | if (h->type == bfd_link_hash_new) |
| 1145 | { |
| 1146 | h->type = bfd_link_hash_undefined; |
| 1147 | h->u.undef.abfd = abfd; |
| 1148 | /* We don't call bfd_link_add_undef to add this to the list of |
| 1149 | undefined symbols because we are going to define it |
| 1150 | ourselves. */ |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /* This is called when BFD has discovered a constructor. This is only |
| 1155 | called for some object file formats--those which do not handle |
| 1156 | constructors in some more clever fashion. This is similar to |
| 1157 | adding an element to a set, but less general. */ |
| 1158 | |
| 1159 | static void |
| 1160 | constructor_callback (struct bfd_link_info *info, |
| 1161 | bfd_boolean constructor, |
| 1162 | const char *name, |
| 1163 | bfd *abfd, |
| 1164 | asection *section, |
| 1165 | bfd_vma value) |
| 1166 | { |
| 1167 | char *s; |
| 1168 | struct bfd_link_hash_entry *h; |
| 1169 | char set_name[1 + sizeof "__CTOR_LIST__"]; |
| 1170 | |
| 1171 | if (config.warn_constructors) |
| 1172 | einfo (_("%P: warning: global constructor %s used\n"), name); |
| 1173 | |
| 1174 | if (!config.build_constructors) |
| 1175 | return; |
| 1176 | |
| 1177 | /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a |
| 1178 | useful error message. */ |
| 1179 | if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL |
| 1180 | && (bfd_link_relocatable (info) |
| 1181 | || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) |
| 1182 | einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n")); |
| 1183 | |
| 1184 | s = set_name; |
| 1185 | if (bfd_get_symbol_leading_char (abfd) != '\0') |
| 1186 | *s++ = bfd_get_symbol_leading_char (abfd); |
| 1187 | if (constructor) |
| 1188 | strcpy (s, "__CTOR_LIST__"); |
| 1189 | else |
| 1190 | strcpy (s, "__DTOR_LIST__"); |
| 1191 | |
| 1192 | h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE); |
| 1193 | if (h == (struct bfd_link_hash_entry *) NULL) |
| 1194 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
| 1195 | if (h->type == bfd_link_hash_new) |
| 1196 | { |
| 1197 | h->type = bfd_link_hash_undefined; |
| 1198 | h->u.undef.abfd = abfd; |
| 1199 | /* We don't call bfd_link_add_undef to add this to the list of |
| 1200 | undefined symbols because we are going to define it |
| 1201 | ourselves. */ |
| 1202 | } |
| 1203 | |
| 1204 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); |
| 1205 | } |
| 1206 | |
| 1207 | /* A structure used by warning_callback to pass information through |
| 1208 | bfd_map_over_sections. */ |
| 1209 | |
| 1210 | struct warning_callback_info |
| 1211 | { |
| 1212 | bfd_boolean found; |
| 1213 | const char *warning; |
| 1214 | const char *symbol; |
| 1215 | asymbol **asymbols; |
| 1216 | }; |
| 1217 | |
| 1218 | /* Look through the relocs to see if we can find a plausible address |
| 1219 | for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */ |
| 1220 | |
| 1221 | static bfd_boolean |
| 1222 | symbol_warning (const char *warning, const char *symbol, bfd *abfd) |
| 1223 | { |
| 1224 | struct warning_callback_info cinfo; |
| 1225 | |
| 1226 | if (!bfd_generic_link_read_symbols (abfd)) |
| 1227 | einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd); |
| 1228 | |
| 1229 | cinfo.found = FALSE; |
| 1230 | cinfo.warning = warning; |
| 1231 | cinfo.symbol = symbol; |
| 1232 | cinfo.asymbols = bfd_get_outsymbols (abfd); |
| 1233 | bfd_map_over_sections (abfd, warning_find_reloc, &cinfo); |
| 1234 | return cinfo.found; |
| 1235 | } |
| 1236 | |
| 1237 | /* This is called when there is a reference to a warning symbol. */ |
| 1238 | |
| 1239 | static void |
| 1240 | warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1241 | const char *warning, |
| 1242 | const char *symbol, |
| 1243 | bfd *abfd, |
| 1244 | asection *section, |
| 1245 | bfd_vma address) |
| 1246 | { |
| 1247 | /* This is a hack to support warn_multiple_gp. FIXME: This should |
| 1248 | have a cleaner interface, but what? */ |
| 1249 | if (!config.warn_multiple_gp |
| 1250 | && strcmp (warning, "using multiple gp values") == 0) |
| 1251 | return; |
| 1252 | |
| 1253 | if (section != NULL) |
| 1254 | einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning); |
| 1255 | else if (abfd == NULL) |
| 1256 | einfo ("%P: %s%s\n", _("warning: "), warning); |
| 1257 | else if (symbol == NULL) |
| 1258 | einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning); |
| 1259 | else if (!symbol_warning (warning, symbol, abfd)) |
| 1260 | { |
| 1261 | bfd *b; |
| 1262 | /* Search all input files for a reference to SYMBOL. */ |
| 1263 | for (b = info->input_bfds; b; b = b->link.next) |
| 1264 | if (b != abfd && symbol_warning (warning, symbol, b)) |
| 1265 | return; |
| 1266 | einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* This is called by warning_callback for each section. It checks the |
| 1271 | relocs of the section to see if it can find a reference to the |
| 1272 | symbol which triggered the warning. If it can, it uses the reloc |
| 1273 | to give an error message with a file and line number. */ |
| 1274 | |
| 1275 | static void |
| 1276 | warning_find_reloc (bfd *abfd, asection *sec, void *iarg) |
| 1277 | { |
| 1278 | struct warning_callback_info *info = (struct warning_callback_info *) iarg; |
| 1279 | long relsize; |
| 1280 | arelent **relpp; |
| 1281 | long relcount; |
| 1282 | arelent **p, **pend; |
| 1283 | |
| 1284 | if (info->found) |
| 1285 | return; |
| 1286 | |
| 1287 | relsize = bfd_get_reloc_upper_bound (abfd, sec); |
| 1288 | if (relsize < 0) |
| 1289 | einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd); |
| 1290 | if (relsize == 0) |
| 1291 | return; |
| 1292 | |
| 1293 | relpp = (arelent **) xmalloc (relsize); |
| 1294 | relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); |
| 1295 | if (relcount < 0) |
| 1296 | einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd); |
| 1297 | |
| 1298 | p = relpp; |
| 1299 | pend = p + relcount; |
| 1300 | for (; p < pend && *p != NULL; p++) |
| 1301 | { |
| 1302 | arelent *q = *p; |
| 1303 | |
| 1304 | if (q->sym_ptr_ptr != NULL |
| 1305 | && *q->sym_ptr_ptr != NULL |
| 1306 | && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) |
| 1307 | { |
| 1308 | /* We found a reloc for the symbol we are looking for. */ |
| 1309 | einfo ("%P: %C: %s%s\n", abfd, sec, q->address, _("warning: "), |
| 1310 | info->warning); |
| 1311 | info->found = TRUE; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | free (relpp); |
| 1317 | } |
| 1318 | |
| 1319 | /* This is called when an undefined symbol is found. */ |
| 1320 | |
| 1321 | static void |
| 1322 | undefined_symbol (struct bfd_link_info *info, |
| 1323 | const char *name, |
| 1324 | bfd *abfd, |
| 1325 | asection *section, |
| 1326 | bfd_vma address, |
| 1327 | bfd_boolean error) |
| 1328 | { |
| 1329 | static char *error_name; |
| 1330 | static unsigned int error_count; |
| 1331 | |
| 1332 | #define MAX_ERRORS_IN_A_ROW 5 |
| 1333 | |
| 1334 | if (info->ignore_hash != NULL |
| 1335 | && bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL) |
| 1336 | return; |
| 1337 | |
| 1338 | if (config.warn_once) |
| 1339 | { |
| 1340 | /* Only warn once about a particular undefined symbol. */ |
| 1341 | add_ignoresym (info, name); |
| 1342 | } |
| 1343 | |
| 1344 | /* We never print more than a reasonable number of errors in a row |
| 1345 | for a single symbol. */ |
| 1346 | if (error_name != NULL |
| 1347 | && strcmp (name, error_name) == 0) |
| 1348 | ++error_count; |
| 1349 | else |
| 1350 | { |
| 1351 | error_count = 0; |
| 1352 | if (error_name != NULL) |
| 1353 | free (error_name); |
| 1354 | error_name = xstrdup (name); |
| 1355 | } |
| 1356 | |
| 1357 | if (section != NULL) |
| 1358 | { |
| 1359 | if (error_count < MAX_ERRORS_IN_A_ROW) |
| 1360 | { |
| 1361 | if (error) |
| 1362 | einfo (_("%X%P: %C: undefined reference to `%pT'\n"), |
| 1363 | abfd, section, address, name); |
| 1364 | else |
| 1365 | einfo (_("%P: %C: warning: undefined reference to `%pT'\n"), |
| 1366 | abfd, section, address, name); |
| 1367 | } |
| 1368 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
| 1369 | { |
| 1370 | if (error) |
| 1371 | einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"), |
| 1372 | abfd, section, address, name); |
| 1373 | else |
| 1374 | einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"), |
| 1375 | abfd, section, address, name); |
| 1376 | } |
| 1377 | else if (error) |
| 1378 | einfo ("%X"); |
| 1379 | } |
| 1380 | else |
| 1381 | { |
| 1382 | if (error_count < MAX_ERRORS_IN_A_ROW) |
| 1383 | { |
| 1384 | if (error) |
| 1385 | einfo (_("%X%P: %pB: undefined reference to `%pT'\n"), |
| 1386 | abfd, name); |
| 1387 | else |
| 1388 | einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"), |
| 1389 | abfd, name); |
| 1390 | } |
| 1391 | else if (error_count == MAX_ERRORS_IN_A_ROW) |
| 1392 | { |
| 1393 | if (error) |
| 1394 | einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"), |
| 1395 | abfd, name); |
| 1396 | else |
| 1397 | einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"), |
| 1398 | abfd, name); |
| 1399 | } |
| 1400 | else if (error) |
| 1401 | einfo ("%X"); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* Counter to limit the number of relocation overflow error messages |
| 1406 | to print. Errors are printed as it is decremented. When it's |
| 1407 | called and the counter is zero, a final message is printed |
| 1408 | indicating more relocations were omitted. When it gets to -1, no |
| 1409 | such errors are printed. If it's initially set to a value less |
| 1410 | than -1, all such errors will be printed (--verbose does this). */ |
| 1411 | |
| 1412 | int overflow_cutoff_limit = 10; |
| 1413 | |
| 1414 | /* This is called when a reloc overflows. */ |
| 1415 | |
| 1416 | static void |
| 1417 | reloc_overflow (struct bfd_link_info *info, |
| 1418 | struct bfd_link_hash_entry *entry, |
| 1419 | const char *name, |
| 1420 | const char *reloc_name, |
| 1421 | bfd_vma addend, |
| 1422 | bfd *abfd, |
| 1423 | asection *section, |
| 1424 | bfd_vma address) |
| 1425 | { |
| 1426 | if (overflow_cutoff_limit == -1) |
| 1427 | return; |
| 1428 | |
| 1429 | einfo ("%X%H:", abfd, section, address); |
| 1430 | |
| 1431 | if (overflow_cutoff_limit >= 0 |
| 1432 | && overflow_cutoff_limit-- == 0) |
| 1433 | { |
| 1434 | einfo (_(" additional relocation overflows omitted from the output\n")); |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | if (entry) |
| 1439 | { |
| 1440 | while (entry->type == bfd_link_hash_indirect |
| 1441 | || entry->type == bfd_link_hash_warning) |
| 1442 | entry = entry->u.i.link; |
| 1443 | switch (entry->type) |
| 1444 | { |
| 1445 | case bfd_link_hash_undefined: |
| 1446 | case bfd_link_hash_undefweak: |
| 1447 | einfo (_(" relocation truncated to fit: " |
| 1448 | "%s against undefined symbol `%pT'"), |
| 1449 | reloc_name, entry->root.string); |
| 1450 | break; |
| 1451 | case bfd_link_hash_defined: |
| 1452 | case bfd_link_hash_defweak: |
| 1453 | einfo (_(" relocation truncated to fit: " |
| 1454 | "%s against symbol `%pT' defined in %pA section in %pB"), |
| 1455 | reloc_name, entry->root.string, |
| 1456 | entry->u.def.section, |
| 1457 | entry->u.def.section == bfd_abs_section_ptr |
| 1458 | ? info->output_bfd : entry->u.def.section->owner); |
| 1459 | break; |
| 1460 | default: |
| 1461 | abort (); |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | else |
| 1466 | einfo (_(" relocation truncated to fit: %s against `%pT'"), |
| 1467 | reloc_name, name); |
| 1468 | if (addend != 0) |
| 1469 | einfo ("+%v", addend); |
| 1470 | einfo ("\n"); |
| 1471 | } |
| 1472 | |
| 1473 | /* This is called when a dangerous relocation is made. */ |
| 1474 | |
| 1475 | static void |
| 1476 | reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1477 | const char *message, |
| 1478 | bfd *abfd, |
| 1479 | asection *section, |
| 1480 | bfd_vma address) |
| 1481 | { |
| 1482 | einfo (_("%X%H: dangerous relocation: %s\n"), |
| 1483 | abfd, section, address, message); |
| 1484 | } |
| 1485 | |
| 1486 | /* This is called when a reloc is being generated attached to a symbol |
| 1487 | that is not being output. */ |
| 1488 | |
| 1489 | static void |
| 1490 | unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1491 | const char *name, |
| 1492 | bfd *abfd, |
| 1493 | asection *section, |
| 1494 | bfd_vma address) |
| 1495 | { |
| 1496 | einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"), |
| 1497 | abfd, section, address, name); |
| 1498 | } |
| 1499 | |
| 1500 | /* This is called if link_info.notice_all is set, or when a symbol in |
| 1501 | link_info.notice_hash is found. Symbols are put in notice_hash |
| 1502 | using the -y option, while notice_all is set if the --cref option |
| 1503 | has been supplied, or if there are any NOCROSSREFS sections in the |
| 1504 | linker script; and if plugins are active, since they need to monitor |
| 1505 | all references from non-IR files. */ |
| 1506 | |
| 1507 | static bfd_boolean |
| 1508 | notice (struct bfd_link_info *info, |
| 1509 | struct bfd_link_hash_entry *h, |
| 1510 | struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED, |
| 1511 | bfd *abfd, |
| 1512 | asection *section, |
| 1513 | bfd_vma value, |
| 1514 | flagword flags ATTRIBUTE_UNUSED) |
| 1515 | { |
| 1516 | const char *name; |
| 1517 | |
| 1518 | if (h == NULL) |
| 1519 | { |
| 1520 | if (command_line.cref || nocrossref_list != NULL) |
| 1521 | return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value); |
| 1522 | return TRUE; |
| 1523 | } |
| 1524 | |
| 1525 | name = h->root.string; |
| 1526 | if (info->notice_hash != NULL |
| 1527 | && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL) |
| 1528 | { |
| 1529 | if (bfd_is_und_section (section)) |
| 1530 | einfo (_("%P: %pB: reference to %s\n"), abfd, name); |
| 1531 | else |
| 1532 | einfo (_("%P: %pB: definition of %s\n"), abfd, name); |
| 1533 | } |
| 1534 | |
| 1535 | if (command_line.cref || nocrossref_list != NULL) |
| 1536 | add_cref (name, abfd, section, value); |
| 1537 | |
| 1538 | return TRUE; |
| 1539 | } |