1 /* ELF emulation code for targets using elf.em.
2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "libiberty.h"
24 #include "filenames.h"
25 #include "safe-ctype.h"
35 #include "ldbuildid.h"
49 /* Style of .note.gnu.build-id section. */
50 const char *ldelf_emit_note_gnu_build_id
;
52 /* These variables are required to pass information back and forth
53 between after_open and check_needed and stat_needed and vercheck. */
55 static struct bfd_link_needed_list
*global_needed
;
56 static lang_input_statement_type
*global_found
;
57 static struct stat global_stat
;
58 static struct bfd_link_needed_list
*global_vercheck_needed
;
59 static bfd_boolean global_vercheck_failed
;
62 ldelf_after_parse (void)
64 if (bfd_link_pie (&link_info
))
65 link_info
.flags_1
|= (bfd_vma
) DF_1_PIE
;
67 if (bfd_link_executable (&link_info
)
68 && link_info
.nointerp
)
70 if (link_info
.dynamic_undefined_weak
> 0)
71 einfo (_("%P: warning: -z dynamic-undefined-weak ignored\n"));
72 link_info
.dynamic_undefined_weak
= 0;
74 after_parse_default ();
77 /* Handle the generation of DT_NEEDED tags. */
80 ldelf_load_symbols (lang_input_statement_type
*entry
)
84 /* Tell the ELF linker that we don't want the output file to have a
85 DT_NEEDED entry for this file, unless it is used to resolve
86 references in a regular object. */
87 if (entry
->flags
.add_DT_NEEDED_for_regular
)
88 link_class
= DYN_AS_NEEDED
;
90 /* Tell the ELF linker that we don't want the output file to have a
91 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
93 if (!entry
->flags
.add_DT_NEEDED_for_dynamic
)
94 link_class
|= DYN_NO_ADD_NEEDED
;
96 if (entry
->flags
.just_syms
97 && (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) != 0)
98 einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
102 || (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) == 0)
105 bfd_elf_set_dyn_lib_class (entry
->the_bfd
,
106 (enum dynamic_lib_link_class
) link_class
);
108 /* Continue on with normal load_symbols processing. */
112 /* On Linux, it's possible to have different versions of the same
113 shared library linked against different versions of libc. The
114 dynamic linker somehow tags which libc version to use in
115 /etc/ld.so.cache, and, based on the libc that it sees in the
116 executable, chooses which version of the shared library to use.
118 We try to do a similar check here by checking whether this shared
119 library needs any other shared libraries which may conflict with
120 libraries we have already included in the link. If it does, we
121 skip it, and try to find another shared library farther on down the
124 This is called via lang_for_each_input_file.
125 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
126 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
127 a conflicting version. */
130 ldelf_vercheck (lang_input_statement_type
*s
)
133 struct bfd_link_needed_list
*l
;
135 if (global_vercheck_failed
)
137 if (s
->the_bfd
== NULL
138 || (bfd_get_file_flags (s
->the_bfd
) & DYNAMIC
) == 0)
141 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
143 soname
= lbasename (bfd_get_filename (s
->the_bfd
));
145 for (l
= global_vercheck_needed
; l
!= NULL
; l
= l
->next
)
149 if (filename_cmp (soname
, l
->name
) == 0)
151 /* Probably can't happen, but it's an easy check. */
155 if (strchr (l
->name
, '/') != NULL
)
158 suffix
= strstr (l
->name
, ".so.");
162 suffix
+= sizeof ".so." - 1;
164 if (filename_ncmp (soname
, l
->name
, suffix
- l
->name
) == 0)
166 /* Here we know that S is a dynamic object FOO.SO.VER1, and
167 the object we are considering needs a dynamic object
168 FOO.SO.VER2, and VER1 and VER2 are different. This
169 appears to be a version mismatch, so we tell the caller
170 to try a different version of this library. */
171 global_vercheck_failed
= TRUE
;
178 /* See if an input file matches a DT_NEEDED entry by running stat on
182 ldelf_stat_needed (lang_input_statement_type
*s
)
188 if (global_found
!= NULL
)
190 if (s
->the_bfd
== NULL
)
193 /* If this input file was an as-needed entry, and wasn't found to be
194 needed at the stage it was linked, then don't say we have loaded it. */
195 if ((bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
198 if (bfd_stat (s
->the_bfd
, &st
) != 0)
200 einfo (_("%P: %pB: bfd_stat failed: %E\n"), s
->the_bfd
);
204 /* Some operating systems, e.g. Windows, do not provide a meaningful
205 st_ino; they always set it to zero. (Windows does provide a
206 meaningful st_dev.) Do not indicate a duplicate library in that
207 case. While there is no guarantee that a system that provides
208 meaningful inode numbers will never set st_ino to zero, this is
209 merely an optimization, so we do not need to worry about false
211 if (st
.st_dev
== global_stat
.st_dev
212 && st
.st_ino
== global_stat
.st_ino
219 /* We issue a warning if it looks like we are including two
220 different versions of the same shared library. For example,
221 there may be a problem if -lc picks up libc.so.6 but some other
222 shared library has a DT_NEEDED entry of libc.so.5. This is a
223 heuristic test, and it will only work if the name looks like
224 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
225 If we really want to issue warnings about mixing version numbers
226 of shared libraries, we need to find a better way. */
228 if (strchr (global_needed
->name
, '/') != NULL
)
230 suffix
= strstr (global_needed
->name
, ".so.");
233 suffix
+= sizeof ".so." - 1;
235 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
237 soname
= lbasename (s
->filename
);
239 if (filename_ncmp (soname
, global_needed
->name
,
240 suffix
- global_needed
->name
) == 0)
241 einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
242 global_needed
->name
, global_needed
->by
, soname
);
245 /* This function is called for each possible name for a dynamic object
246 named by a DT_NEEDED entry. The FORCE parameter indicates whether
247 to skip the check for a conflicting version. */
250 ldelf_try_needed (struct dt_needed
*needed
, int force
, int is_linux
)
253 const char *name
= needed
->name
;
257 abfd
= bfd_openr (name
, bfd_get_target (link_info
.output_bfd
));
261 info_msg (_("attempt to open %s failed\n"), name
);
265 /* Linker needs to decompress sections. */
266 abfd
->flags
|= BFD_DECOMPRESS
;
268 if (! bfd_check_format (abfd
, bfd_object
))
273 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) == 0)
279 /* For DT_NEEDED, they have to match. */
280 if (abfd
->xvec
!= link_info
.output_bfd
->xvec
)
286 /* Check whether this object would include any conflicting library
287 versions. If FORCE is set, then we skip this check; we use this
288 the second time around, if we couldn't find any compatible
289 instance of the shared library. */
293 struct bfd_link_needed_list
*needs
;
295 if (! bfd_elf_get_bfd_needed_list (abfd
, &needs
))
296 einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd
);
300 global_vercheck_needed
= needs
;
301 global_vercheck_failed
= FALSE
;
302 lang_for_each_input_file (ldelf_vercheck
);
303 if (global_vercheck_failed
)
306 /* Return FALSE to force the caller to move on to try
307 another file on the search path. */
311 /* But wait! It gets much worse. On Linux, if a shared
312 library does not use libc at all, we are supposed to skip
313 it the first time around in case we encounter a shared
314 library later on with the same name which does use the
315 version of libc that we want. This is much too horrible
316 to use on any system other than Linux. */
319 struct bfd_link_needed_list
*l
;
321 for (l
= needs
; l
!= NULL
; l
= l
->next
)
322 if (CONST_STRNEQ (l
->name
, "libc.so"))
333 /* We've found a dynamic object matching the DT_NEEDED entry. */
335 /* We have already checked that there is no other input file of the
336 same name. We must now check again that we are not including the
337 same file twice. We need to do this because on many systems
338 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
339 reference libc.so.1. If we have already included libc.so, we
340 don't want to include libc.so.1 if they are the same file, and we
341 can only check that using stat. */
343 if (bfd_stat (abfd
, &global_stat
) != 0)
344 einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd
);
346 /* First strip off everything before the last '/'. */
347 soname
= lbasename (bfd_get_filename (abfd
));
350 info_msg (_("found %s at %s\n"), soname
, name
);
353 lang_for_each_input_file (ldelf_stat_needed
);
354 if (global_found
!= NULL
)
356 /* Return TRUE to indicate that we found the file, even though
357 we aren't going to do anything with it. */
361 /* Specify the soname to use. */
362 bfd_elf_set_dt_needed_name (abfd
, soname
);
364 /* Tell the ELF linker that we don't want the output file to have a
365 DT_NEEDED entry for this file, unless it is used to resolve
366 references in a regular object. */
367 link_class
= DYN_DT_NEEDED
;
369 /* Tell the ELF linker that we don't want the output file to have a
370 DT_NEEDED entry for this file at all if the entry is from a file
371 with DYN_NO_ADD_NEEDED. */
372 if (needed
->by
!= NULL
373 && (bfd_elf_get_dyn_lib_class (needed
->by
) & DYN_NO_ADD_NEEDED
) != 0)
374 link_class
|= DYN_NO_NEEDED
| DYN_NO_ADD_NEEDED
;
376 bfd_elf_set_dyn_lib_class (abfd
, (enum dynamic_lib_link_class
) link_class
);
378 *link_info
.input_bfds_tail
= abfd
;
379 link_info
.input_bfds_tail
= &abfd
->link
.next
;
381 /* Add this file into the symbol table. */
382 if (! bfd_link_add_symbols (abfd
, &link_info
))
383 einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd
);
388 /* Search for a needed file in a path. */
391 ldelf_search_needed (const char *path
, struct dt_needed
*n
, int force
,
392 int is_linux
, int elfsize
)
395 const char *name
= n
->name
;
397 struct dt_needed needed
;
400 return ldelf_try_needed (n
, force
, is_linux
);
402 if (path
== NULL
|| *path
== '\0')
406 needed
.name
= n
->name
;
413 char *filename
, *sset
;
415 s
= strchr (path
, config
.rpath_separator
);
417 s
= path
+ strlen (path
);
419 #if HAVE_DOS_BASED_FILE_SYSTEM
420 /* Assume a match on the second char is part of drive specifier. */
421 else if (config
.rpath_separator
== ':'
425 s
= strchr (s
+ 1, config
.rpath_separator
);
427 s
= path
+ strlen (path
);
430 filename
= (char *) xmalloc (s
- path
+ len
+ 2);
435 memcpy (filename
, path
, s
- path
);
436 filename
[s
- path
] = '/';
437 sset
= filename
+ (s
- path
) + 1;
441 /* PR 20535: Support the same pseudo-environment variables that
442 are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
443 Since there can be more than one occurrence of these tokens in
444 the path we loop until no more are found. Since we might not
445 be able to substitute some of the tokens we maintain an offset
446 into the filename for where we should begin our scan. */
447 while ((var
= strchr (filename
+ offset
, '$')) != NULL
)
449 /* The ld.so manual page does not say, but I am going to assume that
450 these tokens are terminated by a directory separator character
451 (/) or the end of the string. There is also an implication that
452 $ORIGIN should only be used at the start of a path, but that is
455 The ld.so manual page also states that it allows ${ORIGIN},
456 ${LIB} and ${PLATFORM}, so these are supported as well.
458 FIXME: The code could be a lot cleverer about allocating space
459 for the processed string. */
460 char * end
= strchr (var
, '/');
461 const char *replacement
= NULL
;
463 char * freeme
= NULL
;
464 unsigned flen
= strlen (filename
);
467 /* Temporarily terminate the filename at the end of the token. */
475 if (strcmp (v
, "RIGIN") == 0 || strcmp (v
, "RIGIN}") == 0)
477 /* ORIGIN - replace with the full path to the directory
478 containing the program or shared object. */
479 if (needed
.by
== NULL
)
481 if (link_info
.output_bfd
== NULL
)
486 replacement
= bfd_get_filename (link_info
.output_bfd
);
489 replacement
= bfd_get_filename (needed
.by
);
495 if (replacement
[0] == '/')
496 freeme
= xstrdup (replacement
);
499 char * current_dir
= getpwd ();
501 freeme
= xmalloc (strlen (replacement
)
502 + strlen (current_dir
) + 2);
503 sprintf (freeme
, "%s/%s", current_dir
, replacement
);
506 replacement
= freeme
;
507 if ((slash
= strrchr (replacement
, '/')) != NULL
)
514 if (strcmp (v
, "IB") == 0 || strcmp (v
, "IB}") == 0)
516 /* LIB - replace with "lib" in 32-bit environments
517 and "lib64" in 64-bit environments. */
521 case 32: replacement
= "lib"; break;
522 case 64: replacement
= "lib64"; break;
530 /* Supporting $PLATFORM in a cross-hosted environment is not
531 possible. Supporting it in a native environment involves
532 loading the <sys/auxv.h> header file which loads the
533 system <elf.h> header file, which conflicts with the
534 "include/elf/mips.h" header file. */
542 char * filename2
= xmalloc (flen
+ strlen (replacement
));
546 sprintf (filename2
, "%.*s%s/%s",
547 (int)(var
- filename
), filename
,
548 replacement
, end
+ 1);
549 offset
= (var
- filename
) + 1 + strlen (replacement
);
553 sprintf (filename2
, "%.*s%s",
554 (int)(var
- filename
), filename
,
556 offset
= var
- filename
+ strlen (replacement
);
560 filename
= filename2
;
561 /* There is no need to restore the path separator (when
562 end != NULL) as we have replaced the entire string. */
567 /* We only issue an "unrecognised" message in verbose mode
568 as the $<foo> token might be a legitimate component of
569 a path name in the target's file system. */
570 info_msg (_("unrecognised or unsupported token "
571 "'%s' in search path\n"), var
);
573 /* Restore the path separator. */
576 /* PR 20784: Make sure that we resume the scan *after*
577 the token that we could not replace. */
578 offset
= (var
+ 1) - filename
;
584 needed
.name
= filename
;
586 if (ldelf_try_needed (&needed
, force
, is_linux
))
599 /* Prefix the sysroot to absolute paths in PATH, a string containing
600 paths separated by config.rpath_separator. If running on a DOS
601 file system, paths containing a drive spec won't have the sysroot
602 prefix added, unless the sysroot also specifies the same drive. */
605 ldelf_add_sysroot (const char *path
)
610 int dos_drive_sysroot
= HAS_DRIVE_SPEC (ld_sysroot
);
612 len
= strlen (ld_sysroot
);
613 for (extra
= 0, p
= path
; ; )
615 int dos_drive
= HAS_DRIVE_SPEC (p
);
619 if (IS_DIR_SEPARATOR (*p
)
621 || (dos_drive_sysroot
622 && ld_sysroot
[0] == p
[-2])))
624 if (dos_drive
&& dos_drive_sysroot
)
629 p
= strchr (p
, config
.rpath_separator
);
635 ret
= xmalloc (strlen (path
) + extra
+ 1);
637 for (q
= ret
, p
= path
; ; )
640 int dos_drive
= HAS_DRIVE_SPEC (p
);
647 if (IS_DIR_SEPARATOR (*p
)
649 || (dos_drive_sysroot
650 && ld_sysroot
[0] == p
[-2])))
652 if (dos_drive
&& dos_drive_sysroot
)
654 strcpy (q
, ld_sysroot
+ 2);
659 strcpy (q
, ld_sysroot
);
663 end
= strchr (p
, config
.rpath_separator
);
666 size_t n
= end
- p
+ 1;
681 /* Read the system search path the FreeBSD way rather than the Linux way. */
682 #ifdef HAVE_ELF_HINTS_H
683 #include <elf-hints.h>
685 #include "elf-hints-local.h"
689 ldelf_check_ld_elf_hints (const struct bfd_link_needed_list
*l
, int force
,
692 static bfd_boolean initialized
;
693 static const char *ld_elf_hints
;
694 struct dt_needed needed
;
701 tmppath
= concat (ld_sysroot
, _PATH_ELF_HINTS
, (const char *) NULL
);
702 f
= fopen (tmppath
, FOPEN_RB
);
706 struct elfhints_hdr hdr
;
708 if (fread (&hdr
, 1, sizeof (hdr
), f
) == sizeof (hdr
)
709 && hdr
.magic
== ELFHINTS_MAGIC
712 if (fseek (f
, hdr
.strtab
+ hdr
.dirlist
, SEEK_SET
) != -1)
716 b
= xmalloc (hdr
.dirlistlen
+ 1);
717 if (fread (b
, 1, hdr
.dirlistlen
+ 1, f
) ==
719 ld_elf_hints
= ldelf_add_sysroot (b
);
730 if (ld_elf_hints
== NULL
)
734 needed
.name
= l
->name
;
735 return ldelf_search_needed (ld_elf_hints
, &needed
, force
, FALSE
, elfsize
);
738 /* For a native linker, check the file /etc/ld.so.conf for directories
739 in which we may find shared libraries. /etc/ld.so.conf is really
740 only meaningful on Linux. */
742 struct ldelf_ld_so_conf
749 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*, const char *);
752 ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf
*info
,
753 const char *filename
,
761 if (pattern
[0] != '/')
763 char *p
= strrchr (filename
, '/');
764 size_t patlen
= strlen (pattern
) + 1;
766 newp
= xmalloc (p
- filename
+ 1 + patlen
);
767 memcpy (newp
, filename
, p
- filename
+ 1);
768 memcpy (newp
+ (p
- filename
+ 1), pattern
, patlen
);
773 if (glob (pattern
, 0, NULL
, &gl
) == 0)
777 for (i
= 0; i
< gl
.gl_pathc
; ++i
)
778 ldelf_parse_ld_so_conf (info
, gl
.gl_pathv
[i
]);
782 /* If we do not have glob, treat the pattern as a literal filename. */
783 ldelf_parse_ld_so_conf (info
, pattern
);
790 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*info
, const char *filename
)
792 FILE *f
= fopen (filename
, FOPEN_RT
);
800 line
= xmalloc (linelen
);
805 /* Normally this would use getline(3), but we need to be portable. */
806 while ((q
= fgets (p
, linelen
- (p
- line
), f
)) != NULL
807 && strlen (q
) == linelen
- (p
- line
) - 1
808 && line
[linelen
- 2] != '\n')
810 line
= xrealloc (line
, 2 * linelen
);
811 p
= line
+ linelen
- 1;
815 if (q
== NULL
&& p
== line
)
818 p
= strchr (line
, '\n');
822 /* Because the file format does not know any form of quoting we
823 can search forward for the next '#' character and if found
824 make it terminating the line. */
825 p
= strchr (line
, '#');
829 /* Remove leading whitespace. NUL is no whitespace character. */
831 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
834 /* If the line is blank it is ignored. */
838 if (CONST_STRNEQ (p
, "include") && (p
[7] == ' ' || p
[7] == '\t'))
844 while (*p
== ' ' || *p
== '\t')
852 while (*p
!= ' ' && *p
!= '\t' && *p
)
858 ldelf_parse_ld_so_conf_include (info
, filename
, dir
);
865 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
866 && *p
!= '\r' && *p
!= '\v')
869 while (p
!= dir
&& p
[-1] == '/')
871 if (info
->path
== NULL
)
873 info
->alloc
= p
- dir
+ 1 + 256;
874 info
->path
= xmalloc (info
->alloc
);
879 if (info
->len
+ 1 + (p
- dir
) >= info
->alloc
)
881 info
->alloc
+= p
- dir
+ 256;
882 info
->path
= xrealloc (info
->path
, info
->alloc
);
884 info
->path
[info
->len
++] = config
.rpath_separator
;
886 memcpy (info
->path
+ info
->len
, dir
, p
- dir
);
887 info
->len
+= p
- dir
;
888 info
->path
[info
->len
] = '\0';
898 ldelf_check_ld_so_conf (const struct bfd_link_needed_list
*l
, int force
,
899 int elfsize
, const char *prefix
)
901 static bfd_boolean initialized
;
902 static const char *ld_so_conf
;
903 struct dt_needed needed
;
908 struct ldelf_ld_so_conf info
;
911 info
.len
= info
.alloc
= 0;
912 tmppath
= concat (ld_sysroot
, prefix
, "/etc/ld.so.conf",
913 (const char *) NULL
);
914 if (!ldelf_parse_ld_so_conf (&info
, tmppath
))
917 tmppath
= concat (ld_sysroot
, "/etc/ld.so.conf",
918 (const char *) NULL
);
919 ldelf_parse_ld_so_conf (&info
, tmppath
);
925 ld_so_conf
= ldelf_add_sysroot (info
.path
);
931 if (ld_so_conf
== NULL
)
936 needed
.name
= l
->name
;
937 return ldelf_search_needed (ld_so_conf
, &needed
, force
, TRUE
, elfsize
);
940 /* See if an input file matches a DT_NEEDED entry by name. */
943 ldelf_check_needed (lang_input_statement_type
*s
)
947 /* Stop looking if we've found a loaded lib. */
948 if (global_found
!= NULL
949 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
950 & DYN_AS_NEEDED
) == 0)
953 if (s
->filename
== NULL
|| s
->the_bfd
== NULL
)
956 /* Don't look for a second non-loaded as-needed lib. */
957 if (global_found
!= NULL
958 && (bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
961 if (filename_cmp (s
->filename
, global_needed
->name
) == 0)
967 if (s
->flags
.search_dirs
)
969 const char *f
= strrchr (s
->filename
, '/');
971 && filename_cmp (f
+ 1, global_needed
->name
) == 0)
978 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
980 && filename_cmp (soname
, global_needed
->name
) == 0)
987 /* This is called after all the input files have been opened. */
990 ldelf_after_open (int use_libpath
, int native
, int is_linux
, int is_freebsd
,
991 int elfsize
, const char *prefix
)
993 struct bfd_link_needed_list
*needed
, *l
;
994 struct elf_link_hash_table
*htab
;
997 bfd
**save_input_bfd_tail
;
999 after_open_default ();
1001 htab
= elf_hash_table (&link_info
);
1002 if (!is_elf_hash_table (htab
))
1005 if (command_line
.out_implib_filename
)
1007 unlink_if_ordinary (command_line
.out_implib_filename
);
1008 link_info
.out_implib_bfd
1009 = bfd_openw (command_line
.out_implib_filename
,
1010 bfd_get_target (link_info
.output_bfd
));
1012 if (link_info
.out_implib_bfd
== NULL
)
1014 einfo (_("%F%P: %s: can't open for writing: %E\n"),
1015 command_line
.out_implib_filename
);
1019 if (ldelf_emit_note_gnu_build_id
!= NULL
)
1021 /* Find an ELF input. */
1022 for (abfd
= link_info
.input_bfds
;
1023 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
1024 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1025 && bfd_count_sections (abfd
) != 0
1026 && !bfd_input_just_syms (abfd
))
1029 /* PR 10555: If there are no ELF input files do not try to
1030 create a .note.gnu-build-id section. */
1032 || !ldelf_setup_build_id (abfd
))
1034 free ((char *) ldelf_emit_note_gnu_build_id
);
1035 ldelf_emit_note_gnu_build_id
= NULL
;
1039 get_elf_backend_data (link_info
.output_bfd
)->setup_gnu_properties (&link_info
);
1041 if (bfd_link_relocatable (&link_info
))
1043 if (link_info
.execstack
== !link_info
.noexecstack
)
1045 /* PR ld/16744: If "-z [no]execstack" has been specified on the
1046 command line and we are perfoming a relocatable link then no
1047 PT_GNU_STACK segment will be created and so the
1048 linkinfo.[no]execstack values set in _handle_option() will have no
1049 effect. Instead we create a .note.GNU-stack section in much the
1050 same way as the assembler does with its --[no]execstack option. */
1051 flagword flags
= SEC_READONLY
| (link_info
.execstack
? SEC_CODE
: 0);
1052 (void) bfd_make_section_with_flags (link_info
.input_bfds
,
1053 ".note.GNU-stack", flags
);
1058 if (!link_info
.traditional_format
)
1061 bfd_boolean warn_eh_frame
= FALSE
;
1064 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1068 if (bfd_input_just_syms (abfd
))
1071 for (s
= abfd
->sections
; s
&& type
< COMPACT_EH_HDR
; s
= s
->next
)
1073 const char *name
= bfd_section_name (s
);
1075 if (bfd_is_abs_section (s
->output_section
))
1077 if (CONST_STRNEQ (name
, ".eh_frame_entry"))
1078 type
= COMPACT_EH_HDR
;
1079 else if (strcmp (name
, ".eh_frame") == 0 && s
->size
> 8)
1080 type
= DWARF2_EH_HDR
;
1089 else if (seen_type
!= type
)
1091 einfo (_("%F%P: compact frame descriptions incompatible with"
1092 " DWARF2 .eh_frame from %pB\n"),
1093 type
== DWARF2_EH_HDR
? abfd
: elfbfd
);
1098 && (type
== COMPACT_EH_HDR
1099 || link_info
.eh_frame_hdr_type
!= 0))
1101 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1104 warn_eh_frame
= TRUE
;
1108 if (seen_type
== COMPACT_EH_HDR
)
1109 link_info
.eh_frame_hdr_type
= COMPACT_EH_HDR
;
1113 const struct elf_backend_data
*bed
;
1115 bed
= get_elf_backend_data (elfbfd
);
1116 s
= bfd_make_section_with_flags (elfbfd
, ".eh_frame_hdr",
1117 bed
->dynamic_sec_flags
1120 && bfd_set_section_alignment (s
, 2))
1122 htab
->eh_info
.hdr_sec
= s
;
1123 warn_eh_frame
= FALSE
;
1127 einfo (_("%P: warning: cannot create .eh_frame_hdr section,"
1128 " --eh-frame-hdr ignored\n"));
1131 /* Get the list of files which appear in DT_NEEDED entries in
1132 dynamic objects included in the link (often there will be none).
1133 For each such file, we want to track down the corresponding
1134 library, and include the symbol table in the link. This is what
1135 the runtime dynamic linker will do. Tracking the files down here
1136 permits one dynamic object to include another without requiring
1137 special action by the person doing the link. Note that the
1138 needed list can actually grow while we are stepping through this
1140 save_input_bfd_tail
= link_info
.input_bfds_tail
;
1141 needed
= bfd_elf_get_needed_list (link_info
.output_bfd
, &link_info
);
1142 for (l
= needed
; l
!= NULL
; l
= l
->next
)
1144 struct bfd_link_needed_list
*ll
;
1145 struct dt_needed n
, nn
;
1148 /* If the lib that needs this one was --as-needed and wasn't
1149 found to be needed, then this lib isn't needed either. */
1151 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_AS_NEEDED
) != 0)
1154 /* Skip the lib if --no-copy-dt-needed-entries and
1155 --allow-shlib-undefined is in effect. */
1157 && link_info
.unresolved_syms_in_shared_libs
== RM_IGNORE
1158 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_NO_ADD_NEEDED
) != 0)
1161 /* If we've already seen this file, skip it. */
1162 for (ll
= needed
; ll
!= l
; ll
= ll
->next
)
1164 || (bfd_elf_get_dyn_lib_class (ll
->by
) & DYN_AS_NEEDED
) == 0)
1165 && strcmp (ll
->name
, l
->name
) == 0)
1170 /* See if this file was included in the link explicitly. */
1172 global_found
= NULL
;
1173 lang_for_each_input_file (ldelf_check_needed
);
1174 if (global_found
!= NULL
1175 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
1176 & DYN_AS_NEEDED
) == 0)
1183 info_msg (_("%s needed by %pB\n"), l
->name
, l
->by
);
1185 /* As-needed libs specified on the command line (or linker script)
1186 take priority over libs found in search dirs. */
1187 if (global_found
!= NULL
)
1189 nn
.name
= global_found
->filename
;
1190 if (ldelf_try_needed (&nn
, TRUE
, is_linux
))
1194 /* We need to find this file and include the symbol table. We
1195 want to search for the file in the same way that the dynamic
1196 linker will search. That means that we want to use
1197 rpath_link, rpath, then the environment variable
1198 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1199 entries (native only), then the linker script LIB_SEARCH_DIRS.
1200 We do not search using the -L arguments.
1202 We search twice. The first time, we skip objects which may
1203 introduce version mismatches. The second time, we force
1204 their use. See ldelf_vercheck comment. */
1205 for (force
= 0; force
< 2; force
++)
1208 search_dirs_type
*search
;
1210 struct bfd_link_needed_list
*rp
;
1213 if (ldelf_search_needed (command_line
.rpath_link
, &n
, force
,
1219 path
= command_line
.rpath
;
1222 path
= ldelf_add_sysroot (path
);
1223 found
= ldelf_search_needed (path
, &n
, force
,
1225 free ((char *) path
);
1232 if (command_line
.rpath_link
== NULL
1233 && command_line
.rpath
== NULL
)
1235 path
= (const char *) getenv ("LD_RUN_PATH");
1237 && ldelf_search_needed (path
, &n
, force
,
1241 path
= (const char *) getenv ("LD_LIBRARY_PATH");
1243 && ldelf_search_needed (path
, &n
, force
,
1250 rp
= bfd_elf_get_runpath_list (link_info
.output_bfd
, &link_info
);
1251 for (; !found
&& rp
!= NULL
; rp
= rp
->next
)
1253 path
= ldelf_add_sysroot (rp
->name
);
1254 found
= (rp
->by
== l
->by
1255 && ldelf_search_needed (path
, &n
, force
,
1256 is_linux
, elfsize
));
1257 free ((char *) path
);
1263 && ldelf_check_ld_elf_hints (l
, force
, elfsize
))
1267 && ldelf_check_ld_so_conf (l
, force
, elfsize
, prefix
))
1271 len
= strlen (l
->name
);
1272 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
1276 if (search
->cmdline
)
1278 filename
= (char *) xmalloc (strlen (search
->name
) + len
+ 2);
1279 sprintf (filename
, "%s/%s", search
->name
, l
->name
);
1281 if (ldelf_try_needed (&nn
, force
, is_linux
))
1292 einfo (_("%P: warning: %s, needed by %pB, not found "
1293 "(try using -rpath or -rpath-link)\n"),
1297 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1298 if (bfd_get_format (abfd
) == bfd_object
1299 && ((abfd
->flags
) & DYNAMIC
) != 0
1300 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1301 && (elf_dyn_lib_class (abfd
) & (DYN_AS_NEEDED
| DYN_NO_NEEDED
)) == 0
1302 && elf_dt_name (abfd
) != NULL
)
1304 if (bfd_elf_add_dt_needed_tag (abfd
, &link_info
) < 0)
1305 einfo (_("%F%P: failed to add DT_NEEDED dynamic tag\n"));
1308 link_info
.input_bfds_tail
= save_input_bfd_tail
;
1309 *save_input_bfd_tail
= NULL
;
1311 if (link_info
.eh_frame_hdr_type
== COMPACT_EH_HDR
)
1312 if (!bfd_elf_parse_eh_frame_entries (NULL
, &link_info
))
1313 einfo (_("%F%P: failed to parse EH frame entries\n"));
1316 static bfd_size_type
1317 id_note_section_size (bfd
*abfd ATTRIBUTE_UNUSED
)
1319 const char *style
= ldelf_emit_note_gnu_build_id
;
1321 bfd_size_type build_id_size
;
1323 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1324 size
= (size
+ 3) & -(bfd_size_type
) 4;
1326 build_id_size
= compute_build_id_size (style
);
1328 size
+= build_id_size
;
1336 write_build_id (bfd
*abfd
)
1338 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1339 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1342 Elf_Internal_Shdr
*i_shdr
;
1343 unsigned char *contents
, *id_bits
;
1346 Elf_External_Note
*e_note
;
1348 style
= t
->o
->build_id
.style
;
1349 asec
= t
->o
->build_id
.sec
;
1350 if (bfd_is_abs_section (asec
->output_section
))
1352 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
1353 " --build-id ignored\n"));
1356 i_shdr
= &elf_section_data (asec
->output_section
)->this_hdr
;
1358 if (i_shdr
->contents
== NULL
)
1360 if (asec
->contents
== NULL
)
1361 asec
->contents
= (unsigned char *) xmalloc (asec
->size
);
1362 contents
= asec
->contents
;
1365 contents
= i_shdr
->contents
+ asec
->output_offset
;
1367 e_note
= (Elf_External_Note
*) contents
;
1368 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1369 size
= (size
+ 3) & -(bfd_size_type
) 4;
1370 id_bits
= contents
+ size
;
1371 size
= asec
->size
- size
;
1373 bfd_h_put_32 (abfd
, sizeof "GNU", &e_note
->namesz
);
1374 bfd_h_put_32 (abfd
, size
, &e_note
->descsz
);
1375 bfd_h_put_32 (abfd
, NT_GNU_BUILD_ID
, &e_note
->type
);
1376 memcpy (e_note
->name
, "GNU", sizeof "GNU");
1378 generate_build_id (abfd
, style
, bed
->s
->checksum_contents
, id_bits
, size
);
1380 position
= i_shdr
->sh_offset
+ asec
->output_offset
;
1382 return (bfd_seek (abfd
, position
, SEEK_SET
) == 0
1383 && bfd_bwrite (contents
, size
, abfd
) == size
);
1386 /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
1389 ldelf_setup_build_id (bfd
*ibfd
)
1395 size
= id_note_section_size (ibfd
);
1398 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1402 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_IN_MEMORY
1403 | SEC_LINKER_CREATED
| SEC_READONLY
| SEC_DATA
);
1404 s
= bfd_make_section_with_flags (ibfd
, ".note.gnu.build-id", flags
);
1405 if (s
!= NULL
&& bfd_set_section_alignment (s
, 2))
1407 struct elf_obj_tdata
*t
= elf_tdata (link_info
.output_bfd
);
1408 t
->o
->build_id
.after_write_object_contents
= &write_build_id
;
1409 t
->o
->build_id
.style
= ldelf_emit_note_gnu_build_id
;
1410 t
->o
->build_id
.sec
= s
;
1411 elf_section_type (s
) = SHT_NOTE
;
1416 einfo (_("%P: warning: cannot create .note.gnu.build-id section,"
1417 " --build-id ignored\n"));
1421 /* Look through an expression for an assignment statement. */
1424 ldelf_find_exp_assignment (etree_type
*exp
)
1426 bfd_boolean provide
= FALSE
;
1428 switch (exp
->type
.node_class
)
1431 case etree_provided
:
1435 /* We call record_link_assignment even if the symbol is defined.
1436 This is because if it is defined by a dynamic object, we
1437 actually want to use the value defined by the linker script,
1438 not the value from the dynamic object (because we are setting
1439 symbols like etext). If the symbol is defined by a regular
1440 object, then, as it happens, calling record_link_assignment
1442 if (strcmp (exp
->assign
.dst
, ".") != 0)
1444 if (!bfd_elf_record_link_assignment (link_info
.output_bfd
,
1446 exp
->assign
.dst
, provide
,
1447 exp
->assign
.hidden
))
1448 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1451 ldelf_find_exp_assignment (exp
->assign
.src
);
1455 ldelf_find_exp_assignment (exp
->binary
.lhs
);
1456 ldelf_find_exp_assignment (exp
->binary
.rhs
);
1460 ldelf_find_exp_assignment (exp
->trinary
.cond
);
1461 ldelf_find_exp_assignment (exp
->trinary
.lhs
);
1462 ldelf_find_exp_assignment (exp
->trinary
.rhs
);
1466 ldelf_find_exp_assignment (exp
->unary
.child
);
1474 /* This is called by the before_allocation routine via
1475 lang_for_each_statement. It locates any assignment statements, and
1476 tells the ELF backend about them, in case they are assignments to
1477 symbols which are referred to by dynamic objects. */
1480 ldelf_find_statement_assignment (lang_statement_union_type
*s
)
1482 if (s
->header
.type
== lang_assignment_statement_enum
)
1483 ldelf_find_exp_assignment (s
->assignment_statement
.exp
);
1486 /* Used by before_allocation and handle_option. */
1489 ldelf_append_to_separated_string (char **to
, char *op_arg
)
1492 *to
= xstrdup (op_arg
);
1495 size_t to_len
= strlen (*to
);
1496 size_t op_arg_len
= strlen (op_arg
);
1500 /* First see whether OPTARG is already in the path. */
1503 if (strncmp (op_arg
, cp
, op_arg_len
) == 0
1504 && (cp
[op_arg_len
] == 0
1505 || cp
[op_arg_len
] == config
.rpath_separator
))
1509 /* Not yet found. */
1510 cp
= strchr (cp
, config
.rpath_separator
);
1518 buf
= xmalloc (to_len
+ op_arg_len
+ 2);
1519 sprintf (buf
, "%s%c%s", *to
,
1520 config
.rpath_separator
, op_arg
);
1527 /* This is called after the sections have been attached to output
1528 sections, but before any sizes or addresses have been set. */
1531 ldelf_before_allocation (char *audit
, char *depaudit
,
1532 const char *default_interpreter_name
)
1537 struct bfd_link_hash_entry
*ehdr_start
= NULL
;
1538 unsigned char ehdr_start_save_type
= 0;
1539 char ehdr_start_save_u
[sizeof ehdr_start
->u
1540 - sizeof ehdr_start
->u
.def
.next
] = "";
1542 if (is_elf_hash_table (link_info
.hash
))
1544 _bfd_elf_tls_setup (link_info
.output_bfd
, &link_info
);
1546 /* Make __ehdr_start hidden if it has been referenced, to
1547 prevent the symbol from being dynamic. */
1548 if (!bfd_link_relocatable (&link_info
))
1550 struct elf_link_hash_table
*htab
= elf_hash_table (&link_info
);
1551 struct elf_link_hash_entry
*h
1552 = elf_link_hash_lookup (htab
, "__ehdr_start", FALSE
, FALSE
, TRUE
);
1554 /* Only adjust the export class if the symbol was referenced
1555 and not defined, otherwise leave it alone. */
1557 && (h
->root
.type
== bfd_link_hash_new
1558 || h
->root
.type
== bfd_link_hash_undefined
1559 || h
->root
.type
== bfd_link_hash_undefweak
1560 || h
->root
.type
== bfd_link_hash_common
))
1562 const struct elf_backend_data
*bed
;
1563 bed
= get_elf_backend_data (link_info
.output_bfd
);
1564 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, TRUE
);
1565 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
1566 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
1567 /* Don't leave the symbol undefined. Undefined hidden
1568 symbols typically won't have dynamic relocations, but
1569 we most likely will need dynamic relocations for
1570 __ehdr_start if we are building a PIE or shared
1572 ehdr_start
= &h
->root
;
1573 ehdr_start_save_type
= ehdr_start
->type
;
1574 memcpy (ehdr_start_save_u
,
1575 (char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1576 sizeof ehdr_start_save_u
);
1577 ehdr_start
->type
= bfd_link_hash_defined
;
1578 ehdr_start
->u
.def
.section
= bfd_abs_section_ptr
;
1579 ehdr_start
->u
.def
.value
= 0;
1583 /* If we are going to make any variable assignments, we need to
1584 let the ELF backend know about them in case the variables are
1585 referred to by dynamic objects. */
1586 lang_for_each_statement (ldelf_find_statement_assignment
);
1589 /* Let the ELF backend work out the sizes of any sections required
1590 by dynamic linking. */
1591 rpath
= command_line
.rpath
;
1593 rpath
= (const char *) getenv ("LD_RUN_PATH");
1595 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1596 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1598 const char *audit_libs
= elf_dt_audit (abfd
);
1600 /* If the input bfd contains an audit entry, we need to add it as
1601 a dep audit entry. */
1602 if (audit_libs
&& *audit_libs
!= '\0')
1604 char *cp
= xstrdup (audit_libs
);
1608 char *cp2
= strchr (cp
, config
.rpath_separator
);
1616 if (cp
!= NULL
&& *cp
!= '\0')
1617 ldelf_append_to_separated_string (&depaudit
, cp
);
1619 cp
= more
? ++cp2
: NULL
;
1625 if (! (bfd_elf_size_dynamic_sections
1626 (link_info
.output_bfd
, command_line
.soname
, rpath
,
1627 command_line
.filter_shlib
, audit
, depaudit
,
1628 (const char * const *) command_line
.auxiliary_filters
,
1629 &link_info
, &sinterp
)))
1630 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1632 if (sinterp
!= NULL
)
1634 /* Let the user override the dynamic linker we are using. */
1635 if (command_line
.interpreter
!= NULL
)
1636 default_interpreter_name
= command_line
.interpreter
;
1637 if (default_interpreter_name
!= NULL
)
1639 sinterp
->contents
= (bfd_byte
*) default_interpreter_name
;
1640 sinterp
->size
= strlen ((char *) sinterp
->contents
) + 1;
1644 /* Look for any sections named .gnu.warning. As a GNU extensions,
1645 we treat such sections as containing warning messages. We print
1646 out the warning message, and then zero out the section size so
1647 that it does not get copied into the output file. */
1650 LANG_FOR_EACH_INPUT_STATEMENT (is
)
1656 if (is
->flags
.just_syms
)
1659 s
= bfd_get_section_by_name (is
->the_bfd
, ".gnu.warning");
1664 msg
= (char *) xmalloc ((size_t) (sz
+ 1));
1665 if (! bfd_get_section_contents (is
->the_bfd
, s
, msg
,
1667 einfo (_("%F%P: %pB: can't read contents of section .gnu.warning: %E\n"),
1670 (*link_info
.callbacks
->warning
) (&link_info
, msg
,
1671 (const char *) NULL
, is
->the_bfd
,
1672 (asection
*) NULL
, (bfd_vma
) 0);
1675 /* Clobber the section size, so that we don't waste space
1676 copying the warning into the output file. If we've already
1677 sized the output section, adjust its size. The adjustment
1678 is on rawsize because targets that size sections early will
1679 have called lang_reset_memory_regions after sizing. */
1680 if (s
->output_section
!= NULL
1681 && s
->output_section
->rawsize
>= s
->size
)
1682 s
->output_section
->rawsize
-= s
->size
;
1686 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1687 warning section don't get copied to the output. */
1688 s
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
1692 before_allocation_default ();
1694 if (!bfd_elf_size_dynsym_hash_dynstr (link_info
.output_bfd
, &link_info
))
1695 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1697 if (ehdr_start
!= NULL
)
1699 /* If we twiddled __ehdr_start to defined earlier, put it back
1701 ehdr_start
->type
= ehdr_start_save_type
;
1702 memcpy ((char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1704 sizeof ehdr_start_save_u
);
1707 /* Try to open a dynamic archive. This is where we know that ELF
1708 dynamic libraries have an extension of .so (or .sl on oddball systems
1712 ldelf_open_dynamic_archive (const char *arch
, search_dirs_type
*search
,
1713 lang_input_statement_type
*entry
)
1715 const char *filename
;
1718 bfd_boolean opened
= FALSE
;
1720 if (! entry
->flags
.maybe_archive
)
1723 filename
= entry
->filename
;
1724 len
= strlen (search
->name
) + strlen (filename
);
1725 if (entry
->flags
.full_name_provided
)
1728 string
= (char *) xmalloc (len
);
1729 sprintf (string
, "%s/%s", search
->name
, filename
);
1735 len
+= strlen (arch
) + sizeof "/lib.so";
1736 #ifdef EXTRA_SHLIB_EXTENSION
1737 xlen
= (strlen (EXTRA_SHLIB_EXTENSION
) > 3
1738 ? strlen (EXTRA_SHLIB_EXTENSION
) - 3
1741 string
= (char *) xmalloc (len
+ xlen
);
1742 sprintf (string
, "%s/lib%s%s.so", search
->name
, filename
, arch
);
1743 #ifdef EXTRA_SHLIB_EXTENSION
1744 /* Try the .so extension first. If that fails build a new filename
1745 using EXTRA_SHLIB_EXTENSION. */
1746 opened
= ldfile_try_open_bfd (string
, entry
);
1748 strcpy (string
+ len
- 4, EXTRA_SHLIB_EXTENSION
);
1752 if (!opened
&& !ldfile_try_open_bfd (string
, entry
))
1758 entry
->filename
= string
;
1760 /* We have found a dynamic object to include in the link. The ELF
1761 backend linker will create a DT_NEEDED entry in the .dynamic
1762 section naming this file. If this file includes a DT_SONAME
1763 entry, it will be used. Otherwise, the ELF linker will just use
1764 the name of the file. For an archive found by searching, like
1765 this one, the DT_NEEDED entry should consist of just the name of
1766 the file, without the path information used to find it. Note
1767 that we only need to do this if we have a dynamic object; an
1768 archive will never be referenced by a DT_NEEDED entry.
1770 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1771 very pretty. I haven't been able to think of anything that is
1773 if (bfd_check_format (entry
->the_bfd
, bfd_object
)
1774 && (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
1776 ASSERT (entry
->flags
.maybe_archive
&& entry
->flags
.search_dirs
);
1778 /* Rather than duplicating the logic above. Just use the
1779 filename we recorded earlier. */
1781 if (!entry
->flags
.full_name_provided
)
1782 filename
= lbasename (entry
->filename
);
1783 bfd_elf_set_dt_needed_name (entry
->the_bfd
, filename
);
1789 /* A variant of lang_output_section_find used by place_orphan. */
1791 static lang_output_section_statement_type
*
1792 output_rel_find (int isdyn
, int rela
)
1794 lang_output_section_statement_type
*lookup
;
1795 lang_output_section_statement_type
*last
= NULL
;
1796 lang_output_section_statement_type
*last_alloc
= NULL
;
1797 lang_output_section_statement_type
*last_ro_alloc
= NULL
;
1798 lang_output_section_statement_type
*last_rel
= NULL
;
1799 lang_output_section_statement_type
*last_rel_alloc
= NULL
;
1801 for (lookup
= (void *) lang_os_list
.head
;
1803 lookup
= lookup
->next
)
1805 if (lookup
->constraint
>= 0
1806 && CONST_STRNEQ (lookup
->name
, ".rel"))
1808 int lookrela
= lookup
->name
[4] == 'a';
1810 /* .rel.dyn must come before all other reloc sections, to suit
1815 /* Don't place after .rel.plt as doing so results in wrong
1817 if (strcmp (".plt", lookup
->name
+ 4 + lookrela
) == 0)
1820 if (rela
== lookrela
|| last_rel
== NULL
)
1822 if ((rela
== lookrela
|| last_rel_alloc
== NULL
)
1823 && lookup
->bfd_section
!= NULL
1824 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1825 last_rel_alloc
= lookup
;
1829 if (lookup
->bfd_section
!= NULL
1830 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1832 last_alloc
= lookup
;
1833 if ((lookup
->bfd_section
->flags
& SEC_READONLY
) != 0)
1834 last_ro_alloc
= lookup
;
1839 return last_rel_alloc
;
1845 return last_ro_alloc
;
1853 /* Return whether IN is suitable to be part of OUT. */
1856 elf_orphan_compatible (asection
*in
, asection
*out
)
1858 /* Non-zero sh_info implies a section with SHF_INFO_LINK with
1859 unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
1860 section where sh_info specifies a symbol table. (We won't see
1861 SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
1862 can't merge SHT_REL/SHT_RELA using differing symbol tables, and
1863 shouldn't merge sections with differing unknown semantics. */
1864 if (elf_section_data (out
)->this_hdr
.sh_info
1865 != elf_section_data (in
)->this_hdr
.sh_info
)
1867 /* We can't merge with a member of an output section group or merge
1868 two sections with differing SHF_EXCLUDE or other processor and OS
1869 specific flags when doing a relocatable link. */
1870 if (bfd_link_relocatable (&link_info
)
1871 && (elf_next_in_group (out
) != NULL
1872 || ((elf_section_flags (out
) ^ elf_section_flags (in
))
1873 & (SHF_MASKPROC
| SHF_MASKOS
)) != 0))
1875 return _bfd_elf_match_sections_by_type (link_info
.output_bfd
, out
,
1879 /* Place an orphan section. We use this to put random SHF_ALLOC
1880 sections in the right segment. */
1882 lang_output_section_statement_type
*
1883 ldelf_place_orphan (asection
*s
, const char *secname
, int constraint
)
1885 static struct orphan_save hold
[] =
1888 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
,
1891 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1894 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_THREAD_LOCAL
,
1897 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
,
1903 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1906 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1909 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_SMALL_DATA
,
1915 enum orphan_save_index
1927 static int orphan_init_done
= 0;
1928 struct orphan_save
*place
;
1929 lang_output_section_statement_type
*after
;
1930 lang_output_section_statement_type
*os
;
1931 lang_output_section_statement_type
*match_by_name
= NULL
;
1933 int elfinput
= s
->owner
->xvec
->flavour
== bfd_target_elf_flavour
;
1934 int elfoutput
= link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
;
1935 unsigned int sh_type
= elfinput
? elf_section_type (s
) : SHT_NULL
;
1939 if (!bfd_link_relocatable (&link_info
)
1940 && link_info
.combreloc
1941 && (s
->flags
& SEC_ALLOC
))
1947 secname
= ".rela.dyn";
1951 secname
= ".rel.dyn";
1957 else if (CONST_STRNEQ (secname
, ".rel"))
1959 secname
= secname
[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1964 if (!bfd_link_relocatable (&link_info
)
1967 && (s
->flags
& SEC_ALLOC
) != 0
1968 && (elf_tdata (s
->owner
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
1969 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
1971 /* Find the output mbind section with the same type, attributes
1972 and sh_info field. */
1973 for (os
= (void *) lang_os_list
.head
;
1976 if (os
->bfd_section
!= NULL
1977 && !bfd_is_abs_section (os
->bfd_section
)
1978 && (elf_section_flags (os
->bfd_section
) & SHF_GNU_MBIND
) != 0
1979 && ((s
->flags
& (SEC_ALLOC
1984 == (os
->bfd_section
->flags
& (SEC_ALLOC
1989 && (elf_section_data (os
->bfd_section
)->this_hdr
.sh_info
1990 == elf_section_data (s
)->this_hdr
.sh_info
))
1992 lang_add_section (&os
->children
, s
, NULL
, os
);
1996 /* Create the output mbind section with the ".mbind." prefix
1998 if ((s
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
1999 secname
= ".mbind.bss";
2000 else if ((s
->flags
& SEC_READONLY
) == 0)
2001 secname
= ".mbind.data";
2002 else if ((s
->flags
& SEC_CODE
) == 0)
2003 secname
= ".mbind.rodata";
2005 secname
= ".mbind.text";
2006 elf_tdata (link_info
.output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
2009 /* Look through the script to see where to place this section. The
2010 script includes entries added by previous lang_insert_orphan
2011 calls, so this loop puts multiple compatible orphans of the same
2012 name into a single output section. */
2013 if (constraint
== 0)
2014 for (os
= lang_output_section_find (secname
);
2016 os
= next_matching_output_section_statement (os
, 0))
2018 /* If we don't match an existing output section, tell
2019 lang_insert_orphan to create a new output section. */
2020 constraint
= SPECIAL
;
2022 /* Check to see if we already have an output section statement
2023 with this name, and its bfd section has compatible flags.
2024 If the section already exists but does not have any flags
2025 set, then it has been created by the linker, possibly as a
2026 result of a --section-start command line switch. */
2027 if (os
->bfd_section
!= NULL
2028 && (os
->bfd_section
->flags
== 0
2029 || (((s
->flags
^ os
->bfd_section
->flags
)
2030 & (SEC_LOAD
| SEC_ALLOC
)) == 0
2033 || elf_orphan_compatible (s
, os
->bfd_section
)))))
2035 lang_add_section (&os
->children
, s
, NULL
, os
);
2039 /* Save unused output sections in case we can match them
2040 against orphans later. */
2041 if (os
->bfd_section
== NULL
)
2045 /* If we didn't match an active output section, see if we matched an
2046 unused one and use that. */
2049 lang_add_section (&match_by_name
->children
, s
, NULL
, match_by_name
);
2050 return match_by_name
;
2053 if (!orphan_init_done
)
2055 struct orphan_save
*ho
;
2057 for (ho
= hold
; ho
< hold
+ sizeof (hold
) / sizeof (hold
[0]); ++ho
)
2058 if (ho
->name
!= NULL
)
2060 ho
->os
= lang_output_section_find (ho
->name
);
2061 if (ho
->os
!= NULL
&& ho
->os
->flags
== 0)
2062 ho
->os
->flags
= ho
->flags
;
2064 orphan_init_done
= 1;
2067 /* If this is a final link, then always put .gnu.warning.SYMBOL
2068 sections into the .text section to get them out of the way. */
2069 if (bfd_link_executable (&link_info
)
2070 && CONST_STRNEQ (s
->name
, ".gnu.warning.")
2071 && hold
[orphan_text
].os
!= NULL
)
2073 os
= hold
[orphan_text
].os
;
2074 lang_add_section (&os
->children
, s
, NULL
, os
);
2079 if (!bfd_link_relocatable (&link_info
))
2082 while ((nexts
= bfd_get_next_section_by_name (nexts
->owner
, nexts
))
2084 if (nexts
->output_section
== NULL
2085 && (nexts
->flags
& SEC_EXCLUDE
) == 0
2086 && ((nexts
->flags
^ flags
) & (SEC_LOAD
| SEC_ALLOC
)) == 0
2087 && (nexts
->owner
->flags
& DYNAMIC
) == 0
2088 && !bfd_input_just_syms (nexts
->owner
)
2089 && _bfd_elf_match_sections_by_type (nexts
->owner
, nexts
,
2091 flags
= (((flags
^ SEC_READONLY
)
2092 | (nexts
->flags
^ SEC_READONLY
))
2096 /* Decide which segment the section should go in based on the
2097 section name and section flags. We put loadable .note sections
2098 right after the .interp section, so that the PT_NOTE segment is
2099 stored right after the program headers where the OS can read it
2100 in the first page. */
2103 if ((flags
& (SEC_ALLOC
| SEC_DEBUGGING
)) == 0)
2104 place
= &hold
[orphan_nonalloc
];
2105 else if ((flags
& SEC_ALLOC
) == 0)
2107 else if ((flags
& SEC_LOAD
) != 0
2109 ? sh_type
== SHT_NOTE
2110 : CONST_STRNEQ (secname
, ".note")))
2111 place
= &hold
[orphan_interp
];
2112 else if ((flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_THREAD_LOCAL
)) == 0)
2113 place
= &hold
[orphan_bss
];
2114 else if ((flags
& SEC_SMALL_DATA
) != 0)
2115 place
= &hold
[orphan_sdata
];
2116 else if ((flags
& SEC_THREAD_LOCAL
) != 0)
2117 place
= &hold
[orphan_tdata
];
2118 else if ((flags
& SEC_READONLY
) == 0)
2119 place
= &hold
[orphan_data
];
2120 else if ((flags
& SEC_LOAD
) != 0
2122 ? sh_type
== SHT_RELA
|| sh_type
== SHT_REL
2123 : CONST_STRNEQ (secname
, ".rel")))
2124 place
= &hold
[orphan_rel
];
2125 else if ((flags
& SEC_CODE
) == 0)
2126 place
= &hold
[orphan_rodata
];
2128 place
= &hold
[orphan_text
];
2133 if (place
->os
== NULL
)
2135 if (place
->name
!= NULL
)
2136 place
->os
= lang_output_section_find (place
->name
);
2139 int rela
= elfinput
? sh_type
== SHT_RELA
: secname
[4] == 'a';
2140 place
->os
= output_rel_find (isdyn
, rela
);
2146 = lang_output_section_find_by_flags (s
, flags
, &place
->os
,
2147 _bfd_elf_match_sections_by_type
);
2149 /* *ABS* is always the first output section statement. */
2150 after
= (void *) lang_os_list
.head
;
2153 return lang_insert_orphan (s
, secname
, constraint
, after
, place
, NULL
, NULL
);
2157 ldelf_before_place_orphans (void)
2161 for (abfd
= link_info
.input_bfds
;
2162 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
2163 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
2164 && bfd_count_sections (abfd
) != 0
2165 && !bfd_input_just_syms (abfd
))
2168 for (isec
= abfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
2170 /* Discard a section if any of its linked-to section has
2172 asection
*linked_to_sec
;
2173 for (linked_to_sec
= elf_linked_to_section (isec
);
2174 linked_to_sec
!= NULL
;
2175 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
2176 if (discarded_section (linked_to_sec
))
2178 isec
->output_section
= bfd_abs_section_ptr
;