* sun build fix for thinko (?)
[binutils-gdb.git] / ld / ldfile.c
blob921b9127c4325dd7ae9aed39eef761e0786b5af1
1 /* Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
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.
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.
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. */
20 ldfile.c
22 look after all the file stuff
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"
39 #include <ctype.h>
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;
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 */
59 /* LOCAL */
61 static search_dirs_type **search_tail_ptr = &search_head;
63 typedef struct search_arch
65 char *name;
66 struct search_arch *next;
67 } search_arch_type;
69 static search_arch_type *search_arch_head;
70 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
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));
77 void
78 ldfile_add_library_path (name, cmdline)
79 const char *name;
80 boolean cmdline;
82 search_dirs_type *new;
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;
92 /* Try to open a BFD for a lang_input_statement. */
94 boolean
95 ldfile_try_open_bfd (attempt, entry)
96 const char *attempt;
97 lang_input_statement_type *entry;
99 entry->the_bfd = bfd_openr (attempt, entry->target);
101 if (trace_file_tries)
102 info_msg ("attempt to open %s %s\n", attempt,
103 entry->the_bfd == NULL ? "failed" : "succeeded");
105 if (entry->the_bfd != NULL)
106 return true;
107 else
108 return false;
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. */
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;
121 search_dirs_type *search;
123 /* If this is not an archive, try to open it in the current
124 directory first. */
125 if (! entry->is_archive)
127 if (ldfile_try_open_bfd (entry->filename, entry))
128 return true;
131 for (search = search_head;
132 search != (search_dirs_type *)NULL;
133 search = search->next)
135 char *string;
137 if (entry->dynamic && ! link_info.relocateable)
139 if (ldemul_open_dynamic_archive (arch, search, entry))
140 return true;
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);
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);
159 if (ldfile_try_open_bfd (string, entry))
161 entry->filename = string;
162 return true;
165 free (string);
168 return false;
171 /* Open the input file specified by ENTRY. */
173 void
174 ldfile_open_file (entry)
175 lang_input_statement_type *entry;
177 if (entry->the_bfd != NULL)
178 return;
180 if (! entry->search_dirs_flag)
182 if (ldfile_try_open_bfd (entry->filename, entry))
183 return;
185 else
187 search_arch_type *arch;
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)
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
203 einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
206 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
208 static FILE *
209 try_open (name, exten)
210 const char *name;
211 const char *exten;
213 FILE *result;
214 char buff[1000];
216 result = fopen (name, "r");
217 if (trace_file_tries)
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);
226 if (result != NULL)
227 return result;
229 if (*exten)
231 sprintf (buff, "%s%s", name, exten);
232 result = fopen (buff, "r");
233 if (trace_file_tries)
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);
243 return result;
246 /* Try to open NAME; if that fails, look for it in any directories
247 specified with -L, without and with EXTEND apppended. */
249 FILE *
250 ldfile_find_command_file (name, extend)
251 const char *name;
252 const char *extend;
254 search_dirs_type *search;
255 FILE *result;
256 char buffer[1000];
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;
270 return result;
273 void
274 ldfile_open_command_file (name)
275 const char *name;
277 FILE *ldlex_input_stack;
278 ldlex_input_stack = ldfile_find_command_file(name, "");
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);
284 lex_push_file(ldlex_input_stack, name);
286 ldfile_input_filename = name;
287 lineno = 1;
288 had_script = true;
295 #ifdef GNU960
296 static
297 char *
298 gnu960_map_archname( name )
299 char *name;
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, ""
313 struct tabentry *tp;
316 for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
317 if ( !strcmp(name,tp->cmd_switch) ){
318 break;
322 if ( tp->cmd_switch == NULL ){
323 einfo("%P%F: unknown architecture: %s\n",name);
325 return tp->arch;
330 void
331 ldfile_add_arch(name)
332 char *name;
334 search_arch_type *new =
335 (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
338 if (*name != '\0') {
339 if (ldfile_output_machine_name[0] != '\0') {
340 einfo("%P%F: target architecture respecified\n");
341 return;
343 ldfile_output_machine_name = name;
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;
353 #else /* not GNU960 */
356 void
357 ldfile_add_arch (in_name)
358 CONST char * in_name;
360 char *name = buystring(in_name);
361 search_arch_type *new =
362 (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
364 ldfile_output_machine_name = in_name;
366 new->name = name;
367 new->next = (search_arch_type*)NULL;
368 while (*name) {
369 if (isupper(*name)) *name = tolower(*name);
370 name++;
372 *search_arch_tail_ptr = new;
373 search_arch_tail_ptr = &new->next;
376 #endif
378 /* Set the output architecture */
379 void
380 ldfile_set_output_arch (string)
381 CONST char *string;
383 bfd_arch_info_type *arch = bfd_scan_arch(string);
385 if (arch) {
386 ldfile_output_architecture = arch->arch;
387 ldfile_output_machine = arch->mach;
388 ldfile_output_machine_name = arch->printable_name;
390 else {
391 einfo("%P%F: cannot represent machine `%s'\n", string);