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