]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/elf32.em
* top.c (gdb_readline): Always accept "rn" as a line-ending.
[thirdparty/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}
e3dc8847 15 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
5e2f1575 16 2002, 2003, 2004 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"
3882b010 41#include "safe-ctype.h"
fcf65871 42#include "getopt.h"
252b5132
RH
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"
df2a7313 53#include <ldgram.h>
e0ee487b 54#include "elf/common.h"
252b5132 55
0c7a8e5a
AM
56/* Declare functions used by various EXTRA_EM_FILEs. */
57static void gld${EMULATION_NAME}_before_parse (void);
58static void gld${EMULATION_NAME}_after_open (void);
59static void gld${EMULATION_NAME}_before_allocation (void);
b34976b6 60static bfd_boolean gld${EMULATION_NAME}_place_orphan
0c7a8e5a
AM
61 (lang_input_statement_type *file, asection *s);
62static void gld${EMULATION_NAME}_finish (void);
252b5132 63
41392f03
AM
64EOF
65
d3989512
JJ
66if [ "x${USE_LIBPATH}" = xyes ] ; then
67 case ${target} in
68 *-*-linux-gnu*)
69 cat >>e${EMULATION_NAME}.c <<EOF
70#include <glob.h>
71EOF
72 ;;
73 esac
74fi
75
41392f03
AM
76# Import any needed special functions and/or overrides.
77#
78if test -n "$EXTRA_EM_FILE" ; then
79. ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
80fi
81
f813923c 82# Functions in this file can be overridden by setting the LDEMUL_* shell
41392f03
AM
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#
88if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
89cat >>e${EMULATION_NAME}.c <<EOF
90
252b5132 91static void
0c7a8e5a 92gld${EMULATION_NAME}_before_parse (void)
252b5132 93{
5e2f1575 94 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
b34976b6
AM
95 config.dynamic_link = ${DYNAMIC_LINK-TRUE};
96 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
252b5132
RH
97}
98
41392f03
AM
99EOF
100fi
101
4a43e768
AM
102if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
103cat >>e${EMULATION_NAME}.c <<EOF
104/* Handle as_needed DT_NEEDED. */
105
106static bfd_boolean
107gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
108{
e56f61be 109 int class = 0;
4a43e768
AM
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. */
e56f61be
L
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);
4a43e768
AM
128
129 /* Continue on with normal load_symbols processing. */
130 return FALSE;
131}
132EOF
133fi
134
41392f03 135cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
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
140static struct bfd_link_needed_list *global_needed;
141static struct stat global_stat;
b34976b6 142static bfd_boolean global_found;
252b5132 143static struct bfd_link_needed_list *global_vercheck_needed;
b34976b6 144static bfd_boolean global_vercheck_failed;
252b5132 145
252b5132 146
04925e1e
AM
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.
252b5132 152
04925e1e
AM
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.
252b5132 158
04925e1e
AM
159 This is called via lang_for_each_input_file.
160 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
f813923c 161 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
04925e1e 162 a conflicting version. */
252b5132 163
04925e1e 164static void
0c7a8e5a 165gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
04925e1e 166{
1c9acd94 167 const char *soname;
04925e1e 168 struct bfd_link_needed_list *l;
252b5132 169
04925e1e
AM
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;
252b5132 175
04925e1e
AM
176 soname = bfd_elf_get_dt_soname (s->the_bfd);
177 if (soname == NULL)
fed2999d 178 soname = lbasename (bfd_get_filename (s->the_bfd));
04925e1e
AM
179
180 for (l = global_vercheck_needed; l != NULL; l = l->next)
181 {
182 const char *suffix;
183
1c9acd94 184 if (strcmp (soname, l->name) == 0)
04925e1e
AM
185 {
186 /* Probably can't happen, but it's an easy check. */
187 continue;
252b5132
RH
188 }
189
04925e1e 190 if (strchr (l->name, '/') != NULL)
252b5132
RH
191 continue;
192
04925e1e
AM
193 suffix = strstr (l->name, ".so.");
194 if (suffix == NULL)
195 continue;
196
197 suffix += sizeof ".so." - 1;
198
1c9acd94 199 if (strncmp (soname, l->name, suffix - l->name) == 0)
04925e1e
AM
200 {
201 /* Here we know that S is a dynamic object FOO.SO.VER1, and
0c7a8e5a
AM
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. */
b34976b6 206 global_vercheck_failed = TRUE;
04925e1e
AM
207 return;
208 }
252b5132
RH
209 }
210}
211
252b5132 212
04925e1e
AM
213/* See if an input file matches a DT_NEEDED entry by running stat on
214 the file. */
215
216static void
0c7a8e5a 217gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
252b5132 218{
04925e1e
AM
219 struct stat st;
220 const char *suffix;
221 const char *soname;
252b5132 222
04925e1e
AM
223 if (global_found)
224 return;
225 if (s->the_bfd == NULL)
226 return;
227
228 if (bfd_stat (s->the_bfd, &st) != 0)
252b5132 229 {
04925e1e
AM
230 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
231 return;
232 }
252b5132 233
04925e1e
AM
234 if (st.st_dev == global_stat.st_dev
235 && st.st_ino == global_stat.st_ino)
236 {
b34976b6 237 global_found = TRUE;
04925e1e
AM
238 return;
239 }
252b5132 240
04925e1e
AM
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
f813923c 245 heuristic test, and it will only work if the name looks like
04925e1e
AM
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. */
252b5132 249
04925e1e
AM
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;
252b5132 256
04925e1e
AM
257 soname = bfd_elf_get_dt_soname (s->the_bfd);
258 if (soname == NULL)
fed2999d 259 soname = lbasename (s->filename);
252b5132 260
cde43e70 261 if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
04925e1e 262 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
1c9acd94 263 global_needed->name, global_needed->by, soname);
252b5132
RH
264}
265
e56f61be
L
266struct dt_needed
267{
268 bfd *by;
269 const char *name;
270};
04925e1e 271
252b5132
RH
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
b34976b6 276static bfd_boolean
e56f61be
L
277gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
278 int force)
252b5132
RH
279{
280 bfd *abfd;
e56f61be 281 const char *name = needed->name;
ec4eb78a 282 const char *soname;
e56f61be 283 int class;
252b5132
RH
284
285 abfd = bfd_openr (name, bfd_get_target (output_bfd));
286 if (abfd == NULL)
b34976b6 287 return FALSE;
252b5132
RH
288 if (! bfd_check_format (abfd, bfd_object))
289 {
cde43e70 290 bfd_close (abfd);
b34976b6 291 return FALSE;
252b5132
RH
292 }
293 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
294 {
cde43e70 295 bfd_close (abfd);
b34976b6 296 return FALSE;
252b5132
RH
297 }
298
c4bb8067
L
299 /* For DT_NEEDED, they have to match. */
300 if (abfd->xvec != output_bfd->xvec)
301 {
302 bfd_close (abfd);
b34976b6 303 return FALSE;
c4bb8067
L
304 }
305
252b5132
RH
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;
b34976b6 321 global_vercheck_failed = FALSE;
252b5132
RH
322 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
323 if (global_vercheck_failed)
324 {
cde43e70 325 bfd_close (abfd);
b34976b6 326 /* Return FALSE to force the caller to move on to try
0c7a8e5a 327 another file on the search path. */
b34976b6 328 return FALSE;
252b5132
RH
329 }
330
331 /* But wait! It gets much worse. On Linux, if a shared
0c7a8e5a
AM
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. */
252b5132
RH
337
338EOF
339case ${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 {
cde43e70 350 bfd_close (abfd);
b34976b6 351 return FALSE;
252b5132
RH
352 }
353 }
354
355EOF
356 ;;
357esac
358cat >>e${EMULATION_NAME}.c <<EOF
359 }
360 }
361
362 /* We've found a dynamic object matching the DT_NEEDED entry. */
363
04925e1e
AM
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);
ec4eb78a
L
374
375 /* First strip off everything before the last '/'. */
fed2999d 376 soname = lbasename (abfd->filename);
ec4eb78a
L
377
378 if (trace_file_tries)
379 info_msg (_("found %s at %s\n"), soname, name);
380
b34976b6 381 global_found = FALSE;
04925e1e
AM
382 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
383 if (global_found)
384 {
b34976b6 385 /* Return TRUE to indicate that we found the file, even though
0c7a8e5a 386 we aren't going to do anything with it. */
b34976b6 387 return TRUE;
04925e1e
AM
388 }
389
4a43e768
AM
390 /* Specify the soname to use. */
391 bfd_elf_set_dt_needed_name (abfd, soname);
04925e1e 392
4a43e768
AM
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. */
e56f61be
L
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
402 && (bfd_elf_get_dyn_lib_class (needed->by)
403 & DYN_NO_ADD_NEEDED) != 0)
404 class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
405
406 bfd_elf_set_dyn_lib_class (abfd, class);
04925e1e
AM
407
408 /* Add this file into the symbol table. */
409 if (! bfd_link_add_symbols (abfd, &link_info))
410 einfo ("%F%B: could not read symbols: %E\n", abfd);
411
b34976b6 412 return TRUE;
04925e1e
AM
413}
414
415
416/* Search for a needed file in a path. */
417
b34976b6 418static bfd_boolean
e56f61be
L
419gld${EMULATION_NAME}_search_needed (const char *path,
420 struct dt_needed *n, int force)
04925e1e
AM
421{
422 const char *s;
e56f61be 423 const char *name = n->name;
04925e1e 424 size_t len;
e56f61be 425 struct dt_needed needed;
04925e1e 426
cc5ce052 427 if (name[0] == '/')
e56f61be 428 return gld${EMULATION_NAME}_try_needed (n, force);
cc5ce052 429
04925e1e 430 if (path == NULL || *path == '\0')
b34976b6 431 return FALSE;
e56f61be
L
432
433 needed.by = n->by;
434 needed.name = n->name;
435
04925e1e
AM
436 len = strlen (name);
437 while (1)
438 {
439 char *filename, *sset;
440
441 s = strchr (path, ':');
442 if (s == NULL)
443 s = path + strlen (path);
444
445 filename = (char *) xmalloc (s - path + len + 2);
446 if (s == path)
447 sset = filename;
448 else
449 {
450 memcpy (filename, path, s - path);
451 filename[s - path] = '/';
452 sset = filename + (s - path) + 1;
453 }
454 strcpy (sset, name);
455
e56f61be
L
456 needed.name = filename;
457 if (gld${EMULATION_NAME}_try_needed (&needed, force))
b34976b6 458 return TRUE;
04925e1e
AM
459
460 free (filename);
461
462 if (*s == '\0')
463 break;
464 path = s + 1;
465 }
466
b34976b6 467 return FALSE;
04925e1e
AM
468}
469
470EOF
9c8ebd6a 471if [ "x${USE_LIBPATH}" = xyes ] ; then
3dc16cab 472 cat >>e${EMULATION_NAME}.c <<EOF
9c8ebd6a
DJ
473
474/* Add the sysroot to every entry in a colon-separated path. */
475
476static char *
0c7a8e5a 477gld${EMULATION_NAME}_add_sysroot (const char *path)
9c8ebd6a
DJ
478{
479 int len, colons, i;
480 char *ret, *p;
481
482 len = strlen (path);
483 colons = 0;
484 i = 0;
485 while (path[i])
486 if (path[i++] == ':')
487 colons++;
488
489 if (path[i])
490 colons++;
491
42644a89 492 len = len + (colons + 1) * strlen (ld_sysroot);
9c8ebd6a
DJ
493 ret = xmalloc (len + 1);
494 strcpy (ret, ld_sysroot);
495 p = ret + strlen (ret);
496 i = 0;
497 while (path[i])
498 if (path[i] == ':')
499 {
0c7a8e5a 500 *p++ = path[i++];
9c8ebd6a
DJ
501 strcpy (p, ld_sysroot);
502 p = p + strlen (p);
9c8ebd6a
DJ
503 }
504 else
505 *p++ = path[i++];
506
507 *p = 0;
508 return ret;
509}
04925e1e 510
3dc16cab
DJ
511EOF
512 case ${target} in
513 *-*-linux-gnu*)
514 cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
515/* For a native linker, check the file /etc/ld.so.conf for directories
516 in which we may find shared libraries. /etc/ld.so.conf is really
482e8b32 517 only meaningful on Linux. */
04925e1e 518
d3989512 519struct gld${EMULATION_NAME}_ld_so_conf
04925e1e 520{
d3989512
JJ
521 char *path;
522 size_t len, alloc;
523};
04925e1e 524
d3989512
JJ
525static void
526gld${EMULATION_NAME}_parse_ld_so_conf
527 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
528
529static void
530gld${EMULATION_NAME}_parse_ld_so_conf_include
531 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename,
532 const char *pattern)
533{
534 char *newp = NULL;
535 glob_t gl;
536
537 if (pattern[0] != '/')
04925e1e 538 {
d3989512
JJ
539 char *p = strrchr (filename, '/');
540 size_t patlen = strlen (pattern) + 1;
04925e1e 541
d3989512
JJ
542 newp = xmalloc (p - filename + 1 + patlen);
543 memcpy (newp, filename, p - filename + 1);
544 memcpy (newp + (p - filename + 1), pattern, patlen);
545 pattern = newp;
546 }
04925e1e 547
d3989512
JJ
548 if (glob (pattern, 0, NULL, &gl) == 0)
549 {
550 size_t i;
551
552 for (i = 0; i < gl.gl_pathc; ++i)
553 gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]);
554 globfree (&gl);
555 }
556
557 if (newp)
558 free (newp);
559}
560
561static void
562gld${EMULATION_NAME}_parse_ld_so_conf
563 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename)
564{
565 FILE *f = fopen (filename, FOPEN_RT);
563f4125
JJ
566 char *line;
567 size_t linelen;
d3989512
JJ
568
569 if (f == NULL)
570 return;
571
563f4125
JJ
572 linelen = 256;
573 line = xmalloc (linelen);
574 do
d3989512 575 {
563f4125
JJ
576 char *p = line, *q;
577
578 /* Normally this would use getline(3), but we need to be portable. */
579 while ((q = fgets (p, linelen - (p - line), f)) != NULL
580 && strlen (q) == linelen - (p - line) - 1
581 && line[linelen - 2] != '\n')
582 {
583 line = xrealloc (line, 2 * linelen);
584 p = line + linelen - 1;
585 linelen += linelen;
586 }
587
588 if (q == NULL && p == line)
589 break;
d3989512
JJ
590
591 p = strchr (line, '\n');
592 if (p)
593 *p = '\0';
594
595 /* Because the file format does not know any form of quoting we
596 can search forward for the next '#' character and if found
597 make it terminating the line. */
598 p = strchr (line, '#');
599 if (p)
600 *p = '\0';
601
602 /* Remove leading whitespace. NUL is no whitespace character. */
603 p = line;
604 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
605 ++p;
606
607 /* If the line is blank it is ignored. */
608 if (p[0] == '\0')
609 continue;
04925e1e 610
d3989512
JJ
611 if (!strncmp (p, "include", 7) && (p[7] == ' ' || p[7] == '\t'))
612 {
613 char *dir, c;
614 p += 8;
615 do
04925e1e 616 {
d3989512
JJ
617 while (*p == ' ' || *p == '\t')
618 ++p;
04925e1e 619
d3989512
JJ
620 if (*p == '\0')
621 break;
252b5132 622
d3989512
JJ
623 dir = p;
624
625 while (*p != ' ' && *p != '\t' && *p)
626 ++p;
627
628 c = *p;
629 *p++ = '\0';
630 if (dir[0] != '\0')
631 gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename,
632 dir);
633 }
634 while (c != '\0');
635 }
636 else
637 {
638 char *dir = p;
639 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
640 && *p != '\r' && *p != '\v')
641 ++p;
642
643 while (p != dir && p[-1] == '/')
644 --p;
645 if (info->path == NULL)
646 {
647 info->alloc = p - dir + 1 + 256;
648 info->path = xmalloc (info->alloc);
649 info->len = 0;
650 }
04925e1e
AM
651 else
652 {
d3989512
JJ
653 if (info->len + 1 + (p - dir) >= info->alloc)
654 {
655 info->alloc += p - dir + 256;
656 info->path = xrealloc (info->path, info->alloc);
657 }
658 info->path[info->len++] = ':';
04925e1e 659 }
d3989512
JJ
660 memcpy (info->path + info->len, dir, p - dir);
661 info->len += p - dir;
662 info->path[info->len] = '\0';
663 }
664 }
563f4125 665 while (! feof (f));
d3989512
JJ
666 free (line);
667 fclose (f);
668}
252b5132 669
d3989512
JJ
670static bfd_boolean
671gld${EMULATION_NAME}_check_ld_so_conf (const char *name, int force)
672{
673 static bfd_boolean initialized;
674 static char *ld_so_conf;
675 struct dt_needed needed;
252b5132 676
d3989512
JJ
677 if (! initialized)
678 {
679 char *tmppath;
680 struct gld${EMULATION_NAME}_ld_so_conf info;
9c8ebd6a 681
d3989512
JJ
682 tmppath = concat (ld_sysroot, "/etc/ld.so.conf", NULL);
683 info.path = NULL;
684 info.len = info.alloc = 0;
685 gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
686 free (tmppath);
687 if (info.path)
688 {
689 char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
690 free (info.path);
691 ld_so_conf = d;
04925e1e 692 }
b34976b6 693 initialized = TRUE;
04925e1e 694 }
49e56c49 695
04925e1e 696 if (ld_so_conf == NULL)
b34976b6 697 return FALSE;
252b5132 698
e56f61be
L
699
700 needed.by = NULL;
701 needed.name = name;
702 return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
252b5132
RH
703}
704
04925e1e 705EOF
9c8ebd6a
DJ
706 # Linux
707 ;;
04925e1e
AM
708 esac
709fi
710cat >>e${EMULATION_NAME}.c <<EOF
711
252b5132
RH
712/* See if an input file matches a DT_NEEDED entry by name. */
713
714static void
0c7a8e5a 715gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
252b5132
RH
716{
717 if (global_found)
718 return;
719
577a0623 720 if (s->filename != NULL)
252b5132 721 {
577a0623
AM
722 const char *f;
723
724 if (strcmp (s->filename, global_needed->name) == 0)
725 {
b34976b6 726 global_found = TRUE;
577a0623
AM
727 return;
728 }
729
730 if (s->search_dirs_flag)
731 {
732 f = strrchr (s->filename, '/');
733 if (f != NULL
734 && strcmp (f + 1, global_needed->name) == 0)
735 {
b34976b6 736 global_found = TRUE;
577a0623
AM
737 return;
738 }
739 }
252b5132
RH
740 }
741
742 if (s->the_bfd != NULL)
743 {
744 const char *soname;
745
746 soname = bfd_elf_get_dt_soname (s->the_bfd);
747 if (soname != NULL
748 && strcmp (soname, global_needed->name) == 0)
749 {
b34976b6 750 global_found = TRUE;
252b5132
RH
751 return;
752 }
753 }
252b5132
RH
754}
755
41392f03
AM
756EOF
757
758if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
759cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
760
761/* This is called after all the input files have been opened. */
252b5132
RH
762
763static void
0c7a8e5a 764gld${EMULATION_NAME}_after_open (void)
252b5132 765{
04925e1e 766 struct bfd_link_needed_list *needed, *l;
252b5132 767
04925e1e 768 /* We only need to worry about this when doing a final link. */
1049f94e 769 if (link_info.relocatable || !link_info.executable)
252b5132
RH
770 return;
771
04925e1e
AM
772 /* Get the list of files which appear in DT_NEEDED entries in
773 dynamic objects included in the link (often there will be none).
774 For each such file, we want to track down the corresponding
775 library, and include the symbol table in the link. This is what
776 the runtime dynamic linker will do. Tracking the files down here
777 permits one dynamic object to include another without requiring
778 special action by the person doing the link. Note that the
779 needed list can actually grow while we are stepping through this
780 loop. */
781 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
782 for (l = needed; l != NULL; l = l->next)
252b5132 783 {
04925e1e 784 struct bfd_link_needed_list *ll;
e56f61be 785 struct dt_needed n, nn;
04925e1e 786 int force;
252b5132 787
04925e1e
AM
788 /* If we've already seen this file, skip it. */
789 for (ll = needed; ll != l; ll = ll->next)
790 if (strcmp (ll->name, l->name) == 0)
791 break;
792 if (ll != l)
793 continue;
252b5132 794
04925e1e
AM
795 /* See if this file was included in the link explicitly. */
796 global_needed = l;
b34976b6 797 global_found = FALSE;
04925e1e
AM
798 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
799 if (global_found)
800 continue;
252b5132 801
e56f61be
L
802 n.by = l->by;
803 n.name = l->name;
804 nn.by = l->by;
ec4eb78a
L
805 if (trace_file_tries)
806 info_msg (_("%s needed by %B\n"), l->name, l->by);
807
04925e1e
AM
808 /* We need to find this file and include the symbol table. We
809 want to search for the file in the same way that the dynamic
810 linker will search. That means that we want to use
811 rpath_link, rpath, then the environment variable
ec4eb78a
L
812 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
813 entries (native only), then the linker script LIB_SEARCH_DIRS.
814 We do not search using the -L arguments.
252b5132 815
04925e1e
AM
816 We search twice. The first time, we skip objects which may
817 introduce version mismatches. The second time, we force
818 their use. See gld${EMULATION_NAME}_vercheck comment. */
819 for (force = 0; force < 2; force++)
820 {
04925e1e
AM
821 size_t len;
822 search_dirs_type *search;
6dd8c765 823EOF
9c8ebd6a 824if [ "x${USE_LIBPATH}" = xyes ] ; then
6dd8c765 825cat >>e${EMULATION_NAME}.c <<EOF
dcb0bd0e 826 const char *lib_path;
6dd8c765
L
827 struct bfd_link_needed_list *rp;
828 int found;
829EOF
6dd8c765
L
830fi
831cat >>e${EMULATION_NAME}.c <<EOF
252b5132 832
04925e1e 833 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
e56f61be 834 &n, force))
04925e1e 835 break;
dcb0bd0e 836EOF
9c8ebd6a 837if [ "x${USE_LIBPATH}" = xyes ] ; then
dcb0bd0e 838cat >>e${EMULATION_NAME}.c <<EOF
04925e1e 839 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
e56f61be 840 &n, force))
04925e1e 841 break;
9c8ebd6a
DJ
842EOF
843fi
844if [ "x${NATIVE}" = xyes ] ; then
845cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
846 if (command_line.rpath_link == NULL
847 && command_line.rpath == NULL)
848 {
849 lib_path = (const char *) getenv ("LD_RUN_PATH");
e56f61be 850 if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
04925e1e
AM
851 force))
852 break;
853 }
04925e1e 854 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
e56f61be 855 if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
04925e1e 856 break;
9c8ebd6a
DJ
857EOF
858fi
859if [ "x${USE_LIBPATH}" = xyes ] ; then
860cat >>e${EMULATION_NAME}.c <<EOF
ec4eb78a 861 found = 0;
6dd8c765
L
862 rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
863 for (; !found && rp != NULL; rp = rp->next)
ec4eb78a 864 {
9c8ebd6a 865 char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
ec4eb78a 866 found = (rp->by == l->by
9c8ebd6a 867 && gld${EMULATION_NAME}_search_needed (tmpname,
e56f61be 868 &n,
ec4eb78a 869 force));
9c8ebd6a 870 free (tmpname);
ec4eb78a
L
871 }
872 if (found)
873 break;
874
04925e1e 875EOF
04925e1e
AM
876fi
877cat >>e${EMULATION_NAME}.c <<EOF
878 len = strlen (l->name);
879 for (search = search_head; search != NULL; search = search->next)
880 {
881 char *filename;
252b5132 882
04925e1e
AM
883 if (search->cmdline)
884 continue;
885 filename = (char *) xmalloc (strlen (search->name) + len + 2);
886 sprintf (filename, "%s/%s", search->name, l->name);
e56f61be
L
887 nn.name = filename;
888 if (gld${EMULATION_NAME}_try_needed (&nn, force))
04925e1e
AM
889 break;
890 free (filename);
891 }
892 if (search != NULL)
893 break;
894EOF
9c8ebd6a
DJ
895if [ "x${USE_LIBPATH}" = xyes ] ; then
896 case ${target} in
897 *-*-linux-gnu*)
898 cat >>e${EMULATION_NAME}.c <<EOF
04925e1e
AM
899 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
900 break;
901EOF
9c8ebd6a
DJ
902 # Linux
903 ;;
04925e1e
AM
904 esac
905fi
906cat >>e${EMULATION_NAME}.c <<EOF
907 }
252b5132 908
04925e1e
AM
909 if (force < 2)
910 continue;
252b5132 911
e374f1d9 912 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
04925e1e
AM
913 l->name, l->by);
914 }
915}
252b5132 916
41392f03
AM
917EOF
918fi
919
920cat >>e${EMULATION_NAME}.c <<EOF
252b5132 921
04925e1e 922/* Look through an expression for an assignment statement. */
252b5132 923
04925e1e 924static void
0c7a8e5a 925gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
04925e1e
AM
926{
927 struct bfd_link_hash_entry *h;
928
929 switch (exp->type.node_class)
252b5132 930 {
04925e1e
AM
931 case etree_provide:
932 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
b34976b6 933 FALSE, FALSE, FALSE);
04925e1e
AM
934 if (h == NULL)
935 break;
252b5132 936
04925e1e
AM
937 /* We call record_link_assignment even if the symbol is defined.
938 This is because if it is defined by a dynamic object, we
939 actually want to use the value defined by the linker script,
940 not the value from the dynamic object (because we are setting
941 symbols like etext). If the symbol is defined by a regular
942 object, then, as it happens, calling record_link_assignment
943 will do no harm. */
944
945 /* Fall through. */
946 case etree_assign:
947 if (strcmp (exp->assign.dst, ".") != 0)
252b5132 948 {
efdffbe2 949 if (! (bfd_elf_record_link_assignment
04925e1e 950 (output_bfd, &link_info, exp->assign.dst,
b34976b6 951 exp->type.node_class == etree_provide ? TRUE : FALSE)))
04925e1e
AM
952 einfo ("%P%F: failed to record assignment to %s: %E\n",
953 exp->assign.dst);
252b5132 954 }
04925e1e
AM
955 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
956 break;
252b5132 957
04925e1e
AM
958 case etree_binary:
959 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
960 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
961 break;
252b5132 962
04925e1e
AM
963 case etree_trinary:
964 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
965 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
966 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
967 break;
252b5132 968
04925e1e
AM
969 case etree_unary:
970 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
971 break;
252b5132 972
04925e1e
AM
973 default:
974 break;
252b5132
RH
975 }
976}
977
04925e1e
AM
978
979/* This is called by the before_allocation routine via
980 lang_for_each_statement. It locates any assignment statements, and
981 tells the ELF backend about them, in case they are assignments to
982 symbols which are referred to by dynamic objects. */
983
984static void
0c7a8e5a 985gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
04925e1e
AM
986{
987 if (s->header.type == lang_assignment_statement_enum)
988 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
989}
990
41392f03
AM
991EOF
992
993if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
57e6abd2
AO
994 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
995 ELF_INTERPRETER_SET_DEFAULT="
996 if (sinterp != NULL)
997 {
998 sinterp->contents = ${ELF_INTERPRETER_NAME};
eea6121a 999 sinterp->size = strlen (sinterp->contents) + 1;
57e6abd2
AO
1000 }
1001
1002"
1003 else
1004 ELF_INTERPRETER_SET_DEFAULT=
1005 fi
41392f03 1006cat >>e${EMULATION_NAME}.c <<EOF
04925e1e 1007
252b5132
RH
1008/* This is called after the sections have been attached to output
1009 sections, but before any sizes or addresses have been set. */
1010
1011static void
0c7a8e5a 1012gld${EMULATION_NAME}_before_allocation (void)
252b5132
RH
1013{
1014 const char *rpath;
1015 asection *sinterp;
1016
fd574c46 1017 if (link_info.hash->type == bfd_link_elf_hash_table)
e1918d23
AM
1018 _bfd_elf_tls_setup (output_bfd, &link_info);
1019
252b5132
RH
1020 /* If we are going to make any variable assignments, we need to let
1021 the ELF backend know about them in case the variables are
1022 referred to by dynamic objects. */
1023 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1024
1025 /* Let the ELF backend work out the sizes of any sections required
1026 by dynamic linking. */
1027 rpath = command_line.rpath;
1028 if (rpath == NULL)
1029 rpath = (const char *) getenv ("LD_RUN_PATH");
5a580b3a 1030 if (! (bfd_elf_size_dynamic_sections
0c7a8e5a 1031 (output_bfd, command_line.soname, rpath,
605d9b3a 1032 command_line.filter_shlib,
252b5132
RH
1033 (const char * const *) command_line.auxiliary_filters,
1034 &link_info, &sinterp, lang_elf_version_info)))
1035 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
57e6abd2 1036${ELF_INTERPRETER_SET_DEFAULT}
252b5132
RH
1037 /* Let the user override the dynamic linker we are using. */
1038 if (command_line.interpreter != NULL
1039 && sinterp != NULL)
1040 {
1041 sinterp->contents = (bfd_byte *) command_line.interpreter;
eea6121a 1042 sinterp->size = strlen (command_line.interpreter) + 1;
252b5132
RH
1043 }
1044
1045 /* Look for any sections named .gnu.warning. As a GNU extensions,
1046 we treat such sections as containing warning messages. We print
1047 out the warning message, and then zero out the section size so
1048 that it does not get copied into the output file. */
1049
1050 {
1051 LANG_FOR_EACH_INPUT_STATEMENT (is)
1052 {
1053 asection *s;
1054 bfd_size_type sz;
8c675694 1055 bfd_size_type prefix_len;
252b5132 1056 char *msg;
b34976b6 1057 bfd_boolean ret;
8c675694 1058 const char * gnu_warning_prefix = _("warning: ");
252b5132
RH
1059
1060 if (is->just_syms_flag)
1061 continue;
1062
1063 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1064 if (s == NULL)
1065 continue;
1066
eea6121a 1067 sz = s->size;
8c675694
NC
1068 prefix_len = strlen (gnu_warning_prefix);
1069 msg = xmalloc ((size_t) (prefix_len + sz + 1));
1070 strcpy (msg, gnu_warning_prefix);
1071 if (! bfd_get_section_contents (is->the_bfd, s, msg + prefix_len,
1072 (file_ptr) 0, sz))
252b5132
RH
1073 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1074 is->the_bfd);
8c675694 1075 msg[prefix_len + sz] = '\0';
252b5132
RH
1076 ret = link_info.callbacks->warning (&link_info, msg,
1077 (const char *) NULL,
1078 is->the_bfd, (asection *) NULL,
1079 (bfd_vma) 0);
1080 ASSERT (ret);
1081 free (msg);
1082
1083 /* Clobber the section size, so that we don't waste copying the
1084 warning into the output file. */
eea6121a 1085 s->size = 0;
252b5132
RH
1086 }
1087 }
1088}
1089
41392f03
AM
1090EOF
1091fi
1092
1093if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1094cat >>e${EMULATION_NAME}.c <<EOF
252b5132 1095
04925e1e
AM
1096/* Try to open a dynamic archive. This is where we know that ELF
1097 dynamic libraries have an extension of .so (or .sl on oddball systems
1098 like hpux). */
1099
b34976b6 1100static bfd_boolean
0c7a8e5a
AM
1101gld${EMULATION_NAME}_open_dynamic_archive
1102 (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
252b5132 1103{
04925e1e
AM
1104 const char *filename;
1105 char *string;
252b5132 1106
04925e1e 1107 if (! entry->is_archive)
b34976b6 1108 return FALSE;
252b5132 1109
04925e1e 1110 filename = entry->filename;
252b5132 1111
04925e1e
AM
1112 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
1113 is defined, but it does not seem worth the headache to optimize
1114 away those two bytes of space. */
1115 string = (char *) xmalloc (strlen (search->name)
1116 + strlen (filename)
1117 + strlen (arch)
1118#ifdef EXTRA_SHLIB_EXTENSION
1119 + strlen (EXTRA_SHLIB_EXTENSION)
1120#endif
1121 + sizeof "/lib.so");
1122
1123 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1124
1125#ifdef EXTRA_SHLIB_EXTENSION
1126 /* Try the .so extension first. If that fails build a new filename
1127 using EXTRA_SHLIB_EXTENSION. */
1128 if (! ldfile_try_open_bfd (string, entry))
1129 sprintf (string, "%s/lib%s%s%s", search->name,
1130 filename, arch, EXTRA_SHLIB_EXTENSION);
1131#endif
1132
1133 if (! ldfile_try_open_bfd (string, entry))
252b5132 1134 {
04925e1e 1135 free (string);
b34976b6 1136 return FALSE;
04925e1e 1137 }
252b5132 1138
04925e1e 1139 entry->filename = string;
252b5132 1140
04925e1e
AM
1141 /* We have found a dynamic object to include in the link. The ELF
1142 backend linker will create a DT_NEEDED entry in the .dynamic
1143 section naming this file. If this file includes a DT_SONAME
1144 entry, it will be used. Otherwise, the ELF linker will just use
1145 the name of the file. For an archive found by searching, like
1146 this one, the DT_NEEDED entry should consist of just the name of
1147 the file, without the path information used to find it. Note
1148 that we only need to do this if we have a dynamic object; an
1149 archive will never be referenced by a DT_NEEDED entry.
252b5132 1150
04925e1e
AM
1151 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1152 very pretty. I haven't been able to think of anything that is
1153 pretty, though. */
1154 if (bfd_check_format (entry->the_bfd, bfd_object)
1155 && (entry->the_bfd->flags & DYNAMIC) != 0)
1156 {
04925e1e 1157 ASSERT (entry->is_archive && entry->search_dirs_flag);
252b5132 1158
04925e1e 1159 /* Rather than duplicating the logic above. Just use the
1c9acd94 1160 filename we recorded earlier. */
04925e1e 1161
fed2999d 1162 filename = lbasename (entry->filename);
1c9acd94 1163 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
04925e1e
AM
1164 }
1165
b34976b6 1166 return TRUE;
04925e1e
AM
1167}
1168
41392f03
AM
1169EOF
1170fi
cde43e70
AM
1171
1172if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
41392f03
AM
1173cat >>e${EMULATION_NAME}.c <<EOF
1174
1175/* A variant of lang_output_section_find. Used by place_orphan. */
04925e1e 1176
04925e1e 1177static lang_output_section_statement_type *
2a36a117 1178output_rel_find (asection *sec, int isdyn)
04925e1e
AM
1179{
1180 lang_statement_union_type *u;
1181 lang_output_section_statement_type *lookup;
ba493122 1182 lang_output_section_statement_type *last = NULL;
2a36a117 1183 lang_output_section_statement_type *last_alloc = NULL;
ba493122
AM
1184 lang_output_section_statement_type *last_rel = NULL;
1185 lang_output_section_statement_type *last_rel_alloc = NULL;
24cdb50a 1186 int rela = sec->name[4] == 'a';
04925e1e 1187
ba493122 1188 for (u = lang_output_section_statement.head; u; u = lookup->next)
04925e1e
AM
1189 {
1190 lookup = &u->output_section_statement;
0841712e
JJ
1191 if (lookup->constraint != -1
1192 && strncmp (".rel", lookup->name, 4) == 0)
04925e1e 1193 {
24cdb50a 1194 int lookrela = lookup->name[4] == 'a';
ba493122 1195
2a36a117
AM
1196 /* .rel.dyn must come before all other reloc sections, to suit
1197 GNU ld.so. */
1198 if (isdyn)
1199 break;
1200
1201 /* Don't place after .rel.plt as doing so results in wrong
1202 dynamic tags. */
1203 if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
ba493122 1204 break;
2a36a117
AM
1205
1206 if (rela == lookrela || last_rel == NULL)
24cdb50a 1207 last_rel = lookup;
2a36a117
AM
1208 if ((rela == lookrela || last_rel_alloc == NULL)
1209 && lookup->bfd_section != NULL
ba493122
AM
1210 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1211 last_rel_alloc = lookup;
04925e1e 1212 }
2a36a117
AM
1213
1214 last = lookup;
1215 if (lookup->bfd_section != NULL
1216 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1217 last_alloc = lookup;
252b5132 1218 }
ba493122
AM
1219
1220 if (last_rel_alloc)
1221 return last_rel_alloc;
1222
1223 if (last_rel)
1224 return last_rel;
1225
2a36a117
AM
1226 if (last_alloc)
1227 return last_alloc;
1228
ba493122 1229 return last;
252b5132
RH
1230}
1231
95500e1c
JJ
1232/* Find the last output section before given output statement.
1233 Used by place_orphan. */
1234
1235static asection *
0c7a8e5a 1236output_prev_sec_find (lang_output_section_statement_type *os)
95500e1c
JJ
1237{
1238 asection *s = (asection *) NULL;
1239 lang_statement_union_type *u;
1240 lang_output_section_statement_type *lookup;
1241
1242 for (u = lang_output_section_statement.head;
1243 u != (lang_statement_union_type *) NULL;
1244 u = lookup->next)
1245 {
1246 lookup = &u->output_section_statement;
1247 if (lookup == os)
cde43e70
AM
1248 return s;
1249
1250 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
95500e1c
JJ
1251 s = lookup->bfd_section;
1252 }
1253
cde43e70 1254 return NULL;
95500e1c
JJ
1255}
1256
252b5132
RH
1257/* Place an orphan section. We use this to put random SHF_ALLOC
1258 sections in the right segment. */
1259
41392f03 1260struct orphan_save {
6a345e87 1261 lang_output_section_statement_type *os;
5ba47421 1262 asection **section;
6a345e87 1263 lang_statement_union_type **stmt;
1e5e5400 1264 lang_statement_union_type **os_tail;
6a345e87 1265};
252b5132 1266
b34976b6 1267static bfd_boolean
0c7a8e5a 1268gld${EMULATION_NAME}_place_orphan (lang_input_statement_type *file, asection *s)
252b5132 1269{
aea4bd9d
AM
1270 static struct orphan_save hold_text;
1271 static struct orphan_save hold_rodata;
1272 static struct orphan_save hold_data;
1273 static struct orphan_save hold_bss;
1274 static struct orphan_save hold_rel;
1275 static struct orphan_save hold_interp;
c940e6fc 1276 static struct orphan_save hold_sdata;
f9989030 1277 static int count = 1;
6a345e87 1278 struct orphan_save *place;
71bfc0ae 1279 lang_statement_list_type *old;
252b5132
RH
1280 lang_statement_list_type add;
1281 etree_type *address;
a854a4a7 1282 const char *secname;
a854a4a7 1283 const char *ps = NULL;
252b5132 1284 lang_output_section_statement_type *os;
1e5e5400 1285 lang_statement_union_type **os_tail;
36478e9d 1286 etree_type *load_base;
24cdb50a 1287 int isdyn = 0;
9f61903d 1288 asection *sec;
252b5132 1289
aea4bd9d 1290 secname = bfd_get_section_name (s->owner, s);
9f61903d 1291
1049f94e 1292 if (! link_info.relocatable
24cdb50a 1293 && link_info.combreloc
a8927cfd 1294 && (s->flags & SEC_ALLOC)
24cdb50a
AM
1295 && strncmp (secname, ".rel", 4) == 0)
1296 {
1297 if (secname[4] == 'a')
1298 secname = ".rela.dyn";
1299 else
1300 secname = ".rel.dyn";
1301 isdyn = 1;
1302 }
aea4bd9d 1303
d0d6a25b 1304 if (isdyn || (!config.unique_orphan_sections && !unique_section_p (s)))
252b5132 1305 {
6d02a667 1306 /* Look through the script to see where to place this section. */
a854a4a7
AM
1307 os = lang_output_section_find (secname);
1308
1309 if (os != NULL
0f33d40b
AM
1310 && (os->bfd_section == NULL
1311 || ((s->flags ^ os->bfd_section->flags)
1312 & (SEC_LOAD | SEC_ALLOC)) == 0))
a854a4a7 1313 {
0f33d40b 1314 /* We already have an output section statement with this
22af9fd3 1315 name, and its bfd section, if any, has compatible flags. */
39dcfe18 1316 lang_add_section (&os->children, s, os, file);
b34976b6 1317 return TRUE;
a854a4a7 1318 }
252b5132
RH
1319 }
1320
aea4bd9d
AM
1321 if (hold_text.os == NULL)
1322 hold_text.os = lang_output_section_find (".text");
252b5132
RH
1323
1324 /* If this is a final link, then always put .gnu.warning.SYMBOL
1325 sections into the .text section to get them out of the way. */
36af4a4e 1326 if (link_info.executable
1049f94e 1327 && ! link_info.relocatable
252b5132 1328 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
6a345e87 1329 && hold_text.os != NULL)
252b5132 1330 {
39dcfe18 1331 lang_add_section (&hold_text.os->children, s, hold_text.os, file);
b34976b6 1332 return TRUE;
252b5132
RH
1333 }
1334
1335 /* Decide which segment the section should go in based on the
1336 section name and section flags. We put loadable .note sections
1337 right after the .interp section, so that the PT_NOTE segment is
1338 stored right after the program headers where the OS can read it
1339 in the first page. */
aea4bd9d
AM
1340#define HAVE_SECTION(hold, name) \
1341(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1342
71bfc0ae
AM
1343 place = NULL;
1344 if ((s->flags & SEC_ALLOC) == 0)
1345 ;
252b5132 1346 else if ((s->flags & SEC_LOAD) != 0
577a0623 1347 && strncmp (secname, ".note", 5) == 0
aea4bd9d 1348 && HAVE_SECTION (hold_interp, ".interp"))
6a345e87 1349 place = &hold_interp;
252b5132 1350 else if ((s->flags & SEC_HAS_CONTENTS) == 0
aea4bd9d 1351 && HAVE_SECTION (hold_bss, ".bss"))
6a345e87 1352 place = &hold_bss;
c940e6fc
JW
1353 else if ((s->flags & SEC_SMALL_DATA) != 0
1354 && HAVE_SECTION (hold_sdata, ".sdata"))
1355 place = &hold_sdata;
252b5132 1356 else if ((s->flags & SEC_READONLY) == 0
aea4bd9d 1357 && HAVE_SECTION (hold_data, ".data"))
6a345e87 1358 place = &hold_data;
252b5132 1359 else if (strncmp (secname, ".rel", 4) == 0
ba493122 1360 && (s->flags & SEC_LOAD) != 0
aea4bd9d 1361 && (hold_rel.os != NULL
2a36a117 1362 || (hold_rel.os = output_rel_find (s, isdyn)) != NULL))
ba493122 1363 place = &hold_rel;
71bfc0ae 1364 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == SEC_READONLY
aea4bd9d 1365 && HAVE_SECTION (hold_rodata, ".rodata"))
6a345e87 1366 place = &hold_rodata;
71bfc0ae 1367 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == (SEC_CODE | SEC_READONLY)
6a345e87
AM
1368 && hold_text.os != NULL)
1369 place = &hold_text;
252b5132 1370
aea4bd9d
AM
1371#undef HAVE_SECTION
1372
252b5132
RH
1373 /* Choose a unique name for the section. This will be needed if the
1374 same section name appears in the input file with different
9f61903d
NC
1375 loadable or allocatable characteristics. But if the section
1376 already exists but does not have any flags set, then it has been
63cf1d37 1377 created by the linker, probably as a result of a --section-start
9f61903d
NC
1378 command line switch. */
1379 if ((sec = bfd_get_section_by_name (output_bfd, secname)) != NULL
1380 && bfd_get_section_flags (output_bfd, sec) != 0)
b3ea3584 1381 {
24cdb50a
AM
1382 secname = bfd_get_unique_section_name (output_bfd, secname, &count);
1383 if (secname == NULL)
b3ea3584
AM
1384 einfo ("%F%P: place_orphan failed: %E\n");
1385 }
252b5132 1386
71bfc0ae
AM
1387 /* Start building a list of statements for this section.
1388 First save the current statement pointer. */
1389 old = stat_ptr;
1390
1391 /* If we have found an appropriate place for the output section
1392 statements for this orphan, add them to our own private list,
1393 inserting them later into the global statement list. */
5ba47421 1394 if (place != NULL)
252b5132 1395 {
5ba47421
AM
1396 stat_ptr = &add;
1397 lang_list_init (stat_ptr);
71bfc0ae 1398 }
5ba47421 1399
71bfc0ae
AM
1400 if (config.build_constructors)
1401 {
5ba47421
AM
1402 /* If the name of the section is representable in C, then create
1403 symbols to mark the start and the end of the section. */
24cdb50a 1404 for (ps = secname; *ps != '\0'; ps++)
3882b010 1405 if (! ISALNUM (*ps) && *ps != '_')
5ba47421 1406 break;
71bfc0ae 1407 if (*ps == '\0')
5ba47421
AM
1408 {
1409 char *symname;
1410 etree_type *e_align;
1411
24cdb50a
AM
1412 symname = (char *) xmalloc (ps - secname + sizeof "__start_");
1413 sprintf (symname, "__start_%s", secname);
5ba47421
AM
1414 e_align = exp_unop (ALIGN_K,
1415 exp_intop ((bfd_vma) 1 << s->alignment_power));
1416 lang_add_assignment (exp_assop ('=', symname, e_align));
1417 }
252b5132
RH
1418 }
1419
36478e9d 1420 address = NULL;
1049f94e 1421 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
252b5132 1422 address = exp_intop ((bfd_vma) 0);
36478e9d
AM
1423
1424 load_base = NULL;
1425 if (place != NULL && place->os->load_base != NULL)
1426 {
1427 etree_type *lma_from_vma;
1428 lma_from_vma = exp_binop ('-', place->os->load_base,
1429 exp_nameop (ADDR, place->os->name));
1430 load_base = exp_binop ('+', lma_from_vma,
1431 exp_nameop (ADDR, secname));
1432 }
252b5132 1433
1e5e5400 1434 os_tail = lang_output_section_statement.tail;
24cdb50a 1435 os = lang_enter_output_section_statement (secname, address, 0,
aea4bd9d
AM
1436 (etree_type *) NULL,
1437 (etree_type *) NULL,
0841712e 1438 load_base, 0);
252b5132 1439
39dcfe18 1440 lang_add_section (&os->children, s, os, file);
252b5132 1441
20d04697
L
1442 lang_leave_output_section_statement
1443 ((bfd_vma) 0, "*default*",
ee3cc2e2 1444 (struct lang_output_section_phdr_list *) NULL, NULL);
20d04697 1445
71bfc0ae 1446 if (config.build_constructors && *ps == '\0')
252b5132 1447 {
71bfc0ae 1448 char *symname;
252b5132 1449
71bfc0ae
AM
1450 /* lang_leave_ouput_section_statement resets stat_ptr. Put
1451 stat_ptr back where we want it. */
1452 if (place != NULL)
1453 stat_ptr = &add;
252b5132 1454
24cdb50a
AM
1455 symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
1456 sprintf (symname, "__stop_%s", secname);
71bfc0ae
AM
1457 lang_add_assignment (exp_assop ('=', symname,
1458 exp_nameop (NAME, ".")));
1459 }
5ba47421 1460
71bfc0ae
AM
1461 /* Restore the global list pointer. */
1462 stat_ptr = old;
1463
25844aae 1464 if (place != NULL && os->bfd_section != NULL)
71bfc0ae 1465 {
cde43e70 1466 asection *snew, **pps;
5ba47421
AM
1467
1468 snew = os->bfd_section;
95500e1c 1469
cde43e70
AM
1470 /* Shuffle the bfd section list to make the output file look
1471 neater. This is really only cosmetic. */
1472 if (place->section == NULL)
5ba47421 1473 {
cde43e70 1474 asection *bfd_section = place->os->bfd_section;
5ba47421 1475
cde43e70
AM
1476 /* If the output statement hasn't been used to place
1477 any input sections (and thus doesn't have an output
1478 bfd_section), look for the closest prior output statement
1479 having an output section. */
1480 if (bfd_section == NULL)
1481 bfd_section = output_prev_sec_find (place->os);
1482
1483 if (bfd_section != NULL && bfd_section != snew)
1484 place->section = &bfd_section->next;
1485 }
1486
1487 if (place->section != NULL)
1488 {
3dfe7a8c 1489 /* Unlink the section. */
5ba47421
AM
1490 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1491 ;
3dfe7a8c 1492 bfd_section_list_remove (output_bfd, pps);
5ba47421
AM
1493
1494 /* Now tack it on to the "place->os" section list. */
3dfe7a8c 1495 bfd_section_list_insert (output_bfd, place->section, snew);
5ba47421 1496 }
5ba47421 1497
cde43e70
AM
1498 /* Save the end of this list. Further ophans of this type will
1499 follow the one we've just added. */
1500 place->section = &snew->next;
1501
1502 /* The following is non-cosmetic. We try to put the output
1503 statements in some sort of reasonable order here, because
1504 they determine the final load addresses of the orphan
1505 sections. In addition, placing output statements in the
1506 wrong order may require extra segments. For instance,
1507 given a typical situation of all read-only sections placed
1508 in one segment and following that a segment containing all
1509 the read-write sections, we wouldn't want to place an orphan
1510 read/write section before or amongst the read-only ones. */
ed15ac6a 1511 if (add.head != NULL)
01cc8ff8 1512 {
1e5e5400
AM
1513 lang_statement_union_type *newly_added_os;
1514
ed15ac6a
AM
1515 if (place->stmt == NULL)
1516 {
1517 /* Put the new statement list right at the head. */
1518 *add.tail = place->os->header.next;
1519 place->os->header.next = add.head;
1e5e5400
AM
1520
1521 place->os_tail = &place->os->next;
ed15ac6a
AM
1522 }
1523 else
1524 {
1525 /* Put it after the last orphan statement we added. */
1526 *add.tail = *place->stmt;
1527 *place->stmt = add.head;
1528 }
71bfc0ae 1529
ed15ac6a
AM
1530 /* Fix the global list pointer if we happened to tack our
1531 new list at the tail. */
1532 if (*old->tail == add.head)
1533 old->tail = add.tail;
71bfc0ae 1534
ed15ac6a
AM
1535 /* Save the end of this list. */
1536 place->stmt = add.tail;
1e5e5400
AM
1537
1538 /* Do the same for the list of output section statements. */
1539 newly_added_os = *os_tail;
1540 *os_tail = NULL;
1541 newly_added_os->output_section_statement.next = *place->os_tail;
1542 *place->os_tail = newly_added_os;
1543 place->os_tail = &newly_added_os->output_section_statement.next;
1544
1545 /* Fixing the global list pointer here is a little different.
1546 We added to the list in lang_enter_output_section_statement,
1547 trimmed off the new output_section_statment above when
1548 assigning *os_tail = NULL, but possibly added it back in
1549 the same place when assigning *place->os_tail. */
1550 if (*os_tail == NULL)
1551 lang_output_section_statement.tail = os_tail;
ed15ac6a 1552 }
6a345e87 1553 }
252b5132 1554
b34976b6 1555 return TRUE;
252b5132 1556}
c56feb2b
AM
1557EOF
1558fi
1559
1560if test x"$LDEMUL_FINISH" != xgld"$EMULATION_NAME"_finish; then
1561cat >>e${EMULATION_NAME}.c <<EOF
252b5132 1562
73d074b4 1563static void
0c7a8e5a 1564gld${EMULATION_NAME}_finish (void)
73d074b4 1565{
7a945db6 1566 if (bfd_elf_discard_info (output_bfd, &link_info))
73d074b4 1567 {
e3dc8847
HPN
1568 lang_reset_memory_regions ();
1569
73d074b4
DJ
1570 /* Resize the sections. */
1571 lang_size_sections (stat_ptr->head, abs_output_section,
cf888e70 1572 &stat_ptr->head, 0, (bfd_vma) 0, NULL, TRUE);
73d074b4
DJ
1573
1574 /* Redo special stuff. */
1575 ldemul_after_allocation ();
1576
1577 /* Do the assignments again. */
1578 lang_do_assignments (stat_ptr->head, abs_output_section,
2c382fb6 1579 (fill_type *) 0, (bfd_vma) 0);
73d074b4
DJ
1580 }
1581}
41392f03
AM
1582EOF
1583fi
1584
1585if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1586cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
1587
1588static char *
0c7a8e5a 1589gld${EMULATION_NAME}_get_script (int *isfile)
252b5132
RH
1590EOF
1591
1592if test -n "$COMPILE_IN"
1593then
1594# Scripts compiled in.
1595
1596# sed commands to quote an ld script as a C string.
597e2591 1597sc="-f stringify.sed"
252b5132
RH
1598
1599cat >>e${EMULATION_NAME}.c <<EOF
60bcf0fa 1600{
252b5132
RH
1601 *isfile = 0;
1602
1049f94e 1603 if (link_info.relocatable && config.build_constructors)
597e2591 1604 return
252b5132 1605EOF
b34976b6 1606sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1049f94e 1607echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
b34976b6
AM
1608sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1609echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1610sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
61585df2 1611if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
b34976b6
AM
1612echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1613sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
db6751f2 1614fi
36af4a4e
JJ
1615if test -n "$GENERATE_PIE_SCRIPT" ; then
1616if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
8c37241b
JJ
1617echo ' ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c
1618echo ' && link_info.relro' >> e${EMULATION_NAME}.c
1619echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1620sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c
36af4a4e
JJ
1621echo ' ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1622sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c
1623fi
1624echo ' ; else if (link_info.pie) return' >> e${EMULATION_NAME}.c
1625sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c
1626fi
252b5132 1627if test -n "$GENERATE_SHLIB_SCRIPT" ; then
82434356 1628if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
8c37241b
JJ
1629echo ' ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c
1630echo ' && link_info.relro' >> e${EMULATION_NAME}.c
1631echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1632sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c
db6751f2 1633echo ' ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c
b34976b6 1634sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c
82434356 1635fi
b34976b6
AM
1636echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1637sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
252b5132 1638fi
82434356 1639if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
8c37241b
JJ
1640echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c
1641echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c
1642sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c
b34976b6
AM
1643echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c
1644sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c
82434356 1645fi
b34976b6
AM
1646echo ' ; else return' >> e${EMULATION_NAME}.c
1647sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1648echo '; }' >> e${EMULATION_NAME}.c
252b5132
RH
1649
1650else
1651# Scripts read from the filesystem.
1652
1653cat >>e${EMULATION_NAME}.c <<EOF
60bcf0fa 1654{
252b5132
RH
1655 *isfile = 1;
1656
1049f94e 1657 if (link_info.relocatable && config.build_constructors)
252b5132 1658 return "ldscripts/${EMULATION_NAME}.xu";
1049f94e 1659 else if (link_info.relocatable)
252b5132
RH
1660 return "ldscripts/${EMULATION_NAME}.xr";
1661 else if (!config.text_read_only)
1662 return "ldscripts/${EMULATION_NAME}.xbn";
a060b769
AM
1663EOF
1664if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
1665else
1666cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
1667 else if (!config.magic_demand_paged)
1668 return "ldscripts/${EMULATION_NAME}.xn";
a060b769
AM
1669EOF
1670fi
36af4a4e
JJ
1671if test -n "$GENERATE_PIE_SCRIPT" ; then
1672if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1673cat >>e${EMULATION_NAME}.c <<EOF
8c37241b
JJ
1674 else if (link_info.pie && link_info.combreloc
1675 && link_info.relro && (link_info.flags & DT_BIND_NOW))
1676 return "ldscripts/${EMULATION_NAME}.xdw";
36af4a4e
JJ
1677 else if (link_info.pie && link_info.combreloc)
1678 return "ldscripts/${EMULATION_NAME}.xdc";
1679EOF
1680fi
1681cat >>e${EMULATION_NAME}.c <<EOF
1682 else if (link_info.pie)
1683 return "ldscripts/${EMULATION_NAME}.xd";
1684EOF
1685fi
a060b769
AM
1686if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1687if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1688cat >>e${EMULATION_NAME}.c <<EOF
8c37241b
JJ
1689 else if (link_info.shared && link_info.combreloc
1690 && link_info.relro && (link_info.flags & DT_BIND_NOW))
1691 return "ldscripts/${EMULATION_NAME}.xsw";
a060b769
AM
1692 else if (link_info.shared && link_info.combreloc)
1693 return "ldscripts/${EMULATION_NAME}.xsc";
1694EOF
1695fi
1696cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
1697 else if (link_info.shared)
1698 return "ldscripts/${EMULATION_NAME}.xs";
a060b769
AM
1699EOF
1700fi
1701if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1702cat >>e${EMULATION_NAME}.c <<EOF
8c37241b
JJ
1703 else if (link_info.combreloc && link_info.relro
1704 && (link_info.flags & DT_BIND_NOW))
1705 return "ldscripts/${EMULATION_NAME}.xw";
a060b769
AM
1706 else if (link_info.combreloc)
1707 return "ldscripts/${EMULATION_NAME}.xc";
1708EOF
1709fi
1710cat >>e${EMULATION_NAME}.c <<EOF
252b5132
RH
1711 else
1712 return "ldscripts/${EMULATION_NAME}.x";
1713}
252b5132 1714
3b108066 1715EOF
41392f03
AM
1716fi
1717fi
3b108066 1718
41392f03 1719if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
3b108066 1720
e0ee487b
L
1721if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1722cat >>e${EMULATION_NAME}.c <<EOF
1723 $PARSE_AND_LIST_PROLOGUE
1724EOF
1725fi
1726
e0ee487b
L
1727cat >>e${EMULATION_NAME}.c <<EOF
1728
6c1439be
L
1729#define OPTION_DISABLE_NEW_DTAGS (400)
1730#define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
a1ab1d2a 1731#define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
65765700 1732#define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1)
6c1439be 1733
3bcf5557 1734static void
0c7a8e5a
AM
1735gld${EMULATION_NAME}_add_options
1736 (int ns, char **shortopts, int nl, struct option **longopts,
1737 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
e0ee487b 1738{
3bcf5557
AM
1739 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
1740 static const struct option xtra_long[] = {
e0ee487b 1741EOF
e0ee487b
L
1742
1743if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1744cat >>e${EMULATION_NAME}.c <<EOF
3bcf5557
AM
1745 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1746 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1747 {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
1748 {"Bgroup", no_argument, NULL, OPTION_GROUP},
e0ee487b
L
1749EOF
1750fi
1751
1752if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1753cat >>e${EMULATION_NAME}.c <<EOF
3bcf5557 1754 $PARSE_AND_LIST_LONGOPTS
e0ee487b
L
1755EOF
1756fi
1757
e0ee487b 1758cat >>e${EMULATION_NAME}.c <<EOF
3bcf5557
AM
1759 {NULL, no_argument, NULL, 0}
1760 };
1761
1762 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
1763 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
1764 *longopts = (struct option *)
1765 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
1766 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
1767}
04925e1e 1768
3bcf5557 1769static bfd_boolean
0c7a8e5a 1770gld${EMULATION_NAME}_handle_option (int optc)
e0ee487b 1771{
e0ee487b
L
1772 switch (optc)
1773 {
1774 default:
3bcf5557 1775 return FALSE;
6c1439be 1776
e0ee487b 1777EOF
e0ee487b
L
1778
1779if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1780cat >>e${EMULATION_NAME}.c <<EOF
6c1439be 1781 case OPTION_DISABLE_NEW_DTAGS:
b34976b6 1782 link_info.new_dtags = FALSE;
6c1439be
L
1783 break;
1784
1785 case OPTION_ENABLE_NEW_DTAGS:
b34976b6 1786 link_info.new_dtags = TRUE;
6c1439be
L
1787 break;
1788
65765700 1789 case OPTION_EH_FRAME_HDR:
b34976b6 1790 link_info.eh_frame_hdr = TRUE;
65765700
JJ
1791 break;
1792
a1ab1d2a
UD
1793 case OPTION_GROUP:
1794 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
1795 /* Groups must be self-contained. */
560e09e9
NC
1796 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
1797 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
a1ab1d2a
UD
1798 break;
1799
e0ee487b
L
1800 case 'z':
1801 if (strcmp (optarg, "initfirst") == 0)
1802 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1803 else if (strcmp (optarg, "interpose") == 0)
1804 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1805 else if (strcmp (optarg, "loadfltr") == 0)
1806 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1807 else if (strcmp (optarg, "nodefaultlib") == 0)
1808 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1809 else if (strcmp (optarg, "nodelete") == 0)
1810 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1811 else if (strcmp (optarg, "nodlopen") == 0)
1812 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1813 else if (strcmp (optarg, "nodump") == 0)
1814 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1815 else if (strcmp (optarg, "now") == 0)
1816 {
1817 link_info.flags |= (bfd_vma) DF_BIND_NOW;
1818 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1819 }
1820 else if (strcmp (optarg, "origin") == 0)
1821 {
1822 link_info.flags |= (bfd_vma) DF_ORIGIN;
1823 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1824 }
a1ab1d2a 1825 else if (strcmp (optarg, "defs") == 0)
560e09e9 1826 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
aa713662 1827 else if (strcmp (optarg, "muldefs") == 0)
b34976b6 1828 link_info.allow_multiple_definition = TRUE;
db6751f2 1829 else if (strcmp (optarg, "combreloc") == 0)
b34976b6 1830 link_info.combreloc = TRUE;
db6751f2 1831 else if (strcmp (optarg, "nocombreloc") == 0)
b34976b6 1832 link_info.combreloc = FALSE;
8bd621d8 1833 else if (strcmp (optarg, "nocopyreloc") == 0)
0c7a8e5a 1834 link_info.nocopyreloc = TRUE;
9ee5e499
JJ
1835 else if (strcmp (optarg, "execstack") == 0)
1836 {
1837 link_info.execstack = TRUE;
1838 link_info.noexecstack = FALSE;
1839 }
1840 else if (strcmp (optarg, "noexecstack") == 0)
1841 {
1842 link_info.noexecstack = TRUE;
1843 link_info.execstack = FALSE;
1844 }
8c37241b
JJ
1845 else if (strcmp (optarg, "relro") == 0)
1846 link_info.relro = TRUE;
1847 else if (strcmp (optarg, "norelro") == 0)
1848 link_info.relro = FALSE;
e0ee487b 1849 /* What about the other Solaris -z options? FIXME. */
6c1439be 1850 break;
e0ee487b
L
1851EOF
1852fi
1853
1854if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1855cat >>e${EMULATION_NAME}.c <<EOF
1856 $PARSE_AND_LIST_ARGS_CASES
1857EOF
1858fi
1859
e0ee487b
L
1860cat >>e${EMULATION_NAME}.c <<EOF
1861 }
1862
3bcf5557 1863 return TRUE;
e0ee487b
L
1864}
1865
41392f03 1866EOF
41392f03 1867
4b209b22 1868if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
41392f03
AM
1869cat >>e${EMULATION_NAME}.c <<EOF
1870
e0ee487b 1871static void
0c7a8e5a 1872gld${EMULATION_NAME}_list_options (FILE * file)
e0ee487b
L
1873{
1874EOF
e0ee487b
L
1875
1876if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1877cat >>e${EMULATION_NAME}.c <<EOF
a1ab1d2a 1878 fprintf (file, _(" -Bgroup\t\tSelects group name lookup rules for DSO\n"));
6c1439be
L
1879 fprintf (file, _(" --disable-new-dtags\tDisable new dynamic tags\n"));
1880 fprintf (file, _(" --enable-new-dtags\tEnable new dynamic tags\n"));
65765700 1881 fprintf (file, _(" --eh-frame-hdr\tCreate .eh_frame_hdr section\n"));
ec38dd05 1882 fprintf (file, _(" -z combreloc\t\tMerge dynamic relocs into one section and sort\n"));
560e09e9 1883 fprintf (file, _(" -z defs\t\tReport unresolved symbols in object files.\n"));
9ee5e499 1884 fprintf (file, _(" -z execstack\t\tMark executable as requiring executable stack\n"));
f813923c
HPN
1885 fprintf (file, _(" -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
1886 fprintf (file, _(" -z interpose\t\tMark object to interpose all DSOs but executable\n"));
e0ee487b 1887 fprintf (file, _(" -z loadfltr\t\tMark object requiring immediate process\n"));
aa713662 1888 fprintf (file, _(" -z muldefs\t\tAllow multiple definitions\n"));
146e7110
AM
1889 fprintf (file, _(" -z nocombreloc\tDon't merge dynamic relocs into one section\n"));
1890 fprintf (file, _(" -z nocopyreloc\tDon't create copy relocs\n"));
e0ee487b
L
1891 fprintf (file, _(" -z nodefaultlib\tMark object not to use default search paths\n"));
1892 fprintf (file, _(" -z nodelete\t\tMark DSO non-deletable at runtime\n"));
f813923c
HPN
1893 fprintf (file, _(" -z nodlopen\t\tMark DSO not available to dlopen\n"));
1894 fprintf (file, _(" -z nodump\t\tMark DSO not available to dldump\n"));
10b4329d 1895 fprintf (file, _(" -z noexecstack\tMark executable as not requiring executable stack\n"));
8c37241b 1896 fprintf (file, _(" -z norelro\t\tDon't create RELRO program header\n"));
e0ee487b 1897 fprintf (file, _(" -z now\t\tMark object non-lazy runtime binding\n"));
73e87d70 1898 fprintf (file, _(" -z origin\t\tMark object requiring immediate \$ORIGIN processing\n\t\t\t at runtime\n"));
8c37241b 1899 fprintf (file, _(" -z relro\t\tCreate RELRO program header\n"));
19e3be22 1900 fprintf (file, _(" -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
e0ee487b
L
1901EOF
1902fi
1903
1904if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1905cat >>e${EMULATION_NAME}.c <<EOF
1906 $PARSE_AND_LIST_OPTIONS
1907EOF
1908fi
1909
e0ee487b
L
1910cat >>e${EMULATION_NAME}.c <<EOF
1911}
1912EOF
e0ee487b
L
1913
1914if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1915cat >>e${EMULATION_NAME}.c <<EOF
1916 $PARSE_AND_LIST_EPILOGUE
1917EOF
1918fi
41392f03
AM
1919fi
1920else
41392f03 1921cat >>e${EMULATION_NAME}.c <<EOF
3bcf5557
AM
1922#define gld${EMULATION_NAME}_add_options NULL
1923#define gld${EMULATION_NAME}_handle_option NULL
41392f03 1924EOF
4b209b22 1925if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
41392f03 1926cat >>e${EMULATION_NAME}.c <<EOF
4b209b22 1927#define gld${EMULATION_NAME}_list_options NULL
41392f03
AM
1928EOF
1929fi
1930fi
e0ee487b 1931
252b5132
RH
1932cat >>e${EMULATION_NAME}.c <<EOF
1933
60bcf0fa 1934struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
252b5132 1935{
41392f03
AM
1936 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
1937 ${LDEMUL_SYSLIB-syslib_default},
1938 ${LDEMUL_HLL-hll_default},
1939 ${LDEMUL_AFTER_PARSE-after_parse_default},
1940 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
1941 ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
1942 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
1943 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
1944 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
1945 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
252b5132
RH
1946 "${EMULATION_NAME}",
1947 "${OUTPUT_FORMAT}",
c56feb2b 1948 ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
41392f03
AM
1949 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
1950 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
1951 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
1952 ${LDEMUL_SET_SYMBOLS-NULL},
3bcf5557
AM
1953 ${LDEMUL_PARSE_ARGS-NULL},
1954 gld${EMULATION_NAME}_add_options,
1955 gld${EMULATION_NAME}_handle_option,
41392f03 1956 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
4b209b22 1957 ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
4a43e768 1958 ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
41392f03 1959 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
fac1652d 1960 ${LDEMUL_NEW_VERS_PATTERN-NULL}
252b5132
RH
1961};
1962EOF