]>
Commit | Line | Data |
---|---|---|
2fa0b342 DHW |
1 | |
2 | /* Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GLD, the Gnu Linker. | |
5 | ||
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) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. */ | |
19 | ||
2fa0b342 DHW |
20 | /* |
21 | ldfile.c | |
22 | ||
23 | look after all the file stuff | |
24 | ||
25 | */ | |
26 | ||
2fa0b342 | 27 | #include "bfd.h" |
cffdcde9 | 28 | #include "sysdep.h" |
2fa0b342 DHW |
29 | |
30 | #include "ldmisc.h" | |
31 | #include "ldlang.h" | |
32 | #include "ldfile.h" | |
cffdcde9 | 33 | #include <ctype.h> |
2fa0b342 DHW |
34 | /* EXPORT */ |
35 | char *ldfile_input_filename; | |
070aa819 | 36 | CONST char * ldfile_output_machine_name =""; |
2fa0b342 DHW |
37 | unsigned long ldfile_output_machine; |
38 | enum bfd_architecture ldfile_output_architecture; | |
2fa0b342 DHW |
39 | |
40 | /* IMPORT */ | |
41 | ||
cffdcde9 | 42 | extern boolean had_script; |
bbd2521f | 43 | extern boolean trace_file_tries; |
2fa0b342 DHW |
44 | |
45 | ||
cffdcde9 DM |
46 | #ifdef VMS |
47 | char *slash = ""; | |
48 | #else | |
49 | char *slash = "/"; | |
50 | #endif | |
51 | ||
2fa0b342 DHW |
52 | |
53 | ||
54 | ||
cffdcde9 DM |
55 | /* LOCAL */ |
56 | typedef struct search_dirs | |
2fa0b342 DHW |
57 | { |
58 | char *name; | |
cffdcde9 | 59 | struct search_dirs *next; |
2fa0b342 DHW |
60 | } search_dirs_type; |
61 | ||
62 | static search_dirs_type *search_head; | |
63 | static search_dirs_type **search_tail_ptr = &search_head; | |
64 | ||
cffdcde9 | 65 | typedef struct search_arch |
2fa0b342 DHW |
66 | { |
67 | char *name; | |
cffdcde9 | 68 | struct search_arch *next; |
2fa0b342 DHW |
69 | } search_arch_type; |
70 | ||
71 | static search_arch_type *search_arch_head; | |
72 | static search_arch_type **search_arch_tail_ptr = &search_arch_head; | |
73 | ||
1418c83b SC |
74 | |
75 | ||
2fa0b342 DHW |
76 | void |
77 | ldfile_add_library_path(name) | |
78 | char *name; | |
79 | { | |
80 | search_dirs_type *new = | |
cffdcde9 | 81 | (search_dirs_type *)ldmalloc((bfd_size_type)(sizeof(search_dirs_type))); |
2fa0b342 DHW |
82 | new->name = name; |
83 | new->next = (search_dirs_type*)NULL; | |
84 | *search_tail_ptr = new; | |
85 | search_tail_ptr = &new->next; | |
86 | } | |
87 | ||
88 | ||
89 | static bfd* | |
90 | cached_bfd_openr(attempt,entry) | |
91 | char *attempt; | |
92 | lang_input_statement_type *entry; | |
93 | { | |
94 | entry->the_bfd = bfd_openr(attempt, entry->target); | |
bbd2521f | 95 | if (trace_file_tries == true ) { |
99fe4553 SC |
96 | info("attempt to open %s %s\n", attempt, |
97 | (entry->the_bfd == (bfd *)NULL) ? "failed" : "succeeded" ); | |
1418c83b | 98 | } |
2fa0b342 DHW |
99 | return entry->the_bfd; |
100 | } | |
101 | ||
102 | static bfd * | |
103 | open_a(arch, entry, lib, suffix) | |
104 | char *arch; | |
105 | lang_input_statement_type *entry; | |
106 | char *lib; | |
107 | char *suffix; | |
108 | { | |
109 | bfd*desc; | |
110 | search_dirs_type *search ; | |
111 | for (search = search_head; | |
112 | search != (search_dirs_type *)NULL; | |
113 | search = search->next) | |
114 | { | |
115 | char buffer[1000]; | |
116 | char *string; | |
117 | if (entry->is_archive == true) { | |
118 | sprintf(buffer, | |
cffdcde9 | 119 | "%s%s%s%s%s%s", |
2fa0b342 | 120 | search->name, |
cffdcde9 | 121 | slash, |
2fa0b342 DHW |
122 | lib, |
123 | entry->filename, arch, suffix); | |
124 | } | |
125 | else { | |
1418c83b SC |
126 | if (entry->filename[0] == '/' || entry->filename[0] == '.') { |
127 | strcpy(buffer, entry->filename); | |
128 | } else { | |
cffdcde9 | 129 | sprintf(buffer,"%s%s%s",search->name, slash, entry->filename); |
1418c83b | 130 | } |
2fa0b342 DHW |
131 | } |
132 | string = buystring(buffer); | |
133 | desc = cached_bfd_openr (string, entry); | |
134 | if (desc) | |
135 | { | |
136 | entry->filename = string; | |
137 | entry->search_dirs_flag = false; | |
138 | entry->the_bfd = desc; | |
139 | return desc; | |
140 | } | |
141 | free(string); | |
142 | } | |
143 | return (bfd *)NULL; | |
144 | } | |
145 | ||
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. */ | |
149 | ||
150 | void | |
151 | ldfile_open_file (entry) | |
152 | lang_input_statement_type *entry; | |
153 | { | |
154 | ||
155 | if (entry->superfile) | |
156 | ldfile_open_file (entry->superfile); | |
157 | ||
158 | if (entry->search_dirs_flag) | |
159 | { | |
160 | search_arch_type *arch; | |
1418c83b SC |
161 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ |
162 | ||
2fa0b342 DHW |
163 | for (arch = search_arch_head; |
164 | arch != (search_arch_type *)NULL; | |
165 | arch = arch->next) { | |
cffdcde9 | 166 | if (open_a(arch->name,entry,"lib",".a") != (bfd *)NULL) { |
2fa0b342 DHW |
167 | return; |
168 | } | |
cffdcde9 DM |
169 | #ifdef VMS |
170 | if (open_a(arch->name,entry,":lib",".a") != (bfd *)NULL) { | |
2fa0b342 DHW |
171 | return; |
172 | } | |
cffdcde9 | 173 | #endif |
2fa0b342 DHW |
174 | |
175 | } | |
176 | ||
2fa0b342 DHW |
177 | } |
178 | else { | |
179 | entry->the_bfd = cached_bfd_openr (entry->filename, entry); | |
2fa0b342 | 180 | } |
2fa0b342 | 181 | |
bbd2521f DM |
182 | if (!entry->the_bfd) |
183 | einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name); | |
2fa0b342 DHW |
184 | } |
185 | ||
186 | ||
cffdcde9 | 187 | /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */ |
2fa0b342 DHW |
188 | |
189 | static FILE * | |
190 | try_open(name, exten) | |
191 | char *name; | |
192 | char *exten; | |
193 | { | |
194 | FILE *result; | |
195 | char buff[1000]; | |
cffdcde9 | 196 | |
2fa0b342 | 197 | result = fopen(name, "r"); |
bbd2521f | 198 | if (trace_file_tries == true) { |
1418c83b SC |
199 | if (result == (FILE *)NULL) { |
200 | info("can't find "); | |
201 | } | |
2fa0b342 | 202 | info("%s\n",name); |
cffdcde9 DM |
203 | } |
204 | if (result != (FILE *)NULL) { | |
2fa0b342 DHW |
205 | return result; |
206 | } | |
2fa0b342 | 207 | |
cffdcde9 DM |
208 | if (*exten) { |
209 | sprintf(buff, "%s%s", name, exten); | |
210 | result = fopen(buff, "r"); | |
bbd2521f | 211 | if (trace_file_tries == true) { |
cffdcde9 DM |
212 | if (result == (FILE *)NULL) { |
213 | info("can't find "); | |
214 | } | |
215 | info("%s\n", buff); | |
1418c83b | 216 | } |
2fa0b342 DHW |
217 | } |
218 | return result; | |
219 | } | |
cffdcde9 DM |
220 | |
221 | /* Try to open NAME; if that fails, look for it in any directories | |
222 | specified with -L, without and with EXTEND apppended. */ | |
223 | ||
2fa0b342 DHW |
224 | static FILE * |
225 | find_a_name(name, extend) | |
226 | char *name; | |
227 | char *extend; | |
228 | { | |
229 | search_dirs_type *search; | |
230 | FILE *result; | |
231 | char buffer[1000]; | |
cffdcde9 | 232 | |
2fa0b342 DHW |
233 | /* First try raw name */ |
234 | result = try_open(name,""); | |
235 | if (result == (FILE *)NULL) { | |
236 | /* Try now prefixes */ | |
237 | for (search = search_head; | |
238 | search != (search_dirs_type *)NULL; | |
239 | search = search->next) { | |
240 | sprintf(buffer,"%s/%s", search->name, name); | |
241 | result = try_open(buffer, extend); | |
242 | if (result)break; | |
243 | } | |
244 | } | |
245 | return result; | |
246 | } | |
247 | ||
cffdcde9 DM |
248 | void |
249 | ldfile_open_command_file(name) | |
2fa0b342 DHW |
250 | char *name; |
251 | { | |
cffdcde9 DM |
252 | FILE *ldlex_input_stack; |
253 | ldlex_input_stack = find_a_name(name, ""); | |
2fa0b342 DHW |
254 | |
255 | if (ldlex_input_stack == (FILE *)NULL) { | |
bbd2521f | 256 | einfo("%P%F: cannot open load script file %s: %E\n",name); |
2fa0b342 | 257 | } |
cffdcde9 DM |
258 | lex_push_file(ldlex_input_stack, name); |
259 | ||
2fa0b342 DHW |
260 | ldfile_input_filename = name; |
261 | had_script = true; | |
262 | } | |
263 | ||
264 | ||
265 | ||
266 | ||
99fe4553 SC |
267 | |
268 | #ifdef GNU960 | |
269 | static | |
270 | char * | |
271 | gnu960_map_archname( name ) | |
272 | char *name; | |
273 | { | |
274 | struct tabentry { char *cmd_switch; char *arch; }; | |
275 | static struct tabentry arch_tab[] = { | |
276 | "", "", | |
277 | "KA", "ka", | |
278 | "KB", "kb", | |
279 | "KC", "mc", /* Synonym for MC */ | |
280 | "MC", "mc", | |
281 | "CA", "ca", | |
282 | "SA", "ka", /* Functionally equivalent to KA */ | |
283 | "SB", "kb", /* Functionally equivalent to KB */ | |
284 | NULL, "" | |
285 | }; | |
286 | struct tabentry *tp; | |
287 | ||
288 | ||
289 | for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){ | |
290 | if ( !strcmp(name,tp->cmd_switch) ){ | |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
295 | if ( tp->cmd_switch == NULL ){ | |
cffdcde9 | 296 | einfo("%P%F: unknown architecture: %s\n",name); |
99fe4553 SC |
297 | } |
298 | return tp->arch; | |
299 | } | |
300 | ||
301 | ||
302 | ||
303 | void | |
304 | ldfile_add_arch(name) | |
305 | char *name; | |
306 | { | |
307 | search_arch_type *new = | |
cffdcde9 | 308 | (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type))); |
99fe4553 SC |
309 | |
310 | ||
311 | if (*name != '\0') { | |
312 | if (ldfile_output_machine_name[0] != '\0') { | |
cffdcde9 | 313 | einfo("%P%F: target architecture respecified\n"); |
99fe4553 SC |
314 | return; |
315 | } | |
316 | ldfile_output_machine_name = name; | |
317 | } | |
318 | ||
319 | new->next = (search_arch_type*)NULL; | |
320 | new->name = gnu960_map_archname( name ); | |
321 | *search_arch_tail_ptr = new; | |
322 | search_arch_tail_ptr = &new->next; | |
323 | ||
324 | } | |
325 | ||
326 | #else /* not GNU960 */ | |
327 | ||
328 | ||
2fa0b342 | 329 | void |
cffdcde9 DM |
330 | ldfile_add_arch (in_name) |
331 | CONST char * in_name; | |
2fa0b342 | 332 | { |
1418c83b | 333 | char *name = buystring(in_name); |
2fa0b342 | 334 | search_arch_type *new = |
cffdcde9 | 335 | (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type))); |
1418c83b SC |
336 | |
337 | ldfile_output_machine_name = in_name; | |
2fa0b342 DHW |
338 | |
339 | new->name = name; | |
340 | new->next = (search_arch_type*)NULL; | |
341 | while (*name) { | |
342 | if (isupper(*name)) *name = tolower(*name); | |
343 | name++; | |
344 | } | |
345 | *search_arch_tail_ptr = new; | |
346 | search_arch_tail_ptr = &new->next; | |
347 | ||
348 | } | |
99fe4553 | 349 | #endif |
a37cc0c0 SC |
350 | |
351 | /* Set the output architecture */ | |
352 | void | |
cffdcde9 DM |
353 | ldfile_set_output_arch (string) |
354 | CONST char *string; | |
a37cc0c0 | 355 | { |
cffdcde9 DM |
356 | bfd_arch_info_type *arch = bfd_scan_arch(string); |
357 | ||
358 | if (arch) { | |
359 | ldfile_output_architecture = arch->arch; | |
360 | ldfile_output_machine = arch->mach; | |
361 | ldfile_output_machine_name = arch->printable_name; | |
a37cc0c0 SC |
362 | } |
363 | else { | |
cffdcde9 | 364 | einfo("%P%F: Can't represent machine `%s'\n", string); |
a37cc0c0 SC |
365 | } |
366 | } |