* read.c (s_lcomm): For .lcomm 1, set align to 0, not 1.
[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}
5f3548d6 7 Copyright (C) 1991, 1993, 1994, 1995 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
25Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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"
37#include "config.h"
38#include "ldmain.h"
39#include "ldemul.h"
40#include "ldfile.h"
41#include "ldmisc.h"
42#include "ldexp.h"
43#include "ldlang.h"
5efddb2e 44#include "ldgram.h"
2a9fa50c
ILT
45
46static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
5efddb2e
ILT
47static boolean gld${EMULATION_NAME}_open_dynamic_archive
48 PARAMS ((const char *, lang_input_statement_type *));
7523be35
ILT
49static void gld${EMULATION_NAME}_after_open PARAMS ((void));
50static void gld${EMULATION_NAME}_check_needed
51 PARAMS ((lang_input_statement_type *));
52static boolean gld${EMULATION_NAME}_search_needed
53 PARAMS ((const char *, const char *));
54static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *));
2a9fa50c
ILT
55static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
56static void gld${EMULATION_NAME}_find_statement_assignment
57 PARAMS ((lang_statement_union_type *));
58static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
5efddb2e
ILT
59static boolean gld${EMULATION_NAME}_place_orphan
60 PARAMS ((lang_input_statement_type *, asection *));
61static void gld${EMULATION_NAME}_place_section
62 PARAMS ((lang_statement_union_type *));
2a9fa50c
ILT
63static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
64
65static void
66gld${EMULATION_NAME}_before_parse()
67{
68 ldfile_output_architecture = bfd_arch_${ARCH};
5efddb2e
ILT
69 config.dynamic_link = ${DYNAMIC_LINK-true};
70}
71
72/* Try to open a dynamic archive. This is where we know that ELF
73 dynamic libraries have an extension of .so. */
74
75static boolean
76gld${EMULATION_NAME}_open_dynamic_archive (arch, entry)
77 const char *arch;
78 lang_input_statement_type *entry;
79{
80 const char *filename;
81
82 filename = entry->filename;
83
84 if (! ldfile_open_file_search (arch, entry, "lib", ".so"))
85 return false;
86
87 /* We have found a dynamic object to include in the link. The ELF
88 backend linker will create a DT_NEEDED entry in the .dynamic
89 section naming this file. If this file includes a DT_SONAME
90 entry, it will be used. Otherwise, the ELF linker will just use
91 the name of the file. For an archive found by searching, like
92 this one, the DT_NEEDED entry should consist of just the name of
93 the file, without the path information used to find it. Note
94 that we only need to do this if we have a dynamic object; an
95 archive will never be referenced by a DT_NEEDED entry.
96
97 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
98 very pretty. I haven't been able to think of anything that is
99 pretty, though. */
100 if (bfd_check_format (entry->the_bfd, bfd_object)
101 && (entry->the_bfd->flags & DYNAMIC) != 0)
102 {
103 char *needed_name;
104
105 ASSERT (entry->is_archive && entry->search_dirs_flag);
106 needed_name = (char *) xmalloc (strlen (filename)
107 + strlen (arch)
108 + sizeof "lib.so");
109 sprintf (needed_name, "lib%s%s.so", filename, arch);
110 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
111 }
112
113 return true;
2a9fa50c
ILT
114}
115
7523be35
ILT
116/* These variables are required to pass information back and forth
117 between after_open and check_needed. */
118
119static struct bfd_elf_link_needed_list *global_needed;
120static boolean global_found;
121
122/* This is called after all the input files have been opened. */
123
124static void
125gld${EMULATION_NAME}_after_open ()
126{
127 struct bfd_elf_link_needed_list *needed, *l;
128
129 /* Get the list of files which appear in DT_NEEDED entries in
130 dynamic objects included in the link (often there will be none).
131 For each such file, we want to track down the corresponding
132 library, and include the symbol table in the link. This is what
133 the runtime dynamic linker will do. Tracking the files down here
134 permits one dynamic object to include another without requiring
135 special action by the person doing the link. Note that the
136 needed list can actually grow while we are stepping through this
137 loop. */
138 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
139 for (l = needed; l != NULL; l = l->next)
140 {
141 struct bfd_elf_link_needed_list *ll;
142 const char *lib_path;
143 size_t len;
144 search_dirs_type *search;
145
146 /* If we've already seen this file, skip it. */
147 for (ll = needed; ll != l; ll = ll->next)
148 if (strcmp (ll->name, l->name) == 0)
149 break;
150 if (ll != l)
151 continue;
152
153 /* See if this file was included in the link explicitly. */
154 global_needed = l;
155 global_found = false;
156 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
157 if (global_found)
158 continue;
159
160 /* We need to find this file and include the symbol table. We
161 want to search for the file in the same way that the dynamic
162 linker will search. That means that we want to use rpath,
163 then the environment variable LD_LIBRARY_PATH, then the
164 linker script LIB_SEARCH_DIRS. We do not search using the -L
165 arguments. */
166 if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name))
167 continue;
168 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
169 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
170 continue;
171 len = strlen (l->name);
172 for (search = search_head; search != NULL; search = search->next)
173 {
174 char *filename;
175
176 if (search->cmdline)
177 continue;
178 filename = (char *) xmalloc (strlen (search->name) + len + 2);
179 sprintf (filename, "%s/%s", search->name, l->name);
180 if (gld${EMULATION_NAME}_try_needed (filename))
181 break;
182 free (filename);
183 }
184 if (search != NULL)
185 continue;
186
187 einfo ("%P: warning: %s, needed by %B, not found\n",
188 l->name, l->by);
189 }
190}
191
192/* Search for a needed file in a path. */
193
194static boolean
195gld${EMULATION_NAME}_search_needed (path, name)
196 const char *path;
197 const char *name;
198{
199 const char *s;
200 size_t len;
201
202 if (path == NULL || *path == '\0')
203 return false;
204 len = strlen (name);
205 while (1)
206 {
207 char *filename, *sset;
208
209 s = strchr (path, ':');
210 if (s == NULL)
211 s = path + strlen (path);
212
213 filename = (char *) xmalloc (s - path + len + 2);
214 if (s == path)
215 sset = filename;
216 else
217 {
218 memcpy (filename, path, s - path);
219 filename[s - path] = '/';
220 sset = filename + (s - path) + 1;
221 }
222 strcpy (sset, name);
223
224 if (gld${EMULATION_NAME}_try_needed (filename))
225 return true;
226
227 free (filename);
228
229 if (*s == '\0')
230 break;
231 path = s + 1;
232 }
233
234 return false;
235}
236
237/* This function is called for each possible name for a dynamic object
238 named by a DT_NEEDED entry. */
239
240static boolean
241gld${EMULATION_NAME}_try_needed (name)
242 const char *name;
243{
244 bfd *abfd;
245
246 abfd = bfd_openr (name, bfd_get_target (output_bfd));
247 if (abfd == NULL)
248 return false;
249 if (! bfd_check_format (abfd, bfd_object))
250 {
251 (void) bfd_close (abfd);
252 return false;
253 }
254 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
255 {
256 (void) bfd_close (abfd);
257 return false;
258 }
259
260 /* We've found a dynamic object matching the DT_NEEDED entry. */
261
262 /* Tell the ELF backend that don't want the output file to have a
263 DT_NEEDED entry for this file. */
264 bfd_elf_set_dt_needed_name (abfd, "");
265
266 /* Add this file into the symbol table. */
267 if (! bfd_link_add_symbols (abfd, &link_info))
268 einfo ("%F%B: could not read symbols: %E\n", abfd);
269
270 return true;
271}
272
273/* See if an input file matches a DT_NEEDED entry. */
274
275static void
276gld${EMULATION_NAME}_check_needed (s)
277 lang_input_statement_type *s;
278{
279 if (s->filename != NULL
280 && strcmp (s->filename, global_needed->name) == 0)
281 global_found = true;
282}
283
2a9fa50c
ILT
284/* This is called after the sections have been attached to output
285 sections, but before any sizes or addresses have been set. */
286
287static void
288gld${EMULATION_NAME}_before_allocation ()
289{
7fb9ca5f
ILT
290 asection *sinterp;
291
2a9fa50c
ILT
292 /* If we are going to make any variable assignments, we need to let
293 the ELF backend know about them in case the variables are
294 referred to by dynamic objects. */
295 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
296
297 /* Let the ELF backend work out the sizes of any sections required
298 by dynamic linking. */
5efddb2e
ILT
299 if (! bfd_elf32_size_dynamic_sections (output_bfd,
300 command_line.soname,
301 command_line.rpath,
302 command_line.export_dynamic,
303 &link_info,
7fb9ca5f 304 &sinterp))
2a9fa50c 305 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
7fb9ca5f
ILT
306
307 /* Let the user override the dynamic linker we are using. */
308 if (command_line.interpreter != NULL
309 && sinterp != NULL)
310 {
311 sinterp->contents = (bfd_byte *) command_line.interpreter;
312 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
313 }
5f3548d6
ILT
314
315 /* Look for any sections named .gnu.warning. As a GNU extensions,
316 we treat such sections as containing warning messages. We print
317 out the warning message, and then zero out the section size so
318 that it does not get copied into the output file. */
319
320 {
321 LANG_FOR_EACH_INPUT_STATEMENT (is)
322 {
323 asection *s;
324 bfd_size_type sz;
325 char *msg;
326 boolean ret;
327
328 if (is->just_syms_flag)
329 continue;
330
331 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
332 if (s == NULL)
333 continue;
334
335 sz = bfd_section_size (is->the_bfd, s);
336 msg = xmalloc ((size_t) sz + 1);
337 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
338 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
339 is->the_bfd);
340 msg[sz] = '\0';
341 ret = link_info.callbacks->warning (&link_info, msg);
342 ASSERT (ret);
343 free (msg);
344
345 /* Clobber the section size, so that we don't waste copying the
346 warning into the output file. */
347 s->_raw_size = 0;
348 }
349 }
7523be35
ILT
350
351#if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
352 /* For MIPS ELF the .reginfo section requires special handling.
353 Each input section is 24 bytes, and the final output section must
354 also be 24 bytes. We handle this by clobbering all but the first
355 input section size to 0. The .reginfo section is handled
356 specially by the backend code anyhow. */
357 {
358 boolean found = false;
359 LANG_FOR_EACH_INPUT_STATEMENT (is)
360 {
361 asection *s;
362
363 if (is->just_syms_flag)
364 continue;
365
366 s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
367 if (s == NULL)
368 continue;
369
370 if (! found)
371 {
372 found = true;
373 continue;
374 }
375
376 s->_raw_size = 0;
377 s->_cooked_size = 0;
378 }
379 }
380#endif
2a9fa50c
ILT
381}
382
383/* This is called by the before_allocation routine via
384 lang_for_each_statement. It locates any assignment statements, and
385 tells the ELF backend about them, in case they are assignments to
386 symbols which are referred to by dynamic objects. */
387
388static void
389gld${EMULATION_NAME}_find_statement_assignment (s)
390 lang_statement_union_type *s;
391{
392 if (s->header.type == lang_assignment_statement_enum)
393 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
394}
395
396/* Look through an expression for an assignment statement. */
397
398static void
399gld${EMULATION_NAME}_find_exp_assignment (exp)
400 etree_type *exp;
401{
7158d2b1
ILT
402 struct bfd_link_hash_entry *h;
403
2a9fa50c
ILT
404 switch (exp->type.node_class)
405 {
7158d2b1
ILT
406 case etree_provide:
407 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
408 false, false, false);
409 if (h == NULL)
410 break;
411
412 /* We call record_link_assignment even if the symbol is defined.
413 This is because if it is defined by a dynamic object, we
414 actually want to use the value defined by the linker script,
415 not the value from the dynamic object (because we are setting
416 symbols like etext). If the symbol is defined by a regular
417 object, then, as it happens, calling record_link_assignment
418 will do no harm. */
419
420 /* Fall through. */
2a9fa50c
ILT
421 case etree_assign:
422 if (strcmp (exp->assign.dst, ".") != 0)
423 {
7158d2b1
ILT
424 if (! (bfd_elf32_record_link_assignment
425 (output_bfd, &link_info, exp->assign.dst,
426 exp->type.node_class == etree_provide ? true : false)))
2a9fa50c
ILT
427 einfo ("%P%F: failed to record assignment to %s: %E\n",
428 exp->assign.dst);
429 }
430 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
431 break;
432
433 case etree_binary:
434 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
435 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
436 break;
437
438 case etree_trinary:
439 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
440 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
441 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
442 break;
443
444 case etree_unary:
445 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
446 break;
447
448 default:
449 break;
450 }
451}
452
5efddb2e
ILT
453/* Place an orphan section. We use this to put random SHF_ALLOC
454 sections in the right segment. */
455
456static asection *hold_section;
457static lang_output_section_statement_type *hold_use;
458static lang_output_section_statement_type *hold_text;
459static lang_output_section_statement_type *hold_data;
460static lang_output_section_statement_type *hold_bss;
7523be35 461static lang_output_section_statement_type *hold_rel;
5efddb2e
ILT
462
463/*ARGSUSED*/
464static boolean
465gld${EMULATION_NAME}_place_orphan (file, s)
466 lang_input_statement_type *file;
467 asection *s;
468{
469 lang_output_section_statement_type *place;
470 asection *snew, **pps;
471 lang_statement_list_type *old;
472 lang_statement_list_type add;
473 etree_type *address;
474 const char *secname, *ps;
475 lang_output_section_statement_type *os;
476
477 if ((s->flags & SEC_ALLOC) == 0)
478 return false;
479
480 /* Look through the script to see where to place this section. */
481 hold_section = s;
482 hold_use = NULL;
483 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
484
485 if (hold_use != NULL)
486 {
487 /* We have already placed a section with this name. */
488 wild_doit (&hold_use->children, s, hold_use, file);
489 return true;
490 }
491
7523be35
ILT
492 secname = bfd_get_section_name (s->owner, s);
493
5efddb2e 494 /* Decide which segment the section should go in based on the
7523be35 495 section name and section flags. */
5efddb2e
ILT
496 place = NULL;
497 if ((s->flags & SEC_HAS_CONTENTS) == 0
498 && hold_bss != NULL)
499 place = hold_bss;
500 else if ((s->flags & SEC_READONLY) == 0
501 && hold_data != NULL)
502 place = hold_data;
7523be35
ILT
503 else if (strncmp (secname, ".rel", 4) == 0
504 && hold_rel != NULL)
505 place = hold_rel;
5efddb2e
ILT
506 else if ((s->flags & SEC_READONLY) != 0
507 && hold_text != NULL)
508 place = hold_text;
509 if (place == NULL)
510 return false;
511
5efddb2e 512 /* Create the section in the output file, and put it in the right
7523be35 513 place. This shuffling is to make the output file look neater. */
5efddb2e
ILT
514 snew = bfd_make_section (output_bfd, secname);
515 if (snew == NULL)
516 einfo ("%P%F: output format %s cannot represent section called %s\n",
517 output_bfd->xvec->name, secname);
7523be35
ILT
518 if (place->bfd_section != NULL)
519 {
520 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
521 ;
522 *pps = snew->next;
523 snew->next = place->bfd_section->next;
524 place->bfd_section->next = snew;
525 }
5efddb2e
ILT
526
527 /* Start building a list of statements for this section. */
528 old = stat_ptr;
529 stat_ptr = &add;
530 lang_list_init (stat_ptr);
531
532 /* If the name of the section is representable in C, then create
533 symbols to mark the start and the end of the section. */
534 for (ps = secname; *ps != '\0'; ps++)
535 if (! isalnum (*ps) && *ps != '_')
536 break;
537 if (*ps == '\0' && config.build_constructors)
538 {
539 char *symname;
540
541 symname = (char *) xmalloc (ps - secname + sizeof "__start_");
542 sprintf (symname, "__start_%s", secname);
543 lang_add_assignment (exp_assop ('=', symname,
544 exp_nameop (NAME, ".")));
545 }
546
547 if (! link_info.relocateable)
548 address = NULL;
549 else
550 address = exp_intop ((bfd_vma) 0);
551
552 lang_enter_output_section_statement (secname, address, 0,
553 (bfd_vma) 0,
554 (etree_type *) NULL,
555 (etree_type *) NULL,
556 (etree_type *) NULL);
557
558 os = lang_output_section_statement_lookup (secname);
559 wild_doit (&os->children, s, os, file);
560
561 lang_leave_output_section_statement ((bfd_vma) 0, "*default*");
562 stat_ptr = &add;
563
564 if (*ps == '\0' && config.build_constructors)
565 {
566 char *symname;
567
568 symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
569 sprintf (symname, "__stop_%s", secname);
570 lang_add_assignment (exp_assop ('=', symname,
571 exp_nameop (NAME, ".")));
572 }
573
574 /* Now stick the new statement list right after PLACE. */
575 *add.tail = place->header.next;
576 place->header.next = add.head;
577
578 stat_ptr = old;
579
580 return true;
581}
582
583static void
584gld${EMULATION_NAME}_place_section (s)
585 lang_statement_union_type *s;
586{
587 lang_output_section_statement_type *os;
588
589 if (s->header.type != lang_output_section_statement_enum)
590 return;
591
592 os = &s->output_section_statement;
593
594 if (strcmp (os->name, hold_section->name) == 0)
595 hold_use = os;
596
597 if (strcmp (os->name, ".text") == 0)
598 hold_text = os;
599 else if (strcmp (os->name, ".data") == 0)
600 hold_data = os;
601 else if (strcmp (os->name, ".bss") == 0)
602 hold_bss = os;
7523be35
ILT
603 else if (hold_rel == NULL
604 && strncmp (os->name, ".rel", 4) == 0)
605 hold_rel = os;
5efddb2e
ILT
606}
607
2a9fa50c
ILT
608static char *
609gld${EMULATION_NAME}_get_script(isfile)
610 int *isfile;
611EOF
612
613if test -n "$COMPILE_IN"
614then
615# Scripts compiled in.
616
617# sed commands to quote an ld script as a C string.
618sc='s/["\\]/\\&/g
619s/$/\\n\\/
6201s/^/"/
621$s/$/n"/
622'
623
624cat >>e${EMULATION_NAME}.c <<EOF
625{
626 *isfile = 0;
627
628 if (link_info.relocateable == true && config.build_constructors == true)
629 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
630 else if (link_info.relocateable == true)
631 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
632 else if (!config.text_read_only)
633 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
634 else if (!config.magic_demand_paged)
635 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
5efddb2e
ILT
636 else if (link_info.shared)
637 return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
2a9fa50c
ILT
638 else
639 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
640}
641EOF
642
643else
644# Scripts read from the filesystem.
645
646cat >>e${EMULATION_NAME}.c <<EOF
647{
648 *isfile = 1;
649
650 if (link_info.relocateable == true && config.build_constructors == true)
651 return "ldscripts/${EMULATION_NAME}.xu";
652 else if (link_info.relocateable == true)
653 return "ldscripts/${EMULATION_NAME}.xr";
654 else if (!config.text_read_only)
655 return "ldscripts/${EMULATION_NAME}.xbn";
656 else if (!config.magic_demand_paged)
657 return "ldscripts/${EMULATION_NAME}.xn";
5efddb2e
ILT
658 else if (link_info.shared)
659 return "ldscripts/${EMULATION_NAME}.xs";
2a9fa50c
ILT
660 else
661 return "ldscripts/${EMULATION_NAME}.x";
662}
663EOF
664
665fi
666
667cat >>e${EMULATION_NAME}.c <<EOF
668
669struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
670{
671 gld${EMULATION_NAME}_before_parse,
672 syslib_default,
673 hll_default,
674 after_parse_default,
7523be35 675 gld${EMULATION_NAME}_after_open,
2a9fa50c
ILT
676 after_allocation_default,
677 set_output_arch_default,
678 ldemul_default_target,
679 gld${EMULATION_NAME}_before_allocation,
680 gld${EMULATION_NAME}_get_script,
681 "${EMULATION_NAME}",
5efddb2e
ILT
682 "${OUTPUT_FORMAT}",
683 NULL,
684 NULL,
685 gld${EMULATION_NAME}_open_dynamic_archive,
686 gld${EMULATION_NAME}_place_orphan
2a9fa50c
ILT
687};
688EOF
This page took 0.076917 seconds and 4 git commands to generate.