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