1 /* ELF emulation code for targets using elf.em.
2 Copyright (C) 1991-2022 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 bool 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;
75 /* Disable DT_RELR if not building PIE nor shared library. */
76 if (!bfd_link_pic (&link_info
))
77 link_info
.enable_dt_relr
= 0;
79 /* Add 3 spare tags for DT_RELR, DT_RELRSZ and DT_RELRENT. */
80 if (link_info
.enable_dt_relr
)
81 link_info
.spare_dynamic_tags
+= 3;
83 after_parse_default ();
84 if (link_info
.commonpagesize
> link_info
.maxpagesize
)
86 if (!link_info
.commonpagesize_is_set
)
87 link_info
.commonpagesize
= link_info
.maxpagesize
;
88 else if (!link_info
.maxpagesize_is_set
)
89 link_info
.maxpagesize
= link_info
.commonpagesize
;
91 einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"),
92 link_info
.commonpagesize
, link_info
.maxpagesize
);
96 /* Handle the generation of DT_NEEDED tags. */
99 ldelf_load_symbols (lang_input_statement_type
*entry
)
103 /* Tell the ELF linker that we don't want the output file to have a
104 DT_NEEDED entry for this file, unless it is used to resolve
105 references in a regular object. */
106 if (entry
->flags
.add_DT_NEEDED_for_regular
)
107 link_class
= DYN_AS_NEEDED
;
109 /* Tell the ELF linker that we don't want the output file to have a
110 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
112 if (!entry
->flags
.add_DT_NEEDED_for_dynamic
)
113 link_class
|= DYN_NO_ADD_NEEDED
;
115 if (entry
->flags
.just_syms
116 && (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) != 0)
117 einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
121 || (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) == 0)
124 bfd_elf_set_dyn_lib_class (entry
->the_bfd
,
125 (enum dynamic_lib_link_class
) link_class
);
127 /* Continue on with normal load_symbols processing. */
131 /* On Linux, it's possible to have different versions of the same
132 shared library linked against different versions of libc. The
133 dynamic linker somehow tags which libc version to use in
134 /etc/ld.so.cache, and, based on the libc that it sees in the
135 executable, chooses which version of the shared library to use.
137 We try to do a similar check here by checking whether this shared
138 library needs any other shared libraries which may conflict with
139 libraries we have already included in the link. If it does, we
140 skip it, and try to find another shared library farther on down the
143 This is called via lang_for_each_input_file.
144 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
145 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
146 a conflicting version. */
149 ldelf_vercheck (lang_input_statement_type
*s
)
152 struct bfd_link_needed_list
*l
;
154 if (global_vercheck_failed
)
156 if (s
->the_bfd
== NULL
157 || (bfd_get_file_flags (s
->the_bfd
) & DYNAMIC
) == 0)
160 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
162 soname
= lbasename (bfd_get_filename (s
->the_bfd
));
164 for (l
= global_vercheck_needed
; l
!= NULL
; l
= l
->next
)
168 if (filename_cmp (soname
, l
->name
) == 0)
170 /* Probably can't happen, but it's an easy check. */
174 if (strchr (l
->name
, '/') != NULL
)
177 suffix
= strstr (l
->name
, ".so.");
181 suffix
+= sizeof ".so." - 1;
183 if (filename_ncmp (soname
, l
->name
, suffix
- l
->name
) == 0)
185 /* Here we know that S is a dynamic object FOO.SO.VER1, and
186 the object we are considering needs a dynamic object
187 FOO.SO.VER2, and VER1 and VER2 are different. This
188 appears to be a version mismatch, so we tell the caller
189 to try a different version of this library. */
190 global_vercheck_failed
= true;
197 /* See if an input file matches a DT_NEEDED entry by running stat on
201 ldelf_stat_needed (lang_input_statement_type
*s
)
207 if (global_found
!= NULL
)
209 if (s
->the_bfd
== NULL
)
212 /* If this input file was an as-needed entry, and wasn't found to be
213 needed at the stage it was linked, then don't say we have loaded it. */
214 if ((bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
217 if (bfd_stat (s
->the_bfd
, &st
) != 0)
219 einfo (_("%P: %pB: bfd_stat failed: %E\n"), s
->the_bfd
);
223 /* Some operating systems, e.g. Windows, do not provide a meaningful
224 st_ino; they always set it to zero. (Windows does provide a
225 meaningful st_dev.) Do not indicate a duplicate library in that
226 case. While there is no guarantee that a system that provides
227 meaningful inode numbers will never set st_ino to zero, this is
228 merely an optimization, so we do not need to worry about false
230 if (st
.st_dev
== global_stat
.st_dev
231 && st
.st_ino
== global_stat
.st_ino
238 /* We issue a warning if it looks like we are including two
239 different versions of the same shared library. For example,
240 there may be a problem if -lc picks up libc.so.6 but some other
241 shared library has a DT_NEEDED entry of libc.so.5. This is a
242 heuristic test, and it will only work if the name looks like
243 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
244 If we really want to issue warnings about mixing version numbers
245 of shared libraries, we need to find a better way. */
247 if (strchr (global_needed
->name
, '/') != NULL
)
249 suffix
= strstr (global_needed
->name
, ".so.");
252 suffix
+= sizeof ".so." - 1;
254 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
256 soname
= lbasename (s
->filename
);
258 if (filename_ncmp (soname
, global_needed
->name
,
259 suffix
- global_needed
->name
) == 0)
260 einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
261 global_needed
->name
, global_needed
->by
, soname
);
264 /* This function is called for each possible name for a dynamic object
265 named by a DT_NEEDED entry. The FORCE parameter indicates whether
266 to skip the check for a conflicting version. */
269 ldelf_try_needed (struct dt_needed
*needed
, int force
, int is_linux
)
272 const char *name
= needed
->name
;
276 abfd
= bfd_openr (name
, bfd_get_target (link_info
.output_bfd
));
280 info_msg (_("attempt to open %s failed\n"), name
);
284 track_dependency_files (name
);
286 /* Linker needs to decompress sections. */
287 abfd
->flags
|= BFD_DECOMPRESS
;
289 if (! bfd_check_format (abfd
, bfd_object
))
294 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) == 0)
300 /* For DT_NEEDED, they have to match. */
301 if (abfd
->xvec
!= link_info
.output_bfd
->xvec
)
307 /* Check whether this object would include any conflicting library
308 versions. If FORCE is set, then we skip this check; we use this
309 the second time around, if we couldn't find any compatible
310 instance of the shared library. */
314 struct bfd_link_needed_list
*needs
;
316 if (! bfd_elf_get_bfd_needed_list (abfd
, &needs
))
317 einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd
);
321 global_vercheck_needed
= needs
;
322 global_vercheck_failed
= false;
323 lang_for_each_input_file (ldelf_vercheck
);
324 if (global_vercheck_failed
)
327 /* Return FALSE to force the caller to move on to try
328 another file on the search path. */
332 /* But wait! It gets much worse. On Linux, if a shared
333 library does not use libc at all, we are supposed to skip
334 it the first time around in case we encounter a shared
335 library later on with the same name which does use the
336 version of libc that we want. This is much too horrible
337 to use on any system other than Linux. */
340 struct bfd_link_needed_list
*l
;
342 for (l
= needs
; l
!= NULL
; l
= l
->next
)
343 if (startswith (l
->name
, "libc.so"))
354 /* We've found a dynamic object matching the DT_NEEDED entry. */
356 /* We have already checked that there is no other input file of the
357 same name. We must now check again that we are not including the
358 same file twice. We need to do this because on many systems
359 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
360 reference libc.so.1. If we have already included libc.so, we
361 don't want to include libc.so.1 if they are the same file, and we
362 can only check that using stat. */
364 if (bfd_stat (abfd
, &global_stat
) != 0)
365 einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd
);
367 /* First strip off everything before the last '/'. */
368 soname
= lbasename (bfd_get_filename (abfd
));
371 info_msg (_("found %s at %s\n"), soname
, name
);
374 lang_for_each_input_file (ldelf_stat_needed
);
375 if (global_found
!= NULL
)
377 /* Return TRUE to indicate that we found the file, even though
378 we aren't going to do anything with it. */
382 /* Specify the soname to use. */
383 bfd_elf_set_dt_needed_name (abfd
, soname
);
385 /* Tell the ELF linker that we don't want the output file to have a
386 DT_NEEDED entry for this file, unless it is used to resolve
387 references in a regular object. */
388 link_class
= DYN_DT_NEEDED
;
390 /* Tell the ELF linker that we don't want the output file to have a
391 DT_NEEDED entry for this file at all if the entry is from a file
392 with DYN_NO_ADD_NEEDED. */
393 if (needed
->by
!= NULL
394 && (bfd_elf_get_dyn_lib_class (needed
->by
) & DYN_NO_ADD_NEEDED
) != 0)
395 link_class
|= DYN_NO_NEEDED
| DYN_NO_ADD_NEEDED
;
397 bfd_elf_set_dyn_lib_class (abfd
, (enum dynamic_lib_link_class
) link_class
);
399 *link_info
.input_bfds_tail
= abfd
;
400 link_info
.input_bfds_tail
= &abfd
->link
.next
;
402 /* Add this file into the symbol table. */
403 if (! bfd_link_add_symbols (abfd
, &link_info
))
404 einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd
);
409 /* Search for a needed file in a path. */
412 ldelf_search_needed (const char *path
, struct dt_needed
*n
, int force
,
413 int is_linux
, int elfsize
)
416 const char *name
= n
->name
;
418 struct dt_needed needed
;
421 return ldelf_try_needed (n
, force
, is_linux
);
423 if (path
== NULL
|| *path
== '\0')
427 needed
.name
= n
->name
;
434 char *filename
, *sset
;
436 s
= strchr (path
, config
.rpath_separator
);
438 s
= path
+ strlen (path
);
440 #if HAVE_DOS_BASED_FILE_SYSTEM
441 /* Assume a match on the second char is part of drive specifier. */
442 else if (config
.rpath_separator
== ':'
446 s
= strchr (s
+ 1, config
.rpath_separator
);
448 s
= path
+ strlen (path
);
451 filename
= (char *) xmalloc (s
- path
+ len
+ 2);
456 memcpy (filename
, path
, s
- path
);
457 filename
[s
- path
] = '/';
458 sset
= filename
+ (s
- path
) + 1;
462 /* PR 20535: Support the same pseudo-environment variables that
463 are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
464 Since there can be more than one occurrence of these tokens in
465 the path we loop until no more are found. Since we might not
466 be able to substitute some of the tokens we maintain an offset
467 into the filename for where we should begin our scan. */
468 while ((var
= strchr (filename
+ offset
, '$')) != NULL
)
470 /* The ld.so manual page does not say, but I am going to assume that
471 these tokens are terminated by a directory separator character
472 (/) or the end of the string. There is also an implication that
473 $ORIGIN should only be used at the start of a path, but that is
476 The ld.so manual page also states that it allows ${ORIGIN},
477 ${LIB} and ${PLATFORM}, so these are supported as well.
479 FIXME: The code could be a lot cleverer about allocating space
480 for the processed string. */
481 char * end
= strchr (var
, '/');
482 const char *replacement
= NULL
;
484 char * freeme
= NULL
;
485 unsigned flen
= strlen (filename
);
488 /* Temporarily terminate the filename at the end of the token. */
496 if (strcmp (v
, "RIGIN") == 0 || strcmp (v
, "RIGIN}") == 0)
498 /* ORIGIN - replace with the full path to the directory
499 containing the program or shared object. */
500 if (needed
.by
== NULL
)
502 if (link_info
.output_bfd
== NULL
)
507 replacement
= bfd_get_filename (link_info
.output_bfd
);
510 replacement
= bfd_get_filename (needed
.by
);
516 if (replacement
[0] == '/')
517 freeme
= xstrdup (replacement
);
520 char * current_dir
= getpwd ();
522 freeme
= xmalloc (strlen (replacement
)
523 + strlen (current_dir
) + 2);
524 sprintf (freeme
, "%s/%s", current_dir
, replacement
);
527 replacement
= freeme
;
528 if ((slash
= strrchr (replacement
, '/')) != NULL
)
535 if (strcmp (v
, "IB") == 0 || strcmp (v
, "IB}") == 0)
537 /* LIB - replace with "lib" in 32-bit environments
538 and "lib64" in 64-bit environments. */
542 case 32: replacement
= "lib"; break;
543 case 64: replacement
= "lib64"; break;
551 /* Supporting $PLATFORM in a cross-hosted environment is not
552 possible. Supporting it in a native environment involves
553 loading the <sys/auxv.h> header file which loads the
554 system <elf.h> header file, which conflicts with the
555 "include/elf/mips.h" header file. */
563 char * filename2
= xmalloc (flen
+ strlen (replacement
));
567 sprintf (filename2
, "%.*s%s/%s",
568 (int)(var
- filename
), filename
,
569 replacement
, end
+ 1);
570 offset
= (var
- filename
) + 1 + strlen (replacement
);
574 sprintf (filename2
, "%.*s%s",
575 (int)(var
- filename
), filename
,
577 offset
= var
- filename
+ strlen (replacement
);
581 filename
= filename2
;
582 /* There is no need to restore the path separator (when
583 end != NULL) as we have replaced the entire string. */
588 /* We only issue an "unrecognised" message in verbose mode
589 as the $<foo> token might be a legitimate component of
590 a path name in the target's file system. */
591 info_msg (_("unrecognised or unsupported token "
592 "'%s' in search path\n"), var
);
594 /* Restore the path separator. */
597 /* PR 20784: Make sure that we resume the scan *after*
598 the token that we could not replace. */
599 offset
= (var
+ 1) - filename
;
605 needed
.name
= filename
;
607 if (ldelf_try_needed (&needed
, force
, is_linux
))
620 /* Prefix the sysroot to absolute paths in PATH, a string containing
621 paths separated by config.rpath_separator. If running on a DOS
622 file system, paths containing a drive spec won't have the sysroot
623 prefix added, unless the sysroot also specifies the same drive. */
626 ldelf_add_sysroot (const char *path
)
631 int dos_drive_sysroot
= HAS_DRIVE_SPEC (ld_sysroot
);
633 len
= strlen (ld_sysroot
);
634 for (extra
= 0, p
= path
; ; )
636 int dos_drive
= HAS_DRIVE_SPEC (p
);
640 if (IS_DIR_SEPARATOR (*p
)
642 || (dos_drive_sysroot
643 && ld_sysroot
[0] == p
[-2])))
645 if (dos_drive
&& dos_drive_sysroot
)
650 p
= strchr (p
, config
.rpath_separator
);
656 ret
= xmalloc (strlen (path
) + extra
+ 1);
658 for (q
= ret
, p
= path
; ; )
661 int dos_drive
= HAS_DRIVE_SPEC (p
);
668 if (IS_DIR_SEPARATOR (*p
)
670 || (dos_drive_sysroot
671 && ld_sysroot
[0] == p
[-2])))
673 if (dos_drive
&& dos_drive_sysroot
)
675 strcpy (q
, ld_sysroot
+ 2);
680 strcpy (q
, ld_sysroot
);
684 end
= strchr (p
, config
.rpath_separator
);
687 size_t n
= end
- p
+ 1;
702 /* Read the system search path the FreeBSD way rather than the Linux way. */
703 #ifdef HAVE_ELF_HINTS_H
704 #include <elf-hints.h>
706 #include "elf-hints-local.h"
710 ldelf_check_ld_elf_hints (const struct bfd_link_needed_list
*l
, int force
,
713 static bool initialized
;
714 static const char *ld_elf_hints
;
715 struct dt_needed needed
;
722 tmppath
= concat (ld_sysroot
, _PATH_ELF_HINTS
, (const char *) NULL
);
723 f
= fopen (tmppath
, FOPEN_RB
);
727 struct elfhints_hdr hdr
;
729 if (fread (&hdr
, 1, sizeof (hdr
), f
) == sizeof (hdr
)
730 && hdr
.magic
== ELFHINTS_MAGIC
733 if (fseek (f
, hdr
.strtab
+ hdr
.dirlist
, SEEK_SET
) != -1)
737 b
= xmalloc (hdr
.dirlistlen
+ 1);
738 if (fread (b
, 1, hdr
.dirlistlen
+ 1, f
) ==
740 ld_elf_hints
= ldelf_add_sysroot (b
);
751 if (ld_elf_hints
== NULL
)
755 needed
.name
= l
->name
;
756 return ldelf_search_needed (ld_elf_hints
, &needed
, force
, false, elfsize
);
759 /* For a native linker, check the file /etc/ld.so.conf for directories
760 in which we may find shared libraries. /etc/ld.so.conf is really
761 only meaningful on Linux. */
763 struct ldelf_ld_so_conf
770 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*, const char *);
773 ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf
*info
,
774 const char *filename
,
782 if (pattern
[0] != '/')
784 char *p
= strrchr (filename
, '/');
785 size_t patlen
= strlen (pattern
) + 1;
787 newp
= xmalloc (p
- filename
+ 1 + patlen
);
788 memcpy (newp
, filename
, p
- filename
+ 1);
789 memcpy (newp
+ (p
- filename
+ 1), pattern
, patlen
);
794 if (glob (pattern
, 0, NULL
, &gl
) == 0)
798 for (i
= 0; i
< gl
.gl_pathc
; ++i
)
799 ldelf_parse_ld_so_conf (info
, gl
.gl_pathv
[i
]);
803 /* If we do not have glob, treat the pattern as a literal filename. */
804 ldelf_parse_ld_so_conf (info
, pattern
);
811 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*info
, const char *filename
)
813 FILE *f
= fopen (filename
, FOPEN_RT
);
821 line
= xmalloc (linelen
);
826 /* Normally this would use getline(3), but we need to be portable. */
827 while ((q
= fgets (p
, linelen
- (p
- line
), f
)) != NULL
828 && strlen (q
) == linelen
- (p
- line
) - 1
829 && line
[linelen
- 2] != '\n')
831 line
= xrealloc (line
, 2 * linelen
);
832 p
= line
+ linelen
- 1;
836 if (q
== NULL
&& p
== line
)
839 p
= strchr (line
, '\n');
843 /* Because the file format does not know any form of quoting we
844 can search forward for the next '#' character and if found
845 make it terminating the line. */
846 p
= strchr (line
, '#');
850 /* Remove leading whitespace. NUL is no whitespace character. */
852 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
855 /* If the line is blank it is ignored. */
859 if (startswith (p
, "include") && (p
[7] == ' ' || p
[7] == '\t'))
865 while (*p
== ' ' || *p
== '\t')
873 while (*p
!= ' ' && *p
!= '\t' && *p
)
879 ldelf_parse_ld_so_conf_include (info
, filename
, dir
);
886 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
887 && *p
!= '\r' && *p
!= '\v')
890 while (p
!= dir
&& p
[-1] == '/')
892 if (info
->path
== NULL
)
894 info
->alloc
= p
- dir
+ 1 + 256;
895 info
->path
= xmalloc (info
->alloc
);
900 if (info
->len
+ 1 + (p
- dir
) >= info
->alloc
)
902 info
->alloc
+= p
- dir
+ 256;
903 info
->path
= xrealloc (info
->path
, info
->alloc
);
905 info
->path
[info
->len
++] = config
.rpath_separator
;
907 memcpy (info
->path
+ info
->len
, dir
, p
- dir
);
908 info
->len
+= p
- dir
;
909 info
->path
[info
->len
] = '\0';
919 ldelf_check_ld_so_conf (const struct bfd_link_needed_list
*l
, int force
,
920 int elfsize
, const char *prefix
)
922 static bool initialized
;
923 static const char *ld_so_conf
;
924 struct dt_needed needed
;
929 struct ldelf_ld_so_conf info
;
932 info
.len
= info
.alloc
= 0;
933 tmppath
= concat (ld_sysroot
, prefix
, "/etc/ld.so.conf",
934 (const char *) NULL
);
935 if (!ldelf_parse_ld_so_conf (&info
, tmppath
))
938 tmppath
= concat (ld_sysroot
, "/etc/ld.so.conf",
939 (const char *) NULL
);
940 ldelf_parse_ld_so_conf (&info
, tmppath
);
946 ld_so_conf
= ldelf_add_sysroot (info
.path
);
952 if (ld_so_conf
== NULL
)
957 needed
.name
= l
->name
;
958 return ldelf_search_needed (ld_so_conf
, &needed
, force
, true, elfsize
);
961 /* See if an input file matches a DT_NEEDED entry by name. */
964 ldelf_check_needed (lang_input_statement_type
*s
)
968 /* Stop looking if we've found a loaded lib. */
969 if (global_found
!= NULL
970 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
971 & DYN_AS_NEEDED
) == 0)
974 if (s
->filename
== NULL
|| s
->the_bfd
== NULL
)
977 /* Don't look for a second non-loaded as-needed lib. */
978 if (global_found
!= NULL
979 && (bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
982 if (filename_cmp (s
->filename
, global_needed
->name
) == 0)
988 if (s
->flags
.search_dirs
)
990 const char *f
= strrchr (s
->filename
, '/');
992 && filename_cmp (f
+ 1, global_needed
->name
) == 0)
999 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
1001 && filename_cmp (soname
, global_needed
->name
) == 0)
1008 /* This is called after all the input files have been opened. */
1011 ldelf_after_open (int use_libpath
, int native
, int is_linux
, int is_freebsd
,
1012 int elfsize
, const char *prefix
)
1014 struct bfd_link_needed_list
*needed
, *l
;
1015 struct elf_link_hash_table
*htab
;
1018 bfd
**save_input_bfd_tail
;
1020 after_open_default ();
1022 htab
= elf_hash_table (&link_info
);
1023 if (!is_elf_hash_table (&htab
->root
))
1026 if (command_line
.out_implib_filename
)
1028 unlink_if_ordinary (command_line
.out_implib_filename
);
1029 link_info
.out_implib_bfd
1030 = bfd_openw (command_line
.out_implib_filename
,
1031 bfd_get_target (link_info
.output_bfd
));
1033 if (link_info
.out_implib_bfd
== NULL
)
1035 einfo (_("%F%P: %s: can't open for writing: %E\n"),
1036 command_line
.out_implib_filename
);
1040 if (ldelf_emit_note_gnu_build_id
!= NULL
)
1042 /* Find an ELF input. */
1043 for (abfd
= link_info
.input_bfds
;
1044 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
1045 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1046 && bfd_count_sections (abfd
) != 0
1047 && !bfd_input_just_syms (abfd
))
1050 /* PR 10555: If there are no ELF input files do not try to
1051 create a .note.gnu-build-id section. */
1053 || !ldelf_setup_build_id (abfd
))
1055 free ((char *) ldelf_emit_note_gnu_build_id
);
1056 ldelf_emit_note_gnu_build_id
= NULL
;
1060 get_elf_backend_data (link_info
.output_bfd
)->setup_gnu_properties (&link_info
);
1062 /* Do not allow executable files to be used as inputs to the link. */
1063 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1065 /* Discard input .note.gnu.build-id sections. */
1066 s
= bfd_get_section_by_name (abfd
, ".note.gnu.build-id");
1069 if (s
!= elf_tdata (link_info
.output_bfd
)->o
->build_id
.sec
)
1070 s
->flags
|= SEC_EXCLUDE
;
1071 s
= bfd_get_next_section_by_name (NULL
, s
);
1074 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
1075 && !bfd_input_just_syms (abfd
)
1076 && elf_tdata (abfd
) != NULL
1077 /* FIXME: Maybe check for other non-supportable types as well ? */
1078 && (elf_tdata (abfd
)->elf_header
->e_type
== ET_EXEC
1079 || (elf_tdata (abfd
)->elf_header
->e_type
== ET_DYN
1080 && elf_tdata (abfd
)->is_pie
)))
1081 einfo (_("%F%P: cannot use executable file '%pB' as input to a link\n"),
1085 if (bfd_link_relocatable (&link_info
))
1087 if (link_info
.execstack
== !link_info
.noexecstack
)
1089 /* PR ld/16744: If "-z [no]execstack" has been specified on the
1090 command line and we are perfoming a relocatable link then no
1091 PT_GNU_STACK segment will be created and so the
1092 linkinfo.[no]execstack values set in _handle_option() will have no
1093 effect. Instead we create a .note.GNU-stack section in much the
1094 same way as the assembler does with its --[no]execstack option. */
1095 flagword flags
= SEC_READONLY
| (link_info
.execstack
? SEC_CODE
: 0);
1096 (void) bfd_make_section_with_flags (link_info
.input_bfds
,
1097 ".note.GNU-stack", flags
);
1102 if (!link_info
.traditional_format
)
1105 bool warn_eh_frame
= false;
1108 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1112 if (bfd_input_just_syms (abfd
))
1115 for (s
= abfd
->sections
; s
&& type
< COMPACT_EH_HDR
; s
= s
->next
)
1117 const char *name
= bfd_section_name (s
);
1119 if (bfd_is_abs_section (s
->output_section
))
1121 if (startswith (name
, ".eh_frame_entry"))
1122 type
= COMPACT_EH_HDR
;
1123 else if (strcmp (name
, ".eh_frame") == 0 && s
->size
> 8)
1124 type
= DWARF2_EH_HDR
;
1133 else if (seen_type
!= type
)
1135 einfo (_("%F%P: compact frame descriptions incompatible with"
1136 " DWARF2 .eh_frame from %pB\n"),
1137 type
== DWARF2_EH_HDR
? abfd
: elfbfd
);
1142 && (type
== COMPACT_EH_HDR
1143 || link_info
.eh_frame_hdr_type
!= 0))
1145 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1148 warn_eh_frame
= true;
1152 if (seen_type
== COMPACT_EH_HDR
)
1153 link_info
.eh_frame_hdr_type
= COMPACT_EH_HDR
;
1157 const struct elf_backend_data
*bed
;
1159 bed
= get_elf_backend_data (elfbfd
);
1160 s
= bfd_make_section_with_flags (elfbfd
, ".eh_frame_hdr",
1161 bed
->dynamic_sec_flags
1164 && bfd_set_section_alignment (s
, 2))
1166 htab
->eh_info
.hdr_sec
= s
;
1167 warn_eh_frame
= false;
1171 einfo (_("%P: warning: cannot create .eh_frame_hdr section,"
1172 " --eh-frame-hdr ignored\n"));
1175 /* Get the list of files which appear in DT_NEEDED entries in
1176 dynamic objects included in the link (often there will be none).
1177 For each such file, we want to track down the corresponding
1178 library, and include the symbol table in the link. This is what
1179 the runtime dynamic linker will do. Tracking the files down here
1180 permits one dynamic object to include another without requiring
1181 special action by the person doing the link. Note that the
1182 needed list can actually grow while we are stepping through this
1184 save_input_bfd_tail
= link_info
.input_bfds_tail
;
1185 needed
= bfd_elf_get_needed_list (link_info
.output_bfd
, &link_info
);
1186 for (l
= needed
; l
!= NULL
; l
= l
->next
)
1188 struct bfd_link_needed_list
*ll
;
1189 struct dt_needed n
, nn
;
1192 /* If the lib that needs this one was --as-needed and wasn't
1193 found to be needed, then this lib isn't needed either. */
1195 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_AS_NEEDED
) != 0)
1198 /* Skip the lib if --no-copy-dt-needed-entries and
1199 --allow-shlib-undefined is in effect. */
1201 && link_info
.unresolved_syms_in_shared_libs
== RM_IGNORE
1202 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_NO_ADD_NEEDED
) != 0)
1205 /* If we've already seen this file, skip it. */
1206 for (ll
= needed
; ll
!= l
; ll
= ll
->next
)
1208 || (bfd_elf_get_dyn_lib_class (ll
->by
) & DYN_AS_NEEDED
) == 0)
1209 && strcmp (ll
->name
, l
->name
) == 0)
1214 /* See if this file was included in the link explicitly. */
1216 global_found
= NULL
;
1217 lang_for_each_input_file (ldelf_check_needed
);
1218 if (global_found
!= NULL
1219 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
1220 & DYN_AS_NEEDED
) == 0)
1227 info_msg (_("%s needed by %pB\n"), l
->name
, l
->by
);
1229 /* As-needed libs specified on the command line (or linker script)
1230 take priority over libs found in search dirs. */
1231 if (global_found
!= NULL
)
1233 nn
.name
= global_found
->filename
;
1234 if (ldelf_try_needed (&nn
, true, is_linux
))
1238 /* We need to find this file and include the symbol table. We
1239 want to search for the file in the same way that the dynamic
1240 linker will search. That means that we want to use
1241 rpath_link, rpath, then the environment variable
1242 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1243 entries (native only), then the linker script LIB_SEARCH_DIRS.
1244 We do not search using the -L arguments.
1246 We search twice. The first time, we skip objects which may
1247 introduce version mismatches. The second time, we force
1248 their use. See ldelf_vercheck comment. */
1249 for (force
= 0; force
< 2; force
++)
1252 search_dirs_type
*search
;
1254 struct bfd_link_needed_list
*rp
;
1257 if (ldelf_search_needed (command_line
.rpath_link
, &n
, force
,
1263 path
= command_line
.rpath
;
1266 path
= ldelf_add_sysroot (path
);
1267 found
= ldelf_search_needed (path
, &n
, force
,
1269 free ((char *) path
);
1276 if (command_line
.rpath_link
== NULL
1277 && command_line
.rpath
== NULL
)
1279 path
= (const char *) getenv ("LD_RUN_PATH");
1281 && ldelf_search_needed (path
, &n
, force
,
1285 path
= (const char *) getenv ("LD_LIBRARY_PATH");
1287 && ldelf_search_needed (path
, &n
, force
,
1294 rp
= bfd_elf_get_runpath_list (link_info
.output_bfd
, &link_info
);
1295 for (; !found
&& rp
!= NULL
; rp
= rp
->next
)
1297 path
= ldelf_add_sysroot (rp
->name
);
1298 found
= (rp
->by
== l
->by
1299 && ldelf_search_needed (path
, &n
, force
,
1300 is_linux
, elfsize
));
1301 free ((char *) path
);
1307 && ldelf_check_ld_elf_hints (l
, force
, elfsize
))
1311 && ldelf_check_ld_so_conf (l
, force
, elfsize
, prefix
))
1315 len
= strlen (l
->name
);
1316 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
1320 if (search
->cmdline
)
1322 filename
= (char *) xmalloc (strlen (search
->name
) + len
+ 2);
1323 sprintf (filename
, "%s/%s", search
->name
, l
->name
);
1325 if (ldelf_try_needed (&nn
, force
, is_linux
))
1336 einfo (_("%P: warning: %s, needed by %pB, not found "
1337 "(try using -rpath or -rpath-link)\n"),
1341 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1342 if (bfd_get_format (abfd
) == bfd_object
1343 && ((abfd
->flags
) & DYNAMIC
) != 0
1344 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1345 && (elf_dyn_lib_class (abfd
) & (DYN_AS_NEEDED
| DYN_NO_NEEDED
)) == 0
1346 && elf_dt_name (abfd
) != NULL
)
1348 if (bfd_elf_add_dt_needed_tag (abfd
, &link_info
) < 0)
1349 einfo (_("%F%P: failed to add DT_NEEDED dynamic tag\n"));
1352 link_info
.input_bfds_tail
= save_input_bfd_tail
;
1353 *save_input_bfd_tail
= NULL
;
1355 if (link_info
.eh_frame_hdr_type
== COMPACT_EH_HDR
)
1356 if (!bfd_elf_parse_eh_frame_entries (NULL
, &link_info
))
1357 einfo (_("%F%P: failed to parse EH frame entries\n"));
1360 static bfd_size_type
1361 id_note_section_size (bfd
*abfd ATTRIBUTE_UNUSED
)
1363 const char *style
= ldelf_emit_note_gnu_build_id
;
1365 bfd_size_type build_id_size
;
1367 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1368 size
= (size
+ 3) & -(bfd_size_type
) 4;
1370 build_id_size
= compute_build_id_size (style
);
1372 size
+= build_id_size
;
1380 write_build_id (bfd
*abfd
)
1382 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1383 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1386 Elf_Internal_Shdr
*i_shdr
;
1387 unsigned char *contents
, *id_bits
;
1390 Elf_External_Note
*e_note
;
1392 style
= t
->o
->build_id
.style
;
1393 asec
= t
->o
->build_id
.sec
;
1394 if (bfd_is_abs_section (asec
->output_section
))
1396 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
1397 " --build-id ignored\n"));
1400 i_shdr
= &elf_section_data (asec
->output_section
)->this_hdr
;
1402 if (i_shdr
->contents
== NULL
)
1404 if (asec
->contents
== NULL
)
1405 asec
->contents
= (unsigned char *) xmalloc (asec
->size
);
1406 contents
= asec
->contents
;
1409 contents
= i_shdr
->contents
+ asec
->output_offset
;
1411 e_note
= (Elf_External_Note
*) contents
;
1412 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1413 size
= (size
+ 3) & -(bfd_size_type
) 4;
1414 id_bits
= contents
+ size
;
1415 size
= asec
->size
- size
;
1417 /* Clear the build ID field. */
1418 memset (id_bits
, 0, size
);
1420 bfd_h_put_32 (abfd
, sizeof "GNU", &e_note
->namesz
);
1421 bfd_h_put_32 (abfd
, size
, &e_note
->descsz
);
1422 bfd_h_put_32 (abfd
, NT_GNU_BUILD_ID
, &e_note
->type
);
1423 memcpy (e_note
->name
, "GNU", sizeof "GNU");
1425 generate_build_id (abfd
, style
, bed
->s
->checksum_contents
, id_bits
, size
);
1427 position
= i_shdr
->sh_offset
+ asec
->output_offset
;
1429 return (bfd_seek (abfd
, position
, SEEK_SET
) == 0
1430 && bfd_bwrite (contents
, size
, abfd
) == size
);
1433 /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
1436 ldelf_setup_build_id (bfd
*ibfd
)
1442 size
= id_note_section_size (ibfd
);
1445 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1449 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_IN_MEMORY
1450 | SEC_LINKER_CREATED
| SEC_READONLY
| SEC_DATA
);
1451 s
= bfd_make_section_anyway_with_flags (ibfd
, ".note.gnu.build-id",
1453 if (s
!= NULL
&& bfd_set_section_alignment (s
, 2))
1455 struct elf_obj_tdata
*t
= elf_tdata (link_info
.output_bfd
);
1456 t
->o
->build_id
.after_write_object_contents
= &write_build_id
;
1457 t
->o
->build_id
.style
= ldelf_emit_note_gnu_build_id
;
1458 t
->o
->build_id
.sec
= s
;
1459 elf_section_type (s
) = SHT_NOTE
;
1464 einfo (_("%P: warning: cannot create .note.gnu.build-id section,"
1465 " --build-id ignored\n"));
1469 /* Look through an expression for an assignment statement. */
1472 ldelf_find_exp_assignment (etree_type
*exp
)
1474 bool provide
= false;
1476 switch (exp
->type
.node_class
)
1479 case etree_provided
:
1483 /* We call record_link_assignment even if the symbol is defined.
1484 This is because if it is defined by a dynamic object, we
1485 actually want to use the value defined by the linker script,
1486 not the value from the dynamic object (because we are setting
1487 symbols like etext). If the symbol is defined by a regular
1488 object, then, as it happens, calling record_link_assignment
1490 if (strcmp (exp
->assign
.dst
, ".") != 0)
1492 if (!bfd_elf_record_link_assignment (link_info
.output_bfd
,
1494 exp
->assign
.dst
, provide
,
1495 exp
->assign
.hidden
))
1496 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1499 ldelf_find_exp_assignment (exp
->assign
.src
);
1503 ldelf_find_exp_assignment (exp
->binary
.lhs
);
1504 ldelf_find_exp_assignment (exp
->binary
.rhs
);
1508 ldelf_find_exp_assignment (exp
->trinary
.cond
);
1509 ldelf_find_exp_assignment (exp
->trinary
.lhs
);
1510 ldelf_find_exp_assignment (exp
->trinary
.rhs
);
1514 ldelf_find_exp_assignment (exp
->unary
.child
);
1522 /* This is called by the before_allocation routine via
1523 lang_for_each_statement. It locates any assignment statements, and
1524 tells the ELF backend about them, in case they are assignments to
1525 symbols which are referred to by dynamic objects. */
1528 ldelf_find_statement_assignment (lang_statement_union_type
*s
)
1530 if (s
->header
.type
== lang_assignment_statement_enum
)
1531 ldelf_find_exp_assignment (s
->assignment_statement
.exp
);
1534 /* Used by before_allocation and handle_option. */
1537 ldelf_append_to_separated_string (char **to
, char *op_arg
)
1540 *to
= xstrdup (op_arg
);
1543 size_t to_len
= strlen (*to
);
1544 size_t op_arg_len
= strlen (op_arg
);
1548 /* First see whether OPTARG is already in the path. */
1551 if (strncmp (op_arg
, cp
, op_arg_len
) == 0
1552 && (cp
[op_arg_len
] == 0
1553 || cp
[op_arg_len
] == config
.rpath_separator
))
1557 /* Not yet found. */
1558 cp
= strchr (cp
, config
.rpath_separator
);
1566 buf
= xmalloc (to_len
+ op_arg_len
+ 2);
1567 sprintf (buf
, "%s%c%s", *to
,
1568 config
.rpath_separator
, op_arg
);
1575 /* This is called after the sections have been attached to output
1576 sections, but before any sizes or addresses have been set. */
1579 ldelf_before_allocation (char *audit
, char *depaudit
,
1580 const char *default_interpreter_name
)
1585 struct bfd_link_hash_entry
*ehdr_start
= NULL
;
1586 unsigned char ehdr_start_save_type
= 0;
1587 char ehdr_start_save_u
[sizeof ehdr_start
->u
1588 - sizeof ehdr_start
->u
.def
.next
] = "";
1590 if (is_elf_hash_table (link_info
.hash
))
1592 _bfd_elf_tls_setup (link_info
.output_bfd
, &link_info
);
1594 /* Make __ehdr_start hidden if it has been referenced, to
1595 prevent the symbol from being dynamic. */
1596 if (!bfd_link_relocatable (&link_info
))
1598 struct elf_link_hash_table
*htab
= elf_hash_table (&link_info
);
1599 struct elf_link_hash_entry
*h
1600 = elf_link_hash_lookup (htab
, "__ehdr_start", false, false, true);
1602 /* Only adjust the export class if the symbol was referenced
1603 and not defined, otherwise leave it alone. */
1605 && (h
->root
.type
== bfd_link_hash_new
1606 || h
->root
.type
== bfd_link_hash_undefined
1607 || h
->root
.type
== bfd_link_hash_undefweak
1608 || h
->root
.type
== bfd_link_hash_common
))
1610 /* Don't leave the symbol undefined. Undefined hidden
1611 symbols typically won't have dynamic relocations, but
1612 we most likely will need dynamic relocations for
1613 __ehdr_start if we are building a PIE or shared
1615 ehdr_start
= &h
->root
;
1616 ehdr_start_save_type
= ehdr_start
->type
;
1617 memcpy (ehdr_start_save_u
,
1618 (char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1619 sizeof ehdr_start_save_u
);
1620 ehdr_start
->type
= bfd_link_hash_defined
;
1621 /* It will be converted to section-relative later. */
1622 ehdr_start
->u
.def
.section
= bfd_abs_section_ptr
;
1623 ehdr_start
->u
.def
.value
= 0;
1627 /* If we are going to make any variable assignments, we need to
1628 let the ELF backend know about them in case the variables are
1629 referred to by dynamic objects. */
1630 lang_for_each_statement (ldelf_find_statement_assignment
);
1633 /* Let the ELF backend work out the sizes of any sections required
1634 by dynamic linking. */
1635 rpath
= command_line
.rpath
;
1637 rpath
= (const char *) getenv ("LD_RUN_PATH");
1639 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1640 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1642 const char *audit_libs
= elf_dt_audit (abfd
);
1644 /* If the input bfd contains an audit entry, we need to add it as
1645 a dep audit entry. */
1646 if (audit_libs
&& *audit_libs
!= '\0')
1648 char *cp
= xstrdup (audit_libs
);
1652 char *cp2
= strchr (cp
, config
.rpath_separator
);
1660 if (cp
!= NULL
&& *cp
!= '\0')
1661 ldelf_append_to_separated_string (&depaudit
, cp
);
1663 cp
= more
? ++cp2
: NULL
;
1669 if (! (bfd_elf_size_dynamic_sections
1670 (link_info
.output_bfd
, command_line
.soname
, rpath
,
1671 command_line
.filter_shlib
, audit
, depaudit
,
1672 (const char * const *) command_line
.auxiliary_filters
,
1673 &link_info
, &sinterp
)))
1674 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1676 if (sinterp
!= NULL
)
1678 /* Let the user override the dynamic linker we are using. */
1679 if (command_line
.interpreter
!= NULL
)
1680 default_interpreter_name
= command_line
.interpreter
;
1681 if (default_interpreter_name
!= NULL
)
1683 sinterp
->contents
= (bfd_byte
*) default_interpreter_name
;
1684 sinterp
->size
= strlen ((char *) sinterp
->contents
) + 1;
1688 /* Look for any sections named .gnu.warning. As a GNU extensions,
1689 we treat such sections as containing warning messages. We print
1690 out the warning message, and then zero out the section size so
1691 that it does not get copied into the output file. */
1694 LANG_FOR_EACH_INPUT_STATEMENT (is
)
1700 if (is
->flags
.just_syms
)
1703 s
= bfd_get_section_by_name (is
->the_bfd
, ".gnu.warning");
1708 msg
= (char *) xmalloc ((size_t) (sz
+ 1));
1709 if (! bfd_get_section_contents (is
->the_bfd
, s
, msg
,
1711 einfo (_("%F%P: %pB: can't read contents of section .gnu.warning: %E\n"),
1714 (*link_info
.callbacks
->warning
) (&link_info
, msg
,
1715 (const char *) NULL
, is
->the_bfd
,
1716 (asection
*) NULL
, (bfd_vma
) 0);
1719 /* Clobber the section size, so that we don't waste space
1720 copying the warning into the output file. If we've already
1721 sized the output section, adjust its size. The adjustment
1722 is on rawsize because targets that size sections early will
1723 have called lang_reset_memory_regions after sizing. */
1724 if (s
->output_section
!= NULL
1725 && s
->output_section
->rawsize
>= s
->size
)
1726 s
->output_section
->rawsize
-= s
->size
;
1730 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1731 warning section don't get copied to the output. */
1732 s
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
1736 before_allocation_default ();
1738 if (!bfd_elf_size_dynsym_hash_dynstr (link_info
.output_bfd
, &link_info
))
1739 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1741 if (ehdr_start
!= NULL
)
1743 /* If we twiddled __ehdr_start to defined earlier, put it back
1745 ehdr_start
->type
= ehdr_start_save_type
;
1746 memcpy ((char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1748 sizeof ehdr_start_save_u
);
1751 /* Try to open a dynamic archive. This is where we know that ELF
1752 dynamic libraries have an extension of .so (or .sl on oddball systems
1756 ldelf_open_dynamic_archive (const char *arch
, search_dirs_type
*search
,
1757 lang_input_statement_type
*entry
)
1759 const char *filename
;
1762 bool opened
= false;
1764 if (! entry
->flags
.maybe_archive
)
1767 filename
= entry
->filename
;
1768 len
= strlen (search
->name
) + strlen (filename
);
1769 if (entry
->flags
.full_name_provided
)
1772 string
= (char *) xmalloc (len
);
1773 sprintf (string
, "%s/%s", search
->name
, filename
);
1779 len
+= strlen (arch
) + sizeof "/lib.so";
1780 #ifdef EXTRA_SHLIB_EXTENSION
1781 xlen
= (strlen (EXTRA_SHLIB_EXTENSION
) > 3
1782 ? strlen (EXTRA_SHLIB_EXTENSION
) - 3
1785 string
= (char *) xmalloc (len
+ xlen
);
1786 sprintf (string
, "%s/lib%s%s.so", search
->name
, filename
, arch
);
1787 #ifdef EXTRA_SHLIB_EXTENSION
1788 /* Try the .so extension first. If that fails build a new filename
1789 using EXTRA_SHLIB_EXTENSION. */
1790 opened
= ldfile_try_open_bfd (string
, entry
);
1792 strcpy (string
+ len
- 4, EXTRA_SHLIB_EXTENSION
);
1796 if (!opened
&& !ldfile_try_open_bfd (string
, entry
))
1802 entry
->filename
= string
;
1804 /* We have found a dynamic object to include in the link. The ELF
1805 backend linker will create a DT_NEEDED entry in the .dynamic
1806 section naming this file. If this file includes a DT_SONAME
1807 entry, it will be used. Otherwise, the ELF linker will just use
1808 the name of the file. For an archive found by searching, like
1809 this one, the DT_NEEDED entry should consist of just the name of
1810 the file, without the path information used to find it. Note
1811 that we only need to do this if we have a dynamic object; an
1812 archive will never be referenced by a DT_NEEDED entry.
1814 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1815 very pretty. I haven't been able to think of anything that is
1817 if (bfd_check_format (entry
->the_bfd
, bfd_object
)
1818 && (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
1820 ASSERT (entry
->flags
.maybe_archive
&& entry
->flags
.search_dirs
);
1822 /* Rather than duplicating the logic above. Just use the
1823 filename we recorded earlier. */
1825 if (!entry
->flags
.full_name_provided
)
1826 filename
= lbasename (entry
->filename
);
1827 bfd_elf_set_dt_needed_name (entry
->the_bfd
, filename
);
1833 /* A variant of lang_output_section_find used by place_orphan. */
1835 static lang_output_section_statement_type
*
1836 output_rel_find (int isdyn
, int rela
)
1838 lang_output_section_statement_type
*lookup
;
1839 lang_output_section_statement_type
*last
= NULL
;
1840 lang_output_section_statement_type
*last_alloc
= NULL
;
1841 lang_output_section_statement_type
*last_ro_alloc
= NULL
;
1842 lang_output_section_statement_type
*last_rel
= NULL
;
1843 lang_output_section_statement_type
*last_rel_alloc
= NULL
;
1845 for (lookup
= (void *) lang_os_list
.head
;
1847 lookup
= lookup
->next
)
1849 if (lookup
->constraint
>= 0
1850 && startswith (lookup
->name
, ".rel"))
1852 int lookrela
= lookup
->name
[4] == 'a';
1854 /* .rel.dyn must come before all other reloc sections, to suit
1859 /* Don't place after .rel.plt as doing so results in wrong
1861 if (strcmp (".plt", lookup
->name
+ 4 + lookrela
) == 0)
1864 if (rela
== lookrela
|| last_rel
== NULL
)
1866 if ((rela
== lookrela
|| last_rel_alloc
== NULL
)
1867 && lookup
->bfd_section
!= NULL
1868 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1869 last_rel_alloc
= lookup
;
1873 if (lookup
->bfd_section
!= NULL
1874 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1876 last_alloc
= lookup
;
1877 if ((lookup
->bfd_section
->flags
& SEC_READONLY
) != 0)
1878 last_ro_alloc
= lookup
;
1883 return last_rel_alloc
;
1889 return last_ro_alloc
;
1897 /* Return whether IN is suitable to be part of OUT. */
1900 elf_orphan_compatible (asection
*in
, asection
*out
)
1902 /* Non-zero sh_info implies a section with SHF_INFO_LINK with
1903 unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
1904 section where sh_info specifies a symbol table. (We won't see
1905 SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
1906 can't merge SHT_REL/SHT_RELA using differing symbol tables, and
1907 shouldn't merge sections with differing unknown semantics. */
1908 if (elf_section_data (out
)->this_hdr
.sh_info
1909 != elf_section_data (in
)->this_hdr
.sh_info
)
1911 /* We can't merge with a member of an output section group or merge
1912 two sections with differing SHF_EXCLUDE or other processor and OS
1913 specific flags when doing a relocatable link. */
1914 if (bfd_link_relocatable (&link_info
)
1915 && (elf_next_in_group (out
) != NULL
1916 || ((elf_section_flags (out
) ^ elf_section_flags (in
))
1917 & (SHF_MASKPROC
| SHF_MASKOS
)) != 0))
1919 return _bfd_elf_match_sections_by_type (link_info
.output_bfd
, out
,
1923 /* Place an orphan section. We use this to put random SHF_ALLOC
1924 sections in the right segment. */
1926 lang_output_section_statement_type
*
1927 ldelf_place_orphan (asection
*s
, const char *secname
, int constraint
)
1929 static struct orphan_save hold
[] =
1932 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
,
1935 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1938 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_THREAD_LOCAL
,
1941 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
,
1947 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1950 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1953 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_SMALL_DATA
,
1959 enum orphan_save_index
1971 static int orphan_init_done
= 0;
1972 struct orphan_save
*place
;
1973 lang_output_section_statement_type
*after
;
1974 lang_output_section_statement_type
*os
;
1975 lang_output_section_statement_type
*match_by_name
= NULL
;
1977 int elfinput
= s
->owner
->xvec
->flavour
== bfd_target_elf_flavour
;
1978 int elfoutput
= link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
;
1979 unsigned int sh_type
= elfinput
? elf_section_type (s
) : SHT_NULL
;
1983 if (!bfd_link_relocatable (&link_info
)
1984 && link_info
.combreloc
1985 && (s
->flags
& SEC_ALLOC
))
1991 secname
= ".rela.dyn";
1995 secname
= ".rel.dyn";
2001 else if (startswith (secname
, ".rel"))
2003 secname
= secname
[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
2008 if (!bfd_link_relocatable (&link_info
)
2011 && (s
->flags
& SEC_ALLOC
) != 0
2012 && (elf_tdata (s
->owner
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
2013 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
2015 /* Find the output mbind section with the same type, attributes
2016 and sh_info field. */
2017 for (os
= (void *) lang_os_list
.head
;
2020 if (os
->bfd_section
!= NULL
2021 && !bfd_is_abs_section (os
->bfd_section
)
2022 && (elf_section_flags (os
->bfd_section
) & SHF_GNU_MBIND
) != 0
2023 && ((s
->flags
& (SEC_ALLOC
2028 == (os
->bfd_section
->flags
& (SEC_ALLOC
2033 && (elf_section_data (os
->bfd_section
)->this_hdr
.sh_info
2034 == elf_section_data (s
)->this_hdr
.sh_info
))
2036 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
2040 /* Create the output mbind section with the ".mbind." prefix
2042 if ((s
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2043 secname
= ".mbind.bss";
2044 else if ((s
->flags
& SEC_READONLY
) == 0)
2045 secname
= ".mbind.data";
2046 else if ((s
->flags
& SEC_CODE
) == 0)
2047 secname
= ".mbind.rodata";
2049 secname
= ".mbind.text";
2050 elf_tdata (link_info
.output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
2053 /* Look through the script to see where to place this section. The
2054 script includes entries added by previous lang_insert_orphan
2055 calls, so this loop puts multiple compatible orphans of the same
2056 name into a single output section. */
2057 if (constraint
== 0)
2058 for (os
= lang_output_section_find (secname
);
2060 os
= next_matching_output_section_statement (os
, 0))
2062 /* If we don't match an existing output section, tell
2063 lang_insert_orphan to create a new output section. */
2064 constraint
= SPECIAL
;
2066 /* Check to see if we already have an output section statement
2067 with this name, and its bfd section has compatible flags.
2068 If the section already exists but does not have any flags
2069 set, then it has been created by the linker, possibly as a
2070 result of a --section-start command line switch. */
2071 if (os
->bfd_section
!= NULL
2072 && (os
->bfd_section
->flags
== 0
2073 || (((s
->flags
^ os
->bfd_section
->flags
)
2074 & (SEC_LOAD
| SEC_ALLOC
)) == 0
2077 || elf_orphan_compatible (s
, os
->bfd_section
)))))
2079 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
2083 /* Save unused output sections in case we can match them
2084 against orphans later. */
2085 if (os
->bfd_section
== NULL
)
2089 /* If we didn't match an active output section, see if we matched an
2090 unused one and use that. */
2093 lang_add_section (&match_by_name
->children
, s
, NULL
, NULL
, match_by_name
);
2094 return match_by_name
;
2097 if (!orphan_init_done
)
2099 struct orphan_save
*ho
;
2101 for (ho
= hold
; ho
< hold
+ sizeof (hold
) / sizeof (hold
[0]); ++ho
)
2102 if (ho
->name
!= NULL
)
2104 ho
->os
= lang_output_section_find (ho
->name
);
2105 if (ho
->os
!= NULL
&& ho
->os
->flags
== 0)
2106 ho
->os
->flags
= ho
->flags
;
2108 orphan_init_done
= 1;
2111 /* If this is a final link, then always put .gnu.warning.SYMBOL
2112 sections into the .text section to get them out of the way. */
2113 if (bfd_link_executable (&link_info
)
2114 && startswith (s
->name
, ".gnu.warning.")
2115 && hold
[orphan_text
].os
!= NULL
)
2117 os
= hold
[orphan_text
].os
;
2118 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
2123 if (!bfd_link_relocatable (&link_info
))
2126 while ((nexts
= bfd_get_next_section_by_name (nexts
->owner
, nexts
))
2128 if (nexts
->output_section
== NULL
2129 && (nexts
->flags
& SEC_EXCLUDE
) == 0
2130 && ((nexts
->flags
^ flags
) & (SEC_LOAD
| SEC_ALLOC
)) == 0
2131 && (nexts
->owner
->flags
& DYNAMIC
) == 0
2132 && !bfd_input_just_syms (nexts
->owner
)
2133 && _bfd_elf_match_sections_by_type (nexts
->owner
, nexts
,
2135 flags
= (((flags
^ SEC_READONLY
)
2136 | (nexts
->flags
^ SEC_READONLY
))
2140 /* Decide which segment the section should go in based on the
2141 section name and section flags. We put loadable .note sections
2142 right after the .interp section, so that the PT_NOTE segment is
2143 stored right after the program headers where the OS can read it
2144 in the first page. */
2147 if ((flags
& (SEC_ALLOC
| SEC_DEBUGGING
)) == 0)
2148 place
= &hold
[orphan_nonalloc
];
2149 else if ((flags
& SEC_ALLOC
) == 0)
2151 else if ((flags
& SEC_LOAD
) != 0
2153 ? sh_type
== SHT_NOTE
2154 : startswith (secname
, ".note")))
2155 place
= &hold
[orphan_interp
];
2156 else if ((flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_THREAD_LOCAL
)) == 0)
2157 place
= &hold
[orphan_bss
];
2158 else if ((flags
& SEC_SMALL_DATA
) != 0)
2159 place
= &hold
[orphan_sdata
];
2160 else if ((flags
& SEC_THREAD_LOCAL
) != 0)
2161 place
= &hold
[orphan_tdata
];
2162 else if ((flags
& SEC_READONLY
) == 0)
2163 place
= &hold
[orphan_data
];
2164 else if ((flags
& SEC_LOAD
) != 0
2166 ? sh_type
== SHT_RELA
|| sh_type
== SHT_REL
2167 : startswith (secname
, ".rel")))
2168 place
= &hold
[orphan_rel
];
2169 else if ((flags
& SEC_CODE
) == 0)
2170 place
= &hold
[orphan_rodata
];
2172 place
= &hold
[orphan_text
];
2177 if (place
->os
== NULL
)
2179 if (place
->name
!= NULL
)
2180 place
->os
= lang_output_section_find (place
->name
);
2183 int rela
= elfinput
? sh_type
== SHT_RELA
: secname
[4] == 'a';
2184 place
->os
= output_rel_find (isdyn
, rela
);
2190 = lang_output_section_find_by_flags (s
, flags
, &place
->os
,
2191 _bfd_elf_match_sections_by_type
);
2193 /* *ABS* is always the first output section statement. */
2194 after
= (void *) lang_os_list
.head
;
2197 return lang_insert_orphan (s
, secname
, constraint
, after
, place
, NULL
, NULL
);
2201 ldelf_before_place_orphans (void)
2205 for (abfd
= link_info
.input_bfds
;
2206 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
2207 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
2208 && bfd_count_sections (abfd
) != 0
2209 && !bfd_input_just_syms (abfd
))
2212 for (isec
= abfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
2214 /* Discard a section if any of its linked-to section has
2216 asection
*linked_to_sec
;
2217 for (linked_to_sec
= elf_linked_to_section (isec
);
2218 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
2219 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
2221 if (discarded_section (linked_to_sec
))
2223 isec
->output_section
= bfd_abs_section_ptr
;
2224 isec
->flags
|= SEC_EXCLUDE
;
2227 linked_to_sec
->linker_mark
= 1;
2229 for (linked_to_sec
= elf_linked_to_section (isec
);
2230 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
2231 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
2232 linked_to_sec
->linker_mark
= 0;
2238 ldelf_set_output_arch (void)
2240 set_output_arch_default ();
2241 if (link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2242 elf_link_info (link_info
.output_bfd
) = &link_info
;