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