Subversion Repositories Kolibri OS

Rev

Rev 5211 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5211 Rev 6324
1
/* Linker file opening and searching.
1
/* Linker file opening and searching.
2
   Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
-
 
3
   2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
-
 
4
   Free Software Foundation, Inc.
2
   Copyright (C) 1991-2015 Free Software Foundation, Inc.
5
 
3
 
6
   This file is part of the GNU Binutils.
4
   This file is part of the GNU Binutils.
7
 
5
 
8
   This program is free software; you can redistribute it and/or modify
6
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
7
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
8
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
9
   (at your option) any later version.
12
 
10
 
13
   This program is distributed in the hope that it will be useful,
11
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
14
   GNU General Public License for more details.
17
 
15
 
18
   You should have received a copy of the GNU General Public License
16
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
17
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
19
   MA 02110-1301, USA.  */
22
 
20
 
23
#include "sysdep.h"
21
#include "sysdep.h"
24
#include "bfd.h"
22
#include "bfd.h"
25
#include "bfdlink.h"
23
#include "bfdlink.h"
26
#include "safe-ctype.h"
24
#include "safe-ctype.h"
27
#include "ld.h"
25
#include "ld.h"
28
#include "ldmisc.h"
26
#include "ldmisc.h"
29
#include "ldexp.h"
27
#include "ldexp.h"
30
#include "ldlang.h"
28
#include "ldlang.h"
31
#include "ldfile.h"
29
#include "ldfile.h"
32
#include "ldmain.h"
30
#include "ldmain.h"
33
#include 
31
#include 
34
#include "ldlex.h"
32
#include "ldlex.h"
35
#include "ldemul.h"
33
#include "ldemul.h"
36
#include "libiberty.h"
34
#include "libiberty.h"
37
#include "filenames.h"
35
#include "filenames.h"
38
#ifdef ENABLE_PLUGINS
36
#ifdef ENABLE_PLUGINS
39
#include "plugin-api.h"
37
#include "plugin-api.h"
40
#include "plugin.h"
38
#include "plugin.h"
41
#endif /* ENABLE_PLUGINS */
39
#endif /* ENABLE_PLUGINS */
42
 
40
 
43
bfd_boolean  ldfile_assumed_script = FALSE;
41
bfd_boolean  ldfile_assumed_script = FALSE;
44
const char * ldfile_output_machine_name = "";
42
const char * ldfile_output_machine_name = "";
45
unsigned long ldfile_output_machine;
43
unsigned long ldfile_output_machine;
46
enum bfd_architecture ldfile_output_architecture;
44
enum bfd_architecture ldfile_output_architecture;
47
search_dirs_type * search_head;
45
search_dirs_type * search_head;
48
 
46
 
49
#ifdef VMS
47
#ifdef VMS
50
static char * slash = "";
48
static char * slash = "";
51
#else
49
#else
52
#if defined (_WIN32) && ! defined (__CYGWIN32__)
50
#if defined (_WIN32) && ! defined (__CYGWIN32__)
53
static char * slash = "\\";
51
static char * slash = "\\";
54
#else
52
#else
55
static char * slash = "/";
53
static char * slash = "/";
56
#endif
54
#endif
57
#endif
55
#endif
58
 
56
 
59
typedef struct search_arch
57
typedef struct search_arch
60
{
58
{
61
  char *name;
59
  char *name;
62
  struct search_arch *next;
60
  struct search_arch *next;
63
} search_arch_type;
61
} search_arch_type;
64
 
62
 
65
static search_dirs_type **search_tail_ptr = &search_head;
63
static search_dirs_type **search_tail_ptr = &search_head;
66
static search_arch_type *search_arch_head;
64
static search_arch_type *search_arch_head;
67
static search_arch_type **search_arch_tail_ptr = &search_arch_head;
65
static search_arch_type **search_arch_tail_ptr = &search_arch_head;
68
 
66
 
69
/* Test whether a pathname, after canonicalization, is the same or a
67
/* Test whether a pathname, after canonicalization, is the same or a
70
   sub-directory of the sysroot directory.  */
68
   sub-directory of the sysroot directory.  */
71
 
69
 
