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