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