source: trunk/src/binutils/ld/ldlang.c@ 492

Last change on this file since 492 was 10, checked in by bird, 22 years ago

Initial revision

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