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