00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "bfd.h"
00029 #include "sysdep.h"
00030 #include "bfdlink.h"
00031 #include "safe-ctype.h"
00032 #include "ld.h"
00033 #include "ldmisc.h"
00034 #include "ldexp.h"
00035 #include "ldlang.h"
00036 #include "ldfile.h"
00037 #include "ldmain.h"
00038 #include <ldgram.h>
00039 #include "ldlex.h"
00040 #include "ldemul.h"
00041 #include "libiberty.h"
00042 #include "filenames.h"
00043
00044 #ifdef KEY
00045 extern bfd_boolean is_ipa;
00046 extern bfd_boolean ipa_is_whirl(bfd *);
00047 extern void (*p_Ipalink_ErrMsg_EC_infile)(char *);
00048 #endif
00049
00050 const char * ldfile_input_filename;
00051 bfd_boolean ldfile_assumed_script = FALSE;
00052 const char * ldfile_output_machine_name = "";
00053 unsigned long ldfile_output_machine;
00054 enum bfd_architecture ldfile_output_architecture;
00055 search_dirs_type * search_head;
00056
00057 #ifdef VMS
00058 char * slash = "";
00059 #else
00060 #if defined (_WIN32) && ! defined (__CYGWIN32__)
00061 char * slash = "\\";
00062 #else
00063 char * slash = "/";
00064 #endif
00065 #endif
00066
00067 typedef struct search_arch
00068 {
00069 char *name;
00070 struct search_arch *next;
00071 } search_arch_type;
00072
00073 static search_dirs_type **search_tail_ptr = &search_head;
00074 static search_arch_type *search_arch_head;
00075 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
00076
00077
00078
00079
00080 static bfd_boolean
00081 is_sysrooted_pathname (const char *name, bfd_boolean notsame)
00082 {
00083 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
00084 int len;
00085 bfd_boolean result;
00086
00087 if (! realname)
00088 return FALSE;
00089
00090 len = strlen (realname);
00091
00092 if (((! notsame && len == ld_canon_sysroot_len)
00093 || (len >= ld_canon_sysroot_len
00094 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
00095 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
00096 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
00097 result = TRUE;
00098 else
00099 result = FALSE;
00100
00101 if (realname)
00102 free (realname);
00103
00104 return result;
00105 }
00106
00107
00108
00109
00110 void
00111 ldfile_add_library_path (const char *name, bfd_boolean cmdline)
00112 {
00113 search_dirs_type *new;
00114
00115 if (!cmdline && config.only_cmd_line_lib_dirs)
00116 return;
00117
00118 new = xmalloc (sizeof (search_dirs_type));
00119 new->next = NULL;
00120 new->cmdline = cmdline;
00121 *search_tail_ptr = new;
00122 search_tail_ptr = &new->next;
00123
00124
00125
00126 if (name[0] == '=')
00127 {
00128 new->name = concat (ld_sysroot, name + 1, NULL);
00129 new->sysrooted = TRUE;
00130 }
00131 else
00132 {
00133 new->name = xstrdup (name);
00134 new->sysrooted = is_sysrooted_pathname (name, FALSE);
00135 }
00136 }
00137
00138
00139
00140 bfd_boolean
00141 ldfile_try_open_bfd (const char *attempt,
00142 lang_input_statement_type *entry)
00143 {
00144 entry->the_bfd = bfd_openr (attempt, entry->target);
00145
00146 if (trace_file_tries)
00147 {
00148 if (entry->the_bfd == NULL)
00149 info_msg (_("attempt to open %s failed\n"), attempt);
00150 else
00151 info_msg (_("attempt to open %s succeeded\n"), attempt);
00152 }
00153
00154 if (entry->the_bfd == NULL)
00155 {
00156 if (bfd_get_error () == bfd_error_invalid_target)
00157 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
00158 return FALSE;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 if (entry->search_dirs_flag || !entry->dynamic)
00168 {
00169 bfd *check;
00170
00171 if (bfd_check_format (entry->the_bfd, bfd_archive))
00172 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
00173 else
00174 check = entry->the_bfd;
00175
00176 if (check != NULL)
00177 {
00178 if (! bfd_check_format (check, bfd_object))
00179 {
00180 if (check == entry->the_bfd
00181 && entry->search_dirs_flag
00182 && bfd_get_error () == bfd_error_file_not_recognized
00183 && ! ldemul_unrecognized_file (entry))
00184 {
00185 int token, skip = 0;
00186 char *arg, *arg1, *arg2, *arg3;
00187 extern FILE *yyin;
00188
00189
00190 ldfile_open_command_file (attempt);
00191
00192 ldfile_assumed_script = TRUE;
00193 parser_input = input_selected;
00194 ldlex_both ();
00195 token = INPUT_SCRIPT;
00196 while (token != 0)
00197 {
00198 switch (token)
00199 {
00200 case OUTPUT_FORMAT:
00201 if ((token = yylex ()) != '(')
00202 continue;
00203 if ((token = yylex ()) != NAME)
00204 continue;
00205 arg1 = yylval.name;
00206 arg2 = NULL;
00207 arg3 = NULL;
00208 token = yylex ();
00209 if (token == ',')
00210 {
00211 if ((token = yylex ()) != NAME)
00212 {
00213 free (arg1);
00214 continue;
00215 }
00216 arg2 = yylval.name;
00217 if ((token = yylex ()) != ','
00218 || (token = yylex ()) != NAME)
00219 {
00220 free (arg1);
00221 free (arg2);
00222 continue;
00223 }
00224 arg3 = yylval.name;
00225 token = yylex ();
00226 }
00227 if (token == ')')
00228 {
00229 switch (command_line.endian)
00230 {
00231 default:
00232 case ENDIAN_UNSET:
00233 arg = arg1; break;
00234 case ENDIAN_BIG:
00235 arg = arg2 ? arg2 : arg1; break;
00236 case ENDIAN_LITTLE:
00237 arg = arg3 ? arg3 : arg1; break;
00238 }
00239 if (strcmp (arg, lang_get_output_target ()) != 0)
00240 skip = 1;
00241 }
00242 free (arg1);
00243 if (arg2) free (arg2);
00244 if (arg3) free (arg3);
00245 break;
00246 case NAME:
00247 case LNAME:
00248 case VERS_IDENTIFIER:
00249 case VERS_TAG:
00250 free (yylval.name);
00251 break;
00252 case INT:
00253 if (yylval.bigint.str)
00254 free (yylval.bigint.str);
00255 break;
00256 }
00257 token = yylex ();
00258 }
00259 ldlex_popstate ();
00260 ldfile_assumed_script = FALSE;
00261 fclose (yyin);
00262 yyin = NULL;
00263 if (skip)
00264 {
00265 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
00266 attempt, entry->local_sym_name);
00267 bfd_close (entry->the_bfd);
00268 entry->the_bfd = NULL;
00269 return FALSE;
00270 }
00271 }
00272 return TRUE;
00273 }
00274
00275 #ifdef KEY
00276 if (is_ipa &&
00277 ipa_is_whirl(check))
00278 return TRUE;
00279 #endif
00280
00281 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
00282 {
00283 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
00284 attempt);
00285 bfd_close (entry->the_bfd);
00286 entry->the_bfd = NULL;
00287 return FALSE;
00288 }
00289
00290 if (entry->search_dirs_flag
00291 && !bfd_arch_get_compatible (check, output_bfd,
00292 command_line.accept_unknown_input_arch)
00293
00294 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
00295 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
00296 && bfd_check_format (entry->the_bfd, bfd_archive)))
00297 {
00298 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
00299 attempt, entry->local_sym_name);
00300 bfd_close (entry->the_bfd);
00301 entry->the_bfd = NULL;
00302 return FALSE;
00303 }
00304 }
00305 }
00306
00307 return TRUE;
00308 }
00309
00310 #ifdef KEY
00311
00312 static FILE *
00313 read_gcc_output(const char *cmdline)
00314 {
00315 FILE *fp = NULL;
00316 int cmd_len = strlen (cmdline);
00317 char *gcc_cmd = (char *)malloc((5+cmd_len)*sizeof(char));
00318 if (!gcc_cmd) {
00319 printf ("Failed to malloc!\n");
00320 exit (1);
00321 }
00322 strcpy (gcc_cmd, "gcc ");
00323 strcat (gcc_cmd, cmdline);
00324 #if 0
00325 char *gcc_path = get_full_phase_name(P_ld);
00326
00327 if (asprintf(&gcc_cmd, "%s %s", gcc_path, cmdline) == -1) {
00328 internal_error("cannot allocate memory");
00329 goto bail;
00330 }
00331 #endif
00332 if ((fp = popen(gcc_cmd, "r")) == NULL) {
00333 fp = NULL;
00334 }
00335
00336 free (gcc_cmd);
00337 return fp;
00338 }
00339
00340 bfd_boolean
00341 ldfile_try_auto_open_bfd (const char *gcc_option, lang_input_statement_type *entry)
00342 {
00343 FILE *fp = read_gcc_output (gcc_option);
00344 if (!fp) return FALSE;
00345 char buf[1000];
00346 while (fread(buf, 1, 1000, fp) != 0) {
00347 if (strstr (buf, entry->filename) != 0) {
00348 char *arg = buf;
00349 while (*arg == '/') arg++;
00350 arg--;
00351 char *iter = arg;
00352 while (*iter != '\n') iter++;
00353 *iter = '\0';
00354 if (arg && ldfile_try_open_bfd (arg, entry)) {
00355 entry->filename = arg;
00356 pclose (fp);
00357 return TRUE;
00358 }
00359 }
00360 }
00361 pclose (fp);
00362 return FALSE;
00363 }
00364
00365 bfd_boolean
00366 ldfile_open_file_auto_search (lang_input_statement_type *entry)
00367 {
00368 int liblen = strlen (entry->filename);
00369
00370
00371 char *gcc_option = (char *) malloc ((30 + liblen) * sizeof(char));
00372 if (!gcc_option) {
00373 printf ("Failed to malloc!\n");
00374 exit (1);
00375 }
00376 strcpy (gcc_option, "-print-file-name=");
00377 if (!strstr (entry->filename, "lib"))
00378 strcat (gcc_option, "lib");
00379 if (strstr (entry->filename, ".so") ||
00380 strstr (entry->filename, ".a")) {
00381 strcat (gcc_option, entry->filename);
00382 if (ldfile_try_auto_open_bfd (gcc_option, entry)) {
00383 free (gcc_option);
00384 return TRUE;
00385 }
00386 }
00387 strcat (gcc_option, entry->filename);
00388 strcat (gcc_option, ".so");
00389 if (ldfile_try_auto_open_bfd (gcc_option, entry)) {
00390 free (gcc_option);
00391 return TRUE;
00392 }
00393 else {
00394 int option_len = strlen (gcc_option);
00395 *(gcc_option + option_len - 1) = '\0';
00396 *(gcc_option + option_len - 2) = 'a';
00397 if (ldfile_try_auto_open_bfd (gcc_option, entry)) {
00398 free (gcc_option);
00399 return TRUE;
00400 }
00401 }
00402 return FALSE;
00403 }
00404 #endif
00405
00406
00407
00408
00409
00410 bfd_boolean
00411 ldfile_open_file_search (const char *arch,
00412 lang_input_statement_type *entry,
00413 const char *lib,
00414 const char *suffix)
00415 {
00416 search_dirs_type *search;
00417
00418
00419
00420 if (! entry->is_archive)
00421 {
00422 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
00423 {
00424 char *name = concat (ld_sysroot, entry->filename,
00425 (const char *) NULL);
00426 if (ldfile_try_open_bfd (name, entry))
00427 {
00428 entry->filename = name;
00429 return TRUE;
00430 }
00431 free (name);
00432 }
00433 else if (ldfile_try_open_bfd (entry->filename, entry))
00434 {
00435 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
00436 && is_sysrooted_pathname (entry->filename, TRUE);
00437 return TRUE;
00438 }
00439
00440 if (IS_ABSOLUTE_PATH (entry->filename))
00441 return FALSE;
00442 }
00443
00444 for (search = search_head; search != NULL; search = search->next)
00445 {
00446 char *string;
00447
00448 if (entry->dynamic && ! link_info.relocatable)
00449 {
00450 if (ldemul_open_dynamic_archive (arch, search, entry))
00451 {
00452 entry->sysrooted = search->sysrooted;
00453 return TRUE;
00454 }
00455 }
00456
00457 string = xmalloc (strlen (search->name)
00458 + strlen (slash)
00459 + strlen (lib)
00460 + strlen (entry->filename)
00461 + strlen (arch)
00462 + strlen (suffix)
00463 + 1);
00464
00465 if (entry->is_archive)
00466 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
00467 lib, entry->filename, arch, suffix);
00468 else
00469 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
00470
00471 if (ldfile_try_open_bfd (string, entry))
00472 {
00473 entry->filename = string;
00474 entry->sysrooted = search->sysrooted;
00475 return TRUE;
00476 }
00477 free (string);
00478 }
00479
00480 #ifdef KEY
00481 return ldfile_open_file_auto_search (entry);
00482 #endif
00483 }
00484
00485
00486
00487 void
00488 ldfile_open_file (lang_input_statement_type *entry)
00489 {
00490 if (entry->the_bfd != NULL)
00491 return;
00492
00493 if (! entry->search_dirs_flag)
00494 {
00495 if (ldfile_try_open_bfd (entry->filename, entry))
00496 return;
00497 if (strcmp (entry->filename, entry->local_sym_name) != 0)
00498 einfo (_("%F%P: %s (%s): No such file: %E\n"),
00499 entry->filename, entry->local_sym_name);
00500 else
00501 #ifdef KEY
00502 (*p_Ipalink_ErrMsg_EC_infile)(entry->local_sym_name);
00503 #else
00504 einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
00505 #endif
00506 }
00507 else
00508 {
00509 search_arch_type *arch;
00510 bfd_boolean found = FALSE;
00511
00512
00513 for (arch = search_arch_head; arch != NULL; arch = arch->next)
00514 {
00515 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
00516 if (found)
00517 break;
00518 #ifdef VMS
00519 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
00520 if (found)
00521 break;
00522 #endif
00523 found = ldemul_find_potential_libraries (arch->name, entry);
00524 if (found)
00525 break;
00526 }
00527
00528
00529
00530 if (found)
00531 entry->search_dirs_flag = FALSE;
00532 else if (entry->sysrooted
00533 && ld_sysroot
00534 && IS_ABSOLUTE_PATH (entry->local_sym_name))
00535 einfo (_("%F%P: cannot find %s inside %s\n"),
00536 entry->local_sym_name, ld_sysroot);
00537 else
00538 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
00539 }
00540 }
00541
00542
00543
00544 static FILE *
00545 try_open (const char *name, const char *exten)
00546 {
00547 FILE *result;
00548 char buff[1000];
00549
00550 result = fopen (name, "r");
00551
00552 if (trace_file_tries)
00553 {
00554 if (result == NULL)
00555 info_msg (_("cannot find script file %s\n"), name);
00556 else
00557 info_msg (_("opened script file %s\n"), name);
00558 }
00559
00560 if (result != NULL)
00561 return result;
00562
00563 if (*exten)
00564 {
00565 sprintf (buff, "%s%s", name, exten);
00566 result = fopen (buff, "r");
00567
00568 if (trace_file_tries)
00569 {
00570 if (result == NULL)
00571 info_msg (_("cannot find script file %s\n"), buff);
00572 else
00573 info_msg (_("opened script file %s\n"), buff);
00574 }
00575 }
00576
00577 return result;
00578 }
00579
00580
00581
00582
00583 FILE *
00584 ldfile_find_command_file (const char *name, const char *extend)
00585 {
00586 search_dirs_type *search;
00587 FILE *result;
00588 char buffer[1000];
00589
00590
00591 result = try_open (name, "");
00592 if (result == NULL)
00593 {
00594
00595 for (search = search_head; search != NULL; search = search->next)
00596 {
00597 sprintf (buffer, "%s%s%s", search->name, slash, name);
00598
00599 result = try_open (buffer, extend);
00600 if (result)
00601 break;
00602 }
00603 }
00604
00605 return result;
00606 }
00607
00608 void
00609 ldfile_open_command_file (const char *name)
00610 {
00611 FILE *ldlex_input_stack;
00612 ldlex_input_stack = ldfile_find_command_file (name, "");
00613
00614 if (ldlex_input_stack == NULL)
00615 {
00616 bfd_set_error (bfd_error_system_call);
00617 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
00618 }
00619
00620 lex_push_file (ldlex_input_stack, name);
00621
00622 ldfile_input_filename = name;
00623 lineno = 1;
00624
00625 saved_script_handle = ldlex_input_stack;
00626 }
00627
00628 void
00629 ldfile_add_arch (const char *in_name)
00630 {
00631 char *name = xstrdup (in_name);
00632 search_arch_type *new = xmalloc (sizeof (search_arch_type));
00633
00634 ldfile_output_machine_name = in_name;
00635
00636 new->name = name;
00637 new->next = NULL;
00638 while (*name)
00639 {
00640 *name = TOLOWER (*name);
00641 name++;
00642 }
00643 *search_arch_tail_ptr = new;
00644 search_arch_tail_ptr = &new->next;
00645
00646 }
00647
00648
00649
00650 void
00651 ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
00652 {
00653 const bfd_arch_info_type *arch = bfd_scan_arch (string);
00654
00655 if (arch)
00656 {
00657 ldfile_output_architecture = arch->arch;
00658 ldfile_output_machine = arch->mach;
00659 ldfile_output_machine_name = arch->printable_name;
00660 }
00661 else if (defarch != bfd_arch_unknown)
00662 ldfile_output_architecture = defarch;
00663 else
00664 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
00665 }