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