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