* ldfile.h (search_dirs_type): Move from ldfile.c, and add cmdline
[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   for (search = search_head;
118        search != (search_dirs_type *)NULL;
119        search = search->next) 
120     {
121       char buffer[1000];
122       char *string;
123       if (entry->is_archive == true) {
124         sprintf(buffer,
125                 "%s%s%s%s%s%s",
126                 search->name,
127                 slash,
128                 lib,
129                 entry->filename, arch, suffix);
130       }
131       else {
132         if (entry->filename[0] == '/' || entry->filename[0] == '.') {
133           strcpy(buffer, entry->filename);
134         } else {
135           sprintf(buffer,"%s%s%s",search->name, slash, entry->filename);
136         } 
137       }
138       string = buystring(buffer);      
139       desc = cached_bfd_openr (string, entry);
140       if (desc)
141         {
142           entry->filename = string;
143           entry->the_bfd =  desc;
144           return desc;
145         }
146       free(string);
147     }
148   return (bfd *)NULL;
149 }
150
151 /* Open the input file specified by 'entry', and return a descriptor.
152    The open file is remembered; if the same file is opened twice in a row,
153    a new open is not actually done.  */
154
155 void
156 ldfile_open_file (entry)
157      lang_input_statement_type *entry;
158 {
159   ASSERT (entry->the_bfd == NULL);
160
161   if (! entry->search_dirs_flag)
162     entry->the_bfd = cached_bfd_openr (entry->filename, entry);
163   else
164     {
165       search_arch_type *arch;
166
167       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
168       for (arch = search_arch_head;
169            arch != (search_arch_type *) NULL;
170            arch = arch->next)
171         {
172           if (config.dynamic_link)
173             {
174               /* FIXME: Perhaps we will sometimes want something other
175                  than .so.  */
176               if (open_a (arch->name, entry, "lib", ".so") != (bfd *) NULL)
177                 return;
178             }
179           if (open_a (arch->name, entry, "lib", ".a") != (bfd *) NULL)
180             return;
181 #ifdef VMS
182           if (open_a (arch->name, entry, ":lib", ".a") != (bfd *) NULL)
183             return;
184 #endif
185         }
186     }
187
188   if (entry->the_bfd == NULL)
189     einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
190 }
191
192 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
193
194 static FILE *
195 try_open(name, exten)
196      char *name;
197      char *exten;
198 {
199   FILE *result;
200   char buff[1000];
201
202   result = fopen(name, "r");
203   if (trace_file_tries == true) {
204     if (result == (FILE *)NULL) {
205       info_msg ("cannot find ");
206     }
207     info_msg ("%s\n",name);
208   }
209   if (result != (FILE *)NULL) {
210     return result;
211   }
212
213   if (*exten) {
214     sprintf(buff, "%s%s", name, exten);
215     result = fopen(buff, "r");
216     if (trace_file_tries == true) {
217       if (result == (FILE *)NULL) {
218         info_msg ("cannot find ");
219       }
220       info_msg ("%s\n", buff);
221     }
222   }
223   return result;
224 }
225
226 /* Try to open NAME; if that fails, look for it in any directories
227    specified with -L, without and with EXTEND apppended.  */
228
229 FILE *
230 ldfile_find_command_file(name, extend)
231 char *name;
232 char *extend;
233 {
234   search_dirs_type *search;
235   FILE *result;
236   char buffer[1000];
237
238   /* First try raw name */
239   result = try_open(name,"");
240   if (result == (FILE *)NULL) {
241     /* Try now prefixes */
242     for (search = search_head;
243          search != (search_dirs_type *)NULL;
244          search = search->next) {
245       sprintf(buffer,"%s/%s", search->name, name);
246       result = try_open(buffer, extend);
247       if (result)break;
248     }
249   }
250   return result;
251 }
252
253 void
254 ldfile_open_command_file(name)
255 char *name;
256 {
257   FILE *ldlex_input_stack;
258   ldlex_input_stack = ldfile_find_command_file(name, "");
259
260   if (ldlex_input_stack == (FILE *)NULL) {
261     bfd_set_error (bfd_error_system_call);
262     einfo("%P%F: cannot open linker script file %s: %E\n",name);
263   }
264   lex_push_file(ldlex_input_stack, name);
265   
266   ldfile_input_filename = name;
267   had_script = true;
268 }
269
270
271
272
273
274 #ifdef GNU960
275 static
276 char *
277 gnu960_map_archname( name )
278 char *name;
279 {
280   struct tabentry { char *cmd_switch; char *arch; };
281   static struct tabentry arch_tab[] = {
282         "",   "",
283         "KA", "ka",
284         "KB", "kb",
285         "KC", "mc",     /* Synonym for MC */
286         "MC", "mc",
287         "CA", "ca",
288         "SA", "ka",     /* Functionally equivalent to KA */
289         "SB", "kb",     /* Functionally equivalent to KB */
290         NULL, ""
291   };
292   struct tabentry *tp;
293   
294
295   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
296     if ( !strcmp(name,tp->cmd_switch) ){
297       break;
298     }
299   }
300
301   if ( tp->cmd_switch == NULL ){
302     einfo("%P%F: unknown architecture: %s\n",name);
303   }
304   return tp->arch;
305 }
306
307
308
309 void
310 ldfile_add_arch(name)
311 char *name;
312 {
313   search_arch_type *new =
314     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
315
316
317   if (*name != '\0') {
318     if (ldfile_output_machine_name[0] != '\0') {
319       einfo("%P%F: target architecture respecified\n");
320       return;
321     }
322     ldfile_output_machine_name = name;
323   }
324
325   new->next = (search_arch_type*)NULL;
326   new->name = gnu960_map_archname( name );
327   *search_arch_tail_ptr = new;
328   search_arch_tail_ptr = &new->next;
329
330 }
331
332 #else   /* not GNU960 */
333
334
335 void
336 ldfile_add_arch (in_name)
337      CONST char * in_name;
338 {
339   char *name = buystring(in_name);
340   search_arch_type *new =
341     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
342
343   ldfile_output_machine_name = in_name;
344
345   new->name = name;
346   new->next = (search_arch_type*)NULL;
347   while (*name) {
348     if (isupper(*name)) *name = tolower(*name);
349     name++;
350   }
351   *search_arch_tail_ptr = new;
352   search_arch_tail_ptr = &new->next;
353
354 }
355 #endif
356
357 /* Set the output architecture */
358 void
359 ldfile_set_output_arch (string)
360      CONST char *string;
361 {
362   bfd_arch_info_type *arch = bfd_scan_arch(string);
363
364   if (arch) {
365     ldfile_output_architecture = arch->arch;
366     ldfile_output_machine = arch->mach;
367     ldfile_output_machine_name = arch->printable_name;
368   }
369   else {
370     einfo("%P%F: cannot represent machine `%s'\n", string);
371   }
372 }