72
static bfd_boolean
70
static bfd_boolean
73
is_sysrooted_pathname (const char *name)
71
is_sysrooted_pathname (const char *name)
74
{
72
{
75
  char *realname;
73
  char *realname;
76
  int len;
74
  int len;
77
  bfd_boolean result;
75
  bfd_boolean result;
78
 
76
 
79
  if (ld_canon_sysroot == NULL)
77
  if (ld_canon_sysroot == NULL)
80
    return FALSE;
78
    return FALSE;
81
 
79
 
82
  realname = lrealpath (name);
80
  realname = lrealpath (name);
83
  len = strlen (realname);
81
  len = strlen (realname);
84
  result = FALSE;
82
  result = FALSE;
85
  if (len > ld_canon_sysroot_len
83
  if (len > ld_canon_sysroot_len
86
      && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
84
      && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
87
    {
85
    {
88
      realname[ld_canon_sysroot_len] = '\0';
86
      realname[ld_canon_sysroot_len] = '\0';
89
      result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
87
      result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
90
    }
88
    }
91
 
89
 
92
  free (realname);
90
  free (realname);
93
  return result;
91
  return result;
94
}
92
}
95
 
93
 
96
/* Adds NAME to the library search path.
94
/* Adds NAME to the library search path.
97
   Makes a copy of NAME using xmalloc().  */
95
   Makes a copy of NAME using xmalloc().  */
98
 
96
 
99
void
97
void
100
ldfile_add_library_path (const char *name, bfd_boolean cmdline)
98
ldfile_add_library_path (const char *name, bfd_boolean cmdline)
101
{
99
{
102
  search_dirs_type *new_dirs;
100
  search_dirs_type *new_dirs;
103
 
101
 
104
  if (!cmdline && config.only_cmd_line_lib_dirs)
102
  if (!cmdline && config.only_cmd_line_lib_dirs)
105
    return;
103
    return;
106
 
104
 
107
  new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
105
  new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
108
  new_dirs->next = NULL;
106
  new_dirs->next = NULL;
109
  new_dirs->cmdline = cmdline;
107
  new_dirs->cmdline = cmdline;
110
  *search_tail_ptr = new_dirs;
108
  *search_tail_ptr = new_dirs;
111
  search_tail_ptr = &new_dirs->next;
109
  search_tail_ptr = &new_dirs->next;
112
 
110
 
113
  /* If a directory is marked as honoring sysroot, prepend the sysroot path
111
  /* If a directory is marked as honoring sysroot, prepend the sysroot path
114
     now.  */
112
     now.  */
115
  if (name[0] == '=')
113
  if (name[0] == '=')
116
    new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
114
    new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
117
  else
115
  else
118
    new_dirs->name = xstrdup (name);
116
    new_dirs->name = xstrdup (name);
119
}
117
}
120
 
118
 
121
/* Try to open a BFD for a lang_input_statement.  */
119
/* Try to open a BFD for a lang_input_statement.  */
122
 
120
 
123
bfd_boolean
121
bfd_boolean
124
ldfile_try_open_bfd (const char *attempt,
122
ldfile_try_open_bfd (const char *attempt,
125
		     lang_input_statement_type *entry)
123
		     lang_input_statement_type *entry)
126
{
124
{
127
  entry->the_bfd = bfd_openr (attempt, entry->target);
125
  entry->the_bfd = bfd_openr (attempt, entry->target);
128
 
126
 
129
  if (verbose)
127
  if (verbose)
130
    {
128
    {
131
      if (entry->the_bfd == NULL)
129
      if (entry->the_bfd == NULL)
132
	info_msg (_("attempt to open %s failed\n"), attempt);
130
	info_msg (_("attempt to open %s failed\n"), attempt);
133
      else
131
      else
134
	info_msg (_("attempt to open %s succeeded\n"), attempt);
132
	info_msg (_("attempt to open %s succeeded\n"), attempt);
135
    }
133
    }
136
 
134
 
137
  if (entry->the_bfd == NULL)
135
  if (entry->the_bfd == NULL)
138
    {
136
    {
139
      if (bfd_get_error () == bfd_error_invalid_target)
137
      if (bfd_get_error () == bfd_error_invalid_target)
140
	einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
138
	einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
141
      return FALSE;
139
      return FALSE;
142
    }
140
    }
143
 
141
 
144
  /* Linker needs to decompress sections.  */
142
  /* Linker needs to decompress sections.  */
145
  entry->the_bfd->flags |= BFD_DECOMPRESS;
143
  entry->the_bfd->flags |= BFD_DECOMPRESS;
-
 
144
 
-
 
145
  /* This is a linker input BFD.  */
-
 
146
  entry->the_bfd->is_linker_input = 1;
-
 
147
 
-
 
148
#ifdef ENABLE_PLUGINS
-
 
149
  if (entry->flags.lto_output)
-
 
150
    entry->the_bfd->lto_output = 1;
-
 
151
#endif
146
 
152
 
147
  /* If we are searching for this file, see if the architecture is
153
  /* If we are searching for this file, see if the architecture is
148
     compatible with the output file.  If it isn't, keep searching.
154
     compatible with the output file.  If it isn't, keep searching.
149
     If we can't open the file as an object file, stop the search
155
     If we can't open the file as an object file, stop the search
150
     here.  If we are statically linking, ensure that we don't link
156
     here.  If we are statically linking, ensure that we don't link
151
     a dynamic object.
157
     a dynamic object.
152
 
158
 
153
     In the code below, it's OK to exit early if the check fails,
159
     In the code below, it's OK to exit early if the check fails,
154
     closing the checked BFD and returning FALSE, but if the BFD
160
     closing the checked BFD and returning FALSE, but if the BFD
155
     checks out compatible, do not exit early returning TRUE, or
161
     checks out compatible, do not exit early returning TRUE, or
156
     the plugins will not get a chance to claim the file.  */
162
     the plugins will not get a chance to claim the file.  */
157
 
163
 
158
  if (entry->flags.search_dirs || !entry->flags.dynamic)
164
  if (entry->flags.search_dirs || !entry->flags.dynamic)
159
    {
165
    {
160
      bfd *check;
166
      bfd *check;
161
 
167
 
162
      if (bfd_check_format (entry->the_bfd, bfd_archive))
168
      if (bfd_check_format (entry->the_bfd, bfd_archive))
163
	check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
169
	check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
164
      else
170
      else
165
	check = entry->the_bfd;
171
	check = entry->the_bfd;
166
 
172
 
167
      if (check != NULL)
173
      if (check != NULL)
168
	{
174
	{
169
	  if (! bfd_check_format (check, bfd_object))
175
	  if (! bfd_check_format (check, bfd_object))
170
	    {
176
	    {
171
	      if (check == entry->the_bfd
177
	      if (check == entry->the_bfd
172
		  && entry->flags.search_dirs
178
		  && entry->flags.search_dirs
173
		  && bfd_get_error () == bfd_error_file_not_recognized
179
		  && bfd_get_error () == bfd_error_file_not_recognized
174
		  && ! ldemul_unrecognized_file (entry))
180
		  && ! ldemul_unrecognized_file (entry))
175
		{
181
		{
176
		  int token, skip = 0;
182
		  int token, skip = 0;
177
		  char *arg, *arg1, *arg2, *arg3;
183
		  char *arg, *arg1, *arg2, *arg3;
178
		  extern FILE *yyin;
184
		  extern FILE *yyin;
179
 
185
 
180
		  /* Try to interpret the file as a linker script.  */
186
		  /* Try to interpret the file as a linker script.  */
181
		  ldfile_open_command_file (attempt);
187
		  ldfile_open_command_file (attempt);
182
 
188
 
183
		  ldfile_assumed_script = TRUE;
189
		  ldfile_assumed_script = TRUE;
184
		  parser_input = input_selected;
190
		  parser_input = input_selected;
185
		  ldlex_both ();
191
		  ldlex_both ();
186
		  token = INPUT_SCRIPT;
192
		  token = INPUT_SCRIPT;
187
		  while (token != 0)
193
		  while (token != 0)
188
		    {
194
		    {
189
		      switch (token)
195
		      switch (token)
190
			{
196
			{
191
			case OUTPUT_FORMAT:
197
			case OUTPUT_FORMAT:
192
			  if ((token = yylex ()) != '(')
198
			  if ((token = yylex ()) != '(')
193
			    continue;
199
			    continue;
194
			  if ((token = yylex ()) != NAME)
200
			  if ((token = yylex ()) != NAME)
195
			    continue;
201
			    continue;
196
			  arg1 = yylval.name;
202
			  arg1 = yylval.name;
197
			  arg2 = NULL;
203
			  arg2 = NULL;
198
			  arg3 = NULL;
204
			  arg3 = NULL;
199
			  token = yylex ();
205
			  token = yylex ();
200
			  if (token == ',')
206
			  if (token == ',')
201
			    {
207
			    {
202
			      if ((token = yylex ()) != NAME)
208
			      if ((token = yylex ()) != NAME)
203
				{
209
				{
204
				  free (arg1);
210
				  free (arg1);
205
				  continue;
211
				  continue;
206
				}
212
				}
207
			      arg2 = yylval.name;
213
			      arg2 = yylval.name;
208
			      if ((token = yylex ()) != ','
214
			      if ((token = yylex ()) != ','
209
				  || (token = yylex ()) != NAME)
215
				  || (token = yylex ()) != NAME)
210
				{
216
				{
211
				  free (arg1);
217
				  free (arg1);
212
				  free (arg2);
218
				  free (arg2);
213
				  continue;
219
				  continue;
214
				}
220
				}
215
			      arg3 = yylval.name;
221
			      arg3 = yylval.name;
216
			      token = yylex ();
222
			      token = yylex ();
217
			    }
223
			    }
218
			  if (token == ')')
224
			  if (token == ')')
219
			    {
225
			    {
220
			      switch (command_line.endian)
226
			      switch (command_line.endian)
221
				{
227
				{
222
				default:
228
				default:
223
				case ENDIAN_UNSET:
229
				case ENDIAN_UNSET:
224
				  arg = arg1; break;
230
				  arg = arg1; break;
225
				case ENDIAN_BIG:
231
				case ENDIAN_BIG:
226
				  arg = arg2 ? arg2 : arg1; break;
232
				  arg = arg2 ? arg2 : arg1; break;
227
				case ENDIAN_LITTLE:
233
				case ENDIAN_LITTLE:
228
				  arg = arg3 ? arg3 : arg1; break;
234
				  arg = arg3 ? arg3 : arg1; break;
229
				}
235
				}
230
			      if (strcmp (arg, lang_get_output_target ()) != 0)
236
			      if (strcmp (arg, lang_get_output_target ()) != 0)
231
				skip = 1;
237
				skip = 1;
232
			    }
238
			    }
233
			  free (arg1);
239
			  free (arg1);
234
			  if (arg2) free (arg2);
240
			  if (arg2) free (arg2);
235
			  if (arg3) free (arg3);
241
			  if (arg3) free (arg3);
236
			  break;
242
			  break;
237
			case NAME:
243
			case NAME:
238
			case LNAME:
244
			case LNAME:
239
			case VERS_IDENTIFIER:
245
			case VERS_IDENTIFIER:
240
			case VERS_TAG:
246
			case VERS_TAG:
241
			  free (yylval.name);
247
			  free (yylval.name);
242
			  break;
248
			  break;
243
			case INT:
249
			case INT:
244
			  if (yylval.bigint.str)
250
			  if (yylval.bigint.str)
245
			    free (yylval.bigint.str);
251
			    free (yylval.bigint.str);
246
			  break;
252
			  break;
247
			}
253
			}
248
		      token = yylex ();
254
		      token = yylex ();
249
		    }
255
		    }
250
		  ldlex_popstate ();
256
		  ldlex_popstate ();
251
		  ldfile_assumed_script = FALSE;
257
		  ldfile_assumed_script = FALSE;
252
		  fclose (yyin);
258
		  fclose (yyin);
253
		  yyin = NULL;
259
		  yyin = NULL;
254
		  if (skip)
260
		  if (skip)
255
		    {
261
		    {
256
		      if (command_line.warn_search_mismatch)
262
		      if (command_line.warn_search_mismatch)
257
			einfo (_("%P: skipping incompatible %s "
263
			einfo (_("%P: skipping incompatible %s "
258
				 "when searching for %s\n"),
264
				 "when searching for %s\n"),
259
			       attempt, entry->local_sym_name);
265
			       attempt, entry->local_sym_name);
260
		      bfd_close (entry->the_bfd);
266
		      bfd_close (entry->the_bfd);
261
		      entry->the_bfd = NULL;
267
		      entry->the_bfd = NULL;
262
		      return FALSE;
268
		      return FALSE;
263
		    }
269
		    }
264
		}
270
		}
265
	      goto success;
271
	      goto success;
266
	    }
272
	    }
267
 
273
 
268
	  if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
274
	  if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
269
	    {
275
	    {
270
	      einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
276
	      einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
271
		     attempt);
277
		     attempt);
272
	      bfd_close (entry->the_bfd);
278
	      bfd_close (entry->the_bfd);
273
	      entry->the_bfd = NULL;
279
	      entry->the_bfd = NULL;
274
	      return FALSE;
280
	      return FALSE;
275
	    }
281
	    }
276
 
282
 
277
	  if (entry->flags.search_dirs
283
	  if (entry->flags.search_dirs
278
	      && !bfd_arch_get_compatible (check, link_info.output_bfd,
284
	      && !bfd_arch_get_compatible (check, link_info.output_bfd,
279
					   command_line.accept_unknown_input_arch)
285
					   command_line.accept_unknown_input_arch)
280
	      /* XCOFF archives can have 32 and 64 bit objects.  */
286
	      /* XCOFF archives can have 32 and 64 bit objects.  */
281
	      && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
287
	      && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
282
		    && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
288
		    && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
283
		    && bfd_check_format (entry->the_bfd, bfd_archive)))
289
		    && bfd_check_format (entry->the_bfd, bfd_archive)))
284
	    {
290
	    {
285
	      if (command_line.warn_search_mismatch)
291
	      if (command_line.warn_search_mismatch)
286
		einfo (_("%P: skipping incompatible %s "
292
		einfo (_("%P: skipping incompatible %s "
287
			 "when searching for %s\n"),
293
			 "when searching for %s\n"),
288
		       attempt, entry->local_sym_name);
294
		       attempt, entry->local_sym_name);
289
	      bfd_close (entry->the_bfd);
295
	      bfd_close (entry->the_bfd);
290
	      entry->the_bfd = NULL;
296
	      entry->the_bfd = NULL;
291
	      return FALSE;
297
	      return FALSE;
292
	    }
298
	    }
293
	}
299
	}
294
    }
300
    }
