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