Check in an entry that somehow got missed long ago.
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
CommitLineData
2a9fa50c
ILT
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3cat >e${EMULATION_NAME}.c <<EOF
4/* This file is is generated by a shell script. DO NOT EDIT! */
5
6/* 32 bit ELF emulation code for ${EMULATION_NAME}
62548927 7 Copyright (C) 1991, 93, 94, 95, 1996 Free Software Foundation, Inc.
2a9fa50c
ILT
8 Written by Steve Chamberlain <sac@cygnus.com>
9 ELF support by Ian Lance Taylor <ian@cygnus.com>
10
11This file is part of GLD, the Gnu Linker.
12
13This program is free software; you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation; either version 2 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
943fbd5b 25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2a9fa50c
ILT
26
27#define TARGET_IS_${EMULATION_NAME}
28
29#include "bfd.h"
30#include "sysdep.h"
5efddb2e
ILT
31
32#include <ctype.h>
33
2a9fa50c
ILT
34#include "bfdlink.h"
35
36#include "ld.h"
2a9fa50c
ILT
37#include "ldmain.h"
38#include "ldemul.h"
39#include "ldfile.h"
40#include "ldmisc.h"
41#include "ldexp.h"
42#include "ldlang.h"
5efddb2e 43#include "ldgram.h"
2a9fa50c
ILT
44
45static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
5efddb2e 46static boolean gld${EMULATION_NAME}_open_dynamic_archive
ed753c20 47 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
7523be35
ILT
48static void gld${EMULATION_NAME}_after_open PARAMS ((void));
49static void gld${EMULATION_NAME}_check_needed
50 PARAMS ((lang_input_statement_type *));
8055e7a8
ILT
51static void gld${EMULATION_NAME}_stat_needed
52 PARAMS ((lang_input_statement_type *));
7523be35
ILT
53static boolean gld${EMULATION_NAME}_search_needed
54 PARAMS ((const char *, const char *));
55static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *));
2a9fa50c
ILT
56static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
57static void gld${EMULATION_NAME}_find_statement_assignment
58 PARAMS ((lang_statement_union_type *));
59static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
5efddb2e
ILT
60static boolean gld${EMULATION_NAME}_place_orphan
61 PARAMS ((lang_input_statement_type *, asection *));
62static void gld${EMULATION_NAME}_place_section
63 PARAMS ((lang_statement_union_type *));
2a9fa50c
ILT
64static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
65
66static void
67gld${EMULATION_NAME}_before_parse()
68{
db3bfcd6 69 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
5efddb2e
ILT
70 config.dynamic_link = ${DYNAMIC_LINK-true};
71}
72
73/* Try to open a dynamic archive. This is where we know that ELF
74 dynamic libraries have an extension of .so. */
75
76static boolean
ed753c20 77gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
5efddb2e 78 const char *arch;
ed753c20 79 search_dirs_type *search;
5efddb2e
ILT
80 lang_input_statement_type *entry;
81{
82 const char *filename;
ed753c20
ILT
83 char *string;
84
85 if (! entry->is_archive)
86 return false;
5efddb2e
ILT
87
88 filename = entry->filename;
89
ed753c20
ILT
90 string = (char *) xmalloc (strlen (search->name)
91 + strlen (filename)
92 + strlen (arch)
93 + sizeof "/lib.so");
94
95 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
96
97 if (! ldfile_try_open_bfd (string, entry))
98 {
99 free (string);
100 return false;
101 }
102
103 entry->filename = string;
5efddb2e
ILT
104
105 /* We have found a dynamic object to include in the link. The ELF
106 backend linker will create a DT_NEEDED entry in the .dynamic
107 section naming this file. If this file includes a DT_SONAME
108 entry, it will be used. Otherwise, the ELF linker will just use
109 the name of the file. For an archive found by searching, like
110 this one, the DT_NEEDED entry should consist of just the name of
111 the file, without the path information used to find it. Note
112 that we only need to do this if we have a dynamic object; an
113 archive will never be referenced by a DT_NEEDED entry.
114
115 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
116 very pretty. I haven't been able to think of anything that is
117 pretty, though. */
118 if (bfd_check_format (entry->the_bfd, bfd_object)
119 && (entry->the_bfd->flags & DYNAMIC) != 0)
120 {
121 char *needed_name;
122
123 ASSERT (entry->is_archive && entry->search_dirs_flag);
124 needed_name = (char *) xmalloc (strlen (filename)
125 + strlen (arch)
126 + sizeof "lib.so");
127 sprintf (needed_name, "lib%s%s.so", filename, arch);
128 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
129 }
130
131 return true;
2a9fa50c
ILT
132}
133
6cc7365e
ILT
134EOF
135if [ "x${host}" = "x${target}" ] ; then
136cat >>e${EMULATION_NAME}.c <<EOF
137
138/* For a native linker, check the file /etc/ld.so.conf for directories
139 in which we may find shared libraries. /etc/ld.so.conf is really
140 only meaningful on Linux, but we check it on other systems anyhow. */
141
142static boolean gld${EMULATION_NAME}_check_ld_so_conf PARAMS ((const char *));
143
144static boolean
145gld${EMULATION_NAME}_check_ld_so_conf (name)
146 const char *name;
147{
148 static boolean initialized;
149 static char *ld_so_conf;
150
151 if (! initialized)
152 {
153 FILE *f;
154
155 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
156 if (f != NULL)
157 {
158 char *b;
159 size_t len, alloc;
160 int c;
161
162 len = 0;
163 alloc = 100;
164 b = (char *) xmalloc (alloc);
165
166 while ((c = getc (f)) != EOF)
167 {
168 if (len + 1 >= alloc)
169 {
170 alloc *= 2;
171 b = (char *) xrealloc (b, alloc);
172 }
173 if (c != ':'
174 && c != ' '
175 && c != '\t'
176 && c != '\n'
177 && c != ',')
178 {
179 b[len] = c;
180 ++len;
181 }
182 else
183 {
184 if (len > 0 && b[len - 1] != ':')
185 {
186 b[len] = ':';
187 ++len;
188 }
189 }
190 }
191
192 if (len > 0 && b[len - 1] == ':')
193 --len;
194
195 if (len > 0)
196 b[len] = '\0';
197 else
198 {
199 free (b);
200 b = NULL;
201 }
202
203 fclose (f);
204
205 ld_so_conf = b;
206 }
207
208 initialized = true;
209 }
210
211 if (ld_so_conf == NULL)
212 return false;
213
214 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name);
215}
216
217EOF
218fi
219cat >>e${EMULATION_NAME}.c <<EOF
220
7523be35 221/* These variables are required to pass information back and forth
8055e7a8 222 between after_open and check_needed and stat_needed. */
7523be35 223
62548927 224static struct bfd_link_needed_list *global_needed;
8055e7a8 225static struct stat global_stat;
7523be35
ILT
226static boolean global_found;
227
228/* This is called after all the input files have been opened. */
229
230static void
231gld${EMULATION_NAME}_after_open ()
232{
62548927 233 struct bfd_link_needed_list *needed, *l;
7523be35 234
776f2590
ILT
235 /* We only need to worry about this when doing a final link. */
236 if (link_info.relocateable || link_info.shared)
237 return;
238
7523be35
ILT
239 /* Get the list of files which appear in DT_NEEDED entries in
240 dynamic objects included in the link (often there will be none).
241 For each such file, we want to track down the corresponding
242 library, and include the symbol table in the link. This is what
243 the runtime dynamic linker will do. Tracking the files down here
244 permits one dynamic object to include another without requiring
245 special action by the person doing the link. Note that the
246 needed list can actually grow while we are stepping through this
247 loop. */
248 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
249 for (l = needed; l != NULL; l = l->next)
250 {
62548927 251 struct bfd_link_needed_list *ll;
7523be35
ILT
252 const char *lib_path;
253 size_t len;
254 search_dirs_type *search;
255
256 /* If we've already seen this file, skip it. */
257 for (ll = needed; ll != l; ll = ll->next)
258 if (strcmp (ll->name, l->name) == 0)
259 break;
260 if (ll != l)
261 continue;
262
263 /* See if this file was included in the link explicitly. */
264 global_needed = l;
265 global_found = false;
266 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
267 if (global_found)
268 continue;
269
270 /* We need to find this file and include the symbol table. We
271 want to search for the file in the same way that the dynamic
62548927
ILT
272 linker will search. That means that we want to use
273 rpath_link, rpath, then the environment variable
274 LD_LIBRARY_PATH (native only), then the linker script
275 LIB_SEARCH_DIRS. We do not search using the -L arguments. */
276 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
277 l->name))
278 continue;
7523be35
ILT
279 if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name))
280 continue;
62548927
ILT
281 if (command_line.rpath_link == NULL
282 && command_line.rpath == NULL)
283 {
284 lib_path = (const char *) getenv ("LD_RUN_PATH");
285 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
286 continue;
287 }
288EOF
289if [ "x${host}" = "x${target}" ] ; then
290cat >>e${EMULATION_NAME}.c <<EOF
7523be35
ILT
291 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
292 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
293 continue;
62548927
ILT
294EOF
295fi
296cat >>e${EMULATION_NAME}.c <<EOF
7523be35
ILT
297 len = strlen (l->name);
298 for (search = search_head; search != NULL; search = search->next)
299 {
300 char *filename;
301
302 if (search->cmdline)
303 continue;
304 filename = (char *) xmalloc (strlen (search->name) + len + 2);
305 sprintf (filename, "%s/%s", search->name, l->name);
306 if (gld${EMULATION_NAME}_try_needed (filename))
307 break;
308 free (filename);
309 }
310 if (search != NULL)
311 continue;
6cc7365e
ILT
312EOF
313if [ "x${host}" = "x${target}" ] ; then
314cat >>e${EMULATION_NAME}.c <<EOF
315 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name))
316 continue;
317EOF
318fi
319cat >>e${EMULATION_NAME}.c <<EOF
7523be35
ILT
320
321 einfo ("%P: warning: %s, needed by %B, not found\n",
322 l->name, l->by);
323 }
324}
325
326/* Search for a needed file in a path. */
327
328static boolean
329gld${EMULATION_NAME}_search_needed (path, name)
330 const char *path;
331 const char *name;
332{
333 const char *s;
334 size_t len;
335
336 if (path == NULL || *path == '\0')
337 return false;
338 len = strlen (name);
339 while (1)
340 {
341 char *filename, *sset;
342
343 s = strchr (path, ':');
344 if (s == NULL)
345 s = path + strlen (path);
346
347 filename = (char *) xmalloc (s - path + len + 2);
348 if (s == path)
349 sset = filename;
350 else
351 {
352 memcpy (filename, path, s - path);
353 filename[s - path] = '/';
354 sset = filename + (s - path) + 1;
355 }
356 strcpy (sset, name);
357
358 if (gld${EMULATION_NAME}_try_needed (filename))
359 return true;
360
361 free (filename);
362
363 if (*s == '\0')
364 break;
365 path = s + 1;
366 }
367
368 return false;
369}
370
371/* This function is called for each possible name for a dynamic object
372 named by a DT_NEEDED entry. */
373
374static boolean
375gld${EMULATION_NAME}_try_needed (name)
376 const char *name;
377{
378 bfd *abfd;
379
380 abfd = bfd_openr (name, bfd_get_target (output_bfd));
381 if (abfd == NULL)
382 return false;
383 if (! bfd_check_format (abfd, bfd_object))
384 {
385 (void) bfd_close (abfd);
386 return false;
387 }
388 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
389 {
390 (void) bfd_close (abfd);
391 return false;
392 }
393
394 /* We've found a dynamic object matching the DT_NEEDED entry. */
395
8055e7a8
ILT
396 /* We have already checked that there is no other input file of the
397 same name. We must now check again that we are not including the
398 same file twice. We need to do this because on many systems
399 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
400 reference libc.so.1. If we have already included libc.so, we
401 don't want to include libc.so.1 if they are the same file, and we
402 can only check that using stat. */
403
404 if (bfd_stat (abfd, &global_stat) != 0)
405 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
406 global_found = false;
407 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
408 if (global_found)
409 {
410 /* Return true to indicate that we found the file, even though
411 we aren't going to do anything with it. */
412 return true;
413 }
414
7523be35
ILT
415 /* Tell the ELF backend that don't want the output file to have a
416 DT_NEEDED entry for this file. */
417 bfd_elf_set_dt_needed_name (abfd, "");
418
419 /* Add this file into the symbol table. */
420 if (! bfd_link_add_symbols (abfd, &link_info))
421 einfo ("%F%B: could not read symbols: %E\n", abfd);
422
423 return true;
424}
425
8055e7a8 426/* See if an input file matches a DT_NEEDED entry by name. */
7523be35
ILT
427
428static void
429gld${EMULATION_NAME}_check_needed (s)
430 lang_input_statement_type *s;
431{
169df8d5
ILT
432 if (global_found)
433 return;
434
7523be35
ILT
435 if (s->filename != NULL
436 && strcmp (s->filename, global_needed->name) == 0)
169df8d5
ILT
437 {
438 global_found = true;
439 return;
440 }
441
442 if (s->the_bfd != NULL)
443 {
444 const char *soname;
445
446 soname = bfd_elf_get_dt_soname (s->the_bfd);
447 if (soname != NULL
448 && strcmp (soname, global_needed->name) == 0)
449 {
450 global_found = true;
451 return;
452 }
453 }
454
455 if (s->search_dirs_flag
456 && s->filename != NULL
457 && strchr (global_needed->name, '/') == NULL)
ed753c20
ILT
458 {
459 const char *f;
460
461 f = strrchr (s->filename, '/');
462 if (f != NULL
463 && strcmp (f + 1, global_needed->name) == 0)
169df8d5
ILT
464 {
465 global_found = true;
466 return;
467 }
ed753c20 468 }
7523be35
ILT
469}
470
8055e7a8
ILT
471/* See if an input file matches a DT_NEEDED entry by running stat on
472 the file. */
473
474static void
475gld${EMULATION_NAME}_stat_needed (s)
476 lang_input_statement_type *s;
477{
62548927 478 struct stat st;
169df8d5
ILT
479 const char *suffix;
480 const char *soname;
481 const char *f;
62548927 482
8055e7a8
ILT
483 if (global_found)
484 return;
62548927
ILT
485 if (s->the_bfd == NULL)
486 return;
487
488 if (bfd_stat (s->the_bfd, &st) != 0)
8055e7a8 489 {
62548927
ILT
490 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
491 return;
492 }
8055e7a8 493
62548927
ILT
494 if (st.st_dev == global_stat.st_dev
495 && st.st_ino == global_stat.st_ino)
496 {
497 global_found = true;
498 return;
499 }
500
501 /* We issue a warning if it looks like we are including two
502 different versions of the same shared library. For example,
503 there may be a problem if -lc picks up libc.so.6 but some other
169df8d5
ILT
504 shared library has a DT_NEEDED entry of libc.so.5. This is a
505 hueristic test, and it will only work if the name looks like
506 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
507 If we really want to issue warnings about mixing version numbers
508 of shared libraries, we need to find a better way. */
62548927
ILT
509
510 if (strchr (global_needed->name, '/') != NULL)
511 return;
169df8d5
ILT
512 suffix = strstr (global_needed->name, ".so.");
513 if (suffix == NULL)
514 return;
515 suffix += sizeof ".so." - 1;
516
517 soname = bfd_elf_get_dt_soname (s->the_bfd);
518 if (soname == NULL)
519 soname = s->filename;
62548927 520
169df8d5 521 f = strrchr (soname, '/');
62548927
ILT
522 if (f != NULL)
523 ++f;
524 else
169df8d5 525 f = soname;
62548927 526
169df8d5 527 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
62548927 528 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
169df8d5 529 global_needed->name, global_needed->by, f);
8055e7a8
ILT
530}
531
2a9fa50c
ILT
532/* This is called after the sections have been attached to output
533 sections, but before any sizes or addresses have been set. */
534
535static void
536gld${EMULATION_NAME}_before_allocation ()
537{
62548927 538 const char *rpath;
7fb9ca5f
ILT
539 asection *sinterp;
540
2a9fa50c
ILT
541 /* If we are going to make any variable assignments, we need to let
542 the ELF backend know about them in case the variables are
543 referred to by dynamic objects. */
544 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
545
546 /* Let the ELF backend work out the sizes of any sections required
547 by dynamic linking. */
62548927
ILT
548 rpath = command_line.rpath;
549 if (rpath == NULL)
550 rpath = (const char *) getenv ("LD_RUN_PATH");
5efddb2e
ILT
551 if (! bfd_elf32_size_dynamic_sections (output_bfd,
552 command_line.soname,
62548927 553 rpath,
5efddb2e
ILT
554 command_line.export_dynamic,
555 &link_info,
7fb9ca5f 556 &sinterp))
2a9fa50c 557 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
7fb9ca5f
ILT
558
559 /* Let the user override the dynamic linker we are using. */
560 if (command_line.interpreter != NULL
561 && sinterp != NULL)
562 {
563 sinterp->contents = (bfd_byte *) command_line.interpreter;
564 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
565 }
5f3548d6
ILT
566
567 /* Look for any sections named .gnu.warning. As a GNU extensions,
568 we treat such sections as containing warning messages. We print
569 out the warning message, and then zero out the section size so
570 that it does not get copied into the output file. */
571
572 {
573 LANG_FOR_EACH_INPUT_STATEMENT (is)
574 {
575 asection *s;
576 bfd_size_type sz;
577 char *msg;
578 boolean ret;
579
580 if (is->just_syms_flag)
581 continue;
582
583 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
584 if (s == NULL)
585 continue;
586
587 sz = bfd_section_size (is->the_bfd, s);
588 msg = xmalloc ((size_t) sz + 1);
589 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
590 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
591 is->the_bfd);
592 msg[sz] = '\0';
8055e7a8
ILT
593 ret = link_info.callbacks->warning (&link_info, msg,
594 (const char *) NULL,
595 is->the_bfd, (asection *) NULL,
596 (bfd_vma) 0);
5f3548d6
ILT
597 ASSERT (ret);
598 free (msg);
599
600 /* Clobber the section size, so that we don't waste copying the
601 warning into the output file. */
602 s->_raw_size = 0;
603 }
604 }
7523be35
ILT
605
606#if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
607 /* For MIPS ELF the .reginfo section requires special handling.
608 Each input section is 24 bytes, and the final output section must
609 also be 24 bytes. We handle this by clobbering all but the first
610 input section size to 0. The .reginfo section is handled
611 specially by the backend code anyhow. */
612 {
613 boolean found = false;
614 LANG_FOR_EACH_INPUT_STATEMENT (is)
615 {
616 asection *s;
617
618 if (is->just_syms_flag)
619 continue;
620
621 s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
622 if (s == NULL)
623 continue;
624
625 if (! found)
626 {
627 found = true;
628 continue;
629 }
630
631 s->_raw_size = 0;
632 s->_cooked_size = 0;
633 }
634 }
635#endif
2a9fa50c
ILT
636}
637
638/* This is called by the before_allocation routine via
639 lang_for_each_statement. It locates any assignment statements, and
640 tells the ELF backend about them, in case they are assignments to
641 symbols which are referred to by dynamic objects. */
642
643static void
644gld${EMULATION_NAME}_find_statement_assignment (s)
645 lang_statement_union_type *s;
646{
647 if (s->header.type == lang_assignment_statement_enum)
648 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
649}
650
651/* Look through an expression for an assignment statement. */
652
653static void
654gld${EMULATION_NAME}_find_exp_assignment (exp)
655 etree_type *exp;
656{
7158d2b1
ILT
657 struct bfd_link_hash_entry *h;
658
2a9fa50c
ILT
659 switch (exp->type.node_class)
660 {
7158d2b1
ILT
661 case etree_provide:
662 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
663 false, false, false);
664 if (h == NULL)
665 break;
666
667 /* We call record_link_assignment even if the symbol is defined.
668 This is because if it is defined by a dynamic object, we
669 actually want to use the value defined by the linker script,
670 not the value from the dynamic object (because we are setting
671 symbols like etext). If the symbol is defined by a regular
672 object, then, as it happens, calling record_link_assignment
673 will do no harm. */
674
675 /* Fall through. */
2a9fa50c
ILT
676 case etree_assign:
677 if (strcmp (exp->assign.dst, ".") != 0)
678 {
7158d2b1
ILT
679 if (! (bfd_elf32_record_link_assignment
680 (output_bfd, &link_info, exp->assign.dst,
681 exp->type.node_class == etree_provide ? true : false)))
2a9fa50c
ILT
682 einfo ("%P%F: failed to record assignment to %s: %E\n",
683 exp->assign.dst);
684 }
685 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
686 break;
687
688 case etree_binary:
689 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
690 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
691 break;
692
693 case etree_trinary:
6cc7365e 694 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
2a9fa50c
ILT
695 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
696 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
697 break;
698
699 case etree_unary:
700 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
701 break;
702
703 default:
704 break;
705 }
706}
707
5efddb2e
ILT
708/* Place an orphan section. We use this to put random SHF_ALLOC
709 sections in the right segment. */
710
711static asection *hold_section;
712static lang_output_section_statement_type *hold_use;
713static lang_output_section_statement_type *hold_text;
714static lang_output_section_statement_type *hold_data;
715static lang_output_section_statement_type *hold_bss;
7523be35 716static lang_output_section_statement_type *hold_rel;
5efddb2e
ILT
717
718/*ARGSUSED*/
719static boolean
720gld${EMULATION_NAME}_place_orphan (file, s)
721 lang_input_statement_type *file;
722 asection *s;
723{
724 lang_output_section_statement_type *place;
725 asection *snew, **pps;
726 lang_statement_list_type *old;
727 lang_statement_list_type add;
728 etree_type *address;
729 const char *secname, *ps;
730 lang_output_section_statement_type *os;
731
732 if ((s->flags & SEC_ALLOC) == 0)
733 return false;
734
735 /* Look through the script to see where to place this section. */
736 hold_section = s;
737 hold_use = NULL;
738 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
739
740 if (hold_use != NULL)
741 {
742 /* We have already placed a section with this name. */
743 wild_doit (&hold_use->children, s, hold_use, file);
744 return true;
745 }
746
7523be35
ILT
747 secname = bfd_get_section_name (s->owner, s);
748
ed753c20
ILT
749 /* If this is a final link, then always put .gnu.warning.SYMBOL
750 sections into the .text section to get them out of the way. */
751 if (! link_info.shared
752 && ! link_info.relocateable
753 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
754 && hold_text != NULL)
755 {
756 wild_doit (&hold_text->children, s, hold_text, file);
757 return true;
758 }
759
5efddb2e 760 /* Decide which segment the section should go in based on the
7523be35 761 section name and section flags. */
5efddb2e
ILT
762 place = NULL;
763 if ((s->flags & SEC_HAS_CONTENTS) == 0
764 && hold_bss != NULL)
765 place = hold_bss;
766 else if ((s->flags & SEC_READONLY) == 0
767 && hold_data != NULL)
768 place = hold_data;
7523be35
ILT
769 else if (strncmp (secname, ".rel", 4) == 0
770 && hold_rel != NULL)
771 place = hold_rel;
5efddb2e
ILT
772 else if ((s->flags & SEC_READONLY) != 0
773 && hold_text != NULL)
774 place = hold_text;
775 if (place == NULL)
776 return false;
777
5efddb2e 778 /* Create the section in the output file, and put it in the right
7523be35 779 place. This shuffling is to make the output file look neater. */
5efddb2e
ILT
780 snew = bfd_make_section (output_bfd, secname);
781 if (snew == NULL)
782 einfo ("%P%F: output format %s cannot represent section called %s\n",
783 output_bfd->xvec->name, secname);
7523be35
ILT
784 if (place->bfd_section != NULL)
785 {
786 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
787 ;
788 *pps = snew->next;
789 snew->next = place->bfd_section->next;
790 place->bfd_section->next = snew;
791 }
5efddb2e
ILT
792
793 /* Start building a list of statements for this section. */
794 old = stat_ptr;
795 stat_ptr = &add;
796 lang_list_init (stat_ptr);
797
798 /* If the name of the section is representable in C, then create
799 symbols to mark the start and the end of the section. */
800 for (ps = secname; *ps != '\0'; ps++)
801 if (! isalnum (*ps) && *ps != '_')
802 break;
803 if (*ps == '\0' && config.build_constructors)
804 {
805 char *symname;
806
807 symname = (char *) xmalloc (ps - secname + sizeof "__start_");
808 sprintf (symname, "__start_%s", secname);
809 lang_add_assignment (exp_assop ('=', symname,
810 exp_nameop (NAME, ".")));
811 }
812
813 if (! link_info.relocateable)
814 address = NULL;
815 else
816 address = exp_intop ((bfd_vma) 0);
817
818 lang_enter_output_section_statement (secname, address, 0,
819 (bfd_vma) 0,
820 (etree_type *) NULL,
821 (etree_type *) NULL,
822 (etree_type *) NULL);
823
824 os = lang_output_section_statement_lookup (secname);
825 wild_doit (&os->children, s, os, file);
826
827 lang_leave_output_section_statement ((bfd_vma) 0, "*default*");
828 stat_ptr = &add;
829
830 if (*ps == '\0' && config.build_constructors)
831 {
832 char *symname;
833
834 symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
835 sprintf (symname, "__stop_%s", secname);
836 lang_add_assignment (exp_assop ('=', symname,
837 exp_nameop (NAME, ".")));
838 }
839
840 /* Now stick the new statement list right after PLACE. */
841 *add.tail = place->header.next;
842 place->header.next = add.head;
843
844 stat_ptr = old;
845
846 return true;
847}
848
849static void
850gld${EMULATION_NAME}_place_section (s)
851 lang_statement_union_type *s;
852{
853 lang_output_section_statement_type *os;
854
855 if (s->header.type != lang_output_section_statement_enum)
856 return;
857
858 os = &s->output_section_statement;
859
860 if (strcmp (os->name, hold_section->name) == 0)
861 hold_use = os;
862
863 if (strcmp (os->name, ".text") == 0)
864 hold_text = os;
865 else if (strcmp (os->name, ".data") == 0)
866 hold_data = os;
867 else if (strcmp (os->name, ".bss") == 0)
868 hold_bss = os;
7523be35
ILT
869 else if (hold_rel == NULL
870 && strncmp (os->name, ".rel", 4) == 0)
871 hold_rel = os;
5efddb2e
ILT
872}
873
2a9fa50c
ILT
874static char *
875gld${EMULATION_NAME}_get_script(isfile)
876 int *isfile;
877EOF
878
879if test -n "$COMPILE_IN"
880then
881# Scripts compiled in.
882
883# sed commands to quote an ld script as a C string.
884sc='s/["\\]/\\&/g
885s/$/\\n\\/
8861s/^/"/
887$s/$/n"/
888'
889
890cat >>e${EMULATION_NAME}.c <<EOF
891{
892 *isfile = 0;
893
894 if (link_info.relocateable == true && config.build_constructors == true)
895 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
896 else if (link_info.relocateable == true)
897 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
898 else if (!config.text_read_only)
899 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
900 else if (!config.magic_demand_paged)
901 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
5efddb2e
ILT
902 else if (link_info.shared)
903 return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
2a9fa50c
ILT
904 else
905 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
906}
907EOF
908
909else
910# Scripts read from the filesystem.
911
912cat >>e${EMULATION_NAME}.c <<EOF
913{
914 *isfile = 1;
915
916 if (link_info.relocateable == true && config.build_constructors == true)
917 return "ldscripts/${EMULATION_NAME}.xu";
918 else if (link_info.relocateable == true)
919 return "ldscripts/${EMULATION_NAME}.xr";
920 else if (!config.text_read_only)
921 return "ldscripts/${EMULATION_NAME}.xbn";
922 else if (!config.magic_demand_paged)
923 return "ldscripts/${EMULATION_NAME}.xn";
5efddb2e
ILT
924 else if (link_info.shared)
925 return "ldscripts/${EMULATION_NAME}.xs";
2a9fa50c
ILT
926 else
927 return "ldscripts/${EMULATION_NAME}.x";
928}
929EOF
930
931fi
932
933cat >>e${EMULATION_NAME}.c <<EOF
934
935struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
936{
937 gld${EMULATION_NAME}_before_parse,
938 syslib_default,
939 hll_default,
940 after_parse_default,
7523be35 941 gld${EMULATION_NAME}_after_open,
2a9fa50c
ILT
942 after_allocation_default,
943 set_output_arch_default,
944 ldemul_default_target,
945 gld${EMULATION_NAME}_before_allocation,
946 gld${EMULATION_NAME}_get_script,
947 "${EMULATION_NAME}",
5efddb2e
ILT
948 "${OUTPUT_FORMAT}",
949 NULL,
950 NULL,
951 gld${EMULATION_NAME}_open_dynamic_archive,
952 gld${EMULATION_NAME}_place_orphan
2a9fa50c
ILT
953};
954EOF
This page took 0.178457 seconds and 4 git commands to generate.