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