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