]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldlang.c
comment fixes
[thirdparty/binutils-gdb.git] / ld / ldlang.c
1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libiberty.h"
25 #include "obstack.h"
26 #include "bfdlink.h"
27
28 #include "ld.h"
29 #include "ldmain.h"
30 #include "ldgram.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldemul.h"
34 #include "ldlex.h"
35 #include "ldmisc.h"
36 #include "ldctor.h"
37 #include "ldfile.h"
38 #include "fnmatch.h"
39
40 #include <ctype.h>
41
42 /* FORWARDS */
43 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
44 size_t,
45 lang_statement_list_type*));
46
47
48 /* LOCALS */
49 static struct obstack stat_obstack;
50
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static CONST char *startup_file;
54 static lang_statement_list_type input_file_chain;
55 static boolean placed_commons = false;
56 static lang_output_section_statement_type *default_common_section;
57 static boolean map_option_f;
58 static bfd_vma print_dot;
59 static lang_input_statement_type *first_file;
60 static lang_statement_list_type lang_output_section_statement;
61 static CONST char *current_target;
62 static CONST char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65
66 static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69 static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
71 const char *target, boolean add_to_list));
72 static void init_os PARAMS ((lang_output_section_statement_type *s));
73 static void exp_init_os PARAMS ((etree_type *));
74 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
75 static boolean wildcardp PARAMS ((const char *));
76 static lang_statement_union_type *wild_sort
77 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
78 asection *));
79 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
80 const char *section,
81 lang_input_statement_type *file,
82 lang_output_section_statement_type *output));
83 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
84 static void load_symbols PARAMS ((lang_input_statement_type *entry,
85 lang_statement_list_type *));
86 static void wild_file PARAMS ((lang_wild_statement_type *, const char *,
87 lang_input_statement_type *,
88 lang_output_section_statement_type *));
89 static void wild PARAMS ((lang_wild_statement_type *s,
90 const char *section, const char *file,
91 const char *target,
92 lang_output_section_statement_type *output));
93 static bfd *open_output PARAMS ((const char *name));
94 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
95 static void open_input_bfds
96 PARAMS ((lang_statement_union_type *statement, boolean));
97 static void lang_reasonable_defaults PARAMS ((void));
98 static void lang_place_undefineds PARAMS ((void));
99 static void map_input_to_output_sections
100 PARAMS ((lang_statement_union_type *s,
101 const char *target,
102 lang_output_section_statement_type *output_section_statement));
103 static void print_output_section_statement
104 PARAMS ((lang_output_section_statement_type *output_section_statement));
105 static void print_assignment
106 PARAMS ((lang_assignment_statement_type *assignment,
107 lang_output_section_statement_type *output_section));
108 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
109 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
110 static void print_input_section PARAMS ((lang_input_section_type *in));
111 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
112 static void print_data_statement PARAMS ((lang_data_statement_type *data));
113 static void print_address_statement PARAMS ((lang_address_statement_type *));
114 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
115 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
116 static void print_wild_statement
117 PARAMS ((lang_wild_statement_type *w,
118 lang_output_section_statement_type *os));
119 static void print_group
120 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
121 static void print_statement PARAMS ((lang_statement_union_type *s,
122 lang_output_section_statement_type *os));
123 static void print_statement_list PARAMS ((lang_statement_union_type *s,
124 lang_output_section_statement_type *os));
125 static void print_statements PARAMS ((void));
126 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
127 fill_type fill, unsigned int power,
128 asection *output_section_statement,
129 bfd_vma dot));
130 static bfd_vma size_input_section
131 PARAMS ((lang_statement_union_type **this_ptr,
132 lang_output_section_statement_type *output_section_statement,
133 fill_type fill, bfd_vma dot, boolean relax));
134 static void lang_finish PARAMS ((void));
135 static void ignore_bfd_errors PARAMS ((const char *, ...));
136 static void lang_check PARAMS ((void));
137 static void lang_common PARAMS ((void));
138 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
139 static void lang_place_orphans PARAMS ((void));
140 static int topower PARAMS ((int));
141 static void lang_set_startof PARAMS ((void));
142 static void reset_memory_regions PARAMS ((void));
143 static void lang_record_phdrs PARAMS ((void));
144 static void lang_gc_wild_section
145 PARAMS ((lang_wild_statement_type *, const char *,
146 lang_input_statement_type *));
147 static void lang_gc_wild_file
148 PARAMS ((lang_wild_statement_type *, const char *,
149 lang_input_statement_type *));
150 static void lang_gc_wild
151 PARAMS ((lang_wild_statement_type *, const char *, const char *));
152 static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
153 static void lang_gc_sections PARAMS ((void));
154
155
156 /* EXPORTS */
157 lang_output_section_statement_type *abs_output_section;
158 lang_statement_list_type *stat_ptr = &statement_list;
159 lang_statement_list_type file_chain = { 0 };
160 const char *entry_symbol = NULL;
161 boolean entry_from_cmdline;
162 boolean lang_has_input_file = false;
163 boolean had_output_filename = false;
164 boolean lang_float_flag = false;
165 boolean delete_output_file_on_failure = false;
166 struct lang_nocrossrefs *nocrossref_list;
167
168 etree_type *base; /* Relocation base - or null */
169
170
171 #if defined(__STDC__) || defined(ALMOST_STDC)
172 #define cat(a,b) a##b
173 #else
174 #define cat(a,b) a/**/b
175 #endif
176
177 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
178
179 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
180
181 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
182
183 #define SECTION_NAME_MAP_LENGTH (16)
184
185 PTR
186 stat_alloc (size)
187 size_t size;
188 {
189 return obstack_alloc (&stat_obstack, size);
190 }
191
192 /*----------------------------------------------------------------------
193 lang_for_each_statement walks the parse tree and calls the provided
194 function for each node
195 */
196
197 static void
198 lang_for_each_statement_worker (func, s)
199 void (*func) PARAMS ((lang_statement_union_type *));
200 lang_statement_union_type *s;
201 {
202 for (; s != (lang_statement_union_type *) NULL; s = s->next)
203 {
204 func (s);
205
206 switch (s->header.type)
207 {
208 case lang_constructors_statement_enum:
209 lang_for_each_statement_worker (func, constructor_list.head);
210 break;
211 case lang_output_section_statement_enum:
212 lang_for_each_statement_worker
213 (func,
214 s->output_section_statement.children.head);
215 break;
216 case lang_wild_statement_enum:
217 lang_for_each_statement_worker
218 (func,
219 s->wild_statement.children.head);
220 break;
221 case lang_group_statement_enum:
222 lang_for_each_statement_worker (func,
223 s->group_statement.children.head);
224 break;
225 case lang_data_statement_enum:
226 case lang_reloc_statement_enum:
227 case lang_object_symbols_statement_enum:
228 case lang_output_statement_enum:
229 case lang_target_statement_enum:
230 case lang_input_section_enum:
231 case lang_input_statement_enum:
232 case lang_assignment_statement_enum:
233 case lang_padding_statement_enum:
234 case lang_address_statement_enum:
235 case lang_fill_statement_enum:
236 break;
237 default:
238 FAIL ();
239 break;
240 }
241 }
242 }
243
244 void
245 lang_for_each_statement (func)
246 void (*func) PARAMS ((lang_statement_union_type *));
247 {
248 lang_for_each_statement_worker (func,
249 statement_list.head);
250 }
251
252 /*----------------------------------------------------------------------*/
253 void
254 lang_list_init (list)
255 lang_statement_list_type *list;
256 {
257 list->head = (lang_statement_union_type *) NULL;
258 list->tail = &list->head;
259 }
260
261 /*----------------------------------------------------------------------
262
263 build a new statement node for the parse tree
264
265 */
266
267 static
268 lang_statement_union_type *
269 new_statement (type, size, list)
270 enum statement_enum type;
271 size_t size;
272 lang_statement_list_type * list;
273 {
274 lang_statement_union_type *new = (lang_statement_union_type *)
275 stat_alloc (size);
276
277 new->header.type = type;
278 new->header.next = (lang_statement_union_type *) NULL;
279 lang_statement_append (list, new, &new->header.next);
280 return new;
281 }
282
283 /*
284 Build a new input file node for the language. There are several ways
285 in which we treat an input file, eg, we only look at symbols, or
286 prefix it with a -l etc.
287
288 We can be supplied with requests for input files more than once;
289 they may, for example be split over serveral lines like foo.o(.text)
290 foo.o(.data) etc, so when asked for a file we check that we havn't
291 got it already so we don't duplicate the bfd.
292
293 */
294 static lang_input_statement_type *
295 new_afile (name, file_type, target, add_to_list)
296 CONST char *name;
297 lang_input_file_enum_type file_type;
298 CONST char *target;
299 boolean add_to_list;
300 {
301 lang_input_statement_type *p;
302
303 if (add_to_list)
304 p = new_stat (lang_input_statement, stat_ptr);
305 else
306 {
307 p = ((lang_input_statement_type *)
308 stat_alloc (sizeof (lang_input_statement_type)));
309 p->header.next = NULL;
310 }
311
312 lang_has_input_file = true;
313 p->target = target;
314 switch (file_type)
315 {
316 case lang_input_file_is_symbols_only_enum:
317 p->filename = name;
318 p->is_archive = false;
319 p->real = true;
320 p->local_sym_name = name;
321 p->just_syms_flag = true;
322 p->search_dirs_flag = false;
323 break;
324 case lang_input_file_is_fake_enum:
325 p->filename = name;
326 p->is_archive = false;
327 p->real = false;
328 p->local_sym_name = name;
329 p->just_syms_flag = false;
330 p->search_dirs_flag = false;
331 break;
332 case lang_input_file_is_l_enum:
333 p->is_archive = true;
334 p->filename = name;
335 p->real = true;
336 p->local_sym_name = concat ("-l", name, (const char *) NULL);
337 p->just_syms_flag = false;
338 p->search_dirs_flag = true;
339 break;
340 case lang_input_file_is_marker_enum:
341 p->filename = name;
342 p->is_archive = false;
343 p->real = false;
344 p->local_sym_name = name;
345 p->just_syms_flag = false;
346 p->search_dirs_flag = true;
347 break;
348 case lang_input_file_is_search_file_enum:
349 p->filename = name;
350 p->is_archive = false;
351 p->real = true;
352 p->local_sym_name = name;
353 p->just_syms_flag = false;
354 p->search_dirs_flag = true;
355 break;
356 case lang_input_file_is_file_enum:
357 p->filename = name;
358 p->is_archive = false;
359 p->real = true;
360 p->local_sym_name = name;
361 p->just_syms_flag = false;
362 p->search_dirs_flag = false;
363 break;
364 default:
365 FAIL ();
366 }
367 p->the_bfd = (bfd *) NULL;
368 p->asymbols = (asymbol **) NULL;
369 p->next_real_file = (lang_statement_union_type *) NULL;
370 p->next = (lang_statement_union_type *) NULL;
371 p->symbol_count = 0;
372 p->dynamic = config.dynamic_link;
373 p->whole_archive = whole_archive;
374 p->loaded = false;
375 lang_statement_append (&input_file_chain,
376 (lang_statement_union_type *) p,
377 &p->next_real_file);
378 return p;
379 }
380
381 lang_input_statement_type *
382 lang_add_input_file (name, file_type, target)
383 CONST char *name;
384 lang_input_file_enum_type file_type;
385 CONST char *target;
386 {
387 lang_has_input_file = true;
388 return new_afile (name, file_type, target, true);
389 }
390
391 /* Build enough state so that the parser can build its tree */
392 void
393 lang_init ()
394 {
395 obstack_begin (&stat_obstack, 1000);
396
397 stat_ptr = &statement_list;
398
399 lang_list_init (stat_ptr);
400
401 lang_list_init (&input_file_chain);
402 lang_list_init (&lang_output_section_statement);
403 lang_list_init (&file_chain);
404 first_file = lang_add_input_file ((char *) NULL,
405 lang_input_file_is_marker_enum,
406 (char *) NULL);
407 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
408
409 abs_output_section->bfd_section = bfd_abs_section_ptr;
410
411 }
412
413 /*----------------------------------------------------------------------
414 A region is an area of memory declared with the
415 MEMORY { name:org=exp, len=exp ... }
416 syntax.
417
418 We maintain a list of all the regions here
419
420 If no regions are specified in the script, then the default is used
421 which is created when looked up to be the entire data space
422 */
423
424 static lang_memory_region_type *lang_memory_region_list;
425 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
426
427 lang_memory_region_type *
428 lang_memory_region_lookup (name)
429 CONST char *CONST name;
430 {
431 lang_memory_region_type *p;
432
433 for (p = lang_memory_region_list;
434 p != (lang_memory_region_type *) NULL;
435 p = p->next)
436 {
437 if (strcmp (p->name, name) == 0)
438 {
439 return p;
440 }
441 }
442
443 #if 0
444 /* This code used to always use the first region in the list as the
445 default region. I changed it to instead use a region
446 encompassing all of memory as the default region. This permits
447 NOLOAD sections to work reasonably without requiring a region.
448 People should specify what region they mean, if they really want
449 a region. */
450 if (strcmp (name, "*default*") == 0)
451 {
452 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
453 {
454 return lang_memory_region_list;
455 }
456 }
457 #endif
458
459 {
460 lang_memory_region_type *new =
461 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
462
463 new->name = buystring (name);
464 new->next = (lang_memory_region_type *) NULL;
465
466 *lang_memory_region_list_tail = new;
467 lang_memory_region_list_tail = &new->next;
468 new->origin = 0;
469 new->flags = 0;
470 new->not_flags = 0;
471 new->length = ~(bfd_size_type)0;
472 new->current = 0;
473 new->had_full_message = false;
474
475 return new;
476 }
477 }
478
479
480 lang_memory_region_type *
481 lang_memory_default (section)
482 asection *section;
483 {
484 lang_memory_region_type *p;
485
486 flagword sec_flags = section->flags;
487
488 /* Override SEC_DATA to mean a writable section. */
489 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
490 sec_flags |= SEC_DATA;
491
492 for (p = lang_memory_region_list;
493 p != (lang_memory_region_type *) NULL;
494 p = p->next)
495 {
496 if ((p->flags & sec_flags) != 0
497 && (p->not_flags & sec_flags) == 0)
498 {
499 return p;
500 }
501 }
502 return lang_memory_region_lookup ("*default*");
503 }
504
505 lang_output_section_statement_type *
506 lang_output_section_find (name)
507 CONST char *CONST name;
508 {
509 lang_statement_union_type *u;
510 lang_output_section_statement_type *lookup;
511
512 for (u = lang_output_section_statement.head;
513 u != (lang_statement_union_type *) NULL;
514 u = lookup->next)
515 {
516 lookup = &u->output_section_statement;
517 if (strcmp (name, lookup->name) == 0)
518 {
519 return lookup;
520 }
521 }
522 return (lang_output_section_statement_type *) NULL;
523 }
524
525 lang_output_section_statement_type *
526 lang_output_section_statement_lookup (name)
527 CONST char *CONST name;
528 {
529 lang_output_section_statement_type *lookup;
530
531 lookup = lang_output_section_find (name);
532 if (lookup == (lang_output_section_statement_type *) NULL)
533 {
534
535 lookup = (lang_output_section_statement_type *)
536 new_stat (lang_output_section_statement, stat_ptr);
537 lookup->region = (lang_memory_region_type *) NULL;
538 lookup->fill = 0;
539 lookup->block_value = 1;
540 lookup->name = name;
541
542 lookup->next = (lang_statement_union_type *) NULL;
543 lookup->bfd_section = (asection *) NULL;
544 lookup->processed = false;
545 lookup->sectype = normal_section;
546 lookup->addr_tree = (etree_type *) NULL;
547 lang_list_init (&lookup->children);
548
549 lookup->memspec = (CONST char *) NULL;
550 lookup->flags = 0;
551 lookup->subsection_alignment = -1;
552 lookup->section_alignment = -1;
553 lookup->load_base = (union etree_union *) NULL;
554 lookup->phdrs = NULL;
555
556 lang_statement_append (&lang_output_section_statement,
557 (lang_statement_union_type *) lookup,
558 &lookup->next);
559 }
560 return lookup;
561 }
562
563 static void
564 lang_map_flags (flag)
565 flagword flag;
566 {
567 if (flag & SEC_ALLOC)
568 minfo ("a");
569
570 if (flag & SEC_CODE)
571 minfo ("x");
572
573 if (flag & SEC_READONLY)
574 minfo ("r");
575
576 if (flag & SEC_DATA)
577 minfo ("w");
578
579 if (flag & SEC_LOAD)
580 minfo ("l");
581 }
582
583 void
584 lang_map ()
585 {
586 lang_memory_region_type *m;
587
588 minfo (_("\nMemory Configuration\n\n"));
589 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
590 _("Name"), _("Origin"), _("Length"), _("Attributes"));
591
592 for (m = lang_memory_region_list;
593 m != (lang_memory_region_type *) NULL;
594 m = m->next)
595 {
596 char buf[100];
597 int len;
598
599 fprintf (config.map_file, "%-16s ", m->name);
600
601 sprintf_vma (buf, m->origin);
602 minfo ("0x%s ", buf);
603 len = strlen (buf);
604 while (len < 16)
605 {
606 print_space ();
607 ++len;
608 }
609
610 minfo ("0x%V", m->length);
611 if (m->flags || m->not_flags)
612 {
613 #ifndef BFD64
614 minfo (" ");
615 #endif
616 if (m->flags)
617 {
618 print_space ();
619 lang_map_flags (m->flags);
620 }
621
622 if (m->not_flags)
623 {
624 minfo (" !");
625 lang_map_flags (m->not_flags);
626 }
627 }
628
629 print_nl ();
630 }
631
632 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
633
634 print_statements ();
635 }
636
637 /* Initialize an output section. */
638
639 static void
640 init_os (s)
641 lang_output_section_statement_type *s;
642 {
643 section_userdata_type *new;
644
645 if (s->bfd_section != NULL)
646 return;
647
648 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
649 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
650
651 new = ((section_userdata_type *)
652 stat_alloc (sizeof (section_userdata_type)));
653
654 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
655 if (s->bfd_section == (asection *) NULL)
656 s->bfd_section = bfd_make_section (output_bfd, s->name);
657 if (s->bfd_section == (asection *) NULL)
658 {
659 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
660 output_bfd->xvec->name, s->name);
661 }
662 s->bfd_section->output_section = s->bfd_section;
663
664 /* We initialize an output sections output offset to minus its own */
665 /* vma to allow us to output a section through itself */
666 s->bfd_section->output_offset = 0;
667 get_userdata (s->bfd_section) = (PTR) new;
668
669 /* If there is a base address, make sure that any sections it might
670 mention are initialized. */
671 if (s->addr_tree != NULL)
672 exp_init_os (s->addr_tree);
673 }
674
675 /* Make sure that all output sections mentioned in an expression are
676 initialized. */
677
678 static void
679 exp_init_os (exp)
680 etree_type *exp;
681 {
682 switch (exp->type.node_class)
683 {
684 case etree_assign:
685 exp_init_os (exp->assign.src);
686 break;
687
688 case etree_binary:
689 exp_init_os (exp->binary.lhs);
690 exp_init_os (exp->binary.rhs);
691 break;
692
693 case etree_trinary:
694 exp_init_os (exp->trinary.cond);
695 exp_init_os (exp->trinary.lhs);
696 exp_init_os (exp->trinary.rhs);
697 break;
698
699 case etree_unary:
700 exp_init_os (exp->unary.child);
701 break;
702
703 case etree_name:
704 switch (exp->type.node_code)
705 {
706 case ADDR:
707 case LOADADDR:
708 case SIZEOF:
709 {
710 lang_output_section_statement_type *os;
711
712 os = lang_output_section_find (exp->name.name);
713 if (os != NULL && os->bfd_section == NULL)
714 init_os (os);
715 }
716 }
717 break;
718
719 default:
720 break;
721 }
722 }
723
724 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
725 once into the output. This routine checks each sections, and
726 arranges to discard it if a section of the same name has already
727 been linked. This code assumes that all relevant sections have the
728 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
729 section name. This is called via bfd_map_over_sections. */
730
731 /*ARGSUSED*/
732 static void
733 section_already_linked (abfd, sec, data)
734 bfd *abfd;
735 asection *sec;
736 PTR data;
737 {
738 lang_input_statement_type *entry = (lang_input_statement_type *) data;
739 struct sec_link_once
740 {
741 struct sec_link_once *next;
742 asection *sec;
743 };
744 static struct sec_link_once *sec_link_once_list;
745 flagword flags;
746 const char *name;
747 struct sec_link_once *l;
748
749 /* If we are only reading symbols from this object, then we want to
750 discard all sections. */
751 if (entry->just_syms_flag)
752 {
753 sec->output_section = bfd_abs_section_ptr;
754 sec->output_offset = sec->vma;
755 return;
756 }
757
758 flags = bfd_get_section_flags (abfd, sec);
759
760 if ((flags & SEC_LINK_ONCE) == 0)
761 return;
762
763 name = bfd_get_section_name (abfd, sec);
764
765 for (l = sec_link_once_list; l != NULL; l = l->next)
766 {
767 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
768 {
769 /* The section has already been linked. See if we should
770 issue a warning. */
771 switch (flags & SEC_LINK_DUPLICATES)
772 {
773 default:
774 abort ();
775
776 case SEC_LINK_DUPLICATES_DISCARD:
777 break;
778
779 case SEC_LINK_DUPLICATES_ONE_ONLY:
780 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
781 abfd, name);
782 break;
783
784 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
785 /* FIXME: We should really dig out the contents of both
786 sections and memcmp them. The COFF/PE spec says that
787 the Microsoft linker does not implement this
788 correctly, so I'm not going to bother doing it
789 either. */
790 /* Fall through. */
791 case SEC_LINK_DUPLICATES_SAME_SIZE:
792 if (bfd_section_size (abfd, sec)
793 != bfd_section_size (l->sec->owner, l->sec))
794 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
795 abfd, name);
796 break;
797 }
798
799 /* Set the output_section field so that wild_doit does not
800 create a lang_input_section structure for this section. */
801 sec->output_section = bfd_abs_section_ptr;
802
803 return;
804 }
805 }
806
807 /* This is the first section with this name. Record it. */
808
809 l = (struct sec_link_once *) xmalloc (sizeof *l);
810 l->sec = sec;
811 l->next = sec_link_once_list;
812 sec_link_once_list = l;
813 }
814 \f
815 /* The wild routines.
816
817 These expand statements like *(.text) and foo.o to a list of
818 explicit actions, like foo.o(.text), bar.o(.text) and
819 foo.o(.text, .data). */
820
821 /* Return true if the PATTERN argument is a wildcard pattern.
822 Although backslashes are treated specially if a pattern contains
823 wildcards, we do not consider the mere presence of a backslash to
824 be enough to cause the the pattern to be treated as a wildcard.
825 That lets us handle DOS filenames more naturally. */
826
827 static boolean
828 wildcardp (pattern)
829 const char *pattern;
830 {
831 const char *s;
832
833 for (s = pattern; *s != '\0'; ++s)
834 if (*s == '?'
835 || *s == '*'
836 || *s == '[')
837 return true;
838 return false;
839 }
840
841 /* Add SECTION to the output section OUTPUT. Do this by creating a
842 lang_input_section statement which is placed at PTR. FILE is the
843 input file which holds SECTION. */
844
845 void
846 wild_doit (ptr, section, output, file)
847 lang_statement_list_type *ptr;
848 asection *section;
849 lang_output_section_statement_type *output;
850 lang_input_statement_type *file;
851 {
852 flagword flags;
853 boolean discard;
854
855 flags = bfd_get_section_flags (section->owner, section);
856
857 discard = false;
858
859 /* If we are doing a final link, discard sections marked with
860 SEC_EXCLUDE. */
861 if (! link_info.relocateable
862 && (flags & SEC_EXCLUDE) != 0)
863 discard = true;
864
865 /* Discard input sections which are assigned to a section named
866 DISCARD_SECTION_NAME. */
867 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
868 discard = true;
869
870 /* Discard debugging sections if we are stripping debugging
871 information. */
872 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
873 && (flags & SEC_DEBUGGING) != 0)
874 discard = true;
875
876 if (discard)
877 {
878 if (section->output_section == NULL)
879 {
880 /* This prevents future calls from assigning this section. */
881 section->output_section = bfd_abs_section_ptr;
882 }
883 return;
884 }
885
886 if (section->output_section == NULL)
887 {
888 boolean first;
889 lang_input_section_type *new;
890 flagword flags;
891
892 if (output->bfd_section == NULL)
893 {
894 init_os (output);
895 first = true;
896 }
897 else
898 first = false;
899
900 /* Add a section reference to the list */
901 new = new_stat (lang_input_section, ptr);
902
903 new->section = section;
904 new->ifile = file;
905 section->output_section = output->bfd_section;
906
907 flags = section->flags;
908
909 /* We don't copy the SEC_NEVER_LOAD flag from an input section
910 to an output section, because we want to be able to include a
911 SEC_NEVER_LOAD section in the middle of an otherwise loaded
912 section (I don't know why we want to do this, but we do).
913 build_link_order in ldwrite.c handles this case by turning
914 the embedded SEC_NEVER_LOAD section into a fill. */
915
916 flags &= ~ SEC_NEVER_LOAD;
917
918 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
919 already been processed. One reason to do this is that on pe
920 format targets, .text$foo sections go into .text and it's odd
921 to see .text with SEC_LINK_ONCE set. */
922
923 if (! link_info.relocateable)
924 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
925
926 /* If this is not the first input section, and the SEC_READONLY
927 flag is not currently set, then don't set it just because the
928 input section has it set. */
929
930 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
931 flags &= ~ SEC_READONLY;
932
933 section->output_section->flags |= flags;
934
935 /* If SEC_READONLY is not set in the input section, then clear
936 it from the output section. */
937 if ((section->flags & SEC_READONLY) == 0)
938 section->output_section->flags &= ~SEC_READONLY;
939
940 switch (output->sectype)
941 {
942 case normal_section:
943 break;
944 case dsect_section:
945 case copy_section:
946 case info_section:
947 case overlay_section:
948 output->bfd_section->flags &= ~SEC_ALLOC;
949 break;
950 case noload_section:
951 output->bfd_section->flags &= ~SEC_LOAD;
952 output->bfd_section->flags |= SEC_NEVER_LOAD;
953 break;
954 }
955
956 if (section->alignment_power > output->bfd_section->alignment_power)
957 output->bfd_section->alignment_power = section->alignment_power;
958
959 /* If supplied an aligment, then force it. */
960 if (output->section_alignment != -1)
961 output->bfd_section->alignment_power = output->section_alignment;
962 }
963 }
964
965 /* Handle wildcard sorting. This returns the lang_input_section which
966 should follow the one we are going to create for SECTION and FILE,
967 based on the sorting requirements of WILD. It returns NULL if the
968 new section should just go at the end of the current list. */
969
970 static lang_statement_union_type *
971 wild_sort (wild, file, section)
972 lang_wild_statement_type *wild;
973 lang_input_statement_type *file;
974 asection *section;
975 {
976 const char *section_name;
977 lang_statement_union_type *l;
978
979 if (! wild->filenames_sorted && ! wild->sections_sorted)
980 return NULL;
981
982 section_name = bfd_get_section_name (file->the_bfd, section);
983 for (l = wild->children.head; l != NULL; l = l->next)
984 {
985 lang_input_section_type *ls;
986
987 if (l->header.type != lang_input_section_enum)
988 continue;
989 ls = &l->input_section;
990
991 /* Sorting by filename takes precedence over sorting by section
992 name. */
993
994 if (wild->filenames_sorted)
995 {
996 int i;
997
998 i = strcmp (file->filename, ls->ifile->filename);
999 if (i > 0)
1000 continue;
1001 else if (i < 0)
1002 break;
1003 }
1004
1005 /* Here either the files are not sorted by name, or we are
1006 looking at the sections for this file. */
1007
1008 if (wild->sections_sorted)
1009 {
1010 if (strcmp (section_name,
1011 bfd_get_section_name (ls->ifile->the_bfd,
1012 ls->section))
1013 < 0)
1014 break;
1015 }
1016 }
1017
1018 return l;
1019 }
1020
1021 /* Expand a wild statement for a particular FILE. SECTION may be
1022 NULL, in which case it is a wild card. */
1023
1024 static void
1025 wild_section (ptr, section, file, output)
1026 lang_wild_statement_type *ptr;
1027 const char *section;
1028 lang_input_statement_type *file;
1029 lang_output_section_statement_type *output;
1030 {
1031 if (file->just_syms_flag == false)
1032 {
1033 register asection *s;
1034 boolean wildcard;
1035
1036 if (section == NULL)
1037 wildcard = false;
1038 else
1039 wildcard = wildcardp (section);
1040
1041 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1042 {
1043 boolean match;
1044
1045 /* Attach all sections named SECTION. If SECTION is NULL,
1046 then attach all sections.
1047
1048 Previously, if SECTION was NULL, this code did not call
1049 wild_doit if the SEC_IS_COMMON flag was set for the
1050 section. I did not understand that, and I took it out.
1051 --ian@cygnus.com. */
1052
1053 if (section == NULL)
1054 match = true;
1055 else
1056 {
1057 const char *name;
1058
1059 name = bfd_get_section_name (file->the_bfd, s);
1060 if (wildcard)
1061 match = fnmatch (section, name, 0) == 0 ? true : false;
1062 else
1063 match = strcmp (section, name) == 0 ? true : false;
1064 }
1065
1066 if (match)
1067 {
1068 lang_statement_union_type *before;
1069
1070 /* If the wild pattern was marked KEEP, the member sections
1071 should be as well. */
1072 if (ptr->keep_sections)
1073 s->flags |= SEC_KEEP;
1074
1075 before = wild_sort (ptr, file, s);
1076
1077 /* Here BEFORE points to the lang_input_section which
1078 should follow the one we are about to add. If BEFORE
1079 is NULL, then the section should just go at the end
1080 of the current list. */
1081
1082 if (before == NULL)
1083 wild_doit (&ptr->children, s, output, file);
1084 else
1085 {
1086 lang_statement_list_type list;
1087 lang_statement_union_type **pp;
1088
1089 lang_list_init (&list);
1090 wild_doit (&list, s, output, file);
1091 ASSERT (list.head != NULL && list.head->next == NULL);
1092
1093 for (pp = &ptr->children.head;
1094 *pp != before;
1095 pp = &(*pp)->next)
1096 ASSERT (*pp != NULL);
1097
1098 list.head->next = *pp;
1099 *pp = list.head;
1100 }
1101 }
1102 }
1103 }
1104 }
1105
1106 /* This is passed a file name which must have been seen already and
1107 added to the statement tree. We will see if it has been opened
1108 already and had its symbols read. If not then we'll read it. */
1109
1110 static lang_input_statement_type *
1111 lookup_name (name)
1112 const char *name;
1113 {
1114 lang_input_statement_type *search;
1115
1116 for (search = (lang_input_statement_type *) input_file_chain.head;
1117 search != (lang_input_statement_type *) NULL;
1118 search = (lang_input_statement_type *) search->next_real_file)
1119 {
1120 if (search->filename == (char *) NULL && name == (char *) NULL)
1121 return search;
1122 if (search->filename != (char *) NULL
1123 && name != (char *) NULL
1124 && strcmp (search->filename, name) == 0)
1125 break;
1126 }
1127
1128 if (search == (lang_input_statement_type *) NULL)
1129 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1130 false);
1131
1132 /* If we have already added this file, or this file is not real
1133 (FIXME: can that ever actually happen?) or the name is NULL
1134 (FIXME: can that ever actually happen?) don't add this file. */
1135 if (search->loaded
1136 || ! search->real
1137 || search->filename == (const char *) NULL)
1138 return search;
1139
1140 load_symbols (search, (lang_statement_list_type *) NULL);
1141
1142 return search;
1143 }
1144
1145 /* Get the symbols for an input file. */
1146
1147 static void
1148 load_symbols (entry, place)
1149 lang_input_statement_type *entry;
1150 lang_statement_list_type *place;
1151 {
1152 char **matching;
1153
1154 if (entry->loaded)
1155 return;
1156
1157 ldfile_open_file (entry);
1158
1159 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1160 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1161 {
1162 bfd_error_type err;
1163 lang_statement_list_type *hold;
1164
1165 err = bfd_get_error ();
1166 if (err == bfd_error_file_ambiguously_recognized)
1167 {
1168 char **p;
1169
1170 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1171 einfo (_("%B: matching formats:"), entry->the_bfd);
1172 for (p = matching; *p != NULL; p++)
1173 einfo (" %s", *p);
1174 einfo ("%F\n");
1175 }
1176 else if (err != bfd_error_file_not_recognized
1177 || place == NULL)
1178 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1179
1180 bfd_close (entry->the_bfd);
1181 entry->the_bfd = NULL;
1182
1183 /* See if the emulation has some special knowledge. */
1184
1185 if (ldemul_unrecognized_file (entry))
1186 return;
1187
1188 /* Try to interpret the file as a linker script. */
1189
1190 ldfile_open_command_file (entry->filename);
1191
1192 hold = stat_ptr;
1193 stat_ptr = place;
1194
1195 ldfile_assumed_script = true;
1196 parser_input = input_script;
1197 yyparse ();
1198 ldfile_assumed_script = false;
1199
1200 stat_ptr = hold;
1201
1202 return;
1203 }
1204
1205 /* We don't call ldlang_add_file for an archive. Instead, the
1206 add_symbols entry point will call ldlang_add_file, via the
1207 add_archive_element callback, for each element of the archive
1208 which is used. */
1209 switch (bfd_get_format (entry->the_bfd))
1210 {
1211 default:
1212 break;
1213
1214 case bfd_object:
1215 ldlang_add_file (entry);
1216 if (trace_files || trace_file_tries)
1217 info_msg ("%I\n", entry);
1218 break;
1219
1220 case bfd_archive:
1221 if (entry->whole_archive)
1222 {
1223 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1224 (bfd *) NULL);
1225 while (member != NULL)
1226 {
1227 if (! bfd_check_format (member, bfd_object))
1228 einfo (_("%F%B: object %B in archive is not object\n"),
1229 entry->the_bfd, member);
1230 if (! ((*link_info.callbacks->add_archive_element)
1231 (&link_info, member, "--whole-archive")))
1232 abort ();
1233 if (! bfd_link_add_symbols (member, &link_info))
1234 einfo (_("%F%B: could not read symbols: %E\n"), member);
1235 member = bfd_openr_next_archived_file (entry->the_bfd,
1236 member);
1237 }
1238
1239 entry->loaded = true;
1240
1241 return;
1242 }
1243 }
1244
1245 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1246 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1247
1248 entry->loaded = true;
1249 }
1250
1251 /* Handle a wild statement for a single file F. */
1252
1253 static void
1254 wild_file (s, section, f, output)
1255 lang_wild_statement_type *s;
1256 const char *section;
1257 lang_input_statement_type *f;
1258 lang_output_section_statement_type *output;
1259 {
1260 if (f->the_bfd == NULL
1261 || ! bfd_check_format (f->the_bfd, bfd_archive))
1262 wild_section (s, section, f, output);
1263 else
1264 {
1265 bfd *member;
1266
1267 /* This is an archive file. We must map each member of the
1268 archive separately. */
1269 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1270 while (member != NULL)
1271 {
1272 /* When lookup_name is called, it will call the add_symbols
1273 entry point for the archive. For each element of the
1274 archive which is included, BFD will call ldlang_add_file,
1275 which will set the usrdata field of the member to the
1276 lang_input_statement. */
1277 if (member->usrdata != NULL)
1278 {
1279 wild_section (s, section,
1280 (lang_input_statement_type *) member->usrdata,
1281 output);
1282 }
1283
1284 member = bfd_openr_next_archived_file (f->the_bfd, member);
1285 }
1286 }
1287 }
1288
1289 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1290 indicating that it is a wildcard. Separate lang_input_section
1291 statements are created for each part of the expansion; they are
1292 added after the wild statement S. OUTPUT is the output section. */
1293
1294 static void
1295 wild (s, section, file, target, output)
1296 lang_wild_statement_type *s;
1297 const char *section;
1298 const char *file;
1299 const char *target;
1300 lang_output_section_statement_type *output;
1301 {
1302 lang_input_statement_type *f;
1303
1304 if (file == (char *) NULL)
1305 {
1306 /* Perform the iteration over all files in the list */
1307 for (f = (lang_input_statement_type *) file_chain.head;
1308 f != (lang_input_statement_type *) NULL;
1309 f = (lang_input_statement_type *) f->next)
1310 {
1311 wild_file (s, section, f, output);
1312 }
1313 }
1314 else if (wildcardp (file))
1315 {
1316 for (f = (lang_input_statement_type *) file_chain.head;
1317 f != (lang_input_statement_type *) NULL;
1318 f = (lang_input_statement_type *) f->next)
1319 {
1320 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1321 wild_file (s, section, f, output);
1322 }
1323 }
1324 else
1325 {
1326 /* Perform the iteration over a single file */
1327 f = lookup_name (file);
1328 wild_file (s, section, f, output);
1329 }
1330
1331 if (section != (char *) NULL
1332 && strcmp (section, "COMMON") == 0
1333 && default_common_section == NULL)
1334 {
1335 /* Remember the section that common is going to in case we later
1336 get something which doesn't know where to put it. */
1337 default_common_section = output;
1338 }
1339 }
1340
1341 /* Open the output file. */
1342
1343 static bfd *
1344 open_output (name)
1345 const char *name;
1346 {
1347 bfd *output;
1348
1349 if (output_target == (char *) NULL)
1350 {
1351 if (current_target != (char *) NULL)
1352 output_target = current_target;
1353 else
1354 output_target = default_target;
1355 }
1356 output = bfd_openw (name, output_target);
1357
1358 if (output == (bfd *) NULL)
1359 {
1360 if (bfd_get_error () == bfd_error_invalid_target)
1361 {
1362 einfo (_("%P%F: target %s not found\n"), output_target);
1363 }
1364 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1365 }
1366
1367 delete_output_file_on_failure = true;
1368
1369 /* output->flags |= D_PAGED;*/
1370
1371 if (! bfd_set_format (output, bfd_object))
1372 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1373 if (! bfd_set_arch_mach (output,
1374 ldfile_output_architecture,
1375 ldfile_output_machine))
1376 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1377
1378 link_info.hash = bfd_link_hash_table_create (output);
1379 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1380 einfo (_("%P%F: can not create link hash table: %E\n"));
1381
1382 bfd_set_gp_size (output, g_switch_value);
1383 return output;
1384 }
1385
1386
1387
1388
1389 static void
1390 ldlang_open_output (statement)
1391 lang_statement_union_type * statement;
1392 {
1393 switch (statement->header.type)
1394 {
1395 case lang_output_statement_enum:
1396 ASSERT (output_bfd == (bfd *) NULL);
1397 output_bfd = open_output (statement->output_statement.name);
1398 ldemul_set_output_arch ();
1399 if (config.magic_demand_paged && !link_info.relocateable)
1400 output_bfd->flags |= D_PAGED;
1401 else
1402 output_bfd->flags &= ~D_PAGED;
1403 if (config.text_read_only)
1404 output_bfd->flags |= WP_TEXT;
1405 else
1406 output_bfd->flags &= ~WP_TEXT;
1407 if (link_info.traditional_format)
1408 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1409 else
1410 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1411 break;
1412
1413 case lang_target_statement_enum:
1414 current_target = statement->target_statement.target;
1415 break;
1416 default:
1417 break;
1418 }
1419 }
1420
1421 /* Open all the input files. */
1422
1423 static void
1424 open_input_bfds (s, force)
1425 lang_statement_union_type *s;
1426 boolean force;
1427 {
1428 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1429 {
1430 switch (s->header.type)
1431 {
1432 case lang_constructors_statement_enum:
1433 open_input_bfds (constructor_list.head, force);
1434 break;
1435 case lang_output_section_statement_enum:
1436 open_input_bfds (s->output_section_statement.children.head, force);
1437 break;
1438 case lang_wild_statement_enum:
1439 /* Maybe we should load the file's symbols */
1440 if (s->wild_statement.filename
1441 && ! wildcardp (s->wild_statement.filename))
1442 (void) lookup_name (s->wild_statement.filename);
1443 open_input_bfds (s->wild_statement.children.head, force);
1444 break;
1445 case lang_group_statement_enum:
1446 {
1447 struct bfd_link_hash_entry *undefs;
1448
1449 /* We must continually search the entries in the group
1450 until no new symbols are added to the list of undefined
1451 symbols. */
1452
1453 do
1454 {
1455 undefs = link_info.hash->undefs_tail;
1456 open_input_bfds (s->group_statement.children.head, true);
1457 }
1458 while (undefs != link_info.hash->undefs_tail);
1459 }
1460 break;
1461 case lang_target_statement_enum:
1462 current_target = s->target_statement.target;
1463 break;
1464 case lang_input_statement_enum:
1465 if (s->input_statement.real == true)
1466 {
1467 lang_statement_list_type add;
1468
1469 s->input_statement.target = current_target;
1470
1471 /* If we are being called from within a group, and this
1472 is an archive which has already been searched, then
1473 force it to be researched. */
1474 if (force
1475 && s->input_statement.loaded
1476 && bfd_check_format (s->input_statement.the_bfd,
1477 bfd_archive))
1478 s->input_statement.loaded = false;
1479
1480 lang_list_init (&add);
1481
1482 load_symbols (&s->input_statement, &add);
1483
1484 if (add.head != NULL)
1485 {
1486 *add.tail = s->next;
1487 s->next = add.head;
1488 }
1489 }
1490 break;
1491 default:
1492 break;
1493 }
1494 }
1495 }
1496
1497 /* If there are [COMMONS] statements, put a wild one into the bss section */
1498
1499 static void
1500 lang_reasonable_defaults ()
1501 {
1502 #if 0
1503 lang_output_section_statement_lookup (".text");
1504 lang_output_section_statement_lookup (".data");
1505
1506 default_common_section =
1507 lang_output_section_statement_lookup (".bss");
1508
1509
1510 if (placed_commons == false)
1511 {
1512 lang_wild_statement_type *new =
1513 new_stat (lang_wild_statement,
1514 &default_common_section->children);
1515
1516 new->section_name = "COMMON";
1517 new->filename = (char *) NULL;
1518 lang_list_init (&new->children);
1519 }
1520 #endif
1521
1522 }
1523
1524 /*
1525 Add the supplied name to the symbol table as an undefined reference.
1526 Remove items from the chain as we open input bfds
1527 */
1528 typedef struct ldlang_undef_chain_list
1529 {
1530 struct ldlang_undef_chain_list *next;
1531 char *name;
1532 } ldlang_undef_chain_list_type;
1533
1534 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1535
1536 void
1537 ldlang_add_undef (name)
1538 CONST char *CONST name;
1539 {
1540 ldlang_undef_chain_list_type *new =
1541 ((ldlang_undef_chain_list_type *)
1542 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1543
1544 new->next = ldlang_undef_chain_list_head;
1545 ldlang_undef_chain_list_head = new;
1546
1547 new->name = buystring (name);
1548 }
1549
1550 /* Run through the list of undefineds created above and place them
1551 into the linker hash table as undefined symbols belonging to the
1552 script file.
1553 */
1554 static void
1555 lang_place_undefineds ()
1556 {
1557 ldlang_undef_chain_list_type *ptr;
1558
1559 for (ptr = ldlang_undef_chain_list_head;
1560 ptr != (ldlang_undef_chain_list_type *) NULL;
1561 ptr = ptr->next)
1562 {
1563 struct bfd_link_hash_entry *h;
1564
1565 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1566 if (h == (struct bfd_link_hash_entry *) NULL)
1567 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1568 if (h->type == bfd_link_hash_new)
1569 {
1570 h->type = bfd_link_hash_undefined;
1571 h->u.undef.abfd = NULL;
1572 bfd_link_add_undef (link_info.hash, h);
1573 }
1574 }
1575 }
1576
1577 /* Open input files and attatch to output sections */
1578 static void
1579 map_input_to_output_sections (s, target, output_section_statement)
1580 lang_statement_union_type * s;
1581 CONST char *target;
1582 lang_output_section_statement_type * output_section_statement;
1583 {
1584 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1585 {
1586 switch (s->header.type)
1587 {
1588
1589
1590 case lang_wild_statement_enum:
1591 wild (&s->wild_statement, s->wild_statement.section_name,
1592 s->wild_statement.filename, target,
1593 output_section_statement);
1594
1595 break;
1596 case lang_constructors_statement_enum:
1597 map_input_to_output_sections (constructor_list.head,
1598 target,
1599 output_section_statement);
1600 break;
1601 case lang_output_section_statement_enum:
1602 map_input_to_output_sections (s->output_section_statement.children.head,
1603 target,
1604 &s->output_section_statement);
1605 break;
1606 case lang_output_statement_enum:
1607 break;
1608 case lang_target_statement_enum:
1609 target = s->target_statement.target;
1610 break;
1611 case lang_group_statement_enum:
1612 map_input_to_output_sections (s->group_statement.children.head,
1613 target,
1614 output_section_statement);
1615 break;
1616 case lang_fill_statement_enum:
1617 case lang_input_section_enum:
1618 case lang_object_symbols_statement_enum:
1619 case lang_data_statement_enum:
1620 case lang_reloc_statement_enum:
1621 case lang_padding_statement_enum:
1622 case lang_input_statement_enum:
1623 if (output_section_statement != NULL
1624 && output_section_statement->bfd_section == NULL)
1625 init_os (output_section_statement);
1626 break;
1627 case lang_assignment_statement_enum:
1628 if (output_section_statement != NULL
1629 && output_section_statement->bfd_section == NULL)
1630 init_os (output_section_statement);
1631
1632 /* Make sure that any sections mentioned in the assignment
1633 are initialized. */
1634 exp_init_os (s->assignment_statement.exp);
1635 break;
1636 case lang_afile_asection_pair_statement_enum:
1637 FAIL ();
1638 break;
1639 case lang_address_statement_enum:
1640 /* Mark the specified section with the supplied address */
1641 {
1642 lang_output_section_statement_type *os =
1643 lang_output_section_statement_lookup
1644 (s->address_statement.section_name);
1645
1646 if (os->bfd_section == NULL)
1647 init_os (os);
1648 os->addr_tree = s->address_statement.address;
1649 }
1650 break;
1651 }
1652 }
1653 }
1654
1655 static void
1656 print_output_section_statement (output_section_statement)
1657 lang_output_section_statement_type * output_section_statement;
1658 {
1659 asection *section = output_section_statement->bfd_section;
1660 int len;
1661
1662 if (output_section_statement != abs_output_section)
1663 {
1664 minfo ("\n%s", output_section_statement->name);
1665
1666 if (section != NULL)
1667 {
1668 print_dot = section->vma;
1669
1670 len = strlen (output_section_statement->name);
1671 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1672 {
1673 print_nl ();
1674 len = 0;
1675 }
1676 while (len < SECTION_NAME_MAP_LENGTH)
1677 {
1678 print_space ();
1679 ++len;
1680 }
1681
1682 minfo ("0x%V %W", section->vma, section->_raw_size);
1683
1684 if (output_section_statement->load_base != NULL)
1685 {
1686 bfd_vma addr;
1687
1688 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1689 "load base", lang_final_phase_enum);
1690 minfo (_(" load address 0x%V"), addr);
1691 }
1692 }
1693
1694 print_nl ();
1695 }
1696
1697 print_statement_list (output_section_statement->children.head,
1698 output_section_statement);
1699 }
1700
1701 static void
1702 print_assignment (assignment, output_section)
1703 lang_assignment_statement_type * assignment;
1704 lang_output_section_statement_type * output_section;
1705 {
1706 int i;
1707 etree_value_type result;
1708
1709 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1710 print_space ();
1711
1712 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1713 lang_final_phase_enum, print_dot, &print_dot);
1714 if (result.valid)
1715 minfo ("0x%V", result.value + result.section->bfd_section->vma);
1716 else
1717 {
1718 minfo ("*undef* ");
1719 #ifdef BFD64
1720 minfo (" ");
1721 #endif
1722 }
1723
1724 minfo (" ");
1725
1726 exp_print_tree (assignment->exp);
1727
1728 print_nl ();
1729 }
1730
1731 static void
1732 print_input_statement (statm)
1733 lang_input_statement_type * statm;
1734 {
1735 if (statm->filename != (char *) NULL)
1736 {
1737 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1738 }
1739 }
1740
1741 /* Print all symbols defined in a particular section. This is called
1742 via bfd_link_hash_traverse. */
1743
1744 static boolean
1745 print_one_symbol (hash_entry, ptr)
1746 struct bfd_link_hash_entry *hash_entry;
1747 PTR ptr;
1748 {
1749 asection *sec = (asection *) ptr;
1750
1751 if ((hash_entry->type == bfd_link_hash_defined
1752 || hash_entry->type == bfd_link_hash_defweak)
1753 && sec == hash_entry->u.def.section)
1754 {
1755 int i;
1756
1757 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1758 print_space ();
1759 minfo ("0x%V ",
1760 (hash_entry->u.def.value
1761 + hash_entry->u.def.section->output_offset
1762 + hash_entry->u.def.section->output_section->vma));
1763
1764 minfo (" %T\n", hash_entry->root.string);
1765 }
1766
1767 return true;
1768 }
1769
1770 /* Print information about an input section to the map file. */
1771
1772 static void
1773 print_input_section (in)
1774 lang_input_section_type * in;
1775 {
1776 asection *i = in->section;
1777 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1778
1779 if (size != 0)
1780 {
1781 print_space ();
1782
1783 minfo ("%s", i->name);
1784
1785 if (i->output_section != NULL)
1786 {
1787 int len;
1788
1789 len = 1 + strlen (i->name);
1790 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1791 {
1792 print_nl ();
1793 len = 0;
1794 }
1795 while (len < SECTION_NAME_MAP_LENGTH)
1796 {
1797 print_space ();
1798 ++len;
1799 }
1800
1801 minfo ("0x%V %W %B\n",
1802 i->output_section->vma + i->output_offset, size,
1803 i->owner);
1804
1805 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1806 {
1807 len = SECTION_NAME_MAP_LENGTH + 3;
1808 #ifdef BFD64
1809 len += 16;
1810 #else
1811 len += 8;
1812 #endif
1813 while (len > 0)
1814 {
1815 print_space ();
1816 --len;
1817 }
1818
1819 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
1820 }
1821
1822 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1823
1824 print_dot = i->output_section->vma + i->output_offset + size;
1825 }
1826 }
1827 }
1828
1829 static void
1830 print_fill_statement (fill)
1831 lang_fill_statement_type * fill;
1832 {
1833 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1834 }
1835
1836 static void
1837 print_data_statement (data)
1838 lang_data_statement_type * data;
1839 {
1840 int i;
1841 bfd_vma addr;
1842 bfd_size_type size;
1843 const char *name;
1844
1845 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1846 print_space ();
1847
1848 addr = data->output_vma;
1849 if (data->output_section != NULL)
1850 addr += data->output_section->vma;
1851
1852 switch (data->type)
1853 {
1854 default:
1855 abort ();
1856 case BYTE:
1857 size = BYTE_SIZE;
1858 name = "BYTE";
1859 break;
1860 case SHORT:
1861 size = SHORT_SIZE;
1862 name = "SHORT";
1863 break;
1864 case LONG:
1865 size = LONG_SIZE;
1866 name = "LONG";
1867 break;
1868 case QUAD:
1869 size = QUAD_SIZE;
1870 name = "QUAD";
1871 break;
1872 case SQUAD:
1873 size = QUAD_SIZE;
1874 name = "SQUAD";
1875 break;
1876 }
1877
1878 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1879
1880 if (data->exp->type.node_class != etree_value)
1881 {
1882 print_space ();
1883 exp_print_tree (data->exp);
1884 }
1885
1886 print_nl ();
1887
1888 print_dot = addr + size;
1889 }
1890
1891 /* Print an address statement. These are generated by options like
1892 -Ttext. */
1893
1894 static void
1895 print_address_statement (address)
1896 lang_address_statement_type *address;
1897 {
1898 minfo (_("Address of section %s set to "), address->section_name);
1899 exp_print_tree (address->address);
1900 print_nl ();
1901 }
1902
1903 /* Print a reloc statement. */
1904
1905 static void
1906 print_reloc_statement (reloc)
1907 lang_reloc_statement_type *reloc;
1908 {
1909 int i;
1910 bfd_vma addr;
1911 bfd_size_type size;
1912
1913 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1914 print_space ();
1915
1916 addr = reloc->output_vma;
1917 if (reloc->output_section != NULL)
1918 addr += reloc->output_section->vma;
1919
1920 size = bfd_get_reloc_size (reloc->howto);
1921
1922 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
1923
1924 if (reloc->name != NULL)
1925 minfo ("%s+", reloc->name);
1926 else
1927 minfo ("%s+", reloc->section->name);
1928
1929 exp_print_tree (reloc->addend_exp);
1930
1931 print_nl ();
1932
1933 print_dot = addr + size;
1934 }
1935
1936 static void
1937 print_padding_statement (s)
1938 lang_padding_statement_type *s;
1939 {
1940 int len;
1941 bfd_vma addr;
1942
1943 minfo (" *fill*");
1944
1945 len = sizeof " *fill*" - 1;
1946 while (len < SECTION_NAME_MAP_LENGTH)
1947 {
1948 print_space ();
1949 ++len;
1950 }
1951
1952 addr = s->output_offset;
1953 if (s->output_section != NULL)
1954 addr += s->output_section->vma;
1955 minfo ("0x%V %W", addr, s->size);
1956
1957 if (s->fill != 0)
1958 minfo (" %u", s->fill);
1959
1960 print_nl ();
1961
1962 print_dot = addr + s->size;
1963 }
1964
1965 static void
1966 print_wild_statement (w, os)
1967 lang_wild_statement_type * w;
1968 lang_output_section_statement_type * os;
1969 {
1970 print_space ();
1971
1972 if (w->filenames_sorted)
1973 minfo ("SORT(");
1974 if (w->filename != NULL)
1975 minfo ("%s", w->filename);
1976 else
1977 minfo ("*");
1978 if (w->filenames_sorted)
1979 minfo (")");
1980
1981 minfo ("(");
1982 if (w->sections_sorted)
1983 minfo ("SORT(");
1984 if (w->section_name != NULL)
1985 minfo ("%s", w->section_name);
1986 else
1987 minfo ("*");
1988 if (w->sections_sorted)
1989 minfo (")");
1990 minfo (")");
1991
1992 print_nl ();
1993
1994 print_statement_list (w->children.head, os);
1995 }
1996
1997 /* Print a group statement. */
1998
1999 static void
2000 print_group (s, os)
2001 lang_group_statement_type *s;
2002 lang_output_section_statement_type *os;
2003 {
2004 fprintf (config.map_file, "START GROUP\n");
2005 print_statement_list (s->children.head, os);
2006 fprintf (config.map_file, "END GROUP\n");
2007 }
2008
2009 /* Print the list of statements in S.
2010 This can be called for any statement type. */
2011
2012 static void
2013 print_statement_list (s, os)
2014 lang_statement_union_type *s;
2015 lang_output_section_statement_type *os;
2016 {
2017 while (s != NULL)
2018 {
2019 print_statement (s, os);
2020 s = s->next;
2021 }
2022 }
2023
2024 /* Print the first statement in statement list S.
2025 This can be called for any statement type. */
2026
2027 static void
2028 print_statement (s, os)
2029 lang_statement_union_type *s;
2030 lang_output_section_statement_type *os;
2031 {
2032 switch (s->header.type)
2033 {
2034 default:
2035 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2036 FAIL ();
2037 break;
2038 case lang_constructors_statement_enum:
2039 if (constructor_list.head != NULL)
2040 {
2041 minfo (" CONSTRUCTORS\n");
2042 print_statement_list (constructor_list.head, os);
2043 }
2044 break;
2045 case lang_wild_statement_enum:
2046 print_wild_statement (&s->wild_statement, os);
2047 break;
2048 case lang_address_statement_enum:
2049 print_address_statement (&s->address_statement);
2050 break;
2051 case lang_object_symbols_statement_enum:
2052 minfo (" CREATE_OBJECT_SYMBOLS\n");
2053 break;
2054 case lang_fill_statement_enum:
2055 print_fill_statement (&s->fill_statement);
2056 break;
2057 case lang_data_statement_enum:
2058 print_data_statement (&s->data_statement);
2059 break;
2060 case lang_reloc_statement_enum:
2061 print_reloc_statement (&s->reloc_statement);
2062 break;
2063 case lang_input_section_enum:
2064 print_input_section (&s->input_section);
2065 break;
2066 case lang_padding_statement_enum:
2067 print_padding_statement (&s->padding_statement);
2068 break;
2069 case lang_output_section_statement_enum:
2070 print_output_section_statement (&s->output_section_statement);
2071 break;
2072 case lang_assignment_statement_enum:
2073 print_assignment (&s->assignment_statement, os);
2074 break;
2075 case lang_target_statement_enum:
2076 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2077 break;
2078 case lang_output_statement_enum:
2079 minfo ("OUTPUT(%s", s->output_statement.name);
2080 if (output_target != NULL)
2081 minfo (" %s", output_target);
2082 minfo (")\n");
2083 break;
2084 case lang_input_statement_enum:
2085 print_input_statement (&s->input_statement);
2086 break;
2087 case lang_group_statement_enum:
2088 print_group (&s->group_statement, os);
2089 break;
2090 case lang_afile_asection_pair_statement_enum:
2091 FAIL ();
2092 break;
2093 }
2094 }
2095
2096 static void
2097 print_statements ()
2098 {
2099 print_statement_list (statement_list.head, abs_output_section);
2100 }
2101
2102 /* Print the first N statements in statement list S to STDERR.
2103 If N == 0, nothing is printed.
2104 If N < 0, the entire list is printed.
2105 Intended to be called from GDB. */
2106
2107 void
2108 dprint_statement (s, n)
2109 lang_statement_union_type * s;
2110 int n;
2111 {
2112 FILE *map_save = config.map_file;
2113
2114 config.map_file = stderr;
2115
2116 if (n < 0)
2117 print_statement_list (s, abs_output_section);
2118 else
2119 {
2120 while (s && --n >= 0)
2121 {
2122 print_statement (s, abs_output_section);
2123 s = s->next;
2124 }
2125 }
2126
2127 config.map_file = map_save;
2128 }
2129
2130 static bfd_vma
2131 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2132 lang_statement_union_type ** this_ptr;
2133 fill_type fill;
2134 unsigned int power;
2135 asection * output_section_statement;
2136 bfd_vma dot;
2137 {
2138 /* Align this section first to the
2139 input sections requirement, then
2140 to the output section's requirement.
2141 If this alignment is > than any seen before,
2142 then record it too. Perform the alignment by
2143 inserting a magic 'padding' statement.
2144 */
2145
2146 unsigned int alignment_needed = align_power (dot, power) - dot;
2147
2148 if (alignment_needed != 0)
2149 {
2150 lang_statement_union_type *new =
2151 ((lang_statement_union_type *)
2152 stat_alloc (sizeof (lang_padding_statement_type)));
2153
2154 /* Link into existing chain */
2155 new->header.next = *this_ptr;
2156 *this_ptr = new;
2157 new->header.type = lang_padding_statement_enum;
2158 new->padding_statement.output_section = output_section_statement;
2159 new->padding_statement.output_offset =
2160 dot - output_section_statement->vma;
2161 new->padding_statement.fill = fill;
2162 new->padding_statement.size = alignment_needed;
2163 }
2164
2165
2166 /* Remember the most restrictive alignment */
2167 if (power > output_section_statement->alignment_power)
2168 {
2169 output_section_statement->alignment_power = power;
2170 }
2171 output_section_statement->_raw_size += alignment_needed;
2172 return alignment_needed + dot;
2173
2174 }
2175
2176 /* Work out how much this section will move the dot point */
2177 static bfd_vma
2178 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2179 lang_statement_union_type ** this_ptr;
2180 lang_output_section_statement_type * output_section_statement;
2181 fill_type fill;
2182 bfd_vma dot;
2183 boolean relax;
2184 {
2185 lang_input_section_type *is = &((*this_ptr)->input_section);
2186 asection *i = is->section;
2187
2188 if (is->ifile->just_syms_flag == false)
2189 {
2190 if (output_section_statement->subsection_alignment != -1)
2191 i->alignment_power =
2192 output_section_statement->subsection_alignment;
2193
2194 dot = insert_pad (this_ptr, fill, i->alignment_power,
2195 output_section_statement->bfd_section, dot);
2196
2197 /* Remember where in the output section this input section goes */
2198
2199 i->output_offset = dot - output_section_statement->bfd_section->vma;
2200
2201 /* Mark how big the output section must be to contain this now
2202 */
2203 if (i->_cooked_size != 0)
2204 dot += i->_cooked_size;
2205 else
2206 dot += i->_raw_size;
2207 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2208 }
2209 else
2210 {
2211 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2212 }
2213
2214 return dot;
2215 }
2216
2217 /* This variable indicates whether bfd_relax_section should be called
2218 again. */
2219
2220 static boolean relax_again;
2221
2222 /* Set the sizes for all the output sections. */
2223
2224 bfd_vma
2225 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2226 lang_statement_union_type * s;
2227 lang_output_section_statement_type * output_section_statement;
2228 lang_statement_union_type ** prev;
2229 fill_type fill;
2230 bfd_vma dot;
2231 boolean relax;
2232 {
2233 /* Size up the sections from their constituent parts */
2234 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2235 {
2236 switch (s->header.type)
2237 {
2238
2239 case lang_output_section_statement_enum:
2240 {
2241 bfd_vma after;
2242 lang_output_section_statement_type *os = &s->output_section_statement;
2243
2244 if (os->bfd_section == NULL)
2245 {
2246 /* This section was never actually created. */
2247 break;
2248 }
2249
2250 /* If this is a COFF shared library section, use the size and
2251 address from the input section. FIXME: This is COFF
2252 specific; it would be cleaner if there were some other way
2253 to do this, but nothing simple comes to mind. */
2254 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2255 {
2256 asection *input;
2257
2258 if (os->children.head == NULL
2259 || os->children.head->next != NULL
2260 || os->children.head->header.type != lang_input_section_enum)
2261 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2262 os->name);
2263
2264 input = os->children.head->input_section.section;
2265 bfd_set_section_vma (os->bfd_section->owner,
2266 os->bfd_section,
2267 bfd_section_vma (input->owner, input));
2268 os->bfd_section->_raw_size = input->_raw_size;
2269 break;
2270 }
2271
2272 if (bfd_is_abs_section (os->bfd_section))
2273 {
2274 /* No matter what happens, an abs section starts at zero */
2275 ASSERT (os->bfd_section->vma == 0);
2276 }
2277 else
2278 {
2279 if (os->addr_tree == (etree_type *) NULL)
2280 {
2281 /* No address specified for this section, get one
2282 from the region specification
2283 */
2284 if (os->region == (lang_memory_region_type *) NULL
2285 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2286 & (SEC_ALLOC | SEC_LOAD)) != 0)
2287 && os->region->name[0] == '*'
2288 && strcmp (os->region->name, "*default*") == 0))
2289 {
2290 os->region = lang_memory_default (os->bfd_section);
2291 }
2292
2293 /* If a loadable section is using the default memory
2294 region, and some non default memory regions were
2295 defined, issue a warning. */
2296 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2297 & (SEC_ALLOC | SEC_LOAD)) != 0
2298 && ! link_info.relocateable
2299 && strcmp (os->region->name, "*default*") == 0
2300 && lang_memory_region_list != NULL
2301 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2302 || lang_memory_region_list->next != NULL))
2303 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2304 bfd_get_section_name (output_bfd, os->bfd_section));
2305
2306 dot = os->region->current;
2307 if (os->section_alignment == -1)
2308 {
2309 bfd_vma olddot;
2310
2311 olddot = dot;
2312 dot = align_power (dot, os->bfd_section->alignment_power);
2313 if (dot != olddot && config.warn_section_align)
2314 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2315 os->name, (unsigned int) (dot - olddot));
2316 }
2317 }
2318 else
2319 {
2320 etree_value_type r;
2321
2322 r = exp_fold_tree (os->addr_tree,
2323 abs_output_section,
2324 lang_allocating_phase_enum,
2325 dot, &dot);
2326 if (r.valid == false)
2327 {
2328 einfo (_("%F%S: non constant address expression for section %s\n"),
2329 os->name);
2330 }
2331 dot = r.value + r.section->bfd_section->vma;
2332 }
2333 /* The section starts here */
2334 /* First, align to what the section needs */
2335
2336 if (os->section_alignment != -1)
2337 dot = align_power (dot, os->section_alignment);
2338
2339 bfd_set_section_vma (0, os->bfd_section, dot);
2340
2341 os->bfd_section->output_offset = 0;
2342 }
2343
2344 (void) lang_size_sections (os->children.head, os, &os->children.head,
2345 os->fill, dot, relax);
2346 /* Ignore the size of the input sections, use the vma and size to */
2347 /* align against */
2348
2349 after = ALIGN_N (os->bfd_section->vma +
2350 os->bfd_section->_raw_size,
2351 /* The coercion here is important, see ld.h. */
2352 (bfd_vma) os->block_value);
2353
2354 if (bfd_is_abs_section (os->bfd_section))
2355 ASSERT (after == os->bfd_section->vma);
2356 else
2357 os->bfd_section->_raw_size = after - os->bfd_section->vma;
2358 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2359 os->processed = true;
2360
2361 /* Replace into region ? */
2362 if (os->region != (lang_memory_region_type *) NULL)
2363 {
2364 os->region->current = dot;
2365 /* Make sure this isn't silly. */
2366 if (os->region->current < os->region->origin
2367 || (os->region->current - os->region->origin
2368 > os->region->length))
2369 {
2370 if (os->addr_tree != (etree_type *) NULL)
2371 {
2372 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2373 os->region->current,
2374 os->bfd_section->owner,
2375 os->bfd_section->name,
2376 os->region->name);
2377 }
2378 else
2379 {
2380 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2381 os->region->name,
2382 os->bfd_section->owner,
2383 os->bfd_section->name);
2384 }
2385 /* Reset the region pointer. */
2386 os->region->current = os->region->origin;
2387 }
2388 }
2389 }
2390 break;
2391
2392 case lang_constructors_statement_enum:
2393 dot = lang_size_sections (constructor_list.head,
2394 output_section_statement,
2395 &s->wild_statement.children.head,
2396 fill,
2397 dot, relax);
2398 break;
2399
2400 case lang_data_statement_enum:
2401 {
2402 unsigned int size = 0;
2403
2404 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2405 s->data_statement.output_section =
2406 output_section_statement->bfd_section;
2407
2408 switch (s->data_statement.type)
2409 {
2410 case QUAD:
2411 case SQUAD:
2412 size = QUAD_SIZE;
2413 break;
2414 case LONG:
2415 size = LONG_SIZE;
2416 break;
2417 case SHORT:
2418 size = SHORT_SIZE;
2419 break;
2420 case BYTE:
2421 size = BYTE_SIZE;
2422 break;
2423 }
2424
2425 dot += size;
2426 output_section_statement->bfd_section->_raw_size += size;
2427 /* The output section gets contents, and then we inspect for
2428 any flags set in the input script which override any ALLOC */
2429 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2430 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2431 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2432 }
2433 }
2434 break;
2435
2436 case lang_reloc_statement_enum:
2437 {
2438 int size;
2439
2440 s->reloc_statement.output_vma =
2441 dot - output_section_statement->bfd_section->vma;
2442 s->reloc_statement.output_section =
2443 output_section_statement->bfd_section;
2444 size = bfd_get_reloc_size (s->reloc_statement.howto);
2445 dot += size;
2446 output_section_statement->bfd_section->_raw_size += size;
2447 }
2448 break;
2449
2450 case lang_wild_statement_enum:
2451
2452 dot = lang_size_sections (s->wild_statement.children.head,
2453 output_section_statement,
2454 &s->wild_statement.children.head,
2455
2456 fill, dot, relax);
2457
2458 break;
2459
2460 case lang_object_symbols_statement_enum:
2461 link_info.create_object_symbols_section =
2462 output_section_statement->bfd_section;
2463 break;
2464 case lang_output_statement_enum:
2465 case lang_target_statement_enum:
2466 break;
2467 case lang_input_section_enum:
2468 {
2469 asection *i;
2470
2471 i = (*prev)->input_section.section;
2472 if (! relax)
2473 {
2474 if (i->_cooked_size == 0)
2475 i->_cooked_size = i->_raw_size;
2476 }
2477 else
2478 {
2479 boolean again;
2480
2481 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2482 einfo (_("%P%F: can't relax section: %E\n"));
2483 if (again)
2484 relax_again = true;
2485 }
2486 dot = size_input_section (prev,
2487 output_section_statement,
2488 output_section_statement->fill,
2489 dot, relax);
2490 }
2491 break;
2492 case lang_input_statement_enum:
2493 break;
2494 case lang_fill_statement_enum:
2495 s->fill_statement.output_section = output_section_statement->bfd_section;
2496
2497 fill = s->fill_statement.fill;
2498 break;
2499 case lang_assignment_statement_enum:
2500 {
2501 bfd_vma newdot = dot;
2502
2503 exp_fold_tree (s->assignment_statement.exp,
2504 output_section_statement,
2505 lang_allocating_phase_enum,
2506 dot,
2507 &newdot);
2508
2509 if (newdot != dot)
2510 {
2511 /* The assignment changed dot. Insert a pad. */
2512 if (output_section_statement == abs_output_section)
2513 {
2514 /* If we don't have an output section, then just adjust
2515 the default memory address. */
2516 lang_memory_region_lookup ("*default*")->current = newdot;
2517 }
2518 else if (!relax)
2519 {
2520 lang_statement_union_type *new =
2521 ((lang_statement_union_type *)
2522 stat_alloc (sizeof (lang_padding_statement_type)));
2523
2524 /* Link into existing chain */
2525 new->header.next = *prev;
2526 *prev = new;
2527 new->header.type = lang_padding_statement_enum;
2528 new->padding_statement.output_section =
2529 output_section_statement->bfd_section;
2530 new->padding_statement.output_offset =
2531 dot - output_section_statement->bfd_section->vma;
2532 new->padding_statement.fill = fill;
2533 new->padding_statement.size = newdot - dot;
2534 output_section_statement->bfd_section->_raw_size +=
2535 new->padding_statement.size;
2536 }
2537
2538 dot = newdot;
2539 }
2540 }
2541 break;
2542
2543 case lang_padding_statement_enum:
2544 /* If we are relaxing, and this is not the first pass, some
2545 padding statements may have been inserted during previous
2546 passes. We may have to move the padding statement to a new
2547 location if dot has a different value at this point in this
2548 pass than it did at this point in the previous pass. */
2549 s->padding_statement.output_offset =
2550 dot - output_section_statement->bfd_section->vma;
2551 dot += s->padding_statement.size;
2552 output_section_statement->bfd_section->_raw_size +=
2553 s->padding_statement.size;
2554 break;
2555
2556 case lang_group_statement_enum:
2557 dot = lang_size_sections (s->group_statement.children.head,
2558 output_section_statement,
2559 &s->group_statement.children.head,
2560 fill, dot, relax);
2561 break;
2562
2563 default:
2564 FAIL ();
2565 break;
2566
2567 /* This can only get here when relaxing is turned on */
2568
2569 case lang_address_statement_enum:
2570 break;
2571 }
2572 prev = &s->header.next;
2573 }
2574 return dot;
2575 }
2576
2577 bfd_vma
2578 lang_do_assignments (s, output_section_statement, fill, dot)
2579 lang_statement_union_type * s;
2580 lang_output_section_statement_type * output_section_statement;
2581 fill_type fill;
2582 bfd_vma dot;
2583 {
2584 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2585 {
2586 switch (s->header.type)
2587 {
2588 case lang_constructors_statement_enum:
2589 dot = lang_do_assignments (constructor_list.head,
2590 output_section_statement,
2591 fill,
2592 dot);
2593 break;
2594
2595 case lang_output_section_statement_enum:
2596 {
2597 lang_output_section_statement_type *os =
2598 &(s->output_section_statement);
2599
2600 if (os->bfd_section != NULL)
2601 {
2602 dot = os->bfd_section->vma;
2603 (void) lang_do_assignments (os->children.head, os,
2604 os->fill, dot);
2605 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2606 }
2607 if (os->load_base)
2608 {
2609 /* If nothing has been placed into the output section then
2610 it won't have a bfd_section. */
2611 if (os->bfd_section)
2612 {
2613 os->bfd_section->lma
2614 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2615 }
2616 }
2617 }
2618 break;
2619 case lang_wild_statement_enum:
2620
2621 dot = lang_do_assignments (s->wild_statement.children.head,
2622 output_section_statement,
2623 fill, dot);
2624
2625 break;
2626
2627 case lang_object_symbols_statement_enum:
2628 case lang_output_statement_enum:
2629 case lang_target_statement_enum:
2630 #if 0
2631 case lang_common_statement_enum:
2632 #endif
2633 break;
2634 case lang_data_statement_enum:
2635 {
2636 etree_value_type value;
2637
2638 value = exp_fold_tree (s->data_statement.exp,
2639 abs_output_section,
2640 lang_final_phase_enum, dot, &dot);
2641 s->data_statement.value = value.value;
2642 if (value.valid == false)
2643 einfo (_("%F%P: invalid data statement\n"));
2644 }
2645 switch (s->data_statement.type)
2646 {
2647 case QUAD:
2648 case SQUAD:
2649 dot += QUAD_SIZE;
2650 break;
2651 case LONG:
2652 dot += LONG_SIZE;
2653 break;
2654 case SHORT:
2655 dot += SHORT_SIZE;
2656 break;
2657 case BYTE:
2658 dot += BYTE_SIZE;
2659 break;
2660 }
2661 break;
2662
2663 case lang_reloc_statement_enum:
2664 {
2665 etree_value_type value;
2666
2667 value = exp_fold_tree (s->reloc_statement.addend_exp,
2668 abs_output_section,
2669 lang_final_phase_enum, dot, &dot);
2670 s->reloc_statement.addend_value = value.value;
2671 if (value.valid == false)
2672 einfo (_("%F%P: invalid reloc statement\n"));
2673 }
2674 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2675 break;
2676
2677 case lang_input_section_enum:
2678 {
2679 asection *in = s->input_section.section;
2680
2681 if (in->_cooked_size != 0)
2682 dot += in->_cooked_size;
2683 else
2684 dot += in->_raw_size;
2685 }
2686 break;
2687
2688 case lang_input_statement_enum:
2689 break;
2690 case lang_fill_statement_enum:
2691 fill = s->fill_statement.fill;
2692 break;
2693 case lang_assignment_statement_enum:
2694 {
2695 exp_fold_tree (s->assignment_statement.exp,
2696 output_section_statement,
2697 lang_final_phase_enum,
2698 dot,
2699 &dot);
2700 }
2701
2702 break;
2703 case lang_padding_statement_enum:
2704 dot += s->padding_statement.size;
2705 break;
2706
2707 case lang_group_statement_enum:
2708 dot = lang_do_assignments (s->group_statement.children.head,
2709 output_section_statement,
2710 fill, dot);
2711
2712 break;
2713
2714 default:
2715 FAIL ();
2716 break;
2717 case lang_address_statement_enum:
2718 break;
2719 }
2720
2721 }
2722 return dot;
2723 }
2724
2725 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2726 operator .startof. (section_name), it produces an undefined symbol
2727 .startof.section_name. Similarly, when it sees
2728 .sizeof. (section_name), it produces an undefined symbol
2729 .sizeof.section_name. For all the output sections, we look for
2730 such symbols, and set them to the correct value. */
2731
2732 static void
2733 lang_set_startof ()
2734 {
2735 asection *s;
2736
2737 if (link_info.relocateable)
2738 return;
2739
2740 for (s = output_bfd->sections; s != NULL; s = s->next)
2741 {
2742 const char *secname;
2743 char *buf;
2744 struct bfd_link_hash_entry *h;
2745
2746 secname = bfd_get_section_name (output_bfd, s);
2747 buf = xmalloc (10 + strlen (secname));
2748
2749 sprintf (buf, ".startof.%s", secname);
2750 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2751 if (h != NULL && h->type == bfd_link_hash_undefined)
2752 {
2753 h->type = bfd_link_hash_defined;
2754 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2755 h->u.def.section = bfd_abs_section_ptr;
2756 }
2757
2758 sprintf (buf, ".sizeof.%s", secname);
2759 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2760 if (h != NULL && h->type == bfd_link_hash_undefined)
2761 {
2762 h->type = bfd_link_hash_defined;
2763 if (s->_cooked_size != 0)
2764 h->u.def.value = s->_cooked_size;
2765 else
2766 h->u.def.value = s->_raw_size;
2767 h->u.def.section = bfd_abs_section_ptr;
2768 }
2769
2770 free (buf);
2771 }
2772 }
2773
2774 static void
2775 lang_finish ()
2776 {
2777 struct bfd_link_hash_entry *h;
2778 boolean warn;
2779
2780 if (link_info.relocateable || link_info.shared)
2781 warn = false;
2782 else
2783 warn = true;
2784
2785 if (entry_symbol == (char *) NULL)
2786 {
2787 /* No entry has been specified. Look for start, but don't warn
2788 if we don't find it. */
2789 entry_symbol = "start";
2790 warn = false;
2791 }
2792
2793 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2794 if (h != (struct bfd_link_hash_entry *) NULL
2795 && (h->type == bfd_link_hash_defined
2796 || h->type == bfd_link_hash_defweak)
2797 && h->u.def.section->output_section != NULL)
2798 {
2799 bfd_vma val;
2800
2801 val = (h->u.def.value
2802 + bfd_get_section_vma (output_bfd,
2803 h->u.def.section->output_section)
2804 + h->u.def.section->output_offset);
2805 if (! bfd_set_start_address (output_bfd, val))
2806 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
2807 }
2808 else
2809 {
2810 bfd_vma val;
2811 CONST char *send;
2812
2813 /* We couldn't find the entry symbol. Try parsing it as a
2814 number. */
2815 val = bfd_scan_vma (entry_symbol, &send, 0);
2816 if (*send == '\0')
2817 {
2818 if (! bfd_set_start_address (output_bfd, val))
2819 einfo (_("%P%F: can't set start address\n"));
2820 }
2821 else
2822 {
2823 asection *ts;
2824
2825 /* Can't find the entry symbol, and it's not a number. Use
2826 the first address in the text section. */
2827 ts = bfd_get_section_by_name (output_bfd, ".text");
2828 if (ts != (asection *) NULL)
2829 {
2830 if (warn)
2831 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
2832 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2833 if (! bfd_set_start_address (output_bfd,
2834 bfd_get_section_vma (output_bfd,
2835 ts)))
2836 einfo (_("%P%F: can't set start address\n"));
2837 }
2838 else
2839 {
2840 if (warn)
2841 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
2842 entry_symbol);
2843 }
2844 }
2845 }
2846 }
2847
2848 /* This is a small function used when we want to ignore errors from
2849 BFD. */
2850
2851 static void
2852 #ifdef ANSI_PROTOTYPES
2853 ignore_bfd_errors (const char *s, ...)
2854 #else
2855 ignore_bfd_errors (s)
2856 const char *s;
2857 #endif
2858 {
2859 /* Don't do anything. */
2860 }
2861
2862 /* Check that the architecture of all the input files is compatible
2863 with the output file. Also call the backend to let it do any
2864 other checking that is needed. */
2865
2866 static void
2867 lang_check ()
2868 {
2869 lang_statement_union_type *file;
2870 bfd *input_bfd;
2871 CONST bfd_arch_info_type *compatible;
2872
2873 for (file = file_chain.head;
2874 file != (lang_statement_union_type *) NULL;
2875 file = file->input_statement.next)
2876 {
2877 input_bfd = file->input_statement.the_bfd;
2878 compatible = bfd_arch_get_compatible (input_bfd,
2879 output_bfd);
2880 if (compatible == NULL)
2881 {
2882 if (command_line.warn_mismatch)
2883 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
2884 bfd_printable_name (input_bfd), input_bfd,
2885 bfd_printable_name (output_bfd));
2886 }
2887 else
2888 {
2889 bfd_error_handler_type pfn = NULL;
2890
2891 /* If we aren't supposed to warn about mismatched input
2892 files, temporarily set the BFD error handler to a
2893 function which will do nothing. We still want to call
2894 bfd_merge_private_bfd_data, since it may set up
2895 information which is needed in the output file. */
2896 if (! command_line.warn_mismatch)
2897 pfn = bfd_set_error_handler (ignore_bfd_errors);
2898 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2899 {
2900 if (command_line.warn_mismatch)
2901 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
2902 input_bfd);
2903 }
2904 if (! command_line.warn_mismatch)
2905 bfd_set_error_handler (pfn);
2906 }
2907 }
2908 }
2909
2910 /* Look through all the global common symbols and attach them to the
2911 correct section. The -sort-common command line switch may be used
2912 to roughly sort the entries by size. */
2913
2914 static void
2915 lang_common ()
2916 {
2917 if (link_info.relocateable
2918 && ! command_line.force_common_definition)
2919 return;
2920
2921 if (! config.sort_common)
2922 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2923 else
2924 {
2925 int power;
2926
2927 for (power = 4; power >= 0; power--)
2928 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2929 (PTR) &power);
2930 }
2931 }
2932
2933 /* Place one common symbol in the correct section. */
2934
2935 static boolean
2936 lang_one_common (h, info)
2937 struct bfd_link_hash_entry *h;
2938 PTR info;
2939 {
2940 unsigned int power_of_two;
2941 bfd_vma size;
2942 asection *section;
2943
2944 if (h->type != bfd_link_hash_common)
2945 return true;
2946
2947 size = h->u.c.size;
2948 power_of_two = h->u.c.p->alignment_power;
2949
2950 if (config.sort_common
2951 && power_of_two < (unsigned int) *(int *) info)
2952 return true;
2953
2954 section = h->u.c.p->section;
2955
2956 /* Increase the size of the section. */
2957 section->_cooked_size = ALIGN_N (section->_cooked_size,
2958 (bfd_size_type) (1 << power_of_two));
2959
2960 /* Adjust the alignment if necessary. */
2961 if (power_of_two > section->alignment_power)
2962 section->alignment_power = power_of_two;
2963
2964 /* Change the symbol from common to defined. */
2965 h->type = bfd_link_hash_defined;
2966 h->u.def.section = section;
2967 h->u.def.value = section->_cooked_size;
2968
2969 /* Increase the size of the section. */
2970 section->_cooked_size += size;
2971
2972 /* Make sure the section is allocated in memory, and make sure that
2973 it is no longer a common section. */
2974 section->flags |= SEC_ALLOC;
2975 section->flags &= ~ SEC_IS_COMMON;
2976
2977 if (config.map_file != NULL)
2978 {
2979 static boolean header_printed;
2980 int len;
2981 char *name;
2982 char buf[50];
2983
2984 if (! header_printed)
2985 {
2986 minfo (_("\nAllocating common symbols\n"));
2987 minfo (_("Common symbol size file\n\n"));
2988 header_printed = true;
2989 }
2990
2991 name = demangle (h->root.string);
2992 minfo ("%s", name);
2993 len = strlen (name);
2994 free (name);
2995
2996 if (len >= 19)
2997 {
2998 print_nl ();
2999 len = 0;
3000 }
3001 while (len < 20)
3002 {
3003 print_space ();
3004 ++len;
3005 }
3006
3007 minfo ("0x");
3008 if (size <= 0xffffffff)
3009 sprintf (buf, "%lx", (unsigned long) size);
3010 else
3011 sprintf_vma (buf, size);
3012 minfo ("%s", buf);
3013 len = strlen (buf);
3014
3015 while (len < 16)
3016 {
3017 print_space ();
3018 ++len;
3019 }
3020
3021 minfo ("%B\n", section->owner);
3022 }
3023
3024 return true;
3025 }
3026
3027 /*
3028 run through the input files and ensure that every input
3029 section has somewhere to go. If one is found without
3030 a destination then create an input request and place it
3031 into the statement tree.
3032 */
3033
3034 static void
3035 lang_place_orphans ()
3036 {
3037 lang_input_statement_type *file;
3038
3039 for (file = (lang_input_statement_type *) file_chain.head;
3040 file != (lang_input_statement_type *) NULL;
3041 file = (lang_input_statement_type *) file->next)
3042 {
3043 asection *s;
3044
3045 for (s = file->the_bfd->sections;
3046 s != (asection *) NULL;
3047 s = s->next)
3048 {
3049 if (s->output_section == (asection *) NULL)
3050 {
3051 /* This section of the file is not attatched, root
3052 around for a sensible place for it to go */
3053
3054 if (file->just_syms_flag)
3055 {
3056 /* We are only retrieving symbol values from this
3057 file. We want the symbols to act as though the
3058 values in the file are absolute. */
3059 s->output_section = bfd_abs_section_ptr;
3060 s->output_offset = s->vma;
3061 }
3062 else if (strcmp (s->name, "COMMON") == 0)
3063 {
3064 /* This is a lonely common section which must have
3065 come from an archive. We attach to the section
3066 with the wildcard. */
3067 if (! link_info.relocateable
3068 || command_line.force_common_definition)
3069 {
3070 if (default_common_section == NULL)
3071 {
3072 #if 0
3073 /* This message happens when using the
3074 svr3.ifile linker script, so I have
3075 disabled it. */
3076 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3077 #endif
3078 default_common_section =
3079 lang_output_section_statement_lookup (".bss");
3080
3081 }
3082 wild_doit (&default_common_section->children, s,
3083 default_common_section, file);
3084 }
3085 }
3086 else if (ldemul_place_orphan (file, s))
3087 ;
3088 else
3089 {
3090 lang_output_section_statement_type *os =
3091 lang_output_section_statement_lookup (s->name);
3092
3093 wild_doit (&os->children, s, os, file);
3094 }
3095 }
3096 }
3097 }
3098 }
3099
3100
3101 void
3102 lang_set_flags (ptr, flags)
3103 lang_memory_region_type *ptr;
3104 CONST char *flags;
3105 {
3106 flagword *ptr_flags = &ptr->flags;
3107
3108 ptr->flags = ptr->not_flags = 0;
3109 while (*flags)
3110 {
3111 switch (*flags)
3112 {
3113 case '!':
3114 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
3115 break;
3116
3117 case 'A': case 'a':
3118 *ptr_flags |= SEC_ALLOC;
3119 break;
3120
3121 case 'R': case 'r':
3122 *ptr_flags |= SEC_READONLY;
3123 break;
3124
3125 case 'W': case 'w':
3126 *ptr_flags |= SEC_DATA;
3127 break;
3128
3129 case 'X': case 'x':
3130 *ptr_flags |= SEC_CODE;
3131 break;
3132
3133 case 'L': case 'l':
3134 case 'I': case 'i':
3135 *ptr_flags |= SEC_LOAD;
3136 break;
3137
3138 default:
3139 einfo (_("%P%F: invalid syntax in flags\n"));
3140 break;
3141 }
3142 flags++;
3143 }
3144 }
3145
3146 /* Call a function on each input file. This function will be called
3147 on an archive, but not on the elements. */
3148
3149 void
3150 lang_for_each_input_file (func)
3151 void (*func) PARAMS ((lang_input_statement_type *));
3152 {
3153 lang_input_statement_type *f;
3154
3155 for (f = (lang_input_statement_type *) input_file_chain.head;
3156 f != NULL;
3157 f = (lang_input_statement_type *) f->next_real_file)
3158 func (f);
3159 }
3160
3161 /* Call a function on each file. The function will be called on all
3162 the elements of an archive which are included in the link, but will
3163 not be called on the archive file itself. */
3164
3165 void
3166 lang_for_each_file (func)
3167 void (*func) PARAMS ((lang_input_statement_type *));
3168 {
3169 lang_input_statement_type *f;
3170
3171 for (f = (lang_input_statement_type *) file_chain.head;
3172 f != (lang_input_statement_type *) NULL;
3173 f = (lang_input_statement_type *) f->next)
3174 {
3175 func (f);
3176 }
3177 }
3178
3179 #if 0
3180
3181 /* Not used. */
3182
3183 void
3184 lang_for_each_input_section (func)
3185 void (*func) PARAMS ((bfd * ab, asection * as));
3186 {
3187 lang_input_statement_type *f;
3188
3189 for (f = (lang_input_statement_type *) file_chain.head;
3190 f != (lang_input_statement_type *) NULL;
3191 f = (lang_input_statement_type *) f->next)
3192 {
3193 asection *s;
3194
3195 for (s = f->the_bfd->sections;
3196 s != (asection *) NULL;
3197 s = s->next)
3198 {
3199 func (f->the_bfd, s);
3200 }
3201 }
3202 }
3203
3204 #endif
3205
3206 void
3207 ldlang_add_file (entry)
3208 lang_input_statement_type * entry;
3209 {
3210 bfd **pp;
3211
3212 lang_statement_append (&file_chain,
3213 (lang_statement_union_type *) entry,
3214 &entry->next);
3215
3216 /* The BFD linker needs to have a list of all input BFDs involved in
3217 a link. */
3218 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3219 ASSERT (entry->the_bfd != output_bfd);
3220 for (pp = &link_info.input_bfds;
3221 *pp != (bfd *) NULL;
3222 pp = &(*pp)->link_next)
3223 ;
3224 *pp = entry->the_bfd;
3225 entry->the_bfd->usrdata = (PTR) entry;
3226 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3227
3228 /* Look through the sections and check for any which should not be
3229 included in the link. We need to do this now, so that we can
3230 notice when the backend linker tries to report multiple
3231 definition errors for symbols which are in sections we aren't
3232 going to link. FIXME: It might be better to entirely ignore
3233 symbols which are defined in sections which are going to be
3234 discarded. This would require modifying the backend linker for
3235 each backend which might set the SEC_LINK_ONCE flag. If we do
3236 this, we should probably handle SEC_EXCLUDE in the same way. */
3237
3238 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3239 }
3240
3241 void
3242 lang_add_output (name, from_script)
3243 CONST char *name;
3244 int from_script;
3245 {
3246 /* Make -o on command line override OUTPUT in script. */
3247 if (had_output_filename == false || !from_script)
3248 {
3249 output_filename = name;
3250 had_output_filename = true;
3251 }
3252 }
3253
3254
3255 static lang_output_section_statement_type *current_section;
3256
3257 static int
3258 topower (x)
3259 int x;
3260 {
3261 unsigned int i = 1;
3262 int l;
3263
3264 if (x < 0)
3265 return -1;
3266
3267 for (l = 0; l < 32; l++)
3268 {
3269 if (i >= (unsigned int) x)
3270 return l;
3271 i <<= 1;
3272 }
3273
3274 return 0;
3275 }
3276
3277 void
3278 lang_enter_output_section_statement (output_section_statement_name,
3279 address_exp, sectype, block_value,
3280 align, subalign, ebase)
3281 const char *output_section_statement_name;
3282 etree_type * address_exp;
3283 enum section_type sectype;
3284 bfd_vma block_value;
3285 etree_type *align;
3286 etree_type *subalign;
3287 etree_type *ebase;
3288 {
3289 lang_output_section_statement_type *os;
3290
3291 current_section =
3292 os =
3293 lang_output_section_statement_lookup (output_section_statement_name);
3294
3295
3296
3297 /* Add this statement to tree */
3298 /* add_statement(lang_output_section_statement_enum,
3299 output_section_statement);*/
3300 /* Make next things chain into subchain of this */
3301
3302 if (os->addr_tree ==
3303 (etree_type *) NULL)
3304 {
3305 os->addr_tree =
3306 address_exp;
3307 }
3308 os->sectype = sectype;
3309 if (sectype != noload_section)
3310 os->flags = SEC_NO_FLAGS;
3311 else
3312 os->flags = SEC_NEVER_LOAD;
3313 os->block_value = block_value ? block_value : 1;
3314 stat_ptr = &os->children;
3315
3316 os->subsection_alignment = topower(
3317 exp_get_value_int(subalign, -1,
3318 "subsection alignment",
3319 0));
3320 os->section_alignment = topower(
3321 exp_get_value_int(align, -1,
3322 "section alignment", 0));
3323
3324 os->load_base = ebase;
3325 }
3326
3327
3328 void
3329 lang_final ()
3330 {
3331 lang_output_statement_type *new =
3332 new_stat (lang_output_statement, stat_ptr);
3333
3334 new->name = output_filename;
3335 }
3336
3337 /* Reset the current counters in the regions */
3338 static void
3339 reset_memory_regions ()
3340 {
3341 lang_memory_region_type *p = lang_memory_region_list;
3342
3343 for (p = lang_memory_region_list;
3344 p != (lang_memory_region_type *) NULL;
3345 p = p->next)
3346 {
3347 p->old_length = (bfd_size_type) (p->current - p->origin);
3348 p->current = p->origin;
3349 }
3350 }
3351
3352 /* ??? At some point this traversal for GC should share code with the
3353 traversal for manipulating the output file. */
3354
3355 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3356 as needed. SECTION may be NULL, in which case it is a wild card. */
3357
3358 static void
3359 lang_gc_wild_section (ptr, section, file)
3360 lang_wild_statement_type *ptr;
3361 const char *section;
3362 lang_input_statement_type *file;
3363 {
3364 if (file->just_syms_flag == false)
3365 {
3366 register asection *s;
3367 boolean wildcard;
3368
3369 if (section == NULL)
3370 wildcard = false;
3371 else
3372 wildcard = wildcardp (section);
3373
3374 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3375 {
3376 boolean match;
3377
3378 if (section == NULL)
3379 match = true;
3380 else
3381 {
3382 const char *name;
3383
3384 name = bfd_get_section_name (file->the_bfd, s);
3385 if (wildcard)
3386 match = fnmatch (section, name, 0) == 0 ? true : false;
3387 else
3388 match = strcmp (section, name) == 0 ? true : false;
3389 }
3390
3391 if (match)
3392 {
3393 /* If the wild pattern was marked KEEP, the member sections
3394 should be as well. */
3395 if (ptr->keep_sections)
3396 s->flags |= SEC_KEEP;
3397 }
3398 }
3399 }
3400 }
3401
3402 /* Handle a wild statement for a single file F. */
3403
3404 static void
3405 lang_gc_wild_file (s, section, f)
3406 lang_wild_statement_type *s;
3407 const char *section;
3408 lang_input_statement_type *f;
3409 {
3410 if (f->the_bfd == NULL
3411 || ! bfd_check_format (f->the_bfd, bfd_archive))
3412 lang_gc_wild_section (s, section, f);
3413 else
3414 {
3415 bfd *member;
3416
3417 /* This is an archive file. We must map each member of the
3418 archive separately. */
3419 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
3420 while (member != NULL)
3421 {
3422 /* When lookup_name is called, it will call the add_symbols
3423 entry point for the archive. For each element of the
3424 archive which is included, BFD will call ldlang_add_file,
3425 which will set the usrdata field of the member to the
3426 lang_input_statement. */
3427 if (member->usrdata != NULL)
3428 {
3429 lang_gc_wild_section (s, section,
3430 (lang_input_statement_type *) member->usrdata);
3431 }
3432
3433 member = bfd_openr_next_archived_file (f->the_bfd, member);
3434 }
3435 }
3436 }
3437
3438 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3439 may be NULL, indicating that it is a wildcard. */
3440
3441 static void
3442 lang_gc_wild (s, section, file)
3443 lang_wild_statement_type *s;
3444 const char *section;
3445 const char *file;
3446 {
3447 lang_input_statement_type *f;
3448
3449 if (file == (char *) NULL)
3450 {
3451 /* Perform the iteration over all files in the list */
3452 for (f = (lang_input_statement_type *) file_chain.head;
3453 f != (lang_input_statement_type *) NULL;
3454 f = (lang_input_statement_type *) f->next)
3455 {
3456 lang_gc_wild_file (s, section, f);
3457 }
3458 }
3459 else if (wildcardp (file))
3460 {
3461 for (f = (lang_input_statement_type *) file_chain.head;
3462 f != (lang_input_statement_type *) NULL;
3463 f = (lang_input_statement_type *) f->next)
3464 {
3465 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
3466 lang_gc_wild_file (s, section, f);
3467 }
3468 }
3469 else
3470 {
3471 /* Perform the iteration over a single file */
3472 f = lookup_name (file);
3473 lang_gc_wild_file (s, section, f);
3474 }
3475 }
3476
3477 /* Iterate over sections marking them against GC. */
3478
3479 static void
3480 lang_gc_sections_1 (s)
3481 lang_statement_union_type * s;
3482 {
3483 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3484 {
3485 switch (s->header.type)
3486 {
3487 case lang_wild_statement_enum:
3488 lang_gc_wild (&s->wild_statement,
3489 s->wild_statement.section_name,
3490 s->wild_statement.filename);
3491 break;
3492 case lang_constructors_statement_enum:
3493 lang_gc_sections_1 (constructor_list.head);
3494 break;
3495 case lang_output_section_statement_enum:
3496 lang_gc_sections_1 (s->output_section_statement.children.head);
3497 break;
3498 case lang_group_statement_enum:
3499 lang_gc_sections_1 (s->group_statement.children.head);
3500 break;
3501 }
3502 }
3503 }
3504
3505 static void
3506 lang_gc_sections ()
3507 {
3508 struct bfd_link_hash_entry *h;
3509 ldlang_undef_chain_list_type *ulist, fake_list_start;
3510
3511 /* Keep all sections so marked in the link script. */
3512
3513 lang_gc_sections_1 (statement_list.head);
3514
3515 /* Keep all sections containing symbols undefined on the command-line.
3516 Handle the entry symbol at the same time. */
3517
3518 fake_list_start.next = ldlang_undef_chain_list_head;
3519 fake_list_start.name = entry_symbol;
3520
3521 for (ulist = &fake_list_start; ulist; ulist = ulist->next)
3522 {
3523 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3524 false, false, false);
3525
3526 if (h != (struct bfd_link_hash_entry *) NULL
3527 && (h->type == bfd_link_hash_defined
3528 || h->type == bfd_link_hash_defweak)
3529 && ! bfd_is_abs_section (h->u.def.section))
3530 {
3531 h->u.def.section->flags |= SEC_KEEP;
3532 }
3533 }
3534
3535 bfd_gc_sections (output_bfd, &link_info);
3536 }
3537
3538 void
3539 lang_process ()
3540 {
3541 lang_reasonable_defaults ();
3542 current_target = default_target;
3543
3544 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3545
3546 ldemul_create_output_section_statements ();
3547
3548 /* Add to the hash table all undefineds on the command line */
3549 lang_place_undefineds ();
3550
3551 /* Create a bfd for each input file */
3552 current_target = default_target;
3553 open_input_bfds (statement_list.head, false);
3554
3555 ldemul_after_open ();
3556
3557 /* Make sure that we're not mixing architectures. We call this
3558 after all the input files have been opened, but before we do any
3559 other processing, so that any operations merge_private_bfd_data
3560 does on the output file will be known during the rest of the
3561 link. */
3562 lang_check ();
3563
3564 /* Build all sets based on the information gathered from the input
3565 files. */
3566 ldctor_build_sets ();
3567
3568 /* Remove unreferenced sections if asked to. */
3569 if (command_line.gc_sections)
3570 lang_gc_sections ();
3571
3572 /* Size up the common data */
3573 lang_common ();
3574
3575 /* Run through the contours of the script and attach input sections
3576 to the correct output sections
3577 */
3578 map_input_to_output_sections (statement_list.head, (char *) NULL,
3579 (lang_output_section_statement_type *) NULL);
3580
3581
3582 /* Find any sections not attached explicitly and handle them */
3583 lang_place_orphans ();
3584
3585 ldemul_before_allocation ();
3586
3587 /* We must record the program headers before we try to fix the
3588 section positions, since they will affect SIZEOF_HEADERS. */
3589 lang_record_phdrs ();
3590
3591 /* Now run around and relax if we can */
3592 if (command_line.relax)
3593 {
3594 /* First time round is a trial run to get the 'worst case'
3595 addresses of the objects if there was no relaxing. */
3596 lang_size_sections (statement_list.head,
3597 abs_output_section,
3598 &(statement_list.head), 0, (bfd_vma) 0, false);
3599
3600 /* Keep relaxing until bfd_relax_section gives up. */
3601 do
3602 {
3603 reset_memory_regions ();
3604
3605 relax_again = false;
3606
3607 /* Do all the assignments with our current guesses as to
3608 section sizes. */
3609 lang_do_assignments (statement_list.head,
3610 abs_output_section,
3611 (fill_type) 0, (bfd_vma) 0);
3612
3613 /* Perform another relax pass - this time we know where the
3614 globals are, so can make better guess. */
3615 lang_size_sections (statement_list.head,
3616 abs_output_section,
3617 &(statement_list.head), 0, (bfd_vma) 0, true);
3618 }
3619 while (relax_again);
3620 }
3621 else
3622 {
3623 /* Size up the sections. */
3624 lang_size_sections (statement_list.head,
3625 abs_output_section,
3626 &(statement_list.head), 0, (bfd_vma) 0, false);
3627 }
3628
3629 /* See if anything special should be done now we know how big
3630 everything is. */
3631 ldemul_after_allocation ();
3632
3633 /* Fix any .startof. or .sizeof. symbols. */
3634 lang_set_startof ();
3635
3636 /* Do all the assignments, now that we know the final restingplaces
3637 of all the symbols */
3638
3639 lang_do_assignments (statement_list.head,
3640 abs_output_section,
3641 (fill_type) 0, (bfd_vma) 0);
3642
3643 /* Final stuffs */
3644
3645 ldemul_finish ();
3646 lang_finish ();
3647 }
3648
3649 /* EXPORTED TO YACC */
3650
3651 void
3652 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
3653 keep_sections)
3654 const char *const section_name;
3655 boolean sections_sorted;
3656 const char *const filename;
3657 boolean filenames_sorted;
3658 boolean keep_sections;
3659 {
3660 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3661 stat_ptr);
3662
3663 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3664 {
3665 placed_commons = true;
3666 }
3667 if (filename != (char *) NULL)
3668 {
3669 lang_has_input_file = true;
3670 }
3671 new->section_name = section_name;
3672 new->sections_sorted = sections_sorted;
3673 new->filename = filename;
3674 new->filenames_sorted = filenames_sorted;
3675 new->keep_sections = keep_sections;
3676 lang_list_init (&new->children);
3677 }
3678
3679 void
3680 lang_section_start (name, address)
3681 CONST char *name;
3682 etree_type * address;
3683 {
3684 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3685
3686 ad->section_name = name;
3687 ad->address = address;
3688 }
3689
3690 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3691 because of a -e argument on the command line, or zero if this is
3692 called by ENTRY in a linker script. Command line arguments take
3693 precedence. */
3694
3695 void
3696 lang_add_entry (name, cmdline)
3697 CONST char *name;
3698 boolean cmdline;
3699 {
3700 if (entry_symbol == NULL
3701 || cmdline
3702 || ! entry_from_cmdline)
3703 {
3704 entry_symbol = name;
3705 entry_from_cmdline = cmdline;
3706 }
3707 }
3708
3709 void
3710 lang_add_target (name)
3711 CONST char *name;
3712 {
3713 lang_target_statement_type *new = new_stat (lang_target_statement,
3714 stat_ptr);
3715
3716 new->target = name;
3717
3718 }
3719
3720 void
3721 lang_add_map (name)
3722 CONST char *name;
3723 {
3724 while (*name)
3725 {
3726 switch (*name)
3727 {
3728 case 'F':
3729 map_option_f = true;
3730 break;
3731 }
3732 name++;
3733 }
3734 }
3735
3736 void
3737 lang_add_fill (exp)
3738 int exp;
3739 {
3740 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3741 stat_ptr);
3742
3743 new->fill = exp;
3744 }
3745
3746 void
3747 lang_add_data (type, exp)
3748 int type;
3749 union etree_union *exp;
3750 {
3751
3752 lang_data_statement_type *new = new_stat (lang_data_statement,
3753 stat_ptr);
3754
3755 new->exp = exp;
3756 new->type = type;
3757
3758 }
3759
3760 /* Create a new reloc statement. RELOC is the BFD relocation type to
3761 generate. HOWTO is the corresponding howto structure (we could
3762 look this up, but the caller has already done so). SECTION is the
3763 section to generate a reloc against, or NAME is the name of the
3764 symbol to generate a reloc against. Exactly one of SECTION and
3765 NAME must be NULL. ADDEND is an expression for the addend. */
3766
3767 void
3768 lang_add_reloc (reloc, howto, section, name, addend)
3769 bfd_reloc_code_real_type reloc;
3770 reloc_howto_type *howto;
3771 asection *section;
3772 const char *name;
3773 union etree_union *addend;
3774 {
3775 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3776
3777 p->reloc = reloc;
3778 p->howto = howto;
3779 p->section = section;
3780 p->name = name;
3781 p->addend_exp = addend;
3782
3783 p->addend_value = 0;
3784 p->output_section = NULL;
3785 p->output_vma = 0;
3786 }
3787
3788 void
3789 lang_add_assignment (exp)
3790 etree_type * exp;
3791 {
3792 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3793 stat_ptr);
3794
3795 new->exp = exp;
3796 }
3797
3798 void
3799 lang_add_attribute (attribute)
3800 enum statement_enum attribute;
3801 {
3802 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3803 }
3804
3805 void
3806 lang_startup (name)
3807 CONST char *name;
3808 {
3809 if (startup_file != (char *) NULL)
3810 {
3811 einfo (_("%P%Fmultiple STARTUP files\n"));
3812 }
3813 first_file->filename = name;
3814 first_file->local_sym_name = name;
3815 first_file->real = true;
3816
3817 startup_file = name;
3818 }
3819
3820 void
3821 lang_float (maybe)
3822 boolean maybe;
3823 {
3824 lang_float_flag = maybe;
3825 }
3826
3827 void
3828 lang_leave_output_section_statement (fill, memspec, phdrs)
3829 bfd_vma fill;
3830 const char *memspec;
3831 struct lang_output_section_phdr_list *phdrs;
3832 {
3833 current_section->fill = fill;
3834 current_section->region = lang_memory_region_lookup (memspec);
3835 current_section->phdrs = phdrs;
3836 stat_ptr = &statement_list;
3837 }
3838
3839 /*
3840 Create an absolute symbol with the given name with the value of the
3841 address of first byte of the section named.
3842
3843 If the symbol already exists, then do nothing.
3844 */
3845 void
3846 lang_abs_symbol_at_beginning_of (secname, name)
3847 const char *secname;
3848 const char *name;
3849 {
3850 struct bfd_link_hash_entry *h;
3851
3852 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3853 if (h == (struct bfd_link_hash_entry *) NULL)
3854 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3855
3856 if (h->type == bfd_link_hash_new
3857 || h->type == bfd_link_hash_undefined)
3858 {
3859 asection *sec;
3860
3861 h->type = bfd_link_hash_defined;
3862
3863 sec = bfd_get_section_by_name (output_bfd, secname);
3864 if (sec == (asection *) NULL)
3865 h->u.def.value = 0;
3866 else
3867 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3868
3869 h->u.def.section = bfd_abs_section_ptr;
3870 }
3871 }
3872
3873 /*
3874 Create an absolute symbol with the given name with the value of the
3875 address of the first byte after the end of the section named.
3876
3877 If the symbol already exists, then do nothing.
3878 */
3879 void
3880 lang_abs_symbol_at_end_of (secname, name)
3881 const char *secname;
3882 const char *name;
3883 {
3884 struct bfd_link_hash_entry *h;
3885
3886 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3887 if (h == (struct bfd_link_hash_entry *) NULL)
3888 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3889
3890 if (h->type == bfd_link_hash_new
3891 || h->type == bfd_link_hash_undefined)
3892 {
3893 asection *sec;
3894
3895 h->type = bfd_link_hash_defined;
3896
3897 sec = bfd_get_section_by_name (output_bfd, secname);
3898 if (sec == (asection *) NULL)
3899 h->u.def.value = 0;
3900 else
3901 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3902 + bfd_section_size (output_bfd, sec));
3903
3904 h->u.def.section = bfd_abs_section_ptr;
3905 }
3906 }
3907
3908 void
3909 lang_statement_append (list, element, field)
3910 lang_statement_list_type * list;
3911 lang_statement_union_type * element;
3912 lang_statement_union_type ** field;
3913 {
3914 *(list->tail) = element;
3915 list->tail = field;
3916 }
3917
3918 /* Set the output format type. -oformat overrides scripts. */
3919
3920 void
3921 lang_add_output_format (format, big, little, from_script)
3922 const char *format;
3923 const char *big;
3924 const char *little;
3925 int from_script;
3926 {
3927 if (output_target == NULL || !from_script)
3928 {
3929 if (command_line.endian == ENDIAN_BIG
3930 && big != NULL)
3931 format = big;
3932 else if (command_line.endian == ENDIAN_LITTLE
3933 && little != NULL)
3934 format = little;
3935
3936 output_target = format;
3937 }
3938 }
3939
3940 /* Enter a group. This creates a new lang_group_statement, and sets
3941 stat_ptr to build new statements within the group. */
3942
3943 void
3944 lang_enter_group ()
3945 {
3946 lang_group_statement_type *g;
3947
3948 g = new_stat (lang_group_statement, stat_ptr);
3949 lang_list_init (&g->children);
3950 stat_ptr = &g->children;
3951 }
3952
3953 /* Leave a group. This just resets stat_ptr to start writing to the
3954 regular list of statements again. Note that this will not work if
3955 groups can occur inside anything else which can adjust stat_ptr,
3956 but currently they can't. */
3957
3958 void
3959 lang_leave_group ()
3960 {
3961 stat_ptr = &statement_list;
3962 }
3963
3964 /* Add a new program header. This is called for each entry in a PHDRS
3965 command in a linker script. */
3966
3967 void
3968 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
3969 const char *name;
3970 etree_type *type;
3971 boolean filehdr;
3972 boolean phdrs;
3973 etree_type *at;
3974 etree_type *flags;
3975 {
3976 struct lang_phdr *n, **pp;
3977
3978 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3979 n->next = NULL;
3980 n->name = name;
3981 n->type = exp_get_value_int (type, 0, "program header type",
3982 lang_final_phase_enum);
3983 n->filehdr = filehdr;
3984 n->phdrs = phdrs;
3985 n->at = at;
3986 n->flags = flags;
3987
3988 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3989 ;
3990 *pp = n;
3991 }
3992
3993 /* Record the program header information in the output BFD. FIXME: We
3994 should not be calling an ELF specific function here. */
3995
3996 static void
3997 lang_record_phdrs ()
3998 {
3999 unsigned int alc;
4000 asection **secs;
4001 struct lang_output_section_phdr_list *last;
4002 struct lang_phdr *l;
4003 lang_statement_union_type *u;
4004
4005 alc = 10;
4006 secs = (asection **) xmalloc (alc * sizeof (asection *));
4007 last = NULL;
4008 for (l = lang_phdr_list; l != NULL; l = l->next)
4009 {
4010 unsigned int c;
4011 flagword flags;
4012 bfd_vma at;
4013
4014 c = 0;
4015 for (u = lang_output_section_statement.head;
4016 u != NULL;
4017 u = u->output_section_statement.next)
4018 {
4019 lang_output_section_statement_type *os;
4020 struct lang_output_section_phdr_list *pl;
4021
4022 os = &u->output_section_statement;
4023
4024 pl = os->phdrs;
4025 if (pl != NULL)
4026 last = pl;
4027 else
4028 {
4029 if (os->sectype == noload_section
4030 || os->bfd_section == NULL
4031 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4032 continue;
4033 pl = last;
4034 }
4035
4036 if (os->bfd_section == NULL)
4037 continue;
4038
4039 for (; pl != NULL; pl = pl->next)
4040 {
4041 if (strcmp (pl->name, l->name) == 0)
4042 {
4043 if (c >= alc)
4044 {
4045 alc *= 2;
4046 secs = ((asection **)
4047 xrealloc (secs, alc * sizeof (asection *)));
4048 }
4049 secs[c] = os->bfd_section;
4050 ++c;
4051 pl->used = true;
4052 }
4053 }
4054 }
4055
4056 if (l->flags == NULL)
4057 flags = 0;
4058 else
4059 flags = exp_get_vma (l->flags, 0, "phdr flags",
4060 lang_final_phase_enum);
4061
4062 if (l->at == NULL)
4063 at = 0;
4064 else
4065 at = exp_get_vma (l->at, 0, "phdr load address",
4066 lang_final_phase_enum);
4067
4068 if (! bfd_record_phdr (output_bfd, l->type,
4069 l->flags == NULL ? false : true,
4070 flags,
4071 l->at == NULL ? false : true,
4072 at, l->filehdr, l->phdrs, c, secs))
4073 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4074 }
4075
4076 free (secs);
4077
4078 /* Make sure all the phdr assignments succeeded. */
4079 for (u = lang_output_section_statement.head;
4080 u != NULL;
4081 u = u->output_section_statement.next)
4082 {
4083 struct lang_output_section_phdr_list *pl;
4084
4085 if (u->output_section_statement.bfd_section == NULL)
4086 continue;
4087
4088 for (pl = u->output_section_statement.phdrs;
4089 pl != NULL;
4090 pl = pl->next)
4091 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4092 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4093 u->output_section_statement.name, pl->name);
4094 }
4095 }
4096
4097 /* Record a list of sections which may not be cross referenced. */
4098
4099 void
4100 lang_add_nocrossref (l)
4101 struct lang_nocrossref *l;
4102 {
4103 struct lang_nocrossrefs *n;
4104
4105 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4106 n->next = nocrossref_list;
4107 n->list = l;
4108 nocrossref_list = n;
4109
4110 /* Set notice_all so that we get informed about all symbols. */
4111 link_info.notice_all = true;
4112 }
4113 \f
4114 /* Overlay handling. We handle overlays with some static variables. */
4115
4116 /* The overlay virtual address. */
4117 static etree_type *overlay_vma;
4118
4119 /* The overlay load address. */
4120 static etree_type *overlay_lma;
4121
4122 /* Whether nocrossrefs is set for this overlay. */
4123 static int overlay_nocrossrefs;
4124
4125 /* An expression for the maximum section size seen so far. */
4126 static etree_type *overlay_max;
4127
4128 /* A list of all the sections in this overlay. */
4129
4130 struct overlay_list
4131 {
4132 struct overlay_list *next;
4133 lang_output_section_statement_type *os;
4134 };
4135
4136 static struct overlay_list *overlay_list;
4137
4138 /* Start handling an overlay. */
4139
4140 void
4141 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4142 etree_type *vma_expr;
4143 etree_type *lma_expr;
4144 int nocrossrefs;
4145 {
4146 /* The grammar should prevent nested overlays from occurring. */
4147 ASSERT (overlay_vma == NULL
4148 && overlay_lma == NULL
4149 && overlay_list == NULL
4150 && overlay_max == NULL);
4151
4152 overlay_vma = vma_expr;
4153 overlay_lma = lma_expr;
4154 overlay_nocrossrefs = nocrossrefs;
4155 }
4156
4157 /* Start a section in an overlay. We handle this by calling
4158 lang_enter_output_section_statement with the correct VMA and LMA. */
4159
4160 void
4161 lang_enter_overlay_section (name)
4162 const char *name;
4163 {
4164 struct overlay_list *n;
4165 etree_type *size;
4166
4167 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4168 0, 0, 0, overlay_lma);
4169
4170 /* If this is the first section, then base the VMA and LMA of future
4171 sections on this one. This will work correctly even if `.' is
4172 used in the addresses. */
4173 if (overlay_list == NULL)
4174 {
4175 overlay_vma = exp_nameop (ADDR, name);
4176 overlay_lma = exp_nameop (LOADADDR, name);
4177 }
4178
4179 /* Remember the section. */
4180 n = (struct overlay_list *) xmalloc (sizeof *n);
4181 n->os = current_section;
4182 n->next = overlay_list;
4183 overlay_list = n;
4184
4185 size = exp_nameop (SIZEOF, name);
4186
4187 /* Adjust the LMA for the next section. */
4188 overlay_lma = exp_binop ('+', overlay_lma, size);
4189
4190 /* Arrange to work out the maximum section end address. */
4191 if (overlay_max == NULL)
4192 overlay_max = size;
4193 else
4194 overlay_max = exp_binop (MAX, overlay_max, size);
4195 }
4196
4197 /* Finish a section in an overlay. There isn't any special to do
4198 here. */
4199
4200 void
4201 lang_leave_overlay_section (fill, phdrs)
4202 bfd_vma fill;
4203 struct lang_output_section_phdr_list *phdrs;
4204 {
4205 const char *name;
4206 char *clean, *s2;
4207 const char *s1;
4208 char *buf;
4209
4210 name = current_section->name;
4211
4212 lang_leave_output_section_statement (fill, "*default*", phdrs);
4213
4214 /* Define the magic symbols. */
4215
4216 clean = xmalloc (strlen (name) + 1);
4217 s2 = clean;
4218 for (s1 = name; *s1 != '\0'; s1++)
4219 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4220 *s2++ = *s1;
4221 *s2 = '\0';
4222
4223 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4224 sprintf (buf, "__load_start_%s", clean);
4225 lang_add_assignment (exp_assop ('=', buf,
4226 exp_nameop (LOADADDR, name)));
4227
4228 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4229 sprintf (buf, "__load_stop_%s", clean);
4230 lang_add_assignment (exp_assop ('=', buf,
4231 exp_binop ('+',
4232 exp_nameop (LOADADDR, name),
4233 exp_nameop (SIZEOF, name))));
4234
4235 free (clean);
4236 }
4237
4238 /* Finish an overlay. If there are any overlay wide settings, this
4239 looks through all the sections in the overlay and sets them. */
4240
4241 void
4242 lang_leave_overlay (fill, memspec, phdrs)
4243 bfd_vma fill;
4244 const char *memspec;
4245 struct lang_output_section_phdr_list *phdrs;
4246 {
4247 lang_memory_region_type *region;
4248 struct overlay_list *l;
4249 struct lang_nocrossref *nocrossref;
4250
4251 if (memspec == NULL)
4252 region = NULL;
4253 else
4254 region = lang_memory_region_lookup (memspec);
4255
4256 nocrossref = NULL;
4257
4258 l = overlay_list;
4259 while (l != NULL)
4260 {
4261 struct overlay_list *next;
4262
4263 if (fill != 0 && l->os->fill == 0)
4264 l->os->fill = fill;
4265 if (region != NULL && l->os->region == NULL)
4266 l->os->region = region;
4267 if (phdrs != NULL && l->os->phdrs == NULL)
4268 l->os->phdrs = phdrs;
4269
4270 if (overlay_nocrossrefs)
4271 {
4272 struct lang_nocrossref *nc;
4273
4274 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4275 nc->name = l->os->name;
4276 nc->next = nocrossref;
4277 nocrossref = nc;
4278 }
4279
4280 next = l->next;
4281 free (l);
4282 l = next;
4283 }
4284
4285 if (nocrossref != NULL)
4286 lang_add_nocrossref (nocrossref);
4287
4288 /* Update . for the end of the overlay. */
4289 lang_add_assignment (exp_assop ('=', ".",
4290 exp_binop ('+', overlay_vma, overlay_max)));
4291
4292 overlay_vma = NULL;
4293 overlay_lma = NULL;
4294 overlay_nocrossrefs = 0;
4295 overlay_list = NULL;
4296 overlay_max = NULL;
4297 }
4298 \f
4299 /* Version handling. This is only useful for ELF. */
4300
4301 /* This global variable holds the version tree that we build. */
4302
4303 struct bfd_elf_version_tree *lang_elf_version_info;
4304
4305 /* This is called for each variable name or match expression. */
4306
4307 struct bfd_elf_version_expr *
4308 lang_new_vers_regex (orig, new)
4309 struct bfd_elf_version_expr *orig;
4310 const char *new;
4311 {
4312 struct bfd_elf_version_expr *ret;
4313
4314 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4315 ret->next = orig;
4316 ret->match = new;
4317 return ret;
4318 }
4319
4320 /* This is called for each set of variable names and match
4321 expressions. */
4322
4323 struct bfd_elf_version_tree *
4324 lang_new_vers_node (globals, locals)
4325 struct bfd_elf_version_expr *globals;
4326 struct bfd_elf_version_expr *locals;
4327 {
4328 struct bfd_elf_version_tree *ret;
4329
4330 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4331 ret->next = NULL;
4332 ret->name = NULL;
4333 ret->vernum = 0;
4334 ret->globals = globals;
4335 ret->locals = locals;
4336 ret->deps = NULL;
4337 ret->name_indx = (unsigned int) -1;
4338 ret->used = 0;
4339 return ret;
4340 }
4341
4342 /* This static variable keeps track of version indices. */
4343
4344 static int version_index;
4345
4346 /* This is called when we know the name and dependencies of the
4347 version. */
4348
4349 void
4350 lang_register_vers_node (name, version, deps)
4351 const char *name;
4352 struct bfd_elf_version_tree *version;
4353 struct bfd_elf_version_deps *deps;
4354 {
4355 struct bfd_elf_version_tree *t, **pp;
4356 struct bfd_elf_version_expr *e1;
4357
4358 /* Make sure this node has a unique name. */
4359 for (t = lang_elf_version_info; t != NULL; t = t->next)
4360 if (strcmp (t->name, name) == 0)
4361 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4362
4363 /* Check the global and local match names, and make sure there
4364 aren't any duplicates. */
4365
4366 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4367 {
4368 for (t = lang_elf_version_info; t != NULL; t = t->next)
4369 {
4370 struct bfd_elf_version_expr *e2;
4371
4372 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4373 if (strcmp (e1->match, e2->match) == 0)
4374 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4375 e1->match);
4376 }
4377 }
4378
4379 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4380 {
4381 for (t = lang_elf_version_info; t != NULL; t = t->next)
4382 {
4383 struct bfd_elf_version_expr *e2;
4384
4385 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4386 if (strcmp (e1->match, e2->match) == 0)
4387 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4388 e1->match);
4389 }
4390 }
4391
4392 version->deps = deps;
4393 version->name = name;
4394 ++version_index;
4395 version->vernum = version_index;
4396
4397 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4398 ;
4399 *pp = version;
4400 }
4401
4402 /* This is called when we see a version dependency. */
4403
4404 struct bfd_elf_version_deps *
4405 lang_add_vers_depend (list, name)
4406 struct bfd_elf_version_deps *list;
4407 const char *name;
4408 {
4409 struct bfd_elf_version_deps *ret;
4410 struct bfd_elf_version_tree *t;
4411
4412 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4413 ret->next = list;
4414
4415 for (t = lang_elf_version_info; t != NULL; t = t->next)
4416 {
4417 if (strcmp (t->name, name) == 0)
4418 {
4419 ret->version_needed = t;
4420 return ret;
4421 }
4422 }
4423
4424 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
4425
4426 return ret;
4427 }