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