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