Describe AT option of SECTIONS command, at long last.
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
2fa0b342
DHW
1
2/* Copyright (C) 1991 Free Software Foundation, Inc.
3
4This file is part of GLD, the Gnu Linker.
5
6GLD is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GLD is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GLD; see the file COPYING. If not, write to
18the 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 "ldmain.h"
35#include "ldlex.h"
36
cffdcde9 37#include <ctype.h>
fcf276c4 38
2fa0b342 39char *ldfile_input_filename;
b5b2c886 40const char *ldfile_output_machine_name = "";
2fa0b342
DHW
41unsigned long ldfile_output_machine;
42enum bfd_architecture ldfile_output_architecture;
2fa0b342 43
b5b2c886
SS
44/* start-sanitize-mpw */
45#ifndef MPW
46/* end-sanitize-mpw */
cffdcde9
DM
47#ifdef VMS
48char *slash = "";
49#else
50char *slash = "/";
51#endif
b5b2c886
SS
52/* start-sanitize-mpw */
53#else /* MPW */
54/* The MPW path char is a colon. */
55char *slash = ":";
56#endif /* MPW */
57/* end-sanitize-mpw */
cffdcde9 58
2fa0b342
DHW
59
60
61
cffdcde9
DM
62/* LOCAL */
63typedef struct search_dirs
2fa0b342
DHW
64{
65 char *name;
cffdcde9 66 struct search_dirs *next;
2fa0b342
DHW
67} search_dirs_type;
68
69static search_dirs_type *search_head;
70static search_dirs_type **search_tail_ptr = &search_head;
71
cffdcde9 72typedef struct search_arch
2fa0b342
DHW
73{
74 char *name;
cffdcde9 75 struct search_arch *next;
2fa0b342
DHW
76} search_arch_type;
77
78static search_arch_type *search_arch_head;
79static search_arch_type **search_arch_tail_ptr = &search_arch_head;
80
b5b2c886
SS
81static bfd *cached_bfd_openr PARAMS ((const char *attempt,
82 lang_input_statement_type *entry));
83static bfd *open_a PARAMS ((char *arch, lang_input_statement_type *entry,
84 char *lib, char *suffix));
85static FILE *try_open PARAMS ((char *name, char *exten));
1418c83b 86
2fa0b342
DHW
87void
88ldfile_add_library_path(name)
89char *name;
90{
91 search_dirs_type *new =
cffdcde9 92 (search_dirs_type *)ldmalloc((bfd_size_type)(sizeof(search_dirs_type)));
2fa0b342
DHW
93 new->name = name;
94 new->next = (search_dirs_type*)NULL;
95 *search_tail_ptr = new;
96 search_tail_ptr = &new->next;
97}
98
99
b5b2c886 100static bfd *
2fa0b342 101cached_bfd_openr(attempt,entry)
b5b2c886
SS
102 const char *attempt;
103 lang_input_statement_type *entry;
2fa0b342
DHW
104{
105 entry->the_bfd = bfd_openr(attempt, entry->target);
bbd2521f 106 if (trace_file_tries == true ) {
fcf276c4 107 info_msg ("attempt to open %s %s\n", attempt,
99fe4553 108 (entry->the_bfd == (bfd *)NULL) ? "failed" : "succeeded" );
1418c83b 109 }
2fa0b342
DHW
110 return entry->the_bfd;
111}
112
113static bfd *
114open_a(arch, entry, lib, suffix)
b5b2c886
SS
115 char *arch;
116 lang_input_statement_type *entry;
117 char *lib;
118 char *suffix;
2fa0b342
DHW
119{
120 bfd*desc;
121 search_dirs_type *search ;
122 for (search = search_head;
123 search != (search_dirs_type *)NULL;
124 search = search->next)
125 {
126 char buffer[1000];
127 char *string;
128 if (entry->is_archive == true) {
129 sprintf(buffer,
cffdcde9 130 "%s%s%s%s%s%s",
2fa0b342 131 search->name,
cffdcde9 132 slash,
2fa0b342
DHW
133 lib,
134 entry->filename, arch, suffix);
135 }
136 else {
1418c83b
SC
137 if (entry->filename[0] == '/' || entry->filename[0] == '.') {
138 strcpy(buffer, entry->filename);
139 } else {
cffdcde9 140 sprintf(buffer,"%s%s%s",search->name, slash, entry->filename);
1418c83b 141 }
2fa0b342
DHW
142 }
143 string = buystring(buffer);
144 desc = cached_bfd_openr (string, entry);
145 if (desc)
146 {
147 entry->filename = string;
148 entry->search_dirs_flag = false;
149 entry->the_bfd = desc;
150 return desc;
151 }
152 free(string);
153 }
154 return (bfd *)NULL;
155}
156
157/* Open the input file specified by 'entry', and return a descriptor.
158 The open file is remembered; if the same file is opened twice in a row,
159 a new open is not actually done. */
160
161void
162ldfile_open_file (entry)
163lang_input_statement_type *entry;
164{
165
166 if (entry->superfile)
167 ldfile_open_file (entry->superfile);
168
169 if (entry->search_dirs_flag)
170 {
171 search_arch_type *arch;
1418c83b
SC
172 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
173
2fa0b342
DHW
174 for (arch = search_arch_head;
175 arch != (search_arch_type *)NULL;
176 arch = arch->next) {
cffdcde9 177 if (open_a(arch->name,entry,"lib",".a") != (bfd *)NULL) {
2fa0b342
DHW
178 return;
179 }
cffdcde9
DM
180#ifdef VMS
181 if (open_a(arch->name,entry,":lib",".a") != (bfd *)NULL) {
2fa0b342
DHW
182 return;
183 }
cffdcde9 184#endif
2fa0b342
DHW
185
186 }
187
2fa0b342
DHW
188 }
189 else {
190 entry->the_bfd = cached_bfd_openr (entry->filename, entry);
2fa0b342 191 }
2fa0b342 192
bbd2521f
DM
193 if (!entry->the_bfd)
194 einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
2fa0b342
DHW
195}
196
197
cffdcde9 198/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
2fa0b342
DHW
199
200static FILE *
201try_open(name, exten)
b5b2c886
SS
202 char *name;
203 char *exten;
2fa0b342
DHW
204{
205 FILE *result;
206 char buff[1000];
cffdcde9 207
2fa0b342 208 result = fopen(name, "r");
bbd2521f 209 if (trace_file_tries == true) {
1418c83b 210 if (result == (FILE *)NULL) {
fcf276c4 211 info_msg ("cannot find ");
1418c83b 212 }
fcf276c4 213 info_msg ("%s\n",name);
cffdcde9
DM
214 }
215 if (result != (FILE *)NULL) {
2fa0b342
DHW
216 return result;
217 }
2fa0b342 218
cffdcde9
DM
219 if (*exten) {
220 sprintf(buff, "%s%s", name, exten);
221 result = fopen(buff, "r");
bbd2521f 222 if (trace_file_tries == true) {
cffdcde9 223 if (result == (FILE *)NULL) {
fcf276c4 224 info_msg ("cannot find ");
cffdcde9 225 }
fcf276c4 226 info_msg ("%s\n", buff);
1418c83b 227 }
2fa0b342
DHW
228 }
229 return result;
230}
cffdcde9
DM
231
232/* Try to open NAME; if that fails, look for it in any directories
233 specified with -L, without and with EXTEND apppended. */
234
fcf276c4
ILT
235FILE *
236ldfile_find_command_file(name, extend)
2fa0b342
DHW
237char *name;
238char *extend;
239{
240 search_dirs_type *search;
241 FILE *result;
242 char buffer[1000];
cffdcde9 243
2fa0b342
DHW
244 /* First try raw name */
245 result = try_open(name,"");
246 if (result == (FILE *)NULL) {
247 /* Try now prefixes */
248 for (search = search_head;
249 search != (search_dirs_type *)NULL;
250 search = search->next) {
251 sprintf(buffer,"%s/%s", search->name, name);
252 result = try_open(buffer, extend);
253 if (result)break;
254 }
255 }
256 return result;
257}
258
cffdcde9
DM
259void
260ldfile_open_command_file(name)
2fa0b342
DHW
261char *name;
262{
cffdcde9 263 FILE *ldlex_input_stack;
fcf276c4 264 ldlex_input_stack = ldfile_find_command_file(name, "");
2fa0b342
DHW
265
266 if (ldlex_input_stack == (FILE *)NULL) {
fcf276c4 267 einfo("%P%F: cannot open linker script file %s: %E\n",name);
2fa0b342 268 }
cffdcde9
DM
269 lex_push_file(ldlex_input_stack, name);
270
2fa0b342
DHW
271 ldfile_input_filename = name;
272 had_script = true;
273}
274
275
276
277
99fe4553
SC
278
279#ifdef GNU960
280static
281char *
282gnu960_map_archname( name )
283char *name;
284{
285 struct tabentry { char *cmd_switch; char *arch; };
286 static struct tabentry arch_tab[] = {
287 "", "",
288 "KA", "ka",
289 "KB", "kb",
290 "KC", "mc", /* Synonym for MC */
291 "MC", "mc",
292 "CA", "ca",
293 "SA", "ka", /* Functionally equivalent to KA */
294 "SB", "kb", /* Functionally equivalent to KB */
295 NULL, ""
296 };
297 struct tabentry *tp;
298
299
300 for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
301 if ( !strcmp(name,tp->cmd_switch) ){
302 break;
303 }
304 }
305
306 if ( tp->cmd_switch == NULL ){
cffdcde9 307 einfo("%P%F: unknown architecture: %s\n",name);
99fe4553
SC
308 }
309 return tp->arch;
310}
311
312
313
314void
315ldfile_add_arch(name)
316char *name;
317{
318 search_arch_type *new =
cffdcde9 319 (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type)));
99fe4553
SC
320
321
322 if (*name != '\0') {
323 if (ldfile_output_machine_name[0] != '\0') {
cffdcde9 324 einfo("%P%F: target architecture respecified\n");
99fe4553
SC
325 return;
326 }
327 ldfile_output_machine_name = name;
328 }
329
330 new->next = (search_arch_type*)NULL;
331 new->name = gnu960_map_archname( name );
332 *search_arch_tail_ptr = new;
333 search_arch_tail_ptr = &new->next;
334
335}
336
337#else /* not GNU960 */
338
339
2fa0b342 340void
cffdcde9
DM
341ldfile_add_arch (in_name)
342 CONST char * in_name;
2fa0b342 343{
1418c83b 344 char *name = buystring(in_name);
2fa0b342 345 search_arch_type *new =
cffdcde9 346 (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type)));
1418c83b
SC
347
348 ldfile_output_machine_name = in_name;
2fa0b342
DHW
349
350 new->name = name;
351 new->next = (search_arch_type*)NULL;
352 while (*name) {
353 if (isupper(*name)) *name = tolower(*name);
354 name++;
355 }
356 *search_arch_tail_ptr = new;
357 search_arch_tail_ptr = &new->next;
358
359}
99fe4553 360#endif
a37cc0c0
SC
361
362/* Set the output architecture */
363void
cffdcde9
DM
364ldfile_set_output_arch (string)
365 CONST char *string;
a37cc0c0 366{
cffdcde9
DM
367 bfd_arch_info_type *arch = bfd_scan_arch(string);
368
369 if (arch) {
370 ldfile_output_architecture = arch->arch;
371 ldfile_output_machine = arch->mach;
372 ldfile_output_machine_name = arch->printable_name;
a37cc0c0
SC
373 }
374 else {
fcf276c4 375 einfo("%P%F: cannot represent machine `%s'\n", string);
a37cc0c0
SC
376 }
377}
This page took 0.116848 seconds and 4 git commands to generate.