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