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