295
success:
301
success:
296
#ifdef ENABLE_PLUGINS
302
#ifdef ENABLE_PLUGINS
297
  /* If plugins are active, they get first chance to claim
303
  /* If plugins are active, they get first chance to claim
298
     any successfully-opened input file.  We skip archives
304
     any successfully-opened input file.  We skip archives
299
     here; the plugin wants us to offer it the individual
305
     here; the plugin wants us to offer it the individual
300
     members when we enumerate them, not the whole file.  We
306
     members when we enumerate them, not the whole file.  We
301
     also ignore corefiles, because that's just weird.  It is
307
     also ignore corefiles, because that's just weird.  It is
302
     a needed side-effect of calling  bfd_check_format with
308
     a needed side-effect of calling  bfd_check_format with
303
     bfd_object that it sets the bfd's arch and mach, which
309
     bfd_object that it sets the bfd's arch and mach, which
304
     will be needed when and if we want to bfd_create a new
310
     will be needed when and if we want to bfd_create a new
305
     one using this one as a template.  */
311
     one using this one as a template.  */
306
  if (bfd_check_format (entry->the_bfd, bfd_object)
-
 
307
      && plugin_active_plugins_p ()
312
  if (link_info.lto_plugin_active
308
      && !no_more_claiming)
313
      && !no_more_claiming
309
    {
-
 
310
      int fd = open (attempt, O_RDONLY | O_BINARY);
314
      && bfd_check_format (entry->the_bfd, bfd_object))
311
      if (fd >= 0)
-
 
312
	{
-
 
313
	  struct ld_plugin_input_file file;
-
 
314
 
-
 
315
	  file.name = attempt;
-
 
316
	  file.offset = 0;
-
 
317
	  file.filesize = lseek (fd, 0, SEEK_END);
-
 
318
	  file.fd = fd;
-
 
319
	  plugin_maybe_claim (&file, entry);
315
    plugin_maybe_claim (entry);
320
	}
-
 
321
    }
