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