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