-
 
322
#endif /* ENABLE_PLUGINS */
316
#endif /* ENABLE_PLUGINS */
323
 
317
 
324
  /* It opened OK, the format checked out, and the plugins have had
318
  /* It opened OK, the format checked out, and the plugins have had
325
     their chance to claim it, so this is success.  */
319
     their chance to claim it, so this is success.  */
326
  return TRUE;
320
  return TRUE;
327
}
321
}
328
 
322
 
329
/* Search for and open the file specified by ENTRY.  If it is an
323
/* Search for and open the file specified by ENTRY.  If it is an
330
   archive, use ARCH, LIB and SUFFIX to modify the file name.  */
324
   archive, use ARCH, LIB and SUFFIX to modify the file name.  */
331
 
325
 
332
bfd_boolean
326
bfd_boolean
333
ldfile_open_file_search (const char *arch,
327
ldfile_open_file_search (const char *arch,
334
			 lang_input_statement_type *entry,
328
			 lang_input_statement_type *entry,
335
			 const char *lib,
329
			 const char *lib,
336
			 const char *suffix)
330
			 const char *suffix)
337
{
331
{
338
  search_dirs_type *search;
332
  search_dirs_type *search;
339
 
333
 
340
  /* If this is not an archive, try to open it in the current
334
  /* If this is not an archive, try to open it in the current
341
     directory first.  */
335
     directory first.  */
342
  if (! entry->flags.maybe_archive)
336
  if (! entry->flags.maybe_archive)
343
    {
337
    {
344
      if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
338
      if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
345
	{
339
	{
346
	  char *name = concat (ld_sysroot, entry->filename,
340
	  char *name = concat (ld_sysroot, entry->filename,
347
			       (const char *) NULL);
341
			       (const char *) NULL);
348
	  if (ldfile_try_open_bfd (name, entry))
342
	  if (ldfile_try_open_bfd (name, entry))
349
	    {
343
	    {
350
	      entry->filename = name;
344
	      entry->filename = name;
351
	      return TRUE;
345
	      return TRUE;
352
	    }
346
	    }
353
	  free (name);
347
	  free (name);
354
	}
348
	}
355
      else if (ldfile_try_open_bfd (entry->filename, entry))
349
      else if (ldfile_try_open_bfd (entry->filename, entry))
356
	return TRUE;
350
	return TRUE;
357
 
351
 
358
      if (IS_ABSOLUTE_PATH (entry->filename))
352
      if (IS_ABSOLUTE_PATH (entry->filename))
359
	return FALSE;
353
	return FALSE;
360
    }
354
    }
361
 
355
 
362
  for (search = search_head; search != NULL; search = search->next)
356
  for (search = search_head; search != NULL; search = search->next)
363
    {
357
    {
364
      char *string;
358
      char *string;
365
 
359
 
366
      if (entry->flags.dynamic && ! link_info.relocatable)
360
      if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
367
	{
361
	{
368
	  if (ldemul_open_dynamic_archive (arch, search, entry))
362
	  if (ldemul_open_dynamic_archive (arch, search, entry))
369
	    return TRUE;
363
	    return TRUE;
370
	}
364
	}
371
 
365
 
372
      if (entry->flags.maybe_archive)
366
      if (entry->flags.maybe_archive && !entry->flags.full_name_provided)
373
	string = concat (search->name, slash, lib, entry->filename,
367
	string = concat (search->name, slash, lib, entry->filename,
374
			 arch, suffix, (const char *) NULL);
368
			 arch, suffix, (const char *) NULL);
375
      else
369
      else
376
	string = concat (search->name, slash, entry->filename,
370
	string = concat (search->name, slash, entry->filename,
377
			 (const char *) 0);
371
			 (const char *) 0);
378
 
372
 
379
      if (ldfile_try_open_bfd (string, entry))
373
      if (ldfile_try_open_bfd (string, entry))
380
	{
374
	{
381
	  entry->filename = string;
375
	  entry->filename = string;
382
	  return TRUE;
376
	  return TRUE;
383
	}
377
	}
384
 
378
 
385
      free (string);
379
      free (string);
386
    }
380
    }
387
 
381
 
388
  return FALSE;
382
  return FALSE;
389
}
383
}
390
 
