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