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
;
44 /* start-sanitize-mpw */
46 /* end-sanitize-mpw */
52 /* start-sanitize-mpw */
54 /* The MPW path char is a colon. */
57 /* end-sanitize-mpw */
63 typedef struct search_dirs
66 struct search_dirs
*next
;
69 static search_dirs_type
*search_head
;
70 static search_dirs_type
**search_tail_ptr
= &search_head
;
72 typedef struct search_arch
75 struct search_arch
*next
;
78 static search_arch_type
*search_arch_head
;
79 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
81 static bfd
*cached_bfd_openr
PARAMS ((const char *attempt
,
82 lang_input_statement_type
*entry
));
83 static bfd
*open_a
PARAMS ((char *arch
, lang_input_statement_type
*entry
,
84 char *lib
, char *suffix
));
85 static FILE *try_open
PARAMS ((char *name
, char *exten
));
88 ldfile_add_library_path(name
)
91 search_dirs_type
*new =
92 (search_dirs_type
*)xmalloc((bfd_size_type
)(sizeof(search_dirs_type
)));
94 new->next
= (search_dirs_type
*)NULL
;
95 *search_tail_ptr
= new;
96 search_tail_ptr
= &new->next
;
101 cached_bfd_openr(attempt
,entry
)
103 lang_input_statement_type
*entry
;
105 entry
->the_bfd
= bfd_openr(attempt
, entry
->target
);
106 if (trace_file_tries
== true ) {
107 info_msg ("attempt to open %s %s\n", attempt
,
108 (entry
->the_bfd
== (bfd
*)NULL
) ? "failed" : "succeeded" );
110 return entry
->the_bfd
;
114 open_a(arch
, entry
, lib
, suffix
)
116 lang_input_statement_type
*entry
;
121 search_dirs_type
*search
;
122 for (search
= search_head
;
123 search
!= (search_dirs_type
*)NULL
;
124 search
= search
->next
)
128 if (entry
->is_archive
== true) {
134 entry
->filename
, arch
, suffix
);
137 if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.') {
138 strcpy(buffer
, entry
->filename
);
140 sprintf(buffer
,"%s%s%s",search
->name
, slash
, entry
->filename
);
143 string
= buystring(buffer
);
144 desc
= cached_bfd_openr (string
, entry
);
147 entry
->filename
= string
;
148 entry
->the_bfd
= desc
;
156 /* Open the input file specified by 'entry', and return a descriptor.
157 The open file is remembered; if the same file is opened twice in a row,
158 a new open is not actually done. */
161 ldfile_open_file (entry
)
162 lang_input_statement_type
*entry
;
164 ASSERT (entry
->the_bfd
== NULL
);
166 if (! entry
->search_dirs_flag
)
167 entry
->the_bfd
= cached_bfd_openr (entry
->filename
, entry
);
170 search_arch_type
*arch
;
172 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
173 for (arch
= search_arch_head
;
174 arch
!= (search_arch_type
*) NULL
;
177 if (config
.dynamic_link
)
179 /* FIXME: Perhaps we will sometimes want something other
181 if (open_a (arch
->name
, entry
, "lib", ".so") != (bfd
*) NULL
)
184 if (open_a (arch
->name
, entry
, "lib", ".a") != (bfd
*) NULL
)
187 if (open_a (arch
->name
, entry
, ":lib", ".a") != (bfd
*) NULL
)
193 if (entry
->the_bfd
== NULL
)
194 einfo("%F%P: cannot open %s: %E\n", entry
->local_sym_name
);
197 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
200 try_open(name
, exten
)
207 result
= fopen(name
, "r");
208 if (trace_file_tries
== true) {
209 if (result
== (FILE *)NULL
) {
210 info_msg ("cannot find ");
212 info_msg ("%s\n",name
);
214 if (result
!= (FILE *)NULL
) {
219 sprintf(buff
, "%s%s", name
, exten
);
220 result
= fopen(buff
, "r");
221 if (trace_file_tries
== true) {
222 if (result
== (FILE *)NULL
) {
223 info_msg ("cannot find ");
225 info_msg ("%s\n", buff
);
231 /* Try to open NAME; if that fails, look for it in any directories
232 specified with -L, without and with EXTEND apppended. */
235 ldfile_find_command_file(name
, extend
)
239 search_dirs_type
*search
;
243 /* First try raw name */
244 result
= try_open(name
,"");
245 if (result
== (FILE *)NULL
) {
246 /* Try now prefixes */
247 for (search
= search_head
;
248 search
!= (search_dirs_type
*)NULL
;
249 search
= search
->next
) {
250 sprintf(buffer
,"%s/%s", search
->name
, name
);
251 result
= try_open(buffer
, extend
);
259 ldfile_open_command_file(name
)
262 FILE *ldlex_input_stack
;
263 ldlex_input_stack
= ldfile_find_command_file(name
, "");
265 if (ldlex_input_stack
== (FILE *)NULL
) {
266 bfd_set_error (bfd_error_system_call
);
267 einfo("%P%F: cannot open linker script file %s: %E\n",name
);
269 lex_push_file(ldlex_input_stack
, name
);
271 ldfile_input_filename
= name
;
282 gnu960_map_archname( name
)
285 struct tabentry
{ char *cmd_switch
; char *arch
; };
286 static struct tabentry arch_tab
[] = {
290 "KC", "mc", /* Synonym for MC */
293 "SA", "ka", /* Functionally equivalent to KA */
294 "SB", "kb", /* Functionally equivalent to KB */
300 for ( tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++ ){
301 if ( !strcmp(name
,tp
->cmd_switch
) ){
306 if ( tp
->cmd_switch
== NULL
){
307 einfo("%P%F: unknown architecture: %s\n",name
);
315 ldfile_add_arch(name
)
318 search_arch_type
*new =
319 (search_arch_type
*)xmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
323 if (ldfile_output_machine_name
[0] != '\0') {
324 einfo("%P%F: target architecture respecified\n");
327 ldfile_output_machine_name
= name
;
330 new->next
= (search_arch_type
*)NULL
;
331 new->name
= gnu960_map_archname( name
);
332 *search_arch_tail_ptr
= new;
333 search_arch_tail_ptr
= &new->next
;
337 #else /* not GNU960 */
341 ldfile_add_arch (in_name
)
342 CONST
char * in_name
;
344 char *name
= buystring(in_name
);
345 search_arch_type
*new =
346 (search_arch_type
*)xmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
348 ldfile_output_machine_name
= in_name
;
351 new->next
= (search_arch_type
*)NULL
;
353 if (isupper(*name
)) *name
= tolower(*name
);
356 *search_arch_tail_ptr
= new;
357 search_arch_tail_ptr
= &new->next
;
362 /* Set the output architecture */
364 ldfile_set_output_arch (string
)
367 bfd_arch_info_type
*arch
= bfd_scan_arch(string
);
370 ldfile_output_architecture
= arch
->arch
;
371 ldfile_output_machine
= arch
->mach
;
372 ldfile_output_machine_name
= arch
->printable_name
;
375 einfo("%P%F: cannot represent machine `%s'\n", string
);