This commit was manufactured by cvs2svn to create branch 'binutils'.
[external/binutils.git] / ld / ldfile.c
1
2 /* Copyright (C) 1991 Free Software Foundation, Inc.
3
4 This file is part of GLD, the Gnu Linker.
5
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /*
21    $Id$ 
22
23    $Log$
24    Revision 1.1  1991/03/21 21:28:37  gumby
25    Initial revision
26
27  * Revision 1.2  1991/03/15  18:45:55  rich
28  * foo
29  *
30  * Revision 1.1  1991/03/13  00:48:18  chrisb
31  * Initial revision
32  *
33  * Revision 1.4  1991/03/10  09:31:24  rich
34  *  Modified Files:
35  *      Makefile config.h ld-emul.c ld-emul.h ld-gld.c ld-gld960.c
36  *      ld-lnk960.c ld.h lddigest.c ldexp.c ldexp.h ldfile.c ldfile.h
37  *      ldgram.y ldinfo.h ldlang.c ldlang.h ldlex.h ldlex.l ldmain.c
38  *      ldmain.h ldmisc.c ldmisc.h ldsym.c ldsym.h ldversion.c
39  *      ldversion.h ldwarn.h ldwrite.c ldwrite.h y.tab.h
40  *
41  * As of this round of changes, ld now builds on all hosts of (Intel960)
42  * interest and copy passes my copy test on big endian hosts again.
43  *
44  * Revision 1.3  1991/02/22  17:15:00  sac
45  * Added RCS keywords and copyrights
46  *
47 */
48
49 /*
50  ldfile.c
51
52  look after all the file stuff
53
54  */
55
56 #include "sysdep.h"
57 #include "bfd.h"
58
59 #include "ldmisc.h"
60 #include "ldlang.h"
61 #include "ldfile.h"
62
63 #include <ctype.h>
64
65 /* EXPORT */
66 char *ldfile_input_filename;
67 char *ldfile_output_machine_name;
68 unsigned long ldfile_output_machine;
69 enum bfd_architecture ldfile_output_architecture;
70 boolean had_script;
71
72 /* IMPORT */
73
74 extern boolean option_v;
75
76
77
78
79
80 /* LOACL */
81 typedef struct search_dirs_struct 
82 {
83   char *name;
84   struct search_dirs_struct *next;
85 } search_dirs_type;
86
87 static search_dirs_type *search_head;
88 static search_dirs_type **search_tail_ptr = &search_head;
89
90 typedef struct search_arch_struct 
91 {
92   char *name; 
93   struct search_arch_struct *next;
94 } search_arch_type;
95
96 static search_arch_type *search_arch_head;
97 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
98  
99 void
100 ldfile_add_library_path(name)
101 char *name;
102 {
103   search_dirs_type *new =
104     (search_dirs_type *)ldmalloc(sizeof(search_dirs_type));
105   new->name = name;
106   new->next = (search_dirs_type*)NULL;
107   *search_tail_ptr = new;
108   search_tail_ptr = &new->next;
109 }
110
111
112 static bfd*
113 cached_bfd_openr(attempt,entry)
114 char *attempt;
115 lang_input_statement_type  *entry;
116 {
117   entry->the_bfd = bfd_openr(attempt, entry->target);
118
119
120   return entry->the_bfd;
121 }
122
123 static bfd *
124 open_a(arch, entry, lib, suffix)
125 char *arch;
126 lang_input_statement_type *entry;
127 char *lib;
128 char *suffix;
129 {
130   bfd*desc;
131   search_dirs_type *search ;
132   for (search = search_head;
133        search != (search_dirs_type *)NULL;
134        search = search->next) 
135     {
136       char buffer[1000];
137       char *string;
138       if (entry->is_archive == true) {
139         sprintf(buffer,
140                 "%s/%s%s%s%s",
141                 search->name,
142                 lib,
143                 entry->filename, arch, suffix);
144       }
145       else {
146               if (entry->filename[0] == '/') {
147                       strcpy(buffer, entry->filename);
148               } else {
149                       sprintf(buffer,"%s/%s",search->name, entry->filename);
150               }                 /*  */
151       }
152       string = buystring(buffer);      
153       desc = cached_bfd_openr (string, entry);
154       if (desc)
155         {
156           entry->filename = string;
157           entry->search_dirs_flag = false;
158           entry->the_bfd =  desc;
159           return desc;
160         }
161       free(string);
162     }
163   return (bfd *)NULL;
164 }
165
166 /* Open the input file specified by 'entry', and return a descriptor.
167    The open file is remembered; if the same file is opened twice in a row,
168    a new open is not actually done.  */
169
170 void
171 ldfile_open_file (entry)
172 lang_input_statement_type *entry;
173 {
174
175   if (entry->superfile)
176     ldfile_open_file (entry->superfile);
177
178   if (entry->search_dirs_flag)
179     {
180       search_arch_type *arch;
181       for (arch = search_arch_head;
182            arch != (search_arch_type *)NULL;
183            arch = arch->next) {
184         if (open_a(arch->name,entry,"","") != (bfd *)NULL) {
185           return;
186         }
187         if (open_a(arch->name,entry,"lib",".a") != (bfd *)NULL) {
188           return;
189         }
190
191       }
192
193
194     }
195   else {
196     entry->the_bfd = cached_bfd_openr (entry->filename, entry);
197
198   }
199   if (!entry->the_bfd)  info("%F%P: %E %I\n", entry);
200
201 }
202
203
204
205
206
207
208 static FILE *
209 try_open(name, exten)
210 char *name;
211 char *exten;
212 {
213   FILE *result;
214   char buff[1000];
215   result = fopen(name, "r");
216   if (result && option_v == true) {
217     info("%s\n",name);
218     return result;
219   }
220   sprintf(buff, "%s%s", name, exten);
221   result = fopen(buff, "r");
222
223   if (result && option_v == true) {
224     info("%s\n", buff);
225   }
226   return result;
227 }
228 static FILE *
229 find_a_name(name, extend)
230 char *name;
231 char *extend;
232 {
233   search_dirs_type *search;
234   FILE *result;
235   char buffer[1000];
236   /* First try raw name */
237   result = try_open(name,"");
238   if (result == (FILE *)NULL) {
239     /* Try now prefixes */
240     for (search = search_head;
241          search != (search_dirs_type *)NULL;
242          search = search->next) {
243       sprintf(buffer,"%s/%s", search->name, name);
244       result = try_open(buffer, extend);
245       if (result)break;
246     }
247   }
248   return result;
249 }
250
251 void ldfile_open_command_file(name)
252 char *name;
253 {
254   extern FILE *ldlex_input_stack;
255   ldlex_input_stack = find_a_name(name, ".ld");
256
257   if (ldlex_input_stack == (FILE *)NULL) {
258     info("%P%F cannot open load script file %s\n",name);
259   }
260   ldfile_input_filename = name;
261   had_script = true;
262 }
263
264
265
266
267 void
268 ldfile_add_arch(name)
269 char *name;
270 {
271   search_arch_type *new =
272     (search_arch_type *)ldmalloc(sizeof(search_arch_type));
273   ldfile_output_machine_name = name;
274
275   new->name = name;
276   new->next = (search_arch_type*)NULL;
277   while (*name) {
278     if (isupper(*name)) *name = tolower(*name);
279     name++;
280   }
281   *search_arch_tail_ptr = new;
282   search_arch_tail_ptr = &new->next;
283
284 }