Fix bug in previous delta
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
CommitLineData
252b5132
RH
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.
5test -z "${ELFSIZE}" && ELFSIZE=32
86af25fe
L
6if [ -z "$MACHINE" ]; then
7 OUTPUT_ARCH=${ARCH}
8else
9 OUTPUT_ARCH=${ARCH}:${MACHINE}
10fi
252b5132
RH
11cat >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}
a2b64bed 15 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
87f2a346 16 Free Software Foundation, Inc.
252b5132
RH
17 Written by Steve Chamberlain <sac@cygnus.com>
18 ELF support by Ian Lance Taylor <ian@cygnus.com>
19
20This file is part of GLD, the Gnu Linker.
21
22This program is free software; you can redistribute it and/or modify
23it under the terms of the GNU General Public License as published by
24the Free Software Foundation; either version 2 of the License, or
25(at your option) any later version.
26
27This program is distributed in the hope that it will be useful,
28but WITHOUT ANY WARRANTY; without even the implied warranty of
29MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30GNU General Public License for more details.
31
32You should have received a copy of the GNU General Public License
33along with this program; if not, write to the Free Software
34Foundation, 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"
1c9acd94 40#include "libiberty.h"
252b5132
RH
41
42#include <ctype.h>
43
44#include "bfdlink.h"
45
46#include "ld.h"
47#include "ldmain.h"
252b5132
RH
48#include "ldmisc.h"
49#include "ldexp.h"
50#include "ldlang.h"
b71e2778
AM
51#include "ldfile.h"
52#include "ldemul.h"
252b5132 53#include "ldgram.h"
e0ee487b 54#include "elf/common.h"
252b5132
RH
55
56static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
04925e1e 57static void gld${EMULATION_NAME}_vercheck
252b5132
RH
58 PARAMS ((lang_input_statement_type *));
59static void gld${EMULATION_NAME}_stat_needed
60 PARAMS ((lang_input_statement_type *));
04925e1e 61static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
252b5132
RH
62static boolean gld${EMULATION_NAME}_search_needed
63 PARAMS ((const char *, const char *, int));
04925e1e 64static void gld${EMULATION_NAME}_check_needed
252b5132 65 PARAMS ((lang_input_statement_type *));
04925e1e
AM
66static void gld${EMULATION_NAME}_after_open PARAMS ((void));
67static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
252b5132
RH
68static void gld${EMULATION_NAME}_find_statement_assignment
69 PARAMS ((lang_statement_union_type *));
04925e1e
AM
70static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
71static boolean gld${EMULATION_NAME}_open_dynamic_archive
72 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
73static lang_output_section_statement_type *output_rel_find PARAMS ((void));
73810896
AM
74static asection *output_prev_sec_find
75 PARAMS ((lang_output_section_statement_type *));
252b5132
RH
76static boolean gld${EMULATION_NAME}_place_orphan
77 PARAMS ((lang_input_statement_type *, asection *));
252b5132
RH
78static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
79
41392f03
AM
80EOF
81
82# Import any needed special functions and/or overrides.
83#
84if test -n "$EXTRA_EM_FILE" ; then
85. ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
86fi
87
f813923c 88# Functions in this file can be overridden by setting the LDEMUL_* shell
41392f03
AM
89# variables. If the name of the overriding function is the same as is
90# defined in this file, then don't output this file's version.
91# If a different overriding name is given then output the standard function
92# as presumably it is called from the overriding function.
93#
94if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
95cat >>e${EMULATION_NAME}.c <<EOF
96
252b5132 97static void
41392f03 98gld${EMULATION_NAME}_before_parse ()
252b5132 99{
86af25fe
L
100 const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
101 if (arch)
102 {
103 ldfile_output_architecture = arch->arch;
104 ldfile_output_machine = arch->mach;
105 ldfile_output_machine_name = arch->printable_name;
106 }
107 else
108 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
252b5132
RH
109 config.dynamic_link = ${DYNAMIC_LINK-true};
110 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
111}
112
41392f03
AM
113EOF
114fi
115
116cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
117
118/* These variables are required to pass information back and forth
119 between after_open and check_needed and stat_needed and vercheck. */
120
121static struct bfd_link_needed_list *global_needed;
122static struct stat global_stat;
123static boolean global_found;
124static struct bfd_link_needed_list *global_vercheck_needed;
125static boolean global_vercheck_failed;
126
252b5132 127
04925e1e
AM
128/* On Linux, it's possible to have different versions of the same
129 shared library linked against different versions of libc. The
130 dynamic linker somehow tags which libc version to use in
131 /etc/ld.so.cache, and, based on the libc that it sees in the
132 executable, chooses which version of the shared library to use.
252b5132 133
04925e1e
AM
134 We try to do a similar check here by checking whether this shared
135 library needs any other shared libraries which may conflict with
136 libraries we have already included in the link. If it does, we
137 skip it, and try to find another shared library farther on down the
138 link path.
252b5132 139
04925e1e
AM
140 This is called via lang_for_each_input_file.
141 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
f813923c 142 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
04925e1e 143 a conflicting version. */
252b5132 144
04925e1e
AM
145static void
146gld${EMULATION_NAME}_vercheck (s)
147 lang_input_statement_type *s;
148{
1c9acd94 149 const char *soname;
04925e1e 150 struct bfd_link_needed_list *l;
252b5132 151
04925e1e
AM
152 if (global_vercheck_failed)
153 return;
154 if (s->the_bfd == NULL
155 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
156 return;
252b5132 157
04925e1e
AM
158 soname = bfd_elf_get_dt_soname (s->the_bfd);
159 if (soname == NULL)
1c9acd94 160 soname = basename (bfd_get_filename (s->the_bfd));
04925e1e
AM
161
162 for (l = global_vercheck_needed; l != NULL; l = l->next)
163 {
164 const char *suffix;
165
1c9acd94 166 if (strcmp (soname, l->name) == 0)
04925e1e
AM
167 {
168 /* Probably can't happen, but it's an easy check. */
169 continue;
252b5132
RH
170 }
171
04925e1e 172 if (strchr (l->name, '/') != NULL)
252b5132
RH
173 continue;
174
04925e1e
AM
175 suffix = strstr (l->name, ".so.");
176 if (suffix == NULL)
177 continue;
178
179 suffix += sizeof ".so." - 1;
180
1c9acd94 181 if (strncmp (soname, l->name, suffix - l->name) == 0)
04925e1e
AM
182 {
183 /* Here we know that S is a dynamic object FOO.SO.VER1, and
184 the object we are considering needs a dynamic object
185 FOO.SO.VER2, and VER1 and VER2 are different. This
186 appears to be a version mismatch, so we tell the caller
187 to try a different version of this library. */
188 global_vercheck_failed = true;
189 return;
190 }
252b5132
RH
191 }
192}
193
252b5132 194
04925e1e
AM
195/* See if an input file matches a DT_NEEDED entry by running stat on
196 the file. */
197
198static void
199gld${EMULATION_NAME}_stat_needed (s)
200 lang_input_statement_type *s;
252b5132 201{
04925e1e
AM
202 struct stat st;
203 const char *suffix;
204 const char *soname;
252b5132 205
04925e1e
AM
206 if (global_found)
207 return;
208 if (s->the_bfd == NULL)
209 return;
210
211 if (bfd_stat (s->the_bfd, &st) != 0)
252b5132 212 {
04925e1e
AM
213 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
214 return;
215 }
252b5132 216
04925e1e
AM
217 if (st.st_dev == global_stat.st_dev
218 && st.st_ino == global_stat.st_ino)
219 {
220 global_found = true;
221 return;
222 }
252b5132 223
04925e1e
AM
224 /* We issue a warning if it looks like we are including two
225 different versions of the same shared library. For example,
226 there may be a problem if -lc picks up libc.so.6 but some other
227 shared library has a DT_NEEDED entry of libc.so.5. This is a
f813923c 228 heuristic test, and it will only work if the name looks like
04925e1e
AM
229 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
230 If we really want to issue warnings about mixing version numbers
231 of shared libraries, we need to find a better way. */
252b5132 232
04925e1e
AM
233 if (strchr (global_needed->name, '/') != NULL)
234 return;
235 suffix = strstr (global_needed->name, ".so.");
236 if (suffix == NULL)
237 return;
238 suffix += sizeof ".so." - 1;
252b5132 239
04925e1e
AM
240 soname = bfd_elf_get_dt_soname (s->the_bfd);
241 if (soname == NULL)
1c9acd94 242 soname = basename (s->filename);
252b5132 243
1c9acd94
L
244 if (strncmp (soname, global_needed->name,
245 suffix - global_needed->name) == 0)
04925e1e 246 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
1c9acd94 247 global_needed->name, global_needed->by, soname);
252b5132
RH
248}
249
04925e1e 250
252b5132
RH
251/* This function is called for each possible name for a dynamic object
252 named by a DT_NEEDED entry. The FORCE parameter indicates whether
253 to skip the check for a conflicting version. */
254
255static boolean
256gld${EMULATION_NAME}_try_needed (name, force)
257 const char *name;
258 int force;
259{
260 bfd *abfd;
ec4eb78a 261 const char *soname;
252b5132
RH
262
263 abfd = bfd_openr (name, bfd_get_target (output_bfd));
264 if (abfd == NULL)
265 return false;
266 if (! bfd_check_format (abfd, bfd_object))
267 {
268 (void) bfd_close (abfd);
269 return false;
270 }
271 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
272 {
273 (void) bfd_close (abfd);
274 return false;
275 }
276
277 /* Check whether this object would include any conflicting library
278 versions. If FORCE is set, then we skip this check; we use this
279 the second time around, if we couldn't find any compatible
280 instance of the shared library. */
281
282 if (! force)
283 {
284 struct bfd_link_needed_list *needed;
285
286 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
287 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
288
289 if (needed != NULL)
290 {
291 global_vercheck_needed = needed;
292 global_vercheck_failed = false;
293 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
294 if (global_vercheck_failed)
295 {
296 (void) bfd_close (abfd);
297 /* Return false to force the caller to move on to try
298 another file on the search path. */
299 return false;
300 }
301
302 /* But wait! It gets much worse. On Linux, if a shared
303 library does not use libc at all, we are supposed to skip
304 it the first time around in case we encounter a shared
305 library later on with the same name which does use the
306 version of libc that we want. This is much too horrible
307 to use on any system other than Linux. */
308
309EOF
310case ${target} in
311 *-*-linux-gnu*)
312 cat >>e${EMULATION_NAME}.c <<EOF
313 {
314 struct bfd_link_needed_list *l;
315
316 for (l = needed; l != NULL; l = l->next)
317 if (strncmp (l->name, "libc.so", 7) == 0)
318 break;
319 if (l == NULL)
320 {
321 (void) bfd_close (abfd);
322 return false;
323 }
324 }
325
326EOF
327 ;;
328esac
329cat >>e${EMULATION_NAME}.c <<EOF
330 }
331 }
332
333 /* We've found a dynamic object matching the DT_NEEDED entry. */
334
04925e1e
AM
335 /* We have already checked that there is no other input file of the
336 same name. We must now check again that we are not including the
337 same file twice. We need to do this because on many systems
338 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
339 reference libc.so.1. If we have already included libc.so, we
340 don't want to include libc.so.1 if they are the same file, and we
341 can only check that using stat. */
342
343 if (bfd_stat (abfd, &global_stat) != 0)
344 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
ec4eb78a
L
345
346 /* First strip off everything before the last '/'. */
1c9acd94 347 soname = basename (abfd->filename);
ec4eb78a
L
348
349 if (trace_file_tries)
350 info_msg (_("found %s at %s\n"), soname, name);
351
04925e1e
AM
352 global_found = false;
353 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
354 if (global_found)
355 {
356 /* Return true to indicate that we found the file, even though
357 we aren't going to do anything with it. */
358 return true;
359 }
360
41392f03 361 /* Tell the ELF backend that we don't want the output file to have a
04925e1e
AM
362 DT_NEEDED entry for this file. */
363 bfd_elf_set_dt_needed_name (abfd, "");
364
c2a1a436
DB
365 /* Previos basename call was clobbered in lang_for_each_input_file. */
366 soname = basename (abfd->filename);
367
04925e1e
AM
368 /* Tell the ELF backend that the output file needs a DT_NEEDED
369 entry for this file if it is used to resolve the reference in
370 a regular object. */
ec4eb78a 371 bfd_elf_set_dt_needed_soname (abfd, soname);
04925e1e
AM
372
373 /* Add this file into the symbol table. */
374 if (! bfd_link_add_symbols (abfd, &link_info))
375 einfo ("%F%B: could not read symbols: %E\n", abfd);
376
377 return true;
378}
379
380
381/* Search for a needed file in a path. */
382
383static boolean
384gld${EMULATION_NAME}_search_needed (path, name, force)
385 const char *path;
386 const char *name;
387 int force;
388{
389 const char *s;
390 size_t len;
391
cc5ce052
UD
392 if (name[0] == '/')
393 return gld${EMULATION_NAME}_try_needed (name, force);
394
04925e1e
AM
395 if (path == NULL || *path == '\0')
396 return false;
397 len = strlen (name);
398 while (1)
399 {
400 char *filename, *sset;
401
402 s = strchr (path, ':');
403 if (s == NULL)
404 s = path + strlen (path);
405
406 filename = (char *) xmalloc (s - path + len + 2);
407 if (s == path)
408 sset = filename;
409 else
410 {
411 memcpy (filename, path, s - path);
412 filename[s - path] = '/';
413 sset = filename + (s - path) + 1;
414 }
415 strcpy (sset, name);
416
417 if (gld${EMULATION_NAME}_try_needed (filename, force))
418 return true;
419
420 free (filename);
421
422 if (*s == '\0')
423 break;
424 path = s + 1;
425 }
426
427 return false;
428}
429
430EOF
431if [ "x${host}" = "x${target}" ] ; then
432 case " ${EMULATION_LIBPATH} " in
433 *" ${EMULATION_NAME} "*)
482e8b32
DB
434 case ${target} in
435 *-*-linux-gnu*)
436 cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
437
438/* For a native linker, check the file /etc/ld.so.conf for directories
439 in which we may find shared libraries. /etc/ld.so.conf is really
482e8b32 440 only meaningful on Linux. */
04925e1e
AM
441
442static boolean gld${EMULATION_NAME}_check_ld_so_conf
443 PARAMS ((const char *, int));
444
445static boolean
446gld${EMULATION_NAME}_check_ld_so_conf (name, force)
447 const char *name;
448 int force;
449{
450 static boolean initialized;
451 static char *ld_so_conf;
452
453 if (! initialized)
454 {
455 FILE *f;
456
457 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
458 if (f != NULL)
459 {
460 char *b;
461 size_t len, alloc;
462 int c;
463
464 len = 0;
465 alloc = 100;
466 b = (char *) xmalloc (alloc);
467
468 while ((c = getc (f)) != EOF)
469 {
470 if (len + 1 >= alloc)
471 {
472 alloc *= 2;
473 b = (char *) xrealloc (b, alloc);
474 }
475 if (c != ':'
476 && c != ' '
477 && c != '\t'
478 && c != '\n'
479 && c != ',')
480 {
481 b[len] = c;
482 ++len;
483 }
484 else
485 {
486 if (len > 0 && b[len - 1] != ':')
487 {
488 b[len] = ':';
489 ++len;
490 }
491 }
492 }
493
494 if (len > 0 && b[len - 1] == ':')
495 --len;
252b5132 496
04925e1e
AM
497 if (len > 0)
498 b[len] = '\0';
499 else
500 {
501 free (b);
502 b = NULL;
503 }
252b5132 504
04925e1e 505 fclose (f);
252b5132 506
04925e1e
AM
507 ld_so_conf = b;
508 }
49e56c49 509
04925e1e
AM
510 initialized = true;
511 }
49e56c49 512
04925e1e
AM
513 if (ld_so_conf == NULL)
514 return false;
252b5132 515
04925e1e 516 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
252b5132
RH
517}
518
04925e1e 519EOF
482e8b32
DB
520 # Linux
521 ;;
522 esac
04925e1e
AM
523 esac
524fi
525cat >>e${EMULATION_NAME}.c <<EOF
526
252b5132
RH
527/* See if an input file matches a DT_NEEDED entry by name. */
528
529static void
530gld${EMULATION_NAME}_check_needed (s)
531 lang_input_statement_type *s;
532{
533 if (global_found)
534 return;
535
577a0623 536 if (s->filename != NULL)
252b5132 537 {
577a0623
AM
538 const char *f;
539
540 if (strcmp (s->filename, global_needed->name) == 0)
541 {
542 global_found = true;
543 return;
544 }
545
546 if (s->search_dirs_flag)
547 {
548 f = strrchr (s->filename, '/');
549 if (f != NULL
550 && strcmp (f + 1, global_needed->name) == 0)
551 {
552 global_found = true;
553 return;
554 }
555 }
252b5132
RH
556 }
557
558 if (s->the_bfd != NULL)
559 {
560 const char *soname;
561
562 soname = bfd_elf_get_dt_soname (s->the_bfd);
563 if (soname != NULL
564 && strcmp (soname, global_needed->name) == 0)
565 {
566 global_found = true;
567 return;
568 }
569 }
252b5132
RH
570}
571
41392f03
AM
572EOF
573
574if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
575cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
576
577/* This is called after all the input files have been opened. */
252b5132
RH
578
579static void
04925e1e 580gld${EMULATION_NAME}_after_open ()
252b5132 581{
04925e1e 582 struct bfd_link_needed_list *needed, *l;
252b5132 583
04925e1e
AM
584 /* We only need to worry about this when doing a final link. */
585 if (link_info.relocateable || link_info.shared)
252b5132
RH
586 return;
587
04925e1e
AM
588 /* Get the list of files which appear in DT_NEEDED entries in
589 dynamic objects included in the link (often there will be none).
590 For each such file, we want to track down the corresponding
591 library, and include the symbol table in the link. This is what
592 the runtime dynamic linker will do. Tracking the files down here
593 permits one dynamic object to include another without requiring
594 special action by the person doing the link. Note that the
595 needed list can actually grow while we are stepping through this
596 loop. */
597 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
598 for (l = needed; l != NULL; l = l->next)
252b5132 599 {
04925e1e
AM
600 struct bfd_link_needed_list *ll;
601 int force;
252b5132 602
04925e1e
AM
603 /* If we've already seen this file, skip it. */
604 for (ll = needed; ll != l; ll = ll->next)
605 if (strcmp (ll->name, l->name) == 0)
606 break;
607 if (ll != l)
608 continue;
252b5132 609
04925e1e
AM
610 /* See if this file was included in the link explicitly. */
611 global_needed = l;
612 global_found = false;
613 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
614 if (global_found)
615 continue;
252b5132 616
ec4eb78a
L
617 if (trace_file_tries)
618 info_msg (_("%s needed by %B\n"), l->name, l->by);
619
04925e1e
AM
620 /* We need to find this file and include the symbol table. We
621 want to search for the file in the same way that the dynamic
622 linker will search. That means that we want to use
623 rpath_link, rpath, then the environment variable
ec4eb78a
L
624 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
625 entries (native only), then the linker script LIB_SEARCH_DIRS.
626 We do not search using the -L arguments.
252b5132 627
04925e1e
AM
628 We search twice. The first time, we skip objects which may
629 introduce version mismatches. The second time, we force
630 their use. See gld${EMULATION_NAME}_vercheck comment. */
631 for (force = 0; force < 2; force++)
632 {
04925e1e
AM
633 size_t len;
634 search_dirs_type *search;
6dd8c765
L
635EOF
636if [ "x${host}" = "x${target}" ] ; then
637 case " ${EMULATION_LIBPATH} " in
638 *" ${EMULATION_NAME} "*)
639cat >>e${EMULATION_NAME}.c <<EOF
dcb0bd0e 640 const char *lib_path;
6dd8c765
L
641 struct bfd_link_needed_list *rp;
642 int found;
643EOF
644 ;;
645 esac
646fi
647cat >>e${EMULATION_NAME}.c <<EOF
252b5132 648
04925e1e
AM
649 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
650 l->name, force))
651 break;
dcb0bd0e
L
652EOF
653if [ "x${host}" = "x${target}" ] ; then
654 case " ${EMULATION_LIBPATH} " in
655 *" ${EMULATION_NAME} "*)
656cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
657 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
658 l->name, force))
659 break;
660 if (command_line.rpath_link == NULL
661 && command_line.rpath == NULL)
662 {
663 lib_path = (const char *) getenv ("LD_RUN_PATH");
664 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
665 force))
666 break;
667 }
04925e1e
AM
668 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
669 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
670 break;
ec4eb78a
L
671
672 found = 0;
6dd8c765
L
673 rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
674 for (; !found && rp != NULL; rp = rp->next)
ec4eb78a
L
675 {
676 found = (rp->by == l->by
677 && gld${EMULATION_NAME}_search_needed (rp->name,
678 l->name,
679 force));
680 }
681 if (found)
682 break;
683
04925e1e
AM
684EOF
685 ;;
686 esac
687fi
688cat >>e${EMULATION_NAME}.c <<EOF
689 len = strlen (l->name);
690 for (search = search_head; search != NULL; search = search->next)
691 {
692 char *filename;
252b5132 693
04925e1e
AM
694 if (search->cmdline)
695 continue;
696 filename = (char *) xmalloc (strlen (search->name) + len + 2);
697 sprintf (filename, "%s/%s", search->name, l->name);
698 if (gld${EMULATION_NAME}_try_needed (filename, force))
699 break;
700 free (filename);
701 }
702 if (search != NULL)
703 break;
704EOF
705if [ "x${host}" = "x${target}" ] ; then
706 case " ${EMULATION_LIBPATH} " in
707 *" ${EMULATION_NAME} "*)
482e8b32
DB
708 case ${target} in
709 *-*-linux-gnu*)
710 cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
711 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
712 break;
713EOF
482e8b32
DB
714 # Linux
715 ;;
716 esac
04925e1e
AM
717 ;;
718 esac
719fi
720cat >>e${EMULATION_NAME}.c <<EOF
721 }
252b5132 722
04925e1e
AM
723 if (force < 2)
724 continue;
252b5132 725
e374f1d9 726 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
04925e1e
AM
727 l->name, l->by);
728 }
729}
252b5132 730
41392f03
AM
731EOF
732fi
733
734cat >>e${EMULATION_NAME}.c <<EOF
252b5132 735
04925e1e 736/* Look through an expression for an assignment statement. */
252b5132 737
04925e1e
AM
738static void
739gld${EMULATION_NAME}_find_exp_assignment (exp)
740 etree_type *exp;
741{
742 struct bfd_link_hash_entry *h;
743
744 switch (exp->type.node_class)
252b5132 745 {
04925e1e
AM
746 case etree_provide:
747 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
748 false, false, false);
749 if (h == NULL)
750 break;
252b5132 751
04925e1e
AM
752 /* We call record_link_assignment even if the symbol is defined.
753 This is because if it is defined by a dynamic object, we
754 actually want to use the value defined by the linker script,
755 not the value from the dynamic object (because we are setting
756 symbols like etext). If the symbol is defined by a regular
757 object, then, as it happens, calling record_link_assignment
758 will do no harm. */
759
760 /* Fall through. */
761 case etree_assign:
762 if (strcmp (exp->assign.dst, ".") != 0)
252b5132 763 {
04925e1e
AM
764 if (! (bfd_elf${ELFSIZE}_record_link_assignment
765 (output_bfd, &link_info, exp->assign.dst,
766 exp->type.node_class == etree_provide ? true : false)))
767 einfo ("%P%F: failed to record assignment to %s: %E\n",
768 exp->assign.dst);
252b5132 769 }
04925e1e
AM
770 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
771 break;
252b5132 772
04925e1e
AM
773 case etree_binary:
774 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
775 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
776 break;
252b5132 777
04925e1e
AM
778 case etree_trinary:
779 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
780 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
781 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
782 break;
252b5132 783
04925e1e
AM
784 case etree_unary:
785 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
786 break;
252b5132 787
04925e1e
AM
788 default:
789 break;
252b5132
RH
790 }
791}
792
04925e1e
AM
793
794/* This is called by the before_allocation routine via
795 lang_for_each_statement. It locates any assignment statements, and
796 tells the ELF backend about them, in case they are assignments to
797 symbols which are referred to by dynamic objects. */
798
799static void
800gld${EMULATION_NAME}_find_statement_assignment (s)
801 lang_statement_union_type *s;
802{
803 if (s->header.type == lang_assignment_statement_enum)
804 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
805}
806
41392f03
AM
807EOF
808
809if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
57e6abd2
AO
810 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
811 ELF_INTERPRETER_SET_DEFAULT="
812 if (sinterp != NULL)
813 {
814 sinterp->contents = ${ELF_INTERPRETER_NAME};
815 sinterp->_raw_size = strlen (sinterp->contents) + 1;
816 }
817
818"
819 else
820 ELF_INTERPRETER_SET_DEFAULT=
821 fi
41392f03 822cat >>e${EMULATION_NAME}.c <<EOF
04925e1e 823
252b5132
RH
824/* This is called after the sections have been attached to output
825 sections, but before any sizes or addresses have been set. */
826
827static void
828gld${EMULATION_NAME}_before_allocation ()
829{
830 const char *rpath;
831 asection *sinterp;
832
833 /* If we are going to make any variable assignments, we need to let
834 the ELF backend know about them in case the variables are
835 referred to by dynamic objects. */
836 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
837
838 /* Let the ELF backend work out the sizes of any sections required
839 by dynamic linking. */
840 rpath = command_line.rpath;
841 if (rpath == NULL)
842 rpath = (const char *) getenv ("LD_RUN_PATH");
843 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
844 (output_bfd, command_line.soname, rpath,
605d9b3a 845 command_line.filter_shlib,
252b5132
RH
846 (const char * const *) command_line.auxiliary_filters,
847 &link_info, &sinterp, lang_elf_version_info)))
848 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
57e6abd2 849${ELF_INTERPRETER_SET_DEFAULT}
252b5132
RH
850 /* Let the user override the dynamic linker we are using. */
851 if (command_line.interpreter != NULL
852 && sinterp != NULL)
853 {
854 sinterp->contents = (bfd_byte *) command_line.interpreter;
855 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
856 }
857
858 /* Look for any sections named .gnu.warning. As a GNU extensions,
859 we treat such sections as containing warning messages. We print
860 out the warning message, and then zero out the section size so
861 that it does not get copied into the output file. */
862
863 {
864 LANG_FOR_EACH_INPUT_STATEMENT (is)
865 {
866 asection *s;
867 bfd_size_type sz;
868 char *msg;
869 boolean ret;
870
871 if (is->just_syms_flag)
872 continue;
873
874 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
875 if (s == NULL)
876 continue;
877
878 sz = bfd_section_size (is->the_bfd, s);
879 msg = xmalloc ((size_t) sz + 1);
880 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
881 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
882 is->the_bfd);
883 msg[sz] = '\0';
884 ret = link_info.callbacks->warning (&link_info, msg,
885 (const char *) NULL,
886 is->the_bfd, (asection *) NULL,
887 (bfd_vma) 0);
888 ASSERT (ret);
889 free (msg);
890
891 /* Clobber the section size, so that we don't waste copying the
892 warning into the output file. */
893 s->_raw_size = 0;
894 }
895 }
896}
897
41392f03
AM
898EOF
899fi
900
901if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
902cat >>e${EMULATION_NAME}.c <<EOF
252b5132 903
04925e1e
AM
904/* Try to open a dynamic archive. This is where we know that ELF
905 dynamic libraries have an extension of .so (or .sl on oddball systems
906 like hpux). */
907
908static boolean
909gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
910 const char *arch;
911 search_dirs_type *search;
912 lang_input_statement_type *entry;
252b5132 913{
04925e1e
AM
914 const char *filename;
915 char *string;
252b5132 916
04925e1e
AM
917 if (! entry->is_archive)
918 return false;
252b5132 919
04925e1e 920 filename = entry->filename;
252b5132 921
04925e1e
AM
922 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
923 is defined, but it does not seem worth the headache to optimize
924 away those two bytes of space. */
925 string = (char *) xmalloc (strlen (search->name)
926 + strlen (filename)
927 + strlen (arch)
928#ifdef EXTRA_SHLIB_EXTENSION
929 + strlen (EXTRA_SHLIB_EXTENSION)
930#endif
931 + sizeof "/lib.so");
932
933 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
934
935#ifdef EXTRA_SHLIB_EXTENSION
936 /* Try the .so extension first. If that fails build a new filename
937 using EXTRA_SHLIB_EXTENSION. */
938 if (! ldfile_try_open_bfd (string, entry))
939 sprintf (string, "%s/lib%s%s%s", search->name,
940 filename, arch, EXTRA_SHLIB_EXTENSION);
941#endif
942
943 if (! ldfile_try_open_bfd (string, entry))
252b5132 944 {
04925e1e
AM
945 free (string);
946 return false;
947 }
252b5132 948
04925e1e 949 entry->filename = string;
252b5132 950
04925e1e
AM
951 /* We have found a dynamic object to include in the link. The ELF
952 backend linker will create a DT_NEEDED entry in the .dynamic
953 section naming this file. If this file includes a DT_SONAME
954 entry, it will be used. Otherwise, the ELF linker will just use
955 the name of the file. For an archive found by searching, like
956 this one, the DT_NEEDED entry should consist of just the name of
957 the file, without the path information used to find it. Note
958 that we only need to do this if we have a dynamic object; an
959 archive will never be referenced by a DT_NEEDED entry.
252b5132 960
04925e1e
AM
961 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
962 very pretty. I haven't been able to think of anything that is
963 pretty, though. */
964 if (bfd_check_format (entry->the_bfd, bfd_object)
965 && (entry->the_bfd->flags & DYNAMIC) != 0)
966 {
04925e1e 967 ASSERT (entry->is_archive && entry->search_dirs_flag);
252b5132 968
04925e1e 969 /* Rather than duplicating the logic above. Just use the
1c9acd94 970 filename we recorded earlier. */
04925e1e 971
1c9acd94
L
972 filename = xstrdup (basename (entry->filename));
973 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
04925e1e
AM
974 }
975
976 return true;
977}
978
41392f03
AM
979EOF
980fi
981cat >>e${EMULATION_NAME}.c <<EOF
982
983/* A variant of lang_output_section_find. Used by place_orphan. */
04925e1e 984
04925e1e
AM
985static lang_output_section_statement_type *
986output_rel_find ()
987{
988 lang_statement_union_type *u;
989 lang_output_section_statement_type *lookup;
990
991 for (u = lang_output_section_statement.head;
992 u != (lang_statement_union_type *) NULL;
993 u = lookup->next)
994 {
995 lookup = &u->output_section_statement;
996 if (strncmp (".rel", lookup->name, 4) == 0
997 && lookup->bfd_section != NULL
998 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
999 {
1000 return lookup;
1001 }
252b5132 1002 }
04925e1e 1003 return (lang_output_section_statement_type *) NULL;
252b5132
RH
1004}
1005
41392f03
AM
1006EOF
1007
1008if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1009cat >>e${EMULATION_NAME}.c <<EOF
04925e1e 1010
95500e1c
JJ
1011/* Find the last output section before given output statement.
1012 Used by place_orphan. */
1013
1014static asection *
1015output_prev_sec_find (os)
1016 lang_output_section_statement_type *os;
1017{
1018 asection *s = (asection *) NULL;
1019 lang_statement_union_type *u;
1020 lang_output_section_statement_type *lookup;
1021
1022 for (u = lang_output_section_statement.head;
1023 u != (lang_statement_union_type *) NULL;
1024 u = lookup->next)
1025 {
1026 lookup = &u->output_section_statement;
1027 if (lookup == os)
1028 break;
5030876b
L
1029 if (lookup->bfd_section != NULL
1030 && lookup->bfd_section != bfd_abs_section_ptr
1031 && lookup->bfd_section != bfd_com_section_ptr
1032 && lookup->bfd_section != bfd_und_section_ptr)
95500e1c
JJ
1033 s = lookup->bfd_section;
1034 }
1035
1036 if (u == NULL)
1037 return NULL;
1038
1039 return s;
1040}
1041
1042
252b5132
RH
1043/* Place an orphan section. We use this to put random SHF_ALLOC
1044 sections in the right segment. */
1045
41392f03 1046struct orphan_save {
6a345e87 1047 lang_output_section_statement_type *os;
5ba47421 1048 asection **section;
6a345e87
AM
1049 lang_statement_union_type **stmt;
1050};
252b5132 1051
252b5132
RH
1052static boolean
1053gld${EMULATION_NAME}_place_orphan (file, s)
1054 lang_input_statement_type *file;
1055 asection *s;
1056{
aea4bd9d
AM
1057 static struct orphan_save hold_text;
1058 static struct orphan_save hold_rodata;
1059 static struct orphan_save hold_data;
1060 static struct orphan_save hold_bss;
1061 static struct orphan_save hold_rel;
1062 static struct orphan_save hold_interp;
c940e6fc 1063 static struct orphan_save hold_sdata;
f9989030 1064 static int count = 1;
6a345e87 1065 struct orphan_save *place;
71bfc0ae 1066 lang_statement_list_type *old;
252b5132
RH
1067 lang_statement_list_type add;
1068 etree_type *address;
a854a4a7 1069 const char *secname;
252b5132 1070 const char *outsecname;
a854a4a7 1071 const char *ps = NULL;
252b5132
RH
1072 lang_output_section_statement_type *os;
1073
aea4bd9d
AM
1074 secname = bfd_get_section_name (s->owner, s);
1075
577a0623 1076 if (! config.unique_orphan_sections && ! unique_section_p (secname))
252b5132 1077 {
6d02a667 1078 /* Look through the script to see where to place this section. */
a854a4a7
AM
1079 os = lang_output_section_find (secname);
1080
1081 if (os != NULL
1082 && os->bfd_section != NULL
1083 && ((s->flags ^ os->bfd_section->flags)
1084 & (SEC_LOAD | SEC_ALLOC)) == 0)
1085 {
1086 /* We have already placed a section with this name. */
1087 wild_doit (&os->children, s, os, file);
1088 return true;
1089 }
252b5132
RH
1090 }
1091
aea4bd9d
AM
1092 if (hold_text.os == NULL)
1093 hold_text.os = lang_output_section_find (".text");
252b5132
RH
1094
1095 /* If this is a final link, then always put .gnu.warning.SYMBOL
1096 sections into the .text section to get them out of the way. */
1097 if (! link_info.shared
1098 && ! link_info.relocateable
1099 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
6a345e87 1100 && hold_text.os != NULL)
252b5132 1101 {
6a345e87 1102 wild_doit (&hold_text.os->children, s, hold_text.os, file);
252b5132
RH
1103 return true;
1104 }
1105
1106 /* Decide which segment the section should go in based on the
1107 section name and section flags. We put loadable .note sections
1108 right after the .interp section, so that the PT_NOTE segment is
1109 stored right after the program headers where the OS can read it
1110 in the first page. */
aea4bd9d
AM
1111#define HAVE_SECTION(hold, name) \
1112(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1113
252b5132
RH
1114 if (s->flags & SEC_EXCLUDE)
1115 return false;
71bfc0ae
AM
1116
1117 place = NULL;
1118 if ((s->flags & SEC_ALLOC) == 0)
1119 ;
252b5132 1120 else if ((s->flags & SEC_LOAD) != 0
577a0623 1121 && strncmp (secname, ".note", 5) == 0
aea4bd9d 1122 && HAVE_SECTION (hold_interp, ".interp"))
6a345e87 1123 place = &hold_interp;
252b5132 1124 else if ((s->flags & SEC_HAS_CONTENTS) == 0
aea4bd9d 1125 && HAVE_SECTION (hold_bss, ".bss"))
6a345e87 1126 place = &hold_bss;
c940e6fc
JW
1127 else if ((s->flags & SEC_SMALL_DATA) != 0
1128 && HAVE_SECTION (hold_sdata, ".sdata"))
1129 place = &hold_sdata;
252b5132 1130 else if ((s->flags & SEC_READONLY) == 0
aea4bd9d 1131 && HAVE_SECTION (hold_data, ".data"))
6a345e87 1132 place = &hold_data;
252b5132 1133 else if (strncmp (secname, ".rel", 4) == 0
aea4bd9d
AM
1134 && (hold_rel.os != NULL
1135 || (hold_rel.os = output_rel_find ()) != NULL))
6a345e87 1136 place = &hold_rel;
71bfc0ae 1137 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == SEC_READONLY
aea4bd9d 1138 && HAVE_SECTION (hold_rodata, ".rodata"))
6a345e87 1139 place = &hold_rodata;
71bfc0ae 1140 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == (SEC_CODE | SEC_READONLY)
6a345e87
AM
1141 && hold_text.os != NULL)
1142 place = &hold_text;
252b5132 1143
aea4bd9d
AM
1144#undef HAVE_SECTION
1145
252b5132
RH
1146 /* Choose a unique name for the section. This will be needed if the
1147 same section name appears in the input file with different
f813923c 1148 loadable or allocatable characteristics. */
252b5132
RH
1149 outsecname = secname;
1150 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
b3ea3584
AM
1151 {
1152 outsecname = bfd_get_unique_section_name (output_bfd,
1153 outsecname,
1154 &count);
1155 if (outsecname == NULL)
1156 einfo ("%F%P: place_orphan failed: %E\n");
1157 }
252b5132 1158
71bfc0ae
AM
1159 /* Start building a list of statements for this section.
1160 First save the current statement pointer. */
1161 old = stat_ptr;
1162
1163 /* If we have found an appropriate place for the output section
1164 statements for this orphan, add them to our own private list,
1165 inserting them later into the global statement list. */
5ba47421 1166 if (place != NULL)
252b5132 1167 {
5ba47421
AM
1168 stat_ptr = &add;
1169 lang_list_init (stat_ptr);
71bfc0ae 1170 }
5ba47421 1171
71bfc0ae
AM
1172 if (config.build_constructors)
1173 {
5ba47421
AM
1174 /* If the name of the section is representable in C, then create
1175 symbols to mark the start and the end of the section. */
1176 for (ps = outsecname; *ps != '\0'; ps++)
1177 if (! isalnum ((unsigned char) *ps) && *ps != '_')
1178 break;
71bfc0ae 1179 if (*ps == '\0')
5ba47421
AM
1180 {
1181 char *symname;
1182 etree_type *e_align;
1183
1184 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1185 sprintf (symname, "__start_%s", outsecname);
1186 e_align = exp_unop (ALIGN_K,
1187 exp_intop ((bfd_vma) 1 << s->alignment_power));
1188 lang_add_assignment (exp_assop ('=', symname, e_align));
1189 }
252b5132
RH
1190 }
1191
01cc8ff8 1192 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
252b5132 1193 address = exp_intop ((bfd_vma) 0);
01cc8ff8
AM
1194 else
1195 address = NULL;
252b5132 1196
aea4bd9d
AM
1197 os = lang_enter_output_section_statement (outsecname, address, 0,
1198 (bfd_vma) 0,
1199 (etree_type *) NULL,
1200 (etree_type *) NULL,
1201 (etree_type *) NULL);
252b5132 1202
252b5132
RH
1203 wild_doit (&os->children, s, os, file);
1204
20d04697
L
1205 lang_leave_output_section_statement
1206 ((bfd_vma) 0, "*default*",
1207 (struct lang_output_section_phdr_list *) NULL, "*default*");
1208
71bfc0ae 1209 if (config.build_constructors && *ps == '\0')
252b5132 1210 {
71bfc0ae 1211 char *symname;
252b5132 1212
71bfc0ae
AM
1213 /* lang_leave_ouput_section_statement resets stat_ptr. Put
1214 stat_ptr back where we want it. */
1215 if (place != NULL)
1216 stat_ptr = &add;
252b5132 1217
71bfc0ae
AM
1218 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1219 sprintf (symname, "__stop_%s", outsecname);
1220 lang_add_assignment (exp_assop ('=', symname,
1221 exp_nameop (NAME, ".")));
1222 }
5ba47421 1223
71bfc0ae
AM
1224 /* Restore the global list pointer. */
1225 stat_ptr = old;
1226
1227 if (place != NULL)
1228 {
95500e1c 1229 asection *snew, **pps, *bfd_section;
5ba47421
AM
1230
1231 snew = os->bfd_section;
95500e1c
JJ
1232 bfd_section = place->os->bfd_section;
1233 if (place->section == NULL && bfd_section == NULL)
1234 bfd_section = output_prev_sec_find (place->os);
1235
f9989030 1236 if (place->section != NULL
95500e1c
JJ
1237 || (bfd_section != NULL
1238 && bfd_section != snew))
5ba47421 1239 {
71bfc0ae
AM
1240 /* Shuffle the section to make the output file look neater.
1241 This is really only cosmetic. */
5ba47421
AM
1242 if (place->section == NULL)
1243 {
1244#if 0
1245 /* Finding the end of the list is a little tricky. We
1246 make a wild stab at it by comparing section flags. */
95500e1c
JJ
1247 flagword first_flags = bfd_section->flags;
1248 for (pps = &bfd_section->next;
5ba47421
AM
1249 *pps != NULL && (*pps)->flags == first_flags;
1250 pps = &(*pps)->next)
1251 ;
1252 place->section = pps;
1253#else
1254 /* Put orphans after the first section on the list. */
95500e1c 1255 place->section = &bfd_section->next;
5ba47421
AM
1256#endif
1257 }
1258
1259 /* Unlink the section. */
1260 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1261 ;
1262 *pps = snew->next;
1263
1264 /* Now tack it on to the "place->os" section list. */
1265 snew->next = *place->section;
1266 *place->section = snew;
1267 }
1268 place->section = &snew->next; /* Save the end of this list. */
1269
ed15ac6a 1270 if (add.head != NULL)
01cc8ff8 1271 {
ed15ac6a
AM
1272 /* We try to put the output statements in some sort of
1273 reasonable order here, because they determine the final
1274 load addresses of the orphan sections. */
1275 if (place->stmt == NULL)
1276 {
1277 /* Put the new statement list right at the head. */
1278 *add.tail = place->os->header.next;
1279 place->os->header.next = add.head;
1280 }
1281 else
1282 {
1283 /* Put it after the last orphan statement we added. */
1284 *add.tail = *place->stmt;
1285 *place->stmt = add.head;
1286 }
71bfc0ae 1287
ed15ac6a
AM
1288 /* Fix the global list pointer if we happened to tack our
1289 new list at the tail. */
1290 if (*old->tail == add.head)
1291 old->tail = add.tail;
71bfc0ae 1292
ed15ac6a
AM
1293 /* Save the end of this list. */
1294 place->stmt = add.tail;
1295 }
6a345e87 1296 }
252b5132
RH
1297
1298 return true;
1299}
1300
41392f03
AM
1301EOF
1302fi
1303
1304if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1305cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
1306
1307static char *
41392f03 1308gld${EMULATION_NAME}_get_script (isfile)
252b5132
RH
1309 int *isfile;
1310EOF
1311
1312if test -n "$COMPILE_IN"
1313then
1314# Scripts compiled in.
1315
1316# sed commands to quote an ld script as a C string.
597e2591 1317sc="-f stringify.sed"
252b5132
RH
1318
1319cat >>e${EMULATION_NAME}.c <<EOF
60bcf0fa 1320{
252b5132
RH
1321 *isfile = 0;
1322
1323 if (link_info.relocateable == true && config.build_constructors == true)
597e2591 1324 return
252b5132 1325EOF
597e2591
ILT
1326sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1327echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1328sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1329echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1330sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1331echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1332sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
252b5132
RH
1333
1334if test -n "$GENERATE_SHLIB_SCRIPT" ; then
597e2591
ILT
1335echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1336sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
252b5132
RH
1337fi
1338
597e2591
ILT
1339echo ' ; else return' >> e${EMULATION_NAME}.c
1340sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1341echo '; }' >> e${EMULATION_NAME}.c
252b5132
RH
1342
1343else
1344# Scripts read from the filesystem.
1345
1346cat >>e${EMULATION_NAME}.c <<EOF
60bcf0fa 1347{
252b5132
RH
1348 *isfile = 1;
1349
1350 if (link_info.relocateable == true && config.build_constructors == true)
1351 return "ldscripts/${EMULATION_NAME}.xu";
1352 else if (link_info.relocateable == true)
1353 return "ldscripts/${EMULATION_NAME}.xr";
1354 else if (!config.text_read_only)
1355 return "ldscripts/${EMULATION_NAME}.xbn";
1356 else if (!config.magic_demand_paged)
1357 return "ldscripts/${EMULATION_NAME}.xn";
1358 else if (link_info.shared)
1359 return "ldscripts/${EMULATION_NAME}.xs";
1360 else
1361 return "ldscripts/${EMULATION_NAME}.x";
1362}
252b5132 1363
3b108066 1364EOF
41392f03
AM
1365fi
1366fi
3b108066 1367
41392f03 1368if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
3b108066 1369
4b209b22 1370if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
3b108066 1371
e0ee487b
L
1372if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1373cat >>e${EMULATION_NAME}.c <<EOF
1374 $PARSE_AND_LIST_PROLOGUE
1375EOF
1376fi
1377
e0ee487b
L
1378cat >>e${EMULATION_NAME}.c <<EOF
1379
1380#include "getopt.h"
1381
6c1439be
L
1382#define OPTION_DISABLE_NEW_DTAGS (400)
1383#define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
a1ab1d2a 1384#define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
6c1439be 1385
e0ee487b
L
1386static struct option longopts[] =
1387{
1388EOF
e0ee487b
L
1389
1390if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1391cat >>e${EMULATION_NAME}.c <<EOF
699845ef
L
1392 /* getopt allows abbreviations, so we do this to stop it from
1393 treating -d/-e as abbreviations for these options. */
6c1439be 1394 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
699845ef
L
1395 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1396 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
6c1439be 1397 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
a1ab1d2a
UD
1398 {"Bgroup", no_argument, NULL, OPTION_GROUP},
1399 {"Bgroup", no_argument, NULL, OPTION_GROUP},
e0ee487b
L
1400EOF
1401fi
1402
1403if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1404cat >>e${EMULATION_NAME}.c <<EOF
1405 $PARSE_AND_LIST_LONGOPTS
1406EOF
1407fi
1408
e0ee487b
L
1409cat >>e${EMULATION_NAME}.c <<EOF
1410 {NULL, no_argument, NULL, 0}
1411};
1412
04925e1e 1413
4b209b22 1414static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
41392f03 1415
e0ee487b 1416static int
4b209b22 1417gld${EMULATION_NAME}_parse_args (argc, argv)
e0ee487b
L
1418 int argc;
1419 char ** argv;
1420{
4b209b22
AM
1421 int longind;
1422 int optc;
1423 static int prevoptind = -1;
e0ee487b
L
1424 int prevopterr = opterr;
1425 int wanterror;
e0ee487b 1426
4b209b22 1427 if (prevoptind != optind)
e0ee487b
L
1428 opterr = 0;
1429
4b209b22
AM
1430 wanterror = opterr;
1431 prevoptind = optind;
1432
41392f03
AM
1433 optc = getopt_long_only (argc, argv,
1434 "-${PARSE_AND_LIST_SHORTOPTS}z:", longopts,
1435 &longind);
e0ee487b
L
1436 opterr = prevopterr;
1437
1438 switch (optc)
1439 {
1440 default:
1441 if (wanterror)
1442 xexit (1);
4b209b22 1443 optind = prevoptind;
e0ee487b 1444 return 0;
6c1439be 1445
e0ee487b 1446EOF
e0ee487b
L
1447
1448if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1449cat >>e${EMULATION_NAME}.c <<EOF
6c1439be
L
1450 case OPTION_DISABLE_NEW_DTAGS:
1451 link_info.new_dtags = false;
1452 break;
1453
1454 case OPTION_ENABLE_NEW_DTAGS:
1455 link_info.new_dtags = true;
1456 break;
1457
a1ab1d2a
UD
1458 case OPTION_GROUP:
1459 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
1460 /* Groups must be self-contained. */
1461 link_info.no_undefined = true;
1462 break;
1463
e0ee487b
L
1464 case 'z':
1465 if (strcmp (optarg, "initfirst") == 0)
1466 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1467 else if (strcmp (optarg, "interpose") == 0)
1468 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1469 else if (strcmp (optarg, "loadfltr") == 0)
1470 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1471 else if (strcmp (optarg, "nodefaultlib") == 0)
1472 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1473 else if (strcmp (optarg, "nodelete") == 0)
1474 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1475 else if (strcmp (optarg, "nodlopen") == 0)
1476 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1477 else if (strcmp (optarg, "nodump") == 0)
1478 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1479 else if (strcmp (optarg, "now") == 0)
1480 {
1481 link_info.flags |= (bfd_vma) DF_BIND_NOW;
1482 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1483 }
1484 else if (strcmp (optarg, "origin") == 0)
1485 {
1486 link_info.flags |= (bfd_vma) DF_ORIGIN;
1487 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1488 }
a1ab1d2a
UD
1489 else if (strcmp (optarg, "defs") == 0)
1490 link_info.no_undefined = true;
e0ee487b 1491 /* What about the other Solaris -z options? FIXME. */
6c1439be 1492 break;
e0ee487b
L
1493EOF
1494fi
1495
1496if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1497cat >>e${EMULATION_NAME}.c <<EOF
1498 $PARSE_AND_LIST_ARGS_CASES
1499EOF
1500fi
1501
e0ee487b
L
1502cat >>e${EMULATION_NAME}.c <<EOF
1503 }
1504
1505 return 1;
1506}
1507
41392f03
AM
1508EOF
1509fi
1510
4b209b22 1511if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
41392f03
AM
1512cat >>e${EMULATION_NAME}.c <<EOF
1513
4b209b22 1514static void gld${EMULATION_NAME}_list_options PARAMS ((FILE * file));
04925e1e 1515
e0ee487b 1516static void
4b209b22 1517gld${EMULATION_NAME}_list_options (file)
e0ee487b
L
1518 FILE * file;
1519{
1520EOF
e0ee487b
L
1521
1522if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1523cat >>e${EMULATION_NAME}.c <<EOF
a1ab1d2a 1524 fprintf (file, _(" -Bgroup\t\tSelects group name lookup rules for DSO\n"));
6c1439be
L
1525 fprintf (file, _(" --disable-new-dtags\tDisable new dynamic tags\n"));
1526 fprintf (file, _(" --enable-new-dtags\tEnable new dynamic tags\n"));
a1ab1d2a 1527 fprintf (file, _(" -z defs\t\tDisallows undefined symbols\n"));
f813923c
HPN
1528 fprintf (file, _(" -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
1529 fprintf (file, _(" -z interpose\t\tMark object to interpose all DSOs but executable\n"));
e0ee487b
L
1530 fprintf (file, _(" -z loadfltr\t\tMark object requiring immediate process\n"));
1531 fprintf (file, _(" -z nodefaultlib\tMark object not to use default search paths\n"));
1532 fprintf (file, _(" -z nodelete\t\tMark DSO non-deletable at runtime\n"));
f813923c
HPN
1533 fprintf (file, _(" -z nodlopen\t\tMark DSO not available to dlopen\n"));
1534 fprintf (file, _(" -z nodump\t\tMark DSO not available to dldump\n"));
e0ee487b
L
1535 fprintf (file, _(" -z now\t\tMark object non-lazy runtime binding\n"));
1536 fprintf (file, _(" -z origin\t\tMark object requiring immediate \$ORIGIN processing\n"));
1537 fprintf (file, _("\t\t\t at runtime\n"));
19e3be22 1538 fprintf (file, _(" -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
e0ee487b
L
1539EOF
1540fi
1541
1542if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1543cat >>e${EMULATION_NAME}.c <<EOF
1544 $PARSE_AND_LIST_OPTIONS
1545EOF
1546fi
1547
e0ee487b
L
1548cat >>e${EMULATION_NAME}.c <<EOF
1549}
1550EOF
e0ee487b
L
1551
1552if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1553cat >>e${EMULATION_NAME}.c <<EOF
1554 $PARSE_AND_LIST_EPILOGUE
1555EOF
1556fi
41392f03
AM
1557fi
1558else
4b209b22 1559if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
41392f03 1560cat >>e${EMULATION_NAME}.c <<EOF
4b209b22 1561#define gld${EMULATION_NAME}_parse_args NULL
41392f03
AM
1562EOF
1563fi
4b209b22 1564if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
41392f03 1565cat >>e${EMULATION_NAME}.c <<EOF
4b209b22 1566#define gld${EMULATION_NAME}_list_options NULL
41392f03
AM
1567EOF
1568fi
1569fi
e0ee487b 1570
252b5132
RH
1571cat >>e${EMULATION_NAME}.c <<EOF
1572
60bcf0fa 1573struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
252b5132 1574{
41392f03
AM
1575 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
1576 ${LDEMUL_SYSLIB-syslib_default},
1577 ${LDEMUL_HLL-hll_default},
1578 ${LDEMUL_AFTER_PARSE-after_parse_default},
1579 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
1580 ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
1581 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
1582 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
1583 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
1584 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
252b5132
RH
1585 "${EMULATION_NAME}",
1586 "${OUTPUT_FORMAT}",
41392f03
AM
1587 ${LDEMUL_FINISH-NULL},
1588 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
1589 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
1590 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
1591 ${LDEMUL_SET_SYMBOLS-NULL},
4b209b22 1592 ${LDEMUL_PARSE_ARGS-gld${EMULATION_NAME}_parse_args},
41392f03 1593 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
4b209b22 1594 ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
41392f03
AM
1595 ${LDEMUL_RECOGNIZED_FILE-NULL},
1596 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
252b5132
RH
1597};
1598EOF
This page took 0.157724 seconds and 4 git commands to generate.