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