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