Add support for WinCE based toolchains.
[platform/upstream/binutils.git] / ld / ldfile.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 98, 99, 2000 Free Software Foundation, Inc.
2
3 This file is part of GLD, the Gnu Linker.
4
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING.  If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 02111-1307, USA.  */
19
20 /*
21  ldfile.c
22
23  look after all the file stuff
24
25  */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmisc.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include "ldfile.h"
35 #include "ldmain.h"
36 #include "ldgram.h"
37 #include "ldlex.h"
38 #include "ldemul.h"
39
40 #include <ctype.h>
41
42 const char *ldfile_input_filename;
43 boolean ldfile_assumed_script = false;
44 const char *ldfile_output_machine_name = "";
45 unsigned long ldfile_output_machine;
46 enum bfd_architecture ldfile_output_architecture;
47 search_dirs_type *search_head;
48
49 #ifndef MPW
50 #ifdef VMS
51 char *slash = "";
52 #else
53 #if defined (_WIN32) && ! defined (__CYGWIN32__)
54 char *slash = "\\";
55 #else
56 char *slash = "/";
57 #endif
58 #endif
59 #else /* MPW */
60 /* The MPW path char is a colon. */
61 char *slash = ":";
62 #endif /* MPW */
63
64 /* LOCAL */
65
66 static search_dirs_type **search_tail_ptr = &search_head;
67
68 typedef struct search_arch 
69 {
70   char *name; 
71   struct search_arch *next;
72 } search_arch_type;
73
74 static search_arch_type *search_arch_head;
75 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
76  
77 static FILE *try_open PARAMS ((const char *name, const char *exten));
78
79 void
80 ldfile_add_library_path (name, cmdline)
81      const char *name;
82      boolean cmdline;
83 {
84   search_dirs_type *new;
85
86   new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
87   new->next = NULL;
88   new->name = name;
89   new->cmdline = cmdline;
90   *search_tail_ptr = new;
91   search_tail_ptr = &new->next;
92 }
93
94 /* Try to open a BFD for a lang_input_statement.  */
95
96 boolean
97 ldfile_try_open_bfd (attempt, entry)
98      const char *attempt;
99      lang_input_statement_type *entry;
100 {
101   entry->the_bfd = bfd_openr (attempt, entry->target);
102
103   if (trace_file_tries)
104     {
105       if (entry->the_bfd == NULL)
106         info_msg (_("attempt to open %s failed\n"), attempt);
107       else
108         info_msg (_("attempt to open %s succeeded\n"), attempt);
109     }
110
111   if (entry->the_bfd == NULL)
112     {
113       if (bfd_get_error () == bfd_error_invalid_target)
114         einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
115       return false;
116     }
117
118   /* If we are searching for this file, see if the architecture is
119      compatible with the output file.  If it isn't, keep searching.
120      If we can't open the file as an object file, stop the search
121      here.  */
122
123   if (entry->search_dirs_flag)
124     {
125       bfd *check;
126
127       if (bfd_check_format (entry->the_bfd, bfd_archive))
128         check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
129       else
130         check = entry->the_bfd;
131
132       if (! bfd_check_format (check, bfd_object))
133         return true;
134       if (bfd_arch_get_compatible (check, output_bfd) == NULL)
135         {
136           einfo (_("%P: skipping incompatible %s when searching for %s"),
137                  attempt, entry->local_sym_name);
138           bfd_close (entry->the_bfd);
139           entry->the_bfd = NULL;
140           return false;
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
236       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
237       for (arch = search_arch_head;
238            arch != (search_arch_type *) NULL;
239            arch = arch->next)
240         {
241           if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
242             return;
243 #ifdef VMS
244           if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
245             return;
246 #endif
247           if (ldemul_find_potential_libraries (arch->name, entry))
248             return;
249         }
250       einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
251     }
252 }
253
254 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
255
256 static FILE *
257 try_open (name, exten)
258      const char *name;
259      const char *exten;
260 {
261   FILE *result;
262   char buff[1000];
263
264   result = fopen (name, "r");
265   if (trace_file_tries)
266     {
267       if (result == NULL)
268         info_msg (_("cannot find script file %s\n"), name);
269       else
270         info_msg (_("opened script file %s\n"), name);
271     }
272
273   if (result != NULL)
274     return result;
275
276   if (*exten)
277     {
278       sprintf (buff, "%s%s", name, exten);
279       result = fopen (buff, "r");
280       if (trace_file_tries)
281         {
282           if (result == NULL)
283             info_msg (_("cannot find script file %s\n"), buff);
284           else
285             info_msg (_("opened script file %s\n"), buff);
286         }
287     }
288
289   return result;
290 }
291
292 /* Try to open NAME; if that fails, look for it in any directories
293    specified with -L, without and with EXTEND apppended.  */
294
295 FILE *
296 ldfile_find_command_file (name, extend)
297      const char *name;
298      const char *extend;
299 {
300   search_dirs_type *search;
301   FILE *result;
302   char buffer[1000];
303
304   /* First try raw name */
305   result = try_open(name,"");
306   if (result == (FILE *)NULL) {
307     /* Try now prefixes */
308     for (search = search_head;
309          search != (search_dirs_type *)NULL;
310          search = search->next) {
311       sprintf(buffer,"%s%s%s", search->name, slash, name);
312       result = try_open(buffer, extend);
313       if (result)break;
314     }
315   }
316   return result;
317 }
318
319 void
320 ldfile_open_command_file (name)
321      const char *name;
322 {
323   FILE *ldlex_input_stack;
324   ldlex_input_stack = ldfile_find_command_file(name, "");
325
326   if (ldlex_input_stack == (FILE *)NULL) {
327     bfd_set_error (bfd_error_system_call);
328     einfo(_("%P%F: cannot open linker script file %s: %E\n"),name);
329   }
330   lex_push_file(ldlex_input_stack, name);
331   
332   ldfile_input_filename = name;
333   lineno = 1;
334   had_script = true;
335 }
336
337
338
339
340
341 #ifdef GNU960
342 static
343 char *
344 gnu960_map_archname( name )
345 char *name;
346 {
347   struct tabentry { char *cmd_switch; char *arch; };
348   static struct tabentry arch_tab[] = {
349         "",   "",
350         "KA", "ka",
351         "KB", "kb",
352         "KC", "mc",     /* Synonym for MC */
353         "MC", "mc",
354         "CA", "ca",
355         "SA", "ka",     /* Functionally equivalent to KA */
356         "SB", "kb",     /* Functionally equivalent to KB */
357         NULL, ""
358   };
359   struct tabentry *tp;
360   
361
362   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
363     if ( !strcmp(name,tp->cmd_switch) ){
364       break;
365     }
366   }
367
368   if ( tp->cmd_switch == NULL ){
369     einfo(_("%P%F: unknown architecture: %s\n"),name);
370   }
371   return tp->arch;
372 }
373
374
375
376 void
377 ldfile_add_arch(name)
378 char *name;
379 {
380   search_arch_type *new =
381     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
382
383
384   if (*name != '\0') {
385     if (ldfile_output_machine_name[0] != '\0') {
386       einfo(_("%P%F: target architecture respecified\n"));
387       return;
388     }
389     ldfile_output_machine_name = name;
390   }
391
392   new->next = (search_arch_type*)NULL;
393   new->name = gnu960_map_archname( name );
394   *search_arch_tail_ptr = new;
395   search_arch_tail_ptr = &new->next;
396
397 }
398
399 #else   /* not GNU960 */
400
401
402 void
403 ldfile_add_arch (in_name)
404      CONST char * in_name;
405 {
406   char *name = buystring(in_name);
407   search_arch_type *new =
408     (search_arch_type *) xmalloc (sizeof (search_arch_type));
409
410   ldfile_output_machine_name = in_name;
411
412   new->name = name;
413   new->next = (search_arch_type*)NULL;
414   while (*name)
415     {
416       if (isupper ((unsigned char) *name))
417         *name = tolower ((unsigned char) *name);
418       name++;
419     }
420   *search_arch_tail_ptr = new;
421   search_arch_tail_ptr = &new->next;
422
423 }
424 #endif
425
426 /* Set the output architecture */
427 void
428 ldfile_set_output_arch (string)
429      CONST char *string;
430 {
431   const bfd_arch_info_type *arch = bfd_scan_arch(string);
432
433   if (arch) {
434     ldfile_output_architecture = arch->arch;
435     ldfile_output_machine = arch->mach;
436     ldfile_output_machine_name = arch->printable_name;
437   }
438   else {
439     einfo(_("%P%F: cannot represent machine `%s'\n"), string);
440   }
441 }