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