| 1 | /* Linker file opening and searching. | 
|---|
| 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001 | 
|---|
| 3 | Free Software Foundation, Inc. | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of GLD, the Gnu Linker. | 
|---|
| 6 |  | 
|---|
| 7 | GLD 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 2, or (at your option) | 
|---|
| 10 | any later version. | 
|---|
| 11 |  | 
|---|
| 12 | GLD 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 GLD; see the file COPYING.  If not, write to the Free | 
|---|
| 19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 
|---|
| 20 | 02111-1307, USA.  */ | 
|---|
| 21 |  | 
|---|
| 22 | /* ldfile.c:  look after all the file stuff.  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include "bfd.h" | 
|---|
| 25 | #include "sysdep.h" | 
|---|
| 26 | #include "bfdlink.h" | 
|---|
| 27 | #include "ld.h" | 
|---|
| 28 | #include "ldmisc.h" | 
|---|
| 29 | #include "ldexp.h" | 
|---|
| 30 | #include "ldlang.h" | 
|---|
| 31 | #include "ldfile.h" | 
|---|
| 32 | #include "ldmain.h" | 
|---|
| 33 | #include "ldgram.h" | 
|---|
| 34 | #include "ldlex.h" | 
|---|
| 35 | #include "ldemul.h" | 
|---|
| 36 | #include "libiberty.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <ctype.h> | 
|---|
| 39 |  | 
|---|
| 40 | const char *ldfile_input_filename; | 
|---|
| 41 | boolean ldfile_assumed_script = false; | 
|---|
| 42 | const char *ldfile_output_machine_name = ""; | 
|---|
| 43 | unsigned long ldfile_output_machine; | 
|---|
| 44 | enum bfd_architecture ldfile_output_architecture; | 
|---|
| 45 | search_dirs_type *search_head; | 
|---|
| 46 |  | 
|---|
| 47 | #ifndef MPW | 
|---|
| 48 | #ifdef VMS | 
|---|
| 49 | char *slash = ""; | 
|---|
| 50 | #else | 
|---|
| 51 | #if defined (_WIN32) && ! defined (__CYGWIN32__) | 
|---|
| 52 | char *slash = "\\"; | 
|---|
| 53 | #else | 
|---|
| 54 | char *slash = "/"; | 
|---|
| 55 | #endif | 
|---|
| 56 | #endif | 
|---|
| 57 | #else /* MPW */ | 
|---|
| 58 | /* The MPW path char is a colon.  */ | 
|---|
| 59 | char *slash = ":"; | 
|---|
| 60 | #endif /* MPW */ | 
|---|
| 61 |  | 
|---|
| 62 | /* LOCAL */ | 
|---|
| 63 |  | 
|---|
| 64 | static search_dirs_type **search_tail_ptr = &search_head; | 
|---|
| 65 |  | 
|---|
| 66 | typedef struct search_arch { | 
|---|
| 67 | char *name; | 
|---|
| 68 | struct search_arch *next; | 
|---|
| 69 | } search_arch_type; | 
|---|
| 70 |  | 
|---|
| 71 | static search_arch_type *search_arch_head; | 
|---|
| 72 | static search_arch_type **search_arch_tail_ptr = &search_arch_head; | 
|---|
| 73 |  | 
|---|
| 74 | static FILE *try_open PARAMS ((const char *name, const char *exten)); | 
|---|
| 75 |  | 
|---|
| 76 | void | 
|---|
| 77 | ldfile_add_library_path (name, cmdline) | 
|---|
| 78 | const char *name; | 
|---|
| 79 | boolean cmdline; | 
|---|
| 80 | { | 
|---|
| 81 | search_dirs_type *new; | 
|---|
| 82 |  | 
|---|
| 83 | new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); | 
|---|
| 84 | new->next = NULL; | 
|---|
| 85 | new->name = name; | 
|---|
| 86 | new->cmdline = cmdline; | 
|---|
| 87 | *search_tail_ptr = new; | 
|---|
| 88 | search_tail_ptr = &new->next; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /* Try to open a BFD for a lang_input_statement.  */ | 
|---|
| 92 |  | 
|---|
| 93 | boolean | 
|---|
| 94 | ldfile_try_open_bfd (attempt, entry) | 
|---|
| 95 | const char *attempt; | 
|---|
| 96 | lang_input_statement_type *entry; | 
|---|
| 97 | { | 
|---|
| 98 | entry->the_bfd = bfd_openr (attempt, entry->target); | 
|---|
| 99 |  | 
|---|
| 100 | if (trace_file_tries) | 
|---|
| 101 | { | 
|---|
| 102 | if (entry->the_bfd == NULL) | 
|---|
| 103 | info_msg (_("attempt to open %s failed\n"), attempt); | 
|---|
| 104 | else | 
|---|
| 105 | info_msg (_("attempt to open %s succeeded\n"), attempt); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | if (entry->the_bfd == NULL) | 
|---|
| 109 | { | 
|---|
| 110 | if (bfd_get_error () == bfd_error_invalid_target) | 
|---|
| 111 | einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target); | 
|---|
| 112 | return false; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /* If we are searching for this file, see if the architecture is | 
|---|
| 116 | compatible with the output file.  If it isn't, keep searching. | 
|---|
| 117 | If we can't open the file as an object file, stop the search | 
|---|
| 118 | here.  */ | 
|---|
| 119 |  | 
|---|
| 120 | if (entry->search_dirs_flag) | 
|---|
| 121 | { | 
|---|
| 122 | bfd *check; | 
|---|
| 123 |  | 
|---|
| 124 | if (bfd_check_format (entry->the_bfd, bfd_archive)) | 
|---|
| 125 | check = bfd_openr_next_archived_file (entry->the_bfd, NULL); | 
|---|
| 126 | else | 
|---|
| 127 | check = entry->the_bfd; | 
|---|
| 128 |  | 
|---|
| 129 | if (check != NULL) | 
|---|
| 130 | { | 
|---|
| 131 | if (! bfd_check_format (check, bfd_object)) | 
|---|
| 132 | return true; | 
|---|
| 133 | if (bfd_arch_get_compatible (check, output_bfd) == NULL) | 
|---|
| 134 | { | 
|---|
| 135 | einfo (_("%P: skipping incompatible %s when searching for %s\n"), | 
|---|
| 136 | attempt, entry->local_sym_name); | 
|---|
| 137 | bfd_close (entry->the_bfd); | 
|---|
| 138 | entry->the_bfd = NULL; | 
|---|
| 139 | return false; | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | return true; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /* Search for and open the file specified by ENTRY.  If it is an | 
|---|
| 148 | archive, use ARCH, LIB and SUFFIX to modify the file name.  */ | 
|---|
| 149 |  | 
|---|
| 150 | boolean | 
|---|
| 151 | ldfile_open_file_search (arch, entry, lib, suffix) | 
|---|
| 152 | const char *arch; | 
|---|
| 153 | lang_input_statement_type *entry; | 
|---|
| 154 | const char *lib; | 
|---|
| 155 | const char *suffix; | 
|---|
| 156 | { | 
|---|
| 157 | search_dirs_type *search; | 
|---|
| 158 |  | 
|---|
| 159 | /* If this is not an archive, try to open it in the current | 
|---|
| 160 | directory first.  */ | 
|---|
| 161 | if (! entry->is_archive) | 
|---|
| 162 | { | 
|---|
| 163 | if (ldfile_try_open_bfd (entry->filename, entry)) | 
|---|
| 164 | return true; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | for (search = search_head; | 
|---|
| 168 | search != (search_dirs_type *) NULL; | 
|---|
| 169 | search = search->next) | 
|---|
| 170 | { | 
|---|
| 171 | char *string; | 
|---|
| 172 |  | 
|---|
| 173 | if (entry->dynamic && ! link_info.relocateable) | 
|---|
| 174 | { | 
|---|
| 175 | if (ldemul_open_dynamic_archive (arch, search, entry)) | 
|---|
| 176 | return true; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | string = (char *) xmalloc (strlen (search->name) | 
|---|
| 180 | + strlen (slash) | 
|---|
| 181 | + strlen (lib) | 
|---|
| 182 | + strlen (entry->filename) | 
|---|
| 183 | + strlen (arch) | 
|---|
| 184 | + strlen (suffix) | 
|---|
| 185 | + 1); | 
|---|
| 186 |  | 
|---|
| 187 | if (entry->is_archive) | 
|---|
| 188 | sprintf (string, "%s%s%s%s%s%s", search->name, slash, | 
|---|
| 189 | lib, entry->filename, arch, suffix); | 
|---|
| 190 | else if (entry->filename[0] == '/' || entry->filename[0] == '.' | 
|---|
| 191 | #if defined (__MSDOS__) || defined (_WIN32) | 
|---|
| 192 | || entry->filename[0] == '\\' | 
|---|
| 193 | || (isalpha (entry->filename[0]) | 
|---|
| 194 | && entry->filename[1] == ':') | 
|---|
| 195 | #endif | 
|---|
| 196 | ) | 
|---|
| 197 | strcpy (string, entry->filename); | 
|---|
| 198 | else | 
|---|
| 199 | sprintf (string, "%s%s%s", search->name, slash, entry->filename); | 
|---|
| 200 |  | 
|---|
| 201 | if (ldfile_try_open_bfd (string, entry)) | 
|---|
| 202 | { | 
|---|
| 203 | entry->filename = string; | 
|---|
| 204 | return true; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | free (string); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | return false; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | /* Open the input file specified by ENTRY.  */ | 
|---|
| 214 |  | 
|---|
| 215 | void | 
|---|
| 216 | ldfile_open_file (entry) | 
|---|
| 217 | lang_input_statement_type *entry; | 
|---|
| 218 | { | 
|---|
| 219 | if (entry->the_bfd != NULL) | 
|---|
| 220 | return; | 
|---|
| 221 |  | 
|---|
| 222 | if (! entry->search_dirs_flag) | 
|---|
| 223 | { | 
|---|
| 224 | if (ldfile_try_open_bfd (entry->filename, entry)) | 
|---|
| 225 | return; | 
|---|
| 226 | if (strcmp (entry->filename, entry->local_sym_name) != 0) | 
|---|
| 227 | einfo (_("%F%P: cannot open %s for %s: %E\n"), | 
|---|
| 228 | entry->filename, entry->local_sym_name); | 
|---|
| 229 | else | 
|---|
| 230 | einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name); | 
|---|
| 231 | } | 
|---|
| 232 | else | 
|---|
| 233 | { | 
|---|
| 234 | search_arch_type *arch; | 
|---|
| 235 | boolean found = false; | 
|---|
| 236 |  | 
|---|
| 237 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ | 
|---|
| 238 | for (arch = search_arch_head; | 
|---|
| 239 | arch != (search_arch_type *) NULL; | 
|---|
| 240 | arch = arch->next) | 
|---|
| 241 | { | 
|---|
| 242 | found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); | 
|---|
| 243 | if (found) | 
|---|
| 244 | break; | 
|---|
| 245 | #ifdef VMS | 
|---|
| 246 | found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); | 
|---|
| 247 | if (found) | 
|---|
| 248 | break; | 
|---|
| 249 | #endif | 
|---|
| 250 | found = ldemul_find_potential_libraries (arch->name, entry); | 
|---|
| 251 | if (found) | 
|---|
| 252 | break; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | /* If we have found the file, we don't need to search directories | 
|---|
| 256 | again.  */ | 
|---|
| 257 | if (found) | 
|---|
| 258 | entry->search_dirs_flag = false; | 
|---|
| 259 | else | 
|---|
| 260 | einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name); | 
|---|
| 261 | } | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */ | 
|---|
| 265 |  | 
|---|
| 266 | static FILE * | 
|---|
| 267 | try_open (name, exten) | 
|---|
| 268 | const char *name; | 
|---|
| 269 | const char *exten; | 
|---|
| 270 | { | 
|---|
| 271 | FILE *result; | 
|---|
| 272 | char buff[1000]; | 
|---|
| 273 |  | 
|---|
| 274 | result = fopen (name, "r"); | 
|---|
| 275 |  | 
|---|
| 276 | if (trace_file_tries) | 
|---|
| 277 | { | 
|---|
| 278 | if (result == NULL) | 
|---|
| 279 | info_msg (_("cannot find script file %s\n"), name); | 
|---|
| 280 | else | 
|---|
| 281 | info_msg (_("opened script file %s\n"), name); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | if (result != NULL) | 
|---|
| 285 | return result; | 
|---|
| 286 |  | 
|---|
| 287 | if (*exten) | 
|---|
| 288 | { | 
|---|
| 289 | sprintf (buff, "%s%s", name, exten); | 
|---|
| 290 | result = fopen (buff, "r"); | 
|---|
| 291 |  | 
|---|
| 292 | if (trace_file_tries) | 
|---|
| 293 | { | 
|---|
| 294 | if (result == NULL) | 
|---|
| 295 | info_msg (_("cannot find script file %s\n"), buff); | 
|---|
| 296 | else | 
|---|
| 297 | info_msg (_("opened script file %s\n"), buff); | 
|---|
| 298 | } | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | return result; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | /* Try to open NAME; if that fails, look for it in any directories | 
|---|
| 305 | specified with -L, without and with EXTEND apppended.  */ | 
|---|
| 306 |  | 
|---|
| 307 | FILE * | 
|---|
| 308 | ldfile_find_command_file (name, extend) | 
|---|
| 309 | const char *name; | 
|---|
| 310 | const char *extend; | 
|---|
| 311 | { | 
|---|
| 312 | search_dirs_type *search; | 
|---|
| 313 | FILE *result; | 
|---|
| 314 | char buffer[1000]; | 
|---|
| 315 |  | 
|---|
| 316 | /* First try raw name.  */ | 
|---|
| 317 | result = try_open (name, ""); | 
|---|
| 318 | if (result == (FILE *) NULL) | 
|---|
| 319 | { | 
|---|
| 320 | /* Try now prefixes.  */ | 
|---|
| 321 | for (search = search_head; | 
|---|
| 322 | search != (search_dirs_type *) NULL; | 
|---|
| 323 | search = search->next) | 
|---|
| 324 | { | 
|---|
| 325 | sprintf (buffer, "%s%s%s", search->name, slash, name); | 
|---|
| 326 |  | 
|---|
| 327 | result = try_open (buffer, extend); | 
|---|
| 328 | if (result) | 
|---|
| 329 | break; | 
|---|
| 330 | } | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | return result; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | void | 
|---|
| 337 | ldfile_open_command_file (name) | 
|---|
| 338 | const char *name; | 
|---|
| 339 | { | 
|---|
| 340 | FILE *ldlex_input_stack; | 
|---|
| 341 | ldlex_input_stack = ldfile_find_command_file (name, ""); | 
|---|
| 342 |  | 
|---|
| 343 | if (ldlex_input_stack == (FILE *) NULL) | 
|---|
| 344 | { | 
|---|
| 345 | bfd_set_error (bfd_error_system_call); | 
|---|
| 346 | einfo (_("%P%F: cannot open linker script file %s: %E\n"), name); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | lex_push_file (ldlex_input_stack, name); | 
|---|
| 350 |  | 
|---|
| 351 | ldfile_input_filename = name; | 
|---|
| 352 | lineno = 1; | 
|---|
| 353 | had_script = true; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | #ifdef GNU960 | 
|---|
| 357 | static char * | 
|---|
| 358 | gnu960_map_archname (name) | 
|---|
| 359 | char *name; | 
|---|
| 360 | { | 
|---|
| 361 | struct tabentry { char *cmd_switch; char *arch; }; | 
|---|
| 362 | static struct tabentry arch_tab[] = | 
|---|
| 363 | { | 
|---|
| 364 | "",   "", | 
|---|
| 365 | "KA", "ka", | 
|---|
| 366 | "KB", "kb", | 
|---|
| 367 | "KC", "mc",     /* Synonym for MC */ | 
|---|
| 368 | "MC", "mc", | 
|---|
| 369 | "CA", "ca", | 
|---|
| 370 | "SA", "ka",     /* Functionally equivalent to KA */ | 
|---|
| 371 | "SB", "kb",     /* Functionally equivalent to KB */ | 
|---|
| 372 | NULL, "" | 
|---|
| 373 | }; | 
|---|
| 374 | struct tabentry *tp; | 
|---|
| 375 |  | 
|---|
| 376 | for (tp = arch_tab; tp->cmd_switch != NULL; tp++) | 
|---|
| 377 | { | 
|---|
| 378 | if (! strcmp (name,tp->cmd_switch)) | 
|---|
| 379 | break; | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | if (tp->cmd_switch == NULL) | 
|---|
| 383 | einfo (_("%P%F: unknown architecture: %s\n"), name); | 
|---|
| 384 |  | 
|---|
| 385 | return tp->arch; | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | void | 
|---|
| 389 | ldfile_add_arch (name) | 
|---|
| 390 | char *name; | 
|---|
| 391 | { | 
|---|
| 392 | search_arch_type *new = | 
|---|
| 393 | (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type))); | 
|---|
| 394 |  | 
|---|
| 395 | if (*name != '\0') | 
|---|
| 396 | { | 
|---|
| 397 | if (ldfile_output_machine_name[0] != '\0') | 
|---|
| 398 | { | 
|---|
| 399 | einfo (_("%P%F: target architecture respecified\n")); | 
|---|
| 400 | return; | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | ldfile_output_machine_name = name; | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 | new->next = (search_arch_type *) NULL; | 
|---|
| 407 | new->name = gnu960_map_archname (name); | 
|---|
| 408 | *search_arch_tail_ptr = new; | 
|---|
| 409 | search_arch_tail_ptr = &new->next; | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | #else /* not GNU960 */ | 
|---|
| 413 |  | 
|---|
| 414 | void | 
|---|
| 415 | ldfile_add_arch (in_name) | 
|---|
| 416 | CONST char *in_name; | 
|---|
| 417 | { | 
|---|
| 418 | char *name = xstrdup (in_name); | 
|---|
| 419 | search_arch_type *new = | 
|---|
| 420 | (search_arch_type *) xmalloc (sizeof (search_arch_type)); | 
|---|
| 421 |  | 
|---|
| 422 | ldfile_output_machine_name = in_name; | 
|---|
| 423 |  | 
|---|
| 424 | new->name = name; | 
|---|
| 425 | new->next = (search_arch_type *) NULL; | 
|---|
| 426 | while (*name) | 
|---|
| 427 | { | 
|---|
| 428 | if (isupper ((unsigned char) *name)) | 
|---|
| 429 | *name = tolower ((unsigned char) *name); | 
|---|
| 430 | name++; | 
|---|
| 431 | } | 
|---|
| 432 | *search_arch_tail_ptr = new; | 
|---|
| 433 | search_arch_tail_ptr = &new->next; | 
|---|
| 434 |  | 
|---|
| 435 | } | 
|---|
| 436 | #endif | 
|---|
| 437 |  | 
|---|
| 438 | /* Set the output architecture.  */ | 
|---|
| 439 |  | 
|---|
| 440 | void | 
|---|
| 441 | ldfile_set_output_arch (string) | 
|---|
| 442 | CONST char *string; | 
|---|
| 443 | { | 
|---|
| 444 | const bfd_arch_info_type *arch = bfd_scan_arch (string); | 
|---|
| 445 |  | 
|---|
| 446 | if (arch) | 
|---|
| 447 | { | 
|---|
| 448 | ldfile_output_architecture = arch->arch; | 
|---|
| 449 | ldfile_output_machine = arch->mach; | 
|---|
| 450 | ldfile_output_machine_name = arch->printable_name; | 
|---|
| 451 | } | 
|---|
| 452 | else | 
|---|
| 453 | { | 
|---|
| 454 | einfo (_("%P%F: cannot represent machine `%s'\n"), string); | 
|---|
| 455 | } | 
|---|
| 456 | } | 
|---|