384
 
391
/* Open the input file specified by ENTRY.
385
/* Open the input file specified by ENTRY.
392
   PR 4437: Do not stop on the first missing file, but
386
   PR 4437: Do not stop on the first missing file, but
393
   continue processing other input files in case there
387
   continue processing other input files in case there
394
   are more errors to report.  */
388
   are more errors to report.  */
395
 
389
 
396
void
390
void
397
ldfile_open_file (lang_input_statement_type *entry)
391
ldfile_open_file (lang_input_statement_type *entry)
398
{
392
{
399
  if (entry->the_bfd != NULL)
393
  if (entry->the_bfd != NULL)
400
    return;
394
    return;
401
 
395
 
402
  if (! entry->flags.search_dirs)
396
  if (! entry->flags.search_dirs)
403
    {
397
    {
404
      if (ldfile_try_open_bfd (entry->filename, entry))
398
      if (ldfile_try_open_bfd (entry->filename, entry))
405
	return;
399
	return;
406
 
400
 
407
      if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
401
      if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
408
	einfo (_("%P: cannot find %s (%s): %E\n"),
402
	einfo (_("%P: cannot find %s (%s): %E\n"),
409
	       entry->filename, entry->local_sym_name);
403
	       entry->filename, entry->local_sym_name);
410
      else
404
      else
411
	einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
405
	einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
412
 
406
 
413
      entry->flags.missing_file = TRUE;
407
      entry->flags.missing_file = TRUE;
414
      input_flags.missing_file = TRUE;
408
      input_flags.missing_file = TRUE;
415
    }
409
    }
416
  else
410
  else
417
    {
411
    {
418
      search_arch_type *arch;
412
      search_arch_type *arch;
419
      bfd_boolean found = FALSE;
413
      bfd_boolean found = FALSE;
420
 
414
 
421
      /* Try to open  or lib.a */
415
      /* Try to open  or lib.a */
422
      for (arch = search_arch_head; arch != NULL; arch = arch->next)
416
      for (arch = search_arch_head; arch != NULL; arch = arch->next)
423
	{
417
	{
424
	  found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
418
	  found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
425
	  if (found)
419
	  if (found)
426
	    break;
420
	    break;
427
#ifdef VMS
421
#ifdef VMS
428
	  found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
422
	  found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
429
	  if (found)
423
	  if (found)
430
	    break;
424
	    break;
431
#endif
425
#endif
432
	  found = ldemul_find_potential_libraries (arch->name, entry);
426
	  found = ldemul_find_potential_libraries (arch->name, entry);
433
	  if (found)
427
	  if (found)
434
	    break;
428
	    break;
435
	}
429
	}
436
 
430
 
437
      /* If we have found the file, we don't need to search directories
431
      /* If we have found the file, we don't need to search directories
438
	 again.  */
432
	 again.  */
439
      if (found)
433
      if (found)
440
	entry->flags.search_dirs = FALSE;
434
	entry->flags.search_dirs = FALSE;
441
      else
435
      else
442
	{
436
	{
443
	  if (entry->flags.sysrooted
437
	  if (entry->flags.sysrooted
444
	       && ld_sysroot
438
	       && ld_sysroot
445
	       && IS_ABSOLUTE_PATH (entry->local_sym_name))
439
	       && IS_ABSOLUTE_PATH (entry->local_sym_name))
446
	    einfo (_("%P: cannot find %s inside %s\n"),
440
	    einfo (_("%P: cannot find %s inside %s\n"),
447
		   entry->local_sym_name, ld_sysroot);
441
		   entry->local_sym_name, ld_sysroot);
448
	  else
442
	  else
449
	    einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
443
	    einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
450
	  entry->flags.missing_file = TRUE;
444
	  entry->flags.missing_file = TRUE;
451
	  input_flags.missing_file = TRUE;
445
	  input_flags.missing_file = TRUE;
452
	}
446
	}
453
    }
447
    }
454
}
448
}
455
 
