1 /* ELF emulation code for targets using elf32.em.
2 Copyright (C) 1991-2019 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"
32 #include "ldbuildid.h"
46 /* Style of .note.gnu.build-id section. */
47 const char *ldelf_emit_note_gnu_build_id
;
49 /* These variables are required to pass information back and forth
50 between after_open and check_needed and stat_needed and vercheck. */
52 static struct bfd_link_needed_list
*global_needed
;
53 static lang_input_statement_type
*global_found
;
54 static struct stat global_stat
;
55 static struct bfd_link_needed_list
*global_vercheck_needed
;
56 static bfd_boolean global_vercheck_failed
;
59 ldelf_after_parse (void)
61 if (bfd_link_pie (&link_info
))
62 link_info
.flags_1
|= (bfd_vma
) DF_1_PIE
;
64 if (bfd_link_executable (&link_info
)
65 && link_info
.nointerp
)
67 if (link_info
.dynamic_undefined_weak
> 0)
68 einfo (_("%P: warning: -z dynamic-undefined-weak ignored\n"));
69 link_info
.dynamic_undefined_weak
= 0;
71 after_parse_default ();
74 /* Handle the generation of DT_NEEDED tags. */
77 ldelf_load_symbols (lang_input_statement_type
*entry
)
81 /* Tell the ELF linker that we don't want the output file to have a
82 DT_NEEDED entry for this file, unless it is used to resolve
83 references in a regular object. */
84 if (entry
->flags
.add_DT_NEEDED_for_regular
)
85 link_class
= DYN_AS_NEEDED
;
87 /* Tell the ELF linker that we don't want the output file to have a
88 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
90 if (!entry
->flags
.add_DT_NEEDED_for_dynamic
)
91 link_class
|= DYN_NO_ADD_NEEDED
;
93 if (entry
->flags
.just_syms
94 && (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) != 0)
95 einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
99 || (bfd_get_file_flags (entry
->the_bfd
) & DYNAMIC
) == 0)
102 bfd_elf_set_dyn_lib_class (entry
->the_bfd
,
103 (enum dynamic_lib_link_class
) link_class
);
105 /* Continue on with normal load_symbols processing. */
109 /* On Linux, it's possible to have different versions of the same
110 shared library linked against different versions of libc. The
111 dynamic linker somehow tags which libc version to use in
112 /etc/ld.so.cache, and, based on the libc that it sees in the
113 executable, chooses which version of the shared library to use.
115 We try to do a similar check here by checking whether this shared
116 library needs any other shared libraries which may conflict with
117 libraries we have already included in the link. If it does, we
118 skip it, and try to find another shared library farther on down the
121 This is called via lang_for_each_input_file.
122 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
123 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
124 a conflicting version. */
127 ldelf_vercheck (lang_input_statement_type
*s
)
130 struct bfd_link_needed_list
*l
;
132 if (global_vercheck_failed
)
134 if (s
->the_bfd
== NULL
135 || (bfd_get_file_flags (s
->the_bfd
) & DYNAMIC
) == 0)
138 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
140 soname
= lbasename (bfd_get_filename (s
->the_bfd
));
142 for (l
= global_vercheck_needed
; l
!= NULL
; l
= l
->next
)
146 if (filename_cmp (soname
, l
->name
) == 0)
148 /* Probably can't happen, but it's an easy check. */
152 if (strchr (l
->name
, '/') != NULL
)
155 suffix
= strstr (l
->name
, ".so.");
159 suffix
+= sizeof ".so." - 1;
161 if (filename_ncmp (soname
, l
->name
, suffix
- l
->name
) == 0)
163 /* Here we know that S is a dynamic object FOO.SO.VER1, and
164 the object we are considering needs a dynamic object
165 FOO.SO.VER2, and VER1 and VER2 are different. This
166 appears to be a version mismatch, so we tell the caller
167 to try a different version of this library. */
168 global_vercheck_failed
= TRUE
;
175 /* See if an input file matches a DT_NEEDED entry by running stat on
179 ldelf_stat_needed (lang_input_statement_type
*s
)
185 if (global_found
!= NULL
)
187 if (s
->the_bfd
== NULL
)
190 /* If this input file was an as-needed entry, and wasn't found to be
191 needed at the stage it was linked, then don't say we have loaded it. */
192 if ((bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
195 if (bfd_stat (s
->the_bfd
, &st
) != 0)
197 einfo (_("%P: %pB: bfd_stat failed: %E\n"), s
->the_bfd
);
201 /* Some operating systems, e.g. Windows, do not provide a meaningful
202 st_ino; they always set it to zero. (Windows does provide a
203 meaningful st_dev.) Do not indicate a duplicate library in that
204 case. While there is no guarantee that a system that provides
205 meaningful inode numbers will never set st_ino to zero, this is
206 merely an optimization, so we do not need to worry about false
208 if (st
.st_dev
== global_stat
.st_dev
209 && st
.st_ino
== global_stat
.st_ino
216 /* We issue a warning if it looks like we are including two
217 different versions of the same shared library. For example,
218 there may be a problem if -lc picks up libc.so.6 but some other
219 shared library has a DT_NEEDED entry of libc.so.5. This is a
220 heuristic test, and it will only work if the name looks like
221 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
222 If we really want to issue warnings about mixing version numbers
223 of shared libraries, we need to find a better way. */
225 if (strchr (global_needed
->name
, '/') != NULL
)
227 suffix
= strstr (global_needed
->name
, ".so.");
230 suffix
+= sizeof ".so." - 1;
232 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
234 soname
= lbasename (s
->filename
);
236 if (filename_ncmp (soname
, global_needed
->name
,
237 suffix
- global_needed
->name
) == 0)
238 einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
239 global_needed
->name
, global_needed
->by
, soname
);
242 /* This function is called for each possible name for a dynamic object
243 named by a DT_NEEDED entry. The FORCE parameter indicates whether
244 to skip the check for a conflicting version. */
247 ldelf_try_needed (struct dt_needed
*needed
, int force
, int is_linux
)
250 const char *name
= needed
->name
;
254 abfd
= bfd_openr (name
, bfd_get_target (link_info
.output_bfd
));
258 info_msg (_("attempt to open %s failed\n"), name
);
262 /* Linker needs to decompress sections. */
263 abfd
->flags
|= BFD_DECOMPRESS
;
265 if (! bfd_check_format (abfd
, bfd_object
))
270 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) == 0)
276 /* For DT_NEEDED, they have to match. */
277 if (abfd
->xvec
!= link_info
.output_bfd
->xvec
)
283 /* Check whether this object would include any conflicting library
284 versions. If FORCE is set, then we skip this check; we use this
285 the second time around, if we couldn't find any compatible
286 instance of the shared library. */
290 struct bfd_link_needed_list
*needs
;
292 if (! bfd_elf_get_bfd_needed_list (abfd
, &needs
))
293 einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd
);
297 global_vercheck_needed
= needs
;
298 global_vercheck_failed
= FALSE
;
299 lang_for_each_input_file (ldelf_vercheck
);
300 if (global_vercheck_failed
)
303 /* Return FALSE to force the caller to move on to try
304 another file on the search path. */
308 /* But wait! It gets much worse. On Linux, if a shared
309 library does not use libc at all, we are supposed to skip
310 it the first time around in case we encounter a shared
311 library later on with the same name which does use the
312 version of libc that we want. This is much too horrible
313 to use on any system other than Linux. */
316 struct bfd_link_needed_list
*l
;
318 for (l
= needs
; l
!= NULL
; l
= l
->next
)
319 if (CONST_STRNEQ (l
->name
, "libc.so"))
330 /* We've found a dynamic object matching the DT_NEEDED entry. */
332 /* We have already checked that there is no other input file of the
333 same name. We must now check again that we are not including the
334 same file twice. We need to do this because on many systems
335 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
336 reference libc.so.1. If we have already included libc.so, we
337 don't want to include libc.so.1 if they are the same file, and we
338 can only check that using stat. */
340 if (bfd_stat (abfd
, &global_stat
) != 0)
341 einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd
);
343 /* First strip off everything before the last '/'. */
344 soname
= lbasename (bfd_get_filename (abfd
));
347 info_msg (_("found %s at %s\n"), soname
, name
);
350 lang_for_each_input_file (ldelf_stat_needed
);
351 if (global_found
!= NULL
)
353 /* Return TRUE to indicate that we found the file, even though
354 we aren't going to do anything with it. */
358 /* Specify the soname to use. */
359 bfd_elf_set_dt_needed_name (abfd
, soname
);
361 /* Tell the ELF linker that we don't want the output file to have a
362 DT_NEEDED entry for this file, unless it is used to resolve
363 references in a regular object. */
364 link_class
= DYN_DT_NEEDED
;
366 /* Tell the ELF linker that we don't want the output file to have a
367 DT_NEEDED entry for this file at all if the entry is from a file
368 with DYN_NO_ADD_NEEDED. */
369 if (needed
->by
!= NULL
370 && (bfd_elf_get_dyn_lib_class (needed
->by
) & DYN_NO_ADD_NEEDED
) != 0)
371 link_class
|= DYN_NO_NEEDED
| DYN_NO_ADD_NEEDED
;
373 bfd_elf_set_dyn_lib_class (abfd
, (enum dynamic_lib_link_class
) link_class
);
375 /* Add this file into the symbol table. */
376 if (! bfd_link_add_symbols (abfd
, &link_info
))
377 einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd
);
382 /* Search for a needed file in a path. */
385 ldelf_search_needed (const char *path
, struct dt_needed
*n
, int force
,
386 int is_linux
, int elfsize
)
389 const char *name
= n
->name
;
391 struct dt_needed needed
;
394 return ldelf_try_needed (n
, force
, is_linux
);
396 if (path
== NULL
|| *path
== '\0')
400 needed
.name
= n
->name
;
407 char *filename
, *sset
;
409 s
= strchr (path
, config
.rpath_separator
);
411 s
= path
+ strlen (path
);
413 #if HAVE_DOS_BASED_FILE_SYSTEM
414 /* Assume a match on the second char is part of drive specifier. */
415 else if (config
.rpath_separator
== ':'
419 s
= strchr (s
+ 1, config
.rpath_separator
);
421 s
= path
+ strlen (path
);
424 filename
= (char *) xmalloc (s
- path
+ len
+ 2);
429 memcpy (filename
, path
, s
- path
);
430 filename
[s
- path
] = '/';
431 sset
= filename
+ (s
- path
) + 1;
435 /* PR 20535: Support the same pseudo-environment variables that
436 are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
437 Since there can be more than one occurrence of these tokens in
438 the path we loop until no more are found. Since we might not
439 be able to substitute some of the tokens we maintain an offset
440 into the filename for where we should begin our scan. */
441 while ((var
= strchr (filename
+ offset
, '$')) != NULL
)
443 /* The ld.so manual page does not say, but I am going to assume that
444 these tokens are terminated by a directory separator character
445 (/) or the end of the string. There is also an implication that
446 $ORIGIN should only be used at the start of a path, but that is
449 The ld.so manual page also states that it allows ${ORIGIN},
450 ${LIB} and ${PLATFORM}, so these are supported as well.
452 FIXME: The code could be a lot cleverer about allocating space
453 for the processed string. */
454 char * end
= strchr (var
, '/');
455 const char *replacement
= NULL
;
457 char * freeme
= NULL
;
458 unsigned flen
= strlen (filename
);
461 /* Temporarily terminate the filename at the end of the token. */
469 if (strcmp (v
, "RIGIN") == 0 || strcmp (v
, "RIGIN}") == 0)
471 /* ORIGIN - replace with the full path to the directory
472 containing the program or shared object. */
473 if (needed
.by
== NULL
)
475 if (link_info
.output_bfd
== NULL
)
480 replacement
= bfd_get_filename (link_info
.output_bfd
);
483 replacement
= bfd_get_filename (needed
.by
);
489 if (replacement
[0] == '/')
490 freeme
= xstrdup (replacement
);
493 char * current_dir
= getpwd ();
495 freeme
= xmalloc (strlen (replacement
)
496 + strlen (current_dir
) + 2);
497 sprintf (freeme
, "%s/%s", current_dir
, replacement
);
500 replacement
= freeme
;
501 if ((slash
= strrchr (replacement
, '/')) != NULL
)
508 if (strcmp (v
, "IB") == 0 || strcmp (v
, "IB}") == 0)
510 /* LIB - replace with "lib" in 32-bit environments
511 and "lib64" in 64-bit environments. */
515 case 32: replacement
= "lib"; break;
516 case 64: replacement
= "lib64"; break;
524 /* Supporting $PLATFORM in a cross-hosted environment is not
525 possible. Supporting it in a native environment involves
526 loading the <sys/auxv.h> header file which loads the
527 system <elf.h> header file, which conflicts with the
528 "include/elf/mips.h" header file. */
536 char * filename2
= xmalloc (flen
+ strlen (replacement
));
540 sprintf (filename2
, "%.*s%s/%s",
541 (int)(var
- filename
), filename
,
542 replacement
, end
+ 1);
543 offset
= (var
- filename
) + 1 + strlen (replacement
);
547 sprintf (filename2
, "%.*s%s",
548 (int)(var
- filename
), filename
,
550 offset
= var
- filename
+ strlen (replacement
);
554 filename
= filename2
;
555 /* There is no need to restore the path separator (when
556 end != NULL) as we have replaced the entire string. */
561 /* We only issue an "unrecognised" message in verbose mode
562 as the $<foo> token might be a legitimate component of
563 a path name in the target's file system. */
564 info_msg (_("unrecognised or unsupported token "
565 "'%s' in search path\n"), var
);
567 /* Restore the path separator. */
570 /* PR 20784: Make sure that we resume the scan *after*
571 the token that we could not replace. */
572 offset
= (var
+ 1) - filename
;
578 needed
.name
= filename
;
580 if (ldelf_try_needed (&needed
, force
, is_linux
))
593 /* Prefix the sysroot to absolute paths in PATH, a string containing
594 paths separated by config.rpath_separator. If running on a DOS
595 file system, paths containing a drive spec won't have the sysroot
596 prefix added, unless the sysroot also specifies the same drive. */
599 ldelf_add_sysroot (const char *path
)
604 int dos_drive_sysroot
= HAS_DRIVE_SPEC (ld_sysroot
);
606 len
= strlen (ld_sysroot
);
607 for (extra
= 0, p
= path
; ; )
609 int dos_drive
= HAS_DRIVE_SPEC (p
);
613 if (IS_DIR_SEPARATOR (*p
)
615 || (dos_drive_sysroot
616 && ld_sysroot
[0] == p
[-2])))
618 if (dos_drive
&& dos_drive_sysroot
)
623 p
= strchr (p
, config
.rpath_separator
);
629 ret
= xmalloc (strlen (path
) + extra
+ 1);
631 for (q
= ret
, p
= path
; ; )
634 int dos_drive
= HAS_DRIVE_SPEC (p
);
641 if (IS_DIR_SEPARATOR (*p
)
643 || (dos_drive_sysroot
644 && ld_sysroot
[0] == p
[-2])))
646 if (dos_drive
&& dos_drive_sysroot
)
648 strcpy (q
, ld_sysroot
+ 2);
653 strcpy (q
, ld_sysroot
);
657 end
= strchr (p
, config
.rpath_separator
);
660 size_t n
= end
- p
+ 1;
675 /* Read the system search path the FreeBSD way rather than the Linux way. */
676 #ifdef HAVE_ELF_HINTS_H
677 #include <elf-hints.h>
679 #include "elf-hints-local.h"
683 ldelf_check_ld_elf_hints (const struct bfd_link_needed_list
*l
, int force
,
686 static bfd_boolean initialized
;
687 static const char *ld_elf_hints
;
688 struct dt_needed needed
;
695 tmppath
= concat (ld_sysroot
, _PATH_ELF_HINTS
, (const char *) NULL
);
696 f
= fopen (tmppath
, FOPEN_RB
);
700 struct elfhints_hdr hdr
;
702 if (fread (&hdr
, 1, sizeof (hdr
), f
) == sizeof (hdr
)
703 && hdr
.magic
== ELFHINTS_MAGIC
706 if (fseek (f
, hdr
.strtab
+ hdr
.dirlist
, SEEK_SET
) != -1)
710 b
= xmalloc (hdr
.dirlistlen
+ 1);
711 if (fread (b
, 1, hdr
.dirlistlen
+ 1, f
) ==
713 ld_elf_hints
= ldelf_add_sysroot (b
);
724 if (ld_elf_hints
== NULL
)
728 needed
.name
= l
->name
;
729 return ldelf_search_needed (ld_elf_hints
, &needed
, force
, FALSE
, elfsize
);
732 /* For a native linker, check the file /etc/ld.so.conf for directories
733 in which we may find shared libraries. /etc/ld.so.conf is really
734 only meaningful on Linux. */
736 struct ldelf_ld_so_conf
743 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*, const char *);
746 ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf
*info
,
747 const char *filename
,
755 if (pattern
[0] != '/')
757 char *p
= strrchr (filename
, '/');
758 size_t patlen
= strlen (pattern
) + 1;
760 newp
= xmalloc (p
- filename
+ 1 + patlen
);
761 memcpy (newp
, filename
, p
- filename
+ 1);
762 memcpy (newp
+ (p
- filename
+ 1), pattern
, patlen
);
767 if (glob (pattern
, 0, NULL
, &gl
) == 0)
771 for (i
= 0; i
< gl
.gl_pathc
; ++i
)
772 ldelf_parse_ld_so_conf (info
, gl
.gl_pathv
[i
]);
776 /* If we do not have glob, treat the pattern as a literal filename. */
777 ldelf_parse_ld_so_conf (info
, pattern
);
785 ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf
*info
, const char *filename
)
787 FILE *f
= fopen (filename
, FOPEN_RT
);
795 line
= xmalloc (linelen
);
800 /* Normally this would use getline(3), but we need to be portable. */
801 while ((q
= fgets (p
, linelen
- (p
- line
), f
)) != NULL
802 && strlen (q
) == linelen
- (p
- line
) - 1
803 && line
[linelen
- 2] != '\n')
805 line
= xrealloc (line
, 2 * linelen
);
806 p
= line
+ linelen
- 1;
810 if (q
== NULL
&& p
== line
)
813 p
= strchr (line
, '\n');
817 /* Because the file format does not know any form of quoting we
818 can search forward for the next '#' character and if found
819 make it terminating the line. */
820 p
= strchr (line
, '#');
824 /* Remove leading whitespace. NUL is no whitespace character. */
826 while (*p
== ' ' || *p
== '\f' || *p
== '\r' || *p
== '\t' || *p
== '\v')
829 /* If the line is blank it is ignored. */
833 if (CONST_STRNEQ (p
, "include") && (p
[7] == ' ' || p
[7] == '\t'))
839 while (*p
== ' ' || *p
== '\t')
847 while (*p
!= ' ' && *p
!= '\t' && *p
)
853 ldelf_parse_ld_so_conf_include (info
, filename
, dir
);
860 while (*p
&& *p
!= '=' && *p
!= ' ' && *p
!= '\t' && *p
!= '\f'
861 && *p
!= '\r' && *p
!= '\v')
864 while (p
!= dir
&& p
[-1] == '/')
866 if (info
->path
== NULL
)
868 info
->alloc
= p
- dir
+ 1 + 256;
869 info
->path
= xmalloc (info
->alloc
);
874 if (info
->len
+ 1 + (p
- dir
) >= info
->alloc
)
876 info
->alloc
+= p
- dir
+ 256;
877 info
->path
= xrealloc (info
->path
, info
->alloc
);
879 info
->path
[info
->len
++] = config
.rpath_separator
;
881 memcpy (info
->path
+ info
->len
, dir
, p
- dir
);
882 info
->len
+= p
- dir
;
883 info
->path
[info
->len
] = '\0';
893 ldelf_check_ld_so_conf (const struct bfd_link_needed_list
*l
, int force
,
896 static bfd_boolean initialized
;
897 static const char *ld_so_conf
;
898 struct dt_needed needed
;
903 struct ldelf_ld_so_conf info
;
906 info
.len
= info
.alloc
= 0;
907 tmppath
= concat (ld_sysroot
, "${prefix}/etc/ld.so.conf",
908 (const char *) NULL
);
909 if (!ldelf_parse_ld_so_conf (&info
, tmppath
))
912 tmppath
= concat (ld_sysroot
, "/etc/ld.so.conf",
913 (const char *) NULL
);
914 ldelf_parse_ld_so_conf (&info
, tmppath
);
920 ld_so_conf
= ldelf_add_sysroot (info
.path
);
926 if (ld_so_conf
== NULL
)
931 needed
.name
= l
->name
;
932 return ldelf_search_needed (ld_so_conf
, &needed
, force
, TRUE
, elfsize
);
935 /* See if an input file matches a DT_NEEDED entry by name. */
938 ldelf_check_needed (lang_input_statement_type
*s
)
942 /* Stop looking if we've found a loaded lib. */
943 if (global_found
!= NULL
944 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
945 & DYN_AS_NEEDED
) == 0)
948 if (s
->filename
== NULL
|| s
->the_bfd
== NULL
)
951 /* Don't look for a second non-loaded as-needed lib. */
952 if (global_found
!= NULL
953 && (bfd_elf_get_dyn_lib_class (s
->the_bfd
) & DYN_AS_NEEDED
) != 0)
956 if (filename_cmp (s
->filename
, global_needed
->name
) == 0)
962 if (s
->flags
.search_dirs
)
964 const char *f
= strrchr (s
->filename
, '/');
966 && filename_cmp (f
+ 1, global_needed
->name
) == 0)
973 soname
= bfd_elf_get_dt_soname (s
->the_bfd
);
975 && filename_cmp (soname
, global_needed
->name
) == 0)
982 /* This is called after all the input files have been opened. */
985 ldelf_after_open (int use_libpath
, int native
, int is_linux
, int is_freebsd
,
988 struct bfd_link_needed_list
*needed
, *l
;
989 struct elf_link_hash_table
*htab
;
993 after_open_default ();
995 htab
= elf_hash_table (&link_info
);
996 if (!is_elf_hash_table (htab
))
999 if (command_line
.out_implib_filename
)
1001 unlink_if_ordinary (command_line
.out_implib_filename
);
1002 link_info
.out_implib_bfd
1003 = bfd_openw (command_line
.out_implib_filename
,
1004 bfd_get_target (link_info
.output_bfd
));
1006 if (link_info
.out_implib_bfd
== NULL
)
1008 einfo (_("%F%P: %s: can't open for writing: %E\n"),
1009 command_line
.out_implib_filename
);
1013 if (ldelf_emit_note_gnu_build_id
!= NULL
)
1015 /* Find an ELF input. */
1016 for (abfd
= link_info
.input_bfds
;
1017 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
1018 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1019 && bfd_count_sections (abfd
) != 0
1020 && !((lang_input_statement_type
*) abfd
->usrdata
)->flags
.just_syms
)
1023 /* PR 10555: If there are no ELF input files do not try to
1024 create a .note.gnu-build-id section. */
1026 || !ldelf_setup_build_id (abfd
))
1028 free ((char *) ldelf_emit_note_gnu_build_id
);
1029 ldelf_emit_note_gnu_build_id
= NULL
;
1033 get_elf_backend_data (link_info
.output_bfd
)->setup_gnu_properties (&link_info
);
1035 if (bfd_link_relocatable (&link_info
))
1037 if (link_info
.execstack
== !link_info
.noexecstack
)
1039 /* PR ld/16744: If "-z [no]execstack" has been specified on the
1040 command line and we are perfoming a relocatable link then no
1041 PT_GNU_STACK segment will be created and so the
1042 linkinfo.[no]execstack values set in _handle_option() will have no
1043 effect. Instead we create a .note.GNU-stack section in much the
1044 same way as the assembler does with its --[no]execstack option. */
1045 flagword flags
= SEC_READONLY
| (link_info
.execstack
? SEC_CODE
: 0);
1046 (void) bfd_make_section_with_flags (link_info
.input_bfds
,
1047 ".note.GNU-stack", flags
);
1052 if (!link_info
.traditional_format
)
1055 bfd_boolean warn_eh_frame
= FALSE
;
1058 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1062 if (((lang_input_statement_type
*) abfd
->usrdata
)->flags
.just_syms
)
1065 for (s
= abfd
->sections
; s
&& type
< COMPACT_EH_HDR
; s
= s
->next
)
1067 const char *name
= bfd_get_section_name (abfd
, s
);
1069 if (bfd_is_abs_section (s
->output_section
))
1071 if (CONST_STRNEQ (name
, ".eh_frame_entry"))
1072 type
= COMPACT_EH_HDR
;
1073 else if (strcmp (name
, ".eh_frame") == 0 && s
->size
> 8)
1074 type
= DWARF2_EH_HDR
;
1083 else if (seen_type
!= type
)
1085 einfo (_("%F%P: compact frame descriptions incompatible with"
1086 " DWARF2 .eh_frame from %pB\n"),
1087 type
== DWARF2_EH_HDR
? abfd
: elfbfd
);
1092 && (type
== COMPACT_EH_HDR
1093 || link_info
.eh_frame_hdr_type
!= 0))
1095 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1098 warn_eh_frame
= TRUE
;
1102 if (seen_type
== COMPACT_EH_HDR
)
1103 link_info
.eh_frame_hdr_type
= COMPACT_EH_HDR
;
1107 const struct elf_backend_data
*bed
;
1109 bed
= get_elf_backend_data (elfbfd
);
1110 s
= bfd_make_section_with_flags (elfbfd
, ".eh_frame_hdr",
1111 bed
->dynamic_sec_flags
1114 && bfd_set_section_alignment (elfbfd
, s
, 2))
1116 htab
->eh_info
.hdr_sec
= s
;
1117 warn_eh_frame
= FALSE
;
1121 einfo (_("%P: warning: cannot create .eh_frame_hdr section,"
1122 " --eh-frame-hdr ignored\n"));
1125 /* Get the list of files which appear in DT_NEEDED entries in
1126 dynamic objects included in the link (often there will be none).
1127 For each such file, we want to track down the corresponding
1128 library, and include the symbol table in the link. This is what
1129 the runtime dynamic linker will do. Tracking the files down here
1130 permits one dynamic object to include another without requiring
1131 special action by the person doing the link. Note that the
1132 needed list can actually grow while we are stepping through this
1134 needed
= bfd_elf_get_needed_list (link_info
.output_bfd
, &link_info
);
1135 for (l
= needed
; l
!= NULL
; l
= l
->next
)
1137 struct bfd_link_needed_list
*ll
;
1138 struct dt_needed n
, nn
;
1141 /* If the lib that needs this one was --as-needed and wasn't
1142 found to be needed, then this lib isn't needed either. */
1144 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_AS_NEEDED
) != 0)
1147 /* Skip the lib if --no-copy-dt-needed-entries and
1148 --allow-shlib-undefined is in effect. */
1150 && link_info
.unresolved_syms_in_shared_libs
== RM_IGNORE
1151 && (bfd_elf_get_dyn_lib_class (l
->by
) & DYN_NO_ADD_NEEDED
) != 0)
1154 /* If we've already seen this file, skip it. */
1155 for (ll
= needed
; ll
!= l
; ll
= ll
->next
)
1157 || (bfd_elf_get_dyn_lib_class (ll
->by
) & DYN_AS_NEEDED
) == 0)
1158 && strcmp (ll
->name
, l
->name
) == 0)
1163 /* See if this file was included in the link explicitly. */
1165 global_found
= NULL
;
1166 lang_for_each_input_file (ldelf_check_needed
);
1167 if (global_found
!= NULL
1168 && (bfd_elf_get_dyn_lib_class (global_found
->the_bfd
)
1169 & DYN_AS_NEEDED
) == 0)
1176 info_msg (_("%s needed by %pB\n"), l
->name
, l
->by
);
1178 /* As-needed libs specified on the command line (or linker script)
1179 take priority over libs found in search dirs. */
1180 if (global_found
!= NULL
)
1182 nn
.name
= global_found
->filename
;
1183 if (ldelf_try_needed (&nn
, TRUE
, is_linux
))
1187 /* We need to find this file and include the symbol table. We
1188 want to search for the file in the same way that the dynamic
1189 linker will search. That means that we want to use
1190 rpath_link, rpath, then the environment variable
1191 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1192 entries (native only), then the linker script LIB_SEARCH_DIRS.
1193 We do not search using the -L arguments.
1195 We search twice. The first time, we skip objects which may
1196 introduce version mismatches. The second time, we force
1197 their use. See ldelf_vercheck comment. */
1198 for (force
= 0; force
< 2; force
++)
1201 search_dirs_type
*search
;
1203 struct bfd_link_needed_list
*rp
;
1206 if (ldelf_search_needed (command_line
.rpath_link
, &n
, force
,
1212 path
= command_line
.rpath
;
1215 path
= ldelf_add_sysroot (path
);
1216 found
= ldelf_search_needed (path
, &n
, force
,
1218 free ((char *) path
);
1225 if (command_line
.rpath_link
== NULL
1226 && command_line
.rpath
== NULL
)
1228 path
= (const char *) getenv ("LD_RUN_PATH");
1230 && ldelf_search_needed (path
, &n
, force
,
1234 path
= (const char *) getenv ("LD_LIBRARY_PATH");
1236 && ldelf_search_needed (path
, &n
, force
,
1243 rp
= bfd_elf_get_runpath_list (link_info
.output_bfd
, &link_info
);
1244 for (; !found
&& rp
!= NULL
; rp
= rp
->next
)
1246 path
= ldelf_add_sysroot (rp
->name
);
1247 found
= (rp
->by
== l
->by
1248 && ldelf_search_needed (path
, &n
, force
,
1249 is_linux
, elfsize
));
1250 free ((char *) path
);
1256 && ldelf_check_ld_elf_hints (l
, force
, elfsize
))
1260 && ldelf_check_ld_so_conf (l
, force
, elfsize
))
1264 len
= strlen (l
->name
);
1265 for (search
= search_head
; search
!= NULL
; search
= search
->next
)
1269 if (search
->cmdline
)
1271 filename
= (char *) xmalloc (strlen (search
->name
) + len
+ 2);
1272 sprintf (filename
, "%s/%s", search
->name
, l
->name
);
1274 if (ldelf_try_needed (&nn
, force
, is_linux
))
1285 einfo (_("%P: warning: %s, needed by %pB, not found "
1286 "(try using -rpath or -rpath-link)\n"),
1290 if (link_info
.eh_frame_hdr_type
== COMPACT_EH_HDR
)
1291 if (!bfd_elf_parse_eh_frame_entries (NULL
, &link_info
))
1292 einfo (_("%F%P: failed to parse EH frame entries\n"));
1295 static bfd_size_type
1296 id_note_section_size (bfd
*abfd ATTRIBUTE_UNUSED
)
1298 const char *style
= ldelf_emit_note_gnu_build_id
;
1300 bfd_size_type build_id_size
;
1302 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1303 size
= (size
+ 3) & -(bfd_size_type
) 4;
1305 build_id_size
= compute_build_id_size (style
);
1307 size
+= build_id_size
;
1315 write_build_id (bfd
*abfd
)
1317 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1318 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1321 Elf_Internal_Shdr
*i_shdr
;
1322 unsigned char *contents
, *id_bits
;
1325 Elf_External_Note
*e_note
;
1327 style
= t
->o
->build_id
.style
;
1328 asec
= t
->o
->build_id
.sec
;
1329 if (bfd_is_abs_section (asec
->output_section
))
1331 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
1332 " --build-id ignored\n"));
1335 i_shdr
= &elf_section_data (asec
->output_section
)->this_hdr
;
1337 if (i_shdr
->contents
== NULL
)
1339 if (asec
->contents
== NULL
)
1340 asec
->contents
= (unsigned char *) xmalloc (asec
->size
);
1341 contents
= asec
->contents
;
1344 contents
= i_shdr
->contents
+ asec
->output_offset
;
1346 e_note
= (Elf_External_Note
*) contents
;
1347 size
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
1348 size
= (size
+ 3) & -(bfd_size_type
) 4;
1349 id_bits
= contents
+ size
;
1350 size
= asec
->size
- size
;
1352 bfd_h_put_32 (abfd
, sizeof "GNU", &e_note
->namesz
);
1353 bfd_h_put_32 (abfd
, size
, &e_note
->descsz
);
1354 bfd_h_put_32 (abfd
, NT_GNU_BUILD_ID
, &e_note
->type
);
1355 memcpy (e_note
->name
, "GNU", sizeof "GNU");
1357 generate_build_id (abfd
, style
, bed
->s
->checksum_contents
, id_bits
, size
);
1359 position
= i_shdr
->sh_offset
+ asec
->output_offset
;
1361 return (bfd_seek (abfd
, position
, SEEK_SET
) == 0
1362 && bfd_bwrite (contents
, size
, abfd
) == size
);
1365 /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
1368 ldelf_setup_build_id (bfd
*ibfd
)
1374 size
= id_note_section_size (ibfd
);
1377 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1381 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_IN_MEMORY
1382 | SEC_LINKER_CREATED
| SEC_READONLY
| SEC_DATA
);
1383 s
= bfd_make_section_with_flags (ibfd
, ".note.gnu.build-id", flags
);
1384 if (s
!= NULL
&& bfd_set_section_alignment (ibfd
, s
, 2))
1386 struct elf_obj_tdata
*t
= elf_tdata (link_info
.output_bfd
);
1387 t
->o
->build_id
.after_write_object_contents
= &write_build_id
;
1388 t
->o
->build_id
.style
= ldelf_emit_note_gnu_build_id
;
1389 t
->o
->build_id
.sec
= s
;
1390 elf_section_type (s
) = SHT_NOTE
;
1395 einfo (_("%P: warning: cannot create .note.gnu.build-id section,"
1396 " --build-id ignored\n"));
1400 /* Look through an expression for an assignment statement. */
1403 ldelf_find_exp_assignment (etree_type
*exp
)
1405 bfd_boolean provide
= FALSE
;
1407 switch (exp
->type
.node_class
)
1410 case etree_provided
:
1414 /* We call record_link_assignment even if the symbol is defined.
1415 This is because if it is defined by a dynamic object, we
1416 actually want to use the value defined by the linker script,
1417 not the value from the dynamic object (because we are setting
1418 symbols like etext). If the symbol is defined by a regular
1419 object, then, as it happens, calling record_link_assignment
1421 if (strcmp (exp
->assign
.dst
, ".") != 0)
1423 if (!bfd_elf_record_link_assignment (link_info
.output_bfd
,
1425 exp
->assign
.dst
, provide
,
1426 exp
->assign
.hidden
))
1427 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1430 ldelf_find_exp_assignment (exp
->assign
.src
);
1434 ldelf_find_exp_assignment (exp
->binary
.lhs
);
1435 ldelf_find_exp_assignment (exp
->binary
.rhs
);
1439 ldelf_find_exp_assignment (exp
->trinary
.cond
);
1440 ldelf_find_exp_assignment (exp
->trinary
.lhs
);
1441 ldelf_find_exp_assignment (exp
->trinary
.rhs
);
1445 ldelf_find_exp_assignment (exp
->unary
.child
);
1453 /* This is called by the before_allocation routine via
1454 lang_for_each_statement. It locates any assignment statements, and
1455 tells the ELF backend about them, in case they are assignments to
1456 symbols which are referred to by dynamic objects. */
1459 ldelf_find_statement_assignment (lang_statement_union_type
*s
)
1461 if (s
->header
.type
== lang_assignment_statement_enum
)
1462 ldelf_find_exp_assignment (s
->assignment_statement
.exp
);
1465 /* Used by before_allocation and handle_option. */
1468 ldelf_append_to_separated_string (char **to
, char *op_arg
)
1471 *to
= xstrdup (op_arg
);
1474 size_t to_len
= strlen (*to
);
1475 size_t op_arg_len
= strlen (op_arg
);
1479 /* First see whether OPTARG is already in the path. */
1482 if (strncmp (op_arg
, cp
, op_arg_len
) == 0
1483 && (cp
[op_arg_len
] == 0
1484 || cp
[op_arg_len
] == config
.rpath_separator
))
1488 /* Not yet found. */
1489 cp
= strchr (cp
, config
.rpath_separator
);
1497 buf
= xmalloc (to_len
+ op_arg_len
+ 2);
1498 sprintf (buf
, "%s%c%s", *to
,
1499 config
.rpath_separator
, op_arg
);
1506 /* This is called after the sections have been attached to output
1507 sections, but before any sizes or addresses have been set. */
1510 ldelf_before_allocation (char *audit
, char *depaudit
,
1511 const char *default_interpreter_name
)
1516 struct bfd_link_hash_entry
*ehdr_start
= NULL
;
1517 unsigned char ehdr_start_save_type
= 0;
1518 char ehdr_start_save_u
[sizeof ehdr_start
->u
1519 - sizeof ehdr_start
->u
.def
.next
] = "";
1521 if (is_elf_hash_table (link_info
.hash
))
1523 _bfd_elf_tls_setup (link_info
.output_bfd
, &link_info
);
1525 /* Make __ehdr_start hidden if it has been referenced, to
1526 prevent the symbol from being dynamic. */
1527 if (!bfd_link_relocatable (&link_info
))
1529 struct elf_link_hash_table
*htab
= elf_hash_table (&link_info
);
1530 struct elf_link_hash_entry
*h
1531 = elf_link_hash_lookup (htab
, "__ehdr_start", FALSE
, FALSE
, TRUE
);
1533 /* Only adjust the export class if the symbol was referenced
1534 and not defined, otherwise leave it alone. */
1536 && (h
->root
.type
== bfd_link_hash_new
1537 || h
->root
.type
== bfd_link_hash_undefined
1538 || h
->root
.type
== bfd_link_hash_undefweak
1539 || h
->root
.type
== bfd_link_hash_common
))
1541 const struct elf_backend_data
*bed
;
1542 bed
= get_elf_backend_data (link_info
.output_bfd
);
1543 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, TRUE
);
1544 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
1545 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
1546 /* Don't leave the symbol undefined. Undefined hidden
1547 symbols typically won't have dynamic relocations, but
1548 we most likely will need dynamic relocations for
1549 __ehdr_start if we are building a PIE or shared
1551 ehdr_start
= &h
->root
;
1552 ehdr_start_save_type
= ehdr_start
->type
;
1553 memcpy (ehdr_start_save_u
,
1554 (char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1555 sizeof ehdr_start_save_u
);
1556 ehdr_start
->type
= bfd_link_hash_defined
;
1557 ehdr_start
->u
.def
.section
= bfd_abs_section_ptr
;
1558 ehdr_start
->u
.def
.value
= 0;
1562 /* If we are going to make any variable assignments, we need to
1563 let the ELF backend know about them in case the variables are
1564 referred to by dynamic objects. */
1565 lang_for_each_statement (ldelf_find_statement_assignment
);
1568 /* Let the ELF backend work out the sizes of any sections required
1569 by dynamic linking. */
1570 rpath
= command_line
.rpath
;
1572 rpath
= (const char *) getenv ("LD_RUN_PATH");
1574 for (abfd
= link_info
.input_bfds
; abfd
; abfd
= abfd
->link
.next
)
1575 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1577 const char *audit_libs
= elf_dt_audit (abfd
);
1579 /* If the input bfd contains an audit entry, we need to add it as
1580 a dep audit entry. */
1581 if (audit_libs
&& *audit_libs
!= '\0')
1583 char *cp
= xstrdup (audit_libs
);
1587 char *cp2
= strchr (cp
, config
.rpath_separator
);
1595 if (cp
!= NULL
&& *cp
!= '\0')
1596 ldelf_append_to_separated_string (&depaudit
, cp
);
1598 cp
= more
? ++cp2
: NULL
;
1604 if (! (bfd_elf_size_dynamic_sections
1605 (link_info
.output_bfd
, command_line
.soname
, rpath
,
1606 command_line
.filter_shlib
, audit
, depaudit
,
1607 (const char * const *) command_line
.auxiliary_filters
,
1608 &link_info
, &sinterp
)))
1609 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1611 if (sinterp
!= NULL
)
1613 /* Let the user override the dynamic linker we are using. */
1614 if (command_line
.interpreter
!= NULL
)
1615 default_interpreter_name
= command_line
.interpreter
;
1616 if (default_interpreter_name
!= NULL
)
1618 sinterp
->contents
= (bfd_byte
*) default_interpreter_name
;
1619 sinterp
->size
= strlen ((char *) sinterp
->contents
) + 1;
1623 /* Look for any sections named .gnu.warning. As a GNU extensions,
1624 we treat such sections as containing warning messages. We print
1625 out the warning message, and then zero out the section size so
1626 that it does not get copied into the output file. */
1629 LANG_FOR_EACH_INPUT_STATEMENT (is
)
1635 if (is
->flags
.just_syms
)
1638 s
= bfd_get_section_by_name (is
->the_bfd
, ".gnu.warning");
1643 msg
= (char *) xmalloc ((size_t) (sz
+ 1));
1644 if (! bfd_get_section_contents (is
->the_bfd
, s
, msg
,
1646 einfo (_("%F%P: %pB: can't read contents of section .gnu.warning: %E\n"),
1649 (*link_info
.callbacks
->warning
) (&link_info
, msg
,
1650 (const char *) NULL
, is
->the_bfd
,
1651 (asection
*) NULL
, (bfd_vma
) 0);
1654 /* Clobber the section size, so that we don't waste space
1655 copying the warning into the output file. If we've already
1656 sized the output section, adjust its size. The adjustment
1657 is on rawsize because targets that size sections early will
1658 have called lang_reset_memory_regions after sizing. */
1659 if (s
->output_section
!= NULL
1660 && s
->output_section
->rawsize
>= s
->size
)
1661 s
->output_section
->rawsize
-= s
->size
;
1665 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1666 warning section don't get copied to the output. */
1667 s
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
1671 before_allocation_default ();
1673 if (!bfd_elf_size_dynsym_hash_dynstr (link_info
.output_bfd
, &link_info
))
1674 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1676 if (ehdr_start
!= NULL
)
1678 /* If we twiddled __ehdr_start to defined earlier, put it back
1680 ehdr_start
->type
= ehdr_start_save_type
;
1681 memcpy ((char *) &ehdr_start
->u
+ sizeof ehdr_start
->u
.def
.next
,
1683 sizeof ehdr_start_save_u
);
1686 /* Try to open a dynamic archive. This is where we know that ELF
1687 dynamic libraries have an extension of .so (or .sl on oddball systems
1691 ldelf_open_dynamic_archive (const char *arch
, search_dirs_type
*search
,
1692 lang_input_statement_type
*entry
)
1694 const char *filename
;
1697 bfd_boolean opened
= FALSE
;
1699 if (! entry
->flags
.maybe_archive
)
1702 filename
= entry
->filename
;
1703 len
= strlen (search
->name
) + strlen (filename
);
1704 if (entry
->flags
.full_name_provided
)
1707 string
= (char *) xmalloc (len
);
1708 sprintf (string
, "%s/%s", search
->name
, filename
);
1714 len
+= strlen (arch
) + sizeof "/lib.so";
1715 #ifdef EXTRA_SHLIB_EXTENSION
1716 xlen
= (strlen (EXTRA_SHLIB_EXTENSION
) > 3
1717 ? strlen (EXTRA_SHLIB_EXTENSION
) - 3
1720 string
= (char *) xmalloc (len
+ xlen
);
1721 sprintf (string
, "%s/lib%s%s.so", search
->name
, filename
, arch
);
1722 #ifdef EXTRA_SHLIB_EXTENSION
1723 /* Try the .so extension first. If that fails build a new filename
1724 using EXTRA_SHLIB_EXTENSION. */
1725 opened
= ldfile_try_open_bfd (string
, entry
);
1727 strcpy (string
+ len
- 4, EXTRA_SHLIB_EXTENSION
);
1731 if (!opened
&& !ldfile_try_open_bfd (string
, entry
))
1737 entry
->filename
= string
;
1739 /* We have found a dynamic object to include in the link. The ELF
1740 backend linker will create a DT_NEEDED entry in the .dynamic
1741 section naming this file. If this file includes a DT_SONAME
1742 entry, it will be used. Otherwise, the ELF linker will just use
1743 the name of the file. For an archive found by searching, like
1744 this one, the DT_NEEDED entry should consist of just the name of
1745 the file, without the path information used to find it. Note
1746 that we only need to do this if we have a dynamic object; an
1747 archive will never be referenced by a DT_NEEDED entry.
1749 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1750 very pretty. I haven't been able to think of anything that is
1752 if (bfd_check_format (entry
->the_bfd
, bfd_object
)
1753 && (entry
->the_bfd
->flags
& DYNAMIC
) != 0)
1755 ASSERT (entry
->flags
.maybe_archive
&& entry
->flags
.search_dirs
);
1757 /* Rather than duplicating the logic above. Just use the
1758 filename we recorded earlier. */
1760 if (!entry
->flags
.full_name_provided
)
1761 filename
= lbasename (entry
->filename
);
1762 bfd_elf_set_dt_needed_name (entry
->the_bfd
, filename
);
1768 /* A variant of lang_output_section_find used by place_orphan. */
1770 static lang_output_section_statement_type
*
1771 output_rel_find (int isdyn
, int rela
)
1773 lang_output_section_statement_type
*lookup
;
1774 lang_output_section_statement_type
*last
= NULL
;
1775 lang_output_section_statement_type
*last_alloc
= NULL
;
1776 lang_output_section_statement_type
*last_ro_alloc
= NULL
;
1777 lang_output_section_statement_type
*last_rel
= NULL
;
1778 lang_output_section_statement_type
*last_rel_alloc
= NULL
;
1780 for (lookup
= &lang_os_list
.head
->output_section_statement
;
1782 lookup
= lookup
->next
)
1784 if (lookup
->constraint
>= 0
1785 && CONST_STRNEQ (lookup
->name
, ".rel"))
1787 int lookrela
= lookup
->name
[4] == 'a';
1789 /* .rel.dyn must come before all other reloc sections, to suit
1794 /* Don't place after .rel.plt as doing so results in wrong
1796 if (strcmp (".plt", lookup
->name
+ 4 + lookrela
) == 0)
1799 if (rela
== lookrela
|| last_rel
== NULL
)
1801 if ((rela
== lookrela
|| last_rel_alloc
== NULL
)
1802 && lookup
->bfd_section
!= NULL
1803 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1804 last_rel_alloc
= lookup
;
1808 if (lookup
->bfd_section
!= NULL
1809 && (lookup
->bfd_section
->flags
& SEC_ALLOC
) != 0)
1811 last_alloc
= lookup
;
1812 if ((lookup
->bfd_section
->flags
& SEC_READONLY
) != 0)
1813 last_ro_alloc
= lookup
;
1818 return last_rel_alloc
;
1824 return last_ro_alloc
;
1832 /* Return whether IN is suitable to be part of OUT. */
1835 elf_orphan_compatible (asection
*in
, asection
*out
)
1837 /* Non-zero sh_info implies a section with SHF_INFO_LINK with
1838 unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
1839 section where sh_info specifies a symbol table. (We won't see
1840 SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
1841 can't merge SHT_REL/SHT_RELA using differing symbol tables, and
1842 shouldn't merge sections with differing unknown semantics. */
1843 if (elf_section_data (out
)->this_hdr
.sh_info
1844 != elf_section_data (in
)->this_hdr
.sh_info
)
1846 /* We can't merge with member of output section group nor merge two
1847 sections with differing SHF_EXCLUDE when doing a relocatable link.
1849 if (bfd_link_relocatable (&link_info
)
1850 && (elf_next_in_group (out
) != NULL
1851 || ((elf_section_flags (out
) ^ elf_section_flags (in
))
1852 & SHF_EXCLUDE
) != 0))
1854 return _bfd_elf_match_sections_by_type (link_info
.output_bfd
, out
,
1858 /* Place an orphan section. We use this to put random SHF_ALLOC
1859 sections in the right segment. */
1861 lang_output_section_statement_type
*
1862 ldelf_place_orphan (asection
*s
, const char *secname
, int constraint
)
1864 static struct orphan_save hold
[] =
1867 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
,
1870 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1873 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_THREAD_LOCAL
,
1876 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
,
1882 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1885 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
,
1888 SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_SMALL_DATA
,
1894 enum orphan_save_index
1906 static int orphan_init_done
= 0;
1907 struct orphan_save
*place
;
1908 lang_output_section_statement_type
*after
;
1909 lang_output_section_statement_type
*os
;
1910 lang_output_section_statement_type
*match_by_name
= NULL
;
1912 int elfinput
= s
->owner
->xvec
->flavour
== bfd_target_elf_flavour
;
1913 int elfoutput
= link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
;
1914 unsigned int sh_type
= elfinput
? elf_section_type (s
) : SHT_NULL
;
1918 if (!bfd_link_relocatable (&link_info
)
1919 && link_info
.combreloc
1920 && (s
->flags
& SEC_ALLOC
))
1926 secname
= ".rela.dyn";
1930 secname
= ".rel.dyn";
1936 else if (CONST_STRNEQ (secname
, ".rel"))
1938 secname
= secname
[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1943 if (!bfd_link_relocatable (&link_info
)
1946 && (s
->flags
& SEC_ALLOC
) != 0
1947 && (elf_tdata (s
->owner
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
1948 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
1950 /* Find the output mbind section with the same type, attributes
1951 and sh_info field. */
1952 for (os
= &lang_os_list
.head
->output_section_statement
;
1955 if (os
->bfd_section
!= NULL
1956 && !bfd_is_abs_section (os
->bfd_section
)
1957 && (elf_section_flags (os
->bfd_section
) & SHF_GNU_MBIND
) != 0
1958 && ((s
->flags
& (SEC_ALLOC
1963 == (os
->bfd_section
->flags
& (SEC_ALLOC
1968 && (elf_section_data (os
->bfd_section
)->this_hdr
.sh_info
1969 == elf_section_data (s
)->this_hdr
.sh_info
))
1971 lang_add_section (&os
->children
, s
, NULL
, os
);
1975 /* Create the output mbind section with the ".mbind." prefix
1977 if ((s
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
1978 secname
= ".mbind.bss";
1979 else if ((s
->flags
& SEC_READONLY
) == 0)
1980 secname
= ".mbind.data";
1981 else if ((s
->flags
& SEC_CODE
) == 0)
1982 secname
= ".mbind.rodata";
1984 secname
= ".mbind.text";
1985 elf_tdata (link_info
.output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1988 /* Look through the script to see where to place this section. The
1989 script includes entries added by previous lang_insert_orphan
1990 calls, so this loop puts multiple compatible orphans of the same
1991 name into a single output section. */
1992 if (constraint
== 0)
1993 for (os
= lang_output_section_find (secname
);
1995 os
= next_matching_output_section_statement (os
, 0))
1997 /* If we don't match an existing output section, tell
1998 lang_insert_orphan to create a new output section. */
1999 constraint
= SPECIAL
;
2001 /* Check to see if we already have an output section statement
2002 with this name, and its bfd section has compatible flags.
2003 If the section already exists but does not have any flags
2004 set, then it has been created by the linker, possibly as a
2005 result of a --section-start command line switch. */
2006 if (os
->bfd_section
!= NULL
2007 && (os
->bfd_section
->flags
== 0
2008 || (((s
->flags
^ os
->bfd_section
->flags
)
2009 & (SEC_LOAD
| SEC_ALLOC
)) == 0
2012 || elf_orphan_compatible (s
, os
->bfd_section
)))))
2014 lang_add_section (&os
->children
, s
, NULL
, os
);
2018 /* Save unused output sections in case we can match them
2019 against orphans later. */
2020 if (os
->bfd_section
== NULL
)
2024 /* If we didn't match an active output section, see if we matched an
2025 unused one and use that. */
2028 lang_add_section (&match_by_name
->children
, s
, NULL
, match_by_name
);
2029 return match_by_name
;
2032 if (!orphan_init_done
)
2034 struct orphan_save
*ho
;
2036 for (ho
= hold
; ho
< hold
+ sizeof (hold
) / sizeof (hold
[0]); ++ho
)
2037 if (ho
->name
!= NULL
)
2039 ho
->os
= lang_output_section_find (ho
->name
);
2040 if (ho
->os
!= NULL
&& ho
->os
->flags
== 0)
2041 ho
->os
->flags
= ho
->flags
;
2043 orphan_init_done
= 1;
2046 /* If this is a final link, then always put .gnu.warning.SYMBOL
2047 sections into the .text section to get them out of the way. */
2048 if (bfd_link_executable (&link_info
)
2049 && CONST_STRNEQ (s
->name
, ".gnu.warning.")
2050 && hold
[orphan_text
].os
!= NULL
)
2052 os
= hold
[orphan_text
].os
;
2053 lang_add_section (&os
->children
, s
, NULL
, os
);
2058 if (!bfd_link_relocatable (&link_info
))
2061 while ((nexts
= bfd_get_next_section_by_name (nexts
->owner
, nexts
))
2063 if (nexts
->output_section
== NULL
2064 && (nexts
->flags
& SEC_EXCLUDE
) == 0
2065 && ((nexts
->flags
^ flags
) & (SEC_LOAD
| SEC_ALLOC
)) == 0
2066 && (nexts
->owner
->flags
& DYNAMIC
) == 0
2067 && nexts
->owner
->usrdata
!= NULL
2068 && !(((lang_input_statement_type
*) nexts
->owner
->usrdata
)
2070 && _bfd_elf_match_sections_by_type (nexts
->owner
, nexts
,
2072 flags
= (((flags
^ SEC_READONLY
)
2073 | (nexts
->flags
^ SEC_READONLY
))
2077 /* Decide which segment the section should go in based on the
2078 section name and section flags. We put loadable .note sections
2079 right after the .interp section, so that the PT_NOTE segment is
2080 stored right after the program headers where the OS can read it
2081 in the first page. */
2084 if ((flags
& (SEC_ALLOC
| SEC_DEBUGGING
)) == 0)
2085 place
= &hold
[orphan_nonalloc
];
2086 else if ((flags
& SEC_ALLOC
) == 0)
2088 else if ((flags
& SEC_LOAD
) != 0
2090 ? sh_type
== SHT_NOTE
2091 : CONST_STRNEQ (secname
, ".note")))
2092 place
= &hold
[orphan_interp
];
2093 else if ((flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_THREAD_LOCAL
)) == 0)
2094 place
= &hold
[orphan_bss
];
2095 else if ((flags
& SEC_SMALL_DATA
) != 0)
2096 place
= &hold
[orphan_sdata
];
2097 else if ((flags
& SEC_THREAD_LOCAL
) != 0)
2098 place
= &hold
[orphan_tdata
];
2099 else if ((flags
& SEC_READONLY
) == 0)
2100 place
= &hold
[orphan_data
];
2101 else if ((flags
& SEC_LOAD
) != 0
2103 ? sh_type
== SHT_RELA
|| sh_type
== SHT_REL
2104 : CONST_STRNEQ (secname
, ".rel")))
2105 place
= &hold
[orphan_rel
];
2106 else if ((flags
& SEC_CODE
) == 0)
2107 place
= &hold
[orphan_rodata
];
2109 place
= &hold
[orphan_text
];
2114 if (place
->os
== NULL
)
2116 if (place
->name
!= NULL
)
2117 place
->os
= lang_output_section_find (place
->name
);
2120 int rela
= elfinput
? sh_type
== SHT_RELA
: secname
[4] == 'a';
2121 place
->os
= output_rel_find (isdyn
, rela
);
2127 = lang_output_section_find_by_flags (s
, flags
, &place
->os
,
2128 _bfd_elf_match_sections_by_type
);
2130 /* *ABS* is always the first output section statement. */
2131 after
= &lang_os_list
.head
->output_section_statement
;
2134 return lang_insert_orphan (s
, secname
, constraint
, after
, place
, NULL
, NULL
);