]>
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 | ||
20 | /* | |
21 | $Id$ | |
2fa0b342 DHW |
22 | */ |
23 | ||
24 | /* | |
25 | ldfile.c | |
26 | ||
27 | look after all the file stuff | |
28 | ||
29 | */ | |
30 | ||
31 | #include "sysdep.h" | |
32 | #include "bfd.h" | |
33 | ||
34 | #include "ldmisc.h" | |
35 | #include "ldlang.h" | |
36 | #include "ldfile.h" | |
37 | ||
2fa0b342 DHW |
38 | /* EXPORT */ |
39 | char *ldfile_input_filename; | |
1418c83b | 40 | CONST char * ldfile_output_machine_name; |
2fa0b342 DHW |
41 | unsigned long ldfile_output_machine; |
42 | enum bfd_architecture ldfile_output_architecture; | |
43 | boolean had_script; | |
44 | ||
45 | /* IMPORT */ | |
46 | ||
47 | extern boolean option_v; | |
48 | ||
49 | ||
50 | ||
51 | ||
52 | ||
53 | /* LOACL */ | |
54 | typedef struct search_dirs_struct | |
55 | { | |
56 | char *name; | |
57 | struct search_dirs_struct *next; | |
58 | } search_dirs_type; | |
59 | ||
60 | static search_dirs_type *search_head; | |
61 | static search_dirs_type **search_tail_ptr = &search_head; | |
62 | ||
63 | typedef struct search_arch_struct | |
64 | { | |
65 | char *name; | |
66 | struct search_arch_struct *next; | |
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 | ||
1418c83b SC |
72 | |
73 | ||
2fa0b342 DHW |
74 | void |
75 | ldfile_add_library_path(name) | |
76 | char *name; | |
77 | { | |
78 | search_dirs_type *new = | |
79 | (search_dirs_type *)ldmalloc(sizeof(search_dirs_type)); | |
80 | new->name = name; | |
81 | new->next = (search_dirs_type*)NULL; | |
82 | *search_tail_ptr = new; | |
83 | search_tail_ptr = &new->next; | |
84 | } | |
85 | ||
86 | ||
87 | static bfd* | |
88 | cached_bfd_openr(attempt,entry) | |
89 | char *attempt; | |
90 | lang_input_statement_type *entry; | |
91 | { | |
92 | entry->the_bfd = bfd_openr(attempt, entry->target); | |
1418c83b SC |
93 | if (option_v == true && entry->the_bfd == (bfd *)NULL) { |
94 | info("attempt to open %s failed\n", attempt); | |
95 | } | |
2fa0b342 DHW |
96 | return entry->the_bfd; |
97 | } | |
98 | ||
99 | static bfd * | |
100 | open_a(arch, entry, lib, suffix) | |
101 | char *arch; | |
102 | lang_input_statement_type *entry; | |
103 | char *lib; | |
104 | char *suffix; | |
105 | { | |
106 | bfd*desc; | |
107 | search_dirs_type *search ; | |
108 | for (search = search_head; | |
109 | search != (search_dirs_type *)NULL; | |
110 | search = search->next) | |
111 | { | |
112 | char buffer[1000]; | |
113 | char *string; | |
114 | if (entry->is_archive == true) { | |
115 | sprintf(buffer, | |
116 | "%s/%s%s%s%s", | |
117 | search->name, | |
118 | lib, | |
119 | entry->filename, arch, suffix); | |
120 | } | |
121 | else { | |
1418c83b SC |
122 | if (entry->filename[0] == '/' || entry->filename[0] == '.') { |
123 | strcpy(buffer, entry->filename); | |
124 | } else { | |
125 | sprintf(buffer,"%s/%s",search->name, entry->filename); | |
126 | } | |
2fa0b342 DHW |
127 | } |
128 | string = buystring(buffer); | |
129 | desc = cached_bfd_openr (string, entry); | |
130 | if (desc) | |
131 | { | |
132 | entry->filename = string; | |
133 | entry->search_dirs_flag = false; | |
134 | entry->the_bfd = desc; | |
135 | return desc; | |
136 | } | |
137 | free(string); | |
138 | } | |
139 | return (bfd *)NULL; | |
140 | } | |
141 | ||
142 | /* Open the input file specified by 'entry', and return a descriptor. | |
143 | The open file is remembered; if the same file is opened twice in a row, | |
144 | a new open is not actually done. */ | |
145 | ||
146 | void | |
147 | ldfile_open_file (entry) | |
148 | lang_input_statement_type *entry; | |
149 | { | |
150 | ||
151 | if (entry->superfile) | |
152 | ldfile_open_file (entry->superfile); | |
153 | ||
154 | if (entry->search_dirs_flag) | |
155 | { | |
156 | search_arch_type *arch; | |
1418c83b SC |
157 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ |
158 | ||
2fa0b342 DHW |
159 | for (arch = search_arch_head; |
160 | arch != (search_arch_type *)NULL; | |
161 | arch = arch->next) { | |
162 | if (open_a(arch->name,entry,"","") != (bfd *)NULL) { | |
163 | return; | |
164 | } | |
165 | if (open_a(arch->name,entry,"lib",".a") != (bfd *)NULL) { | |
166 | return; | |
167 | } | |
168 | ||
169 | } | |
170 | ||
171 | ||
172 | } | |
173 | else { | |
174 | entry->the_bfd = cached_bfd_openr (entry->filename, entry); | |
175 | ||
176 | } | |
177 | if (!entry->the_bfd) info("%F%P: %E %I\n", entry); | |
178 | ||
179 | } | |
180 | ||
181 | ||
182 | ||
183 | ||
184 | ||
185 | ||
186 | static FILE * | |
187 | try_open(name, exten) | |
188 | char *name; | |
189 | char *exten; | |
190 | { | |
191 | FILE *result; | |
192 | char buff[1000]; | |
193 | result = fopen(name, "r"); | |
1418c83b SC |
194 | if (option_v == true) { |
195 | if (result == (FILE *)NULL) { | |
196 | info("can't find "); | |
197 | } | |
2fa0b342 | 198 | info("%s\n",name); |
1418c83b | 199 | |
2fa0b342 DHW |
200 | return result; |
201 | } | |
202 | sprintf(buff, "%s%s", name, exten); | |
203 | result = fopen(buff, "r"); | |
204 | ||
1418c83b SC |
205 | if (option_v == true) { |
206 | if (result == (FILE *)NULL) { | |
207 | info("can't find "); | |
208 | } | |
2fa0b342 DHW |
209 | info("%s\n", buff); |
210 | } | |
211 | return result; | |
212 | } | |
213 | static FILE * | |
214 | find_a_name(name, extend) | |
215 | char *name; | |
216 | char *extend; | |
217 | { | |
218 | search_dirs_type *search; | |
219 | FILE *result; | |
220 | char buffer[1000]; | |
221 | /* First try raw name */ | |
222 | result = try_open(name,""); | |
223 | if (result == (FILE *)NULL) { | |
224 | /* Try now prefixes */ | |
225 | for (search = search_head; | |
226 | search != (search_dirs_type *)NULL; | |
227 | search = search->next) { | |
228 | sprintf(buffer,"%s/%s", search->name, name); | |
229 | result = try_open(buffer, extend); | |
230 | if (result)break; | |
231 | } | |
232 | } | |
233 | return result; | |
234 | } | |
235 | ||
236 | void ldfile_open_command_file(name) | |
237 | char *name; | |
238 | { | |
239 | extern FILE *ldlex_input_stack; | |
240 | ldlex_input_stack = find_a_name(name, ".ld"); | |
241 | ||
242 | if (ldlex_input_stack == (FILE *)NULL) { | |
243 | info("%P%F cannot open load script file %s\n",name); | |
244 | } | |
245 | ldfile_input_filename = name; | |
246 | had_script = true; | |
247 | } | |
248 | ||
249 | ||
250 | ||
251 | ||
252 | void | |
1418c83b SC |
253 | DEFUN(ldfile_add_arch,(in_name), |
254 | CONST char *CONST in_name) | |
2fa0b342 | 255 | { |
1418c83b | 256 | char *name = buystring(in_name); |
2fa0b342 DHW |
257 | search_arch_type *new = |
258 | (search_arch_type *)ldmalloc(sizeof(search_arch_type)); | |
1418c83b SC |
259 | |
260 | ldfile_output_machine_name = in_name; | |
2fa0b342 DHW |
261 | |
262 | new->name = name; | |
263 | new->next = (search_arch_type*)NULL; | |
264 | while (*name) { | |
265 | if (isupper(*name)) *name = tolower(*name); | |
266 | name++; | |
267 | } | |
268 | *search_arch_tail_ptr = new; | |
269 | search_arch_tail_ptr = &new->next; | |
270 | ||
271 | } |