]>
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 ; | |
117 | for (search = search_head; | |
118 | search != (search_dirs_type *)NULL; | |
119 | search = search->next) | |
120 | { | |
121 | char buffer[1000]; | |
122 | char *string; | |
123 | if (entry->is_archive == true) { | |
124 | sprintf(buffer, | |
cffdcde9 | 125 | "%s%s%s%s%s%s", |
2fa0b342 | 126 | search->name, |
cffdcde9 | 127 | slash, |
2fa0b342 DHW |
128 | lib, |
129 | entry->filename, arch, suffix); | |
130 | } | |
131 | else { | |
1418c83b SC |
132 | if (entry->filename[0] == '/' || entry->filename[0] == '.') { |
133 | strcpy(buffer, entry->filename); | |
134 | } else { | |
cffdcde9 | 135 | sprintf(buffer,"%s%s%s",search->name, slash, entry->filename); |
1418c83b | 136 | } |
2fa0b342 DHW |
137 | } |
138 | string = buystring(buffer); | |
139 | desc = cached_bfd_openr (string, entry); | |
140 | if (desc) | |
141 | { | |
142 | entry->filename = string; | |
2fa0b342 DHW |
143 | entry->the_bfd = desc; |
144 | return desc; | |
145 | } | |
146 | free(string); | |
147 | } | |
148 | return (bfd *)NULL; | |
149 | } | |
150 | ||
151 | /* Open the input file specified by 'entry', and return a descriptor. | |
152 | The open file is remembered; if the same file is opened twice in a row, | |
153 | a new open is not actually done. */ | |
154 | ||
155 | void | |
156 | ldfile_open_file (entry) | |
2a9fa50c | 157 | lang_input_statement_type *entry; |
2fa0b342 | 158 | { |
210c52ac | 159 | ASSERT (entry->the_bfd == NULL); |
2fa0b342 | 160 | |
2a9fa50c ILT |
161 | if (! entry->search_dirs_flag) |
162 | entry->the_bfd = cached_bfd_openr (entry->filename, entry); | |
163 | else | |
2fa0b342 DHW |
164 | { |
165 | search_arch_type *arch; | |
2a9fa50c | 166 | |
1418c83b | 167 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ |
2fa0b342 | 168 | for (arch = search_arch_head; |
2a9fa50c ILT |
169 | arch != (search_arch_type *) NULL; |
170 | arch = arch->next) | |
171 | { | |
172 | if (config.dynamic_link) | |
173 | { | |
174 | /* FIXME: Perhaps we will sometimes want something other | |
175 | than .so. */ | |
176 | if (open_a (arch->name, entry, "lib", ".so") != (bfd *) NULL) | |
177 | return; | |
178 | } | |
179 | if (open_a (arch->name, entry, "lib", ".a") != (bfd *) NULL) | |
180 | return; | |
cffdcde9 | 181 | #ifdef VMS |
2a9fa50c ILT |
182 | if (open_a (arch->name, entry, ":lib", ".a") != (bfd *) NULL) |
183 | return; | |
cffdcde9 | 184 | #endif |
2a9fa50c | 185 | } |
2fa0b342 | 186 | } |
2fa0b342 | 187 | |
2a9fa50c | 188 | if (entry->the_bfd == NULL) |
bbd2521f | 189 | einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name); |
2fa0b342 DHW |
190 | } |
191 | ||
cffdcde9 | 192 | /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */ |
2fa0b342 DHW |
193 | |
194 | static FILE * | |
195 | try_open(name, exten) | |
b5b2c886 SS |
196 | char *name; |
197 | char *exten; | |
2fa0b342 DHW |
198 | { |
199 | FILE *result; | |
200 | char buff[1000]; | |
cffdcde9 | 201 | |
2fa0b342 | 202 | result = fopen(name, "r"); |
bbd2521f | 203 | if (trace_file_tries == true) { |
1418c83b | 204 | if (result == (FILE *)NULL) { |
fcf276c4 | 205 | info_msg ("cannot find "); |
1418c83b | 206 | } |
fcf276c4 | 207 | info_msg ("%s\n",name); |
cffdcde9 DM |
208 | } |
209 | if (result != (FILE *)NULL) { | |
2fa0b342 DHW |
210 | return result; |
211 | } | |
2fa0b342 | 212 | |
cffdcde9 DM |
213 | if (*exten) { |
214 | sprintf(buff, "%s%s", name, exten); | |
215 | result = fopen(buff, "r"); | |
bbd2521f | 216 | if (trace_file_tries == true) { |
cffdcde9 | 217 | if (result == (FILE *)NULL) { |
fcf276c4 | 218 | info_msg ("cannot find "); |
cffdcde9 | 219 | } |
fcf276c4 | 220 | info_msg ("%s\n", buff); |
1418c83b | 221 | } |
2fa0b342 DHW |
222 | } |
223 | return result; | |
224 | } | |
cffdcde9 DM |
225 | |
226 | /* Try to open NAME; if that fails, look for it in any directories | |
227 | specified with -L, without and with EXTEND apppended. */ | |
228 | ||
fcf276c4 ILT |
229 | FILE * |
230 | ldfile_find_command_file(name, extend) | |
2fa0b342 DHW |
231 | char *name; |
232 | char *extend; | |
233 | { | |
234 | search_dirs_type *search; | |
235 | FILE *result; | |
236 | char buffer[1000]; | |
cffdcde9 | 237 | |
2fa0b342 DHW |
238 | /* First try raw name */ |
239 | result = try_open(name,""); | |
240 | if (result == (FILE *)NULL) { | |
241 | /* Try now prefixes */ | |
242 | for (search = search_head; | |
243 | search != (search_dirs_type *)NULL; | |
244 | search = search->next) { | |
245 | sprintf(buffer,"%s/%s", search->name, name); | |
246 | result = try_open(buffer, extend); | |
247 | if (result)break; | |
248 | } | |
249 | } | |
250 | return result; | |
251 | } | |
252 | ||
cffdcde9 DM |
253 | void |
254 | ldfile_open_command_file(name) | |
2fa0b342 DHW |
255 | char *name; |
256 | { | |
cffdcde9 | 257 | FILE *ldlex_input_stack; |
fcf276c4 | 258 | ldlex_input_stack = ldfile_find_command_file(name, ""); |
2fa0b342 DHW |
259 | |
260 | if (ldlex_input_stack == (FILE *)NULL) { | |
2a9fa50c | 261 | bfd_set_error (bfd_error_system_call); |
fcf276c4 | 262 | einfo("%P%F: cannot open linker script file %s: %E\n",name); |
2fa0b342 | 263 | } |
cffdcde9 DM |
264 | lex_push_file(ldlex_input_stack, name); |
265 | ||
2fa0b342 DHW |
266 | ldfile_input_filename = name; |
267 | had_script = true; | |
268 | } | |
269 | ||
270 | ||
271 | ||
272 | ||
99fe4553 SC |
273 | |
274 | #ifdef GNU960 | |
275 | static | |
276 | char * | |
277 | gnu960_map_archname( name ) | |
278 | char *name; | |
279 | { | |
280 | struct tabentry { char *cmd_switch; char *arch; }; | |
281 | static struct tabentry arch_tab[] = { | |
282 | "", "", | |
283 | "KA", "ka", | |
284 | "KB", "kb", | |
285 | "KC", "mc", /* Synonym for MC */ | |
286 | "MC", "mc", | |
287 | "CA", "ca", | |
288 | "SA", "ka", /* Functionally equivalent to KA */ | |
289 | "SB", "kb", /* Functionally equivalent to KB */ | |
290 | NULL, "" | |
291 | }; | |
292 | struct tabentry *tp; | |
293 | ||
294 | ||
295 | for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){ | |
296 | if ( !strcmp(name,tp->cmd_switch) ){ | |
297 | break; | |
298 | } | |
299 | } | |
300 | ||
301 | if ( tp->cmd_switch == NULL ){ | |
cffdcde9 | 302 | einfo("%P%F: unknown architecture: %s\n",name); |
99fe4553 SC |
303 | } |
304 | return tp->arch; | |
305 | } | |
306 | ||
307 | ||
308 | ||
309 | void | |
310 | ldfile_add_arch(name) | |
311 | char *name; | |
312 | { | |
313 | search_arch_type *new = | |
2a9fa50c | 314 | (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type))); |
99fe4553 SC |
315 | |
316 | ||
317 | if (*name != '\0') { | |
318 | if (ldfile_output_machine_name[0] != '\0') { | |
cffdcde9 | 319 | einfo("%P%F: target architecture respecified\n"); |
99fe4553 SC |
320 | return; |
321 | } | |
322 | ldfile_output_machine_name = name; | |
323 | } | |
324 | ||
325 | new->next = (search_arch_type*)NULL; | |
326 | new->name = gnu960_map_archname( name ); | |
327 | *search_arch_tail_ptr = new; | |
328 | search_arch_tail_ptr = &new->next; | |
329 | ||
330 | } | |
331 | ||
332 | #else /* not GNU960 */ | |
333 | ||
334 | ||
2fa0b342 | 335 | void |
cffdcde9 DM |
336 | ldfile_add_arch (in_name) |
337 | CONST char * in_name; | |
2fa0b342 | 338 | { |
1418c83b | 339 | char *name = buystring(in_name); |
2fa0b342 | 340 | search_arch_type *new = |
2a9fa50c | 341 | (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type))); |
1418c83b SC |
342 | |
343 | ldfile_output_machine_name = in_name; | |
2fa0b342 DHW |
344 | |
345 | new->name = name; | |
346 | new->next = (search_arch_type*)NULL; | |
347 | while (*name) { | |
348 | if (isupper(*name)) *name = tolower(*name); | |
349 | name++; | |
350 | } | |
351 | *search_arch_tail_ptr = new; | |
352 | search_arch_tail_ptr = &new->next; | |
353 | ||
354 | } | |
99fe4553 | 355 | #endif |
a37cc0c0 SC |
356 | |
357 | /* Set the output architecture */ | |
358 | void | |
cffdcde9 DM |
359 | ldfile_set_output_arch (string) |
360 | CONST char *string; | |
a37cc0c0 | 361 | { |
cffdcde9 DM |
362 | bfd_arch_info_type *arch = bfd_scan_arch(string); |
363 | ||
364 | if (arch) { | |
365 | ldfile_output_architecture = arch->arch; | |
366 | ldfile_output_machine = arch->mach; | |
367 | ldfile_output_machine_name = arch->printable_name; | |
a37cc0c0 SC |
368 | } |
369 | else { | |
fcf276c4 | 370 | einfo("%P%F: cannot represent machine `%s'\n", string); |
a37cc0c0 SC |
371 | } |
372 | } |