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