]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldelf.c
ld: Initial DT_RELR support
[thirdparty/binutils-gdb.git] / ld / ldelf.c
CommitLineData
075a2b89 1/* ELF emulation code for targets using elf.em.
a2c58332 2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
d871d478
AM
3
4 This file is part of the GNU Binutils.
5
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.
10
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.
15
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. */
20
21#include "sysdep.h"
22#include "bfd.h"
23#include "libiberty.h"
24#include "filenames.h"
0aa0727c 25#include "safe-ctype.h"
0b4453c7 26#include "bfdlink.h"
1ff6de03 27#include "ctf-api.h"
d871d478
AM
28#include "ld.h"
29#include "ldmain.h"
30#include "ldmisc.h"
31#include "ldexp.h"
32#include "ldlang.h"
33#include "ldfile.h"
34#include "ldemul.h"
35#include "ldbuildid.h"
36#include <ldgram.h>
37#include "elf-bfd.h"
38#ifdef HAVE_GLOB
39#include <glob.h>
40#endif
41#include "ldelf.h"
42
43struct dt_needed
44{
45 bfd *by;
46 const char *name;
47};
48
49/* Style of .note.gnu.build-id section. */
50const char *ldelf_emit_note_gnu_build_id;
51
52/* These variables are required to pass information back and forth
53 between after_open and check_needed and stat_needed and vercheck. */
54
55static struct bfd_link_needed_list *global_needed;
56static lang_input_statement_type *global_found;
57static struct stat global_stat;
58static struct bfd_link_needed_list *global_vercheck_needed;
f38a2680 59static bool global_vercheck_failed;
d871d478
AM
60
61void
62ldelf_after_parse (void)
63{
64 if (bfd_link_pie (&link_info))
65 link_info.flags_1 |= (bfd_vma) DF_1_PIE;
66
67 if (bfd_link_executable (&link_info)
68 && link_info.nointerp)
69 {
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;
73 }
6a91be86
L
74
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;
78
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;
82
d871d478 83 after_parse_default ();
74e315db
L
84 if (link_info.commonpagesize > link_info.maxpagesize)
85 einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"),
86 link_info.commonpagesize, link_info.maxpagesize);
d871d478
AM
87}
88
89/* Handle the generation of DT_NEEDED tags. */
90
f38a2680 91bool
d871d478
AM
92ldelf_load_symbols (lang_input_statement_type *entry)
93{
94 int link_class = 0;
95
96 /* Tell the ELF linker that we don't want the output file to have a
97 DT_NEEDED entry for this file, unless it is used to resolve
98 references in a regular object. */
99 if (entry->flags.add_DT_NEEDED_for_regular)
100 link_class = DYN_AS_NEEDED;
101
102 /* Tell the ELF linker that we don't want the output file to have a
103 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
104 this file at all. */
105 if (!entry->flags.add_DT_NEEDED_for_dynamic)
106 link_class |= DYN_NO_ADD_NEEDED;
107
108 if (entry->flags.just_syms
109 && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
110 einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
111 entry->the_bfd);
112
113 if (link_class == 0
114 || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
f38a2680 115 return false;
d871d478
AM
116
117 bfd_elf_set_dyn_lib_class (entry->the_bfd,
118 (enum dynamic_lib_link_class) link_class);
119
120 /* Continue on with normal load_symbols processing. */
f38a2680 121 return false;
d871d478
AM
122}
123
124/* On Linux, it's possible to have different versions of the same
125 shared library linked against different versions of libc. The
126 dynamic linker somehow tags which libc version to use in
127 /etc/ld.so.cache, and, based on the libc that it sees in the
128 executable, chooses which version of the shared library to use.
129
130 We try to do a similar check here by checking whether this shared
131 library needs any other shared libraries which may conflict with
132 libraries we have already included in the link. If it does, we
133 skip it, and try to find another shared library farther on down the
134 link path.
135
136 This is called via lang_for_each_input_file.
137 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
138 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
139 a conflicting version. */
140
141static void
142ldelf_vercheck (lang_input_statement_type *s)
143{
144 const char *soname;
145 struct bfd_link_needed_list *l;
146
147 if (global_vercheck_failed)
148 return;
149 if (s->the_bfd == NULL
150 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
151 return;
152
153 soname = bfd_elf_get_dt_soname (s->the_bfd);
154 if (soname == NULL)
155 soname = lbasename (bfd_get_filename (s->the_bfd));
156
157 for (l = global_vercheck_needed; l != NULL; l = l->next)
158 {
159 const char *suffix;
160
161 if (filename_cmp (soname, l->name) == 0)
162 {
163 /* Probably can't happen, but it's an easy check. */
164 continue;
165 }
166
167 if (strchr (l->name, '/') != NULL)
168 continue;
169
170 suffix = strstr (l->name, ".so.");
171 if (suffix == NULL)
172 continue;
173
174 suffix += sizeof ".so." - 1;
175
176 if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
177 {
178 /* Here we know that S is a dynamic object FOO.SO.VER1, and
179 the object we are considering needs a dynamic object
180 FOO.SO.VER2, and VER1 and VER2 are different. This
181 appears to be a version mismatch, so we tell the caller
182 to try a different version of this library. */
f38a2680 183 global_vercheck_failed = true;
d871d478
AM
184 return;
185 }
186 }
187}
188
189
190/* See if an input file matches a DT_NEEDED entry by running stat on
191 the file. */
192
193static void
194ldelf_stat_needed (lang_input_statement_type *s)
195{
196 struct stat st;
197 const char *suffix;
198 const char *soname;
199
200 if (global_found != NULL)
201 return;
202 if (s->the_bfd == NULL)
203 return;
204
205 /* If this input file was an as-needed entry, and wasn't found to be
206 needed at the stage it was linked, then don't say we have loaded it. */
207 if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
208 return;
209
210 if (bfd_stat (s->the_bfd, &st) != 0)
211 {
212 einfo (_("%P: %pB: bfd_stat failed: %E\n"), s->the_bfd);
213 return;
214 }
215
216 /* Some operating systems, e.g. Windows, do not provide a meaningful
217 st_ino; they always set it to zero. (Windows does provide a
218 meaningful st_dev.) Do not indicate a duplicate library in that
219 case. While there is no guarantee that a system that provides
220 meaningful inode numbers will never set st_ino to zero, this is
221 merely an optimization, so we do not need to worry about false
222 negatives. */
223 if (st.st_dev == global_stat.st_dev
224 && st.st_ino == global_stat.st_ino
225 && st.st_ino != 0)
226 {
227 global_found = s;
228 return;
229 }
230
231 /* We issue a warning if it looks like we are including two
232 different versions of the same shared library. For example,
233 there may be a problem if -lc picks up libc.so.6 but some other
234 shared library has a DT_NEEDED entry of libc.so.5. This is a
235 heuristic test, and it will only work if the name looks like
236 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
237 If we really want to issue warnings about mixing version numbers
238 of shared libraries, we need to find a better way. */
239
240 if (strchr (global_needed->name, '/') != NULL)
241 return;
242 suffix = strstr (global_needed->name, ".so.");
243 if (suffix == NULL)
244 return;
245 suffix += sizeof ".so." - 1;
246
247 soname = bfd_elf_get_dt_soname (s->the_bfd);
248 if (soname == NULL)
249 soname = lbasename (s->filename);
250
251 if (filename_ncmp (soname, global_needed->name,
252 suffix - global_needed->name) == 0)
253 einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
254 global_needed->name, global_needed->by, soname);
255}
256
257/* This function is called for each possible name for a dynamic object
258 named by a DT_NEEDED entry. The FORCE parameter indicates whether
259 to skip the check for a conflicting version. */
260
f38a2680 261static bool
d871d478
AM
262ldelf_try_needed (struct dt_needed *needed, int force, int is_linux)
263{
264 bfd *abfd;
265 const char *name = needed->name;
266 const char *soname;
267 int link_class;
268
269 abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
270 if (abfd == NULL)
271 {
272 if (verbose)
273 info_msg (_("attempt to open %s failed\n"), name);
f38a2680 274 return false;
d871d478
AM
275 }
276
f37b21b4
RM
277 track_dependency_files (name);
278
d871d478
AM
279 /* Linker needs to decompress sections. */
280 abfd->flags |= BFD_DECOMPRESS;
281
282 if (! bfd_check_format (abfd, bfd_object))
283 {
284 bfd_close (abfd);
f38a2680 285 return false;
d871d478
AM
286 }
287 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
288 {
289 bfd_close (abfd);
f38a2680 290 return false;
d871d478
AM
291 }
292
293 /* For DT_NEEDED, they have to match. */
294 if (abfd->xvec != link_info.output_bfd->xvec)
295 {
296 bfd_close (abfd);
f38a2680 297 return false;
d871d478
AM
298 }
299
300 /* Check whether this object would include any conflicting library
301 versions. If FORCE is set, then we skip this check; we use this
302 the second time around, if we couldn't find any compatible
303 instance of the shared library. */
304
305 if (!force)
306 {
307 struct bfd_link_needed_list *needs;
308
309 if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
310 einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd);
311
312 if (needs != NULL)
313 {
314 global_vercheck_needed = needs;
f38a2680 315 global_vercheck_failed = false;
d871d478
AM
316 lang_for_each_input_file (ldelf_vercheck);
317 if (global_vercheck_failed)
318 {
319 bfd_close (abfd);
320 /* Return FALSE to force the caller to move on to try
321 another file on the search path. */
f38a2680 322 return false;
d871d478
AM
323 }
324
325 /* But wait! It gets much worse. On Linux, if a shared
326 library does not use libc at all, we are supposed to skip
327 it the first time around in case we encounter a shared
328 library later on with the same name which does use the
329 version of libc that we want. This is much too horrible
330 to use on any system other than Linux. */
331 if (is_linux)
332 {
333 struct bfd_link_needed_list *l;
334
335 for (l = needs; l != NULL; l = l->next)
08dedd66 336 if (startswith (l->name, "libc.so"))
d871d478
AM
337 break;
338 if (l == NULL)
339 {
340 bfd_close (abfd);
f38a2680 341 return false;
d871d478
AM
342 }
343 }
344 }
345 }
346
347 /* We've found a dynamic object matching the DT_NEEDED entry. */
348
349 /* We have already checked that there is no other input file of the
350 same name. We must now check again that we are not including the
351 same file twice. We need to do this because on many systems
352 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
353 reference libc.so.1. If we have already included libc.so, we
354 don't want to include libc.so.1 if they are the same file, and we
355 can only check that using stat. */
356
357 if (bfd_stat (abfd, &global_stat) != 0)
358 einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd);
359
360 /* First strip off everything before the last '/'. */
361 soname = lbasename (bfd_get_filename (abfd));
362
363 if (verbose)
364 info_msg (_("found %s at %s\n"), soname, name);
365
366 global_found = NULL;
367 lang_for_each_input_file (ldelf_stat_needed);
368 if (global_found != NULL)
369 {
370 /* Return TRUE to indicate that we found the file, even though
371 we aren't going to do anything with it. */
f38a2680 372 return true;
d871d478
AM
373 }
374
375 /* Specify the soname to use. */
376 bfd_elf_set_dt_needed_name (abfd, soname);
377
378 /* Tell the ELF linker that we don't want the output file to have a
379 DT_NEEDED entry for this file, unless it is used to resolve
380 references in a regular object. */
381 link_class = DYN_DT_NEEDED;
382
383 /* Tell the ELF linker that we don't want the output file to have a
384 DT_NEEDED entry for this file at all if the entry is from a file
385 with DYN_NO_ADD_NEEDED. */
386 if (needed->by != NULL
387 && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
388 link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
389
390 bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
391
e310298c
AM
392 *link_info.input_bfds_tail = abfd;
393 link_info.input_bfds_tail = &abfd->link.next;
394
d871d478
AM
395 /* Add this file into the symbol table. */
396 if (! bfd_link_add_symbols (abfd, &link_info))
397 einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd);
398
f38a2680 399 return true;
d871d478
AM
400}
401
402/* Search for a needed file in a path. */
403
f38a2680 404static bool
d871d478
AM
405ldelf_search_needed (const char *path, struct dt_needed *n, int force,
406 int is_linux, int elfsize)
407{
408 const char *s;
409 const char *name = n->name;
410 size_t len;
411 struct dt_needed needed;
412
413 if (name[0] == '/')
414 return ldelf_try_needed (n, force, is_linux);
415
416 if (path == NULL || *path == '\0')
f38a2680 417 return false;
d871d478
AM
418
419 needed.by = n->by;
420 needed.name = n->name;
421
422 len = strlen (name);
423 while (1)
424 {
425 unsigned offset = 0;
426 char * var;
427 char *filename, *sset;
428
429 s = strchr (path, config.rpath_separator);
430 if (s == NULL)
431 s = path + strlen (path);
432
433#if HAVE_DOS_BASED_FILE_SYSTEM
434 /* Assume a match on the second char is part of drive specifier. */
435 else if (config.rpath_separator == ':'
436 && s == path + 1
437 && ISALPHA (*path))
438 {
439 s = strchr (s + 1, config.rpath_separator);
440 if (s == NULL)
441 s = path + strlen (path);
442 }
443#endif
444 filename = (char *) xmalloc (s - path + len + 2);
445 if (s == path)
446 sset = filename;
447 else
448 {
449 memcpy (filename, path, s - path);
450 filename[s - path] = '/';
451 sset = filename + (s - path) + 1;
452 }
453 strcpy (sset, name);
454
455 /* PR 20535: Support the same pseudo-environment variables that
456 are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
457 Since there can be more than one occurrence of these tokens in
458 the path we loop until no more are found. Since we might not
459 be able to substitute some of the tokens we maintain an offset
460 into the filename for where we should begin our scan. */
461 while ((var = strchr (filename + offset, '$')) != NULL)
462 {
463 /* The ld.so manual page does not say, but I am going to assume that
464 these tokens are terminated by a directory separator character
465 (/) or the end of the string. There is also an implication that
466 $ORIGIN should only be used at the start of a path, but that is
467 not enforced here.
468
469 The ld.so manual page also states that it allows ${ORIGIN},
470 ${LIB} and ${PLATFORM}, so these are supported as well.
471
472 FIXME: The code could be a lot cleverer about allocating space
473 for the processed string. */
474 char * end = strchr (var, '/');
475 const char *replacement = NULL;
476 char * v = var + 1;
477 char * freeme = NULL;
478 unsigned flen = strlen (filename);
479
480 if (end != NULL)
481 /* Temporarily terminate the filename at the end of the token. */
482 * end = 0;
483
484 if (*v == '{')
485 ++ v;
486 switch (*v++)
487 {
488 case 'O':
489 if (strcmp (v, "RIGIN") == 0 || strcmp (v, "RIGIN}") == 0)
490 {
491 /* ORIGIN - replace with the full path to the directory
492 containing the program or shared object. */
493 if (needed.by == NULL)
494 {
495 if (link_info.output_bfd == NULL)
496 {
497 break;
498 }
499 else
500 replacement = bfd_get_filename (link_info.output_bfd);
501 }
502 else
503 replacement = bfd_get_filename (needed.by);
504
505 if (replacement)
506 {
507 char * slash;
508
509 if (replacement[0] == '/')
510 freeme = xstrdup (replacement);
511 else
512 {
513 char * current_dir = getpwd ();
514
515 freeme = xmalloc (strlen (replacement)
516 + strlen (current_dir) + 2);
517 sprintf (freeme, "%s/%s", current_dir, replacement);
518 }
519
520 replacement = freeme;
521 if ((slash = strrchr (replacement, '/')) != NULL)
522 * slash = 0;
523 }
524 }
525 break;
526
527 case 'L':
528 if (strcmp (v, "IB") == 0 || strcmp (v, "IB}") == 0)
529 {
530 /* LIB - replace with "lib" in 32-bit environments
531 and "lib64" in 64-bit environments. */
532
533 switch (elfsize)
534 {
535 case 32: replacement = "lib"; break;
536 case 64: replacement = "lib64"; break;
537 default:
538 abort ();
539 }
540 }
541 break;
542
543 case 'P':
544 /* Supporting $PLATFORM in a cross-hosted environment is not
545 possible. Supporting it in a native environment involves
546 loading the <sys/auxv.h> header file which loads the
547 system <elf.h> header file, which conflicts with the
548 "include/elf/mips.h" header file. */
549 /* Fall through. */
550 default:
551 break;
552 }
553
554 if (replacement)
555 {
556 char * filename2 = xmalloc (flen + strlen (replacement));
557
558 if (end)
559 {
560 sprintf (filename2, "%.*s%s/%s",
561 (int)(var - filename), filename,
562 replacement, end + 1);
563 offset = (var - filename) + 1 + strlen (replacement);
564 }
565 else
566 {
567 sprintf (filename2, "%.*s%s",
568 (int)(var - filename), filename,
569 replacement);
570 offset = var - filename + strlen (replacement);
571 }
572
573 free (filename);
574 filename = filename2;
575 /* There is no need to restore the path separator (when
576 end != NULL) as we have replaced the entire string. */
577 }
578 else
579 {
580 if (verbose)
581 /* We only issue an "unrecognised" message in verbose mode
582 as the $<foo> token might be a legitimate component of
583 a path name in the target's file system. */
584 info_msg (_("unrecognised or unsupported token "
585 "'%s' in search path\n"), var);
586 if (end)
587 /* Restore the path separator. */
588 * end = '/';
589
590 /* PR 20784: Make sure that we resume the scan *after*
591 the token that we could not replace. */
592 offset = (var + 1) - filename;
593 }
594
595 free (freeme);
596 }
597
598 needed.name = filename;
599
600 if (ldelf_try_needed (&needed, force, is_linux))
f38a2680 601 return true;
d871d478
AM
602
603 free (filename);
604
605 if (*s == '\0')
606 break;
607 path = s + 1;
608 }
609
f38a2680 610 return false;
d871d478
AM
611}
612
613/* Prefix the sysroot to absolute paths in PATH, a string containing
614 paths separated by config.rpath_separator. If running on a DOS
615 file system, paths containing a drive spec won't have the sysroot
616 prefix added, unless the sysroot also specifies the same drive. */
617
618static const char *
619ldelf_add_sysroot (const char *path)
620{
621 size_t len, extra;
622 const char *p;
623 char *ret, *q;
624 int dos_drive_sysroot = HAS_DRIVE_SPEC (ld_sysroot);
625
626 len = strlen (ld_sysroot);
627 for (extra = 0, p = path; ; )
628 {
629 int dos_drive = HAS_DRIVE_SPEC (p);
630
631 if (dos_drive)
632 p += 2;
633 if (IS_DIR_SEPARATOR (*p)
634 && (!dos_drive
635 || (dos_drive_sysroot
636 && ld_sysroot[0] == p[-2])))
637 {
638 if (dos_drive && dos_drive_sysroot)
639 extra += len - 2;
640 else
641 extra += len;
642 }
643 p = strchr (p, config.rpath_separator);
644 if (!p)
645 break;
646 ++p;
647 }
648
649 ret = xmalloc (strlen (path) + extra + 1);
650
651 for (q = ret, p = path; ; )
652 {
653 const char *end;
654 int dos_drive = HAS_DRIVE_SPEC (p);
655
656 if (dos_drive)
657 {
658 *q++ = *p++;
659 *q++ = *p++;
660 }
661 if (IS_DIR_SEPARATOR (*p)
662 && (!dos_drive
663 || (dos_drive_sysroot
664 && ld_sysroot[0] == p[-2])))
665 {
666 if (dos_drive && dos_drive_sysroot)
667 {
668 strcpy (q, ld_sysroot + 2);
669 q += len - 2;
670 }
671 else
672 {
673 strcpy (q, ld_sysroot);
674 q += len;
675 }
676 }
677 end = strchr (p, config.rpath_separator);
678 if (end)
679 {
680 size_t n = end - p + 1;
681 strncpy (q, p, n);
682 q += n;
683 p += n;
684 }
685 else
686 {
687 strcpy (q, p);
688 break;
689 }
690 }
691
692 return ret;
693}
694
695/* Read the system search path the FreeBSD way rather than the Linux way. */
696#ifdef HAVE_ELF_HINTS_H
697#include <elf-hints.h>
698#else
699#include "elf-hints-local.h"
700#endif
701
f38a2680 702static bool
d871d478
AM
703ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
704 int elfsize)
705{
f38a2680 706 static bool initialized;
d871d478
AM
707 static const char *ld_elf_hints;
708 struct dt_needed needed;
709
710 if (!initialized)
711 {
712 FILE *f;
713 char *tmppath;
714
715 tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
716 f = fopen (tmppath, FOPEN_RB);
717 free (tmppath);
718 if (f != NULL)
719 {
720 struct elfhints_hdr hdr;
721
722 if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
723 && hdr.magic == ELFHINTS_MAGIC
724 && hdr.version == 1)
725 {
726 if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
727 {
728 char *b;
729
730 b = xmalloc (hdr.dirlistlen + 1);
731 if (fread (b, 1, hdr.dirlistlen + 1, f) ==
732 hdr.dirlistlen + 1)
733 ld_elf_hints = ldelf_add_sysroot (b);
734
735 free (b);
736 }
737 }
738 fclose (f);
739 }
740
f38a2680 741 initialized = true;
d871d478
AM
742 }
743
744 if (ld_elf_hints == NULL)
f38a2680 745 return false;
d871d478
AM
746
747 needed.by = l->by;
748 needed.name = l->name;
f38a2680 749 return ldelf_search_needed (ld_elf_hints, &needed, force, false, elfsize);
d871d478
AM
750}
751
752/* For a native linker, check the file /etc/ld.so.conf for directories
753 in which we may find shared libraries. /etc/ld.so.conf is really
754 only meaningful on Linux. */
755
756struct ldelf_ld_so_conf
757{
758 char *path;
759 size_t len, alloc;
760};
761
f38a2680 762static bool
d871d478
AM
763ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *, const char *);
764
765static void
766ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf *info,
767 const char *filename,
768 const char *pattern)
769{
770 char *newp = NULL;
771#ifdef HAVE_GLOB
772 glob_t gl;
773#endif
774
775 if (pattern[0] != '/')
776 {
777 char *p = strrchr (filename, '/');
778 size_t patlen = strlen (pattern) + 1;
779
780 newp = xmalloc (p - filename + 1 + patlen);
781 memcpy (newp, filename, p - filename + 1);
782 memcpy (newp + (p - filename + 1), pattern, patlen);
783 pattern = newp;
784 }
785
786#ifdef HAVE_GLOB
787 if (glob (pattern, 0, NULL, &gl) == 0)
788 {
789 size_t i;
790
791 for (i = 0; i < gl.gl_pathc; ++i)
792 ldelf_parse_ld_so_conf (info, gl.gl_pathv[i]);
793 globfree (&gl);
794 }
795#else
796 /* If we do not have glob, treat the pattern as a literal filename. */
797 ldelf_parse_ld_so_conf (info, pattern);
798#endif
799
5e2ab612 800 free (newp);
d871d478
AM
801}
802
f38a2680 803static bool
d871d478
AM
804ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *info, const char *filename)
805{
806 FILE *f = fopen (filename, FOPEN_RT);
807 char *line;
808 size_t linelen;
809
810 if (f == NULL)
f38a2680 811 return false;
d871d478
AM
812
813 linelen = 256;
814 line = xmalloc (linelen);
815 do
816 {
817 char *p = line, *q;
818
819 /* Normally this would use getline(3), but we need to be portable. */
820 while ((q = fgets (p, linelen - (p - line), f)) != NULL
821 && strlen (q) == linelen - (p - line) - 1
822 && line[linelen - 2] != '\n')
823 {
824 line = xrealloc (line, 2 * linelen);
825 p = line + linelen - 1;
826 linelen += linelen;
827 }
828
829 if (q == NULL && p == line)
830 break;
831
832 p = strchr (line, '\n');
833 if (p)
834 *p = '\0';
835
836 /* Because the file format does not know any form of quoting we
837 can search forward for the next '#' character and if found
838 make it terminating the line. */
839 p = strchr (line, '#');
840 if (p)
841 *p = '\0';
842
843 /* Remove leading whitespace. NUL is no whitespace character. */
844 p = line;
845 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
846 ++p;
847
848 /* If the line is blank it is ignored. */
849 if (p[0] == '\0')
850 continue;
851
08dedd66 852 if (startswith (p, "include") && (p[7] == ' ' || p[7] == '\t'))
d871d478
AM
853 {
854 char *dir, c;
855 p += 8;
856 do
857 {
858 while (*p == ' ' || *p == '\t')
859 ++p;
860
861 if (*p == '\0')
862 break;
863
864 dir = p;
865
866 while (*p != ' ' && *p != '\t' && *p)
867 ++p;
868
869 c = *p;
870 *p++ = '\0';
871 if (dir[0] != '\0')
872 ldelf_parse_ld_so_conf_include (info, filename, dir);
873 }
874 while (c != '\0');
875 }
876 else
877 {
878 char *dir = p;
879 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
880 && *p != '\r' && *p != '\v')
881 ++p;
882
883 while (p != dir && p[-1] == '/')
884 --p;
885 if (info->path == NULL)
886 {
887 info->alloc = p - dir + 1 + 256;
888 info->path = xmalloc (info->alloc);
889 info->len = 0;
890 }
891 else
892 {
893 if (info->len + 1 + (p - dir) >= info->alloc)
894 {
895 info->alloc += p - dir + 256;
896 info->path = xrealloc (info->path, info->alloc);
897 }
898 info->path[info->len++] = config.rpath_separator;
899 }
900 memcpy (info->path + info->len, dir, p - dir);
901 info->len += p - dir;
902 info->path[info->len] = '\0';
903 }
904 }
905 while (! feof (f));
906 free (line);
907 fclose (f);
f38a2680 908 return true;
d871d478
AM
909}
910
f38a2680 911static bool
d871d478 912ldelf_check_ld_so_conf (const struct bfd_link_needed_list *l, int force,
ef8f08ca 913 int elfsize, const char *prefix)
d871d478 914{
f38a2680 915 static bool initialized;
d871d478
AM
916 static const char *ld_so_conf;
917 struct dt_needed needed;
918
919 if (! initialized)
920 {
921 char *tmppath;
922 struct ldelf_ld_so_conf info;
923
924 info.path = NULL;
925 info.len = info.alloc = 0;
ef8f08ca 926 tmppath = concat (ld_sysroot, prefix, "/etc/ld.so.conf",
d871d478
AM
927 (const char *) NULL);
928 if (!ldelf_parse_ld_so_conf (&info, tmppath))
929 {
930 free (tmppath);
931 tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
932 (const char *) NULL);
933 ldelf_parse_ld_so_conf (&info, tmppath);
934 }
935 free (tmppath);
936
937 if (info.path)
938 {
939 ld_so_conf = ldelf_add_sysroot (info.path);
940 free (info.path);
941 }
f38a2680 942 initialized = true;
d871d478
AM
943 }
944
945 if (ld_so_conf == NULL)
f38a2680 946 return false;
d871d478
AM
947
948
949 needed.by = l->by;
950 needed.name = l->name;
f38a2680 951 return ldelf_search_needed (ld_so_conf, &needed, force, true, elfsize);
d871d478
AM
952}
953
954/* See if an input file matches a DT_NEEDED entry by name. */
955
956static void
957ldelf_check_needed (lang_input_statement_type *s)
958{
959 const char *soname;
960
961 /* Stop looking if we've found a loaded lib. */
962 if (global_found != NULL
963 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
964 & DYN_AS_NEEDED) == 0)
965 return;
966
967 if (s->filename == NULL || s->the_bfd == NULL)
968 return;
969
970 /* Don't look for a second non-loaded as-needed lib. */
971 if (global_found != NULL
972 && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
973 return;
974
975 if (filename_cmp (s->filename, global_needed->name) == 0)
976 {
977 global_found = s;
978 return;
979 }
980
981 if (s->flags.search_dirs)
982 {
983 const char *f = strrchr (s->filename, '/');
984 if (f != NULL
985 && filename_cmp (f + 1, global_needed->name) == 0)
986 {
987 global_found = s;
988 return;
989 }
990 }
991
992 soname = bfd_elf_get_dt_soname (s->the_bfd);
993 if (soname != NULL
994 && filename_cmp (soname, global_needed->name) == 0)
995 {
996 global_found = s;
997 return;
998 }
999}
1000
1001/* This is called after all the input files have been opened. */
1002
1003void
1004ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
ef8f08ca 1005 int elfsize, const char *prefix)
d871d478
AM
1006{
1007 struct bfd_link_needed_list *needed, *l;
1008 struct elf_link_hash_table *htab;
1009 asection *s;
1010 bfd *abfd;
e310298c 1011 bfd **save_input_bfd_tail;
d871d478
AM
1012
1013 after_open_default ();
1014
1015 htab = elf_hash_table (&link_info);
2cc15b10 1016 if (!is_elf_hash_table (&htab->root))
d871d478
AM
1017 return;
1018
1019 if (command_line.out_implib_filename)
1020 {
1021 unlink_if_ordinary (command_line.out_implib_filename);
1022 link_info.out_implib_bfd
1023 = bfd_openw (command_line.out_implib_filename,
1024 bfd_get_target (link_info.output_bfd));
1025
1026 if (link_info.out_implib_bfd == NULL)
1027 {
1028 einfo (_("%F%P: %s: can't open for writing: %E\n"),
1029 command_line.out_implib_filename);
1030 }
1031 }
1032
1033 if (ldelf_emit_note_gnu_build_id != NULL)
1034 {
1035 /* Find an ELF input. */
1036 for (abfd = link_info.input_bfds;
1037 abfd != (bfd *) NULL; abfd = abfd->link.next)
1038 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1039 && bfd_count_sections (abfd) != 0
00f93c44 1040 && !bfd_input_just_syms (abfd))
d871d478
AM
1041 break;
1042
1043 /* PR 10555: If there are no ELF input files do not try to
1044 create a .note.gnu-build-id section. */
1045 if (abfd == NULL
1046 || !ldelf_setup_build_id (abfd))
1047 {
1048 free ((char *) ldelf_emit_note_gnu_build_id);
1049 ldelf_emit_note_gnu_build_id = NULL;
1050 }
1051 }
1052
1053 get_elf_backend_data (link_info.output_bfd)->setup_gnu_properties (&link_info);
1054
a87e1817
NC
1055 /* Do not allow executable files to be used as inputs to the link. */
1056 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1057 {
1f1d0f88
L
1058 /* Discard input .note.gnu.build-id sections. */
1059 s = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
1060 while (s != NULL)
1061 {
1062 if (s != elf_tdata (link_info.output_bfd)->o->build_id.sec)
1063 s->flags |= SEC_EXCLUDE;
1064 s = bfd_get_next_section_by_name (NULL, s);
1065 }
1066
0403e9cc
L
1067 if (abfd->xvec->flavour == bfd_target_elf_flavour
1068 && !bfd_input_just_syms (abfd)
97c79e21 1069 && elf_tdata (abfd) != NULL
a87e1817 1070 /* FIXME: Maybe check for other non-supportable types as well ? */
f64b9b13
AM
1071 && (elf_tdata (abfd)->elf_header->e_type == ET_EXEC
1072 || (elf_tdata (abfd)->elf_header->e_type == ET_DYN
1073 && elf_tdata (abfd)->is_pie)))
a87e1817
NC
1074 einfo (_("%F%P: cannot use executable file '%pB' as input to a link\n"),
1075 abfd);
1076 }
1077
d871d478
AM
1078 if (bfd_link_relocatable (&link_info))
1079 {
1080 if (link_info.execstack == !link_info.noexecstack)
1081 {
1082 /* PR ld/16744: If "-z [no]execstack" has been specified on the
1083 command line and we are perfoming a relocatable link then no
1084 PT_GNU_STACK segment will be created and so the
1085 linkinfo.[no]execstack values set in _handle_option() will have no
1086 effect. Instead we create a .note.GNU-stack section in much the
1087 same way as the assembler does with its --[no]execstack option. */
1088 flagword flags = SEC_READONLY | (link_info.execstack ? SEC_CODE : 0);
1089 (void) bfd_make_section_with_flags (link_info.input_bfds,
1090 ".note.GNU-stack", flags);
1091 }
1092 return;
1093 }
f37b21b4 1094
d871d478
AM
1095 if (!link_info.traditional_format)
1096 {
1097 bfd *elfbfd = NULL;
f38a2680 1098 bool warn_eh_frame = false;
d871d478
AM
1099 int seen_type = 0;
1100
1101 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1102 {
1103 int type = 0;
1104
00f93c44 1105 if (bfd_input_just_syms (abfd))
d871d478
AM
1106 continue;
1107
1108 for (s = abfd->sections; s && type < COMPACT_EH_HDR; s = s->next)
1109 {
fd361982 1110 const char *name = bfd_section_name (s);
d871d478
AM
1111
1112 if (bfd_is_abs_section (s->output_section))
1113 continue;
08dedd66 1114 if (startswith (name, ".eh_frame_entry"))
d871d478
AM
1115 type = COMPACT_EH_HDR;
1116 else if (strcmp (name, ".eh_frame") == 0 && s->size > 8)
1117 type = DWARF2_EH_HDR;
1118 }
1119
1120 if (type != 0)
1121 {
1122 if (seen_type == 0)
1123 {
1124 seen_type = type;
1125 }
1126 else if (seen_type != type)
1127 {
1128 einfo (_("%F%P: compact frame descriptions incompatible with"
1129 " DWARF2 .eh_frame from %pB\n"),
1130 type == DWARF2_EH_HDR ? abfd : elfbfd);
1131 break;
1132 }
1133
1134 if (!elfbfd
1135 && (type == COMPACT_EH_HDR
1136 || link_info.eh_frame_hdr_type != 0))
1137 {
1138 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1139 elfbfd = abfd;
1140
f38a2680 1141 warn_eh_frame = true;
d871d478
AM
1142 }
1143 }
1144
1145 if (seen_type == COMPACT_EH_HDR)
1146 link_info.eh_frame_hdr_type = COMPACT_EH_HDR;
1147 }
1148 if (elfbfd)
1149 {
1150 const struct elf_backend_data *bed;
1151
1152 bed = get_elf_backend_data (elfbfd);
1153 s = bfd_make_section_with_flags (elfbfd, ".eh_frame_hdr",
1154 bed->dynamic_sec_flags
1155 | SEC_READONLY);
1156 if (s != NULL
fd361982 1157 && bfd_set_section_alignment (s, 2))
d871d478
AM
1158 {
1159 htab->eh_info.hdr_sec = s;
f38a2680 1160 warn_eh_frame = false;
d871d478
AM
1161 }
1162 }
1163 if (warn_eh_frame)
1164 einfo (_("%P: warning: cannot create .eh_frame_hdr section,"
1165 " --eh-frame-hdr ignored\n"));
1166 }
1167
1168 /* Get the list of files which appear in DT_NEEDED entries in
1169 dynamic objects included in the link (often there will be none).
1170 For each such file, we want to track down the corresponding
1171 library, and include the symbol table in the link. This is what
1172 the runtime dynamic linker will do. Tracking the files down here
1173 permits one dynamic object to include another without requiring
1174 special action by the person doing the link. Note that the
1175 needed list can actually grow while we are stepping through this
1176 loop. */
e310298c 1177 save_input_bfd_tail = link_info.input_bfds_tail;
d871d478
AM
1178 needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info);
1179 for (l = needed; l != NULL; l = l->next)
1180 {
1181 struct bfd_link_needed_list *ll;
1182 struct dt_needed n, nn;
1183 int force;
1184
1185 /* If the lib that needs this one was --as-needed and wasn't
1186 found to be needed, then this lib isn't needed either. */
1187 if (l->by != NULL
1188 && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0)
1189 continue;
1190
1191 /* Skip the lib if --no-copy-dt-needed-entries and
1192 --allow-shlib-undefined is in effect. */
1193 if (l->by != NULL
1194 && link_info.unresolved_syms_in_shared_libs == RM_IGNORE
1195 && (bfd_elf_get_dyn_lib_class (l->by) & DYN_NO_ADD_NEEDED) != 0)
1196 continue;
1197
1198 /* If we've already seen this file, skip it. */
1199 for (ll = needed; ll != l; ll = ll->next)
1200 if ((ll->by == NULL
1201 || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
1202 && strcmp (ll->name, l->name) == 0)
1203 break;
1204 if (ll != l)
1205 continue;
1206
1207 /* See if this file was included in the link explicitly. */
1208 global_needed = l;
1209 global_found = NULL;
1210 lang_for_each_input_file (ldelf_check_needed);
1211 if (global_found != NULL
1212 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1213 & DYN_AS_NEEDED) == 0)
1214 continue;
1215
1216 n.by = l->by;
1217 n.name = l->name;
1218 nn.by = l->by;
1219 if (verbose)
1220 info_msg (_("%s needed by %pB\n"), l->name, l->by);
1221
1222 /* As-needed libs specified on the command line (or linker script)
1223 take priority over libs found in search dirs. */
1224 if (global_found != NULL)
1225 {
1226 nn.name = global_found->filename;
f38a2680 1227 if (ldelf_try_needed (&nn, true, is_linux))
d871d478
AM
1228 continue;
1229 }
1230
1231 /* We need to find this file and include the symbol table. We
1232 want to search for the file in the same way that the dynamic
1233 linker will search. That means that we want to use
1234 rpath_link, rpath, then the environment variable
1235 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1236 entries (native only), then the linker script LIB_SEARCH_DIRS.
1237 We do not search using the -L arguments.
1238
1239 We search twice. The first time, we skip objects which may
1240 introduce version mismatches. The second time, we force
1241 their use. See ldelf_vercheck comment. */
1242 for (force = 0; force < 2; force++)
1243 {
1244 size_t len;
1245 search_dirs_type *search;
1246 const char *path;
1247 struct bfd_link_needed_list *rp;
1248 int found;
1249
1250 if (ldelf_search_needed (command_line.rpath_link, &n, force,
1251 is_linux, elfsize))
1252 break;
1253
1254 if (use_libpath)
1255 {
1256 path = command_line.rpath;
1257 if (path)
1258 {
1259 path = ldelf_add_sysroot (path);
1260 found = ldelf_search_needed (path, &n, force,
1261 is_linux, elfsize);
1262 free ((char *) path);
1263 if (found)
1264 break;
1265 }
1266 }
1267 if (native)
1268 {
1269 if (command_line.rpath_link == NULL
1270 && command_line.rpath == NULL)
1271 {
1272 path = (const char *) getenv ("LD_RUN_PATH");
1273 if (path
1274 && ldelf_search_needed (path, &n, force,
1275 is_linux, elfsize))
1276 break;
1277 }
1278 path = (const char *) getenv ("LD_LIBRARY_PATH");
1279 if (path
1280 && ldelf_search_needed (path, &n, force,
1281 is_linux, elfsize))
1282 break;
1283 }
1284 if (use_libpath)
1285 {
1286 found = 0;
1287 rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
1288 for (; !found && rp != NULL; rp = rp->next)
1289 {
1290 path = ldelf_add_sysroot (rp->name);
1291 found = (rp->by == l->by
1292 && ldelf_search_needed (path, &n, force,
1293 is_linux, elfsize));
1294 free ((char *) path);
1295 }
1296 if (found)
1297 break;
1298
1299 if (is_freebsd
1300 && ldelf_check_ld_elf_hints (l, force, elfsize))
1301 break;
1302
1303 if (is_linux
ef8f08ca 1304 && ldelf_check_ld_so_conf (l, force, elfsize, prefix))
d871d478
AM
1305 break;
1306 }
1307
1308 len = strlen (l->name);
1309 for (search = search_head; search != NULL; search = search->next)
1310 {
1311 char *filename;
1312
1313 if (search->cmdline)
1314 continue;
1315 filename = (char *) xmalloc (strlen (search->name) + len + 2);
1316 sprintf (filename, "%s/%s", search->name, l->name);
1317 nn.name = filename;
1318 if (ldelf_try_needed (&nn, force, is_linux))
1319 break;
1320 free (filename);
1321 }
1322 if (search != NULL)
1323 break;
1324 }
1325
1326 if (force < 2)
1327 continue;
1328
1329 einfo (_("%P: warning: %s, needed by %pB, not found "
1330 "(try using -rpath or -rpath-link)\n"),
1331 l->name, l->by);
1332 }
1333
e310298c
AM
1334 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1335 if (bfd_get_format (abfd) == bfd_object
1336 && ((abfd->flags) & DYNAMIC) != 0
1337 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1338 && (elf_dyn_lib_class (abfd) & (DYN_AS_NEEDED | DYN_NO_NEEDED)) == 0
1339 && elf_dt_name (abfd) != NULL)
1340 {
1341 if (bfd_elf_add_dt_needed_tag (abfd, &link_info) < 0)
1342 einfo (_("%F%P: failed to add DT_NEEDED dynamic tag\n"));
1343 }
1344
1345 link_info.input_bfds_tail = save_input_bfd_tail;
1346 *save_input_bfd_tail = NULL;
1347
d871d478
AM
1348 if (link_info.eh_frame_hdr_type == COMPACT_EH_HDR)
1349 if (!bfd_elf_parse_eh_frame_entries (NULL, &link_info))
1350 einfo (_("%F%P: failed to parse EH frame entries\n"));
1351}
1352
1353static bfd_size_type
1354id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED)
1355{
1356 const char *style = ldelf_emit_note_gnu_build_id;
1357 bfd_size_type size;
1358 bfd_size_type build_id_size;
1359
1360 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
1361 size = (size + 3) & -(bfd_size_type) 4;
1362
1363 build_id_size = compute_build_id_size (style);
1364 if (build_id_size)
1365 size += build_id_size;
1366 else
1367 size = 0;
1368
1369 return size;
1370}
1371
f38a2680 1372static bool
d871d478
AM
1373write_build_id (bfd *abfd)
1374{
1375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1376 struct elf_obj_tdata *t = elf_tdata (abfd);
1377 const char *style;
1378 asection *asec;
1379 Elf_Internal_Shdr *i_shdr;
1380 unsigned char *contents, *id_bits;
1381 bfd_size_type size;
1382 file_ptr position;
1383 Elf_External_Note *e_note;
1384
1385 style = t->o->build_id.style;
1386 asec = t->o->build_id.sec;
1387 if (bfd_is_abs_section (asec->output_section))
1388 {
1389 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
1390 " --build-id ignored\n"));
f38a2680 1391 return true;
d871d478
AM
1392 }
1393 i_shdr = &elf_section_data (asec->output_section)->this_hdr;
1394
1395 if (i_shdr->contents == NULL)
1396 {
1397 if (asec->contents == NULL)
1398 asec->contents = (unsigned char *) xmalloc (asec->size);
1399 contents = asec->contents;
1400 }
1401 else
1402 contents = i_shdr->contents + asec->output_offset;
1403
1404 e_note = (Elf_External_Note *) contents;
1405 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
1406 size = (size + 3) & -(bfd_size_type) 4;
1407 id_bits = contents + size;
1408 size = asec->size - size;
1409
1f1d0f88
L
1410 /* Clear the build ID field. */
1411 memset (id_bits, 0, size);
1412
d871d478
AM
1413 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
1414 bfd_h_put_32 (abfd, size, &e_note->descsz);
1415 bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
1416 memcpy (e_note->name, "GNU", sizeof "GNU");
1417
1418 generate_build_id (abfd, style, bed->s->checksum_contents, id_bits, size);
1419
1420 position = i_shdr->sh_offset + asec->output_offset;
1421 size = asec->size;
1422 return (bfd_seek (abfd, position, SEEK_SET) == 0
1423 && bfd_bwrite (contents, size, abfd) == size);
1424}
1425
1426/* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
1427
f38a2680 1428bool
d871d478
AM
1429ldelf_setup_build_id (bfd *ibfd)
1430{
1431 asection *s;
1432 bfd_size_type size;
1433 flagword flags;
1434
1435 size = id_note_section_size (ibfd);
1436 if (size == 0)
1437 {
1438 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
f38a2680 1439 return false;
d871d478
AM
1440 }
1441
1442 flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1443 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1f1d0f88
L
1444 s = bfd_make_section_anyway_with_flags (ibfd, ".note.gnu.build-id",
1445 flags);
fd361982 1446 if (s != NULL && bfd_set_section_alignment (s, 2))
d871d478
AM
1447 {
1448 struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
1449 t->o->build_id.after_write_object_contents = &write_build_id;
1450 t->o->build_id.style = ldelf_emit_note_gnu_build_id;
1451 t->o->build_id.sec = s;
1452 elf_section_type (s) = SHT_NOTE;
1453 s->size = size;
f38a2680 1454 return true;
d871d478
AM
1455 }
1456
1457 einfo (_("%P: warning: cannot create .note.gnu.build-id section,"
1458 " --build-id ignored\n"));
f38a2680 1459 return false;
d871d478
AM
1460}
1461
1462/* Look through an expression for an assignment statement. */
1463
1464static void
1465ldelf_find_exp_assignment (etree_type *exp)
1466{
f38a2680 1467 bool provide = false;
d871d478
AM
1468
1469 switch (exp->type.node_class)
1470 {
1471 case etree_provide:
1472 case etree_provided:
f38a2680 1473 provide = true;
d871d478
AM
1474 /* Fallthru */
1475 case etree_assign:
1476 /* We call record_link_assignment even if the symbol is defined.
1477 This is because if it is defined by a dynamic object, we
1478 actually want to use the value defined by the linker script,
1479 not the value from the dynamic object (because we are setting
1480 symbols like etext). If the symbol is defined by a regular
1481 object, then, as it happens, calling record_link_assignment
1482 will do no harm. */
1483 if (strcmp (exp->assign.dst, ".") != 0)
1484 {
1485 if (!bfd_elf_record_link_assignment (link_info.output_bfd,
1486 &link_info,
1487 exp->assign.dst, provide,
1488 exp->assign.hidden))
1489 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1490 exp->assign.dst);
1491 }
1492 ldelf_find_exp_assignment (exp->assign.src);
1493 break;
1494
1495 case etree_binary:
1496 ldelf_find_exp_assignment (exp->binary.lhs);
1497 ldelf_find_exp_assignment (exp->binary.rhs);
1498 break;
1499
1500 case etree_trinary:
1501 ldelf_find_exp_assignment (exp->trinary.cond);
1502 ldelf_find_exp_assignment (exp->trinary.lhs);
1503 ldelf_find_exp_assignment (exp->trinary.rhs);
1504 break;
1505
1506 case etree_unary:
1507 ldelf_find_exp_assignment (exp->unary.child);
1508 break;
1509
1510 default:
1511 break;
1512 }
1513}
1514
1515/* This is called by the before_allocation routine via
1516 lang_for_each_statement. It locates any assignment statements, and
1517 tells the ELF backend about them, in case they are assignments to
1518 symbols which are referred to by dynamic objects. */
1519
1520static void
1521ldelf_find_statement_assignment (lang_statement_union_type *s)
1522{
1523 if (s->header.type == lang_assignment_statement_enum)
1524 ldelf_find_exp_assignment (s->assignment_statement.exp);
1525}
1526
1527/* Used by before_allocation and handle_option. */
1528
1529void
1530ldelf_append_to_separated_string (char **to, char *op_arg)
1531{
1532 if (*to == NULL)
1533 *to = xstrdup (op_arg);
1534 else
1535 {
1536 size_t to_len = strlen (*to);
1537 size_t op_arg_len = strlen (op_arg);
1538 char *buf;
1539 char *cp = *to;
1540
1541 /* First see whether OPTARG is already in the path. */
1542 do
1543 {
1544 if (strncmp (op_arg, cp, op_arg_len) == 0
1545 && (cp[op_arg_len] == 0
1546 || cp[op_arg_len] == config.rpath_separator))
1547 /* We found it. */
1548 break;
1549
1550 /* Not yet found. */
1551 cp = strchr (cp, config.rpath_separator);
1552 if (cp != NULL)
1553 ++cp;
1554 }
1555 while (cp != NULL);
1556
1557 if (cp == NULL)
1558 {
1559 buf = xmalloc (to_len + op_arg_len + 2);
1560 sprintf (buf, "%s%c%s", *to,
1561 config.rpath_separator, op_arg);
1562 free (*to);
1563 *to = buf;
1564 }
1565 }
1566}
1567
1568/* This is called after the sections have been attached to output
1569 sections, but before any sizes or addresses have been set. */
1570
1571void
1572ldelf_before_allocation (char *audit, char *depaudit,
1573 const char *default_interpreter_name)
1574{
1575 const char *rpath;
1576 asection *sinterp;
1577 bfd *abfd;
1578 struct bfd_link_hash_entry *ehdr_start = NULL;
1579 unsigned char ehdr_start_save_type = 0;
1580 char ehdr_start_save_u[sizeof ehdr_start->u
1581 - sizeof ehdr_start->u.def.next] = "";
1582
1583 if (is_elf_hash_table (link_info.hash))
1584 {
1585 _bfd_elf_tls_setup (link_info.output_bfd, &link_info);
1586
1587 /* Make __ehdr_start hidden if it has been referenced, to
1588 prevent the symbol from being dynamic. */
1589 if (!bfd_link_relocatable (&link_info))
1590 {
1591 struct elf_link_hash_table *htab = elf_hash_table (&link_info);
1592 struct elf_link_hash_entry *h
f38a2680 1593 = elf_link_hash_lookup (htab, "__ehdr_start", false, false, true);
d871d478
AM
1594
1595 /* Only adjust the export class if the symbol was referenced
1596 and not defined, otherwise leave it alone. */
1597 if (h != NULL
1598 && (h->root.type == bfd_link_hash_new
1599 || h->root.type == bfd_link_hash_undefined
1600 || h->root.type == bfd_link_hash_undefweak
1601 || h->root.type == bfd_link_hash_common))
1602 {
1603 const struct elf_backend_data *bed;
1604 bed = get_elf_backend_data (link_info.output_bfd);
f38a2680 1605 (*bed->elf_backend_hide_symbol) (&link_info, h, true);
d871d478
AM
1606 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
1607 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
1608 /* Don't leave the symbol undefined. Undefined hidden
1609 symbols typically won't have dynamic relocations, but
1610 we most likely will need dynamic relocations for
1611 __ehdr_start if we are building a PIE or shared
1612 library. */
1613 ehdr_start = &h->root;
1614 ehdr_start_save_type = ehdr_start->type;
1615 memcpy (ehdr_start_save_u,
1616 (char *) &ehdr_start->u + sizeof ehdr_start->u.def.next,
1617 sizeof ehdr_start_save_u);
1618 ehdr_start->type = bfd_link_hash_defined;
cbd5b99c
L
1619 /* It will be converted to section-relative later. */
1620 ehdr_start->rel_from_abs = 1;
d871d478
AM
1621 ehdr_start->u.def.section = bfd_abs_section_ptr;
1622 ehdr_start->u.def.value = 0;
1623 }
1624 }
1625
1626 /* If we are going to make any variable assignments, we need to
1627 let the ELF backend know about them in case the variables are
1628 referred to by dynamic objects. */
1629 lang_for_each_statement (ldelf_find_statement_assignment);
1630 }
1631
1632 /* Let the ELF backend work out the sizes of any sections required
1633 by dynamic linking. */
1634 rpath = command_line.rpath;
1635 if (rpath == NULL)
1636 rpath = (const char *) getenv ("LD_RUN_PATH");
1637
1638 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1639 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1640 {
1641 const char *audit_libs = elf_dt_audit (abfd);
1642
1643 /* If the input bfd contains an audit entry, we need to add it as
1644 a dep audit entry. */
1645 if (audit_libs && *audit_libs != '\0')
1646 {
1647 char *cp = xstrdup (audit_libs);
1648 do
1649 {
1650 int more = 0;
1651 char *cp2 = strchr (cp, config.rpath_separator);
1652
1653 if (cp2)
1654 {
1655 *cp2 = '\0';
1656 more = 1;
1657 }
1658
1659 if (cp != NULL && *cp != '\0')
1660 ldelf_append_to_separated_string (&depaudit, cp);
1661
1662 cp = more ? ++cp2 : NULL;
1663 }
1664 while (cp != NULL);
1665 }
1666 }
1667
1668 if (! (bfd_elf_size_dynamic_sections
1669 (link_info.output_bfd, command_line.soname, rpath,
1670 command_line.filter_shlib, audit, depaudit,
1671 (const char * const *) command_line.auxiliary_filters,
1672 &link_info, &sinterp)))
1673 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1674
1675 if (sinterp != NULL)
1676 {
1677 /* Let the user override the dynamic linker we are using. */
1678 if (command_line.interpreter != NULL)
1679 default_interpreter_name = command_line.interpreter;
1680 if (default_interpreter_name != NULL)
1681 {
1682 sinterp->contents = (bfd_byte *) default_interpreter_name;
1683 sinterp->size = strlen ((char *) sinterp->contents) + 1;
1684 }
1685 }
1686
1687 /* Look for any sections named .gnu.warning. As a GNU extensions,
1688 we treat such sections as containing warning messages. We print
1689 out the warning message, and then zero out the section size so
1690 that it does not get copied into the output file. */
1691
1692 {
1693 LANG_FOR_EACH_INPUT_STATEMENT (is)
1694 {
1695 asection *s;
1696 bfd_size_type sz;
1697 char *msg;
1698
1699 if (is->flags.just_syms)
1700 continue;
1701
1702 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1703 if (s == NULL)
1704 continue;
1705
1706 sz = s->size;
1707 msg = (char *) xmalloc ((size_t) (sz + 1));
1708 if (! bfd_get_section_contents (is->the_bfd, s, msg,
1709 (file_ptr) 0, sz))
1710 einfo (_("%F%P: %pB: can't read contents of section .gnu.warning: %E\n"),
1711 is->the_bfd);
1712 msg[sz] = '\0';
1713 (*link_info.callbacks->warning) (&link_info, msg,
1714 (const char *) NULL, is->the_bfd,
1715 (asection *) NULL, (bfd_vma) 0);
1716 free (msg);
1717
1718 /* Clobber the section size, so that we don't waste space
1719 copying the warning into the output file. If we've already
1720 sized the output section, adjust its size. The adjustment
1721 is on rawsize because targets that size sections early will
1722 have called lang_reset_memory_regions after sizing. */
1723 if (s->output_section != NULL
1724 && s->output_section->rawsize >= s->size)
1725 s->output_section->rawsize -= s->size;
1726
1727 s->size = 0;
1728
1729 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1730 warning section don't get copied to the output. */
1731 s->flags |= SEC_EXCLUDE | SEC_KEEP;
1732 }
1733 }
1734
1735 before_allocation_default ();
1736
1737 if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info))
1738 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
1739
1740 if (ehdr_start != NULL)
1741 {
1742 /* If we twiddled __ehdr_start to defined earlier, put it back
1743 as it was. */
1744 ehdr_start->type = ehdr_start_save_type;
1745 memcpy ((char *) &ehdr_start->u + sizeof ehdr_start->u.def.next,
1746 ehdr_start_save_u,
1747 sizeof ehdr_start_save_u);
1748 }
1749}
1750/* Try to open a dynamic archive. This is where we know that ELF
1751 dynamic libraries have an extension of .so (or .sl on oddball systems
1752 like hpux). */
1753
f38a2680 1754bool
d871d478
AM
1755ldelf_open_dynamic_archive (const char *arch, search_dirs_type *search,
1756 lang_input_statement_type *entry)
1757{
1758 const char *filename;
1759 char *string;
1760 size_t len;
f38a2680 1761 bool opened = false;
d871d478
AM
1762
1763 if (! entry->flags.maybe_archive)
f38a2680 1764 return false;
d871d478
AM
1765
1766 filename = entry->filename;
1767 len = strlen (search->name) + strlen (filename);
1768 if (entry->flags.full_name_provided)
1769 {
1770 len += sizeof "/";
1771 string = (char *) xmalloc (len);
1772 sprintf (string, "%s/%s", search->name, filename);
1773 }
1774 else
1775 {
1776 size_t xlen = 0;
1777
1778 len += strlen (arch) + sizeof "/lib.so";
1779#ifdef EXTRA_SHLIB_EXTENSION
1780 xlen = (strlen (EXTRA_SHLIB_EXTENSION) > 3
1781 ? strlen (EXTRA_SHLIB_EXTENSION) - 3
1782 : 0);
1783#endif
1784 string = (char *) xmalloc (len + xlen);
1785 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1786#ifdef EXTRA_SHLIB_EXTENSION
1787 /* Try the .so extension first. If that fails build a new filename
1788 using EXTRA_SHLIB_EXTENSION. */
1789 opened = ldfile_try_open_bfd (string, entry);
1790 if (!opened)
1791 strcpy (string + len - 4, EXTRA_SHLIB_EXTENSION);
1792#endif
1793 }
1794
1795 if (!opened && !ldfile_try_open_bfd (string, entry))
1796 {
1797 free (string);
f38a2680 1798 return false;
d871d478
AM
1799 }
1800
1801 entry->filename = string;
1802
1803 /* We have found a dynamic object to include in the link. The ELF
1804 backend linker will create a DT_NEEDED entry in the .dynamic
1805 section naming this file. If this file includes a DT_SONAME
1806 entry, it will be used. Otherwise, the ELF linker will just use
1807 the name of the file. For an archive found by searching, like
1808 this one, the DT_NEEDED entry should consist of just the name of
1809 the file, without the path information used to find it. Note
1810 that we only need to do this if we have a dynamic object; an
1811 archive will never be referenced by a DT_NEEDED entry.
1812
1813 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1814 very pretty. I haven't been able to think of anything that is
1815 pretty, though. */
1816 if (bfd_check_format (entry->the_bfd, bfd_object)
1817 && (entry->the_bfd->flags & DYNAMIC) != 0)
1818 {
1819 ASSERT (entry->flags.maybe_archive && entry->flags.search_dirs);
1820
1821 /* Rather than duplicating the logic above. Just use the
1822 filename we recorded earlier. */
1823
1824 if (!entry->flags.full_name_provided)
1825 filename = lbasename (entry->filename);
1826 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1827 }
1828
f38a2680 1829 return true;
d871d478
AM
1830}
1831
1832/* A variant of lang_output_section_find used by place_orphan. */
1833
1834static lang_output_section_statement_type *
1835output_rel_find (int isdyn, int rela)
1836{
1837 lang_output_section_statement_type *lookup;
1838 lang_output_section_statement_type *last = NULL;
1839 lang_output_section_statement_type *last_alloc = NULL;
1840 lang_output_section_statement_type *last_ro_alloc = NULL;
1841 lang_output_section_statement_type *last_rel = NULL;
1842 lang_output_section_statement_type *last_rel_alloc = NULL;
1843
8ce18f9c 1844 for (lookup = (void *) lang_os_list.head;
d871d478
AM
1845 lookup != NULL;
1846 lookup = lookup->next)
1847 {
1848 if (lookup->constraint >= 0
08dedd66 1849 && startswith (lookup->name, ".rel"))
d871d478
AM
1850 {
1851 int lookrela = lookup->name[4] == 'a';
1852
1853 /* .rel.dyn must come before all other reloc sections, to suit
1854 GNU ld.so. */
1855 if (isdyn)
1856 break;
1857
1858 /* Don't place after .rel.plt as doing so results in wrong
1859 dynamic tags. */
1860 if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
1861 break;
1862
1863 if (rela == lookrela || last_rel == NULL)
1864 last_rel = lookup;
1865 if ((rela == lookrela || last_rel_alloc == NULL)
1866 && lookup->bfd_section != NULL
1867 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1868 last_rel_alloc = lookup;
1869 }
1870
1871 last = lookup;
1872 if (lookup->bfd_section != NULL
1873 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1874 {
1875 last_alloc = lookup;
1876 if ((lookup->bfd_section->flags & SEC_READONLY) != 0)
1877 last_ro_alloc = lookup;
1878 }
1879 }
1880
1881 if (last_rel_alloc)
1882 return last_rel_alloc;
1883
1884 if (last_rel)
1885 return last_rel;
1886
1887 if (last_ro_alloc)
1888 return last_ro_alloc;
1889
1890 if (last_alloc)
1891 return last_alloc;
1892
1893 return last;
1894}
1895
1896/* Return whether IN is suitable to be part of OUT. */
1897
f38a2680 1898static bool
d871d478
AM
1899elf_orphan_compatible (asection *in, asection *out)
1900{
1901 /* Non-zero sh_info implies a section with SHF_INFO_LINK with
1902 unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
1903 section where sh_info specifies a symbol table. (We won't see
1904 SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
1905 can't merge SHT_REL/SHT_RELA using differing symbol tables, and
1906 shouldn't merge sections with differing unknown semantics. */
1907 if (elf_section_data (out)->this_hdr.sh_info
1908 != elf_section_data (in)->this_hdr.sh_info)
f38a2680 1909 return false;
1db6f990
AM
1910 /* We can't merge with a member of an output section group or merge
1911 two sections with differing SHF_EXCLUDE or other processor and OS
1912 specific flags when doing a relocatable link. */
d871d478
AM
1913 if (bfd_link_relocatable (&link_info)
1914 && (elf_next_in_group (out) != NULL
1915 || ((elf_section_flags (out) ^ elf_section_flags (in))
1db6f990 1916 & (SHF_MASKPROC | SHF_MASKOS)) != 0))
f38a2680 1917 return false;
d871d478
AM
1918 return _bfd_elf_match_sections_by_type (link_info.output_bfd, out,
1919 in->owner, in);
1920}
1921
1922/* Place an orphan section. We use this to put random SHF_ALLOC
1923 sections in the right segment. */
1924
1925lang_output_section_statement_type *
1926ldelf_place_orphan (asection *s, const char *secname, int constraint)
1927{
1928 static struct orphan_save hold[] =
1929 {
1930 { ".text",
1931 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1932 0, 0, 0, 0 },
1933 { ".rodata",
1934 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1935 0, 0, 0, 0 },
1936 { ".tdata",
1937 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_THREAD_LOCAL,
1938 0, 0, 0, 0 },
1939 { ".data",
1940 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1941 0, 0, 0, 0 },
1942 { ".bss",
1943 SEC_ALLOC,
1944 0, 0, 0, 0 },
1945 { 0,
1946 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1947 0, 0, 0, 0 },
1948 { ".interp",
1949 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1950 0, 0, 0, 0 },
1951 { ".sdata",
1952 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
1953 0, 0, 0, 0 },
1954 { ".comment",
1955 SEC_HAS_CONTENTS,
1956 0, 0, 0, 0 },
1957 };
1958 enum orphan_save_index
1959 {
1960 orphan_text = 0,
1961 orphan_rodata,
1962 orphan_tdata,
1963 orphan_data,
1964 orphan_bss,
1965 orphan_rel,
1966 orphan_interp,
1967 orphan_sdata,
1968 orphan_nonalloc
1969 };
1970 static int orphan_init_done = 0;
1971 struct orphan_save *place;
1972 lang_output_section_statement_type *after;
1973 lang_output_section_statement_type *os;
1974 lang_output_section_statement_type *match_by_name = NULL;
1975 int isdyn = 0;
1976 int elfinput = s->owner->xvec->flavour == bfd_target_elf_flavour;
1977 int elfoutput = link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour;
1978 unsigned int sh_type = elfinput ? elf_section_type (s) : SHT_NULL;
1979 flagword flags;
1980 asection *nexts;
1981
1982 if (!bfd_link_relocatable (&link_info)
1983 && link_info.combreloc
1984 && (s->flags & SEC_ALLOC))
1985 {
1986 if (elfinput)
1987 switch (sh_type)
1988 {
1989 case SHT_RELA:
1990 secname = ".rela.dyn";
1991 isdyn = 1;
1992 break;
1993 case SHT_REL:
1994 secname = ".rel.dyn";
1995 isdyn = 1;
1996 break;
1997 default:
1998 break;
1999 }
08dedd66 2000 else if (startswith (secname, ".rel"))
d871d478
AM
2001 {
2002 secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
2003 isdyn = 1;
2004 }
2005 }
2006
2007 if (!bfd_link_relocatable (&link_info)
2008 && elfinput
2009 && elfoutput
2010 && (s->flags & SEC_ALLOC) != 0
2011 && (elf_tdata (s->owner)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
2012 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
2013 {
2014 /* Find the output mbind section with the same type, attributes
2015 and sh_info field. */
8ce18f9c 2016 for (os = (void *) lang_os_list.head;
d871d478
AM
2017 os != NULL;
2018 os = os->next)
2019 if (os->bfd_section != NULL
2020 && !bfd_is_abs_section (os->bfd_section)
2021 && (elf_section_flags (os->bfd_section) & SHF_GNU_MBIND) != 0
2022 && ((s->flags & (SEC_ALLOC
2023 | SEC_LOAD
2024 | SEC_HAS_CONTENTS
2025 | SEC_READONLY
2026 | SEC_CODE))
2027 == (os->bfd_section->flags & (SEC_ALLOC
2028 | SEC_LOAD
2029 | SEC_HAS_CONTENTS
2030 | SEC_READONLY
2031 | SEC_CODE)))
2032 && (elf_section_data (os->bfd_section)->this_hdr.sh_info
2033 == elf_section_data (s)->this_hdr.sh_info))
2034 {
b209b5a6 2035 lang_add_section (&os->children, s, NULL, NULL, os);
d871d478
AM
2036 return os;
2037 }
2038
2039 /* Create the output mbind section with the ".mbind." prefix
2040 in section name. */
2041 if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2042 secname = ".mbind.bss";
2043 else if ((s->flags & SEC_READONLY) == 0)
2044 secname = ".mbind.data";
2045 else if ((s->flags & SEC_CODE) == 0)
2046 secname = ".mbind.rodata";
2047 else
2048 secname = ".mbind.text";
2049 elf_tdata (link_info.output_bfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
2050 }
2051
2052 /* Look through the script to see where to place this section. The
2053 script includes entries added by previous lang_insert_orphan
2054 calls, so this loop puts multiple compatible orphans of the same
2055 name into a single output section. */
2056 if (constraint == 0)
2057 for (os = lang_output_section_find (secname);
2058 os != NULL;
2059 os = next_matching_output_section_statement (os, 0))
2060 {
2061 /* If we don't match an existing output section, tell
2062 lang_insert_orphan to create a new output section. */
2063 constraint = SPECIAL;
2064
2065 /* Check to see if we already have an output section statement
2066 with this name, and its bfd section has compatible flags.
2067 If the section already exists but does not have any flags
2068 set, then it has been created by the linker, possibly as a
2069 result of a --section-start command line switch. */
2070 if (os->bfd_section != NULL
2071 && (os->bfd_section->flags == 0
2072 || (((s->flags ^ os->bfd_section->flags)
2073 & (SEC_LOAD | SEC_ALLOC)) == 0
2074 && (!elfinput
2075 || !elfoutput
2076 || elf_orphan_compatible (s, os->bfd_section)))))
2077 {
b209b5a6 2078 lang_add_section (&os->children, s, NULL, NULL, os);
d871d478
AM
2079 return os;
2080 }
2081
2082 /* Save unused output sections in case we can match them
2083 against orphans later. */
2084 if (os->bfd_section == NULL)
2085 match_by_name = os;
2086 }
2087
2088 /* If we didn't match an active output section, see if we matched an
2089 unused one and use that. */
2090 if (match_by_name)
2091 {
b209b5a6 2092 lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
d871d478
AM
2093 return match_by_name;
2094 }
2095
2096 if (!orphan_init_done)
2097 {
2098 struct orphan_save *ho;
2099
2100 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2101 if (ho->name != NULL)
2102 {
2103 ho->os = lang_output_section_find (ho->name);
2104 if (ho->os != NULL && ho->os->flags == 0)
2105 ho->os->flags = ho->flags;
2106 }
2107 orphan_init_done = 1;
2108 }
2109
2110 /* If this is a final link, then always put .gnu.warning.SYMBOL
2111 sections into the .text section to get them out of the way. */
2112 if (bfd_link_executable (&link_info)
08dedd66 2113 && startswith (s->name, ".gnu.warning.")
d871d478
AM
2114 && hold[orphan_text].os != NULL)
2115 {
2116 os = hold[orphan_text].os;
b209b5a6 2117 lang_add_section (&os->children, s, NULL, NULL, os);
d871d478
AM
2118 return os;
2119 }
2120
2121 flags = s->flags;
2122 if (!bfd_link_relocatable (&link_info))
2123 {
2124 nexts = s;
2125 while ((nexts = bfd_get_next_section_by_name (nexts->owner, nexts))
2126 != NULL)
2127 if (nexts->output_section == NULL
2128 && (nexts->flags & SEC_EXCLUDE) == 0
2129 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2130 && (nexts->owner->flags & DYNAMIC) == 0
00f93c44 2131 && !bfd_input_just_syms (nexts->owner)
d871d478
AM
2132 && _bfd_elf_match_sections_by_type (nexts->owner, nexts,
2133 s->owner, s))
2134 flags = (((flags ^ SEC_READONLY)
2135 | (nexts->flags ^ SEC_READONLY))
2136 ^ SEC_READONLY);
2137 }
2138
2139 /* Decide which segment the section should go in based on the
2140 section name and section flags. We put loadable .note sections
2141 right after the .interp section, so that the PT_NOTE segment is
2142 stored right after the program headers where the OS can read it
2143 in the first page. */
2144
2145 place = NULL;
2146 if ((flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2147 place = &hold[orphan_nonalloc];
2148 else if ((flags & SEC_ALLOC) == 0)
2149 ;
2150 else if ((flags & SEC_LOAD) != 0
2151 && (elfinput
2152 ? sh_type == SHT_NOTE
08dedd66 2153 : startswith (secname, ".note")))
d871d478
AM
2154 place = &hold[orphan_interp];
2155 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
2156 place = &hold[orphan_bss];
2157 else if ((flags & SEC_SMALL_DATA) != 0)
2158 place = &hold[orphan_sdata];
2159 else if ((flags & SEC_THREAD_LOCAL) != 0)
2160 place = &hold[orphan_tdata];
2161 else if ((flags & SEC_READONLY) == 0)
2162 place = &hold[orphan_data];
2163 else if ((flags & SEC_LOAD) != 0
2164 && (elfinput
2165 ? sh_type == SHT_RELA || sh_type == SHT_REL
08dedd66 2166 : startswith (secname, ".rel")))
d871d478
AM
2167 place = &hold[orphan_rel];
2168 else if ((flags & SEC_CODE) == 0)
2169 place = &hold[orphan_rodata];
2170 else
2171 place = &hold[orphan_text];
2172
2173 after = NULL;
2174 if (place != NULL)
2175 {
2176 if (place->os == NULL)
2177 {
2178 if (place->name != NULL)
2179 place->os = lang_output_section_find (place->name);
2180 else
2181 {
2182 int rela = elfinput ? sh_type == SHT_RELA : secname[4] == 'a';
2183 place->os = output_rel_find (isdyn, rela);
2184 }
2185 }
2186 after = place->os;
2187 if (after == NULL)
2188 after
2189 = lang_output_section_find_by_flags (s, flags, &place->os,
2190 _bfd_elf_match_sections_by_type);
2191 if (after == NULL)
2192 /* *ABS* is always the first output section statement. */
8ce18f9c 2193 after = (void *) lang_os_list.head;
d871d478
AM
2194 }
2195
2196 return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
2197}
9b538ba7
L
2198
2199void
2200ldelf_before_place_orphans (void)
2201{
2202 bfd *abfd;
2203
2204 for (abfd = link_info.input_bfds;
2205 abfd != (bfd *) NULL; abfd = abfd->link.next)
2206 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2207 && bfd_count_sections (abfd) != 0
2208 && !bfd_input_just_syms (abfd))
2209 {
2210 asection *isec;
2211 for (isec = abfd->sections; isec != NULL; isec = isec->next)
2212 {
2213 /* Discard a section if any of its linked-to section has
2214 been discarded. */
2215 asection *linked_to_sec;
2216 for (linked_to_sec = elf_linked_to_section (isec);
def97fb9 2217 linked_to_sec != NULL && !linked_to_sec->linker_mark;
9b538ba7 2218 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
2219 {
2220 if (discarded_section (linked_to_sec))
2221 {
2222 isec->output_section = bfd_abs_section_ptr;
2223 isec->flags |= SEC_EXCLUDE;
2224 break;
2225 }
2226 linked_to_sec->linker_mark = 1;
2227 }
2228 for (linked_to_sec = elf_linked_to_section (isec);
2229 linked_to_sec != NULL && linked_to_sec->linker_mark;
2230 linked_to_sec = elf_linked_to_section (linked_to_sec))
2231 linked_to_sec->linker_mark = 0;
9b538ba7
L
2232 }
2233 }
2234}
8c0546e9
L
2235
2236void
2237ldelf_set_output_arch (void)
2238{
2239 set_output_arch_default ();
2240 if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour)
2241 elf_link_info (link_info.output_bfd) = &link_info;
2242}