449
 
456
/* Try to open NAME.  */
450
/* Try to open NAME.  */
457
 
451
 
458
static FILE *
452
static FILE *
459
try_open (const char *name, bfd_boolean *sysrooted)
453
try_open (const char *name, bfd_boolean *sysrooted)
460
{
454
{
461
  FILE *result;
455
  FILE *result;
462
 
456
 
463
  result = fopen (name, "r");
457
  result = fopen (name, "r");
464
 
458
 
465
  if (result != NULL)
459
  if (result != NULL)
466
    *sysrooted = is_sysrooted_pathname (name);
460
    *sysrooted = is_sysrooted_pathname (name);
467
 
461
 
468
  if (verbose)
462
  if (verbose)
469
    {
463
    {
470
      if (result == NULL)
464
      if (result == NULL)
471
	info_msg (_("cannot find script file %s\n"), name);
465
	info_msg (_("cannot find script file %s\n"), name);
472
      else
466
      else
473
	info_msg (_("opened script file %s\n"), name);
467
	info_msg (_("opened script file %s\n"), name);
474
    }
468
    }
475
 
469
 
476
  return result;
470
  return result;
477
}
471
}
478
 
472
 
479
/* Return TRUE iff directory DIR contains an "ldscripts" subdirectory.  */
473
/* Return TRUE iff directory DIR contains an "ldscripts" subdirectory.  */
480
 
474
 
481
static bfd_boolean
475
static bfd_boolean
482
check_for_scripts_dir (char *dir)
476
check_for_scripts_dir (char *dir)
483
{
477
{
484
  char *buf;
478
  char *buf;
485
  struct stat s;
479
  struct stat s;
486
  bfd_boolean res;
480
  bfd_boolean res;
487
 
481
 
488
  buf = concat (dir, "/ldscripts", (const char *) NULL);
482
  buf = concat (dir, "/ldscripts", (const char *) NULL);
489
  res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
483
  res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
490
  free (buf);
484
  free (buf);
491
  return res;
485
  return res;
492
}
486
}
493
 
