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