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)
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 look after all the file stuff
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 search_dirs_type
*search_head
;
45 /* start-sanitize-mpw */
47 /* end-sanitize-mpw */
53 /* start-sanitize-mpw */
55 /* The MPW path char is a colon. */
58 /* end-sanitize-mpw */
62 static search_dirs_type
**search_tail_ptr
= &search_head
;
64 typedef struct search_arch
67 struct search_arch
*next
;
70 static search_arch_type
*search_arch_head
;
71 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
73 static bfd
*cached_bfd_openr
PARAMS ((const char *attempt
,
74 lang_input_statement_type
*entry
));
75 static bfd
*open_a
PARAMS ((char *arch
, lang_input_statement_type
*entry
,
76 char *lib
, char *suffix
));
77 static FILE *try_open
PARAMS ((char *name
, char *exten
));
80 ldfile_add_library_path (name
, cmdline
)
84 search_dirs_type
*new;
86 new = (search_dirs_type
*) xmalloc (sizeof (search_dirs_type
));
89 new->cmdline
= cmdline
;
90 *search_tail_ptr
= new;
91 search_tail_ptr
= &new->next
;
96 cached_bfd_openr(attempt
,entry
)
98 lang_input_statement_type
*entry
;
100 entry
->the_bfd
= bfd_openr(attempt
, entry
->target
);
101 if (trace_file_tries
== true ) {
102 info_msg ("attempt to open %s %s\n", attempt
,
103 (entry
->the_bfd
== (bfd
*)NULL
) ? "failed" : "succeeded" );
105 return entry
->the_bfd
;
109 open_a(arch
, entry
, lib
, suffix
)
111 lang_input_statement_type
*entry
;
116 search_dirs_type
*search
;
118 /* If this is not an archive, try to open it in the current
120 if (! entry
->is_archive
)
122 desc
= cached_bfd_openr (entry
->filename
, entry
);
127 for (search
= search_head
;
128 search
!= (search_dirs_type
*)NULL
;
129 search
= search
->next
)
133 if (entry
->is_archive
== true) {
139 entry
->filename
, arch
, suffix
);
142 if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.') {
143 strcpy(buffer
, entry
->filename
);
145 sprintf(buffer
,"%s%s%s",search
->name
, slash
, entry
->filename
);
148 string
= buystring(buffer
);
149 desc
= cached_bfd_openr (string
, entry
);
152 entry
->filename
= string
;
153 entry
->the_bfd
= desc
;
161 /* Open the input file specified by 'entry', and return a descriptor.
162 The open file is remembered; if the same file is opened twice in a row,
163 a new open is not actually done. */
166 ldfile_open_file (entry
)
167 lang_input_statement_type
*entry
;
169 ASSERT (entry
->the_bfd
== NULL
);
171 if (! entry
->search_dirs_flag
)
172 entry
->the_bfd
= cached_bfd_openr (entry
->filename
, entry
);
175 search_arch_type
*arch
;
177 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
178 for (arch
= search_arch_head
;
179 arch
!= (search_arch_type
*) NULL
;
182 if (config
.dynamic_link
)
184 /* FIXME: Perhaps we will sometimes want something other
186 if (open_a (arch
->name
, entry
, "lib", ".so") != (bfd
*) NULL
)
189 if (open_a (arch
->name
, entry
, "lib", ".a") != (bfd
*) NULL
)
192 if (open_a (arch
->name
, entry
, ":lib", ".a") != (bfd
*) NULL
)
198 if (entry
->the_bfd
== NULL
)
199 einfo("%F%P: cannot open %s: %E\n", entry
->local_sym_name
);
202 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
205 try_open(name
, exten
)
212 result
= fopen(name
, "r");
213 if (trace_file_tries
== true) {
214 if (result
== (FILE *)NULL
) {
215 info_msg ("cannot find ");
217 info_msg ("%s\n",name
);
219 if (result
!= (FILE *)NULL
) {
224 sprintf(buff
, "%s%s", name
, exten
);
225 result
= fopen(buff
, "r");
226 if (trace_file_tries
== true) {
227 if (result
== (FILE *)NULL
) {
228 info_msg ("cannot find ");
230 info_msg ("%s\n", buff
);
236 /* Try to open NAME; if that fails, look for it in any directories
237 specified with -L, without and with EXTEND apppended. */
240 ldfile_find_command_file(name
, extend
)
244 search_dirs_type
*search
;
248 /* First try raw name */
249 result
= try_open(name
,"");
250 if (result
== (FILE *)NULL
) {
251 /* Try now prefixes */
252 for (search
= search_head
;
253 search
!= (search_dirs_type
*)NULL
;
254 search
= search
->next
) {
255 sprintf(buffer
,"%s/%s", search
->name
, name
);
256 result
= try_open(buffer
, extend
);
264 ldfile_open_command_file(name
)
267 FILE *ldlex_input_stack
;
268 ldlex_input_stack
= ldfile_find_command_file(name
, "");
270 if (ldlex_input_stack
== (FILE *)NULL
) {
271 bfd_set_error (bfd_error_system_call
);
272 einfo("%P%F: cannot open linker script file %s: %E\n",name
);
274 lex_push_file(ldlex_input_stack
, name
);
276 ldfile_input_filename
= name
;
287 gnu960_map_archname( name
)
290 struct tabentry
{ char *cmd_switch
; char *arch
; };
291 static struct tabentry arch_tab
[] = {
295 "KC", "mc", /* Synonym for MC */
298 "SA", "ka", /* Functionally equivalent to KA */
299 "SB", "kb", /* Functionally equivalent to KB */
305 for ( tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++ ){
306 if ( !strcmp(name
,tp
->cmd_switch
) ){
311 if ( tp
->cmd_switch
== NULL
){
312 einfo("%P%F: unknown architecture: %s\n",name
);
320 ldfile_add_arch(name
)
323 search_arch_type
*new =
324 (search_arch_type
*)xmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
328 if (ldfile_output_machine_name
[0] != '\0') {
329 einfo("%P%F: target architecture respecified\n");
332 ldfile_output_machine_name
= name
;
335 new->next
= (search_arch_type
*)NULL
;
336 new->name
= gnu960_map_archname( name
);
337 *search_arch_tail_ptr
= new;
338 search_arch_tail_ptr
= &new->next
;
342 #else /* not GNU960 */
346 ldfile_add_arch (in_name
)
347 CONST
char * in_name
;
349 char *name
= buystring(in_name
);
350 search_arch_type
*new =
351 (search_arch_type
*)xmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
353 ldfile_output_machine_name
= in_name
;
356 new->next
= (search_arch_type
*)NULL
;
358 if (isupper(*name
)) *name
= tolower(*name
);
361 *search_arch_tail_ptr
= new;
362 search_arch_tail_ptr
= &new->next
;
367 /* Set the output architecture */
369 ldfile_set_output_arch (string
)
372 bfd_arch_info_type
*arch
= bfd_scan_arch(string
);
375 ldfile_output_architecture
= arch
->arch
;
376 ldfile_output_machine
= arch
->mach
;
377 ldfile_output_machine_name
= arch
->printable_name
;
380 einfo("%P%F: cannot represent machine `%s'\n", string
);