dummy commit before egcs merge
[deliverable/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 const char *fn, *ln;
997 boolean fa, la;
998 int i;
999
1000 /* The PE support for the .idata section as generated by
1001 dlltool assumes that files will be sorted by the name of
1002 the archive and then the name of the file within the
1003 archive. */
1004
1005 if (file->the_bfd != NULL
1006 && bfd_my_archive (file->the_bfd) != NULL)
1007 {
1008 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1009 fa = true;
1010 }
1011 else
1012 {
1013 fn = file->filename;
1014 fa = false;
1015 }
1016
1017 if (ls->ifile->the_bfd != NULL
1018 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1019 {
1020 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1021 la = true;
1022 }
1023 else
1024 {
1025 ln = ls->ifile->filename;
1026 la = false;
1027 }
1028
1029 i = strcmp (fn, ln);
1030 if (i > 0)
1031 continue;
1032 else if (i < 0)
1033 break;
1034
1035 if (fa || la)
1036 {
1037 if (fa)
1038 fn = file->filename;
1039 if (la)
1040 ln = ls->ifile->filename;
1041
1042 i = strcmp (fn, ln);
1043 if (i > 0)
1044 continue;
1045 else if (i < 0)
1046 break;
1047 }
1048 }
1049
1050 /* Here either the files are not sorted by name, or we are
1051 looking at the sections for this file. */
1052
1053 if (wild->sections_sorted)
1054 {
1055 if (strcmp (section_name,
1056 bfd_get_section_name (ls->ifile->the_bfd,
1057 ls->section))
1058 < 0)
1059 break;
1060 }
1061 }
1062
1063 return l;
1064 }
1065
1066 /* Expand a wild statement for a particular FILE. SECTION may be
1067 NULL, in which case it is a wild card. */
1068
1069 static void
1070 wild_section (ptr, section, file, output)
1071 lang_wild_statement_type *ptr;
1072 const char *section;
1073 lang_input_statement_type *file;
1074 lang_output_section_statement_type *output;
1075 {
1076 if (file->just_syms_flag == false)
1077 {
1078 register asection *s;
1079 boolean wildcard;
1080
1081 if (section == NULL)
1082 wildcard = false;
1083 else
1084 wildcard = wildcardp (section);
1085
1086 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1087 {
1088 boolean match;
1089
1090 /* Attach all sections named SECTION. If SECTION is NULL,
1091 then attach all sections.
1092
1093 Previously, if SECTION was NULL, this code did not call
1094 wild_doit if the SEC_IS_COMMON flag was set for the
1095 section. I did not understand that, and I took it out.
1096 --ian@cygnus.com. */
1097
1098 if (section == NULL)
1099 match = true;
1100 else
1101 {
1102 const char *name;
1103
1104 name = bfd_get_section_name (file->the_bfd, s);
1105 if (wildcard)
1106 match = fnmatch (section, name, 0) == 0 ? true : false;
1107 else
1108 match = strcmp (section, name) == 0 ? true : false;
1109 }
1110
1111 if (match)
1112 {
1113 lang_statement_union_type *before;
1114
1115 /* If the wild pattern was marked KEEP, the member sections
1116 should be as well. */
1117 if (ptr->keep_sections)
1118 s->flags |= SEC_KEEP;
1119
1120 before = wild_sort (ptr, file, s);
1121
1122 /* Here BEFORE points to the lang_input_section which
1123 should follow the one we are about to add. If BEFORE
1124 is NULL, then the section should just go at the end
1125 of the current list. */
1126
1127 if (before == NULL)
1128 wild_doit (&ptr->children, s, output, file);
1129 else
1130 {
1131 lang_statement_list_type list;
1132 lang_statement_union_type **pp;
1133
1134 lang_list_init (&list);
1135 wild_doit (&list, s, output, file);
1136
1137 /* If we are discarding the section, LIST.HEAD will
1138 be NULL. */
1139 if (list.head != NULL)
1140 {
1141 ASSERT (list.head->next == NULL);
1142
1143 for (pp = &ptr->children.head;
1144 *pp != before;
1145 pp = &(*pp)->next)
1146 ASSERT (*pp != NULL);
1147
1148 list.head->next = *pp;
1149 *pp = list.head;
1150 }
1151 }
1152 }
1153 }
1154 }
1155 }
1156
1157 /* This is passed a file name which must have been seen already and
1158 added to the statement tree. We will see if it has been opened
1159 already and had its symbols read. If not then we'll read it. */
1160
1161 static lang_input_statement_type *
1162 lookup_name (name)
1163 const char *name;
1164 {
1165 lang_input_statement_type *search;
1166
1167 for (search = (lang_input_statement_type *) input_file_chain.head;
1168 search != (lang_input_statement_type *) NULL;
1169 search = (lang_input_statement_type *) search->next_real_file)
1170 {
1171 if (search->filename == (char *) NULL && name == (char *) NULL)
1172 return search;
1173 if (search->filename != (char *) NULL
1174 && name != (char *) NULL
1175 && strcmp (search->filename, name) == 0)
1176 break;
1177 }
1178
1179 if (search == (lang_input_statement_type *) NULL)
1180 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1181 false);
1182
1183 /* If we have already added this file, or this file is not real
1184 (FIXME: can that ever actually happen?) or the name is NULL
1185 (FIXME: can that ever actually happen?) don't add this file. */
1186 if (search->loaded
1187 || ! search->real
1188 || search->filename == (const char *) NULL)
1189 return search;
1190
1191 load_symbols (search, (lang_statement_list_type *) NULL);
1192
1193 return search;
1194 }
1195
1196 /* Get the symbols for an input file. */
1197
1198 static void
1199 load_symbols (entry, place)
1200 lang_input_statement_type *entry;
1201 lang_statement_list_type *place;
1202 {
1203 char **matching;
1204
1205 if (entry->loaded)
1206 return;
1207
1208 ldfile_open_file (entry);
1209
1210 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1211 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1212 {
1213 bfd_error_type err;
1214 lang_statement_list_type *hold;
1215
1216 err = bfd_get_error ();
1217 if (err == bfd_error_file_ambiguously_recognized)
1218 {
1219 char **p;
1220
1221 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1222 einfo (_("%B: matching formats:"), entry->the_bfd);
1223 for (p = matching; *p != NULL; p++)
1224 einfo (" %s", *p);
1225 einfo ("%F\n");
1226 }
1227 else if (err != bfd_error_file_not_recognized
1228 || place == NULL)
1229 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1230
1231 bfd_close (entry->the_bfd);
1232 entry->the_bfd = NULL;
1233
1234 /* See if the emulation has some special knowledge. */
1235
1236 if (ldemul_unrecognized_file (entry))
1237 return;
1238
1239 /* Try to interpret the file as a linker script. */
1240
1241 ldfile_open_command_file (entry->filename);
1242
1243 hold = stat_ptr;
1244 stat_ptr = place;
1245
1246 ldfile_assumed_script = true;
1247 parser_input = input_script;
1248 yyparse ();
1249 ldfile_assumed_script = false;
1250
1251 stat_ptr = hold;
1252
1253 return;
1254 }
1255
1256 /* We don't call ldlang_add_file for an archive. Instead, the
1257 add_symbols entry point will call ldlang_add_file, via the
1258 add_archive_element callback, for each element of the archive
1259 which is used. */
1260 switch (bfd_get_format (entry->the_bfd))
1261 {
1262 default:
1263 break;
1264
1265 case bfd_object:
1266 ldlang_add_file (entry);
1267 if (trace_files || trace_file_tries)
1268 info_msg ("%I\n", entry);
1269 break;
1270
1271 case bfd_archive:
1272 if (entry->whole_archive)
1273 {
1274 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1275 (bfd *) NULL);
1276 while (member != NULL)
1277 {
1278 if (! bfd_check_format (member, bfd_object))
1279 einfo (_("%F%B: object %B in archive is not object\n"),
1280 entry->the_bfd, member);
1281 if (! ((*link_info.callbacks->add_archive_element)
1282 (&link_info, member, "--whole-archive")))
1283 abort ();
1284 if (! bfd_link_add_symbols (member, &link_info))
1285 einfo (_("%F%B: could not read symbols: %E\n"), member);
1286 member = bfd_openr_next_archived_file (entry->the_bfd,
1287 member);
1288 }
1289
1290 entry->loaded = true;
1291
1292 return;
1293 }
1294 }
1295
1296 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1297 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1298
1299 entry->loaded = true;
1300 }
1301
1302 /* Handle a wild statement for a single file F. */
1303
1304 static void
1305 wild_file (s, section, f, output)
1306 lang_wild_statement_type *s;
1307 const char *section;
1308 lang_input_statement_type *f;
1309 lang_output_section_statement_type *output;
1310 {
1311 if (f->the_bfd == NULL
1312 || ! bfd_check_format (f->the_bfd, bfd_archive))
1313 wild_section (s, section, f, output);
1314 else
1315 {
1316 bfd *member;
1317
1318 /* This is an archive file. We must map each member of the
1319 archive separately. */
1320 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1321 while (member != NULL)
1322 {
1323 /* When lookup_name is called, it will call the add_symbols
1324 entry point for the archive. For each element of the
1325 archive which is included, BFD will call ldlang_add_file,
1326 which will set the usrdata field of the member to the
1327 lang_input_statement. */
1328 if (member->usrdata != NULL)
1329 {
1330 wild_section (s, section,
1331 (lang_input_statement_type *) member->usrdata,
1332 output);
1333 }
1334
1335 member = bfd_openr_next_archived_file (f->the_bfd, member);
1336 }
1337 }
1338 }
1339
1340 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1341 indicating that it is a wildcard. Separate lang_input_section
1342 statements are created for each part of the expansion; they are
1343 added after the wild statement S. OUTPUT is the output section. */
1344
1345 static void
1346 wild (s, section, file, target, output)
1347 lang_wild_statement_type *s;
1348 const char *section;
1349 const char *file;
1350 const char *target;
1351 lang_output_section_statement_type *output;
1352 {
1353 lang_input_statement_type *f;
1354
1355 if (file == (char *) NULL)
1356 {
1357 /* Perform the iteration over all files in the list */
1358 for (f = (lang_input_statement_type *) file_chain.head;
1359 f != (lang_input_statement_type *) NULL;
1360 f = (lang_input_statement_type *) f->next)
1361 {
1362 wild_file (s, section, f, output);
1363 }
1364 }
1365 else if (wildcardp (file))
1366 {
1367 for (f = (lang_input_statement_type *) file_chain.head;
1368 f != (lang_input_statement_type *) NULL;
1369 f = (lang_input_statement_type *) f->next)
1370 {
1371 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1372 wild_file (s, section, f, output);
1373 }
1374 }
1375 else
1376 {
1377 /* Perform the iteration over a single file */
1378 f = lookup_name (file);
1379 wild_file (s, section, f, output);
1380 }
1381
1382 if (section != (char *) NULL
1383 && strcmp (section, "COMMON") == 0
1384 && default_common_section == NULL)
1385 {
1386 /* Remember the section that common is going to in case we later
1387 get something which doesn't know where to put it. */
1388 default_common_section = output;
1389 }
1390 }
1391
1392 /* Open the output file. */
1393
1394 static bfd *
1395 open_output (name)
1396 const char *name;
1397 {
1398 bfd *output;
1399
1400 if (output_target == (char *) NULL)
1401 {
1402 if (current_target != (char *) NULL)
1403 output_target = current_target;
1404 else
1405 output_target = default_target;
1406 }
1407 output = bfd_openw (name, output_target);
1408
1409 if (output == (bfd *) NULL)
1410 {
1411 if (bfd_get_error () == bfd_error_invalid_target)
1412 {
1413 einfo (_("%P%F: target %s not found\n"), output_target);
1414 }
1415 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1416 }
1417
1418 delete_output_file_on_failure = true;
1419
1420 /* output->flags |= D_PAGED;*/
1421
1422 if (! bfd_set_format (output, bfd_object))
1423 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1424 if (! bfd_set_arch_mach (output,
1425 ldfile_output_architecture,
1426 ldfile_output_machine))
1427 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1428
1429 link_info.hash = bfd_link_hash_table_create (output);
1430 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1431 einfo (_("%P%F: can not create link hash table: %E\n"));
1432
1433 bfd_set_gp_size (output, g_switch_value);
1434 return output;
1435 }
1436
1437
1438
1439
1440 static void
1441 ldlang_open_output (statement)
1442 lang_statement_union_type * statement;
1443 {
1444 switch (statement->header.type)
1445 {
1446 case lang_output_statement_enum:
1447 ASSERT (output_bfd == (bfd *) NULL);
1448 output_bfd = open_output (statement->output_statement.name);
1449 ldemul_set_output_arch ();
1450 if (config.magic_demand_paged && !link_info.relocateable)
1451 output_bfd->flags |= D_PAGED;
1452 else
1453 output_bfd->flags &= ~D_PAGED;
1454 if (config.text_read_only)
1455 output_bfd->flags |= WP_TEXT;
1456 else
1457 output_bfd->flags &= ~WP_TEXT;
1458 if (link_info.traditional_format)
1459 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1460 else
1461 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1462 break;
1463
1464 case lang_target_statement_enum:
1465 current_target = statement->target_statement.target;
1466 break;
1467 default:
1468 break;
1469 }
1470 }
1471
1472 /* Open all the input files. */
1473
1474 static void
1475 open_input_bfds (s, force)
1476 lang_statement_union_type *s;
1477 boolean force;
1478 {
1479 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1480 {
1481 switch (s->header.type)
1482 {
1483 case lang_constructors_statement_enum:
1484 open_input_bfds (constructor_list.head, force);
1485 break;
1486 case lang_output_section_statement_enum:
1487 open_input_bfds (s->output_section_statement.children.head, force);
1488 break;
1489 case lang_wild_statement_enum:
1490 /* Maybe we should load the file's symbols */
1491 if (s->wild_statement.filename
1492 && ! wildcardp (s->wild_statement.filename))
1493 (void) lookup_name (s->wild_statement.filename);
1494 open_input_bfds (s->wild_statement.children.head, force);
1495 break;
1496 case lang_group_statement_enum:
1497 {
1498 struct bfd_link_hash_entry *undefs;
1499
1500 /* We must continually search the entries in the group
1501 until no new symbols are added to the list of undefined
1502 symbols. */
1503
1504 do
1505 {
1506 undefs = link_info.hash->undefs_tail;
1507 open_input_bfds (s->group_statement.children.head, true);
1508 }
1509 while (undefs != link_info.hash->undefs_tail);
1510 }
1511 break;
1512 case lang_target_statement_enum:
1513 current_target = s->target_statement.target;
1514 break;
1515 case lang_input_statement_enum:
1516 if (s->input_statement.real == true)
1517 {
1518 lang_statement_list_type add;
1519
1520 s->input_statement.target = current_target;
1521
1522 /* If we are being called from within a group, and this
1523 is an archive which has already been searched, then
1524 force it to be researched. */
1525 if (force
1526 && s->input_statement.loaded
1527 && bfd_check_format (s->input_statement.the_bfd,
1528 bfd_archive))
1529 s->input_statement.loaded = false;
1530
1531 lang_list_init (&add);
1532
1533 load_symbols (&s->input_statement, &add);
1534
1535 if (add.head != NULL)
1536 {
1537 *add.tail = s->next;
1538 s->next = add.head;
1539 }
1540 }
1541 break;
1542 default:
1543 break;
1544 }
1545 }
1546 }
1547
1548 /* If there are [COMMONS] statements, put a wild one into the bss section */
1549
1550 static void
1551 lang_reasonable_defaults ()
1552 {
1553 #if 0
1554 lang_output_section_statement_lookup (".text");
1555 lang_output_section_statement_lookup (".data");
1556
1557 default_common_section =
1558 lang_output_section_statement_lookup (".bss");
1559
1560
1561 if (placed_commons == false)
1562 {
1563 lang_wild_statement_type *new =
1564 new_stat (lang_wild_statement,
1565 &default_common_section->children);
1566
1567 new->section_name = "COMMON";
1568 new->filename = (char *) NULL;
1569 lang_list_init (&new->children);
1570 }
1571 #endif
1572
1573 }
1574
1575 /*
1576 Add the supplied name to the symbol table as an undefined reference.
1577 Remove items from the chain as we open input bfds
1578 */
1579 typedef struct ldlang_undef_chain_list
1580 {
1581 struct ldlang_undef_chain_list *next;
1582 char *name;
1583 } ldlang_undef_chain_list_type;
1584
1585 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1586
1587 void
1588 ldlang_add_undef (name)
1589 CONST char *CONST name;
1590 {
1591 ldlang_undef_chain_list_type *new =
1592 ((ldlang_undef_chain_list_type *)
1593 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1594
1595 new->next = ldlang_undef_chain_list_head;
1596 ldlang_undef_chain_list_head = new;
1597
1598 new->name = buystring (name);
1599 }
1600
1601 /* Run through the list of undefineds created above and place them
1602 into the linker hash table as undefined symbols belonging to the
1603 script file.
1604 */
1605 static void
1606 lang_place_undefineds ()
1607 {
1608 ldlang_undef_chain_list_type *ptr;
1609
1610 for (ptr = ldlang_undef_chain_list_head;
1611 ptr != (ldlang_undef_chain_list_type *) NULL;
1612 ptr = ptr->next)
1613 {
1614 struct bfd_link_hash_entry *h;
1615
1616 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1617 if (h == (struct bfd_link_hash_entry *) NULL)
1618 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1619 if (h->type == bfd_link_hash_new)
1620 {
1621 h->type = bfd_link_hash_undefined;
1622 h->u.undef.abfd = NULL;
1623 bfd_link_add_undef (link_info.hash, h);
1624 }
1625 }
1626 }
1627
1628 /* Open input files and attatch to output sections */
1629 static void
1630 map_input_to_output_sections (s, target, output_section_statement)
1631 lang_statement_union_type * s;
1632 CONST char *target;
1633 lang_output_section_statement_type * output_section_statement;
1634 {
1635 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1636 {
1637 switch (s->header.type)
1638 {
1639
1640
1641 case lang_wild_statement_enum:
1642 wild (&s->wild_statement, s->wild_statement.section_name,
1643 s->wild_statement.filename, target,
1644 output_section_statement);
1645
1646 break;
1647 case lang_constructors_statement_enum:
1648 map_input_to_output_sections (constructor_list.head,
1649 target,
1650 output_section_statement);
1651 break;
1652 case lang_output_section_statement_enum:
1653 map_input_to_output_sections (s->output_section_statement.children.head,
1654 target,
1655 &s->output_section_statement);
1656 break;
1657 case lang_output_statement_enum:
1658 break;
1659 case lang_target_statement_enum:
1660 target = s->target_statement.target;
1661 break;
1662 case lang_group_statement_enum:
1663 map_input_to_output_sections (s->group_statement.children.head,
1664 target,
1665 output_section_statement);
1666 break;
1667 case lang_fill_statement_enum:
1668 case lang_input_section_enum:
1669 case lang_object_symbols_statement_enum:
1670 case lang_data_statement_enum:
1671 case lang_reloc_statement_enum:
1672 case lang_padding_statement_enum:
1673 case lang_input_statement_enum:
1674 if (output_section_statement != NULL
1675 && output_section_statement->bfd_section == NULL)
1676 init_os (output_section_statement);
1677 break;
1678 case lang_assignment_statement_enum:
1679 if (output_section_statement != NULL
1680 && output_section_statement->bfd_section == NULL)
1681 init_os (output_section_statement);
1682
1683 /* Make sure that any sections mentioned in the assignment
1684 are initialized. */
1685 exp_init_os (s->assignment_statement.exp);
1686 break;
1687 case lang_afile_asection_pair_statement_enum:
1688 FAIL ();
1689 break;
1690 case lang_address_statement_enum:
1691 /* Mark the specified section with the supplied address */
1692 {
1693 lang_output_section_statement_type *os =
1694 lang_output_section_statement_lookup
1695 (s->address_statement.section_name);
1696
1697 if (os->bfd_section == NULL)
1698 init_os (os);
1699 os->addr_tree = s->address_statement.address;
1700 }
1701 break;
1702 }
1703 }
1704 }
1705
1706 static void
1707 print_output_section_statement (output_section_statement)
1708 lang_output_section_statement_type * output_section_statement;
1709 {
1710 asection *section = output_section_statement->bfd_section;
1711 int len;
1712
1713 if (output_section_statement != abs_output_section)
1714 {
1715 minfo ("\n%s", output_section_statement->name);
1716
1717 if (section != NULL)
1718 {
1719 print_dot = section->vma;
1720
1721 len = strlen (output_section_statement->name);
1722 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1723 {
1724 print_nl ();
1725 len = 0;
1726 }
1727 while (len < SECTION_NAME_MAP_LENGTH)
1728 {
1729 print_space ();
1730 ++len;
1731 }
1732
1733 minfo ("0x%V %W", section->vma, section->_raw_size);
1734
1735 if (output_section_statement->load_base != NULL)
1736 {
1737 bfd_vma addr;
1738
1739 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1740 "load base", lang_final_phase_enum);
1741 minfo (_(" load address 0x%V"), addr);
1742 }
1743 }
1744
1745 print_nl ();
1746 }
1747
1748 print_statement_list (output_section_statement->children.head,
1749 output_section_statement);
1750 }
1751
1752 static void
1753 print_assignment (assignment, output_section)
1754 lang_assignment_statement_type * assignment;
1755 lang_output_section_statement_type * output_section;
1756 {
1757 int i;
1758 etree_value_type result;
1759
1760 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1761 print_space ();
1762
1763 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1764 lang_final_phase_enum, print_dot, &print_dot);
1765 if (result.valid)
1766 minfo ("0x%V", result.value + result.section->bfd_section->vma);
1767 else
1768 {
1769 minfo ("*undef* ");
1770 #ifdef BFD64
1771 minfo (" ");
1772 #endif
1773 }
1774
1775 minfo (" ");
1776
1777 exp_print_tree (assignment->exp);
1778
1779 print_nl ();
1780 }
1781
1782 static void
1783 print_input_statement (statm)
1784 lang_input_statement_type * statm;
1785 {
1786 if (statm->filename != (char *) NULL)
1787 {
1788 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1789 }
1790 }
1791
1792 /* Print all symbols defined in a particular section. This is called
1793 via bfd_link_hash_traverse. */
1794
1795 static boolean
1796 print_one_symbol (hash_entry, ptr)
1797 struct bfd_link_hash_entry *hash_entry;
1798 PTR ptr;
1799 {
1800 asection *sec = (asection *) ptr;
1801
1802 if ((hash_entry->type == bfd_link_hash_defined
1803 || hash_entry->type == bfd_link_hash_defweak)
1804 && sec == hash_entry->u.def.section)
1805 {
1806 int i;
1807
1808 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1809 print_space ();
1810 minfo ("0x%V ",
1811 (hash_entry->u.def.value
1812 + hash_entry->u.def.section->output_offset
1813 + hash_entry->u.def.section->output_section->vma));
1814
1815 minfo (" %T\n", hash_entry->root.string);
1816 }
1817
1818 return true;
1819 }
1820
1821 /* Print information about an input section to the map file. */
1822
1823 static void
1824 print_input_section (in)
1825 lang_input_section_type * in;
1826 {
1827 asection *i = in->section;
1828 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1829
1830 if (size != 0)
1831 {
1832 print_space ();
1833
1834 minfo ("%s", i->name);
1835
1836 if (i->output_section != NULL)
1837 {
1838 int len;
1839
1840 len = 1 + strlen (i->name);
1841 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1842 {
1843 print_nl ();
1844 len = 0;
1845 }
1846 while (len < SECTION_NAME_MAP_LENGTH)
1847 {
1848 print_space ();
1849 ++len;
1850 }
1851
1852 minfo ("0x%V %W %B\n",
1853 i->output_section->vma + i->output_offset, size,
1854 i->owner);
1855
1856 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1857 {
1858 len = SECTION_NAME_MAP_LENGTH + 3;
1859 #ifdef BFD64
1860 len += 16;
1861 #else
1862 len += 8;
1863 #endif
1864 while (len > 0)
1865 {
1866 print_space ();
1867 --len;
1868 }
1869
1870 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
1871 }
1872
1873 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1874
1875 print_dot = i->output_section->vma + i->output_offset + size;
1876 }
1877 }
1878 }
1879
1880 static void
1881 print_fill_statement (fill)
1882 lang_fill_statement_type * fill;
1883 {
1884 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1885 }
1886
1887 static void
1888 print_data_statement (data)
1889 lang_data_statement_type * data;
1890 {
1891 int i;
1892 bfd_vma addr;
1893 bfd_size_type size;
1894 const char *name;
1895
1896 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1897 print_space ();
1898
1899 addr = data->output_vma;
1900 if (data->output_section != NULL)
1901 addr += data->output_section->vma;
1902
1903 switch (data->type)
1904 {
1905 default:
1906 abort ();
1907 case BYTE:
1908 size = BYTE_SIZE;
1909 name = "BYTE";
1910 break;
1911 case SHORT:
1912 size = SHORT_SIZE;
1913 name = "SHORT";
1914 break;
1915 case LONG:
1916 size = LONG_SIZE;
1917 name = "LONG";
1918 break;
1919 case QUAD:
1920 size = QUAD_SIZE;
1921 name = "QUAD";
1922 break;
1923 case SQUAD:
1924 size = QUAD_SIZE;
1925 name = "SQUAD";
1926 break;
1927 }
1928
1929 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1930
1931 if (data->exp->type.node_class != etree_value)
1932 {
1933 print_space ();
1934 exp_print_tree (data->exp);
1935 }
1936
1937 print_nl ();
1938
1939 print_dot = addr + size;
1940 }
1941
1942 /* Print an address statement. These are generated by options like
1943 -Ttext. */
1944
1945 static void
1946 print_address_statement (address)
1947 lang_address_statement_type *address;
1948 {
1949 minfo (_("Address of section %s set to "), address->section_name);
1950 exp_print_tree (address->address);
1951 print_nl ();
1952 }
1953
1954 /* Print a reloc statement. */
1955
1956 static void
1957 print_reloc_statement (reloc)
1958 lang_reloc_statement_type *reloc;
1959 {
1960 int i;
1961 bfd_vma addr;
1962 bfd_size_type size;
1963
1964 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1965 print_space ();
1966
1967 addr = reloc->output_vma;
1968 if (reloc->output_section != NULL)
1969 addr += reloc->output_section->vma;
1970
1971 size = bfd_get_reloc_size (reloc->howto);
1972
1973 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
1974
1975 if (reloc->name != NULL)
1976 minfo ("%s+", reloc->name);
1977 else
1978 minfo ("%s+", reloc->section->name);
1979
1980 exp_print_tree (reloc->addend_exp);
1981
1982 print_nl ();
1983
1984 print_dot = addr + size;
1985 }
1986
1987 static void
1988 print_padding_statement (s)
1989 lang_padding_statement_type *s;
1990 {
1991 int len;
1992 bfd_vma addr;
1993
1994 minfo (" *fill*");
1995
1996 len = sizeof " *fill*" - 1;
1997 while (len < SECTION_NAME_MAP_LENGTH)
1998 {
1999 print_space ();
2000 ++len;
2001 }
2002
2003 addr = s->output_offset;
2004 if (s->output_section != NULL)
2005 addr += s->output_section->vma;
2006 minfo ("0x%V %W", addr, s->size);
2007
2008 if (s->fill != 0)
2009 minfo (" %u", s->fill);
2010
2011 print_nl ();
2012
2013 print_dot = addr + s->size;
2014 }
2015
2016 static void
2017 print_wild_statement (w, os)
2018 lang_wild_statement_type * w;
2019 lang_output_section_statement_type * os;
2020 {
2021 print_space ();
2022
2023 if (w->filenames_sorted)
2024 minfo ("SORT(");
2025 if (w->filename != NULL)
2026 minfo ("%s", w->filename);
2027 else
2028 minfo ("*");
2029 if (w->filenames_sorted)
2030 minfo (")");
2031
2032 minfo ("(");
2033 if (w->sections_sorted)
2034 minfo ("SORT(");
2035 if (w->section_name != NULL)
2036 minfo ("%s", w->section_name);
2037 else
2038 minfo ("*");
2039 if (w->sections_sorted)
2040 minfo (")");
2041 minfo (")");
2042
2043 print_nl ();
2044
2045 print_statement_list (w->children.head, os);
2046 }
2047
2048 /* Print a group statement. */
2049
2050 static void
2051 print_group (s, os)
2052 lang_group_statement_type *s;
2053 lang_output_section_statement_type *os;
2054 {
2055 fprintf (config.map_file, "START GROUP\n");
2056 print_statement_list (s->children.head, os);
2057 fprintf (config.map_file, "END GROUP\n");
2058 }
2059
2060 /* Print the list of statements in S.
2061 This can be called for any statement type. */
2062
2063 static void
2064 print_statement_list (s, os)
2065 lang_statement_union_type *s;
2066 lang_output_section_statement_type *os;
2067 {
2068 while (s != NULL)
2069 {
2070 print_statement (s, os);
2071 s = s->next;
2072 }
2073 }
2074
2075 /* Print the first statement in statement list S.
2076 This can be called for any statement type. */
2077
2078 static void
2079 print_statement (s, os)
2080 lang_statement_union_type *s;
2081 lang_output_section_statement_type *os;
2082 {
2083 switch (s->header.type)
2084 {
2085 default:
2086 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2087 FAIL ();
2088 break;
2089 case lang_constructors_statement_enum:
2090 if (constructor_list.head != NULL)
2091 {
2092 minfo (" CONSTRUCTORS\n");
2093 print_statement_list (constructor_list.head, os);
2094 }
2095 break;
2096 case lang_wild_statement_enum:
2097 print_wild_statement (&s->wild_statement, os);
2098 break;
2099 case lang_address_statement_enum:
2100 print_address_statement (&s->address_statement);
2101 break;
2102 case lang_object_symbols_statement_enum:
2103 minfo (" CREATE_OBJECT_SYMBOLS\n");
2104 break;
2105 case lang_fill_statement_enum:
2106 print_fill_statement (&s->fill_statement);
2107 break;
2108 case lang_data_statement_enum:
2109 print_data_statement (&s->data_statement);
2110 break;
2111 case lang_reloc_statement_enum:
2112 print_reloc_statement (&s->reloc_statement);
2113 break;
2114 case lang_input_section_enum:
2115 print_input_section (&s->input_section);
2116 break;
2117 case lang_padding_statement_enum:
2118 print_padding_statement (&s->padding_statement);
2119 break;
2120 case lang_output_section_statement_enum:
2121 print_output_section_statement (&s->output_section_statement);
2122 break;
2123 case lang_assignment_statement_enum:
2124 print_assignment (&s->assignment_statement, os);
2125 break;
2126 case lang_target_statement_enum:
2127 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2128 break;
2129 case lang_output_statement_enum:
2130 minfo ("OUTPUT(%s", s->output_statement.name);
2131 if (output_target != NULL)
2132 minfo (" %s", output_target);
2133 minfo (")\n");
2134 break;
2135 case lang_input_statement_enum:
2136 print_input_statement (&s->input_statement);
2137 break;
2138 case lang_group_statement_enum:
2139 print_group (&s->group_statement, os);
2140 break;
2141 case lang_afile_asection_pair_statement_enum:
2142 FAIL ();
2143 break;
2144 }
2145 }
2146
2147 static void
2148 print_statements ()
2149 {
2150 print_statement_list (statement_list.head, abs_output_section);
2151 }
2152
2153 /* Print the first N statements in statement list S to STDERR.
2154 If N == 0, nothing is printed.
2155 If N < 0, the entire list is printed.
2156 Intended to be called from GDB. */
2157
2158 void
2159 dprint_statement (s, n)
2160 lang_statement_union_type * s;
2161 int n;
2162 {
2163 FILE *map_save = config.map_file;
2164
2165 config.map_file = stderr;
2166
2167 if (n < 0)
2168 print_statement_list (s, abs_output_section);
2169 else
2170 {
2171 while (s && --n >= 0)
2172 {
2173 print_statement (s, abs_output_section);
2174 s = s->next;
2175 }
2176 }
2177
2178 config.map_file = map_save;
2179 }
2180
2181 static bfd_vma
2182 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2183 lang_statement_union_type ** this_ptr;
2184 fill_type fill;
2185 unsigned int power;
2186 asection * output_section_statement;
2187 bfd_vma dot;
2188 {
2189 /* Align this section first to the
2190 input sections requirement, then
2191 to the output section's requirement.
2192 If this alignment is > than any seen before,
2193 then record it too. Perform the alignment by
2194 inserting a magic 'padding' statement.
2195 */
2196
2197 unsigned int alignment_needed = align_power (dot, power) - dot;
2198
2199 if (alignment_needed != 0)
2200 {
2201 lang_statement_union_type *new =
2202 ((lang_statement_union_type *)
2203 stat_alloc (sizeof (lang_padding_statement_type)));
2204
2205 /* Link into existing chain */
2206 new->header.next = *this_ptr;
2207 *this_ptr = new;
2208 new->header.type = lang_padding_statement_enum;
2209 new->padding_statement.output_section = output_section_statement;
2210 new->padding_statement.output_offset =
2211 dot - output_section_statement->vma;
2212 new->padding_statement.fill = fill;
2213 new->padding_statement.size = alignment_needed;
2214 }
2215
2216
2217 /* Remember the most restrictive alignment */
2218 if (power > output_section_statement->alignment_power)
2219 {
2220 output_section_statement->alignment_power = power;
2221 }
2222 output_section_statement->_raw_size += alignment_needed;
2223 return alignment_needed + dot;
2224
2225 }
2226
2227 /* Work out how much this section will move the dot point */
2228 static bfd_vma
2229 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2230 lang_statement_union_type ** this_ptr;
2231 lang_output_section_statement_type * output_section_statement;
2232 fill_type fill;
2233 bfd_vma dot;
2234 boolean relax;
2235 {
2236 lang_input_section_type *is = &((*this_ptr)->input_section);
2237 asection *i = is->section;
2238
2239 if (is->ifile->just_syms_flag == false)
2240 {
2241 if (output_section_statement->subsection_alignment != -1)
2242 i->alignment_power =
2243 output_section_statement->subsection_alignment;
2244
2245 dot = insert_pad (this_ptr, fill, i->alignment_power,
2246 output_section_statement->bfd_section, dot);
2247
2248 /* Remember where in the output section this input section goes */
2249
2250 i->output_offset = dot - output_section_statement->bfd_section->vma;
2251
2252 /* Mark how big the output section must be to contain this now
2253 */
2254 if (i->_cooked_size != 0)
2255 dot += i->_cooked_size;
2256 else
2257 dot += i->_raw_size;
2258 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2259 }
2260 else
2261 {
2262 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2263 }
2264
2265 return dot;
2266 }
2267
2268 /* This variable indicates whether bfd_relax_section should be called
2269 again. */
2270
2271 static boolean relax_again;
2272
2273 /* Set the sizes for all the output sections. */
2274
2275 bfd_vma
2276 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2277 lang_statement_union_type * s;
2278 lang_output_section_statement_type * output_section_statement;
2279 lang_statement_union_type ** prev;
2280 fill_type fill;
2281 bfd_vma dot;
2282 boolean relax;
2283 {
2284 /* Size up the sections from their constituent parts */
2285 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2286 {
2287 switch (s->header.type)
2288 {
2289
2290 case lang_output_section_statement_enum:
2291 {
2292 bfd_vma after;
2293 lang_output_section_statement_type *os = &s->output_section_statement;
2294
2295 if (os->bfd_section == NULL)
2296 {
2297 /* This section was never actually created. */
2298 break;
2299 }
2300
2301 /* If this is a COFF shared library section, use the size and
2302 address from the input section. FIXME: This is COFF
2303 specific; it would be cleaner if there were some other way
2304 to do this, but nothing simple comes to mind. */
2305 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2306 {
2307 asection *input;
2308
2309 if (os->children.head == NULL
2310 || os->children.head->next != NULL
2311 || os->children.head->header.type != lang_input_section_enum)
2312 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2313 os->name);
2314
2315 input = os->children.head->input_section.section;
2316 bfd_set_section_vma (os->bfd_section->owner,
2317 os->bfd_section,
2318 bfd_section_vma (input->owner, input));
2319 os->bfd_section->_raw_size = input->_raw_size;
2320 break;
2321 }
2322
2323 if (bfd_is_abs_section (os->bfd_section))
2324 {
2325 /* No matter what happens, an abs section starts at zero */
2326 ASSERT (os->bfd_section->vma == 0);
2327 }
2328 else
2329 {
2330 if (os->addr_tree == (etree_type *) NULL)
2331 {
2332 /* No address specified for this section, get one
2333 from the region specification
2334 */
2335 if (os->region == (lang_memory_region_type *) NULL
2336 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2337 & (SEC_ALLOC | SEC_LOAD)) != 0)
2338 && os->region->name[0] == '*'
2339 && strcmp (os->region->name, "*default*") == 0))
2340 {
2341 os->region = lang_memory_default (os->bfd_section);
2342 }
2343
2344 /* If a loadable section is using the default memory
2345 region, and some non default memory regions were
2346 defined, issue a warning. */
2347 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2348 & (SEC_ALLOC | SEC_LOAD)) != 0
2349 && ! link_info.relocateable
2350 && strcmp (os->region->name, "*default*") == 0
2351 && lang_memory_region_list != NULL
2352 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2353 || lang_memory_region_list->next != NULL))
2354 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2355 bfd_get_section_name (output_bfd, os->bfd_section));
2356
2357 dot = os->region->current;
2358 if (os->section_alignment == -1)
2359 {
2360 bfd_vma olddot;
2361
2362 olddot = dot;
2363 dot = align_power (dot, os->bfd_section->alignment_power);
2364 if (dot != olddot && config.warn_section_align)
2365 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2366 os->name, (unsigned int) (dot - olddot));
2367 }
2368 }
2369 else
2370 {
2371 etree_value_type r;
2372
2373 r = exp_fold_tree (os->addr_tree,
2374 abs_output_section,
2375 lang_allocating_phase_enum,
2376 dot, &dot);
2377 if (r.valid == false)
2378 {
2379 einfo (_("%F%S: non constant address expression for section %s\n"),
2380 os->name);
2381 }
2382 dot = r.value + r.section->bfd_section->vma;
2383 }
2384 /* The section starts here */
2385 /* First, align to what the section needs */
2386
2387 if (os->section_alignment != -1)
2388 dot = align_power (dot, os->section_alignment);
2389
2390 bfd_set_section_vma (0, os->bfd_section, dot);
2391
2392 os->bfd_section->output_offset = 0;
2393 }
2394
2395 (void) lang_size_sections (os->children.head, os, &os->children.head,
2396 os->fill, dot, relax);
2397 /* Ignore the size of the input sections, use the vma and size to */
2398 /* align against */
2399
2400 after = ALIGN_N (os->bfd_section->vma +
2401 os->bfd_section->_raw_size,
2402 /* The coercion here is important, see ld.h. */
2403 (bfd_vma) os->block_value);
2404
2405 if (bfd_is_abs_section (os->bfd_section))
2406 ASSERT (after == os->bfd_section->vma);
2407 else
2408 os->bfd_section->_raw_size = after - os->bfd_section->vma;
2409 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2410 os->processed = true;
2411
2412 /* Replace into region ? */
2413 if (os->region != (lang_memory_region_type *) NULL)
2414 {
2415 os->region->current = dot;
2416 /* Make sure this isn't silly. */
2417 if (os->region->current < os->region->origin
2418 || (os->region->current - os->region->origin
2419 > os->region->length))
2420 {
2421 if (os->addr_tree != (etree_type *) NULL)
2422 {
2423 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2424 os->region->current,
2425 os->bfd_section->owner,
2426 os->bfd_section->name,
2427 os->region->name);
2428 }
2429 else
2430 {
2431 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2432 os->region->name,
2433 os->bfd_section->owner,
2434 os->bfd_section->name);
2435 }
2436 /* Reset the region pointer. */
2437 os->region->current = os->region->origin;
2438 }
2439 }
2440 }
2441 break;
2442
2443 case lang_constructors_statement_enum:
2444 dot = lang_size_sections (constructor_list.head,
2445 output_section_statement,
2446 &s->wild_statement.children.head,
2447 fill,
2448 dot, relax);
2449 break;
2450
2451 case lang_data_statement_enum:
2452 {
2453 unsigned int size = 0;
2454
2455 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2456 s->data_statement.output_section =
2457 output_section_statement->bfd_section;
2458
2459 switch (s->data_statement.type)
2460 {
2461 case QUAD:
2462 case SQUAD:
2463 size = QUAD_SIZE;
2464 break;
2465 case LONG:
2466 size = LONG_SIZE;
2467 break;
2468 case SHORT:
2469 size = SHORT_SIZE;
2470 break;
2471 case BYTE:
2472 size = BYTE_SIZE;
2473 break;
2474 }
2475
2476 dot += size;
2477 output_section_statement->bfd_section->_raw_size += size;
2478 /* The output section gets contents, and then we inspect for
2479 any flags set in the input script which override any ALLOC */
2480 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2481 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2482 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2483 }
2484 }
2485 break;
2486
2487 case lang_reloc_statement_enum:
2488 {
2489 int size;
2490
2491 s->reloc_statement.output_vma =
2492 dot - output_section_statement->bfd_section->vma;
2493 s->reloc_statement.output_section =
2494 output_section_statement->bfd_section;
2495 size = bfd_get_reloc_size (s->reloc_statement.howto);
2496 dot += size;
2497 output_section_statement->bfd_section->_raw_size += size;
2498 }
2499 break;
2500
2501 case lang_wild_statement_enum:
2502
2503 dot = lang_size_sections (s->wild_statement.children.head,
2504 output_section_statement,
2505 &s->wild_statement.children.head,
2506
2507 fill, dot, relax);
2508
2509 break;
2510
2511 case lang_object_symbols_statement_enum:
2512 link_info.create_object_symbols_section =
2513 output_section_statement->bfd_section;
2514 break;
2515 case lang_output_statement_enum:
2516 case lang_target_statement_enum:
2517 break;
2518 case lang_input_section_enum:
2519 {
2520 asection *i;
2521
2522 i = (*prev)->input_section.section;
2523 if (! relax)
2524 {
2525 if (i->_cooked_size == 0)
2526 i->_cooked_size = i->_raw_size;
2527 }
2528 else
2529 {
2530 boolean again;
2531
2532 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2533 einfo (_("%P%F: can't relax section: %E\n"));
2534 if (again)
2535 relax_again = true;
2536 }
2537 dot = size_input_section (prev,
2538 output_section_statement,
2539 output_section_statement->fill,
2540 dot, relax);
2541 }
2542 break;
2543 case lang_input_statement_enum:
2544 break;
2545 case lang_fill_statement_enum:
2546 s->fill_statement.output_section = output_section_statement->bfd_section;
2547
2548 fill = s->fill_statement.fill;
2549 break;
2550 case lang_assignment_statement_enum:
2551 {
2552 bfd_vma newdot = dot;
2553
2554 exp_fold_tree (s->assignment_statement.exp,
2555 output_section_statement,
2556 lang_allocating_phase_enum,
2557 dot,
2558 &newdot);
2559
2560 if (newdot != dot)
2561 {
2562 /* The assignment changed dot. Insert a pad. */
2563 if (output_section_statement == abs_output_section)
2564 {
2565 /* If we don't have an output section, then just adjust
2566 the default memory address. */
2567 lang_memory_region_lookup ("*default*")->current = newdot;
2568 }
2569 else if (!relax)
2570 {
2571 lang_statement_union_type *new =
2572 ((lang_statement_union_type *)
2573 stat_alloc (sizeof (lang_padding_statement_type)));
2574
2575 /* Link into existing chain */
2576 new->header.next = *prev;
2577 *prev = new;
2578 new->header.type = lang_padding_statement_enum;
2579 new->padding_statement.output_section =
2580 output_section_statement->bfd_section;
2581 new->padding_statement.output_offset =
2582 dot - output_section_statement->bfd_section->vma;
2583 new->padding_statement.fill = fill;
2584 new->padding_statement.size = newdot - dot;
2585 output_section_statement->bfd_section->_raw_size +=
2586 new->padding_statement.size;
2587 }
2588
2589 dot = newdot;
2590 }
2591 }
2592 break;
2593
2594 case lang_padding_statement_enum:
2595 /* If we are relaxing, and this is not the first pass, some
2596 padding statements may have been inserted during previous
2597 passes. We may have to move the padding statement to a new
2598 location if dot has a different value at this point in this
2599 pass than it did at this point in the previous pass. */
2600 s->padding_statement.output_offset =
2601 dot - output_section_statement->bfd_section->vma;
2602 dot += s->padding_statement.size;
2603 output_section_statement->bfd_section->_raw_size +=
2604 s->padding_statement.size;
2605 break;
2606
2607 case lang_group_statement_enum:
2608 dot = lang_size_sections (s->group_statement.children.head,
2609 output_section_statement,
2610 &s->group_statement.children.head,
2611 fill, dot, relax);
2612 break;
2613
2614 default:
2615 FAIL ();
2616 break;
2617
2618 /* This can only get here when relaxing is turned on */
2619
2620 case lang_address_statement_enum:
2621 break;
2622 }
2623 prev = &s->header.next;
2624 }
2625 return dot;
2626 }
2627
2628 bfd_vma
2629 lang_do_assignments (s, output_section_statement, fill, dot)
2630 lang_statement_union_type * s;
2631 lang_output_section_statement_type * output_section_statement;
2632 fill_type fill;
2633 bfd_vma dot;
2634 {
2635 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2636 {
2637 switch (s->header.type)
2638 {
2639 case lang_constructors_statement_enum:
2640 dot = lang_do_assignments (constructor_list.head,
2641 output_section_statement,
2642 fill,
2643 dot);
2644 break;
2645
2646 case lang_output_section_statement_enum:
2647 {
2648 lang_output_section_statement_type *os =
2649 &(s->output_section_statement);
2650
2651 if (os->bfd_section != NULL)
2652 {
2653 dot = os->bfd_section->vma;
2654 (void) lang_do_assignments (os->children.head, os,
2655 os->fill, dot);
2656 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2657 }
2658 if (os->load_base)
2659 {
2660 /* If nothing has been placed into the output section then
2661 it won't have a bfd_section. */
2662 if (os->bfd_section)
2663 {
2664 os->bfd_section->lma
2665 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2666 }
2667 }
2668 }
2669 break;
2670 case lang_wild_statement_enum:
2671
2672 dot = lang_do_assignments (s->wild_statement.children.head,
2673 output_section_statement,
2674 fill, dot);
2675
2676 break;
2677
2678 case lang_object_symbols_statement_enum:
2679 case lang_output_statement_enum:
2680 case lang_target_statement_enum:
2681 #if 0
2682 case lang_common_statement_enum:
2683 #endif
2684 break;
2685 case lang_data_statement_enum:
2686 {
2687 etree_value_type value;
2688
2689 value = exp_fold_tree (s->data_statement.exp,
2690 abs_output_section,
2691 lang_final_phase_enum, dot, &dot);
2692 s->data_statement.value = value.value;
2693 if (value.valid == false)
2694 einfo (_("%F%P: invalid data statement\n"));
2695 }
2696 switch (s->data_statement.type)
2697 {
2698 case QUAD:
2699 case SQUAD:
2700 dot += QUAD_SIZE;
2701 break;
2702 case LONG:
2703 dot += LONG_SIZE;
2704 break;
2705 case SHORT:
2706 dot += SHORT_SIZE;
2707 break;
2708 case BYTE:
2709 dot += BYTE_SIZE;
2710 break;
2711 }
2712 break;
2713
2714 case lang_reloc_statement_enum:
2715 {
2716 etree_value_type value;
2717
2718 value = exp_fold_tree (s->reloc_statement.addend_exp,
2719 abs_output_section,
2720 lang_final_phase_enum, dot, &dot);
2721 s->reloc_statement.addend_value = value.value;
2722 if (value.valid == false)
2723 einfo (_("%F%P: invalid reloc statement\n"));
2724 }
2725 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2726 break;
2727
2728 case lang_input_section_enum:
2729 {
2730 asection *in = s->input_section.section;
2731
2732 if (in->_cooked_size != 0)
2733 dot += in->_cooked_size;
2734 else
2735 dot += in->_raw_size;
2736 }
2737 break;
2738
2739 case lang_input_statement_enum:
2740 break;
2741 case lang_fill_statement_enum:
2742 fill = s->fill_statement.fill;
2743 break;
2744 case lang_assignment_statement_enum:
2745 {
2746 exp_fold_tree (s->assignment_statement.exp,
2747 output_section_statement,
2748 lang_final_phase_enum,
2749 dot,
2750 &dot);
2751 }
2752
2753 break;
2754 case lang_padding_statement_enum:
2755 dot += s->padding_statement.size;
2756 break;
2757
2758 case lang_group_statement_enum:
2759 dot = lang_do_assignments (s->group_statement.children.head,
2760 output_section_statement,
2761 fill, dot);
2762
2763 break;
2764
2765 default:
2766 FAIL ();
2767 break;
2768 case lang_address_statement_enum:
2769 break;
2770 }
2771
2772 }
2773 return dot;
2774 }
2775
2776 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2777 operator .startof. (section_name), it produces an undefined symbol
2778 .startof.section_name. Similarly, when it sees
2779 .sizeof. (section_name), it produces an undefined symbol
2780 .sizeof.section_name. For all the output sections, we look for
2781 such symbols, and set them to the correct value. */
2782
2783 static void
2784 lang_set_startof ()
2785 {
2786 asection *s;
2787
2788 if (link_info.relocateable)
2789 return;
2790
2791 for (s = output_bfd->sections; s != NULL; s = s->next)
2792 {
2793 const char *secname;
2794 char *buf;
2795 struct bfd_link_hash_entry *h;
2796
2797 secname = bfd_get_section_name (output_bfd, s);
2798 buf = xmalloc (10 + strlen (secname));
2799
2800 sprintf (buf, ".startof.%s", secname);
2801 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2802 if (h != NULL && h->type == bfd_link_hash_undefined)
2803 {
2804 h->type = bfd_link_hash_defined;
2805 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2806 h->u.def.section = bfd_abs_section_ptr;
2807 }
2808
2809 sprintf (buf, ".sizeof.%s", secname);
2810 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2811 if (h != NULL && h->type == bfd_link_hash_undefined)
2812 {
2813 h->type = bfd_link_hash_defined;
2814 if (s->_cooked_size != 0)
2815 h->u.def.value = s->_cooked_size;
2816 else
2817 h->u.def.value = s->_raw_size;
2818 h->u.def.section = bfd_abs_section_ptr;
2819 }
2820
2821 free (buf);
2822 }
2823 }
2824
2825 static void
2826 lang_finish ()
2827 {
2828 struct bfd_link_hash_entry *h;
2829 boolean warn;
2830
2831 if (link_info.relocateable || link_info.shared)
2832 warn = false;
2833 else
2834 warn = true;
2835
2836 if (entry_symbol == (char *) NULL)
2837 {
2838 /* No entry has been specified. Look for start, but don't warn
2839 if we don't find it. */
2840 entry_symbol = "start";
2841 warn = false;
2842 }
2843
2844 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2845 if (h != (struct bfd_link_hash_entry *) NULL
2846 && (h->type == bfd_link_hash_defined
2847 || h->type == bfd_link_hash_defweak)
2848 && h->u.def.section->output_section != NULL)
2849 {
2850 bfd_vma val;
2851
2852 val = (h->u.def.value
2853 + bfd_get_section_vma (output_bfd,
2854 h->u.def.section->output_section)
2855 + h->u.def.section->output_offset);
2856 if (! bfd_set_start_address (output_bfd, val))
2857 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
2858 }
2859 else
2860 {
2861 bfd_vma val;
2862 CONST char *send;
2863
2864 /* We couldn't find the entry symbol. Try parsing it as a
2865 number. */
2866 val = bfd_scan_vma (entry_symbol, &send, 0);
2867 if (*send == '\0')
2868 {
2869 if (! bfd_set_start_address (output_bfd, val))
2870 einfo (_("%P%F: can't set start address\n"));
2871 }
2872 else
2873 {
2874 asection *ts;
2875
2876 /* Can't find the entry symbol, and it's not a number. Use
2877 the first address in the text section. */
2878 ts = bfd_get_section_by_name (output_bfd, ".text");
2879 if (ts != (asection *) NULL)
2880 {
2881 if (warn)
2882 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
2883 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2884 if (! bfd_set_start_address (output_bfd,
2885 bfd_get_section_vma (output_bfd,
2886 ts)))
2887 einfo (_("%P%F: can't set start address\n"));
2888 }
2889 else
2890 {
2891 if (warn)
2892 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
2893 entry_symbol);
2894 }
2895 }
2896 }
2897 }
2898
2899 /* This is a small function used when we want to ignore errors from
2900 BFD. */
2901
2902 static void
2903 #ifdef ANSI_PROTOTYPES
2904 ignore_bfd_errors (const char *s, ...)
2905 #else
2906 ignore_bfd_errors (s)
2907 const char *s;
2908 #endif
2909 {
2910 /* Don't do anything. */
2911 }
2912
2913 /* Check that the architecture of all the input files is compatible
2914 with the output file. Also call the backend to let it do any
2915 other checking that is needed. */
2916
2917 static void
2918 lang_check ()
2919 {
2920 lang_statement_union_type *file;
2921 bfd *input_bfd;
2922 CONST bfd_arch_info_type *compatible;
2923
2924 for (file = file_chain.head;
2925 file != (lang_statement_union_type *) NULL;
2926 file = file->input_statement.next)
2927 {
2928 input_bfd = file->input_statement.the_bfd;
2929 compatible = bfd_arch_get_compatible (input_bfd,
2930 output_bfd);
2931 if (compatible == NULL)
2932 {
2933 if (command_line.warn_mismatch)
2934 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
2935 bfd_printable_name (input_bfd), input_bfd,
2936 bfd_printable_name (output_bfd));
2937 }
2938 else
2939 {
2940 bfd_error_handler_type pfn = NULL;
2941
2942 /* If we aren't supposed to warn about mismatched input
2943 files, temporarily set the BFD error handler to a
2944 function which will do nothing. We still want to call
2945 bfd_merge_private_bfd_data, since it may set up
2946 information which is needed in the output file. */
2947 if (! command_line.warn_mismatch)
2948 pfn = bfd_set_error_handler (ignore_bfd_errors);
2949 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2950 {
2951 if (command_line.warn_mismatch)
2952 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
2953 input_bfd);
2954 }
2955 if (! command_line.warn_mismatch)
2956 bfd_set_error_handler (pfn);
2957 }
2958 }
2959 }
2960
2961 /* Look through all the global common symbols and attach them to the
2962 correct section. The -sort-common command line switch may be used
2963 to roughly sort the entries by size. */
2964
2965 static void
2966 lang_common ()
2967 {
2968 if (link_info.relocateable
2969 && ! command_line.force_common_definition)
2970 return;
2971
2972 if (! config.sort_common)
2973 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2974 else
2975 {
2976 int power;
2977
2978 for (power = 4; power >= 0; power--)
2979 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2980 (PTR) &power);
2981 }
2982 }
2983
2984 /* Place one common symbol in the correct section. */
2985
2986 static boolean
2987 lang_one_common (h, info)
2988 struct bfd_link_hash_entry *h;
2989 PTR info;
2990 {
2991 unsigned int power_of_two;
2992 bfd_vma size;
2993 asection *section;
2994
2995 if (h->type != bfd_link_hash_common)
2996 return true;
2997
2998 size = h->u.c.size;
2999 power_of_two = h->u.c.p->alignment_power;
3000
3001 if (config.sort_common
3002 && power_of_two < (unsigned int) *(int *) info)
3003 return true;
3004
3005 section = h->u.c.p->section;
3006
3007 /* Increase the size of the section. */
3008 section->_cooked_size = ALIGN_N (section->_cooked_size,
3009 (bfd_size_type) (1 << power_of_two));
3010
3011 /* Adjust the alignment if necessary. */
3012 if (power_of_two > section->alignment_power)
3013 section->alignment_power = power_of_two;
3014
3015 /* Change the symbol from common to defined. */
3016 h->type = bfd_link_hash_defined;
3017 h->u.def.section = section;
3018 h->u.def.value = section->_cooked_size;
3019
3020 /* Increase the size of the section. */
3021 section->_cooked_size += size;
3022
3023 /* Make sure the section is allocated in memory, and make sure that
3024 it is no longer a common section. */
3025 section->flags |= SEC_ALLOC;
3026 section->flags &= ~ SEC_IS_COMMON;
3027
3028 if (config.map_file != NULL)
3029 {
3030 static boolean header_printed;
3031 int len;
3032 char *name;
3033 char buf[50];
3034
3035 if (! header_printed)
3036 {
3037 minfo (_("\nAllocating common symbols\n"));
3038 minfo (_("Common symbol size file\n\n"));
3039 header_printed = true;
3040 }
3041
3042 name = demangle (h->root.string);
3043 minfo ("%s", name);
3044 len = strlen (name);
3045 free (name);
3046
3047 if (len >= 19)
3048 {
3049 print_nl ();
3050 len = 0;
3051 }
3052 while (len < 20)
3053 {
3054 print_space ();
3055 ++len;
3056 }
3057
3058 minfo ("0x");
3059 if (size <= 0xffffffff)
3060 sprintf (buf, "%lx", (unsigned long) size);
3061 else
3062 sprintf_vma (buf, size);
3063 minfo ("%s", buf);
3064 len = strlen (buf);
3065
3066 while (len < 16)
3067 {
3068 print_space ();
3069 ++len;
3070 }
3071
3072 minfo ("%B\n", section->owner);
3073 }
3074
3075 return true;
3076 }
3077
3078 /*
3079 run through the input files and ensure that every input
3080 section has somewhere to go. If one is found without
3081 a destination then create an input request and place it
3082 into the statement tree.
3083 */
3084
3085 static void
3086 lang_place_orphans ()
3087 {
3088 lang_input_statement_type *file;
3089
3090 for (file = (lang_input_statement_type *) file_chain.head;
3091 file != (lang_input_statement_type *) NULL;
3092 file = (lang_input_statement_type *) file->next)
3093 {
3094 asection *s;
3095
3096 for (s = file->the_bfd->sections;
3097 s != (asection *) NULL;
3098 s = s->next)
3099 {
3100 if (s->output_section == (asection *) NULL)
3101 {
3102 /* This section of the file is not attatched, root
3103 around for a sensible place for it to go */
3104
3105 if (file->just_syms_flag)
3106 {
3107 /* We are only retrieving symbol values from this
3108 file. We want the symbols to act as though the
3109 values in the file are absolute. */
3110 s->output_section = bfd_abs_section_ptr;
3111 s->output_offset = s->vma;
3112 }
3113 else if (strcmp (s->name, "COMMON") == 0)
3114 {
3115 /* This is a lonely common section which must have
3116 come from an archive. We attach to the section
3117 with the wildcard. */
3118 if (! link_info.relocateable
3119 || command_line.force_common_definition)
3120 {
3121 if (default_common_section == NULL)
3122 {
3123 #if 0
3124 /* This message happens when using the
3125 svr3.ifile linker script, so I have
3126 disabled it. */
3127 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3128 #endif
3129 default_common_section =
3130 lang_output_section_statement_lookup (".bss");
3131
3132 }
3133 wild_doit (&default_common_section->children, s,
3134 default_common_section, file);
3135 }
3136 }
3137 else if (ldemul_place_orphan (file, s))
3138 ;
3139 else
3140 {
3141 lang_output_section_statement_type *os =
3142 lang_output_section_statement_lookup (s->name);
3143
3144 wild_doit (&os->children, s, os, file);
3145 }
3146 }
3147 }
3148 }
3149 }
3150
3151
3152 void
3153 lang_set_flags (ptr, flags)
3154 lang_memory_region_type *ptr;
3155 CONST char *flags;
3156 {
3157 flagword *ptr_flags = &ptr->flags;
3158
3159 ptr->flags = ptr->not_flags = 0;
3160 while (*flags)
3161 {
3162 switch (*flags)
3163 {
3164 case '!':
3165 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
3166 break;
3167
3168 case 'A': case 'a':
3169 *ptr_flags |= SEC_ALLOC;
3170 break;
3171
3172 case 'R': case 'r':
3173 *ptr_flags |= SEC_READONLY;
3174 break;
3175
3176 case 'W': case 'w':
3177 *ptr_flags |= SEC_DATA;
3178 break;
3179
3180 case 'X': case 'x':
3181 *ptr_flags |= SEC_CODE;
3182 break;
3183
3184 case 'L': case 'l':
3185 case 'I': case 'i':
3186 *ptr_flags |= SEC_LOAD;
3187 break;
3188
3189 default:
3190 einfo (_("%P%F: invalid syntax in flags\n"));
3191 break;
3192 }
3193 flags++;
3194 }
3195 }
3196
3197 /* Call a function on each input file. This function will be called
3198 on an archive, but not on the elements. */
3199
3200 void
3201 lang_for_each_input_file (func)
3202 void (*func) PARAMS ((lang_input_statement_type *));
3203 {
3204 lang_input_statement_type *f;
3205
3206 for (f = (lang_input_statement_type *) input_file_chain.head;
3207 f != NULL;
3208 f = (lang_input_statement_type *) f->next_real_file)
3209 func (f);
3210 }
3211
3212 /* Call a function on each file. The function will be called on all
3213 the elements of an archive which are included in the link, but will
3214 not be called on the archive file itself. */
3215
3216 void
3217 lang_for_each_file (func)
3218 void (*func) PARAMS ((lang_input_statement_type *));
3219 {
3220 lang_input_statement_type *f;
3221
3222 for (f = (lang_input_statement_type *) file_chain.head;
3223 f != (lang_input_statement_type *) NULL;
3224 f = (lang_input_statement_type *) f->next)
3225 {
3226 func (f);
3227 }
3228 }
3229
3230 #if 0
3231
3232 /* Not used. */
3233
3234 void
3235 lang_for_each_input_section (func)
3236 void (*func) PARAMS ((bfd * ab, asection * as));
3237 {
3238 lang_input_statement_type *f;
3239
3240 for (f = (lang_input_statement_type *) file_chain.head;
3241 f != (lang_input_statement_type *) NULL;
3242 f = (lang_input_statement_type *) f->next)
3243 {
3244 asection *s;
3245
3246 for (s = f->the_bfd->sections;
3247 s != (asection *) NULL;
3248 s = s->next)
3249 {
3250 func (f->the_bfd, s);
3251 }
3252 }
3253 }
3254
3255 #endif
3256
3257 void
3258 ldlang_add_file (entry)
3259 lang_input_statement_type * entry;
3260 {
3261 bfd **pp;
3262
3263 lang_statement_append (&file_chain,
3264 (lang_statement_union_type *) entry,
3265 &entry->next);
3266
3267 /* The BFD linker needs to have a list of all input BFDs involved in
3268 a link. */
3269 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3270 ASSERT (entry->the_bfd != output_bfd);
3271 for (pp = &link_info.input_bfds;
3272 *pp != (bfd *) NULL;
3273 pp = &(*pp)->link_next)
3274 ;
3275 *pp = entry->the_bfd;
3276 entry->the_bfd->usrdata = (PTR) entry;
3277 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3278
3279 /* Look through the sections and check for any which should not be
3280 included in the link. We need to do this now, so that we can
3281 notice when the backend linker tries to report multiple
3282 definition errors for symbols which are in sections we aren't
3283 going to link. FIXME: It might be better to entirely ignore
3284 symbols which are defined in sections which are going to be
3285 discarded. This would require modifying the backend linker for
3286 each backend which might set the SEC_LINK_ONCE flag. If we do
3287 this, we should probably handle SEC_EXCLUDE in the same way. */
3288
3289 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3290 }
3291
3292 void
3293 lang_add_output (name, from_script)
3294 CONST char *name;
3295 int from_script;
3296 {
3297 /* Make -o on command line override OUTPUT in script. */
3298 if (had_output_filename == false || !from_script)
3299 {
3300 output_filename = name;
3301 had_output_filename = true;
3302 }
3303 }
3304
3305
3306 static lang_output_section_statement_type *current_section;
3307
3308 static int
3309 topower (x)
3310 int x;
3311 {
3312 unsigned int i = 1;
3313 int l;
3314
3315 if (x < 0)
3316 return -1;
3317
3318 for (l = 0; l < 32; l++)
3319 {
3320 if (i >= (unsigned int) x)
3321 return l;
3322 i <<= 1;
3323 }
3324
3325 return 0;
3326 }
3327
3328 void
3329 lang_enter_output_section_statement (output_section_statement_name,
3330 address_exp, sectype, block_value,
3331 align, subalign, ebase)
3332 const char *output_section_statement_name;
3333 etree_type * address_exp;
3334 enum section_type sectype;
3335 bfd_vma block_value;
3336 etree_type *align;
3337 etree_type *subalign;
3338 etree_type *ebase;
3339 {
3340 lang_output_section_statement_type *os;
3341
3342 current_section =
3343 os =
3344 lang_output_section_statement_lookup (output_section_statement_name);
3345
3346
3347
3348 /* Add this statement to tree */
3349 /* add_statement(lang_output_section_statement_enum,
3350 output_section_statement);*/
3351 /* Make next things chain into subchain of this */
3352
3353 if (os->addr_tree ==
3354 (etree_type *) NULL)
3355 {
3356 os->addr_tree =
3357 address_exp;
3358 }
3359 os->sectype = sectype;
3360 if (sectype != noload_section)
3361 os->flags = SEC_NO_FLAGS;
3362 else
3363 os->flags = SEC_NEVER_LOAD;
3364 os->block_value = block_value ? block_value : 1;
3365 stat_ptr = &os->children;
3366
3367 os->subsection_alignment = topower(
3368 exp_get_value_int(subalign, -1,
3369 "subsection alignment",
3370 0));
3371 os->section_alignment = topower(
3372 exp_get_value_int(align, -1,
3373 "section alignment", 0));
3374
3375 os->load_base = ebase;
3376 }
3377
3378
3379 void
3380 lang_final ()
3381 {
3382 lang_output_statement_type *new =
3383 new_stat (lang_output_statement, stat_ptr);
3384
3385 new->name = output_filename;
3386 }
3387
3388 /* Reset the current counters in the regions */
3389 static void
3390 reset_memory_regions ()
3391 {
3392 lang_memory_region_type *p = lang_memory_region_list;
3393
3394 for (p = lang_memory_region_list;
3395 p != (lang_memory_region_type *) NULL;
3396 p = p->next)
3397 {
3398 p->old_length = (bfd_size_type) (p->current - p->origin);
3399 p->current = p->origin;
3400 }
3401 }
3402
3403 /* ??? At some point this traversal for GC should share code with the
3404 traversal for manipulating the output file. */
3405
3406 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3407 as needed. SECTION may be NULL, in which case it is a wild card. */
3408
3409 static void
3410 lang_gc_wild_section (ptr, section, file)
3411 lang_wild_statement_type *ptr;
3412 const char *section;
3413 lang_input_statement_type *file;
3414 {
3415 if (file->just_syms_flag == false)
3416 {
3417 register asection *s;
3418 boolean wildcard;
3419
3420 if (section == NULL)
3421 wildcard = false;
3422 else
3423 wildcard = wildcardp (section);
3424
3425 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3426 {
3427 boolean match;
3428
3429 if (section == NULL)
3430 match = true;
3431 else
3432 {
3433 const char *name;
3434
3435 name = bfd_get_section_name (file->the_bfd, s);
3436 if (wildcard)
3437 match = fnmatch (section, name, 0) == 0 ? true : false;
3438 else
3439 match = strcmp (section, name) == 0 ? true : false;
3440 }
3441
3442 if (match)
3443 {
3444 /* If the wild pattern was marked KEEP, the member sections
3445 should be as well. */
3446 if (ptr->keep_sections)
3447 s->flags |= SEC_KEEP;
3448 }
3449 }
3450 }
3451 }
3452
3453 /* Handle a wild statement for a single file F. */
3454
3455 static void
3456 lang_gc_wild_file (s, section, f)
3457 lang_wild_statement_type *s;
3458 const char *section;
3459 lang_input_statement_type *f;
3460 {
3461 if (f->the_bfd == NULL
3462 || ! bfd_check_format (f->the_bfd, bfd_archive))
3463 lang_gc_wild_section (s, section, f);
3464 else
3465 {
3466 bfd *member;
3467
3468 /* This is an archive file. We must map each member of the
3469 archive separately. */
3470 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
3471 while (member != NULL)
3472 {
3473 /* When lookup_name is called, it will call the add_symbols
3474 entry point for the archive. For each element of the
3475 archive which is included, BFD will call ldlang_add_file,
3476 which will set the usrdata field of the member to the
3477 lang_input_statement. */
3478 if (member->usrdata != NULL)
3479 {
3480 lang_gc_wild_section (s, section,
3481 (lang_input_statement_type *) member->usrdata);
3482 }
3483
3484 member = bfd_openr_next_archived_file (f->the_bfd, member);
3485 }
3486 }
3487 }
3488
3489 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3490 may be NULL, indicating that it is a wildcard. */
3491
3492 static void
3493 lang_gc_wild (s, section, file)
3494 lang_wild_statement_type *s;
3495 const char *section;
3496 const char *file;
3497 {
3498 lang_input_statement_type *f;
3499
3500 if (file == (char *) NULL)
3501 {
3502 /* Perform the iteration over all files in the list */
3503 for (f = (lang_input_statement_type *) file_chain.head;
3504 f != (lang_input_statement_type *) NULL;
3505 f = (lang_input_statement_type *) f->next)
3506 {
3507 lang_gc_wild_file (s, section, f);
3508 }
3509 }
3510 else if (wildcardp (file))
3511 {
3512 for (f = (lang_input_statement_type *) file_chain.head;
3513 f != (lang_input_statement_type *) NULL;
3514 f = (lang_input_statement_type *) f->next)
3515 {
3516 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
3517 lang_gc_wild_file (s, section, f);
3518 }
3519 }
3520 else
3521 {
3522 /* Perform the iteration over a single file */
3523 f = lookup_name (file);
3524 lang_gc_wild_file (s, section, f);
3525 }
3526 }
3527
3528 /* Iterate over sections marking them against GC. */
3529
3530 static void
3531 lang_gc_sections_1 (s)
3532 lang_statement_union_type * s;
3533 {
3534 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3535 {
3536 switch (s->header.type)
3537 {
3538 case lang_wild_statement_enum:
3539 lang_gc_wild (&s->wild_statement,
3540 s->wild_statement.section_name,
3541 s->wild_statement.filename);
3542 break;
3543 case lang_constructors_statement_enum:
3544 lang_gc_sections_1 (constructor_list.head);
3545 break;
3546 case lang_output_section_statement_enum:
3547 lang_gc_sections_1 (s->output_section_statement.children.head);
3548 break;
3549 case lang_group_statement_enum:
3550 lang_gc_sections_1 (s->group_statement.children.head);
3551 break;
3552 }
3553 }
3554 }
3555
3556 static void
3557 lang_gc_sections ()
3558 {
3559 struct bfd_link_hash_entry *h;
3560 ldlang_undef_chain_list_type *ulist, fake_list_start;
3561
3562 /* Keep all sections so marked in the link script. */
3563
3564 lang_gc_sections_1 (statement_list.head);
3565
3566 /* Keep all sections containing symbols undefined on the command-line.
3567 Handle the entry symbol at the same time. */
3568
3569 fake_list_start.next = ldlang_undef_chain_list_head;
3570 fake_list_start.name = entry_symbol;
3571
3572 for (ulist = &fake_list_start; ulist; ulist = ulist->next)
3573 {
3574 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3575 false, false, false);
3576
3577 if (h != (struct bfd_link_hash_entry *) NULL
3578 && (h->type == bfd_link_hash_defined
3579 || h->type == bfd_link_hash_defweak)
3580 && ! bfd_is_abs_section (h->u.def.section))
3581 {
3582 h->u.def.section->flags |= SEC_KEEP;
3583 }
3584 }
3585
3586 bfd_gc_sections (output_bfd, &link_info);
3587 }
3588
3589 void
3590 lang_process ()
3591 {
3592 lang_reasonable_defaults ();
3593 current_target = default_target;
3594
3595 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3596
3597 ldemul_create_output_section_statements ();
3598
3599 /* Add to the hash table all undefineds on the command line */
3600 lang_place_undefineds ();
3601
3602 /* Create a bfd for each input file */
3603 current_target = default_target;
3604 open_input_bfds (statement_list.head, false);
3605
3606 ldemul_after_open ();
3607
3608 /* Make sure that we're not mixing architectures. We call this
3609 after all the input files have been opened, but before we do any
3610 other processing, so that any operations merge_private_bfd_data
3611 does on the output file will be known during the rest of the
3612 link. */
3613 lang_check ();
3614
3615 /* Build all sets based on the information gathered from the input
3616 files. */
3617 ldctor_build_sets ();
3618
3619 /* Remove unreferenced sections if asked to. */
3620 if (command_line.gc_sections)
3621 lang_gc_sections ();
3622
3623 /* Size up the common data */
3624 lang_common ();
3625
3626 /* Run through the contours of the script and attach input sections
3627 to the correct output sections
3628 */
3629 map_input_to_output_sections (statement_list.head, (char *) NULL,
3630 (lang_output_section_statement_type *) NULL);
3631
3632
3633 /* Find any sections not attached explicitly and handle them */
3634 lang_place_orphans ();
3635
3636 ldemul_before_allocation ();
3637
3638 /* We must record the program headers before we try to fix the
3639 section positions, since they will affect SIZEOF_HEADERS. */
3640 lang_record_phdrs ();
3641
3642 /* Now run around and relax if we can */
3643 if (command_line.relax)
3644 {
3645 /* First time round is a trial run to get the 'worst case'
3646 addresses of the objects if there was no relaxing. */
3647 lang_size_sections (statement_list.head,
3648 abs_output_section,
3649 &(statement_list.head), 0, (bfd_vma) 0, false);
3650
3651 /* Keep relaxing until bfd_relax_section gives up. */
3652 do
3653 {
3654 reset_memory_regions ();
3655
3656 relax_again = false;
3657
3658 /* Do all the assignments with our current guesses as to
3659 section sizes. */
3660 lang_do_assignments (statement_list.head,
3661 abs_output_section,
3662 (fill_type) 0, (bfd_vma) 0);
3663
3664 /* Perform another relax pass - this time we know where the
3665 globals are, so can make better guess. */
3666 lang_size_sections (statement_list.head,
3667 abs_output_section,
3668 &(statement_list.head), 0, (bfd_vma) 0, true);
3669 }
3670 while (relax_again);
3671 }
3672 else
3673 {
3674 /* Size up the sections. */
3675 lang_size_sections (statement_list.head,
3676 abs_output_section,
3677 &(statement_list.head), 0, (bfd_vma) 0, false);
3678 }
3679
3680 /* See if anything special should be done now we know how big
3681 everything is. */
3682 ldemul_after_allocation ();
3683
3684 /* Fix any .startof. or .sizeof. symbols. */
3685 lang_set_startof ();
3686
3687 /* Do all the assignments, now that we know the final restingplaces
3688 of all the symbols */
3689
3690 lang_do_assignments (statement_list.head,
3691 abs_output_section,
3692 (fill_type) 0, (bfd_vma) 0);
3693
3694 /* Final stuffs */
3695
3696 ldemul_finish ();
3697 lang_finish ();
3698 }
3699
3700 /* EXPORTED TO YACC */
3701
3702 void
3703 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
3704 keep_sections)
3705 const char *const section_name;
3706 boolean sections_sorted;
3707 const char *const filename;
3708 boolean filenames_sorted;
3709 boolean keep_sections;
3710 {
3711 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3712 stat_ptr);
3713
3714 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3715 {
3716 placed_commons = true;
3717 }
3718 if (filename != (char *) NULL)
3719 {
3720 lang_has_input_file = true;
3721 }
3722 new->section_name = section_name;
3723 new->sections_sorted = sections_sorted;
3724 new->filename = filename;
3725 new->filenames_sorted = filenames_sorted;
3726 new->keep_sections = keep_sections;
3727 lang_list_init (&new->children);
3728 }
3729
3730 void
3731 lang_section_start (name, address)
3732 CONST char *name;
3733 etree_type * address;
3734 {
3735 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3736
3737 ad->section_name = name;
3738 ad->address = address;
3739 }
3740
3741 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3742 because of a -e argument on the command line, or zero if this is
3743 called by ENTRY in a linker script. Command line arguments take
3744 precedence. */
3745
3746 void
3747 lang_add_entry (name, cmdline)
3748 CONST char *name;
3749 boolean cmdline;
3750 {
3751 if (entry_symbol == NULL
3752 || cmdline
3753 || ! entry_from_cmdline)
3754 {
3755 entry_symbol = name;
3756 entry_from_cmdline = cmdline;
3757 }
3758 }
3759
3760 void
3761 lang_add_target (name)
3762 CONST char *name;
3763 {
3764 lang_target_statement_type *new = new_stat (lang_target_statement,
3765 stat_ptr);
3766
3767 new->target = name;
3768
3769 }
3770
3771 void
3772 lang_add_map (name)
3773 CONST char *name;
3774 {
3775 while (*name)
3776 {
3777 switch (*name)
3778 {
3779 case 'F':
3780 map_option_f = true;
3781 break;
3782 }
3783 name++;
3784 }
3785 }
3786
3787 void
3788 lang_add_fill (exp)
3789 int exp;
3790 {
3791 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3792 stat_ptr);
3793
3794 new->fill = exp;
3795 }
3796
3797 void
3798 lang_add_data (type, exp)
3799 int type;
3800 union etree_union *exp;
3801 {
3802
3803 lang_data_statement_type *new = new_stat (lang_data_statement,
3804 stat_ptr);
3805
3806 new->exp = exp;
3807 new->type = type;
3808
3809 }
3810
3811 /* Create a new reloc statement. RELOC is the BFD relocation type to
3812 generate. HOWTO is the corresponding howto structure (we could
3813 look this up, but the caller has already done so). SECTION is the
3814 section to generate a reloc against, or NAME is the name of the
3815 symbol to generate a reloc against. Exactly one of SECTION and
3816 NAME must be NULL. ADDEND is an expression for the addend. */
3817
3818 void
3819 lang_add_reloc (reloc, howto, section, name, addend)
3820 bfd_reloc_code_real_type reloc;
3821 reloc_howto_type *howto;
3822 asection *section;
3823 const char *name;
3824 union etree_union *addend;
3825 {
3826 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3827
3828 p->reloc = reloc;
3829 p->howto = howto;
3830 p->section = section;
3831 p->name = name;
3832 p->addend_exp = addend;
3833
3834 p->addend_value = 0;
3835 p->output_section = NULL;
3836 p->output_vma = 0;
3837 }
3838
3839 void
3840 lang_add_assignment (exp)
3841 etree_type * exp;
3842 {
3843 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3844 stat_ptr);
3845
3846 new->exp = exp;
3847 }
3848
3849 void
3850 lang_add_attribute (attribute)
3851 enum statement_enum attribute;
3852 {
3853 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3854 }
3855
3856 void
3857 lang_startup (name)
3858 CONST char *name;
3859 {
3860 if (startup_file != (char *) NULL)
3861 {
3862 einfo (_("%P%Fmultiple STARTUP files\n"));
3863 }
3864 first_file->filename = name;
3865 first_file->local_sym_name = name;
3866 first_file->real = true;
3867
3868 startup_file = name;
3869 }
3870
3871 void
3872 lang_float (maybe)
3873 boolean maybe;
3874 {
3875 lang_float_flag = maybe;
3876 }
3877
3878 void
3879 lang_leave_output_section_statement (fill, memspec, phdrs)
3880 bfd_vma fill;
3881 const char *memspec;
3882 struct lang_output_section_phdr_list *phdrs;
3883 {
3884 current_section->fill = fill;
3885 current_section->region = lang_memory_region_lookup (memspec);
3886 current_section->phdrs = phdrs;
3887 stat_ptr = &statement_list;
3888 }
3889
3890 /*
3891 Create an absolute symbol with the given name with the value of the
3892 address of first byte of the section named.
3893
3894 If the symbol already exists, then do nothing.
3895 */
3896 void
3897 lang_abs_symbol_at_beginning_of (secname, name)
3898 const char *secname;
3899 const char *name;
3900 {
3901 struct bfd_link_hash_entry *h;
3902
3903 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3904 if (h == (struct bfd_link_hash_entry *) NULL)
3905 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3906
3907 if (h->type == bfd_link_hash_new
3908 || h->type == bfd_link_hash_undefined)
3909 {
3910 asection *sec;
3911
3912 h->type = bfd_link_hash_defined;
3913
3914 sec = bfd_get_section_by_name (output_bfd, secname);
3915 if (sec == (asection *) NULL)
3916 h->u.def.value = 0;
3917 else
3918 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3919
3920 h->u.def.section = bfd_abs_section_ptr;
3921 }
3922 }
3923
3924 /*
3925 Create an absolute symbol with the given name with the value of the
3926 address of the first byte after the end of the section named.
3927
3928 If the symbol already exists, then do nothing.
3929 */
3930 void
3931 lang_abs_symbol_at_end_of (secname, name)
3932 const char *secname;
3933 const char *name;
3934 {
3935 struct bfd_link_hash_entry *h;
3936
3937 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3938 if (h == (struct bfd_link_hash_entry *) NULL)
3939 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3940
3941 if (h->type == bfd_link_hash_new
3942 || h->type == bfd_link_hash_undefined)
3943 {
3944 asection *sec;
3945
3946 h->type = bfd_link_hash_defined;
3947
3948 sec = bfd_get_section_by_name (output_bfd, secname);
3949 if (sec == (asection *) NULL)
3950 h->u.def.value = 0;
3951 else
3952 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3953 + bfd_section_size (output_bfd, sec));
3954
3955 h->u.def.section = bfd_abs_section_ptr;
3956 }
3957 }
3958
3959 void
3960 lang_statement_append (list, element, field)
3961 lang_statement_list_type * list;
3962 lang_statement_union_type * element;
3963 lang_statement_union_type ** field;
3964 {
3965 *(list->tail) = element;
3966 list->tail = field;
3967 }
3968
3969 /* Set the output format type. -oformat overrides scripts. */
3970
3971 void
3972 lang_add_output_format (format, big, little, from_script)
3973 const char *format;
3974 const char *big;
3975 const char *little;
3976 int from_script;
3977 {
3978 if (output_target == NULL || !from_script)
3979 {
3980 if (command_line.endian == ENDIAN_BIG
3981 && big != NULL)
3982 format = big;
3983 else if (command_line.endian == ENDIAN_LITTLE
3984 && little != NULL)
3985 format = little;
3986
3987 output_target = format;
3988 }
3989 }
3990
3991 /* Enter a group. This creates a new lang_group_statement, and sets
3992 stat_ptr to build new statements within the group. */
3993
3994 void
3995 lang_enter_group ()
3996 {
3997 lang_group_statement_type *g;
3998
3999 g = new_stat (lang_group_statement, stat_ptr);
4000 lang_list_init (&g->children);
4001 stat_ptr = &g->children;
4002 }
4003
4004 /* Leave a group. This just resets stat_ptr to start writing to the
4005 regular list of statements again. Note that this will not work if
4006 groups can occur inside anything else which can adjust stat_ptr,
4007 but currently they can't. */
4008
4009 void
4010 lang_leave_group ()
4011 {
4012 stat_ptr = &statement_list;
4013 }
4014
4015 /* Add a new program header. This is called for each entry in a PHDRS
4016 command in a linker script. */
4017
4018 void
4019 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4020 const char *name;
4021 etree_type *type;
4022 boolean filehdr;
4023 boolean phdrs;
4024 etree_type *at;
4025 etree_type *flags;
4026 {
4027 struct lang_phdr *n, **pp;
4028
4029 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4030 n->next = NULL;
4031 n->name = name;
4032 n->type = exp_get_value_int (type, 0, "program header type",
4033 lang_final_phase_enum);
4034 n->filehdr = filehdr;
4035 n->phdrs = phdrs;
4036 n->at = at;
4037 n->flags = flags;
4038
4039 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4040 ;
4041 *pp = n;
4042 }
4043
4044 /* Record the program header information in the output BFD. FIXME: We
4045 should not be calling an ELF specific function here. */
4046
4047 static void
4048 lang_record_phdrs ()
4049 {
4050 unsigned int alc;
4051 asection **secs;
4052 struct lang_output_section_phdr_list *last;
4053 struct lang_phdr *l;
4054 lang_statement_union_type *u;
4055
4056 alc = 10;
4057 secs = (asection **) xmalloc (alc * sizeof (asection *));
4058 last = NULL;
4059 for (l = lang_phdr_list; l != NULL; l = l->next)
4060 {
4061 unsigned int c;
4062 flagword flags;
4063 bfd_vma at;
4064
4065 c = 0;
4066 for (u = lang_output_section_statement.head;
4067 u != NULL;
4068 u = u->output_section_statement.next)
4069 {
4070 lang_output_section_statement_type *os;
4071 struct lang_output_section_phdr_list *pl;
4072
4073 os = &u->output_section_statement;
4074
4075 pl = os->phdrs;
4076 if (pl != NULL)
4077 last = pl;
4078 else
4079 {
4080 if (os->sectype == noload_section
4081 || os->bfd_section == NULL
4082 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4083 continue;
4084 pl = last;
4085 }
4086
4087 if (os->bfd_section == NULL)
4088 continue;
4089
4090 for (; pl != NULL; pl = pl->next)
4091 {
4092 if (strcmp (pl->name, l->name) == 0)
4093 {
4094 if (c >= alc)
4095 {
4096 alc *= 2;
4097 secs = ((asection **)
4098 xrealloc (secs, alc * sizeof (asection *)));
4099 }
4100 secs[c] = os->bfd_section;
4101 ++c;
4102 pl->used = true;
4103 }
4104 }
4105 }
4106
4107 if (l->flags == NULL)
4108 flags = 0;
4109 else
4110 flags = exp_get_vma (l->flags, 0, "phdr flags",
4111 lang_final_phase_enum);
4112
4113 if (l->at == NULL)
4114 at = 0;
4115 else
4116 at = exp_get_vma (l->at, 0, "phdr load address",
4117 lang_final_phase_enum);
4118
4119 if (! bfd_record_phdr (output_bfd, l->type,
4120 l->flags == NULL ? false : true,
4121 flags,
4122 l->at == NULL ? false : true,
4123 at, l->filehdr, l->phdrs, c, secs))
4124 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4125 }
4126
4127 free (secs);
4128
4129 /* Make sure all the phdr assignments succeeded. */
4130 for (u = lang_output_section_statement.head;
4131 u != NULL;
4132 u = u->output_section_statement.next)
4133 {
4134 struct lang_output_section_phdr_list *pl;
4135
4136 if (u->output_section_statement.bfd_section == NULL)
4137 continue;
4138
4139 for (pl = u->output_section_statement.phdrs;
4140 pl != NULL;
4141 pl = pl->next)
4142 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4143 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4144 u->output_section_statement.name, pl->name);
4145 }
4146 }
4147
4148 /* Record a list of sections which may not be cross referenced. */
4149
4150 void
4151 lang_add_nocrossref (l)
4152 struct lang_nocrossref *l;
4153 {
4154 struct lang_nocrossrefs *n;
4155
4156 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4157 n->next = nocrossref_list;
4158 n->list = l;
4159 nocrossref_list = n;
4160
4161 /* Set notice_all so that we get informed about all symbols. */
4162 link_info.notice_all = true;
4163 }
4164 \f
4165 /* Overlay handling. We handle overlays with some static variables. */
4166
4167 /* The overlay virtual address. */
4168 static etree_type *overlay_vma;
4169
4170 /* The overlay load address. */
4171 static etree_type *overlay_lma;
4172
4173 /* Whether nocrossrefs is set for this overlay. */
4174 static int overlay_nocrossrefs;
4175
4176 /* An expression for the maximum section size seen so far. */
4177 static etree_type *overlay_max;
4178
4179 /* A list of all the sections in this overlay. */
4180
4181 struct overlay_list
4182 {
4183 struct overlay_list *next;
4184 lang_output_section_statement_type *os;
4185 };
4186
4187 static struct overlay_list *overlay_list;
4188
4189 /* Start handling an overlay. */
4190
4191 void
4192 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4193 etree_type *vma_expr;
4194 etree_type *lma_expr;
4195 int nocrossrefs;
4196 {
4197 /* The grammar should prevent nested overlays from occurring. */
4198 ASSERT (overlay_vma == NULL
4199 && overlay_lma == NULL
4200 && overlay_list == NULL
4201 && overlay_max == NULL);
4202
4203 overlay_vma = vma_expr;
4204 overlay_lma = lma_expr;
4205 overlay_nocrossrefs = nocrossrefs;
4206 }
4207
4208 /* Start a section in an overlay. We handle this by calling
4209 lang_enter_output_section_statement with the correct VMA and LMA. */
4210
4211 void
4212 lang_enter_overlay_section (name)
4213 const char *name;
4214 {
4215 struct overlay_list *n;
4216 etree_type *size;
4217
4218 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4219 0, 0, 0, overlay_lma);
4220
4221 /* If this is the first section, then base the VMA and LMA of future
4222 sections on this one. This will work correctly even if `.' is
4223 used in the addresses. */
4224 if (overlay_list == NULL)
4225 {
4226 overlay_vma = exp_nameop (ADDR, name);
4227 overlay_lma = exp_nameop (LOADADDR, name);
4228 }
4229
4230 /* Remember the section. */
4231 n = (struct overlay_list *) xmalloc (sizeof *n);
4232 n->os = current_section;
4233 n->next = overlay_list;
4234 overlay_list = n;
4235
4236 size = exp_nameop (SIZEOF, name);
4237
4238 /* Adjust the LMA for the next section. */
4239 overlay_lma = exp_binop ('+', overlay_lma, size);
4240
4241 /* Arrange to work out the maximum section end address. */
4242 if (overlay_max == NULL)
4243 overlay_max = size;
4244 else
4245 overlay_max = exp_binop (MAX_K, overlay_max, size);
4246 }
4247
4248 /* Finish a section in an overlay. There isn't any special to do
4249 here. */
4250
4251 void
4252 lang_leave_overlay_section (fill, phdrs)
4253 bfd_vma fill;
4254 struct lang_output_section_phdr_list *phdrs;
4255 {
4256 const char *name;
4257 char *clean, *s2;
4258 const char *s1;
4259 char *buf;
4260
4261 name = current_section->name;
4262
4263 lang_leave_output_section_statement (fill, "*default*", phdrs);
4264
4265 /* Define the magic symbols. */
4266
4267 clean = xmalloc (strlen (name) + 1);
4268 s2 = clean;
4269 for (s1 = name; *s1 != '\0'; s1++)
4270 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4271 *s2++ = *s1;
4272 *s2 = '\0';
4273
4274 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4275 sprintf (buf, "__load_start_%s", clean);
4276 lang_add_assignment (exp_assop ('=', buf,
4277 exp_nameop (LOADADDR, name)));
4278
4279 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4280 sprintf (buf, "__load_stop_%s", clean);
4281 lang_add_assignment (exp_assop ('=', buf,
4282 exp_binop ('+',
4283 exp_nameop (LOADADDR, name),
4284 exp_nameop (SIZEOF, name))));
4285
4286 free (clean);
4287 }
4288
4289 /* Finish an overlay. If there are any overlay wide settings, this
4290 looks through all the sections in the overlay and sets them. */
4291
4292 void
4293 lang_leave_overlay (fill, memspec, phdrs)
4294 bfd_vma fill;
4295 const char *memspec;
4296 struct lang_output_section_phdr_list *phdrs;
4297 {
4298 lang_memory_region_type *region;
4299 struct overlay_list *l;
4300 struct lang_nocrossref *nocrossref;
4301
4302 if (memspec == NULL)
4303 region = NULL;
4304 else
4305 region = lang_memory_region_lookup (memspec);
4306
4307 nocrossref = NULL;
4308
4309 l = overlay_list;
4310 while (l != NULL)
4311 {
4312 struct overlay_list *next;
4313
4314 if (fill != 0 && l->os->fill == 0)
4315 l->os->fill = fill;
4316 if (region != NULL && l->os->region == NULL)
4317 l->os->region = region;
4318 if (phdrs != NULL && l->os->phdrs == NULL)
4319 l->os->phdrs = phdrs;
4320
4321 if (overlay_nocrossrefs)
4322 {
4323 struct lang_nocrossref *nc;
4324
4325 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4326 nc->name = l->os->name;
4327 nc->next = nocrossref;
4328 nocrossref = nc;
4329 }
4330
4331 next = l->next;
4332 free (l);
4333 l = next;
4334 }
4335
4336 if (nocrossref != NULL)
4337 lang_add_nocrossref (nocrossref);
4338
4339 /* Update . for the end of the overlay. */
4340 lang_add_assignment (exp_assop ('=', ".",
4341 exp_binop ('+', overlay_vma, overlay_max)));
4342
4343 overlay_vma = NULL;
4344 overlay_lma = NULL;
4345 overlay_nocrossrefs = 0;
4346 overlay_list = NULL;
4347 overlay_max = NULL;
4348 }
4349 \f
4350 /* Version handling. This is only useful for ELF. */
4351
4352 /* This global variable holds the version tree that we build. */
4353
4354 struct bfd_elf_version_tree *lang_elf_version_info;
4355
4356 /* This is called for each variable name or match expression. */
4357
4358 struct bfd_elf_version_expr *
4359 lang_new_vers_regex (orig, new)
4360 struct bfd_elf_version_expr *orig;
4361 const char *new;
4362 {
4363 struct bfd_elf_version_expr *ret;
4364
4365 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4366 ret->next = orig;
4367 ret->match = new;
4368 return ret;
4369 }
4370
4371 /* This is called for each set of variable names and match
4372 expressions. */
4373
4374 struct bfd_elf_version_tree *
4375 lang_new_vers_node (globals, locals)
4376 struct bfd_elf_version_expr *globals;
4377 struct bfd_elf_version_expr *locals;
4378 {
4379 struct bfd_elf_version_tree *ret;
4380
4381 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4382 ret->next = NULL;
4383 ret->name = NULL;
4384 ret->vernum = 0;
4385 ret->globals = globals;
4386 ret->locals = locals;
4387 ret->deps = NULL;
4388 ret->name_indx = (unsigned int) -1;
4389 ret->used = 0;
4390 return ret;
4391 }
4392
4393 /* This static variable keeps track of version indices. */
4394
4395 static int version_index;
4396
4397 /* This is called when we know the name and dependencies of the
4398 version. */
4399
4400 void
4401 lang_register_vers_node (name, version, deps)
4402 const char *name;
4403 struct bfd_elf_version_tree *version;
4404 struct bfd_elf_version_deps *deps;
4405 {
4406 struct bfd_elf_version_tree *t, **pp;
4407 struct bfd_elf_version_expr *e1;
4408
4409 /* Make sure this node has a unique name. */
4410 for (t = lang_elf_version_info; t != NULL; t = t->next)
4411 if (strcmp (t->name, name) == 0)
4412 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4413
4414 /* Check the global and local match names, and make sure there
4415 aren't any duplicates. */
4416
4417 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4418 {
4419 for (t = lang_elf_version_info; t != NULL; t = t->next)
4420 {
4421 struct bfd_elf_version_expr *e2;
4422
4423 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4424 if (strcmp (e1->match, e2->match) == 0)
4425 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4426 e1->match);
4427 }
4428 }
4429
4430 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4431 {
4432 for (t = lang_elf_version_info; t != NULL; t = t->next)
4433 {
4434 struct bfd_elf_version_expr *e2;
4435
4436 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4437 if (strcmp (e1->match, e2->match) == 0)
4438 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4439 e1->match);
4440 }
4441 }
4442
4443 version->deps = deps;
4444 version->name = name;
4445 ++version_index;
4446 version->vernum = version_index;
4447
4448 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4449 ;
4450 *pp = version;
4451 }
4452
4453 /* This is called when we see a version dependency. */
4454
4455 struct bfd_elf_version_deps *
4456 lang_add_vers_depend (list, name)
4457 struct bfd_elf_version_deps *list;
4458 const char *name;
4459 {
4460 struct bfd_elf_version_deps *ret;
4461 struct bfd_elf_version_tree *t;
4462
4463 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4464 ret->next = list;
4465
4466 for (t = lang_elf_version_info; t != NULL; t = t->next)
4467 {
4468 if (strcmp (t->name, name) == 0)
4469 {
4470 ret->version_needed = t;
4471 return ret;
4472 }
4473 }
4474
4475 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
4476
4477 return ret;
4478 }
This page took 0.126508 seconds and 4 git commands to generate.