| 1 | /* Copyright (C) 1991 Free Software Foundation, Inc. |
| 2 | Written by Steve Chamberlain steve@cygnus.com |
| 3 | |
| 4 | This file is part of GLD, the Gnu Linker. |
| 5 | |
| 6 | GLD is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GLD is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GLD; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | #include "sysdep.h" |
| 23 | |
| 24 | #include "config.h" |
| 25 | #include "ld.h" |
| 26 | #include "ldmain.h" |
| 27 | #include "ldmisc.h" |
| 28 | #include "ldwrite.h" |
| 29 | #include "ldgram.h" |
| 30 | #include "ldsym.h" |
| 31 | #include "ldlang.h" |
| 32 | #include "ldemul.h" |
| 33 | #include "ldlex.h" |
| 34 | #include "ldfile.h" |
| 35 | #include "ldindr.h" |
| 36 | #include "ldwarn.h" |
| 37 | #include "ldctor.h" |
| 38 | #include "lderror.h" |
| 39 | |
| 40 | /* Somewhere above, sys/stat.h got included . . . . */ |
| 41 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
| 42 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 43 | #endif |
| 44 | |
| 45 | #include <string.h> |
| 46 | |
| 47 | static char *get_emulation (); |
| 48 | static void set_scripts_dir (); |
| 49 | |
| 50 | /* IMPORTS */ |
| 51 | extern boolean lang_has_input_file; |
| 52 | extern boolean force_make_executable; |
| 53 | extern boolean relaxing; |
| 54 | extern boolean had_script; |
| 55 | |
| 56 | /* EXPORTS */ |
| 57 | |
| 58 | char *default_target; |
| 59 | char *output_filename = "a.out"; |
| 60 | |
| 61 | /* Name this program was invoked by. */ |
| 62 | char *program_name; |
| 63 | |
| 64 | /* The file that we're creating */ |
| 65 | bfd *output_bfd = 0; |
| 66 | |
| 67 | /* set if -y on the command line */ |
| 68 | int had_y; |
| 69 | |
| 70 | /* The local symbol prefix */ |
| 71 | char lprefix = 'L'; |
| 72 | |
| 73 | /* Set by -G argument, for MIPS ECOFF target. */ |
| 74 | int g_switch_value = 8; |
| 75 | |
| 76 | /* Count the number of global symbols multiply defined. */ |
| 77 | int multiple_def_count; |
| 78 | |
| 79 | /* Count the number of symbols defined through common declarations. |
| 80 | This count is referenced in symdef_library, linear_library, and |
| 81 | modified by enter_global_ref. |
| 82 | |
| 83 | It is incremented when a symbol is created as a common, and |
| 84 | decremented when the common declaration is overridden |
| 85 | |
| 86 | Another way of thinking of it is that this is a count of |
| 87 | all ldsym_types with a ->scoms field */ |
| 88 | |
| 89 | unsigned int commons_pending; |
| 90 | |
| 91 | /* Count the number of global symbols referenced and not defined. |
| 92 | common symbols are not included in this count. */ |
| 93 | |
| 94 | unsigned int undefined_global_sym_count; |
| 95 | |
| 96 | /* Nonzero means print names of input files as processed. */ |
| 97 | boolean trace_files; |
| 98 | |
| 99 | /* Nonzero means same, but note open failures, too. */ |
| 100 | boolean trace_file_tries; |
| 101 | |
| 102 | /* 1 => write load map. */ |
| 103 | boolean write_map; |
| 104 | |
| 105 | #ifdef GNU960 |
| 106 | /* Indicates whether output file will be b.out (default) or coff */ |
| 107 | enum target_flavour output_flavor = BFD_BOUT_FORMAT; |
| 108 | #endif |
| 109 | |
| 110 | /* A count of the total number of local symbols ever seen - by adding |
| 111 | the symbol_count field of each newly read afile.*/ |
| 112 | |
| 113 | unsigned int total_symbols_seen; |
| 114 | |
| 115 | /* A count of the number of read files - the same as the number of elements |
| 116 | in file_chain |
| 117 | */ |
| 118 | unsigned int total_files_seen; |
| 119 | |
| 120 | args_type command_line; |
| 121 | |
| 122 | ld_config_type config; |
| 123 | \f |
| 124 | void |
| 125 | main (argc, argv) |
| 126 | char **argv; |
| 127 | int argc; |
| 128 | { |
| 129 | char *emulation; |
| 130 | |
| 131 | program_name = argv[0]; |
| 132 | |
| 133 | bfd_init (); |
| 134 | |
| 135 | /* Initialize the data about options. */ |
| 136 | |
| 137 | |
| 138 | trace_files = trace_file_tries = false; |
| 139 | write_map = false; |
| 140 | config.relocateable_output = false; |
| 141 | command_line.force_common_definition = false; |
| 142 | |
| 143 | init_bfd_error_vector (); |
| 144 | ldsym_init (); |
| 145 | ldfile_add_arch (""); |
| 146 | |
| 147 | set_scripts_dir (); |
| 148 | |
| 149 | config.make_executable = true; |
| 150 | force_make_executable = false; |
| 151 | |
| 152 | |
| 153 | /* Initialize the cumulative counts of symbols. */ |
| 154 | undefined_global_sym_count = 0; |
| 155 | multiple_def_count = 0; |
| 156 | commons_pending = 0; |
| 157 | |
| 158 | config.magic_demand_paged = true; |
| 159 | config.text_read_only = true; |
| 160 | config.make_executable = true; |
| 161 | |
| 162 | emulation = get_emulation (argc, argv); |
| 163 | ldemul_choose_mode (emulation); |
| 164 | default_target = ldemul_choose_target (); |
| 165 | lang_init (); |
| 166 | ldemul_before_parse (); |
| 167 | lang_has_input_file = false; |
| 168 | parse_args (argc, argv); |
| 169 | |
| 170 | if (had_script == false) |
| 171 | { |
| 172 | /* Read the emulation's appropriate default script. */ |
| 173 | int isfile; |
| 174 | char *s = ldemul_get_script (&isfile); |
| 175 | |
| 176 | if (isfile) |
| 177 | { |
| 178 | /* sizeof counts the terminating NUL. */ |
| 179 | size_t size = strlen (s) + sizeof ("-T "); |
| 180 | char *buf = (char *) ldmalloc(size); |
| 181 | sprintf (buf, "-T %s", s); |
| 182 | parse_line (buf, 0); |
| 183 | free (buf); |
| 184 | } |
| 185 | else |
| 186 | parse_line (s, 1); |
| 187 | } |
| 188 | |
| 189 | if (config.relocateable_output && command_line.relax) |
| 190 | { |
| 191 | einfo ("%P%F: -relax and -r may not be used together\n"); |
| 192 | } |
| 193 | lang_final (); |
| 194 | |
| 195 | if (lang_has_input_file == false) |
| 196 | { |
| 197 | einfo ("%P%F: No input files\n"); |
| 198 | } |
| 199 | |
| 200 | if (trace_files) |
| 201 | { |
| 202 | info ("%P: mode %s\n", emulation); |
| 203 | } |
| 204 | |
| 205 | ldemul_after_parse (); |
| 206 | |
| 207 | |
| 208 | if (config.map_filename) |
| 209 | { |
| 210 | if (strcmp (config.map_filename, "-") == 0) |
| 211 | { |
| 212 | config.map_file = stdout; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | config.map_file = fopen (config.map_filename, FOPEN_WT); |
| 217 | if (config.map_file == (FILE *) NULL) |
| 218 | { |
| 219 | einfo ("%P%F: cannot open map file %s: %E\n", |
| 220 | config.map_filename); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | lang_process (); |
| 227 | |
| 228 | /* Print error messages for any missing symbols, for any warning |
| 229 | symbols, and possibly multiple definitions */ |
| 230 | |
| 231 | |
| 232 | if (config.text_read_only) |
| 233 | { |
| 234 | /* Look for a text section and mark the readonly attribute in it */ |
| 235 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); |
| 236 | |
| 237 | if (found != (asection *) NULL) |
| 238 | { |
| 239 | found->flags |= SEC_READONLY; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (config.relocateable_output) |
| 244 | output_bfd->flags &= ~EXEC_P; |
| 245 | else |
| 246 | output_bfd->flags |= EXEC_P; |
| 247 | |
| 248 | ldwrite (); |
| 249 | |
| 250 | /* Even if we're producing relocateable output, some non-fatal errors should |
| 251 | be reported in the exit status. (What non-fatal errors, if any, do we |
| 252 | want to ignore for relocateable output?) */ |
| 253 | |
| 254 | if (config.make_executable == false && force_make_executable == false) |
| 255 | { |
| 256 | if (trace_files == true) |
| 257 | { |
| 258 | einfo ("%P: Link errors found, deleting executable `%s'\n", |
| 259 | output_filename); |
| 260 | } |
| 261 | |
| 262 | if (output_bfd->iostream) |
| 263 | fclose ((FILE *) (output_bfd->iostream)); |
| 264 | |
| 265 | unlink (output_filename); |
| 266 | exit (1); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | bfd_close (output_bfd); |
| 271 | } |
| 272 | |
| 273 | exit (0); |
| 274 | } |
| 275 | |
| 276 | /* We need to find any explicitly given emulation in order to initialize the |
| 277 | state that's needed by the lex&yacc argument parser (parse_args). */ |
| 278 | |
| 279 | static char * |
| 280 | get_emulation (argc, argv) |
| 281 | int argc; |
| 282 | char **argv; |
| 283 | { |
| 284 | char *emulation; |
| 285 | int i; |
| 286 | |
| 287 | #ifdef GNU960 |
| 288 | check_v960 (argc, argv); |
| 289 | emulation = "gld960"; |
| 290 | for (i = 1; i < argc; i++) |
| 291 | { |
| 292 | if (!strcmp (argv[i], "-Fcoff")) |
| 293 | { |
| 294 | emulation = "lnk960"; |
| 295 | output_flavor = BFD_COFF_FORMAT; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | #else |
| 300 | emulation = (char *) getenv (EMULATION_ENVIRON); |
| 301 | if (emulation == NULL) |
| 302 | emulation = DEFAULT_EMULATION; |
| 303 | #endif |
| 304 | |
| 305 | for (i = 1; i < argc; i++) |
| 306 | { |
| 307 | if (!strncmp (argv[i], "-m", 2)) |
| 308 | { |
| 309 | if (argv[i][2] == '\0') |
| 310 | { |
| 311 | /* -m EMUL */ |
| 312 | if (i < argc - 1) |
| 313 | { |
| 314 | emulation = argv[i + 1]; |
| 315 | i++; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | einfo("%P%F missing argument to -m\n"); |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | /* -mEMUL */ |
| 325 | emulation = &argv[i][2]; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return emulation; |
| 331 | } |
| 332 | |
| 333 | /* If directory DIR contains an "ldscripts" subdirectory, |
| 334 | add DIR to the library search path and return true, |
| 335 | else return false. */ |
| 336 | |
| 337 | static boolean |
| 338 | check_for_scripts_dir (dir) |
| 339 | char *dir; |
| 340 | { |
| 341 | size_t dirlen; |
| 342 | char *buf; |
| 343 | struct stat s; |
| 344 | boolean res; |
| 345 | |
| 346 | dirlen = strlen (dir); |
| 347 | /* sizeof counts the terminating NUL. */ |
| 348 | buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts")); |
| 349 | sprintf (buf, "%s/ldscripts", dir); |
| 350 | |
| 351 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); |
| 352 | free (buf); |
| 353 | if (res) |
| 354 | ldfile_add_library_path (dir); |
| 355 | return res; |
| 356 | } |
| 357 | |
| 358 | /* Set the default directory for finding script files. |
| 359 | Libraries will be searched for here too, but that's ok. |
| 360 | We look for the "ldscripts" directory in: |
| 361 | |
| 362 | SCRIPTDIR (passed from Makefile) |
| 363 | the dir where this program is (for using it from the build tree) |
| 364 | the dir where this program is/../lib (for installing the tool suite elsewhere) */ |
| 365 | |
| 366 | static void |
| 367 | set_scripts_dir () |
| 368 | { |
| 369 | char *end, *dir; |
| 370 | size_t dirlen; |
| 371 | |
| 372 | if (check_for_scripts_dir (SCRIPTDIR)) |
| 373 | return; /* We've been installed normally. */ |
| 374 | |
| 375 | /* Look for "ldscripts" in the dir where our binary is. */ |
| 376 | end = strrchr (program_name, '/'); |
| 377 | if (end) |
| 378 | { |
| 379 | dirlen = end - program_name; |
| 380 | /* Make a copy of program_name in dir. |
| 381 | Leave room for later "/../lib". */ |
| 382 | dir = (char *) ldmalloc (dirlen + 8); |
| 383 | strncpy (dir, program_name, dirlen); |
| 384 | dir[dirlen] = '\0'; |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | dirlen = 1; |
| 389 | dir = (char *) ldmalloc (dirlen + 8); |
| 390 | strcpy (dir, "."); |
| 391 | } |
| 392 | |
| 393 | if (check_for_scripts_dir (dir)) |
| 394 | return; /* Don't free dir. */ |
| 395 | |
| 396 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ |
| 397 | strcpy (dir + dirlen, "/../lib"); |
| 398 | if (check_for_scripts_dir (dir)) |
| 399 | return; |
| 400 | |
| 401 | free (dir); /* Well, we tried. */ |
| 402 | } |
| 403 | |
| 404 | void |
| 405 | read_entry_symbols (desc, entry) |
| 406 | bfd *desc; |
| 407 | struct lang_input_statement_struct *entry; |
| 408 | { |
| 409 | if (entry->asymbols == (asymbol **) NULL) |
| 410 | { |
| 411 | bfd_size_type table_size = get_symtab_upper_bound (desc); |
| 412 | |
| 413 | entry->asymbols = (asymbol **) ldmalloc (table_size); |
| 414 | entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * turn this item into a reference |
| 420 | */ |
| 421 | void |
| 422 | refize (sp, nlist_p) |
| 423 | ldsym_type *sp; |
| 424 | asymbol **nlist_p; |
| 425 | { |
| 426 | asymbol *sym = *nlist_p; |
| 427 | |
| 428 | sym->value = 0; |
| 429 | sym->flags = 0; |
| 430 | sym->section = &bfd_und_section; |
| 431 | sym->udata = (PTR) (sp->srefs_chain); |
| 432 | sp->srefs_chain = nlist_p; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | This function is called for each name which is seen which has a global |
| 437 | scope. It enters the name into the global symbol table in the correct |
| 438 | symbol on the correct chain. Remember that each ldsym_type has three |
| 439 | chains attatched, one of all definitions of a symbol, one of all |
| 440 | references of a symbol and one of all common definitions of a symbol. |
| 441 | |
| 442 | When the function is over, the supplied is left connected to the bfd |
| 443 | to which is was born, with its udata field pointing to the next member |
| 444 | on the chain in which it has been inserted. |
| 445 | |
| 446 | A certain amount of jigery pokery is necessary since commons come |
| 447 | along and upset things, we only keep one item in the common chain; the |
| 448 | one with the biggest size seen sofar. When another common comes along |
| 449 | it either bumps the previous definition into the ref chain, since it |
| 450 | is bigger, or gets turned into a ref on the spot since the one on the |
| 451 | common chain is already bigger. If a real definition comes along then |
| 452 | the common gets bumped off anyway. |
| 453 | |
| 454 | Whilst all this is going on we keep a count of the number of multiple |
| 455 | definitions seen, undefined global symbols and pending commons. |
| 456 | */ |
| 457 | |
| 458 | void |
| 459 | enter_global_ref (nlist_p, name) |
| 460 | asymbol ** nlist_p; /* pointer into symbol table from incoming bfd */ |
| 461 | CONST char *name; /* name of symbol in linker table */ |
| 462 | { |
| 463 | asymbol *sym = *nlist_p; |
| 464 | ldsym_type *sp; |
| 465 | |
| 466 | /* Lookup the name from the incoming bfd's symbol table in the |
| 467 | linker's global symbol table */ |
| 468 | |
| 469 | |
| 470 | flagword this_symbol_flags = sym->flags; |
| 471 | |
| 472 | sp = ldsym_get (name); |
| 473 | |
| 474 | |
| 475 | /* If this symbol already has udata, it means that something strange |
| 476 | has happened. |
| 477 | |
| 478 | The strange thing is that we've had an undefined symbol resolved by |
| 479 | an alias, but the thing the alias defined wasn't in the file. So |
| 480 | the symbol got a udata entry, but the file wasn't loaded. Then |
| 481 | later on the file was loaded, but we don't need to do this |
| 482 | processing again */ |
| 483 | |
| 484 | |
| 485 | if (sym->udata) |
| 486 | return; |
| 487 | |
| 488 | |
| 489 | if (flag_is_constructor (this_symbol_flags)) |
| 490 | { |
| 491 | /* Add this constructor to the list we keep */ |
| 492 | ldlang_add_constructor (sp); |
| 493 | /* Turn any commons into refs */ |
| 494 | if (sp->scoms_chain != (asymbol **) NULL) |
| 495 | { |
| 496 | refize (sp, sp->scoms_chain); |
| 497 | sp->scoms_chain = 0; |
| 498 | } |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | if (bfd_is_com_section (sym->section)) |
| 503 | { |
| 504 | /* If we have a definition of this symbol already then |
| 505 | this common turns into a reference. Also we only |
| 506 | ever point to the largest common, so if we |
| 507 | have a common, but it's bigger that the new symbol |
| 508 | the turn this into a reference too. */ |
| 509 | if (sp->sdefs_chain) |
| 510 | { |
| 511 | /* This is a common symbol, but we already have a definition |
| 512 | for it, so just link it into the ref chain as if |
| 513 | it were a reference */ |
| 514 | if (config.warn_common) |
| 515 | multiple_warn("%C: warning: common of `%s' overridden by definition\n", |
| 516 | sym, |
| 517 | "%C: warning: defined here\n", |
| 518 | *(sp->sdefs_chain)); |
| 519 | refize (sp, nlist_p); |
| 520 | } |
| 521 | else if (sp->scoms_chain) |
| 522 | { |
| 523 | /* If we have a previous common, keep only the biggest */ |
| 524 | if ((*(sp->scoms_chain))->value > sym->value) |
| 525 | { |
| 526 | /* other common is bigger, throw this one away */ |
| 527 | if (config.warn_common) |
| 528 | multiple_warn("%C: warning: common of `%s' overridden by larger common\n", |
| 529 | sym, |
| 530 | "%C: warning: larger common is here\n", |
| 531 | *(sp->scoms_chain)); |
| 532 | refize (sp, nlist_p); |
| 533 | } |
| 534 | else if (sp->scoms_chain != nlist_p) |
| 535 | { |
| 536 | /* other common is smaller, throw that away */ |
| 537 | if (config.warn_common) |
| 538 | { |
| 539 | if ((*(sp->scoms_chain))->value < sym->value) |
| 540 | multiple_warn("%C: warning: common of `%s' overriding smaller common\n", |
| 541 | sym, |
| 542 | "%C: warning: smaller common is here\n", |
| 543 | *(sp->scoms_chain)); |
| 544 | else |
| 545 | multiple_warn("%C: warning: multiple common of `%s'\n", |
| 546 | sym, |
| 547 | "%C: warning: previous common is here\n", |
| 548 | *(sp->scoms_chain)); |
| 549 | } |
| 550 | refize (sp, sp->scoms_chain); |
| 551 | sp->scoms_chain = nlist_p; |
| 552 | } |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | /* This is the first time we've seen a common, so remember it |
| 557 | - if it was undefined before, we know it's defined now. If |
| 558 | the symbol has been marked as really being a constructor, |
| 559 | then treat this as a ref. */ |
| 560 | if (sp->flags & SYM_CONSTRUCTOR) |
| 561 | { |
| 562 | /* Turn this into a ref */ |
| 563 | refize (sp, nlist_p); |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | /* treat like a common */ |
| 568 | if (sp->srefs_chain) |
| 569 | undefined_global_sym_count--; |
| 570 | |
| 571 | commons_pending++; |
| 572 | sp->scoms_chain = nlist_p; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | else if (sym->section != &bfd_und_section) |
| 577 | { |
| 578 | /* This is the definition of a symbol, add to def chain */ |
| 579 | if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) |
| 580 | { |
| 581 | /* Multiple definition */ |
| 582 | multiple_warn("%X%C: multiple definition of `%s'\n", |
| 583 | sym, |
| 584 | "%X%C: first defined here\n", |
| 585 | *(sp->sdefs_chain)); |
| 586 | multiple_def_count++; |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | sym->udata = (PTR) (sp->sdefs_chain); |
| 591 | sp->sdefs_chain = nlist_p; |
| 592 | } |
| 593 | /* A definition overrides a common symbol */ |
| 594 | if (sp->scoms_chain) |
| 595 | { |
| 596 | if (config.warn_common) |
| 597 | multiple_warn("%C: warning: definition of `%s' overriding common\n", |
| 598 | sym, |
| 599 | "%C: warning: common is here\n", |
| 600 | *(sp->scoms_chain)); |
| 601 | refize (sp, sp->scoms_chain); |
| 602 | sp->scoms_chain = 0; |
| 603 | commons_pending--; |
| 604 | } |
| 605 | else if (sp->srefs_chain && relaxing == false) |
| 606 | { |
| 607 | /* If previously was undefined, then remember as defined */ |
| 608 | undefined_global_sym_count--; |
| 609 | } |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | if (sp->scoms_chain == (asymbol **) NULL |
| 614 | && sp->srefs_chain == (asymbol **) NULL |
| 615 | && sp->sdefs_chain == (asymbol **) NULL) |
| 616 | { |
| 617 | /* And it's the first time we've seen it */ |
| 618 | undefined_global_sym_count++; |
| 619 | } |
| 620 | |
| 621 | refize (sp, nlist_p); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0); |
| 626 | ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0); |
| 627 | |
| 628 | |
| 629 | } |
| 630 | |
| 631 | static void |
| 632 | enter_file_symbols (entry) |
| 633 | lang_input_statement_type *entry; |
| 634 | { |
| 635 | asymbol **q; |
| 636 | |
| 637 | entry->common_section = |
| 638 | bfd_make_section_old_way (entry->the_bfd, "COMMON"); |
| 639 | entry->common_section->flags = SEC_NEVER_LOAD; |
| 640 | ldlang_add_file (entry); |
| 641 | |
| 642 | |
| 643 | if (trace_files || trace_file_tries) |
| 644 | { |
| 645 | info ("%I\n", entry); |
| 646 | } |
| 647 | |
| 648 | total_symbols_seen += entry->symbol_count; |
| 649 | total_files_seen++; |
| 650 | if (entry->symbol_count) |
| 651 | { |
| 652 | for (q = entry->asymbols; *q; q++) |
| 653 | { |
| 654 | asymbol *p = *q; |
| 655 | |
| 656 | if (had_y && p->name) |
| 657 | { |
| 658 | /* look up the symbol anyway to see if the trace bit was |
| 659 | set */ |
| 660 | ldsym_type *s = ldsym_get (p->name); |
| 661 | if (s->flags & SYM_Y) |
| 662 | { |
| 663 | einfo ("%B: %s %T\n", entry->the_bfd, |
| 664 | p->section == &bfd_und_section ? "reference to" : "definition of ", |
| 665 | p); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (p->section == &bfd_ind_section) |
| 670 | { |
| 671 | add_indirect (q); |
| 672 | } |
| 673 | else if (p->flags & BSF_WARNING) |
| 674 | { |
| 675 | add_warning (p); |
| 676 | } |
| 677 | else if (p->section == &bfd_und_section |
| 678 | || (p->flags & BSF_GLOBAL) |
| 679 | || bfd_is_com_section (p->section) |
| 680 | || (p->flags & BSF_CONSTRUCTOR)) |
| 681 | |
| 682 | { |
| 683 | |
| 684 | asymbol *p = *q; |
| 685 | |
| 686 | if (p->flags & BSF_INDIRECT) |
| 687 | { |
| 688 | add_indirect (q); |
| 689 | } |
| 690 | else if (p->flags & BSF_WARNING) |
| 691 | { |
| 692 | add_warning (p); |
| 693 | } |
| 694 | else if (p->section == &bfd_und_section |
| 695 | || (p->flags & BSF_GLOBAL) |
| 696 | || bfd_is_com_section (p->section) |
| 697 | || (p->flags & BSF_CONSTRUCTOR)) |
| 698 | { |
| 699 | enter_global_ref (q, p->name); |
| 700 | } |
| 701 | |
| 702 | } |
| 703 | |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | |
| 709 | /* Searching libraries */ |
| 710 | |
| 711 | struct lang_input_statement_struct *decode_library_subfile (); |
| 712 | void linear_library (), symdef_library (); |
| 713 | |
| 714 | /* Search the library ENTRY, already open on descriptor DESC. |
| 715 | This means deciding which library members to load, |
| 716 | making a chain of `struct lang_input_statement_struct' for those members, |
| 717 | and entering their global symbols in the hash table. */ |
| 718 | |
| 719 | void |
| 720 | search_library (entry) |
| 721 | struct lang_input_statement_struct *entry; |
| 722 | { |
| 723 | |
| 724 | /* No need to load a library if no undefined symbols */ |
| 725 | if (!undefined_global_sym_count) |
| 726 | return; |
| 727 | |
| 728 | if (bfd_has_map (entry->the_bfd)) |
| 729 | symdef_library (entry); |
| 730 | else |
| 731 | linear_library (entry); |
| 732 | |
| 733 | } |
| 734 | |
| 735 | #ifdef GNU960 |
| 736 | static |
| 737 | boolean |
| 738 | gnu960_check_format (abfd, format) |
| 739 | bfd *abfd; |
| 740 | bfd_format format; |
| 741 | { |
| 742 | boolean retval; |
| 743 | |
| 744 | if ((bfd_check_format (abfd, format) == true) |
| 745 | && (abfd->xvec->flavour == output_flavor)) |
| 746 | { |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | #endif |
| 755 | |
| 756 | void |
| 757 | ldmain_open_file_read_symbol (entry) |
| 758 | struct lang_input_statement_struct *entry; |
| 759 | { |
| 760 | if (entry->asymbols == (asymbol **) NULL |
| 761 | && entry->real == true |
| 762 | && entry->filename != (char *) NULL) |
| 763 | { |
| 764 | ldfile_open_file (entry); |
| 765 | |
| 766 | |
| 767 | #ifdef GNU960 |
| 768 | if (gnu960_check_format (entry->the_bfd, bfd_object)) |
| 769 | #else |
| 770 | if (bfd_check_format (entry->the_bfd, bfd_object)) |
| 771 | #endif |
| 772 | { |
| 773 | entry->the_bfd->usrdata = (PTR) entry; |
| 774 | |
| 775 | |
| 776 | read_entry_symbols (entry->the_bfd, entry); |
| 777 | |
| 778 | /* look through the sections in the file and see if any of them |
| 779 | are constructors */ |
| 780 | ldlang_check_for_constructors (entry); |
| 781 | |
| 782 | enter_file_symbols (entry); |
| 783 | } |
| 784 | #ifdef GNU960 |
| 785 | else if (gnu960_check_format (entry->the_bfd, bfd_archive)) |
| 786 | #else |
| 787 | else if (bfd_check_format (entry->the_bfd, bfd_archive)) |
| 788 | #endif |
| 789 | { |
| 790 | entry->the_bfd->usrdata = (PTR) entry; |
| 791 | |
| 792 | entry->subfiles = (lang_input_statement_type *) NULL; |
| 793 | search_library (entry); |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | einfo ("%F%B: malformed input file (not rel or archive) \n", |
| 798 | entry->the_bfd); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | } |
| 803 | |
| 804 | /* Construct and return a lang_input_statement_struct for a library member. |
| 805 | The library's lang_input_statement_struct is library_entry, |
| 806 | and the library is open on DESC. |
| 807 | SUBFILE_OFFSET is the byte index in the library of this member's header. |
| 808 | We store the length of the member into *LENGTH_LOC. */ |
| 809 | |
| 810 | lang_input_statement_type * |
| 811 | decode_library_subfile (library_entry, subfile_offset) |
| 812 | struct lang_input_statement_struct *library_entry; |
| 813 | bfd *subfile_offset; |
| 814 | { |
| 815 | register struct lang_input_statement_struct *subentry; |
| 816 | |
| 817 | |
| 818 | /* First, check if we already have a loaded |
| 819 | lang_input_statement_struct for this library subfile. If so, |
| 820 | just return it. Otherwise, allocate some space and build a new one. */ |
| 821 | |
| 822 | if (subfile_offset->usrdata |
| 823 | && ((struct lang_input_statement_struct *) subfile_offset->usrdata)-> |
| 824 | loaded == true) |
| 825 | { |
| 826 | subentry = (struct lang_input_statement_struct *) subfile_offset->usrdata; |
| 827 | } |
| 828 | else |
| 829 | { |
| 830 | subentry = |
| 831 | (struct lang_input_statement_struct *) |
| 832 | ldmalloc ((bfd_size_type) (sizeof (struct lang_input_statement_struct))); |
| 833 | |
| 834 | subentry->filename = subfile_offset->filename; |
| 835 | subentry->local_sym_name = subfile_offset->filename; |
| 836 | subentry->asymbols = 0; |
| 837 | subentry->the_bfd = subfile_offset; |
| 838 | subentry->subfiles = 0; |
| 839 | subentry->next = 0; |
| 840 | subentry->superfile = library_entry; |
| 841 | subentry->is_archive = false; |
| 842 | |
| 843 | subentry->just_syms_flag = false; |
| 844 | subentry->loaded = false; |
| 845 | subentry->chain = 0; |
| 846 | } |
| 847 | return subentry; |
| 848 | } |
| 849 | |
| 850 | boolean subfile_wanted_p (); |
| 851 | void |
| 852 | clear_syms (entry, offset) |
| 853 | struct lang_input_statement_struct *entry; |
| 854 | file_ptr offset; |
| 855 | { |
| 856 | carsym *car; |
| 857 | unsigned long indx = bfd_get_next_mapent (entry->the_bfd, |
| 858 | BFD_NO_MORE_SYMBOLS, |
| 859 | &car); |
| 860 | |
| 861 | while (indx != BFD_NO_MORE_SYMBOLS) |
| 862 | { |
| 863 | if (car->file_offset == offset) |
| 864 | { |
| 865 | car->name = 0; |
| 866 | } |
| 867 | indx = bfd_get_next_mapent (entry->the_bfd, indx, &car); |
| 868 | } |
| 869 | |
| 870 | } |
| 871 | |
| 872 | /* Search a library that has a map |
| 873 | */ |
| 874 | void |
| 875 | symdef_library (entry) |
| 876 | struct lang_input_statement_struct *entry; |
| 877 | |
| 878 | { |
| 879 | register struct lang_input_statement_struct *prev = 0; |
| 880 | |
| 881 | boolean not_finished = true; |
| 882 | |
| 883 | while (not_finished == true) |
| 884 | { |
| 885 | carsym *exported_library_name; |
| 886 | bfd *prev_archive_member_bfd = 0; |
| 887 | |
| 888 | int idx = bfd_get_next_mapent (entry->the_bfd, |
| 889 | BFD_NO_MORE_SYMBOLS, |
| 890 | &exported_library_name); |
| 891 | |
| 892 | not_finished = false; |
| 893 | |
| 894 | while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count) |
| 895 | { |
| 896 | |
| 897 | if (exported_library_name->name) |
| 898 | { |
| 899 | |
| 900 | ldsym_type *sp = ldsym_get_soft (exported_library_name->name); |
| 901 | |
| 902 | /* If we find a symbol that appears to be needed, think carefully |
| 903 | about the archive member that the symbol is in. */ |
| 904 | /* So - if it exists, and is referenced somewhere and is |
| 905 | undefined or */ |
| 906 | if (sp && sp->srefs_chain && !sp->sdefs_chain) |
| 907 | { |
| 908 | bfd *archive_member_bfd = bfd_get_elt_at_index (entry->the_bfd, idx); |
| 909 | struct lang_input_statement_struct *archive_member_lang_input_statement_struct; |
| 910 | |
| 911 | #ifdef GNU960 |
| 912 | if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object)) |
| 913 | #else |
| 914 | if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object)) |
| 915 | #endif |
| 916 | { |
| 917 | |
| 918 | /* Don't think carefully about any archive member |
| 919 | more than once in a given pass. */ |
| 920 | if (prev_archive_member_bfd != archive_member_bfd) |
| 921 | { |
| 922 | |
| 923 | prev_archive_member_bfd = archive_member_bfd; |
| 924 | |
| 925 | /* Read the symbol table of the archive member. */ |
| 926 | |
| 927 | if (archive_member_bfd->usrdata != (PTR) NULL) |
| 928 | { |
| 929 | |
| 930 | archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata; |
| 931 | } |
| 932 | else |
| 933 | { |
| 934 | |
| 935 | archive_member_lang_input_statement_struct = |
| 936 | decode_library_subfile (entry, archive_member_bfd); |
| 937 | archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct; |
| 938 | |
| 939 | } |
| 940 | |
| 941 | if (archive_member_lang_input_statement_struct == 0) |
| 942 | { |
| 943 | einfo ("%F%I contains invalid archive member %s\n", |
| 944 | entry, sp->name); |
| 945 | } |
| 946 | |
| 947 | if (archive_member_lang_input_statement_struct->loaded == false) |
| 948 | { |
| 949 | |
| 950 | read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct); |
| 951 | /* Now scan the symbol table and decide whether to load. */ |
| 952 | |
| 953 | |
| 954 | if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true) |
| 955 | |
| 956 | { |
| 957 | /* This member is needed; load it. |
| 958 | Since we are loading something on this pass, |
| 959 | we must make another pass through the symdef data. */ |
| 960 | |
| 961 | not_finished = true; |
| 962 | |
| 963 | enter_file_symbols (archive_member_lang_input_statement_struct); |
| 964 | |
| 965 | if (prev) |
| 966 | prev->chain = archive_member_lang_input_statement_struct; |
| 967 | else |
| 968 | entry->subfiles = archive_member_lang_input_statement_struct; |
| 969 | |
| 970 | |
| 971 | prev = archive_member_lang_input_statement_struct; |
| 972 | |
| 973 | |
| 974 | /* Clear out this member's symbols from the symdef data |
| 975 | so that following passes won't waste time on them. */ |
| 976 | clear_syms (entry, exported_library_name->file_offset); |
| 977 | archive_member_lang_input_statement_struct->loaded = true; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | void |
| 990 | linear_library (entry) |
| 991 | struct lang_input_statement_struct *entry; |
| 992 | { |
| 993 | boolean more_to_do = true; |
| 994 | register struct lang_input_statement_struct *prev = 0; |
| 995 | |
| 996 | if (entry->complained == false) |
| 997 | { |
| 998 | if (entry->the_bfd->xvec->flavour != bfd_target_ieee_flavour) |
| 999 | |
| 1000 | { |
| 1001 | /* IEEE can use table of contents, so this message is bogus */ |
| 1002 | einfo ("%P: library %s has bad table of contents, rerun ranlib\n", |
| 1003 | entry->the_bfd->filename); |
| 1004 | } |
| 1005 | entry->complained = true; |
| 1006 | |
| 1007 | } |
| 1008 | while (more_to_do) |
| 1009 | { |
| 1010 | |
| 1011 | bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0); |
| 1012 | |
| 1013 | more_to_do = false; |
| 1014 | while (archive) |
| 1015 | { |
| 1016 | /* Don't check this file if it's already been read in |
| 1017 | once */ |
| 1018 | |
| 1019 | if (!archive->usrdata || |
| 1020 | !((lang_input_statement_type *) (archive->usrdata))->loaded) |
| 1021 | { |
| 1022 | #ifdef GNU960 |
| 1023 | if (gnu960_check_format (archive, bfd_object)) |
| 1024 | #else |
| 1025 | if (bfd_check_format (archive, bfd_object)) |
| 1026 | #endif |
| 1027 | { |
| 1028 | register struct lang_input_statement_struct *subentry; |
| 1029 | |
| 1030 | subentry = decode_library_subfile (entry, |
| 1031 | archive); |
| 1032 | |
| 1033 | archive->usrdata = (PTR) subentry; |
| 1034 | if (!subentry) |
| 1035 | return; |
| 1036 | if (subentry->loaded == false) |
| 1037 | { |
| 1038 | read_entry_symbols (archive, subentry); |
| 1039 | |
| 1040 | if (subfile_wanted_p (subentry) == true) |
| 1041 | { |
| 1042 | enter_file_symbols (subentry); |
| 1043 | |
| 1044 | if (prev) |
| 1045 | prev->chain = subentry; |
| 1046 | else |
| 1047 | entry->subfiles = subentry; |
| 1048 | prev = subentry; |
| 1049 | |
| 1050 | more_to_do = true; |
| 1051 | subentry->loaded = true; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | archive = bfd_openr_next_archived_file (entry->the_bfd, archive); |
| 1057 | |
| 1058 | } |
| 1059 | |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* ENTRY is an entry for a file inside an archive |
| 1064 | Its symbols have been read into core, but not entered into the |
| 1065 | linker ymbol table |
| 1066 | Return nonzero if we ought to load this file */ |
| 1067 | |
| 1068 | boolean |
| 1069 | subfile_wanted_p (entry) |
| 1070 | struct lang_input_statement_struct *entry; |
| 1071 | { |
| 1072 | asymbol **q; |
| 1073 | |
| 1074 | if (entry->symbol_count == 0) |
| 1075 | return false; |
| 1076 | |
| 1077 | for (q = entry->asymbols; *q; q++) |
| 1078 | { |
| 1079 | asymbol *p = *q; |
| 1080 | |
| 1081 | /* If the symbol has an interesting definition, we could |
| 1082 | potentially want it. */ |
| 1083 | |
| 1084 | if (p->flags & BSF_INDIRECT) |
| 1085 | { |
| 1086 | /** add_indirect(q);*/ |
| 1087 | } |
| 1088 | |
| 1089 | if (bfd_is_com_section (p->section) |
| 1090 | || (p->flags & BSF_GLOBAL) |
| 1091 | || (p->flags & BSF_INDIRECT)) |
| 1092 | { |
| 1093 | register ldsym_type *sp = ldsym_get_soft (p->name); |
| 1094 | |
| 1095 | /* If this symbol has not been hashed, |
| 1096 | we can't be looking for it. */ |
| 1097 | if (sp != (ldsym_type *) NULL |
| 1098 | && sp->sdefs_chain == (asymbol **) NULL) |
| 1099 | { |
| 1100 | if (sp->srefs_chain != (asymbol **) NULL |
| 1101 | || sp->scoms_chain != (asymbol **) NULL) |
| 1102 | { |
| 1103 | /* This is a symbol we are looking for. It is |
| 1104 | either not yet defined or common. If this is a |
| 1105 | common symbol, then if the symbol in the object |
| 1106 | file is common, we need to combine sizes. But if |
| 1107 | we already have a common symbol, and the symbol |
| 1108 | in the object file is not common, we don't want |
| 1109 | the object file: it is providing a definition for |
| 1110 | a symbol that we already have a definition for |
| 1111 | (this is the else condition below). */ |
| 1112 | if (bfd_is_com_section (p->section)) |
| 1113 | { |
| 1114 | |
| 1115 | /* If the symbol in the table is a constructor, we won't to |
| 1116 | anything fancy with it */ |
| 1117 | if ((sp->flags & SYM_CONSTRUCTOR) == 0) |
| 1118 | { |
| 1119 | /* This libary member has something to |
| 1120 | say about this element. We should |
| 1121 | remember if its a new size */ |
| 1122 | /* Move something from the ref list to the com list */ |
| 1123 | if (sp->scoms_chain) |
| 1124 | { |
| 1125 | /* Already a common symbol, maybe update it */ |
| 1126 | if (p->value > (*(sp->scoms_chain))->value) |
| 1127 | { |
| 1128 | (*(sp->scoms_chain))->value = p->value; |
| 1129 | } |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | /* Take a value from the ref chain |
| 1134 | Here we are moving a symbol from the owning bfd |
| 1135 | to another bfd. We must set up the |
| 1136 | common_section portion of the bfd thing */ |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | sp->scoms_chain = sp->srefs_chain; |
| 1141 | sp->srefs_chain = |
| 1142 | (asymbol **) ((*(sp->srefs_chain))->udata); |
| 1143 | (*(sp->scoms_chain))->udata = (PTR) NULL; |
| 1144 | |
| 1145 | (*(sp->scoms_chain))->section = p->section; |
| 1146 | (*(sp->scoms_chain))->flags = 0; |
| 1147 | /* Remember the size of this item */ |
| 1148 | sp->scoms_chain[0]->value = p->value; |
| 1149 | commons_pending++; |
| 1150 | undefined_global_sym_count--; |
| 1151 | } |
| 1152 | { |
| 1153 | asymbol *com = *(sp->scoms_chain); |
| 1154 | |
| 1155 | if (((lang_input_statement_type *) |
| 1156 | (bfd_asymbol_bfd (com)->usrdata))->common_section == |
| 1157 | (asection *) NULL) |
| 1158 | { |
| 1159 | ((lang_input_statement_type *) |
| 1160 | (bfd_asymbol_bfd (com)->usrdata))->common_section = |
| 1161 | bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON"); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | ASSERT (p->udata == 0); |
| 1166 | } |
| 1167 | else if (sp->scoms_chain == (asymbol **) NULL) |
| 1168 | { |
| 1169 | if (write_map) |
| 1170 | { |
| 1171 | info ("%I needed due to %s\n", entry, sp->name); |
| 1172 | } |
| 1173 | return true; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | void |
| 1184 | add_ysym (text) |
| 1185 | char *text; |
| 1186 | { |
| 1187 | ldsym_type *lookup = ldsym_get (text); |
| 1188 | lookup->flags |= SYM_Y; |
| 1189 | had_y = 1; |
| 1190 | } |