487
 
494
/* Return the default directory for finding script files.
488
/* Return the default directory for finding script files.
495
   We look for the "ldscripts" directory in:
489
   We look for the "ldscripts" directory in:
496
 
490
 
497
   SCRIPTDIR (passed from Makefile)
491
   SCRIPTDIR (passed from Makefile)
498
	     (adjusted according to the current location of the binary)
492
	     (adjusted according to the current location of the binary)
499
   the dir where this program is (for using it from the build tree).  */
493
   the dir where this program is (for using it from the build tree).  */
500
 
494
 
501
static char *
495
static char *
502
find_scripts_dir (void)
496
find_scripts_dir (void)
503
{
497
{
504
  char *dir;
498
  char *dir;
505
 
499
 
506
  dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
500
  dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
507
  if (dir)
501
  if (dir)
508
    {
502
    {
509
      if (check_for_scripts_dir (dir))
503
      if (check_for_scripts_dir (dir))
510
	return dir;
504
	return dir;
511
      free (dir);
505
      free (dir);
512
    }
506
    }
513
 
507
 
514
  dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
508
  dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
515
  if (dir)
509
  if (dir)
516
    {
510
    {
517
      if (check_for_scripts_dir (dir))
511
      if (check_for_scripts_dir (dir))
518
	return dir;
512
	return dir;
519
      free (dir);
513
      free (dir);
520
    }
514
    }
521
 
515
 
522
  /* Look for "ldscripts" in the dir where our binary is.  */
516
  /* Look for "ldscripts" in the dir where our binary is.  */
523
  dir = make_relative_prefix (program_name, ".", ".");
517
  dir = make_relative_prefix (program_name, ".", ".");
524
  if (dir)
518
  if (dir)
525
    {
519
    {
526
      if (check_for_scripts_dir (dir))
520
      if (check_for_scripts_dir (dir))
527
	return dir;
521
	return dir;
528
      free (dir);
522
      free (dir);
529
    }
523
    }
530
 
524
 
531
  return NULL;
525
  return NULL;
532
}
526
}
533
 
527
 
534
/* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
528
/* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
535
   it in directories specified with -L, then in the default script
529
   it in directories specified with -L, then in the default script
536
   directory.  If DEFAULT_ONLY is true, the search is restricted to
530
   directory.  If DEFAULT_ONLY is true, the search is restricted to
537
   the default script location.  */
531
   the default script location.  */
538
 
532
 
539
static FILE *
533
static FILE *
540
ldfile_find_command_file (const char *name,
534
ldfile_find_command_file (const char *name,
541
			  bfd_boolean default_only,
535
			  bfd_boolean default_only,
542
			  bfd_boolean *sysrooted)
536
			  bfd_boolean *sysrooted)
543
{
537
{
544
  search_dirs_type *search;
538
  search_dirs_type *search;
545
  FILE *result = NULL;
539
  FILE *result = NULL;
546
  char *path;
540
  char *path;
547
  static search_dirs_type *script_search;
541
  static search_dirs_type *script_search;
548
 
542
 
549
  if (!default_only)
543
  if (!default_only)
550
    {
544
    {
551
      /* First try raw name.  */
545
      /* First try raw name.  */
552
      result = try_open (name, sysrooted);
546
      result = try_open (name, sysrooted);
553
      if (result != NULL)
547
      if (result != NULL)
554
	return result;
548
	return result;
555
    }
549
    }
556
 
550
 
557
  if (!script_search)
551
  if (!script_search)
558
    {
552
    {
559
      char *script_dir = find_scripts_dir ();
553
      char *script_dir = find_scripts_dir ();
560
      if (script_dir)
554
      if (script_dir)
561
	{
555
	{
562
	  search_dirs_type **save_tail_ptr = search_tail_ptr;
556
	  search_dirs_type **save_tail_ptr = search_tail_ptr;
563
	  search_tail_ptr = &script_search;
557
	  search_tail_ptr = &script_search;
564
	  ldfile_add_library_path (script_dir, TRUE);
558
	  ldfile_add_library_path (script_dir, TRUE);
565
	  search_tail_ptr = save_tail_ptr;
559
	  search_tail_ptr = save_tail_ptr;
566
	}
560
	}
567
    }
561
    }
568
 
562
 
569
  /* Temporarily append script_search to the path list so that the
563
  /* Temporarily append script_search to the path list so that the
570
     paths specified with -L will be searched first.  */
564
     paths specified with -L will be searched first.  */
571
  *search_tail_ptr = script_search;
565
  *search_tail_ptr = script_search;
572
 
566
 
573
  /* Try now prefixes.  */
567
  /* Try now prefixes.  */
574
  for (search = default_only ? script_search : search_head;
568
  for (search = default_only ? script_search : search_head;
575
       search != NULL;
569
       search != NULL;
576
       search = search->next)
570
       search = search->next)
577
    {
571
    {
578
      path = concat (search->name, slash, name, (const char *) NULL);
572
      path = concat (search->name, slash, name, (const char *) NULL);
579
      result = try_open (path, sysrooted);
573
      result = try_open (path, sysrooted);
580
      free (path);
574
      free (path);
581
      if (result)
575
      if (result)
582
	break;
576
	break;
583
    }
577
    }
584
 
578
 
585
  /* Restore the original path list.  */
579
  /* Restore the original path list.  */
586
  *search_tail_ptr = NULL;
580
  *search_tail_ptr = NULL;
587
 
581
 
588
  return result;
582
  return result;
589
}
583
}
590
 
