2 /* Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 look after all the file stuff
35 char *ldfile_input_filename
;
36 CONST
char * ldfile_output_machine_name
="";
37 unsigned long ldfile_output_machine
;
38 enum bfd_architecture ldfile_output_architecture
;
42 extern boolean had_script
;
43 extern boolean option_v
;
56 typedef struct search_dirs
59 struct search_dirs
*next
;
62 static search_dirs_type
*search_head
;
63 static search_dirs_type
**search_tail_ptr
= &search_head
;
65 typedef struct search_arch
68 struct search_arch
*next
;
71 static search_arch_type
*search_arch_head
;
72 static search_arch_type
**search_arch_tail_ptr
= &search_arch_head
;
77 ldfile_add_library_path(name
)
80 search_dirs_type
*new =
81 (search_dirs_type
*)ldmalloc((bfd_size_type
)(sizeof(search_dirs_type
)));
83 new->next
= (search_dirs_type
*)NULL
;
84 *search_tail_ptr
= new;
85 search_tail_ptr
= &new->next
;
90 cached_bfd_openr(attempt
,entry
)
92 lang_input_statement_type
*entry
;
94 entry
->the_bfd
= bfd_openr(attempt
, entry
->target
);
95 if (option_v
== true ) {
96 info("attempt to open %s %s\n", attempt
,
97 (entry
->the_bfd
== (bfd
*)NULL
) ? "failed" : "succeeded" );
99 return entry
->the_bfd
;
103 open_a(arch
, entry
, lib
, suffix
)
105 lang_input_statement_type
*entry
;
110 search_dirs_type
*search
;
111 for (search
= search_head
;
112 search
!= (search_dirs_type
*)NULL
;
113 search
= search
->next
)
117 if (entry
->is_archive
== true) {
123 entry
->filename
, arch
, suffix
);
126 if (entry
->filename
[0] == '/' || entry
->filename
[0] == '.') {
127 strcpy(buffer
, entry
->filename
);
129 sprintf(buffer
,"%s%s%s",search
->name
, slash
, entry
->filename
);
132 string
= buystring(buffer
);
133 desc
= cached_bfd_openr (string
, entry
);
136 entry
->filename
= string
;
137 entry
->search_dirs_flag
= false;
138 entry
->the_bfd
= desc
;
146 /* Open the input file specified by 'entry', and return a descriptor.
147 The open file is remembered; if the same file is opened twice in a row,
148 a new open is not actually done. */
151 ldfile_open_file (entry
)
152 lang_input_statement_type
*entry
;
155 if (entry
->superfile
)
156 ldfile_open_file (entry
->superfile
);
158 if (entry
->search_dirs_flag
)
160 search_arch_type
*arch
;
161 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
163 for (arch
= search_arch_head
;
164 arch
!= (search_arch_type
*)NULL
;
166 if (open_a(arch
->name
,entry
,"lib",".a") != (bfd
*)NULL
) {
170 if (open_a(arch
->name
,entry
,":lib",".a") != (bfd
*)NULL
) {
180 entry
->the_bfd
= cached_bfd_openr (entry
->filename
, entry
);
183 if (!entry
->the_bfd
) einfo("%F%P: Can't open %s, %E\n", entry
->filename
);
188 /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
191 try_open(name
, exten
)
198 result
= fopen(name
, "r");
199 if (option_v
== true) {
200 if (result
== (FILE *)NULL
) {
205 if (result
!= (FILE *)NULL
) {
210 sprintf(buff
, "%s%s", name
, exten
);
211 result
= fopen(buff
, "r");
212 if (option_v
== true) {
213 if (result
== (FILE *)NULL
) {
222 /* Try to open NAME; if that fails, look for it in any directories
223 specified with -L, without and with EXTEND apppended. */
226 find_a_name(name
, extend
)
230 search_dirs_type
*search
;
234 /* First try raw name */
235 result
= try_open(name
,"");
236 if (result
== (FILE *)NULL
) {
237 /* Try now prefixes */
238 for (search
= search_head
;
239 search
!= (search_dirs_type
*)NULL
;
240 search
= search
->next
) {
241 sprintf(buffer
,"%s/%s", search
->name
, name
);
242 result
= try_open(buffer
, extend
);
250 ldfile_open_command_file(name
)
253 FILE *ldlex_input_stack
;
254 ldlex_input_stack
= find_a_name(name
, "");
256 if (ldlex_input_stack
== (FILE *)NULL
) {
257 einfo("%P%F cannot open load script file %s, %E\n",name
);
259 lex_push_file(ldlex_input_stack
, name
);
261 ldfile_input_filename
= name
;
272 gnu960_map_archname( name
)
275 struct tabentry
{ char *cmd_switch
; char *arch
; };
276 static struct tabentry arch_tab
[] = {
280 "KC", "mc", /* Synonym for MC */
283 "SA", "ka", /* Functionally equivalent to KA */
284 "SB", "kb", /* Functionally equivalent to KB */
290 for ( tp
= arch_tab
; tp
->cmd_switch
!= NULL
; tp
++ ){
291 if ( !strcmp(name
,tp
->cmd_switch
) ){
296 if ( tp
->cmd_switch
== NULL
){
297 einfo("%P%F: unknown architecture: %s\n",name
);
305 ldfile_add_arch(name
)
308 search_arch_type
*new =
309 (search_arch_type
*)ldmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
313 if (ldfile_output_machine_name
[0] != '\0') {
314 einfo("%P%F: target architecture respecified\n");
317 ldfile_output_machine_name
= name
;
320 new->next
= (search_arch_type
*)NULL
;
321 new->name
= gnu960_map_archname( name
);
322 *search_arch_tail_ptr
= new;
323 search_arch_tail_ptr
= &new->next
;
327 #else /* not GNU960 */
331 ldfile_add_arch (in_name
)
332 CONST
char * in_name
;
334 char *name
= buystring(in_name
);
335 search_arch_type
*new =
336 (search_arch_type
*)ldmalloc((bfd_size_type
)(sizeof(search_arch_type
)));
338 ldfile_output_machine_name
= in_name
;
341 new->next
= (search_arch_type
*)NULL
;
343 if (isupper(*name
)) *name
= tolower(*name
);
346 *search_arch_tail_ptr
= new;
347 search_arch_tail_ptr
= &new->next
;
352 /* Set the output architecture */
354 ldfile_set_output_arch (string
)
357 bfd_arch_info_type
*arch
= bfd_scan_arch(string
);
360 ldfile_output_architecture
= arch
->arch
;
361 ldfile_output_machine
= arch
->mach
;
362 ldfile_output_machine_name
= arch
->printable_name
;
365 einfo("%P%F: Can't represent machine `%s'\n", string
);