* ldfile.c (open_a): If this is not an archive, try to open it in
[external/binutils.git] / ld / ldfile.c
1 /* Copyright (C) 1991, 92, 93, 94 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 1, 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
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
18
19 /*
20  ldfile.c
21
22  look after all the file stuff
23
24  */
25
26 #include "bfd.h"
27 #include "sysdep.h"
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
37 #include <ctype.h>
38
39 char *ldfile_input_filename;
40 const char *ldfile_output_machine_name = "";
41 unsigned long ldfile_output_machine;
42 enum bfd_architecture ldfile_output_architecture;
43 search_dirs_type *search_head;
44
45 /* start-sanitize-mpw */
46 #ifndef MPW
47 /* end-sanitize-mpw */
48 #ifdef VMS
49 char *slash = "";
50 #else
51 char *slash = "/";
52 #endif
53 /* start-sanitize-mpw */
54 #else /* MPW */
55 /* The MPW path char is a colon. */
56 char *slash = ":";
57 #endif /* MPW */
58 /* end-sanitize-mpw */
59
60 /* LOCAL */
61
62 static search_dirs_type **search_tail_ptr = &search_head;
63
64 typedef struct search_arch 
65 {
66   char *name; 
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;
72  
73 static bfd *cached_bfd_openr PARAMS ((const char *attempt,
74                                       lang_input_statement_type *entry));
75 static bfd *open_a PARAMS ((char *arch, lang_input_statement_type *entry,
76                             char *lib, char *suffix));
77 static FILE *try_open PARAMS ((char *name, 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
95 static bfd *
96 cached_bfd_openr(attempt,entry)
97      const char *attempt;
98      lang_input_statement_type  *entry;
99 {
100   entry->the_bfd = bfd_openr(attempt, entry->target);
101   if (trace_file_tries == true ) {
102     info_msg ("attempt to open %s %s\n", attempt,
103                 (entry->the_bfd == (bfd *)NULL) ? "failed" : "succeeded" );
104   }
105   return entry->the_bfd;
106 }
107
108 static bfd *
109 open_a(arch, entry, lib, suffix)
110      char *arch;
111      lang_input_statement_type *entry;
112      char *lib;
113      char *suffix;
114 {
115   bfd*desc;
116   search_dirs_type *search ;
117
118   /* If this is not an archive, try to open it in the current
119      directory first.  */
120   if (! entry->is_archive)
121     {
122       desc = cached_bfd_openr (entry->filename, entry);
123       if (desc != NULL)
124         return desc;
125     }
126
127   for (search = search_head;
128        search != (search_dirs_type *)NULL;
129        search = search->next) 
130     {
131       char buffer[1000];
132       char *string;
133       if (entry->is_archive == true) {
134         sprintf(buffer,
135                 "%s%s%s%s%s%s",
136                 search->name,
137                 slash,
138                 lib,
139                 entry->filename, arch, suffix);
140       }
141       else {
142         if (entry->filename[0] == '/' || entry->filename[0] == '.') {
143           strcpy(buffer, entry->filename);
144         } else {
145           sprintf(buffer,"%s%s%s",search->name, slash, entry->filename);
146         } 
147       }
148       string = buystring(buffer);      
149       desc = cached_bfd_openr (string, entry);
150       if (desc)
151         {
152           entry->filename = string;
153           entry->the_bfd =  desc;
154           return desc;
155         }
156       free(string);
157     }
158   return (bfd *)NULL;
159 }
160
161 /* Open the input file specified by 'entry', and return a descriptor.
162    The open file is remembered; if the same file is opened twice in a row,
163    a new open is not actually done.  */
164
165 void
166 ldfile_open_file (entry)
167      lang_input_statement_type *entry;
168 {
169   ASSERT (entry->the_bfd == NULL);
170
171   if (! entry->search_dirs_flag)
172     entry->the_bfd = cached_bfd_openr (entry->filename, entry);
173   else
174     {
175       search_arch_type *arch;
176
177       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
178       for (arch = search_arch_head;
179            arch != (search_arch_type *) NULL;
180            arch = arch->next)
181         {
182           if (config.dynamic_link)
183             {
184               /* FIXME: Perhaps we will sometimes want something other
185                  than .so.  */
186               if (open_a (arch->name, entry, "lib", ".so") != (bfd *) NULL)
187                 return;
188             }
189           if (open_a (arch->name, entry, "lib", ".a") != (bfd *) NULL)
190             return;
191 #ifdef VMS
192           if (open_a (arch->name, entry, ":lib", ".a") != (bfd *) NULL)
193             return;
194 #endif
195         }
196     }
197
198   if (entry->the_bfd == NULL)
199     einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
200 }
201
202 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
203
204 static FILE *
205 try_open(name, exten)
206      char *name;
207      char *exten;
208 {
209   FILE *result;
210   char buff[1000];
211
212   result = fopen(name, "r");
213   if (trace_file_tries == true) {
214     if (result == (FILE *)NULL) {
215       info_msg ("cannot find ");
216     }
217     info_msg ("%s\n",name);
218   }
219   if (result != (FILE *)NULL) {
220     return result;
221   }
222
223   if (*exten) {
224     sprintf(buff, "%s%s", name, exten);
225     result = fopen(buff, "r");
226     if (trace_file_tries == true) {
227       if (result == (FILE *)NULL) {
228         info_msg ("cannot find ");
229       }
230       info_msg ("%s\n", buff);
231     }
232   }
233   return result;
234 }
235
236 /* Try to open NAME; if that fails, look for it in any directories
237    specified with -L, without and with EXTEND apppended.  */
238
239 FILE *
240 ldfile_find_command_file(name, extend)
241 char *name;
242 char *extend;
243 {
244   search_dirs_type *search;
245   FILE *result;
246   char buffer[1000];
247
248   /* First try raw name */
249   result = try_open(name,"");
250   if (result == (FILE *)NULL) {
251     /* Try now prefixes */
252     for (search = search_head;
253          search != (search_dirs_type *)NULL;
254          search = search->next) {
255       sprintf(buffer,"%s/%s", search->name, name);
256       result = try_open(buffer, extend);
257       if (result)break;
258     }
259   }
260   return result;
261 }
262
263 void
264 ldfile_open_command_file(name)
265 char *name;
266 {
267   FILE *ldlex_input_stack;
268   ldlex_input_stack = ldfile_find_command_file(name, "");
269
270   if (ldlex_input_stack == (FILE *)NULL) {
271     bfd_set_error (bfd_error_system_call);
272     einfo("%P%F: cannot open linker script file %s: %E\n",name);
273   }
274   lex_push_file(ldlex_input_stack, name);
275   
276   ldfile_input_filename = name;
277   had_script = true;
278 }
279
280
281
282
283
284 #ifdef GNU960
285 static
286 char *
287 gnu960_map_archname( name )
288 char *name;
289 {
290   struct tabentry { char *cmd_switch; char *arch; };
291   static struct tabentry arch_tab[] = {
292         "",   "",
293         "KA", "ka",
294         "KB", "kb",
295         "KC", "mc",     /* Synonym for MC */
296         "MC", "mc",
297         "CA", "ca",
298         "SA", "ka",     /* Functionally equivalent to KA */
299         "SB", "kb",     /* Functionally equivalent to KB */
300         NULL, ""
301   };
302   struct tabentry *tp;
303   
304
305   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
306     if ( !strcmp(name,tp->cmd_switch) ){
307       break;
308     }
309   }
310
311   if ( tp->cmd_switch == NULL ){
312     einfo("%P%F: unknown architecture: %s\n",name);
313   }
314   return tp->arch;
315 }
316
317
318
319 void
320 ldfile_add_arch(name)
321 char *name;
322 {
323   search_arch_type *new =
324     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
325
326
327   if (*name != '\0') {
328     if (ldfile_output_machine_name[0] != '\0') {
329       einfo("%P%F: target architecture respecified\n");
330       return;
331     }
332     ldfile_output_machine_name = name;
333   }
334
335   new->next = (search_arch_type*)NULL;
336   new->name = gnu960_map_archname( name );
337   *search_arch_tail_ptr = new;
338   search_arch_tail_ptr = &new->next;
339
340 }
341
342 #else   /* not GNU960 */
343
344
345 void
346 ldfile_add_arch (in_name)
347      CONST char * in_name;
348 {
349   char *name = buystring(in_name);
350   search_arch_type *new =
351     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
352
353   ldfile_output_machine_name = in_name;
354
355   new->name = name;
356   new->next = (search_arch_type*)NULL;
357   while (*name) {
358     if (isupper(*name)) *name = tolower(*name);
359     name++;
360   }
361   *search_arch_tail_ptr = new;
362   search_arch_tail_ptr = &new->next;
363
364 }
365 #endif
366
367 /* Set the output architecture */
368 void
369 ldfile_set_output_arch (string)
370      CONST char *string;
371 {
372   bfd_arch_info_type *arch = bfd_scan_arch(string);
373
374   if (arch) {
375     ldfile_output_architecture = arch->arch;
376     ldfile_output_machine = arch->mach;
377     ldfile_output_machine_name = arch->printable_name;
378   }
379   else {
380     einfo("%P%F: cannot represent machine `%s'\n", string);
381   }
382 }