584
 
591
/* Open command file NAME.  */
585
/* Open command file NAME.  */
592
 
586
 
593
static void
587
static void
594
ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
588
ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
595
{
589
{
596
  FILE *ldlex_input_stack;
590
  FILE *ldlex_input_stack;
597
  bfd_boolean sysrooted;
591
  bfd_boolean sysrooted;
598
 
592
 
599
  ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted);
593
  ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted);
600
 
594
 
601
  if (ldlex_input_stack == NULL)
595
  if (ldlex_input_stack == NULL)
602
    {
596
    {
603
      bfd_set_error (bfd_error_system_call);
597
      bfd_set_error (bfd_error_system_call);
604
      einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
598
      einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
605
      return;
599
      return;
606
    }
600
    }
607
 
601
 
608
  lex_push_file (ldlex_input_stack, name, sysrooted);
602
  lex_push_file (ldlex_input_stack, name, sysrooted);
609
 
603
 
610
  lineno = 1;
604
  lineno = 1;
611
 
605
 
612
  saved_script_handle = ldlex_input_stack;
606
  saved_script_handle = ldlex_input_stack;
613
}
607
}
614
 
608
 
615
/* Open command file NAME in the current directory, -L directories,
609
/* Open command file NAME in the current directory, -L directories,
616
   the default script location, in that order.  */
610
   the default script location, in that order.  */
617
 
611
 
618
void
612
void
619
ldfile_open_command_file (const char *name)
613
ldfile_open_command_file (const char *name)
620
{
614
{
621
  ldfile_open_command_file_1 (name, FALSE);
615
  ldfile_open_command_file_1 (name, FALSE);
622
}
616
}
623
 
617
 
624
/* Open command file NAME at the default script location.  */
618
/* Open command file NAME at the default script location.  */
625
 
619
 
626
void
620
void
627
ldfile_open_default_command_file (const char *name)
621
ldfile_open_default_command_file (const char *name)
628
{
622
{
629
  ldfile_open_command_file_1 (name, TRUE);
623
  ldfile_open_command_file_1 (name, TRUE);
630
}
624
}
631
 
625
 
632
void
626
void
633
ldfile_add_arch (const char *in_name)
627
ldfile_add_arch (const char *in_name)
634
{
628
{
635
  char *name = xstrdup (in_name);
629
  char *name = xstrdup (in_name);
636
  search_arch_type *new_arch = (search_arch_type *)
630
  search_arch_type *new_arch = (search_arch_type *)
637
      xmalloc (sizeof (search_arch_type));
631
      xmalloc (sizeof (search_arch_type));
638
 
632
 
639
  ldfile_output_machine_name = in_name;
633
  ldfile_output_machine_name = in_name;
640
 
634
 
641
  new_arch->name = name;
635
  new_arch->name = name;
642
  new_arch->next = NULL;
636
  new_arch->next = NULL;
643
  while (*name)
637
  while (*name)
644
    {
638
    {
645
      *name = TOLOWER (*name);
639
      *name = TOLOWER (*name);
646
      name++;
640
      name++;
647
    }
641
    }
648
  *search_arch_tail_ptr = new_arch;
642
  *search_arch_tail_ptr = new_arch;
649
  search_arch_tail_ptr = &new_arch->next;
643
  search_arch_tail_ptr = &new_arch->next;
650
 
644
 
651
}
645
}
652
 
646
 
653
/* Set the output architecture.  */
647
/* Set the output architecture.  */
654
 
648
 
655
void
649
void
656
ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
650
ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
657
{
651
{
658
  const bfd_arch_info_type *arch = bfd_scan_arch (string);
652
  const bfd_arch_info_type *arch = bfd_scan_arch (string);
659
 
653
 
660
  if (arch)
654
  if (arch)
661
    {
655
    {
662
      ldfile_output_architecture = arch->arch;
656
      ldfile_output_architecture = arch->arch;
663
      ldfile_output_machine = arch->mach;
657
      ldfile_output_machine = arch->mach;
664
      ldfile_output_machine_name = arch->printable_name;
658
      ldfile_output_machine_name = arch->printable_name;
665
    }
659
    }
666
  else if (defarch != bfd_arch_unknown)
660
  else if (defarch != bfd_arch_unknown)
667
    ldfile_output_architecture = defarch;
661
    ldfile_output_architecture = defarch;
668
  else
662
  else
669
    einfo (_("%P%F: cannot represent machine `%s'\n"), string);
663
    einfo (_("%P%F: cannot represent machine `%s'\n"), string);
670
}
664
}