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