OSDN Git Service

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