bfd/
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 # This file is now misnamed, because it supports both 32 bit and 64 bit
4 # ELF emulations.
5 test -z "${ELFSIZE}" && ELFSIZE=32
6 if [ -z "$MACHINE" ]; then
7 OUTPUT_ARCH=${ARCH}
8 else
9 OUTPUT_ARCH=${ARCH}:${MACHINE}
10 fi
11 cat >e${EMULATION_NAME}.c <<EOF
12 /* This file is is generated by a shell script. DO NOT EDIT! */
13
14 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
15 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
16 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
17 Written by Steve Chamberlain <sac@cygnus.com>
18 ELF support by Ian Lance Taylor <ian@cygnus.com>
19
20 This file is part of GLD, the Gnu Linker.
21
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
35
36 #define TARGET_IS_${EMULATION_NAME}
37
38 #include "bfd.h"
39 #include "sysdep.h"
40 #include "libiberty.h"
41 #include "safe-ctype.h"
42 #include "getopt.h"
43
44 #include "bfdlink.h"
45
46 #include "ld.h"
47 #include "ldmain.h"
48 #include "ldmisc.h"
49 #include "ldexp.h"
50 #include "ldlang.h"
51 #include "ldfile.h"
52 #include "ldemul.h"
53 #include <ldgram.h>
54 #include "elf/common.h"
55
56 /* Declare functions used by various EXTRA_EM_FILEs. */
57 static void gld${EMULATION_NAME}_before_parse (void);
58 static void gld${EMULATION_NAME}_after_open (void);
59 static void gld${EMULATION_NAME}_before_allocation (void);
60 static bfd_boolean gld${EMULATION_NAME}_place_orphan
61 (lang_input_statement_type *file, asection *s);
62 static void gld${EMULATION_NAME}_finish (void);
63
64 EOF
65
66 if [ "x${USE_LIBPATH}" = xyes ] ; then
67 case ${target} in
68 *-*-linux-gnu*)
69 cat >>e${EMULATION_NAME}.c <<EOF
70 #include <glob.h>
71 EOF
72 ;;
73 esac
74 fi
75
76 # Import any needed special functions and/or overrides.
77 #
78 if test -n "$EXTRA_EM_FILE" ; then
79 . ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
80 fi
81
82 # Functions in this file can be overridden by setting the LDEMUL_* shell
83 # variables. If the name of the overriding function is the same as is
84 # defined in this file, then don't output this file's version.
85 # If a different overriding name is given then output the standard function
86 # as presumably it is called from the overriding function.
87 #
88 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
89 cat >>e${EMULATION_NAME}.c <<EOF
90
91 static void
92 gld${EMULATION_NAME}_before_parse (void)
93 {
94 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
95 config.dynamic_link = ${DYNAMIC_LINK-TRUE};
96 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
97 }
98
99 EOF
100 fi
101
102 if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
103 cat >>e${EMULATION_NAME}.c <<EOF
104 /* Handle as_needed DT_NEEDED. */
105
106 static bfd_boolean
107 gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
108 {
109 int class = 0;
110
111 /* Tell the ELF linker that we don't want the output file to have a
112 DT_NEEDED entry for this file, unless it is used to resolve
113 references in a regular object. */
114 if (entry->as_needed)
115 class = DYN_AS_NEEDED;
116
117 /* Tell the ELF linker that we don't want the output file to have a
118 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
119 this file at all. */
120 if (!entry->add_needed)
121 class |= DYN_NO_ADD_NEEDED;
122
123 if (!class
124 || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
125 return FALSE;
126
127 bfd_elf_set_dyn_lib_class (entry->the_bfd, class);
128
129 /* Continue on with normal load_symbols processing. */
130 return FALSE;
131 }
132 EOF
133 fi
134
135 cat >>e${EMULATION_NAME}.c <<EOF
136
137 /* These variables are required to pass information back and forth
138 between after_open and check_needed and stat_needed and vercheck. */
139
140 static struct bfd_link_needed_list *global_needed;
141 static struct stat global_stat;
142 static bfd_boolean global_found;
143 static struct bfd_link_needed_list *global_vercheck_needed;
144 static bfd_boolean global_vercheck_failed;
145
146
147 /* On Linux, it's possible to have different versions of the same
148 shared library linked against different versions of libc. The
149 dynamic linker somehow tags which libc version to use in
150 /etc/ld.so.cache, and, based on the libc that it sees in the
151 executable, chooses which version of the shared library to use.
152
153 We try to do a similar check here by checking whether this shared
154 library needs any other shared libraries which may conflict with
155 libraries we have already included in the link. If it does, we
156 skip it, and try to find another shared library farther on down the
157 link path.
158
159 This is called via lang_for_each_input_file.
160 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
161 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
162 a conflicting version. */
163
164 static void
165 gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
166 {
167 const char *soname;
168 struct bfd_link_needed_list *l;
169
170 if (global_vercheck_failed)
171 return;
172 if (s->the_bfd == NULL
173 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
174 return;
175
176 soname = bfd_elf_get_dt_soname (s->the_bfd);
177 if (soname == NULL)
178 soname = lbasename (bfd_get_filename (s->the_bfd));
179
180 for (l = global_vercheck_needed; l != NULL; l = l->next)
181 {
182 const char *suffix;
183
184 if (strcmp (soname, l->name) == 0)
185 {
186 /* Probably can't happen, but it's an easy check. */
187 continue;
188 }
189
190 if (strchr (l->name, '/') != NULL)
191 continue;
192
193 suffix = strstr (l->name, ".so.");
194 if (suffix == NULL)
195 continue;
196
197 suffix += sizeof ".so." - 1;
198
199 if (strncmp (soname, l->name, suffix - l->name) == 0)
200 {
201 /* Here we know that S is a dynamic object FOO.SO.VER1, and
202 the object we are considering needs a dynamic object
203 FOO.SO.VER2, and VER1 and VER2 are different. This
204 appears to be a version mismatch, so we tell the caller
205 to try a different version of this library. */
206 global_vercheck_failed = TRUE;
207 return;
208 }
209 }
210 }
211
212
213 /* See if an input file matches a DT_NEEDED entry by running stat on
214 the file. */
215
216 static void
217 gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
218 {
219 struct stat st;
220 const char *suffix;
221 const char *soname;
222
223 if (global_found)
224 return;
225 if (s->the_bfd == NULL)
226 return;
227
228 if (bfd_stat (s->the_bfd, &st) != 0)
229 {
230 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
231 return;
232 }
233
234 if (st.st_dev == global_stat.st_dev
235 && st.st_ino == global_stat.st_ino)
236 {
237 global_found = TRUE;
238 return;
239 }
240
241 /* We issue a warning if it looks like we are including two
242 different versions of the same shared library. For example,
243 there may be a problem if -lc picks up libc.so.6 but some other
244 shared library has a DT_NEEDED entry of libc.so.5. This is a
245 heuristic test, and it will only work if the name looks like
246 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
247 If we really want to issue warnings about mixing version numbers
248 of shared libraries, we need to find a better way. */
249
250 if (strchr (global_needed->name, '/') != NULL)
251 return;
252 suffix = strstr (global_needed->name, ".so.");
253 if (suffix == NULL)
254 return;
255 suffix += sizeof ".so." - 1;
256
257 soname = bfd_elf_get_dt_soname (s->the_bfd);
258 if (soname == NULL)
259 soname = lbasename (s->filename);
260
261 if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
262 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
263 global_needed->name, global_needed->by, soname);
264 }
265
266 struct dt_needed
267 {
268 bfd *by;
269 const char *name;
270 };
271
272 /* This function is called for each possible name for a dynamic object
273 named by a DT_NEEDED entry. The FORCE parameter indicates whether
274 to skip the check for a conflicting version. */
275
276 static bfd_boolean
277 gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
278 int force)
279 {
280 bfd *abfd;
281 const char *name = needed->name;
282 const char *soname;
283 int class;
284
285 abfd = bfd_openr (name, bfd_get_target (output_bfd));
286 if (abfd == NULL)
287 return FALSE;
288 if (! bfd_check_format (abfd, bfd_object))
289 {
290 bfd_close (abfd);
291 return FALSE;
292 }
293 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
294 {
295 bfd_close (abfd);
296 return FALSE;
297 }
298
299 /* For DT_NEEDED, they have to match. */
300 if (abfd->xvec != output_bfd->xvec)
301 {
302 bfd_close (abfd);
303 return FALSE;
304 }
305
306 /* Check whether this object would include any conflicting library
307 versions. If FORCE is set, then we skip this check; we use this
308 the second time around, if we couldn't find any compatible
309 instance of the shared library. */
310
311 if (! force)
312 {
313 struct bfd_link_needed_list *needed;
314
315 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
316 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
317
318 if (needed != NULL)
319 {
320 global_vercheck_needed = needed;
321 global_vercheck_failed = FALSE;
322 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
323 if (global_vercheck_failed)
324 {
325 bfd_close (abfd);
326 /* Return FALSE to force the caller to move on to try
327 another file on the search path. */
328 return FALSE;
329 }
330
331 /* But wait! It gets much worse. On Linux, if a shared
332 library does not use libc at all, we are supposed to skip
333 it the first time around in case we encounter a shared
334 library later on with the same name which does use the
335 version of libc that we want. This is much too horrible
336 to use on any system other than Linux. */
337
338 EOF
339 case ${target} in
340 *-*-linux-gnu*)
341 cat >>e${EMULATION_NAME}.c <<EOF
342 {
343 struct bfd_link_needed_list *l;
344
345 for (l = needed; l != NULL; l = l->next)
346 if (strncmp (l->name, "libc.so", 7) == 0)
347 break;
348 if (l == NULL)
349 {
350 bfd_close (abfd);
351 return FALSE;
352 }
353 }
354
355 EOF
356 ;;
357 esac
358 cat >>e${EMULATION_NAME}.c <<EOF
359 }
360 }
361
362 /* We've found a dynamic object matching the DT_NEEDED entry. */
363
364 /* We have already checked that there is no other input file of the
365 same name. We must now check again that we are not including the
366 same file twice. We need to do this because on many systems
367 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
368 reference libc.so.1. If we have already included libc.so, we
369 don't want to include libc.so.1 if they are the same file, and we
370 can only check that using stat. */
371
372 if (bfd_stat (abfd, &global_stat) != 0)
373 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
374
375 /* First strip off everything before the last '/'. */
376 soname = lbasename (abfd->filename);
377
378 if (trace_file_tries)
379 info_msg (_("found %s at %s\n"), soname, name);
380
381 global_found = FALSE;
382 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
383 if (global_found)
384 {
385 /* Return TRUE to indicate that we found the file, even though
386 we aren't going to do anything with it. */
387 return TRUE;
388 }
389
390 /* Specify the soname to use. */
391 bfd_elf_set_dt_needed_name (abfd, soname);
392
393 /* Tell the ELF linker that we don't want the output file to have a
394 DT_NEEDED entry for this file, unless it is used to resolve
395 references in a regular object. */
396 class = DYN_DT_NEEDED;
397
398 /* Tell the ELF linker that we don't want the output file to have a
399 DT_NEEDED entry for this file at all if the entry is from a file
400 with DYN_NO_ADD_NEEDED. */
401 if (needed->by != NULL
402 && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
403 class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
404
405 bfd_elf_set_dyn_lib_class (abfd, class);
406
407 /* Add this file into the symbol table. */
408 if (! bfd_link_add_symbols (abfd, &link_info))
409 einfo ("%F%B: could not read symbols: %E\n", abfd);
410
411 return TRUE;
412 }
413
414
415 /* Search for a needed file in a path. */
416
417 static bfd_boolean
418 gld${EMULATION_NAME}_search_needed (const char *path,
419 struct dt_needed *n, int force)
420 {
421 const char *s;
422 const char *name = n->name;
423 size_t len;
424 struct dt_needed needed;
425
426 if (name[0] == '/')
427 return gld${EMULATION_NAME}_try_needed (n, force);
428
429 if (path == NULL || *path == '\0')
430 return FALSE;
431
432 needed.by = n->by;
433 needed.name = n->name;
434
435 len = strlen (name);
436 while (1)
437 {
438 char *filename, *sset;
439
440 s = strchr (path, ':');
441 if (s == NULL)
442 s = path + strlen (path);
443
444 filename = (char *) xmalloc (s - path + len + 2);
445 if (s == path)
446 sset = filename;
447 else
448 {
449 memcpy (filename, path, s - path);
450 filename[s - path] = '/';
451 sset = filename + (s - path) + 1;
452 }
453 strcpy (sset, name);
454
455 needed.name = filename;
456 if (gld${EMULATION_NAME}_try_needed (&needed, force))
457 return TRUE;
458
459 free (filename);
460
461 if (*s == '\0')
462 break;
463 path = s + 1;
464 }
465
466 return FALSE;
467 }
468
469 EOF
470 if [ "x${USE_LIBPATH}" = xyes ] ; then
471 cat >>e${EMULATION_NAME}.c <<EOF
472
473 /* Add the sysroot to every entry in a colon-separated path. */
474
475 static char *
476 gld${EMULATION_NAME}_add_sysroot (const char *path)
477 {
478 int len, colons, i;
479 char *ret, *p;
480
481 len = strlen (path);
482 colons = 0;
483 i = 0;
484 while (path[i])
485 if (path[i++] == ':')
486 colons++;
487
488 if (path[i])
489 colons++;
490
491 len = len + (colons + 1) * strlen (ld_sysroot);
492 ret = xmalloc (len + 1);
493 strcpy (ret, ld_sysroot);
494 p = ret + strlen (ret);
495 i = 0;
496 while (path[i])
497 if (path[i] == ':')
498 {
499 *p++ = path[i++];
500 strcpy (p, ld_sysroot);
501 p = p + strlen (p);
502 }
503 else
504 *p++ = path[i++];
505
506 *p = 0;
507 return ret;
508 }
509
510 EOF
511 case ${target} in
512 *-*-linux-gnu*)
513 cat >>e${EMULATION_NAME}.c <<EOF
514 /* For a native linker, check the file /etc/ld.so.conf for directories
515 in which we may find shared libraries. /etc/ld.so.conf is really
516 only meaningful on Linux. */
517
518 struct gld${EMULATION_NAME}_ld_so_conf
519 {
520 char *path;
521 size_t len, alloc;
522 };
523
524 static void
525 gld${EMULATION_NAME}_parse_ld_so_conf
526 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
527
528 static void
529 gld${EMULATION_NAME}_parse_ld_so_conf_include
530 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename,
531 const char *pattern)
532 {
533 char *newp = NULL;
534 glob_t gl;
535
536 if (pattern[0] != '/')
537 {
538 char *p = strrchr (filename, '/');
539 size_t patlen = strlen (pattern) + 1;
540
541 newp = xmalloc (p - filename + 1 + patlen);
542 memcpy (newp, filename, p - filename + 1);
543 memcpy (newp + (p - filename + 1), pattern, patlen);
544 pattern = newp;
545 }
546
547 if (glob (pattern, 0, NULL, &gl) == 0)
548 {
549 size_t i;
550
551 for (i = 0; i < gl.gl_pathc; ++i)
552 gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]);
553 globfree (&gl);
554 }
555
556 if (newp)
557 free (newp);
558 }
559
560 static void
561 gld${EMULATION_NAME}_parse_ld_so_conf
562 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename)
563 {
564 FILE *f = fopen (filename, FOPEN_RT);
565 char *line;
566 size_t linelen;
567
568 if (f == NULL)
569 return;
570
571 linelen = 256;
572 line = xmalloc (linelen);
573 do
574 {
575 char *p = line, *q;
576
577 /* Normally this would use getline(3), but we need to be portable. */
578 while ((q = fgets (p, linelen - (p - line), f)) != NULL
579 && strlen (q) == linelen - (p - line) - 1
580 && line[linelen - 2] != '\n')
581 {
582 line = xrealloc (line, 2 * linelen);
583 p = line + linelen - 1;
584 linelen += linelen;
585 }
586
587 if (q == NULL && p == line)
588 break;
589
590 p = strchr (line, '\n');
591 if (p)
592 *p = '\0';
593
594 /* Because the file format does not know any form of quoting we
595 can search forward for the next '#' character and if found
596 make it terminating the line. */
597 p = strchr (line, '#');
598 if (p)
599 *p = '\0';
600
601 /* Remove leading whitespace. NUL is no whitespace character. */
602 p = line;
603 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
604 ++p;
605
606 /* If the line is blank it is ignored. */
607 if (p[0] == '\0')
608 continue;
609
610 if (!strncmp (p, "include", 7) && (p[7] == ' ' || p[7] == '\t'))
611 {
612 char *dir, c;
613 p += 8;
614 do
615 {
616 while (*p == ' ' || *p == '\t')
617 ++p;
618
619 if (*p == '\0')
620 break;
621
622 dir = p;
623
624 while (*p != ' ' && *p != '\t' && *p)
625 ++p;
626
627 c = *p;
628 *p++ = '\0';
629 if (dir[0] != '\0')
630 gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename,
631 dir);
632 }
633 while (c != '\0');
634 }
635 else
636 {
637 char *dir = p;
638 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
639 && *p != '\r' && *p != '\v')
640 ++p;
641
642 while (p != dir && p[-1] == '/')
643 --p;
644 if (info->path == NULL)
645 {
646 info->alloc = p - dir + 1 + 256;
647 info->path = xmalloc (info->alloc);
648 info->len = 0;
649 }
650 else
651 {
652 if (info->len + 1 + (p - dir) >= info->alloc)
653 {
654 info->alloc += p - dir + 256;
655 info->path = xrealloc (info->path, info->alloc);
656 }
657 info->path[info->len++] = ':';
658 }
659 memcpy (info->path + info->len, dir, p - dir);
660 info->len += p - dir;
661 info->path[info->len] = '\0';
662 }
663 }
664 while (! feof (f));
665 free (line);
666 fclose (f);
667 }
668
669 static bfd_boolean
670 gld${EMULATION_NAME}_check_ld_so_conf (const char *name, int force)
671 {
672 static bfd_boolean initialized;
673 static char *ld_so_conf;
674 struct dt_needed needed;
675
676 if (! initialized)
677 {
678 char *tmppath;
679 struct gld${EMULATION_NAME}_ld_so_conf info;
680
681 tmppath = concat (ld_sysroot, "/etc/ld.so.conf", NULL);
682 info.path = NULL;
683 info.len = info.alloc = 0;
684 gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
685 free (tmppath);
686 if (info.path)
687 {
688 char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
689 free (info.path);
690 ld_so_conf = d;
691 }
692 initialized = TRUE;
693 }
694
695 if (ld_so_conf == NULL)
696 return FALSE;
697
698
699 needed.by = NULL;
700 needed.name = name;
701 return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
702 }
703
704 EOF
705 # Linux
706 ;;
707 esac
708 fi
709 cat >>e${EMULATION_NAME}.c <<EOF
710
711 /* See if an input file matches a DT_NEEDED entry by name. */
712
713 static void
714 gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
715 {
716 if (global_found)
717 return;
718
719 if (s->filename != NULL)
720 {
721 const char *f;
722
723 if (strcmp (s->filename, global_needed->name) == 0)
724 {
725 global_found = TRUE;
726 return;
727 }
728
729 if (s->search_dirs_flag)
730 {
731 f = strrchr (s->filename, '/');
732 if (f != NULL
733 && strcmp (f + 1, global_needed->name) == 0)
734 {
735 global_found = TRUE;
736 return;
737 }
738 }
739 }
740
741 if (s->the_bfd != NULL)
742 {
743 const char *soname;
744
745 soname = bfd_elf_get_dt_soname (s->the_bfd);
746 if (soname != NULL
747 && strcmp (soname, global_needed->name) == 0)
748 {
749 global_found = TRUE;
750 return;
751 }
752 }
753 }
754
755 EOF
756
757 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
758 cat >>e${EMULATION_NAME}.c <<EOF
759
760 /* This is called after all the input files have been opened. */
761
762 static void
763 gld${EMULATION_NAME}_after_open (void)
764 {
765 struct bfd_link_needed_list *needed, *l;
766
767 /* We only need to worry about this when doing a final link. */
768 if (link_info.relocatable || !link_info.executable)
769 return;
770
771 /* Get the list of files which appear in DT_NEEDED entries in
772 dynamic objects included in the link (often there will be none).
773 For each such file, we want to track down the corresponding
774 library, and include the symbol table in the link. This is what
775 the runtime dynamic linker will do. Tracking the files down here
776 permits one dynamic object to include another without requiring
777 special action by the person doing the link. Note that the
778 needed list can actually grow while we are stepping through this
779 loop. */
780 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
781 for (l = needed; l != NULL; l = l->next)
782 {
783 struct bfd_link_needed_list *ll;
784 struct dt_needed n, nn;
785 int force;
786
787 /* If the lib that needs this one was --as-needed and wasn't
788 found to be needed, then this lib isn't needed either. */
789 if (l->by != NULL
790 && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0)
791 continue;
792
793 /* If we've already seen this file, skip it. */
794 for (ll = needed; ll != l; ll = ll->next)
795 if ((ll->by == NULL
796 || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
797 && strcmp (ll->name, l->name) == 0)
798 break;
799 if (ll != l)
800 continue;
801
802 /* See if this file was included in the link explicitly. */
803 global_needed = l;
804 global_found = FALSE;
805 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
806 if (global_found)
807 continue;
808
809 n.by = l->by;
810 n.name = l->name;
811 nn.by = l->by;
812 if (trace_file_tries)
813 info_msg (_("%s needed by %B\n"), l->name, l->by);
814
815 /* We need to find this file and include the symbol table. We
816 want to search for the file in the same way that the dynamic
817 linker will search. That means that we want to use
818 rpath_link, rpath, then the environment variable
819 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
820 entries (native only), then the linker script LIB_SEARCH_DIRS.
821 We do not search using the -L arguments.
822
823 We search twice. The first time, we skip objects which may
824 introduce version mismatches. The second time, we force
825 their use. See gld${EMULATION_NAME}_vercheck comment. */
826 for (force = 0; force < 2; force++)
827 {
828 size_t len;
829 search_dirs_type *search;
830 EOF
831 if [ "x${USE_LIBPATH}" = xyes ] ; then
832 cat >>e${EMULATION_NAME}.c <<EOF
833 const char *lib_path;
834 struct bfd_link_needed_list *rp;
835 int found;
836 EOF
837 fi
838 cat >>e${EMULATION_NAME}.c <<EOF
839
840 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
841 &n, force))
842 break;
843 EOF
844 if [ "x${USE_LIBPATH}" = xyes ] ; then
845 cat >>e${EMULATION_NAME}.c <<EOF
846 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
847 &n, force))
848 break;
849 EOF
850 fi
851 if [ "x${NATIVE}" = xyes ] ; then
852 cat >>e${EMULATION_NAME}.c <<EOF
853 if (command_line.rpath_link == NULL
854 && command_line.rpath == NULL)
855 {
856 lib_path = (const char *) getenv ("LD_RUN_PATH");
857 if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
858 force))
859 break;
860 }
861 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
862 if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
863 break;
864 EOF
865 fi
866 if [ "x${USE_LIBPATH}" = xyes ] ; then
867 cat >>e${EMULATION_NAME}.c <<EOF
868 found = 0;
869 rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
870 for (; !found && rp != NULL; rp = rp->next)
871 {
872 char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
873 found = (rp->by == l->by
874 && gld${EMULATION_NAME}_search_needed (tmpname,
875 &n,
876 force));
877 free (tmpname);
878 }
879 if (found)
880 break;
881
882 EOF
883 fi
884 cat >>e${EMULATION_NAME}.c <<EOF
885 len = strlen (l->name);
886 for (search = search_head; search != NULL; search = search->next)
887 {
888 char *filename;
889
890 if (search->cmdline)
891 continue;
892 filename = (char *) xmalloc (strlen (search->name) + len + 2);
893 sprintf (filename, "%s/%s", search->name, l->name);
894 nn.name = filename;
895 if (gld${EMULATION_NAME}_try_needed (&nn, force))
896 break;
897 free (filename);
898 }
899 if (search != NULL)
900 break;
901 EOF
902 if [ "x${USE_LIBPATH}" = xyes ] ; then
903 case ${target} in
904 *-*-linux-gnu*)
905 cat >>e${EMULATION_NAME}.c <<EOF
906 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
907 break;
908 EOF
909 # Linux
910 ;;
911 esac
912 fi
913 cat >>e${EMULATION_NAME}.c <<EOF
914 }
915
916 if (force < 2)
917 continue;
918
919 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
920 l->name, l->by);
921 }
922 }
923
924 EOF
925 fi
926
927 cat >>e${EMULATION_NAME}.c <<EOF
928
929 /* Look through an expression for an assignment statement. */
930
931 static void
932 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
933 {
934 struct bfd_link_hash_entry *h;
935
936 switch (exp->type.node_class)
937 {
938 case etree_provide:
939 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
940 FALSE, FALSE, FALSE);
941 if (h == NULL)
942 break;
943
944 /* We call record_link_assignment even if the symbol is defined.
945 This is because if it is defined by a dynamic object, we
946 actually want to use the value defined by the linker script,
947 not the value from the dynamic object (because we are setting
948 symbols like etext). If the symbol is defined by a regular
949 object, then, as it happens, calling record_link_assignment
950 will do no harm. */
951
952 /* Fall through. */
953 case etree_assign:
954 if (strcmp (exp->assign.dst, ".") != 0)
955 {
956 if (! (bfd_elf_record_link_assignment
957 (output_bfd, &link_info, exp->assign.dst,
958 exp->type.node_class == etree_provide ? TRUE : FALSE)))
959 einfo ("%P%F: failed to record assignment to %s: %E\n",
960 exp->assign.dst);
961 }
962 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
963 break;
964
965 case etree_binary:
966 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
967 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
968 break;
969
970 case etree_trinary:
971 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
972 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
973 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
974 break;
975
976 case etree_unary:
977 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
978 break;
979
980 default:
981 break;
982 }
983 }
984
985
986 /* This is called by the before_allocation routine via
987 lang_for_each_statement. It locates any assignment statements, and
988 tells the ELF backend about them, in case they are assignments to
989 symbols which are referred to by dynamic objects. */
990
991 static void
992 gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
993 {
994 if (s->header.type == lang_assignment_statement_enum)
995 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
996 }
997
998 EOF
999
1000 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
1001 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
1002 ELF_INTERPRETER_SET_DEFAULT="
1003 if (sinterp != NULL)
1004 {
1005 sinterp->contents = ${ELF_INTERPRETER_NAME};
1006 sinterp->size = strlen (sinterp->contents) + 1;
1007 }
1008
1009 "
1010 else
1011 ELF_INTERPRETER_SET_DEFAULT=
1012 fi
1013 cat >>e${EMULATION_NAME}.c <<EOF
1014
1015 /* This is called after the sections have been attached to output
1016 sections, but before any sizes or addresses have been set. */
1017
1018 static void
1019 gld${EMULATION_NAME}_before_allocation (void)
1020 {
1021 const char *rpath;
1022 asection *sinterp;
1023
1024 if (link_info.hash->type == bfd_link_elf_hash_table)
1025 _bfd_elf_tls_setup (output_bfd, &link_info);
1026
1027 /* If we are going to make any variable assignments, we need to let
1028 the ELF backend know about them in case the variables are
1029 referred to by dynamic objects. */
1030 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1031
1032 /* Let the ELF backend work out the sizes of any sections required
1033 by dynamic linking. */
1034 rpath = command_line.rpath;
1035 if (rpath == NULL)
1036 rpath = (const char *) getenv ("LD_RUN_PATH");
1037 if (! (bfd_elf_size_dynamic_sections
1038 (output_bfd, command_line.soname, rpath,
1039 command_line.filter_shlib,
1040 (const char * const *) command_line.auxiliary_filters,
1041 &link_info, &sinterp, lang_elf_version_info)))
1042 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1043 ${ELF_INTERPRETER_SET_DEFAULT}
1044 /* Let the user override the dynamic linker we are using. */
1045 if (command_line.interpreter != NULL
1046 && sinterp != NULL)
1047 {
1048 sinterp->contents = (bfd_byte *) command_line.interpreter;
1049 sinterp->size = strlen (command_line.interpreter) + 1;
1050 }
1051
1052 /* Look for any sections named .gnu.warning. As a GNU extensions,
1053 we treat such sections as containing warning messages. We print
1054 out the warning message, and then zero out the section size so
1055 that it does not get copied into the output file. */
1056
1057 {
1058 LANG_FOR_EACH_INPUT_STATEMENT (is)
1059 {
1060 asection *s;
1061 bfd_size_type sz;
1062 bfd_size_type prefix_len;
1063 char *msg;
1064 bfd_boolean ret;
1065 const char * gnu_warning_prefix = _("warning: ");
1066
1067 if (is->just_syms_flag)
1068 continue;
1069
1070 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1071 if (s == NULL)
1072 continue;
1073
1074 sz = s->size;
1075 prefix_len = strlen (gnu_warning_prefix);
1076 msg = xmalloc ((size_t) (prefix_len + sz + 1));
1077 strcpy (msg, gnu_warning_prefix);
1078 if (! bfd_get_section_contents (is->the_bfd, s, msg + prefix_len,
1079 (file_ptr) 0, sz))
1080 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1081 is->the_bfd);
1082 msg[prefix_len + sz] = '\0';
1083 ret = link_info.callbacks->warning (&link_info, msg,
1084 (const char *) NULL,
1085 is->the_bfd, (asection *) NULL,
1086 (bfd_vma) 0);
1087 ASSERT (ret);
1088 free (msg);
1089
1090 /* Clobber the section size, so that we don't waste copying the
1091 warning into the output file. */
1092 s->size = 0;
1093 }
1094 }
1095 }
1096
1097 EOF
1098 fi
1099
1100 if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1101 cat >>e${EMULATION_NAME}.c <<EOF
1102
1103 /* Try to open a dynamic archive. This is where we know that ELF
1104 dynamic libraries have an extension of .so (or .sl on oddball systems
1105 like hpux). */
1106
1107 static bfd_boolean
1108 gld${EMULATION_NAME}_open_dynamic_archive
1109 (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
1110 {
1111 const char *filename;
1112 char *string;
1113
1114 if (! entry->is_archive)
1115 return FALSE;
1116
1117 filename = entry->filename;
1118
1119 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
1120 is defined, but it does not seem worth the headache to optimize
1121 away those two bytes of space. */
1122 string = (char *) xmalloc (strlen (search->name)
1123 + strlen (filename)
1124 + strlen (arch)
1125 #ifdef EXTRA_SHLIB_EXTENSION
1126 + strlen (EXTRA_SHLIB_EXTENSION)
1127 #endif
1128 + sizeof "/lib.so");
1129
1130 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1131
1132 #ifdef EXTRA_SHLIB_EXTENSION
1133 /* Try the .so extension first. If that fails build a new filename
1134 using EXTRA_SHLIB_EXTENSION. */
1135 if (! ldfile_try_open_bfd (string, entry))
1136 sprintf (string, "%s/lib%s%s%s", search->name,
1137 filename, arch, EXTRA_SHLIB_EXTENSION);
1138 #endif
1139
1140 if (! ldfile_try_open_bfd (string, entry))
1141 {
1142 free (string);
1143 return FALSE;
1144 }
1145
1146 entry->filename = string;
1147
1148 /* We have found a dynamic object to include in the link. The ELF
1149 backend linker will create a DT_NEEDED entry in the .dynamic
1150 section naming this file. If this file includes a DT_SONAME
1151 entry, it will be used. Otherwise, the ELF linker will just use
1152 the name of the file. For an archive found by searching, like
1153 this one, the DT_NEEDED entry should consist of just the name of
1154 the file, without the path information used to find it. Note
1155 that we only need to do this if we have a dynamic object; an
1156 archive will never be referenced by a DT_NEEDED entry.
1157
1158 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1159 very pretty. I haven't been able to think of anything that is
1160 pretty, though. */
1161 if (bfd_check_format (entry->the_bfd, bfd_object)
1162 && (entry->the_bfd->flags & DYNAMIC) != 0)
1163 {
1164 ASSERT (entry->is_archive && entry->search_dirs_flag);
1165
1166 /* Rather than duplicating the logic above. Just use the
1167 filename we recorded earlier. */
1168
1169 filename = lbasename (entry->filename);
1170 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1171 }
1172
1173 return TRUE;
1174 }
1175
1176 EOF
1177 fi
1178
1179 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1180 cat >>e${EMULATION_NAME}.c <<EOF
1181
1182 /* A variant of lang_output_section_find used by place_orphan. */
1183
1184 static lang_output_section_statement_type *
1185 output_rel_find (asection *sec, int isdyn)
1186 {
1187 lang_output_section_statement_type *lookup;
1188 lang_output_section_statement_type *last = NULL;
1189 lang_output_section_statement_type *last_alloc = NULL;
1190 lang_output_section_statement_type *last_rel = NULL;
1191 lang_output_section_statement_type *last_rel_alloc = NULL;
1192 int rela = sec->name[4] == 'a';
1193
1194 for (lookup = &lang_output_section_statement.head->output_section_statement;
1195 lookup != NULL;
1196 lookup = lookup->next)
1197 {
1198 if (lookup->constraint != -1
1199 && strncmp (".rel", lookup->name, 4) == 0)
1200 {
1201 int lookrela = lookup->name[4] == 'a';
1202
1203 /* .rel.dyn must come before all other reloc sections, to suit
1204 GNU ld.so. */
1205 if (isdyn)
1206 break;
1207
1208 /* Don't place after .rel.plt as doing so results in wrong
1209 dynamic tags. */
1210 if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
1211 break;
1212
1213 if (rela == lookrela || last_rel == NULL)
1214 last_rel = lookup;
1215 if ((rela == lookrela || last_rel_alloc == NULL)
1216 && lookup->bfd_section != NULL
1217 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1218 last_rel_alloc = lookup;
1219 }
1220
1221 last = lookup;
1222 if (lookup->bfd_section != NULL
1223 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1224 last_alloc = lookup;
1225 }
1226
1227 if (last_rel_alloc)
1228 return last_rel_alloc;
1229
1230 if (last_rel)
1231 return last_rel;
1232
1233 if (last_alloc)
1234 return last_alloc;
1235
1236 return last;
1237 }
1238
1239 /* Place an orphan section. We use this to put random SHF_ALLOC
1240 sections in the right segment. */
1241
1242 static bfd_boolean
1243 gld${EMULATION_NAME}_place_orphan (lang_input_statement_type *file, asection *s)
1244 {
1245 static struct orphan_save hold[] =
1246 {
1247 { ".text",
1248 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1249 0, 0, 0, 0 },
1250 { ".rodata",
1251 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1252 0, 0, 0, 0 },
1253 { ".data",
1254 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1255 0, 0, 0, 0 },
1256 { ".bss",
1257 SEC_ALLOC,
1258 0, 0, 0, 0 },
1259 { 0,
1260 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1261 0, 0, 0, 0 },
1262 { ".interp",
1263 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1264 0, 0, 0, 0 },
1265 { ".sdata",
1266 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
1267 0, 0, 0, 0 }
1268 };
1269 enum orphan_save_index
1270 {
1271 orphan_text = 0,
1272 orphan_rodata,
1273 orphan_data,
1274 orphan_bss,
1275 orphan_rel,
1276 orphan_interp,
1277 orphan_sdata
1278 };
1279 static int orphan_init_done = 0;
1280 struct orphan_save *place;
1281 const char *secname;
1282 lang_output_section_statement_type *after;
1283 lang_output_section_statement_type *os;
1284 int isdyn = 0;
1285
1286 secname = bfd_get_section_name (s->owner, s);
1287
1288 if (! link_info.relocatable
1289 && link_info.combreloc
1290 && (s->flags & SEC_ALLOC)
1291 && strncmp (secname, ".rel", 4) == 0)
1292 {
1293 if (secname[4] == 'a')
1294 secname = ".rela.dyn";
1295 else
1296 secname = ".rel.dyn";
1297 isdyn = 1;
1298 }
1299
1300 if (isdyn || (!config.unique_orphan_sections && !unique_section_p (s)))
1301 {
1302 /* Look through the script to see where to place this section. */
1303 os = lang_output_section_find (secname);
1304
1305 if (os != NULL
1306 && (os->bfd_section == NULL
1307 || os->bfd_section->flags == 0
1308 || ((s->flags ^ os->bfd_section->flags)
1309 & (SEC_LOAD | SEC_ALLOC)) == 0))
1310 {
1311 /* We already have an output section statement with this
1312 name, and its bfd section, if any, has compatible flags.
1313 If the section already exists but does not have any flags
1314 set, then it has been created by the linker, probably as a
1315 result of a --section-start command line switch. */
1316 lang_add_section (&os->children, s, os, file);
1317 return TRUE;
1318 }
1319 }
1320
1321 if (!orphan_init_done)
1322 {
1323 struct orphan_save *ho;
1324 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1325 if (ho->name != NULL)
1326 {
1327 ho->os = lang_output_section_find (ho->name);
1328 if (ho->os != NULL && ho->os->flags == 0)
1329 ho->os->flags = ho->flags;
1330 }
1331 orphan_init_done = 1;
1332 }
1333
1334 /* If this is a final link, then always put .gnu.warning.SYMBOL
1335 sections into the .text section to get them out of the way. */
1336 if (link_info.executable
1337 && ! link_info.relocatable
1338 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
1339 && hold[orphan_text].os != NULL)
1340 {
1341 lang_add_section (&hold[orphan_text].os->children, s,
1342 hold[orphan_text].os, file);
1343 return TRUE;
1344 }
1345
1346 /* Decide which segment the section should go in based on the
1347 section name and section flags. We put loadable .note sections
1348 right after the .interp section, so that the PT_NOTE segment is
1349 stored right after the program headers where the OS can read it
1350 in the first page. */
1351
1352 place = NULL;
1353 if ((s->flags & SEC_ALLOC) == 0)
1354 ;
1355 else if ((s->flags & SEC_LOAD) != 0
1356 && strncmp (secname, ".note", 5) == 0)
1357 place = &hold[orphan_interp];
1358 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1359 place = &hold[orphan_bss];
1360 else if ((s->flags & SEC_SMALL_DATA) != 0)
1361 place = &hold[orphan_sdata];
1362 else if ((s->flags & SEC_READONLY) == 0)
1363 place = &hold[orphan_data];
1364 else if (strncmp (secname, ".rel", 4) == 0
1365 && (s->flags & SEC_LOAD) != 0)
1366 place = &hold[orphan_rel];
1367 else if ((s->flags & SEC_CODE) == 0)
1368 place = &hold[orphan_rodata];
1369 else
1370 place = &hold[orphan_text];
1371
1372 after = NULL;
1373 if (place != NULL)
1374 {
1375 if (place->os == NULL)
1376 {
1377 if (place->name != NULL)
1378 place->os = lang_output_section_find (place->name);
1379 else
1380 place->os = output_rel_find (s, isdyn);
1381 }
1382 after = place->os;
1383 if (after == NULL)
1384 after = lang_output_section_find_by_flags (s, &place->os);
1385 if (after == NULL)
1386 /* *ABS* is always the first output section statement. */
1387 after = &lang_output_section_statement.head->output_section_statement;
1388 }
1389
1390 /* Choose a unique name for the section. This will be needed if the
1391 same section name appears in the input file with different
1392 loadable or allocatable characteristics. */
1393 if (bfd_get_section_by_name (output_bfd, secname) != NULL)
1394 {
1395 static int count = 1;
1396 secname = bfd_get_unique_section_name (output_bfd, secname, &count);
1397 if (secname == NULL)
1398 einfo ("%F%P: place_orphan failed: %E\n");
1399 }
1400
1401 lang_insert_orphan (file, s, secname, after, place, NULL, NULL);
1402
1403 return TRUE;
1404 }
1405 EOF
1406 fi
1407
1408 if test x"$LDEMUL_FINISH" != xgld"$EMULATION_NAME"_finish; then
1409 cat >>e${EMULATION_NAME}.c <<EOF
1410
1411 static void
1412 gld${EMULATION_NAME}_finish (void)
1413 {
1414 if (bfd_elf_discard_info (output_bfd, &link_info))
1415 {
1416 lang_reset_memory_regions ();
1417
1418 /* Resize the sections. */
1419 lang_size_sections (stat_ptr->head, abs_output_section,
1420 &stat_ptr->head, 0, (bfd_vma) 0, NULL, TRUE);
1421
1422 /* Redo special stuff. */
1423 ldemul_after_allocation ();
1424
1425 /* Do the assignments again. */
1426 lang_do_assignments (stat_ptr->head, abs_output_section,
1427 (fill_type *) 0, (bfd_vma) 0);
1428 }
1429 }
1430 EOF
1431 fi
1432
1433 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1434 cat >>e${EMULATION_NAME}.c <<EOF
1435
1436 static char *
1437 gld${EMULATION_NAME}_get_script (int *isfile)
1438 EOF
1439
1440 if test -n "$COMPILE_IN"
1441 then
1442 # Scripts compiled in.
1443
1444 # sed commands to quote an ld script as a C string.
1445 sc="-f stringify.sed"
1446
1447 cat >>e${EMULATION_NAME}.c <<EOF
1448 {
1449 *isfile = 0;
1450
1451 if (link_info.relocatable && config.build_constructors)
1452 return
1453 EOF
1454 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1455 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1456 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1457 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1458 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1459 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
1460 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1461 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1462 fi
1463 if test -n "$GENERATE_PIE_SCRIPT" ; then
1464 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1465 echo ' ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c
1466 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
1467 echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1468 sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c
1469 echo ' ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1470 sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c
1471 fi
1472 echo ' ; else if (link_info.pie) return' >> e${EMULATION_NAME}.c
1473 sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c
1474 fi
1475 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1476 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1477 echo ' ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c
1478 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
1479 echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1480 sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c
1481 echo ' ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1482 sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c
1483 fi
1484 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1485 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
1486 fi
1487 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1488 echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c
1489 echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1490 sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c
1491 echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c
1492 sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c
1493 fi
1494 echo ' ; else return' >> e${EMULATION_NAME}.c
1495 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1496 echo '; }' >> e${EMULATION_NAME}.c
1497
1498 else
1499 # Scripts read from the filesystem.
1500
1501 cat >>e${EMULATION_NAME}.c <<EOF
1502 {
1503 *isfile = 1;
1504
1505 if (link_info.relocatable && config.build_constructors)
1506 return "ldscripts/${EMULATION_NAME}.xu";
1507 else if (link_info.relocatable)
1508 return "ldscripts/${EMULATION_NAME}.xr";
1509 else if (!config.text_read_only)
1510 return "ldscripts/${EMULATION_NAME}.xbn";
1511 EOF
1512 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
1513 else
1514 cat >>e${EMULATION_NAME}.c <<EOF
1515 else if (!config.magic_demand_paged)
1516 return "ldscripts/${EMULATION_NAME}.xn";
1517 EOF
1518 fi
1519 if test -n "$GENERATE_PIE_SCRIPT" ; then
1520 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1521 cat >>e${EMULATION_NAME}.c <<EOF
1522 else if (link_info.pie && link_info.combreloc
1523 && link_info.relro && (link_info.flags & DT_BIND_NOW))
1524 return "ldscripts/${EMULATION_NAME}.xdw";
1525 else if (link_info.pie && link_info.combreloc)
1526 return "ldscripts/${EMULATION_NAME}.xdc";
1527 EOF
1528 fi
1529 cat >>e${EMULATION_NAME}.c <<EOF
1530 else if (link_info.pie)
1531 return "ldscripts/${EMULATION_NAME}.xd";
1532 EOF
1533 fi
1534 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1535 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1536 cat >>e${EMULATION_NAME}.c <<EOF
1537 else if (link_info.shared && link_info.combreloc
1538 && link_info.relro && (link_info.flags & DT_BIND_NOW))
1539 return "ldscripts/${EMULATION_NAME}.xsw";
1540 else if (link_info.shared && link_info.combreloc)
1541 return "ldscripts/${EMULATION_NAME}.xsc";
1542 EOF
1543 fi
1544 cat >>e${EMULATION_NAME}.c <<EOF
1545 else if (link_info.shared)
1546 return "ldscripts/${EMULATION_NAME}.xs";
1547 EOF
1548 fi
1549 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1550 cat >>e${EMULATION_NAME}.c <<EOF
1551 else if (link_info.combreloc && link_info.relro
1552 && (link_info.flags & DT_BIND_NOW))
1553 return "ldscripts/${EMULATION_NAME}.xw";
1554 else if (link_info.combreloc)
1555 return "ldscripts/${EMULATION_NAME}.xc";
1556 EOF
1557 fi
1558 cat >>e${EMULATION_NAME}.c <<EOF
1559 else
1560 return "ldscripts/${EMULATION_NAME}.x";
1561 }
1562
1563 EOF
1564 fi
1565 fi
1566
1567 if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1568
1569 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1570 cat >>e${EMULATION_NAME}.c <<EOF
1571 $PARSE_AND_LIST_PROLOGUE
1572 EOF
1573 fi
1574
1575 cat >>e${EMULATION_NAME}.c <<EOF
1576
1577 #define OPTION_DISABLE_NEW_DTAGS (400)
1578 #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
1579 #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
1580 #define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1)
1581 #define OPTION_EXCLUDE_LIBS (OPTION_EH_FRAME_HDR + 1)
1582
1583 static void
1584 gld${EMULATION_NAME}_add_options
1585 (int ns, char **shortopts, int nl, struct option **longopts,
1586 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
1587 {
1588 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
1589 static const struct option xtra_long[] = {
1590 EOF
1591
1592 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1593 cat >>e${EMULATION_NAME}.c <<EOF
1594 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1595 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1596 {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
1597 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
1598 {"Bgroup", no_argument, NULL, OPTION_GROUP},
1599 EOF
1600 fi
1601
1602 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1603 cat >>e${EMULATION_NAME}.c <<EOF
1604 $PARSE_AND_LIST_LONGOPTS
1605 EOF
1606 fi
1607
1608 cat >>e${EMULATION_NAME}.c <<EOF
1609 {NULL, no_argument, NULL, 0}
1610 };
1611
1612 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
1613 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
1614 *longopts = (struct option *)
1615 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
1616 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
1617 }
1618
1619 static bfd_boolean
1620 gld${EMULATION_NAME}_handle_option (int optc)
1621 {
1622 switch (optc)
1623 {
1624 default:
1625 return FALSE;
1626
1627 EOF
1628
1629 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1630 cat >>e${EMULATION_NAME}.c <<EOF
1631 case OPTION_DISABLE_NEW_DTAGS:
1632 link_info.new_dtags = FALSE;
1633 break;
1634
1635 case OPTION_ENABLE_NEW_DTAGS:
1636 link_info.new_dtags = TRUE;
1637 break;
1638
1639 case OPTION_EH_FRAME_HDR:
1640 link_info.eh_frame_hdr = TRUE;
1641 break;
1642
1643 case OPTION_GROUP:
1644 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
1645 /* Groups must be self-contained. */
1646 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
1647 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
1648 break;
1649
1650 case OPTION_EXCLUDE_LIBS:
1651 add_excluded_libs (optarg);
1652 break;
1653
1654 case 'z':
1655 if (strcmp (optarg, "initfirst") == 0)
1656 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1657 else if (strcmp (optarg, "interpose") == 0)
1658 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1659 else if (strcmp (optarg, "loadfltr") == 0)
1660 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1661 else if (strcmp (optarg, "nodefaultlib") == 0)
1662 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1663 else if (strcmp (optarg, "nodelete") == 0)
1664 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1665 else if (strcmp (optarg, "nodlopen") == 0)
1666 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1667 else if (strcmp (optarg, "nodump") == 0)
1668 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1669 else if (strcmp (optarg, "now") == 0)
1670 {
1671 link_info.flags |= (bfd_vma) DF_BIND_NOW;
1672 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1673 }
1674 else if (strcmp (optarg, "origin") == 0)
1675 {
1676 link_info.flags |= (bfd_vma) DF_ORIGIN;
1677 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1678 }
1679 else if (strcmp (optarg, "defs") == 0)
1680 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
1681 else if (strcmp (optarg, "muldefs") == 0)
1682 link_info.allow_multiple_definition = TRUE;
1683 else if (strcmp (optarg, "combreloc") == 0)
1684 link_info.combreloc = TRUE;
1685 else if (strcmp (optarg, "nocombreloc") == 0)
1686 link_info.combreloc = FALSE;
1687 else if (strcmp (optarg, "nocopyreloc") == 0)
1688 link_info.nocopyreloc = TRUE;
1689 else if (strcmp (optarg, "execstack") == 0)
1690 {
1691 link_info.execstack = TRUE;
1692 link_info.noexecstack = FALSE;
1693 }
1694 else if (strcmp (optarg, "noexecstack") == 0)
1695 {
1696 link_info.noexecstack = TRUE;
1697 link_info.execstack = FALSE;
1698 }
1699 else if (strcmp (optarg, "relro") == 0)
1700 link_info.relro = TRUE;
1701 else if (strcmp (optarg, "norelro") == 0)
1702 link_info.relro = FALSE;
1703 /* What about the other Solaris -z options? FIXME. */
1704 break;
1705 EOF
1706 fi
1707
1708 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1709 cat >>e${EMULATION_NAME}.c <<EOF
1710 $PARSE_AND_LIST_ARGS_CASES
1711 EOF
1712 fi
1713
1714 cat >>e${EMULATION_NAME}.c <<EOF
1715 }
1716
1717 return TRUE;
1718 }
1719
1720 EOF
1721
1722 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1723 cat >>e${EMULATION_NAME}.c <<EOF
1724
1725 static void
1726 gld${EMULATION_NAME}_list_options (FILE * file)
1727 {
1728 EOF
1729
1730 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1731 cat >>e${EMULATION_NAME}.c <<EOF
1732 fprintf (file, _(" -Bgroup\t\tSelects group name lookup rules for DSO\n"));
1733 fprintf (file, _(" --disable-new-dtags\tDisable new dynamic tags\n"));
1734 fprintf (file, _(" --enable-new-dtags\tEnable new dynamic tags\n"));
1735 fprintf (file, _(" --eh-frame-hdr\tCreate .eh_frame_hdr section\n"));
1736 fprintf (file, _(" -z combreloc\t\tMerge dynamic relocs into one section and sort\n"));
1737 fprintf (file, _(" -z defs\t\tReport unresolved symbols in object files.\n"));
1738 fprintf (file, _(" -z execstack\t\tMark executable as requiring executable stack\n"));
1739 fprintf (file, _(" -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
1740 fprintf (file, _(" -z interpose\t\tMark object to interpose all DSOs but executable\n"));
1741 fprintf (file, _(" -z loadfltr\t\tMark object requiring immediate process\n"));
1742 fprintf (file, _(" -z muldefs\t\tAllow multiple definitions\n"));
1743 fprintf (file, _(" -z nocombreloc\tDon't merge dynamic relocs into one section\n"));
1744 fprintf (file, _(" -z nocopyreloc\tDon't create copy relocs\n"));
1745 fprintf (file, _(" -z nodefaultlib\tMark object not to use default search paths\n"));
1746 fprintf (file, _(" -z nodelete\t\tMark DSO non-deletable at runtime\n"));
1747 fprintf (file, _(" -z nodlopen\t\tMark DSO not available to dlopen\n"));
1748 fprintf (file, _(" -z nodump\t\tMark DSO not available to dldump\n"));
1749 fprintf (file, _(" -z noexecstack\tMark executable as not requiring executable stack\n"));
1750 fprintf (file, _(" -z norelro\t\tDon't create RELRO program header\n"));
1751 fprintf (file, _(" -z now\t\tMark object non-lazy runtime binding\n"));
1752 fprintf (file, _(" -z origin\t\tMark object requiring immediate \$ORIGIN processing\n\t\t\t at runtime\n"));
1753 fprintf (file, _(" -z relro\t\tCreate RELRO program header\n"));
1754 fprintf (file, _(" -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
1755 EOF
1756 fi
1757
1758 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1759 cat >>e${EMULATION_NAME}.c <<EOF
1760 $PARSE_AND_LIST_OPTIONS
1761 EOF
1762 fi
1763
1764 cat >>e${EMULATION_NAME}.c <<EOF
1765 }
1766 EOF
1767
1768 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1769 cat >>e${EMULATION_NAME}.c <<EOF
1770 $PARSE_AND_LIST_EPILOGUE
1771 EOF
1772 fi
1773 fi
1774 else
1775 cat >>e${EMULATION_NAME}.c <<EOF
1776 #define gld${EMULATION_NAME}_add_options NULL
1777 #define gld${EMULATION_NAME}_handle_option NULL
1778 EOF
1779 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1780 cat >>e${EMULATION_NAME}.c <<EOF
1781 #define gld${EMULATION_NAME}_list_options NULL
1782 EOF
1783 fi
1784 fi
1785
1786 cat >>e${EMULATION_NAME}.c <<EOF
1787
1788 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1789 {
1790 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
1791 ${LDEMUL_SYSLIB-syslib_default},
1792 ${LDEMUL_HLL-hll_default},
1793 ${LDEMUL_AFTER_PARSE-after_parse_default},
1794 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
1795 ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
1796 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
1797 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
1798 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
1799 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
1800 "${EMULATION_NAME}",
1801 "${OUTPUT_FORMAT}",
1802 ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
1803 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
1804 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
1805 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
1806 ${LDEMUL_SET_SYMBOLS-NULL},
1807 ${LDEMUL_PARSE_ARGS-NULL},
1808 gld${EMULATION_NAME}_add_options,
1809 gld${EMULATION_NAME}_handle_option,
1810 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
1811 ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
1812 ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
1813 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
1814 ${LDEMUL_NEW_VERS_PATTERN-NULL}
1815 };
1816 EOF
This page took 0.064608 seconds and 5 git commands to generate.