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