]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldlang.c
--split-by-reloc, --split-by-file extensions. --unique option.
[thirdparty/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"
252b5132
RH
33#include "ldlex.h"
34#include "ldmisc.h"
35#include "ldctor.h"
36#include "ldfile.h"
b71e2778 37#include "ldemul.h"
252b5132
RH
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;
252b5132
RH
61static CONST char *current_target;
62static CONST char *output_target;
63static lang_statement_list_type statement_list;
64static struct lang_phdr *lang_phdr_list;
65
66static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
71 const char *target, boolean add_to_list));
72static void init_os PARAMS ((lang_output_section_statement_type *s));
73static void exp_init_os PARAMS ((etree_type *));
74static void section_already_linked PARAMS ((bfd *, asection *, PTR));
9503fd87
ILT
75static struct bfd_hash_entry *already_linked_newfunc
76 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
77 const char *string));
78static void already_linked_table_init PARAMS ((void));
79static void already_linked_table_free PARAMS ((void));
252b5132
RH
80static boolean wildcardp PARAMS ((const char *));
81static lang_statement_union_type *wild_sort
82 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
83 asection *));
252b5132
RH
84static lang_input_statement_type *lookup_name PARAMS ((const char *name));
85static void load_symbols PARAMS ((lang_input_statement_type *entry,
86 lang_statement_list_type *));
252b5132
RH
87static void wild PARAMS ((lang_wild_statement_type *s,
88 const char *section, const char *file,
89 const char *target,
90 lang_output_section_statement_type *output));
91static bfd *open_output PARAMS ((const char *name));
92static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
93static void open_input_bfds
94 PARAMS ((lang_statement_union_type *statement, boolean));
95static void lang_reasonable_defaults PARAMS ((void));
96static void lang_place_undefineds PARAMS ((void));
97static void map_input_to_output_sections
98 PARAMS ((lang_statement_union_type *s,
99 const char *target,
100 lang_output_section_statement_type *output_section_statement));
101static void print_output_section_statement
102 PARAMS ((lang_output_section_statement_type *output_section_statement));
103static void print_assignment
104 PARAMS ((lang_assignment_statement_type *assignment,
105 lang_output_section_statement_type *output_section));
106static void print_input_statement PARAMS ((lang_input_statement_type *statm));
107static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
108static void print_input_section PARAMS ((lang_input_section_type *in));
109static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
110static void print_data_statement PARAMS ((lang_data_statement_type *data));
111static void print_address_statement PARAMS ((lang_address_statement_type *));
112static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
113static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
114static void print_wild_statement
115 PARAMS ((lang_wild_statement_type *w,
116 lang_output_section_statement_type *os));
117static void print_group
118 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
119static void print_statement PARAMS ((lang_statement_union_type *s,
120 lang_output_section_statement_type *os));
121static void print_statement_list PARAMS ((lang_statement_union_type *s,
122 lang_output_section_statement_type *os));
123static void print_statements PARAMS ((void));
124static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
125 fill_type fill, unsigned int power,
126 asection *output_section_statement,
127 bfd_vma dot));
128static bfd_vma size_input_section
129 PARAMS ((lang_statement_union_type **this_ptr,
130 lang_output_section_statement_type *output_section_statement,
131 fill_type fill, bfd_vma dot, boolean relax));
132static void lang_finish PARAMS ((void));
133static void ignore_bfd_errors PARAMS ((const char *, ...));
134static void lang_check PARAMS ((void));
135static void lang_common PARAMS ((void));
136static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
137static void lang_place_orphans PARAMS ((void));
138static int topower PARAMS ((int));
139static void lang_set_startof PARAMS ((void));
140static void reset_memory_regions PARAMS ((void));
141static void lang_record_phdrs PARAMS ((void));
252b5132
RH
142static void lang_gc_wild
143 PARAMS ((lang_wild_statement_type *, const char *, const char *));
144static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
145static void lang_gc_sections PARAMS ((void));
146static void lang_do_version_exports_section PARAMS ((void));
147static void lang_check_section_addresses PARAMS ((void));
252b5132 148
4dec4d4e
RH
149typedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
150 asection *, lang_input_statement_type *,
151 void *));
152static void walk_wild_section
153 PARAMS ((lang_wild_statement_type *, const char *,
154 lang_input_statement_type *, callback_t, void *));
155static void walk_wild_file
156 PARAMS ((lang_wild_statement_type *, const char *,
157 lang_input_statement_type *, callback_t, void *));
e50d8076
NC
158
159static int get_target PARAMS ((const bfd_target *, void *));
160static void stricpy PARAMS ((char *, char *));
161static void strcut PARAMS ((char *, char *));
162static int name_compare PARAMS ((char *, char *));
163static int closest_target_match PARAMS ((const bfd_target *, void *));
164static char * get_first_input_target PARAMS ((void));
4dec4d4e 165
252b5132
RH
166/* EXPORTS */
167lang_output_section_statement_type *abs_output_section;
aea4bd9d 168lang_statement_list_type lang_output_section_statement;
252b5132 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;
74459f0e
TW
1199
1200 if (section->flags & SEC_BLOCK)
1201 {
1202 section->output_section->flags |= SEC_BLOCK;
1203 /* FIXME: This value should really be obtained from the bfd... */
1204 output->block_value = 128;
1205 }
252b5132
RH
1206 }
1207}
1208
1209/* Handle wildcard sorting. This returns the lang_input_section which
1210 should follow the one we are going to create for SECTION and FILE,
1211 based on the sorting requirements of WILD. It returns NULL if the
1212 new section should just go at the end of the current list. */
1213
1214static lang_statement_union_type *
1215wild_sort (wild, file, section)
1216 lang_wild_statement_type *wild;
1217 lang_input_statement_type *file;
1218 asection *section;
1219{
1220 const char *section_name;
1221 lang_statement_union_type *l;
1222
1223 if (! wild->filenames_sorted && ! wild->sections_sorted)
1224 return NULL;
1225
1226 section_name = bfd_get_section_name (file->the_bfd, section);
1227 for (l = wild->children.head; l != NULL; l = l->next)
1228 {
1229 lang_input_section_type *ls;
1230
1231 if (l->header.type != lang_input_section_enum)
1232 continue;
1233 ls = &l->input_section;
1234
1235 /* Sorting by filename takes precedence over sorting by section
1236 name. */
1237
1238 if (wild->filenames_sorted)
1239 {
1240 const char *fn, *ln;
1241 boolean fa, la;
1242 int i;
1243
1244 /* The PE support for the .idata section as generated by
1245 dlltool assumes that files will be sorted by the name of
1246 the archive and then the name of the file within the
1247 archive. */
1248
1249 if (file->the_bfd != NULL
1250 && bfd_my_archive (file->the_bfd) != NULL)
1251 {
1252 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1253 fa = true;
1254 }
1255 else
1256 {
1257 fn = file->filename;
1258 fa = false;
1259 }
1260
1261 if (ls->ifile->the_bfd != NULL
1262 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1263 {
1264 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1265 la = true;
1266 }
1267 else
1268 {
1269 ln = ls->ifile->filename;
1270 la = false;
1271 }
1272
1273 i = strcmp (fn, ln);
1274 if (i > 0)
1275 continue;
1276 else if (i < 0)
1277 break;
1278
1279 if (fa || la)
1280 {
1281 if (fa)
1282 fn = file->filename;
1283 if (la)
1284 ln = ls->ifile->filename;
1285
1286 i = strcmp (fn, ln);
1287 if (i > 0)
1288 continue;
1289 else if (i < 0)
1290 break;
1291 }
1292 }
1293
1294 /* Here either the files are not sorted by name, or we are
1295 looking at the sections for this file. */
1296
1297 if (wild->sections_sorted)
1298 {
1299 if (strcmp (section_name,
1300 bfd_get_section_name (ls->ifile->the_bfd,
1301 ls->section))
1302 < 0)
1303 break;
1304 }
1305 }
1306
1307 return l;
1308}
1309
1310/* Expand a wild statement for a particular FILE. SECTION may be
1311 NULL, in which case it is a wild card. */
1312
1313static void
4dec4d4e 1314output_section_callback (ptr, section, file, output)
252b5132 1315 lang_wild_statement_type *ptr;
4dec4d4e 1316 asection *section;
252b5132 1317 lang_input_statement_type *file;
4dec4d4e
RH
1318 void *output;
1319{
1320 lang_statement_union_type *before;
1321
1322 /* If the wild pattern was marked KEEP, the member sections
1323 should be as well. */
1324 if (ptr->keep_sections)
1325 section->flags |= SEC_KEEP;
1326
1327 before = wild_sort (ptr, file, section);
1328
1329 /* Here BEFORE points to the lang_input_section which
1330 should follow the one we are about to add. If BEFORE
1331 is NULL, then the section should just go at the end
1332 of the current list. */
1333
1334 if (before == NULL)
1335 wild_doit (&ptr->children, section,
1336 (lang_output_section_statement_type *) output,
1337 file);
1338 else
252b5132 1339 {
4dec4d4e
RH
1340 lang_statement_list_type list;
1341 lang_statement_union_type **pp;
1342
1343 lang_list_init (&list);
1344 wild_doit (&list, section,
1345 (lang_output_section_statement_type *) output,
1346 file);
1347
1348 /* If we are discarding the section, LIST.HEAD will
1349 be NULL. */
1350 if (list.head != NULL)
252b5132 1351 {
4dec4d4e
RH
1352 ASSERT (list.head->next == NULL);
1353
1354 for (pp = &ptr->children.head;
1355 *pp != before;
1356 pp = &(*pp)->next)
1357 ASSERT (*pp != NULL);
1358
1359 list.head->next = *pp;
1360 *pp = list.head;
252b5132
RH
1361 }
1362 }
1363}
1364
1365/* This is passed a file name which must have been seen already and
1366 added to the statement tree. We will see if it has been opened
1367 already and had its symbols read. If not then we'll read it. */
1368
1369static lang_input_statement_type *
1370lookup_name (name)
1371 const char *name;
1372{
1373 lang_input_statement_type *search;
1374
1375 for (search = (lang_input_statement_type *) input_file_chain.head;
1376 search != (lang_input_statement_type *) NULL;
1377 search = (lang_input_statement_type *) search->next_real_file)
1378 {
1379 if (search->filename == (char *) NULL && name == (char *) NULL)
1380 return search;
1381 if (search->filename != (char *) NULL
1382 && name != (char *) NULL
1383 && strcmp (search->filename, name) == 0)
1384 break;
1385 }
1386
1387 if (search == (lang_input_statement_type *) NULL)
1388 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1389 false);
1390
1391 /* If we have already added this file, or this file is not real
1392 (FIXME: can that ever actually happen?) or the name is NULL
1393 (FIXME: can that ever actually happen?) don't add this file. */
1394 if (search->loaded
1395 || ! search->real
1396 || search->filename == (const char *) NULL)
1397 return search;
1398
1399 load_symbols (search, (lang_statement_list_type *) NULL);
1400
1401 return search;
1402}
1403
1404/* Get the symbols for an input file. */
1405
1406static void
1407load_symbols (entry, place)
1408 lang_input_statement_type *entry;
1409 lang_statement_list_type *place;
1410{
1411 char **matching;
1412
1413 if (entry->loaded)
1414 return;
1415
1416 ldfile_open_file (entry);
1417
1418 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1419 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1420 {
1421 bfd_error_type err;
1422 lang_statement_list_type *hold;
1423
1424 err = bfd_get_error ();
1425 if (err == bfd_error_file_ambiguously_recognized)
1426 {
1427 char **p;
1428
1429 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1430 einfo (_("%B: matching formats:"), entry->the_bfd);
1431 for (p = matching; *p != NULL; p++)
1432 einfo (" %s", *p);
1433 einfo ("%F\n");
1434 }
1435 else if (err != bfd_error_file_not_recognized
1436 || place == NULL)
1437 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1438
1439 bfd_close (entry->the_bfd);
1440 entry->the_bfd = NULL;
1441
1442 /* See if the emulation has some special knowledge. */
1443
1444 if (ldemul_unrecognized_file (entry))
1445 return;
1446
1447 /* Try to interpret the file as a linker script. */
1448
1449 ldfile_open_command_file (entry->filename);
1450
1451 hold = stat_ptr;
1452 stat_ptr = place;
1453
1454 ldfile_assumed_script = true;
1455 parser_input = input_script;
1456 yyparse ();
1457 ldfile_assumed_script = false;
1458
1459 stat_ptr = hold;
1460
1461 return;
1462 }
1463
1464 if (ldemul_recognized_file (entry))
1465 return;
1466
1467 /* We don't call ldlang_add_file for an archive. Instead, the
1468 add_symbols entry point will call ldlang_add_file, via the
1469 add_archive_element callback, for each element of the archive
1470 which is used. */
1471 switch (bfd_get_format (entry->the_bfd))
1472 {
1473 default:
1474 break;
1475
1476 case bfd_object:
1477 ldlang_add_file (entry);
1478 if (trace_files || trace_file_tries)
1479 info_msg ("%I\n", entry);
1480 break;
1481
1482 case bfd_archive:
1483 if (entry->whole_archive)
1484 {
1485 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1486 (bfd *) NULL);
1487 while (member != NULL)
1488 {
1489 if (! bfd_check_format (member, bfd_object))
1490 einfo (_("%F%B: object %B in archive is not object\n"),
1491 entry->the_bfd, member);
1492 if (! ((*link_info.callbacks->add_archive_element)
1493 (&link_info, member, "--whole-archive")))
1494 abort ();
1495 if (! bfd_link_add_symbols (member, &link_info))
1496 einfo (_("%F%B: could not read symbols: %E\n"), member);
1497 member = bfd_openr_next_archived_file (entry->the_bfd,
1498 member);
1499 }
1500
1501 entry->loaded = true;
1502
1503 return;
1504 }
1505 }
1506
1507 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1508 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1509
1510 entry->loaded = true;
1511}
1512
4dec4d4e 1513
252b5132
RH
1514
1515/* Handle a wild statement. SECTION or FILE or both may be NULL,
1516 indicating that it is a wildcard. Separate lang_input_section
1517 statements are created for each part of the expansion; they are
1518 added after the wild statement S. OUTPUT is the output section. */
1519
1520static void
1521wild (s, section, file, target, output)
1522 lang_wild_statement_type *s;
1523 const char *section;
1524 const char *file;
87f2a346 1525 const char *target ATTRIBUTE_UNUSED;
252b5132
RH
1526 lang_output_section_statement_type *output;
1527{
4dec4d4e 1528 walk_wild (s, section, file, output_section_callback, (void *) output);
252b5132
RH
1529
1530 if (section != (char *) NULL
1531 && strcmp (section, "COMMON") == 0
1532 && default_common_section == NULL)
1533 {
1534 /* Remember the section that common is going to in case we later
1535 get something which doesn't know where to put it. */
1536 default_common_section = output;
1537 }
1538}
1539
e50d8076
NC
1540/* Return true iff target is the sought target. */
1541static int
1542get_target (target, data)
1543 const bfd_target * target;
1544 void * data;
1545{
1546 const char * sought = (const char *) data;
1547
1548 return strcmp (target->name, sought) == 0;
1549}
1550
1551/* Like strcpy() but convert to lower case as well. */
1552static void
1553stricpy (dest, src)
1554 char * dest;
1555 char * src;
1556{
1557 char c;
1558
1559 while ((c = * src ++) != 0)
1560 {
1561 if (isupper ((unsigned char) c))
1562 c = tolower (c);
1563
1564 * dest ++ = c;
1565 }
1566
1567 * dest = 0;
1568}
1569
1570/* Remove the first occurance of needle (if any) in haystack
1571 from haystack. */
1572static void
1573strcut (haystack, needle)
1574 char * haystack;
1575 char * needle;
1576{
1577 haystack = strstr (haystack, needle);
1578
1579 if (haystack)
1580 {
1581 char * src;
1582
1583 for (src = haystack + strlen (needle); * src;)
1584 * haystack ++ = * src ++;
1585
1586 * haystack = 0;
1587 }
1588}
1589
1590/* Compare two target format name strings.
1591 Return a value indicating how "similar" they are. */
1592static int
1593name_compare (first, second)
1594 char * first;
1595 char * second;
1596{
1597 char * copy1;
1598 char * copy2;
1599 int result;
1600
1601 copy1 = xmalloc (strlen (first) + 1);
1602 copy2 = xmalloc (strlen (second) + 1);
1603
1604 /* Convert the names to lower case. */
1605 stricpy (copy1, first);
1606 stricpy (copy2, second);
1607
1608 /* Remove and endian strings from the name. */
1609 strcut (copy1, "big");
1610 strcut (copy1, "little");
1611 strcut (copy2, "big");
1612 strcut (copy2, "little");
1613
1614 /* Return a value based on how many characters match,
1615 starting from the beginning. If both strings are
1616 the same then return 10 * their length. */
1617 for (result = 0; copy1 [result] == copy2 [result]; result ++)
1618 if (copy1 [result] == 0)
1619 {
1620 result *= 10;
1621 break;
1622 }
1623
1624 free (copy1);
1625 free (copy2);
1626
1627 return result;
1628}
1629
1630/* Set by closest_target_match() below. */
1631static const bfd_target * winner;
1632
1633/* Scan all the valid bfd targets looking for one that has the endianness
1634 requirement that was specified on the command line, and is the nearest
1635 match to the original output target. */
1636static int
1637closest_target_match (target, data)
1638 const bfd_target * target;
1639 void * data;
1640{
1641 const bfd_target * original = (const bfd_target *) data;
1642
1643 if (command_line.endian == ENDIAN_BIG && target->byteorder != BFD_ENDIAN_BIG)
1644 return 0;
1645
1646 if (command_line.endian == ENDIAN_LITTLE && target->byteorder != BFD_ENDIAN_LITTLE)
1647 return 0;
1648
1649 /* Must be the same flavour. */
1650 if (target->flavour != original->flavour)
1651 return 0;
1652
1653 /* If we have not found a potential winner yet, then record this one. */
1654 if (winner == NULL)
1655 {
1656 winner = target;
1657 return 0;
1658 }
1659
1660 /* Oh dear, we now have two potential candidates for a successful match.
1661 Compare their names and choose the better one. */
1662 if (name_compare (target->name, original->name) > name_compare (winner->name, original->name))
1663 winner = target;
1664
1665 /* Keep on searching until wqe have checked them all. */
1666 return 0;
1667}
1668
1669/* Return the BFD target format of the first input file. */
1670static char *
1671get_first_input_target ()
1672{
1673 char * target = NULL;
1674
1675 LANG_FOR_EACH_INPUT_STATEMENT (s)
1676 {
1677 if (s->header.type == lang_input_statement_enum
1678 && s->real)
1679 {
1680 ldfile_open_file (s);
1681
1682 if (s->the_bfd != NULL
1683 && bfd_check_format (s->the_bfd, bfd_object))
1684 {
1685 target = bfd_get_target (s->the_bfd);
1686
1687 if (target != NULL)
1688 break;
1689 }
1690 }
1691 }
1692
1693 return target;
1694}
1695
252b5132
RH
1696/* Open the output file. */
1697
1698static bfd *
1699open_output (name)
e50d8076 1700 const char * name;
252b5132 1701{
e50d8076 1702 bfd * output;
252b5132 1703
e50d8076 1704 /* Has the user told us which output format to use ? */
252b5132
RH
1705 if (output_target == (char *) NULL)
1706 {
e50d8076
NC
1707 /* No - has the current target been set to something other than the default ? */
1708 if (current_target != default_target)
252b5132 1709 output_target = current_target;
e50d8076
NC
1710
1711 /* No - can we determine the format of the first input file ? */
1712 else
1713 {
1714 output_target = get_first_input_target ();
1715
1716 /* Failed - use the default output target. */
1717 if (output_target == NULL)
1718 output_target = default_target;
1719 }
1720 }
1721
1722 /* Has the user requested a particular endianness on the command line ? */
1723 if (command_line.endian != ENDIAN_UNSET)
1724 {
1725 const bfd_target * target;
1b69a0bf 1726 enum bfd_endian desired_endian;
e50d8076
NC
1727
1728 /* Get the chosen target. */
1729 target = bfd_search_for_target (get_target, (void *) output_target);
1730
c13b1b77
NC
1731 /* If the target is not supported, we cannot do anything. */
1732 if (target != NULL)
e50d8076 1733 {
c13b1b77
NC
1734 if (command_line.endian == ENDIAN_BIG)
1735 desired_endian = BFD_ENDIAN_BIG;
e50d8076 1736 else
c13b1b77
NC
1737 desired_endian = BFD_ENDIAN_LITTLE;
1738
1739 /* See if the target has the wrong endianness. This should not happen
1740 if the linker script has provided big and little endian alternatives,
1741 but some scrips don't do this. */
1742 if (target->byteorder != desired_endian)
e50d8076 1743 {
c13b1b77
NC
1744 /* If it does, then see if the target provides
1745 an alternative with the correct endianness. */
1746 if (target->alternative_target != NULL
1747 && (target->alternative_target->byteorder == desired_endian))
1748 output_target = target->alternative_target->name;
e50d8076 1749 else
c13b1b77
NC
1750 {
1751 /* Try to find a target as similar as possible to the default
1752 target, but which has the desired endian characteristic. */
1753 (void) bfd_search_for_target (closest_target_match, (void *) target);
1754
1755 /* Oh dear - we could not find any targets that satisfy our
1756 requirements. */
1757 if (winner == NULL)
1758 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1759 else
1760 output_target = winner->name;
1761 }
e50d8076
NC
1762 }
1763 }
252b5132 1764 }
e50d8076 1765
252b5132
RH
1766 output = bfd_openw (name, output_target);
1767
1768 if (output == (bfd *) NULL)
1769 {
1770 if (bfd_get_error () == bfd_error_invalid_target)
e50d8076
NC
1771 einfo (_("%P%F: target %s not found\n"), output_target);
1772
252b5132
RH
1773 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1774 }
1775
1776 delete_output_file_on_failure = true;
1777
1778 /* output->flags |= D_PAGED;*/
1779
1780 if (! bfd_set_format (output, bfd_object))
1781 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1782 if (! bfd_set_arch_mach (output,
1783 ldfile_output_architecture,
1784 ldfile_output_machine))
1785 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1786
1787 link_info.hash = bfd_link_hash_table_create (output);
1788 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1789 einfo (_("%P%F: can not create link hash table: %E\n"));
1790
1791 bfd_set_gp_size (output, g_switch_value);
1792 return output;
1793}
1794
252b5132
RH
1795static void
1796ldlang_open_output (statement)
1797 lang_statement_union_type * statement;
1798{
1799 switch (statement->header.type)
1800 {
1801 case lang_output_statement_enum:
1802 ASSERT (output_bfd == (bfd *) NULL);
1803 output_bfd = open_output (statement->output_statement.name);
1804 ldemul_set_output_arch ();
1805 if (config.magic_demand_paged && !link_info.relocateable)
1806 output_bfd->flags |= D_PAGED;
1807 else
1808 output_bfd->flags &= ~D_PAGED;
1809 if (config.text_read_only)
1810 output_bfd->flags |= WP_TEXT;
1811 else
1812 output_bfd->flags &= ~WP_TEXT;
1813 if (link_info.traditional_format)
1814 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1815 else
1816 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1817 break;
1818
1819 case lang_target_statement_enum:
1820 current_target = statement->target_statement.target;
1821 break;
1822 default:
1823 break;
1824 }
1825}
1826
1827/* Open all the input files. */
1828
1829static void
1830open_input_bfds (s, force)
1831 lang_statement_union_type *s;
1832 boolean force;
1833{
1834 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1835 {
1836 switch (s->header.type)
1837 {
1838 case lang_constructors_statement_enum:
1839 open_input_bfds (constructor_list.head, force);
1840 break;
1841 case lang_output_section_statement_enum:
1842 open_input_bfds (s->output_section_statement.children.head, force);
1843 break;
1844 case lang_wild_statement_enum:
1845 /* Maybe we should load the file's symbols */
1846 if (s->wild_statement.filename
1847 && ! wildcardp (s->wild_statement.filename))
1848 (void) lookup_name (s->wild_statement.filename);
1849 open_input_bfds (s->wild_statement.children.head, force);
1850 break;
1851 case lang_group_statement_enum:
1852 {
1853 struct bfd_link_hash_entry *undefs;
1854
1855 /* We must continually search the entries in the group
1856 until no new symbols are added to the list of undefined
1857 symbols. */
1858
1859 do
1860 {
1861 undefs = link_info.hash->undefs_tail;
1862 open_input_bfds (s->group_statement.children.head, true);
1863 }
1864 while (undefs != link_info.hash->undefs_tail);
1865 }
1866 break;
1867 case lang_target_statement_enum:
1868 current_target = s->target_statement.target;
1869 break;
1870 case lang_input_statement_enum:
e50d8076 1871 if (s->input_statement.real)
252b5132
RH
1872 {
1873 lang_statement_list_type add;
1874
1875 s->input_statement.target = current_target;
1876
1877 /* If we are being called from within a group, and this
1878 is an archive which has already been searched, then
cd4c806a
L
1879 force it to be researched unless the whole archive
1880 has been loaded already. */
252b5132 1881 if (force
cd4c806a 1882 && !s->input_statement.whole_archive
252b5132
RH
1883 && s->input_statement.loaded
1884 && bfd_check_format (s->input_statement.the_bfd,
1885 bfd_archive))
1886 s->input_statement.loaded = false;
1887
1888 lang_list_init (&add);
1889
1890 load_symbols (&s->input_statement, &add);
1891
1892 if (add.head != NULL)
1893 {
1894 *add.tail = s->next;
1895 s->next = add.head;
1896 }
1897 }
1898 break;
1899 default:
1900 break;
1901 }
1902 }
1903}
1904
1905/* If there are [COMMONS] statements, put a wild one into the bss section */
1906
1907static void
1908lang_reasonable_defaults ()
1909{
1910#if 0
1911 lang_output_section_statement_lookup (".text");
1912 lang_output_section_statement_lookup (".data");
1913
1914 default_common_section =
1915 lang_output_section_statement_lookup (".bss");
1916
1917
1918 if (placed_commons == false)
1919 {
1920 lang_wild_statement_type *new =
1921 new_stat (lang_wild_statement,
1922 &default_common_section->children);
1923
1924 new->section_name = "COMMON";
1925 new->filename = (char *) NULL;
1926 lang_list_init (&new->children);
1927 }
1928#endif
1929
1930}
1931
1932/*
1933 Add the supplied name to the symbol table as an undefined reference.
1934 Remove items from the chain as we open input bfds
1935 */
1936typedef struct ldlang_undef_chain_list
1937{
1938 struct ldlang_undef_chain_list *next;
1939 char *name;
1940} ldlang_undef_chain_list_type;
1941
1942static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1943
1944void
1945ldlang_add_undef (name)
1946 CONST char *CONST name;
1947{
1948 ldlang_undef_chain_list_type *new =
1949 ((ldlang_undef_chain_list_type *)
1950 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1951
1952 new->next = ldlang_undef_chain_list_head;
1953 ldlang_undef_chain_list_head = new;
1954
1955 new->name = buystring (name);
1956}
1957
1958/* Run through the list of undefineds created above and place them
1959 into the linker hash table as undefined symbols belonging to the
1960 script file.
1961*/
1962static void
1963lang_place_undefineds ()
1964{
1965 ldlang_undef_chain_list_type *ptr;
1966
1967 for (ptr = ldlang_undef_chain_list_head;
1968 ptr != (ldlang_undef_chain_list_type *) NULL;
1969 ptr = ptr->next)
1970 {
1971 struct bfd_link_hash_entry *h;
1972
1973 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1974 if (h == (struct bfd_link_hash_entry *) NULL)
1975 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1976 if (h->type == bfd_link_hash_new)
1977 {
1978 h->type = bfd_link_hash_undefined;
1979 h->u.undef.abfd = NULL;
1980 bfd_link_add_undef (link_info.hash, h);
1981 }
1982 }
1983}
1984
1985/* Open input files and attatch to output sections */
1986static void
1987map_input_to_output_sections (s, target, output_section_statement)
1988 lang_statement_union_type * s;
1989 CONST char *target;
1990 lang_output_section_statement_type * output_section_statement;
1991{
1992 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1993 {
1994 switch (s->header.type)
1995 {
1996
1997
1998 case lang_wild_statement_enum:
1999 wild (&s->wild_statement, s->wild_statement.section_name,
2000 s->wild_statement.filename, target,
2001 output_section_statement);
2002
2003 break;
2004 case lang_constructors_statement_enum:
2005 map_input_to_output_sections (constructor_list.head,
2006 target,
2007 output_section_statement);
2008 break;
2009 case lang_output_section_statement_enum:
2010 map_input_to_output_sections (s->output_section_statement.children.head,
2011 target,
2012 &s->output_section_statement);
2013 break;
2014 case lang_output_statement_enum:
2015 break;
2016 case lang_target_statement_enum:
2017 target = s->target_statement.target;
2018 break;
2019 case lang_group_statement_enum:
2020 map_input_to_output_sections (s->group_statement.children.head,
2021 target,
2022 output_section_statement);
2023 break;
2024 case lang_fill_statement_enum:
2025 case lang_input_section_enum:
2026 case lang_object_symbols_statement_enum:
2027 case lang_data_statement_enum:
2028 case lang_reloc_statement_enum:
2029 case lang_padding_statement_enum:
2030 case lang_input_statement_enum:
2031 if (output_section_statement != NULL
2032 && output_section_statement->bfd_section == NULL)
2033 init_os (output_section_statement);
2034 break;
2035 case lang_assignment_statement_enum:
2036 if (output_section_statement != NULL
2037 && output_section_statement->bfd_section == NULL)
2038 init_os (output_section_statement);
2039
2040 /* Make sure that any sections mentioned in the assignment
2041 are initialized. */
2042 exp_init_os (s->assignment_statement.exp);
2043 break;
2044 case lang_afile_asection_pair_statement_enum:
2045 FAIL ();
2046 break;
2047 case lang_address_statement_enum:
2048 /* Mark the specified section with the supplied address */
2049 {
2050 lang_output_section_statement_type *os =
2051 lang_output_section_statement_lookup
2052 (s->address_statement.section_name);
2053
2054 if (os->bfd_section == NULL)
2055 init_os (os);
2056 os->addr_tree = s->address_statement.address;
2057 }
2058 break;
2059 }
2060 }
2061}
2062
2063static void
2064print_output_section_statement (output_section_statement)
2065 lang_output_section_statement_type * output_section_statement;
2066{
2067 asection *section = output_section_statement->bfd_section;
2068 int len;
2069
2070 if (output_section_statement != abs_output_section)
2071 {
2072 minfo ("\n%s", output_section_statement->name);
2073
2074 if (section != NULL)
2075 {
2076 print_dot = section->vma;
2077
2078 len = strlen (output_section_statement->name);
2079 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2080 {
2081 print_nl ();
2082 len = 0;
2083 }
2084 while (len < SECTION_NAME_MAP_LENGTH)
2085 {
2086 print_space ();
2087 ++len;
2088 }
2089
2090 minfo ("0x%V %W", section->vma, section->_raw_size);
2091
2092 if (output_section_statement->load_base != NULL)
2093 {
2094 bfd_vma addr;
2095
2096 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2097 "load base", lang_final_phase_enum);
2098 minfo (_(" load address 0x%V"), addr);
2099 }
2100 }
2101
2102 print_nl ();
2103 }
2104
2105 print_statement_list (output_section_statement->children.head,
2106 output_section_statement);
2107}
2108
2109static void
2110print_assignment (assignment, output_section)
2111 lang_assignment_statement_type * assignment;
2112 lang_output_section_statement_type * output_section;
2113{
2114 int i;
2115 etree_value_type result;
2116
2117 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2118 print_space ();
2119
2120 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2121 lang_final_phase_enum, print_dot, &print_dot);
2122 if (result.valid_p)
2123 minfo ("0x%V", result.value + result.section->bfd_section->vma);
2124 else
2125 {
2126 minfo ("*undef* ");
2127#ifdef BFD64
2128 minfo (" ");
2129#endif
2130 }
2131
2132 minfo (" ");
2133
2134 exp_print_tree (assignment->exp);
2135
2136 print_nl ();
2137}
2138
2139static void
2140print_input_statement (statm)
2141 lang_input_statement_type * statm;
2142{
2143 if (statm->filename != (char *) NULL)
2144 {
2145 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2146 }
2147}
2148
2149/* Print all symbols defined in a particular section. This is called
2150 via bfd_link_hash_traverse. */
2151
2152static boolean
2153print_one_symbol (hash_entry, ptr)
2154 struct bfd_link_hash_entry *hash_entry;
2155 PTR ptr;
2156{
2157 asection *sec = (asection *) ptr;
2158
2159 if ((hash_entry->type == bfd_link_hash_defined
2160 || hash_entry->type == bfd_link_hash_defweak)
2161 && sec == hash_entry->u.def.section)
2162 {
2163 int i;
2164
2165 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2166 print_space ();
2167 minfo ("0x%V ",
2168 (hash_entry->u.def.value
2169 + hash_entry->u.def.section->output_offset
2170 + hash_entry->u.def.section->output_section->vma));
2171
2172 minfo (" %T\n", hash_entry->root.string);
2173 }
2174
2175 return true;
2176}
2177
2178/* Print information about an input section to the map file. */
2179
2180static void
2181print_input_section (in)
2182 lang_input_section_type * in;
2183{
2184 asection *i = in->section;
2185 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
f6af82bd
AM
2186 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2187 ldfile_output_machine);
252b5132
RH
2188 if (size != 0)
2189 {
2190 print_space ();
2191
2192 minfo ("%s", i->name);
2193
2194 if (i->output_section != NULL)
2195 {
2196 int len;
2197
2198 len = 1 + strlen (i->name);
2199 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2200 {
2201 print_nl ();
2202 len = 0;
2203 }
2204 while (len < SECTION_NAME_MAP_LENGTH)
2205 {
2206 print_space ();
2207 ++len;
2208 }
2209
2210 minfo ("0x%V %W %B\n",
4cbfc3ac 2211 i->output_section->vma + i->output_offset, size / opb,
252b5132
RH
2212 i->owner);
2213
2214 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2215 {
2216 len = SECTION_NAME_MAP_LENGTH + 3;
2217#ifdef BFD64
2218 len += 16;
2219#else
2220 len += 8;
2221#endif
2222 while (len > 0)
2223 {
2224 print_space ();
2225 --len;
2226 }
2227
2228 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2229 }
2230
2231 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
2232
4cbfc3ac 2233 print_dot = i->output_section->vma + i->output_offset + size / opb;
252b5132
RH
2234 }
2235 }
2236}
2237
2238static void
2239print_fill_statement (fill)
2240 lang_fill_statement_type * fill;
2241{
2242 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
2243}
2244
2245static void
2246print_data_statement (data)
2247 lang_data_statement_type * data;
2248{
2249 int i;
2250 bfd_vma addr;
2251 bfd_size_type size;
2252 const char *name;
f6af82bd
AM
2253 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2254 ldfile_output_machine);
252b5132
RH
2255
2256 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2257 print_space ();
2258
2259 addr = data->output_vma;
2260 if (data->output_section != NULL)
2261 addr += data->output_section->vma;
2262
2263 switch (data->type)
2264 {
2265 default:
2266 abort ();
2267 case BYTE:
2268 size = BYTE_SIZE;
2269 name = "BYTE";
2270 break;
2271 case SHORT:
2272 size = SHORT_SIZE;
2273 name = "SHORT";
2274 break;
2275 case LONG:
2276 size = LONG_SIZE;
2277 name = "LONG";
2278 break;
2279 case QUAD:
2280 size = QUAD_SIZE;
2281 name = "QUAD";
2282 break;
2283 case SQUAD:
2284 size = QUAD_SIZE;
2285 name = "SQUAD";
2286 break;
2287 }
2288
2289 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2290
2291 if (data->exp->type.node_class != etree_value)
2292 {
2293 print_space ();
2294 exp_print_tree (data->exp);
2295 }
2296
2297 print_nl ();
2298
4cbfc3ac
TW
2299 print_dot = addr + size / opb;
2300
252b5132
RH
2301}
2302
2303/* Print an address statement. These are generated by options like
2304 -Ttext. */
2305
2306static void
2307print_address_statement (address)
2308 lang_address_statement_type *address;
2309{
2310 minfo (_("Address of section %s set to "), address->section_name);
2311 exp_print_tree (address->address);
2312 print_nl ();
2313}
2314
2315/* Print a reloc statement. */
2316
2317static void
2318print_reloc_statement (reloc)
2319 lang_reloc_statement_type *reloc;
2320{
2321 int i;
2322 bfd_vma addr;
2323 bfd_size_type size;
f6af82bd
AM
2324 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2325 ldfile_output_machine);
252b5132
RH
2326
2327 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2328 print_space ();
2329
2330 addr = reloc->output_vma;
2331 if (reloc->output_section != NULL)
2332 addr += reloc->output_section->vma;
2333
2334 size = bfd_get_reloc_size (reloc->howto);
2335
2336 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2337
2338 if (reloc->name != NULL)
2339 minfo ("%s+", reloc->name);
2340 else
2341 minfo ("%s+", reloc->section->name);
2342
2343 exp_print_tree (reloc->addend_exp);
2344
2345 print_nl ();
2346
4cbfc3ac 2347 print_dot = addr + size / opb;
252b5132
RH
2348}
2349
2350static void
2351print_padding_statement (s)
2352 lang_padding_statement_type *s;
2353{
2354 int len;
2355 bfd_vma addr;
f6af82bd
AM
2356 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2357 ldfile_output_machine);
252b5132
RH
2358
2359 minfo (" *fill*");
2360
2361 len = sizeof " *fill*" - 1;
2362 while (len < SECTION_NAME_MAP_LENGTH)
2363 {
2364 print_space ();
2365 ++len;
2366 }
2367
2368 addr = s->output_offset;
2369 if (s->output_section != NULL)
2370 addr += s->output_section->vma;
2371 minfo ("0x%V %W", addr, s->size);
2372
2373 if (s->fill != 0)
2374 minfo (" %u", s->fill);
2375
2376 print_nl ();
2377
4cbfc3ac 2378 print_dot = addr + s->size / opb;
252b5132
RH
2379}
2380
2381static void
2382print_wild_statement (w, os)
2383 lang_wild_statement_type * w;
2384 lang_output_section_statement_type * os;
2385{
2386 print_space ();
2387
2388 if (w->filenames_sorted)
2389 minfo ("SORT(");
18625d54
CM
2390 if (w->exclude_filename_list != NULL)
2391 {
2392 name_list *tmp;
2393 minfo ("EXCLUDE_FILE ( %s", w->exclude_filename_list->name);
2394 for (tmp=w->exclude_filename_list->next; tmp; tmp = tmp->next)
2395 minfo (", %s", tmp->name);
2396 minfo (")");
2397 }
2398 if (w->filename != NULL)
252b5132
RH
2399 minfo ("%s", w->filename);
2400 else
2401 minfo ("*");
2402 if (w->filenames_sorted)
2403 minfo (")");
2404
2405 minfo ("(");
2406 if (w->sections_sorted)
2407 minfo ("SORT(");
2408 if (w->section_name != NULL)
2409 minfo ("%s", w->section_name);
2410 else
2411 minfo ("*");
2412 if (w->sections_sorted)
2413 minfo (")");
2414 minfo (")");
2415
2416 print_nl ();
2417
2418 print_statement_list (w->children.head, os);
2419}
2420
2421/* Print a group statement. */
2422
2423static void
2424print_group (s, os)
2425 lang_group_statement_type *s;
2426 lang_output_section_statement_type *os;
2427{
2428 fprintf (config.map_file, "START GROUP\n");
2429 print_statement_list (s->children.head, os);
2430 fprintf (config.map_file, "END GROUP\n");
2431}
2432
2433/* Print the list of statements in S.
2434 This can be called for any statement type. */
2435
2436static void
2437print_statement_list (s, os)
2438 lang_statement_union_type *s;
2439 lang_output_section_statement_type *os;
2440{
2441 while (s != NULL)
2442 {
2443 print_statement (s, os);
2444 s = s->next;
2445 }
2446}
2447
2448/* Print the first statement in statement list S.
2449 This can be called for any statement type. */
2450
2451static void
2452print_statement (s, os)
2453 lang_statement_union_type *s;
2454 lang_output_section_statement_type *os;
2455{
2456 switch (s->header.type)
2457 {
2458 default:
2459 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2460 FAIL ();
2461 break;
2462 case lang_constructors_statement_enum:
2463 if (constructor_list.head != NULL)
2464 {
2465 if (constructors_sorted)
2466 minfo (" SORT (CONSTRUCTORS)\n");
2467 else
2468 minfo (" CONSTRUCTORS\n");
2469 print_statement_list (constructor_list.head, os);
2470 }
2471 break;
2472 case lang_wild_statement_enum:
2473 print_wild_statement (&s->wild_statement, os);
2474 break;
2475 case lang_address_statement_enum:
2476 print_address_statement (&s->address_statement);
2477 break;
2478 case lang_object_symbols_statement_enum:
2479 minfo (" CREATE_OBJECT_SYMBOLS\n");
2480 break;
2481 case lang_fill_statement_enum:
2482 print_fill_statement (&s->fill_statement);
2483 break;
2484 case lang_data_statement_enum:
2485 print_data_statement (&s->data_statement);
2486 break;
2487 case lang_reloc_statement_enum:
2488 print_reloc_statement (&s->reloc_statement);
2489 break;
2490 case lang_input_section_enum:
2491 print_input_section (&s->input_section);
2492 break;
2493 case lang_padding_statement_enum:
2494 print_padding_statement (&s->padding_statement);
2495 break;
2496 case lang_output_section_statement_enum:
2497 print_output_section_statement (&s->output_section_statement);
2498 break;
2499 case lang_assignment_statement_enum:
2500 print_assignment (&s->assignment_statement, os);
2501 break;
2502 case lang_target_statement_enum:
2503 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2504 break;
2505 case lang_output_statement_enum:
2506 minfo ("OUTPUT(%s", s->output_statement.name);
2507 if (output_target != NULL)
2508 minfo (" %s", output_target);
2509 minfo (")\n");
2510 break;
2511 case lang_input_statement_enum:
2512 print_input_statement (&s->input_statement);
2513 break;
2514 case lang_group_statement_enum:
2515 print_group (&s->group_statement, os);
2516 break;
2517 case lang_afile_asection_pair_statement_enum:
2518 FAIL ();
2519 break;
2520 }
2521}
2522
2523static void
2524print_statements ()
2525{
2526 print_statement_list (statement_list.head, abs_output_section);
2527}
2528
2529/* Print the first N statements in statement list S to STDERR.
2530 If N == 0, nothing is printed.
2531 If N < 0, the entire list is printed.
2532 Intended to be called from GDB. */
2533
2534void
2535dprint_statement (s, n)
2536 lang_statement_union_type * s;
2537 int n;
2538{
2539 FILE *map_save = config.map_file;
2540
2541 config.map_file = stderr;
2542
2543 if (n < 0)
2544 print_statement_list (s, abs_output_section);
2545 else
2546 {
2547 while (s && --n >= 0)
2548 {
2549 print_statement (s, abs_output_section);
2550 s = s->next;
2551 }
2552 }
2553
2554 config.map_file = map_save;
2555}
2556
2557static bfd_vma
2558insert_pad (this_ptr, fill, power, output_section_statement, dot)
2559 lang_statement_union_type ** this_ptr;
2560 fill_type fill;
2561 unsigned int power;
2562 asection * output_section_statement;
2563 bfd_vma dot;
2564{
2565 /* Align this section first to the
2566 input sections requirement, then
2567 to the output section's requirement.
2568 If this alignment is > than any seen before,
2569 then record it too. Perform the alignment by
2570 inserting a magic 'padding' statement.
2571 */
2572
f6af82bd
AM
2573 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2574 ldfile_output_machine);
252b5132
RH
2575 unsigned int alignment_needed = align_power (dot, power) - dot;
2576
2577 if (alignment_needed != 0)
2578 {
2579 lang_statement_union_type *new =
2580 ((lang_statement_union_type *)
2581 stat_alloc (sizeof (lang_padding_statement_type)));
2582
2583 /* Link into existing chain */
2584 new->header.next = *this_ptr;
2585 *this_ptr = new;
2586 new->header.type = lang_padding_statement_enum;
2587 new->padding_statement.output_section = output_section_statement;
2588 new->padding_statement.output_offset =
2589 dot - output_section_statement->vma;
2590 new->padding_statement.fill = fill;
4cbfc3ac 2591 new->padding_statement.size = alignment_needed * opb;
252b5132
RH
2592 }
2593
2594
2595 /* Remember the most restrictive alignment */
2596 if (power > output_section_statement->alignment_power)
2597 {
2598 output_section_statement->alignment_power = power;
2599 }
4cbfc3ac 2600 output_section_statement->_raw_size += alignment_needed * opb;
252b5132 2601
4cbfc3ac 2602 return dot + alignment_needed;
252b5132
RH
2603}
2604
2605/* Work out how much this section will move the dot point */
2606static bfd_vma
2607size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2608 lang_statement_union_type ** this_ptr;
2609 lang_output_section_statement_type * output_section_statement;
2610 fill_type fill;
2611 bfd_vma dot;
87f2a346 2612 boolean relax ATTRIBUTE_UNUSED;
252b5132
RH
2613{
2614 lang_input_section_type *is = &((*this_ptr)->input_section);
2615 asection *i = is->section;
f6af82bd
AM
2616 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2617 ldfile_output_machine);
252b5132
RH
2618
2619 if (is->ifile->just_syms_flag == false)
2620 {
2621 if (output_section_statement->subsection_alignment != -1)
2622 i->alignment_power =
2623 output_section_statement->subsection_alignment;
2624
2625 dot = insert_pad (this_ptr, fill, i->alignment_power,
2626 output_section_statement->bfd_section, dot);
2627
2628 /* Remember where in the output section this input section goes */
2629
2630 i->output_offset = dot - output_section_statement->bfd_section->vma;
2631
2632 /* Mark how big the output section must be to contain this now
2633 */
2634 if (i->_cooked_size != 0)
4cbfc3ac 2635 dot += i->_cooked_size / opb;
252b5132 2636 else
4cbfc3ac
TW
2637 dot += i->_raw_size / opb;
2638 output_section_statement->bfd_section->_raw_size =
2639 (dot - output_section_statement->bfd_section->vma) * opb;
252b5132
RH
2640 }
2641 else
2642 {
2643 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2644 }
2645
2646 return dot;
2647}
2648
33275c22 2649#define IGNORE_SECTION(bfd, s) \
50e05050 2650 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
33275c22
NC
2651 || bfd_section_size (bfd, s) == 0)
2652
252b5132
RH
2653/* Check to see if any allocated sections overlap with other allocated
2654 sections. This can happen when the linker script specifically specifies
2655 the output section addresses of the two sections. */
2656static void
2657lang_check_section_addresses ()
2658{
2659 asection * s;
f6af82bd 2660 unsigned opb = bfd_octets_per_byte (output_bfd);
252b5132
RH
2661
2662 /* Scan all sections in the output list. */
2663 for (s = output_bfd->sections; s != NULL; s = s->next)
33275c22
NC
2664 {
2665 asection * os;
2666
2667 /* Ignore sections which are not loaded or which have no contents. */
2668 if (IGNORE_SECTION (output_bfd, s))
2669 continue;
2670
2671 /* Once we reach section 's' stop our seach. This prevents two
2672 warning messages from being produced, one for 'section A overlaps
2673 section B' and one for 'section B overlaps section A'. */
2674 for (os = output_bfd->sections; os != s; os = os->next)
2675 {
2676 bfd_vma s_start;
2677 bfd_vma s_end;
2678 bfd_vma os_start;
2679 bfd_vma os_end;
2680
2681 /* Only consider loadable sections with real contents. */
2682 if (IGNORE_SECTION (output_bfd, os))
2683 continue;
252b5132 2684
33275c22
NC
2685 /* We must check the sections' LMA addresses not their
2686 VMA addresses because overlay sections can have
2687 overlapping VMAs but they must have distinct LMAs. */
2688 s_start = bfd_section_lma (output_bfd, s);
2689 os_start = bfd_section_lma (output_bfd, os);
9e4ed18c
TW
2690 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2691 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
33275c22
NC
2692
2693 /* Look for an overlap. */
2694 if ((s_end < os_start) || (s_start > os_end))
2695 continue;
2696
2697 einfo (
252b5132 2698_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
33275c22
NC
2699 s->name, s_start, s_end, os->name, os_start, os_end);
2700
2701 /* Once we have found one overlap for this section,
2702 stop looking for others. */
2703 break;
2704 }
2705 }
252b5132
RH
2706}
2707
2708/* This variable indicates whether bfd_relax_section should be called
2709 again. */
2710
2711static boolean relax_again;
2712
562d3460
TW
2713/* Make sure the new address is within the region. We explicitly permit the
2714 current address to be at the exact end of the region when the address is
2715 non-zero, in case the region is at the end of addressable memory and the
2716 calculation wraps around. */
2717
2718static void
2719os_region_check (os, region, tree, base)
2720 lang_output_section_statement_type *os;
2721 struct memory_region_struct *region;
2722 etree_type *tree;
2723 bfd_vma base;
2724{
2725 if ((region->current < region->origin
2726 || (region->current - region->origin > region->length))
2727 && ((region->current != region->origin + region->length)
2728 || base == 0))
2729 {
2730 if (tree != (etree_type *) NULL)
2731 {
2732 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2733 region->current,
2734 os->bfd_section->owner,
2735 os->bfd_section->name,
2736 region->name);
2737 }
2738 else
2739 {
2740 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2741 region->name,
2742 os->bfd_section->owner,
2743 os->bfd_section->name);
2744 }
2745 /* Reset the region pointer. */
2746 region->current = region->origin;
2747 }
2748}
2749
252b5132
RH
2750/* Set the sizes for all the output sections. */
2751
2752bfd_vma
2753lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2754 lang_statement_union_type * s;
2755 lang_output_section_statement_type * output_section_statement;
2756 lang_statement_union_type ** prev;
2757 fill_type fill;
2758 bfd_vma dot;
2759 boolean relax;
2760{
f6af82bd
AM
2761 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2762 ldfile_output_machine);
4cbfc3ac 2763
252b5132
RH
2764 /* Size up the sections from their constituent parts. */
2765 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2766 {
2767 switch (s->header.type)
2768 {
2769 case lang_output_section_statement_enum:
2770 {
2771 bfd_vma after;
2772 lang_output_section_statement_type *os = &s->output_section_statement;
2773
2774 if (os->bfd_section == NULL)
2775 /* This section was never actually created. */
2776 break;
2777
2778 /* If this is a COFF shared library section, use the size and
2779 address from the input section. FIXME: This is COFF
2780 specific; it would be cleaner if there were some other way
2781 to do this, but nothing simple comes to mind. */
2782 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2783 {
2784 asection * input;
2785
2786 if (os->children.head == NULL
2787 || os->children.head->next != NULL
2788 || os->children.head->header.type != lang_input_section_enum)
2789 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2790 os->name);
2791
2792 input = os->children.head->input_section.section;
2793 bfd_set_section_vma (os->bfd_section->owner,
2794 os->bfd_section,
2795 bfd_section_vma (input->owner, input));
2796 os->bfd_section->_raw_size = input->_raw_size;
2797 break;
2798 }
2799
2800 if (bfd_is_abs_section (os->bfd_section))
2801 {
2802 /* No matter what happens, an abs section starts at zero. */
2803 ASSERT (os->bfd_section->vma == 0);
2804 }
2805 else
2806 {
2807 if (os->addr_tree == (etree_type *) NULL)
2808 {
2809 /* No address specified for this section, get one
2810 from the region specification. */
2811 if (os->region == (lang_memory_region_type *) NULL
2812 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2813 & (SEC_ALLOC | SEC_LOAD)) != 0)
2814 && os->region->name[0] == '*'
2815 && strcmp (os->region->name, "*default*") == 0))
2816 {
2817 os->region = lang_memory_default (os->bfd_section);
2818 }
2819
2820 /* If a loadable section is using the default memory
2821 region, and some non default memory regions were
2822 defined, issue a warning. */
2823 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2824 & (SEC_ALLOC | SEC_LOAD)) != 0
2825 && ! link_info.relocateable
2826 && strcmp (os->region->name, "*default*") == 0
2827 && lang_memory_region_list != NULL
2828 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2829 || lang_memory_region_list->next != NULL))
2830 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2831 bfd_get_section_name (output_bfd, os->bfd_section));
2832
2833 dot = os->region->current;
2834
2835 if (os->section_alignment == -1)
2836 {
2837 bfd_vma olddot;
2838
2839 olddot = dot;
2840 dot = align_power (dot, os->bfd_section->alignment_power);
2841
2842 if (dot != olddot && config.warn_section_align)
2843 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2844 os->name, (unsigned int) (dot - olddot));
2845 }
2846 }
2847 else
2848 {
2849 etree_value_type r;
2850
2851 r = exp_fold_tree (os->addr_tree,
2852 abs_output_section,
2853 lang_allocating_phase_enum,
2854 dot, &dot);
2855 if (r.valid_p == false)
2856 {
2857 einfo (_("%F%S: non constant address expression for section %s\n"),
2858 os->name);
2859 }
2860 dot = r.value + r.section->bfd_section->vma;
2861 }
2862
2863 /* The section starts here.
2864 First, align to what the section needs. */
2865
2866 if (os->section_alignment != -1)
2867 dot = align_power (dot, os->section_alignment);
2868
2869 bfd_set_section_vma (0, os->bfd_section, dot);
2870
2871 os->bfd_section->output_offset = 0;
2872 }
2873
2874 (void) lang_size_sections (os->children.head, os, &os->children.head,
2875 os->fill, dot, relax);
2876
4cbfc3ac
TW
2877 /* put the section within the requested block size, or align at
2878 the block boundary */
32edc927
TW
2879 after = ALIGN_N (os->bfd_section->vma
2880 + os->bfd_section->_raw_size / opb,
252b5132
RH
2881 /* The coercion here is important, see ld.h. */
2882 (bfd_vma) os->block_value);
2883
2884 if (bfd_is_abs_section (os->bfd_section))
2885 ASSERT (after == os->bfd_section->vma);
2886 else
4cbfc3ac
TW
2887 os->bfd_section->_raw_size =
2888 (after - os->bfd_section->vma) * opb;
2889 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
252b5132
RH
2890 os->processed = true;
2891
2892 /* Update dot in the region ?
2893 We only do this if the section is going to be allocated,
2894 since unallocated sections do not contribute to the region's
13392b77 2895 overall size in memory.
cce4c4c5
NC
2896
2897 If the SEC_NEVER_LOAD bit is not set, it will affect the
2898 addresses of sections after it. We have to update
2899 dot. */
252b5132 2900 if (os->region != (lang_memory_region_type *) NULL
cce4c4c5
NC
2901 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
2902 & SEC_NEVER_LOAD) == 0
2903 || (bfd_get_section_flags (output_bfd, os->bfd_section)
2904 & (SEC_ALLOC | SEC_LOAD))))
252b5132
RH
2905 {
2906 os->region->current = dot;
2907
562d3460
TW
2908 /* Make sure the new address is within the region. */
2909 os_region_check (os, os->region, os->addr_tree,
2910 os->bfd_section->vma);
2911
2912 /* if there's no load address specified, use the run region as
2913 the load region */
2914 if (os->lma_region == NULL && os->load_base == NULL)
2915 os->lma_region = os->region;
2916
2917 if (os->lma_region != NULL)
2918 {
2919 if (os->load_base != NULL)
2920 {
2921 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
2922 }
2923 else
2924 {
2925 /* don't allocate twice */
2926 if (os->lma_region != os->region)
2927 {
2928 /* set load_base, which will be handled later */
2929 os->load_base = exp_intop (os->lma_region->current);
2930 os->lma_region->current +=
2931 os->bfd_section->_raw_size / opb;
2932 os_region_check (os, os->lma_region, NULL,
2933 os->bfd_section->lma);
2934 }
2935 }
2936 }
252b5132
RH
2937 }
2938 }
2939 break;
2940
2941 case lang_constructors_statement_enum:
2942 dot = lang_size_sections (constructor_list.head,
2943 output_section_statement,
2944 &s->wild_statement.children.head,
2945 fill,
2946 dot, relax);
2947 break;
2948
2949 case lang_data_statement_enum:
2950 {
2951 unsigned int size = 0;
2952
2953 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2954 s->data_statement.output_section =
2955 output_section_statement->bfd_section;
2956
2957 switch (s->data_statement.type)
2958 {
9e4ed18c
TW
2959 default:
2960 abort();
252b5132
RH
2961 case QUAD:
2962 case SQUAD:
2963 size = QUAD_SIZE;
2964 break;
2965 case LONG:
2966 size = LONG_SIZE;
2967 break;
2968 case SHORT:
2969 size = SHORT_SIZE;
2970 break;
2971 case BYTE:
2972 size = BYTE_SIZE;
2973 break;
2974 }
4cbfc3ac
TW
2975 if (size < opb)
2976 size = opb;
2977 dot += size / opb;
252b5132
RH
2978 output_section_statement->bfd_section->_raw_size += size;
2979 /* The output section gets contents, and then we inspect for
2980 any flags set in the input script which override any ALLOC. */
2981 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2982 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2983 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2984 }
2985 }
2986 break;
2987
2988 case lang_reloc_statement_enum:
2989 {
2990 int size;
2991
2992 s->reloc_statement.output_vma =
2993 dot - output_section_statement->bfd_section->vma;
2994 s->reloc_statement.output_section =
2995 output_section_statement->bfd_section;
2996 size = bfd_get_reloc_size (s->reloc_statement.howto);
4cbfc3ac 2997 dot += size / opb;
252b5132
RH
2998 output_section_statement->bfd_section->_raw_size += size;
2999 }
3000 break;
3001
3002 case lang_wild_statement_enum:
3003
3004 dot = lang_size_sections (s->wild_statement.children.head,
3005 output_section_statement,
3006 &s->wild_statement.children.head,
3007
3008 fill, dot, relax);
3009
3010 break;
3011
3012 case lang_object_symbols_statement_enum:
3013 link_info.create_object_symbols_section =
3014 output_section_statement->bfd_section;
3015 break;
3016 case lang_output_statement_enum:
3017 case lang_target_statement_enum:
3018 break;
3019 case lang_input_section_enum:
3020 {
3021 asection *i;
3022
3023 i = (*prev)->input_section.section;
3024 if (! relax)
3025 {
3026 if (i->_cooked_size == 0)
3027 i->_cooked_size = i->_raw_size;
3028 }
3029 else
3030 {
3031 boolean again;
3032
3033 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3034 einfo (_("%P%F: can't relax section: %E\n"));
3035 if (again)
3036 relax_again = true;
3037 }
3038 dot = size_input_section (prev,
3039 output_section_statement,
3040 output_section_statement->fill,
3041 dot, relax);
3042 }
3043 break;
3044 case lang_input_statement_enum:
3045 break;
3046 case lang_fill_statement_enum:
3047 s->fill_statement.output_section = output_section_statement->bfd_section;
3048
3049 fill = s->fill_statement.fill;
3050 break;
3051 case lang_assignment_statement_enum:
3052 {
3053 bfd_vma newdot = dot;
3054
3055 exp_fold_tree (s->assignment_statement.exp,
3056 output_section_statement,
3057 lang_allocating_phase_enum,
3058 dot,
3059 &newdot);
3060
3061 if (newdot != dot)
3062 {
3063 /* The assignment changed dot. Insert a pad. */
3064 if (output_section_statement == abs_output_section)
3065 {
3066 /* If we don't have an output section, then just adjust
3067 the default memory address. */
3068 lang_memory_region_lookup ("*default*")->current = newdot;
3069 }
3070 else if (!relax)
3071 {
3072 lang_statement_union_type *new =
3073 ((lang_statement_union_type *)
3074 stat_alloc (sizeof (lang_padding_statement_type)));
3075
3076 /* Link into existing chain. */
3077 new->header.next = *prev;
3078 *prev = new;
3079 new->header.type = lang_padding_statement_enum;
3080 new->padding_statement.output_section =
3081 output_section_statement->bfd_section;
3082 new->padding_statement.output_offset =
3083 dot - output_section_statement->bfd_section->vma;
3084 new->padding_statement.fill = fill;
4cbfc3ac 3085 new->padding_statement.size = (newdot - dot) * opb;
252b5132
RH
3086 output_section_statement->bfd_section->_raw_size +=
3087 new->padding_statement.size;
3088 }
3089
3090 dot = newdot;
3091 }
3092 }
3093 break;
3094
3095 case lang_padding_statement_enum:
3096 /* If we are relaxing, and this is not the first pass, some
3097 padding statements may have been inserted during previous
3098 passes. We may have to move the padding statement to a new
3099 location if dot has a different value at this point in this
3100 pass than it did at this point in the previous pass. */
3101 s->padding_statement.output_offset =
3102 dot - output_section_statement->bfd_section->vma;
4cbfc3ac 3103 dot += s->padding_statement.size / opb;
252b5132
RH
3104 output_section_statement->bfd_section->_raw_size +=
3105 s->padding_statement.size;
3106 break;
3107
3108 case lang_group_statement_enum:
3109 dot = lang_size_sections (s->group_statement.children.head,
3110 output_section_statement,
3111 &s->group_statement.children.head,
3112 fill, dot, relax);
3113 break;
3114
3115 default:
3116 FAIL ();
3117 break;
3118
3119 /* This can only get here when relaxing is turned on. */
3120
3121 case lang_address_statement_enum:
3122 break;
3123 }
3124 prev = &s->header.next;
3125 }
3126 return dot;
3127}
3128
3129bfd_vma
3130lang_do_assignments (s, output_section_statement, fill, dot)
3131 lang_statement_union_type * s;
3132 lang_output_section_statement_type * output_section_statement;
3133 fill_type fill;
3134 bfd_vma dot;
3135{
f6af82bd
AM
3136 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3137 ldfile_output_machine);
4cbfc3ac 3138
252b5132
RH
3139 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3140 {
3141 switch (s->header.type)
3142 {
3143 case lang_constructors_statement_enum:
3144 dot = lang_do_assignments (constructor_list.head,
3145 output_section_statement,
3146 fill,
3147 dot);
3148 break;
3149
3150 case lang_output_section_statement_enum:
3151 {
3152 lang_output_section_statement_type *os =
3153 &(s->output_section_statement);
3154
3155 if (os->bfd_section != NULL)
3156 {
3157 dot = os->bfd_section->vma;
3158 (void) lang_do_assignments (os->children.head, os,
3159 os->fill, dot);
4cbfc3ac
TW
3160 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3161
252b5132 3162 }
c13b1b77 3163 if (os->load_base)
252b5132
RH
3164 {
3165 /* If nothing has been placed into the output section then
3166 it won't have a bfd_section. */
3167 if (os->bfd_section)
3168 {
3169 os->bfd_section->lma
c13b1b77
NC
3170 = exp_get_abs_int(os->load_base, 0,"load base",
3171 lang_final_phase_enum);
252b5132
RH
3172 }
3173 }
3174 }
3175 break;
3176 case lang_wild_statement_enum:
3177
3178 dot = lang_do_assignments (s->wild_statement.children.head,
3179 output_section_statement,
3180 fill, dot);
3181
3182 break;
3183
3184 case lang_object_symbols_statement_enum:
3185 case lang_output_statement_enum:
3186 case lang_target_statement_enum:
3187#if 0
3188 case lang_common_statement_enum:
3189#endif
3190 break;
3191 case lang_data_statement_enum:
3192 {
3193 etree_value_type value;
3194
3195 value = exp_fold_tree (s->data_statement.exp,
3196 abs_output_section,
3197 lang_final_phase_enum, dot, &dot);
3198 s->data_statement.value = value.value;
3199 if (value.valid_p == false)
3200 einfo (_("%F%P: invalid data statement\n"));
3201 }
4cbfc3ac 3202 {
f6af82bd 3203 unsigned int size;
4cbfc3ac
TW
3204 switch (s->data_statement.type)
3205 {
9e4ed18c
TW
3206 default:
3207 abort();
4cbfc3ac
TW
3208 case QUAD:
3209 case SQUAD:
3210 size = QUAD_SIZE;
3211 break;
3212 case LONG:
3213 size = LONG_SIZE;
3214 break;
3215 case SHORT:
3216 size = SHORT_SIZE;
3217 break;
3218 case BYTE:
3219 size = BYTE_SIZE;
3220 break;
3221 }
3222 if (size < opb)
3223 size = opb;
3224 dot += size / opb;
3225 }
252b5132
RH
3226 break;
3227
3228 case lang_reloc_statement_enum:
3229 {
3230 etree_value_type value;
3231
3232 value = exp_fold_tree (s->reloc_statement.addend_exp,
3233 abs_output_section,
3234 lang_final_phase_enum, dot, &dot);
3235 s->reloc_statement.addend_value = value.value;
3236 if (value.valid_p == false)
3237 einfo (_("%F%P: invalid reloc statement\n"));
3238 }
4cbfc3ac 3239 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
252b5132
RH
3240 break;
3241
3242 case lang_input_section_enum:
3243 {
3244 asection *in = s->input_section.section;
3245
3246 if (in->_cooked_size != 0)
4cbfc3ac 3247 dot += in->_cooked_size / opb;
252b5132 3248 else
4cbfc3ac 3249 dot += in->_raw_size / opb;
252b5132
RH
3250 }
3251 break;
3252
3253 case lang_input_statement_enum:
3254 break;
3255 case lang_fill_statement_enum:
3256 fill = s->fill_statement.fill;
3257 break;
3258 case lang_assignment_statement_enum:
3259 {
3260 exp_fold_tree (s->assignment_statement.exp,
3261 output_section_statement,
3262 lang_final_phase_enum,
3263 dot,
3264 &dot);
3265 }
3266
3267 break;
3268 case lang_padding_statement_enum:
4cbfc3ac 3269 dot += s->padding_statement.size / opb;
252b5132
RH
3270 break;
3271
3272 case lang_group_statement_enum:
3273 dot = lang_do_assignments (s->group_statement.children.head,
3274 output_section_statement,
3275 fill, dot);
3276
3277 break;
3278
3279 default:
3280 FAIL ();
3281 break;
3282 case lang_address_statement_enum:
3283 break;
3284 }
3285
3286 }
3287 return dot;
3288}
3289
3290/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3291 operator .startof. (section_name), it produces an undefined symbol
3292 .startof.section_name. Similarly, when it sees
3293 .sizeof. (section_name), it produces an undefined symbol
3294 .sizeof.section_name. For all the output sections, we look for
3295 such symbols, and set them to the correct value. */
3296
3297static void
3298lang_set_startof ()
3299{
3300 asection *s;
3301
3302 if (link_info.relocateable)
3303 return;
3304
3305 for (s = output_bfd->sections; s != NULL; s = s->next)
3306 {
3307 const char *secname;
3308 char *buf;
3309 struct bfd_link_hash_entry *h;
3310
3311 secname = bfd_get_section_name (output_bfd, s);
3312 buf = xmalloc (10 + strlen (secname));
3313
3314 sprintf (buf, ".startof.%s", secname);
3315 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3316 if (h != NULL && h->type == bfd_link_hash_undefined)
3317 {
3318 h->type = bfd_link_hash_defined;
3319 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3320 h->u.def.section = bfd_abs_section_ptr;
3321 }
3322
3323 sprintf (buf, ".sizeof.%s", secname);
3324 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3325 if (h != NULL && h->type == bfd_link_hash_undefined)
3326 {
f6af82bd
AM
3327 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3328 ldfile_output_machine);
252b5132
RH
3329 h->type = bfd_link_hash_defined;
3330 if (s->_cooked_size != 0)
4cbfc3ac 3331 h->u.def.value = s->_cooked_size / opb;
252b5132 3332 else
4cbfc3ac 3333 h->u.def.value = s->_raw_size / opb;
252b5132
RH
3334 h->u.def.section = bfd_abs_section_ptr;
3335 }
3336
3337 free (buf);
3338 }
3339}
3340
3341static void
3342lang_finish ()
3343{
3344 struct bfd_link_hash_entry *h;
3345 boolean warn;
3346
3347 if (link_info.relocateable || link_info.shared)
3348 warn = false;
3349 else
3350 warn = true;
3351
3352 if (entry_symbol == (char *) NULL)
3353 {
3354 /* No entry has been specified. Look for start, but don't warn
3355 if we don't find it. */
3356 entry_symbol = "start";
3357 warn = false;
3358 }
3359
3360 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
3361 if (h != (struct bfd_link_hash_entry *) NULL
3362 && (h->type == bfd_link_hash_defined
3363 || h->type == bfd_link_hash_defweak)
3364 && h->u.def.section->output_section != NULL)
3365 {
3366 bfd_vma val;
3367
3368 val = (h->u.def.value
3369 + bfd_get_section_vma (output_bfd,
3370 h->u.def.section->output_section)
3371 + h->u.def.section->output_offset);
3372 if (! bfd_set_start_address (output_bfd, val))
3373 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
3374 }
3375 else
3376 {
3377 bfd_vma val;
3378 CONST char *send;
3379
3380 /* We couldn't find the entry symbol. Try parsing it as a
3381 number. */
3382 val = bfd_scan_vma (entry_symbol, &send, 0);
3383 if (*send == '\0')
3384 {
3385 if (! bfd_set_start_address (output_bfd, val))
3386 einfo (_("%P%F: can't set start address\n"));
3387 }
3388 else
3389 {
3390 asection *ts;
3391
3392 /* Can't find the entry symbol, and it's not a number. Use
3393 the first address in the text section. */
3394 ts = bfd_get_section_by_name (output_bfd, ".text");
3395 if (ts != (asection *) NULL)
3396 {
3397 if (warn)
3398 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3399 entry_symbol, bfd_get_section_vma (output_bfd, ts));
3400 if (! bfd_set_start_address (output_bfd,
3401 bfd_get_section_vma (output_bfd,
3402 ts)))
3403 einfo (_("%P%F: can't set start address\n"));
3404 }
3405 else
3406 {
3407 if (warn)
3408 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3409 entry_symbol);
3410 }
3411 }
3412 }
3413}
3414
3415/* This is a small function used when we want to ignore errors from
3416 BFD. */
3417
3418static void
3419#ifdef ANSI_PROTOTYPES
87f2a346 3420ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
252b5132
RH
3421#else
3422ignore_bfd_errors (s)
87f2a346 3423 const char *s ATTRIBUTE_UNUSED;
252b5132
RH
3424#endif
3425{
3426 /* Don't do anything. */
3427}
3428
3429/* Check that the architecture of all the input files is compatible
3430 with the output file. Also call the backend to let it do any
3431 other checking that is needed. */
3432
3433static void
3434lang_check ()
3435{
3436 lang_statement_union_type *file;
3437 bfd *input_bfd;
3438 CONST bfd_arch_info_type *compatible;
3439
3440 for (file = file_chain.head;
3441 file != (lang_statement_union_type *) NULL;
3442 file = file->input_statement.next)
3443 {
3444 input_bfd = file->input_statement.the_bfd;
3445 compatible = bfd_arch_get_compatible (input_bfd,
3446 output_bfd);
3447 if (compatible == NULL)
3448 {
3449 if (command_line.warn_mismatch)
3450 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3451 bfd_printable_name (input_bfd), input_bfd,
3452 bfd_printable_name (output_bfd));
3453 }
3454 else
3455 {
3456 bfd_error_handler_type pfn = NULL;
3457
3458 /* If we aren't supposed to warn about mismatched input
3459 files, temporarily set the BFD error handler to a
3460 function which will do nothing. We still want to call
3461 bfd_merge_private_bfd_data, since it may set up
3462 information which is needed in the output file. */
3463 if (! command_line.warn_mismatch)
3464 pfn = bfd_set_error_handler (ignore_bfd_errors);
3465 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3466 {
3467 if (command_line.warn_mismatch)
3468 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3469 input_bfd);
3470 }
3471 if (! command_line.warn_mismatch)
3472 bfd_set_error_handler (pfn);
3473 }
3474 }
3475}
3476
3477/* Look through all the global common symbols and attach them to the
3478 correct section. The -sort-common command line switch may be used
3479 to roughly sort the entries by size. */
3480
3481static void
3482lang_common ()
3483{
3484 if (link_info.relocateable
3485 && ! command_line.force_common_definition)
3486 return;
3487
3488 if (! config.sort_common)
3489 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3490 else
3491 {
3492 int power;
3493
3494 for (power = 4; power >= 0; power--)
3495 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3496 (PTR) &power);
3497 }
3498}
3499
3500/* Place one common symbol in the correct section. */
3501
3502static boolean
3503lang_one_common (h, info)
3504 struct bfd_link_hash_entry *h;
3505 PTR info;
3506{
3507 unsigned int power_of_two;
3508 bfd_vma size;
3509 asection *section;
f6af82bd
AM
3510 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3511 ldfile_output_machine);
252b5132
RH
3512
3513 if (h->type != bfd_link_hash_common)
3514 return true;
3515
3516 size = h->u.c.size;
3517 power_of_two = h->u.c.p->alignment_power;
3518
3519 if (config.sort_common
3520 && power_of_two < (unsigned int) *(int *) info)
3521 return true;
3522
3523 section = h->u.c.p->section;
3524
3525 /* Increase the size of the section. */
4cbfc3ac
TW
3526 section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb,
3527 (bfd_size_type) (1 << power_of_two)) * opb;
252b5132
RH
3528
3529 /* Adjust the alignment if necessary. */
3530 if (power_of_two > section->alignment_power)
3531 section->alignment_power = power_of_two;
3532
3533 /* Change the symbol from common to defined. */
3534 h->type = bfd_link_hash_defined;
3535 h->u.def.section = section;
3536 h->u.def.value = section->_cooked_size;
3537
3538 /* Increase the size of the section. */
3539 section->_cooked_size += size;
3540
3541 /* Make sure the section is allocated in memory, and make sure that
3542 it is no longer a common section. */
3543 section->flags |= SEC_ALLOC;
3544 section->flags &= ~ SEC_IS_COMMON;
3545
3546 if (config.map_file != NULL)
3547 {
3548 static boolean header_printed;
3549 int len;
3550 char *name;
3551 char buf[50];
3552
3553 if (! header_printed)
3554 {
3555 minfo (_("\nAllocating common symbols\n"));
3556 minfo (_("Common symbol size file\n\n"));
3557 header_printed = true;
3558 }
3559
3560 name = demangle (h->root.string);
3561 minfo ("%s", name);
3562 len = strlen (name);
3563 free (name);
3564
3565 if (len >= 19)
3566 {
3567 print_nl ();
3568 len = 0;
3569 }
3570 while (len < 20)
3571 {
3572 print_space ();
3573 ++len;
3574 }
3575
3576 minfo ("0x");
3577 if (size <= 0xffffffff)
3578 sprintf (buf, "%lx", (unsigned long) size);
3579 else
3580 sprintf_vma (buf, size);
3581 minfo ("%s", buf);
3582 len = strlen (buf);
3583
3584 while (len < 16)
3585 {
3586 print_space ();
3587 ++len;
3588 }
3589
3590 minfo ("%B\n", section->owner);
3591 }
3592
3593 return true;
3594}
3595
3596/*
3597run through the input files and ensure that every input
3598section has somewhere to go. If one is found without
3599a destination then create an input request and place it
3600into the statement tree.
3601*/
3602
3603static void
3604lang_place_orphans ()
3605{
e50d8076 3606 LANG_FOR_EACH_INPUT_STATEMENT (file)
252b5132
RH
3607 {
3608 asection *s;
3609
3610 for (s = file->the_bfd->sections;
3611 s != (asection *) NULL;
3612 s = s->next)
3613 {
3614 if (s->output_section == (asection *) NULL)
3615 {
3616 /* This section of the file is not attatched, root
3617 around for a sensible place for it to go */
3618
3619 if (file->just_syms_flag)
3620 {
3621 /* We are only retrieving symbol values from this
3622 file. We want the symbols to act as though the
3623 values in the file are absolute. */
3624 s->output_section = bfd_abs_section_ptr;
3625 s->output_offset = s->vma;
3626 }
3627 else if (strcmp (s->name, "COMMON") == 0)
3628 {
3629 /* This is a lonely common section which must have
3630 come from an archive. We attach to the section
3631 with the wildcard. */
3632 if (! link_info.relocateable
3633 || command_line.force_common_definition)
3634 {
3635 if (default_common_section == NULL)
3636 {
3637#if 0
3638 /* This message happens when using the
3639 svr3.ifile linker script, so I have
3640 disabled it. */
3641 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3642#endif
3643 default_common_section =
3644 lang_output_section_statement_lookup (".bss");
3645
3646 }
3647 wild_doit (&default_common_section->children, s,
3648 default_common_section, file);
3649 }
3650 }
3651 else if (ldemul_place_orphan (file, s))
3652 ;
3653 else
3654 {
3655 lang_output_section_statement_type *os =
3656 lang_output_section_statement_lookup (s->name);
3657
3658 wild_doit (&os->children, s, os, file);
3659 }
3660 }
3661 }
3662 }
3663}
3664
3665
3666void
aa8804e4 3667lang_set_flags (ptr, flags, invert)
252b5132
RH
3668 lang_memory_region_type *ptr;
3669 CONST char *flags;
aa8804e4 3670 int invert;
252b5132 3671{
aa8804e4 3672 flagword *ptr_flags;
252b5132 3673
aa8804e4 3674 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
252b5132
RH
3675 while (*flags)
3676 {
3677 switch (*flags)
3678 {
252b5132
RH
3679 case 'A': case 'a':
3680 *ptr_flags |= SEC_ALLOC;
3681 break;
3682
3683 case 'R': case 'r':
3684 *ptr_flags |= SEC_READONLY;
3685 break;
3686
3687 case 'W': case 'w':
3688 *ptr_flags |= SEC_DATA;
3689 break;
3690
3691 case 'X': case 'x':
3692 *ptr_flags |= SEC_CODE;
3693 break;
3694
3695 case 'L': case 'l':
3696 case 'I': case 'i':
3697 *ptr_flags |= SEC_LOAD;
3698 break;
3699
3700 default:
3701 einfo (_("%P%F: invalid syntax in flags\n"));
3702 break;
3703 }
3704 flags++;
3705 }
3706}
3707
3708/* Call a function on each input file. This function will be called
3709 on an archive, but not on the elements. */
3710
3711void
3712lang_for_each_input_file (func)
3713 void (*func) PARAMS ((lang_input_statement_type *));
3714{
3715 lang_input_statement_type *f;
3716
3717 for (f = (lang_input_statement_type *) input_file_chain.head;
3718 f != NULL;
3719 f = (lang_input_statement_type *) f->next_real_file)
3720 func (f);
3721}
3722
3723/* Call a function on each file. The function will be called on all
3724 the elements of an archive which are included in the link, but will
3725 not be called on the archive file itself. */
3726
3727void
3728lang_for_each_file (func)
3729 void (*func) PARAMS ((lang_input_statement_type *));
3730{
e50d8076 3731 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132
RH
3732 {
3733 func (f);
3734 }
3735}
3736
3737#if 0
3738
3739/* Not used. */
3740
3741void
3742lang_for_each_input_section (func)
3743 void (*func) PARAMS ((bfd * ab, asection * as));
3744{
e50d8076 3745 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132 3746 {
e50d8076 3747 asection * s;
252b5132
RH
3748
3749 for (s = f->the_bfd->sections;
3750 s != (asection *) NULL;
3751 s = s->next)
3752 {
3753 func (f->the_bfd, s);
3754 }
3755 }
3756}
3757
3758#endif
3759
3760void
3761ldlang_add_file (entry)
3762 lang_input_statement_type * entry;
3763{
3764 bfd **pp;
3765
3766 lang_statement_append (&file_chain,
3767 (lang_statement_union_type *) entry,
3768 &entry->next);
3769
3770 /* The BFD linker needs to have a list of all input BFDs involved in
3771 a link. */
3772 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3773 ASSERT (entry->the_bfd != output_bfd);
3774 for (pp = &link_info.input_bfds;
3775 *pp != (bfd *) NULL;
3776 pp = &(*pp)->link_next)
3777 ;
3778 *pp = entry->the_bfd;
3779 entry->the_bfd->usrdata = (PTR) entry;
3780 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3781
3782 /* Look through the sections and check for any which should not be
3783 included in the link. We need to do this now, so that we can
3784 notice when the backend linker tries to report multiple
3785 definition errors for symbols which are in sections we aren't
3786 going to link. FIXME: It might be better to entirely ignore
3787 symbols which are defined in sections which are going to be
3788 discarded. This would require modifying the backend linker for
3789 each backend which might set the SEC_LINK_ONCE flag. If we do
3790 this, we should probably handle SEC_EXCLUDE in the same way. */
3791
3792 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3793}
3794
3795void
3796lang_add_output (name, from_script)
3797 CONST char *name;
3798 int from_script;
3799{
3800 /* Make -o on command line override OUTPUT in script. */
3801 if (had_output_filename == false || !from_script)
3802 {
3803 output_filename = name;
3804 had_output_filename = true;
3805 }
3806}
3807
3808
3809static lang_output_section_statement_type *current_section;
3810
3811static int
3812topower (x)
3813 int x;
3814{
3815 unsigned int i = 1;
3816 int l;
3817
3818 if (x < 0)
3819 return -1;
3820
3821 for (l = 0; l < 32; l++)
3822 {
3823 if (i >= (unsigned int) x)
3824 return l;
3825 i <<= 1;
3826 }
3827
3828 return 0;
3829}
3830
aea4bd9d 3831lang_output_section_statement_type *
252b5132
RH
3832lang_enter_output_section_statement (output_section_statement_name,
3833 address_exp, sectype, block_value,
3834 align, subalign, ebase)
3835 const char *output_section_statement_name;
3836 etree_type * address_exp;
3837 enum section_type sectype;
3838 bfd_vma block_value;
3839 etree_type *align;
3840 etree_type *subalign;
3841 etree_type *ebase;
3842{
3843 lang_output_section_statement_type *os;
3844
3845 current_section =
3846 os =
3847 lang_output_section_statement_lookup (output_section_statement_name);
3848
3849
3850
3851 /* Add this statement to tree */
3852 /* add_statement(lang_output_section_statement_enum,
3853 output_section_statement);*/
3854 /* Make next things chain into subchain of this */
3855
3856 if (os->addr_tree ==
3857 (etree_type *) NULL)
3858 {
3859 os->addr_tree =
3860 address_exp;
3861 }
3862 os->sectype = sectype;
3863 if (sectype != noload_section)
3864 os->flags = SEC_NO_FLAGS;
3865 else
3866 os->flags = SEC_NEVER_LOAD;
3867 os->block_value = block_value ? block_value : 1;
3868 stat_ptr = &os->children;
3869
3870 os->subsection_alignment = topower(
3871 exp_get_value_int(subalign, -1,
3872 "subsection alignment",
3873 0));
3874 os->section_alignment = topower(
3875 exp_get_value_int(align, -1,
3876 "section alignment", 0));
3877
3878 os->load_base = ebase;
aea4bd9d 3879 return os;
252b5132
RH
3880}
3881
252b5132
RH
3882void
3883lang_final ()
3884{
3885 lang_output_statement_type *new =
3886 new_stat (lang_output_statement, stat_ptr);
3887
3888 new->name = output_filename;
3889}
3890
3891/* Reset the current counters in the regions */
3892static void
3893reset_memory_regions ()
3894{
3895 lang_memory_region_type *p = lang_memory_region_list;
3896
3897 for (p = lang_memory_region_list;
3898 p != (lang_memory_region_type *) NULL;
3899 p = p->next)
3900 {
3901 p->old_length = (bfd_size_type) (p->current - p->origin);
3902 p->current = p->origin;
3903 }
3904}
3905
252b5132
RH
3906/* Expand a wild statement for a particular FILE, marking its sections KEEP
3907 as needed. SECTION may be NULL, in which case it is a wild card. */
3908
3909static void
4dec4d4e 3910gc_section_callback (ptr, section, file, data)
252b5132 3911 lang_wild_statement_type *ptr;
4dec4d4e 3912 asection *section;
87f2a346
ILT
3913 lang_input_statement_type *file ATTRIBUTE_UNUSED;
3914 void *data ATTRIBUTE_UNUSED;
252b5132 3915{
4dec4d4e
RH
3916 /* If the wild pattern was marked KEEP, the member sections
3917 should be as well. */
3918 if (ptr->keep_sections)
3919 section->flags |= SEC_KEEP;
252b5132
RH
3920}
3921
3922/* Handle a wild statement, marking it against GC. SECTION or FILE or both
3923 may be NULL, indicating that it is a wildcard. */
3924
3925static void
3926lang_gc_wild (s, section, file)
3927 lang_wild_statement_type *s;
3928 const char *section;
3929 const char *file;
3930{
4dec4d4e 3931 walk_wild (s, section, file, gc_section_callback, NULL);
252b5132
RH
3932}
3933
3934/* Iterate over sections marking them against GC. */
3935
3936static void
3937lang_gc_sections_1 (s)
3938 lang_statement_union_type * s;
3939{
3940 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3941 {
3942 switch (s->header.type)
3943 {
3944 case lang_wild_statement_enum:
3945 lang_gc_wild (&s->wild_statement,
3946 s->wild_statement.section_name,
3947 s->wild_statement.filename);
3948 break;
3949 case lang_constructors_statement_enum:
3950 lang_gc_sections_1 (constructor_list.head);
3951 break;
3952 case lang_output_section_statement_enum:
3953 lang_gc_sections_1 (s->output_section_statement.children.head);
3954 break;
3955 case lang_group_statement_enum:
3956 lang_gc_sections_1 (s->group_statement.children.head);
3957 break;
3958 default:
3959 break;
3960 }
3961 }
3962}
3963
3964static void
3965lang_gc_sections ()
3966{
3967 struct bfd_link_hash_entry *h;
3968 ldlang_undef_chain_list_type *ulist, fake_list_start;
3969
3970 /* Keep all sections so marked in the link script. */
3971
3972 lang_gc_sections_1 (statement_list.head);
3973
3974 /* Keep all sections containing symbols undefined on the command-line.
3975 Handle the entry symbol at the same time. */
7c83b342
DE
3976
3977 if (entry_symbol != NULL)
3978 {
3979 fake_list_start.next = ldlang_undef_chain_list_head;
3980 fake_list_start.name = (char *) entry_symbol;
3981 ulist = &fake_list_start;
3982 }
252b5132 3983 else
7c83b342 3984 ulist = ldlang_undef_chain_list_head;
252b5132 3985
7c83b342 3986 for (; ulist; ulist = ulist->next)
252b5132
RH
3987 {
3988 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3989 false, false, false);
3990
3991 if (h != (struct bfd_link_hash_entry *) NULL
3992 && (h->type == bfd_link_hash_defined
3993 || h->type == bfd_link_hash_defweak)
3994 && ! bfd_is_abs_section (h->u.def.section))
3995 {
3996 h->u.def.section->flags |= SEC_KEEP;
3997 }
3998 }
3999
4000 bfd_gc_sections (output_bfd, &link_info);
4001}
4002
4003void
4004lang_process ()
4005{
4006 lang_reasonable_defaults ();
4007 current_target = default_target;
4008
4009 lang_for_each_statement (ldlang_open_output); /* Open the output file */
4010
4011 ldemul_create_output_section_statements ();
4012
4013 /* Add to the hash table all undefineds on the command line */
4014 lang_place_undefineds ();
4015
9503fd87
ILT
4016 already_linked_table_init ();
4017
252b5132
RH
4018 /* Create a bfd for each input file */
4019 current_target = default_target;
4020 open_input_bfds (statement_list.head, false);
4021
4022 ldemul_after_open ();
4023
9503fd87
ILT
4024 already_linked_table_free ();
4025
252b5132
RH
4026 /* Make sure that we're not mixing architectures. We call this
4027 after all the input files have been opened, but before we do any
4028 other processing, so that any operations merge_private_bfd_data
4029 does on the output file will be known during the rest of the
4030 link. */
4031 lang_check ();
4032
4033 /* Handle .exports instead of a version script if we're told to do so. */
4034 if (command_line.version_exports_section)
4035 lang_do_version_exports_section ();
4036
4037 /* Build all sets based on the information gathered from the input
4038 files. */
4039 ldctor_build_sets ();
4040
4041 /* Remove unreferenced sections if asked to. */
4042 if (command_line.gc_sections)
4043 lang_gc_sections ();
4044
4045 /* Size up the common data */
4046 lang_common ();
4047
4048 /* Run through the contours of the script and attach input sections
4049 to the correct output sections
4050 */
4051 map_input_to_output_sections (statement_list.head, (char *) NULL,
4052 (lang_output_section_statement_type *) NULL);
4053
4054
4055 /* Find any sections not attached explicitly and handle them */
4056 lang_place_orphans ();
4057
4058 ldemul_before_allocation ();
4059
4060 /* We must record the program headers before we try to fix the
4061 section positions, since they will affect SIZEOF_HEADERS. */
4062 lang_record_phdrs ();
4063
4064 /* Now run around and relax if we can */
4065 if (command_line.relax)
4066 {
4067 /* First time round is a trial run to get the 'worst case'
4068 addresses of the objects if there was no relaxing. */
4069 lang_size_sections (statement_list.head,
4070 abs_output_section,
4071 &(statement_list.head), 0, (bfd_vma) 0, false);
4072
4073 /* Keep relaxing until bfd_relax_section gives up. */
4074 do
4075 {
4076 reset_memory_regions ();
4077
4078 relax_again = false;
4079
4080 /* Note: pe-dll.c does something like this also. If you find
4081 you need to change this code, you probably need to change
4082 pe-dll.c also. DJ */
4083
4084 /* Do all the assignments with our current guesses as to
4085 section sizes. */
4086 lang_do_assignments (statement_list.head,
4087 abs_output_section,
4088 (fill_type) 0, (bfd_vma) 0);
4089
4090 /* Perform another relax pass - this time we know where the
4091 globals are, so can make better guess. */
4092 lang_size_sections (statement_list.head,
4093 abs_output_section,
4094 &(statement_list.head), 0, (bfd_vma) 0, true);
4095 }
4096 while (relax_again);
4097 }
4098 else
4099 {
4100 /* Size up the sections. */
4101 lang_size_sections (statement_list.head,
4102 abs_output_section,
4103 &(statement_list.head), 0, (bfd_vma) 0, false);
4104 }
4105
4106 /* See if anything special should be done now we know how big
4107 everything is. */
4108 ldemul_after_allocation ();
4109
4110 /* Fix any .startof. or .sizeof. symbols. */
4111 lang_set_startof ();
4112
4113 /* Do all the assignments, now that we know the final restingplaces
4114 of all the symbols */
4115
4116 lang_do_assignments (statement_list.head,
4117 abs_output_section,
4118 (fill_type) 0, (bfd_vma) 0);
4119
4120 /* Make sure that the section addresses make sense. */
4121 if (! link_info.relocateable
4122 && command_line.check_section_addresses)
4123 lang_check_section_addresses ();
4124
4125 /* Final stuffs */
4126
4127 ldemul_finish ();
4128 lang_finish ();
4129}
4130
4131/* EXPORTED TO YACC */
4132
4133void
4134lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
18625d54 4135 keep_sections, exclude_filename_list)
252b5132
RH
4136 const char *const section_name;
4137 boolean sections_sorted;
4138 const char *const filename;
4139 boolean filenames_sorted;
4140 boolean keep_sections;
18625d54 4141 struct name_list *exclude_filename_list;
252b5132
RH
4142{
4143 lang_wild_statement_type *new = new_stat (lang_wild_statement,
4144 stat_ptr);
4145
4146 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
4147 {
4148 placed_commons = true;
4149 }
4150 if (filename != NULL && ! wildcardp (filename))
4151 {
4152 lang_has_input_file = true;
4153 }
4154 new->section_name = section_name;
4155 new->sections_sorted = sections_sorted;
4156 new->filename = filename;
4157 new->filenames_sorted = filenames_sorted;
4158 new->keep_sections = keep_sections;
18625d54 4159 new->exclude_filename_list = exclude_filename_list;
252b5132
RH
4160 lang_list_init (&new->children);
4161}
4162
4163void
4164lang_section_start (name, address)
4165 CONST char *name;
4166 etree_type * address;
4167{
4168 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
4169
4170 ad->section_name = name;
4171 ad->address = address;
4172}
4173
4174/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4175 because of a -e argument on the command line, or zero if this is
4176 called by ENTRY in a linker script. Command line arguments take
4177 precedence. */
4178
4179void
4180lang_add_entry (name, cmdline)
4181 CONST char *name;
4182 boolean cmdline;
4183{
4184 if (entry_symbol == NULL
4185 || cmdline
4186 || ! entry_from_cmdline)
4187 {
4188 entry_symbol = name;
4189 entry_from_cmdline = cmdline;
4190 }
4191}
4192
4193void
4194lang_add_target (name)
4195 CONST char *name;
4196{
4197 lang_target_statement_type *new = new_stat (lang_target_statement,
4198 stat_ptr);
4199
4200 new->target = name;
4201
4202}
4203
4204void
4205lang_add_map (name)
4206 CONST char *name;
4207{
4208 while (*name)
4209 {
4210 switch (*name)
4211 {
4212 case 'F':
4213 map_option_f = true;
4214 break;
4215 }
4216 name++;
4217 }
4218}
4219
4220void
4221lang_add_fill (exp)
4222 int exp;
4223{
4224 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4225 stat_ptr);
4226
4227 new->fill = exp;
4228}
4229
4230void
4231lang_add_data (type, exp)
4232 int type;
4233 union etree_union *exp;
4234{
4235
4236 lang_data_statement_type *new = new_stat (lang_data_statement,
4237 stat_ptr);
4238
4239 new->exp = exp;
4240 new->type = type;
4241
4242}
4243
4244/* Create a new reloc statement. RELOC is the BFD relocation type to
4245 generate. HOWTO is the corresponding howto structure (we could
4246 look this up, but the caller has already done so). SECTION is the
4247 section to generate a reloc against, or NAME is the name of the
4248 symbol to generate a reloc against. Exactly one of SECTION and
4249 NAME must be NULL. ADDEND is an expression for the addend. */
4250
4251void
4252lang_add_reloc (reloc, howto, section, name, addend)
4253 bfd_reloc_code_real_type reloc;
4254 reloc_howto_type *howto;
4255 asection *section;
4256 const char *name;
4257 union etree_union *addend;
4258{
4259 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4260
4261 p->reloc = reloc;
4262 p->howto = howto;
4263 p->section = section;
4264 p->name = name;
4265 p->addend_exp = addend;
4266
4267 p->addend_value = 0;
4268 p->output_section = NULL;
4269 p->output_vma = 0;
4270}
4271
4272lang_assignment_statement_type *
4273lang_add_assignment (exp)
4274 etree_type * exp;
4275{
4276 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4277 stat_ptr);
4278
4279 new->exp = exp;
4280 return new;
4281}
4282
4283void
4284lang_add_attribute (attribute)
4285 enum statement_enum attribute;
4286{
4287 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4288}
4289
4290void
4291lang_startup (name)
4292 CONST char *name;
4293{
4294 if (startup_file != (char *) NULL)
4295 {
4296 einfo (_("%P%Fmultiple STARTUP files\n"));
4297 }
4298 first_file->filename = name;
4299 first_file->local_sym_name = name;
4300 first_file->real = true;
4301
4302 startup_file = name;
4303}
4304
4305void
4306lang_float (maybe)
4307 boolean maybe;
4308{
4309 lang_float_flag = maybe;
4310}
4311
4312void
562d3460 4313lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
252b5132
RH
4314 bfd_vma fill;
4315 const char *memspec;
4316 struct lang_output_section_phdr_list *phdrs;
562d3460 4317 const char *lma_memspec;
252b5132
RH
4318{
4319 current_section->fill = fill;
4320 current_section->region = lang_memory_region_lookup (memspec);
562d3460
TW
4321 if (strcmp (lma_memspec, "*default*") != 0)
4322 {
4323 current_section->lma_region = lang_memory_region_lookup (lma_memspec);
4324 /* if no runtime region has been given, but the load region has been,
4325 use the load region */
4326 if (strcmp (memspec, "*default*") == 0)
4327 current_section->region = lang_memory_region_lookup (lma_memspec);
4328 }
252b5132
RH
4329 current_section->phdrs = phdrs;
4330 stat_ptr = &statement_list;
4331}
4332
4333/*
4334 Create an absolute symbol with the given name with the value of the
4335 address of first byte of the section named.
4336
4337 If the symbol already exists, then do nothing.
4338*/
4339void
4340lang_abs_symbol_at_beginning_of (secname, name)
4341 const char *secname;
4342 const char *name;
4343{
4344 struct bfd_link_hash_entry *h;
4345
4346 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4347 if (h == (struct bfd_link_hash_entry *) NULL)
4348 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4349
4350 if (h->type == bfd_link_hash_new
4351 || h->type == bfd_link_hash_undefined)
4352 {
4353 asection *sec;
4354
4355 h->type = bfd_link_hash_defined;
4356
4357 sec = bfd_get_section_by_name (output_bfd, secname);
4358 if (sec == (asection *) NULL)
4359 h->u.def.value = 0;
4360 else
4361 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4362
4363 h->u.def.section = bfd_abs_section_ptr;
4364 }
4365}
4366
4367/*
4368 Create an absolute symbol with the given name with the value of the
4369 address of the first byte after the end of the section named.
4370
4371 If the symbol already exists, then do nothing.
4372*/
4373void
4374lang_abs_symbol_at_end_of (secname, name)
4375 const char *secname;
4376 const char *name;
4377{
4378 struct bfd_link_hash_entry *h;
4379
4380 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4381 if (h == (struct bfd_link_hash_entry *) NULL)
4382 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4383
4384 if (h->type == bfd_link_hash_new
4385 || h->type == bfd_link_hash_undefined)
4386 {
4387 asection *sec;
4388
4389 h->type = bfd_link_hash_defined;
4390
4391 sec = bfd_get_section_by_name (output_bfd, secname);
4392 if (sec == (asection *) NULL)
4393 h->u.def.value = 0;
4394 else
4395 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4cbfc3ac
TW
4396 + bfd_section_size (output_bfd, sec) /
4397 bfd_octets_per_byte (output_bfd));
252b5132
RH
4398
4399 h->u.def.section = bfd_abs_section_ptr;
4400 }
4401}
4402
4403void
4404lang_statement_append (list, element, field)
4405 lang_statement_list_type * list;
4406 lang_statement_union_type * element;
4407 lang_statement_union_type ** field;
4408{
4409 *(list->tail) = element;
4410 list->tail = field;
4411}
4412
4413/* Set the output format type. -oformat overrides scripts. */
4414
4415void
4416lang_add_output_format (format, big, little, from_script)
4417 const char *format;
4418 const char *big;
4419 const char *little;
4420 int from_script;
4421{
4422 if (output_target == NULL || !from_script)
4423 {
4424 if (command_line.endian == ENDIAN_BIG
4425 && big != NULL)
4426 format = big;
4427 else if (command_line.endian == ENDIAN_LITTLE
4428 && little != NULL)
4429 format = little;
4430
4431 output_target = format;
4432 }
4433}
4434
4435/* Enter a group. This creates a new lang_group_statement, and sets
4436 stat_ptr to build new statements within the group. */
4437
4438void
4439lang_enter_group ()
4440{
4441 lang_group_statement_type *g;
4442
4443 g = new_stat (lang_group_statement, stat_ptr);
4444 lang_list_init (&g->children);
4445 stat_ptr = &g->children;
4446}
4447
4448/* Leave a group. This just resets stat_ptr to start writing to the
4449 regular list of statements again. Note that this will not work if
4450 groups can occur inside anything else which can adjust stat_ptr,
4451 but currently they can't. */
4452
4453void
4454lang_leave_group ()
4455{
4456 stat_ptr = &statement_list;
4457}
4458
4459/* Add a new program header. This is called for each entry in a PHDRS
4460 command in a linker script. */
4461
4462void
4463lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4464 const char *name;
4465 etree_type *type;
4466 boolean filehdr;
4467 boolean phdrs;
4468 etree_type *at;
4469 etree_type *flags;
4470{
4471 struct lang_phdr *n, **pp;
4472
4473 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4474 n->next = NULL;
4475 n->name = name;
4476 n->type = exp_get_value_int (type, 0, "program header type",
4477 lang_final_phase_enum);
4478 n->filehdr = filehdr;
4479 n->phdrs = phdrs;
4480 n->at = at;
4481 n->flags = flags;
4482
4483 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4484 ;
4485 *pp = n;
4486}
4487
4488/* Record the program header information in the output BFD. FIXME: We
4489 should not be calling an ELF specific function here. */
4490
4491static void
4492lang_record_phdrs ()
4493{
4494 unsigned int alc;
4495 asection **secs;
4496 struct lang_output_section_phdr_list *last;
4497 struct lang_phdr *l;
4498 lang_statement_union_type *u;
4499
4500 alc = 10;
4501 secs = (asection **) xmalloc (alc * sizeof (asection *));
4502 last = NULL;
4503 for (l = lang_phdr_list; l != NULL; l = l->next)
4504 {
4505 unsigned int c;
4506 flagword flags;
4507 bfd_vma at;
4508
4509 c = 0;
4510 for (u = lang_output_section_statement.head;
4511 u != NULL;
4512 u = u->output_section_statement.next)
4513 {
4514 lang_output_section_statement_type *os;
4515 struct lang_output_section_phdr_list *pl;
4516
4517 os = &u->output_section_statement;
4518
4519 pl = os->phdrs;
4520 if (pl != NULL)
4521 last = pl;
4522 else
4523 {
4524 if (os->sectype == noload_section
4525 || os->bfd_section == NULL
4526 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4527 continue;
4528 pl = last;
4529 }
4530
4531 if (os->bfd_section == NULL)
4532 continue;
4533
4534 for (; pl != NULL; pl = pl->next)
4535 {
4536 if (strcmp (pl->name, l->name) == 0)
4537 {
4538 if (c >= alc)
4539 {
4540 alc *= 2;
4541 secs = ((asection **)
4542 xrealloc (secs, alc * sizeof (asection *)));
4543 }
4544 secs[c] = os->bfd_section;
4545 ++c;
4546 pl->used = true;
4547 }
4548 }
4549 }
4550
4551 if (l->flags == NULL)
4552 flags = 0;
4553 else
4554 flags = exp_get_vma (l->flags, 0, "phdr flags",
4555 lang_final_phase_enum);
4556
4557 if (l->at == NULL)
4558 at = 0;
4559 else
4560 at = exp_get_vma (l->at, 0, "phdr load address",
4561 lang_final_phase_enum);
4562
4563 if (! bfd_record_phdr (output_bfd, l->type,
4564 l->flags == NULL ? false : true,
4565 flags,
4566 l->at == NULL ? false : true,
4567 at, l->filehdr, l->phdrs, c, secs))
4568 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4569 }
4570
4571 free (secs);
4572
4573 /* Make sure all the phdr assignments succeeded. */
4574 for (u = lang_output_section_statement.head;
4575 u != NULL;
4576 u = u->output_section_statement.next)
4577 {
4578 struct lang_output_section_phdr_list *pl;
4579
4580 if (u->output_section_statement.bfd_section == NULL)
4581 continue;
4582
4583 for (pl = u->output_section_statement.phdrs;
4584 pl != NULL;
4585 pl = pl->next)
4586 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4587 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4588 u->output_section_statement.name, pl->name);
4589 }
4590}
4591
4592/* Record a list of sections which may not be cross referenced. */
4593
4594void
4595lang_add_nocrossref (l)
4596 struct lang_nocrossref *l;
4597{
4598 struct lang_nocrossrefs *n;
4599
4600 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4601 n->next = nocrossref_list;
4602 n->list = l;
4603 nocrossref_list = n;
4604
4605 /* Set notice_all so that we get informed about all symbols. */
4606 link_info.notice_all = true;
4607}
4608\f
4609/* Overlay handling. We handle overlays with some static variables. */
4610
4611/* The overlay virtual address. */
4612static etree_type *overlay_vma;
4613
4614/* The overlay load address. */
4615static etree_type *overlay_lma;
4616
4617/* Whether nocrossrefs is set for this overlay. */
4618static int overlay_nocrossrefs;
4619
4620/* An expression for the maximum section size seen so far. */
4621static etree_type *overlay_max;
4622
4623/* A list of all the sections in this overlay. */
4624
4625struct overlay_list
4626{
4627 struct overlay_list *next;
4628 lang_output_section_statement_type *os;
4629};
4630
4631static struct overlay_list *overlay_list;
4632
4633/* Start handling an overlay. */
4634
4635void
4636lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4637 etree_type *vma_expr;
4638 etree_type *lma_expr;
4639 int nocrossrefs;
4640{
4641 /* The grammar should prevent nested overlays from occurring. */
4642 ASSERT (overlay_vma == NULL
4643 && overlay_lma == NULL
4644 && overlay_list == NULL
4645 && overlay_max == NULL);
4646
4647 overlay_vma = vma_expr;
4648 overlay_lma = lma_expr;
4649 overlay_nocrossrefs = nocrossrefs;
4650}
4651
4652/* Start a section in an overlay. We handle this by calling
4653 lang_enter_output_section_statement with the correct VMA and LMA. */
4654
4655void
4656lang_enter_overlay_section (name)
4657 const char *name;
4658{
4659 struct overlay_list *n;
4660 etree_type *size;
4661
4662 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4663 0, 0, 0, overlay_lma);
4664
4665 /* If this is the first section, then base the VMA and LMA of future
4666 sections on this one. This will work correctly even if `.' is
4667 used in the addresses. */
4668 if (overlay_list == NULL)
4669 {
4670 overlay_vma = exp_nameop (ADDR, name);
4671 overlay_lma = exp_nameop (LOADADDR, name);
4672 }
4673
4674 /* Remember the section. */
4675 n = (struct overlay_list *) xmalloc (sizeof *n);
4676 n->os = current_section;
4677 n->next = overlay_list;
4678 overlay_list = n;
4679
4680 size = exp_nameop (SIZEOF, name);
4681
4682 /* Adjust the LMA for the next section. */
4683 overlay_lma = exp_binop ('+', overlay_lma, size);
4684
4685 /* Arrange to work out the maximum section end address. */
4686 if (overlay_max == NULL)
4687 overlay_max = size;
4688 else
4689 overlay_max = exp_binop (MAX_K, overlay_max, size);
4690}
4691
4692/* Finish a section in an overlay. There isn't any special to do
4693 here. */
4694
4695void
4696lang_leave_overlay_section (fill, phdrs)
4697 bfd_vma fill;
4698 struct lang_output_section_phdr_list *phdrs;
4699{
4700 const char *name;
4701 char *clean, *s2;
4702 const char *s1;
4703 char *buf;
4704
4705 name = current_section->name;
4706
562d3460
TW
4707 lang_leave_output_section_statement (fill, "*default*",
4708 phdrs, "*default*");
252b5132
RH
4709
4710 /* Define the magic symbols. */
4711
4712 clean = xmalloc (strlen (name) + 1);
4713 s2 = clean;
4714 for (s1 = name; *s1 != '\0'; s1++)
4715 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4716 *s2++ = *s1;
4717 *s2 = '\0';
4718
4719 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4720 sprintf (buf, "__load_start_%s", clean);
4721 lang_add_assignment (exp_assop ('=', buf,
4722 exp_nameop (LOADADDR, name)));
4723
4724 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4725 sprintf (buf, "__load_stop_%s", clean);
4726 lang_add_assignment (exp_assop ('=', buf,
4727 exp_binop ('+',
4728 exp_nameop (LOADADDR, name),
4729 exp_nameop (SIZEOF, name))));
4730
4731 free (clean);
4732}
4733
4734/* Finish an overlay. If there are any overlay wide settings, this
4735 looks through all the sections in the overlay and sets them. */
4736
4737void
562d3460 4738lang_leave_overlay (fill, memspec, phdrs, lma_memspec)
252b5132
RH
4739 bfd_vma fill;
4740 const char *memspec;
4741 struct lang_output_section_phdr_list *phdrs;
562d3460 4742 const char *lma_memspec;
252b5132
RH
4743{
4744 lang_memory_region_type *region;
562d3460 4745 lang_memory_region_type *lma_region;
252b5132
RH
4746 struct overlay_list *l;
4747 struct lang_nocrossref *nocrossref;
4748
4749 if (memspec == NULL)
4750 region = NULL;
4751 else
4752 region = lang_memory_region_lookup (memspec);
4753
562d3460
TW
4754 if (lma_memspec == NULL)
4755 lma_region = NULL;
4756 else
4757 lma_region = lang_memory_region_lookup (lma_memspec);
4758
252b5132
RH
4759 nocrossref = NULL;
4760
4761 l = overlay_list;
4762 while (l != NULL)
4763 {
4764 struct overlay_list *next;
4765
4766 if (fill != 0 && l->os->fill == 0)
4767 l->os->fill = fill;
4768 if (region != NULL && l->os->region == NULL)
4769 l->os->region = region;
562d3460
TW
4770 if (lma_region != NULL && l->os->lma_region == NULL)
4771 l->os->lma_region = lma_region;
252b5132
RH
4772 if (phdrs != NULL && l->os->phdrs == NULL)
4773 l->os->phdrs = phdrs;
4774
4775 if (overlay_nocrossrefs)
4776 {
4777 struct lang_nocrossref *nc;
4778
4779 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4780 nc->name = l->os->name;
4781 nc->next = nocrossref;
4782 nocrossref = nc;
4783 }
4784
4785 next = l->next;
4786 free (l);
4787 l = next;
4788 }
4789
4790 if (nocrossref != NULL)
4791 lang_add_nocrossref (nocrossref);
4792
4793 /* Update . for the end of the overlay. */
4794 lang_add_assignment (exp_assop ('=', ".",
4795 exp_binop ('+', overlay_vma, overlay_max)));
4796
4797 overlay_vma = NULL;
4798 overlay_lma = NULL;
4799 overlay_nocrossrefs = 0;
4800 overlay_list = NULL;
4801 overlay_max = NULL;
4802}
4803\f
4804/* Version handling. This is only useful for ELF. */
4805
4806/* This global variable holds the version tree that we build. */
4807
4808struct bfd_elf_version_tree *lang_elf_version_info;
4809
4810static int
4811lang_vers_match_lang_c (expr, sym)
4812 struct bfd_elf_version_expr *expr;
4813 const char *sym;
4814{
4815 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4816 return 1;
4817 return fnmatch (expr->pattern, sym, 0) == 0;
4818}
4819
4820static int
4821lang_vers_match_lang_cplusplus (expr, sym)
4822 struct bfd_elf_version_expr *expr;
4823 const char *sym;
4824{
4825 char *alt_sym;
4826 int result;
4827
4828 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4829 return 1;
4830
4831 alt_sym = cplus_demangle(sym, /* DMGL_NO_TPARAMS */ 0);
4832 if (!alt_sym)
4833 {
4834 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4835 Should we early out false in this case? */
4836 result = fnmatch (expr->pattern, sym, 0) == 0;
4837 }
4838 else
4839 {
4840 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4841 free (alt_sym);
4842 }
4843
4844 return result;
4845}
4846
4847static int
4848lang_vers_match_lang_java (expr, sym)
4849 struct bfd_elf_version_expr *expr;
4850 const char *sym;
4851{
4852 char *alt_sym;
4853 int result;
4854
4855 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4856 return 1;
4857
4858 alt_sym = cplus_demangle(sym, DMGL_JAVA);
4859 if (!alt_sym)
4860 {
4861 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4862 Should we early out false in this case? */
4863 result = fnmatch (expr->pattern, sym, 0) == 0;
4864 }
4865 else
4866 {
4867 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4868 free (alt_sym);
4869 }
4870
4871 return result;
4872}
4873
4874/* This is called for each variable name or match expression. */
4875
4876struct bfd_elf_version_expr *
4877lang_new_vers_regex (orig, new, lang)
4878 struct bfd_elf_version_expr *orig;
4879 const char *new;
4880 const char *lang;
4881{
4882 struct bfd_elf_version_expr *ret;
4883
4884 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4885 ret->next = orig;
4886 ret->pattern = new;
4887
4888 if (lang == NULL || strcasecmp (lang, "C") == 0)
4889 ret->match = lang_vers_match_lang_c;
4890 else if (strcasecmp (lang, "C++") == 0)
4891 ret->match = lang_vers_match_lang_cplusplus;
4892 else if (strcasecmp (lang, "Java") == 0)
4893 ret->match = lang_vers_match_lang_java;
4894 else
4895 {
4896 einfo (_("%X%P: unknown language `%s' in version information\n"),
4897 lang);
4898 ret->match = lang_vers_match_lang_c;
4899 }
4900
4901 return ret;
4902}
4903
4904/* This is called for each set of variable names and match
4905 expressions. */
4906
4907struct bfd_elf_version_tree *
4908lang_new_vers_node (globals, locals)
4909 struct bfd_elf_version_expr *globals;
4910 struct bfd_elf_version_expr *locals;
4911{
4912 struct bfd_elf_version_tree *ret;
4913
4914 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4915 ret->next = NULL;
4916 ret->name = NULL;
4917 ret->vernum = 0;
4918 ret->globals = globals;
4919 ret->locals = locals;
4920 ret->deps = NULL;
4921 ret->name_indx = (unsigned int) -1;
4922 ret->used = 0;
4923 return ret;
4924}
4925
4926/* This static variable keeps track of version indices. */
4927
4928static int version_index;
4929
4930/* This is called when we know the name and dependencies of the
4931 version. */
4932
4933void
4934lang_register_vers_node (name, version, deps)
4935 const char *name;
4936 struct bfd_elf_version_tree *version;
4937 struct bfd_elf_version_deps *deps;
4938{
4939 struct bfd_elf_version_tree *t, **pp;
4940 struct bfd_elf_version_expr *e1;
4941
4942 /* Make sure this node has a unique name. */
4943 for (t = lang_elf_version_info; t != NULL; t = t->next)
4944 if (strcmp (t->name, name) == 0)
4945 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4946
4947 /* Check the global and local match names, and make sure there
4948 aren't any duplicates. */
4949
4950 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4951 {
4952 for (t = lang_elf_version_info; t != NULL; t = t->next)
4953 {
4954 struct bfd_elf_version_expr *e2;
4955
4956 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4957 if (strcmp (e1->pattern, e2->pattern) == 0)
4958 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4959 e1->pattern);
4960 }
4961 }
4962
4963 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4964 {
4965 for (t = lang_elf_version_info; t != NULL; t = t->next)
4966 {
4967 struct bfd_elf_version_expr *e2;
4968
4969 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4970 if (strcmp (e1->pattern, e2->pattern) == 0)
4971 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4972 e1->pattern);
4973 }
4974 }
4975
4976 version->deps = deps;
4977 version->name = name;
4978 ++version_index;
4979 version->vernum = version_index;
4980
4981 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4982 ;
4983 *pp = version;
4984}
4985
4986/* This is called when we see a version dependency. */
4987
4988struct bfd_elf_version_deps *
4989lang_add_vers_depend (list, name)
4990 struct bfd_elf_version_deps *list;
4991 const char *name;
4992{
4993 struct bfd_elf_version_deps *ret;
4994 struct bfd_elf_version_tree *t;
4995
4996 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4997 ret->next = list;
4998
4999 for (t = lang_elf_version_info; t != NULL; t = t->next)
5000 {
5001 if (strcmp (t->name, name) == 0)
5002 {
5003 ret->version_needed = t;
5004 return ret;
5005 }
5006 }
5007
5008 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5009
5010 return ret;
5011}
5012
5013static void
5014lang_do_version_exports_section ()
5015{
5016 struct bfd_elf_version_expr *greg = NULL, *lreg;
5017
5018 LANG_FOR_EACH_INPUT_STATEMENT (is)
5019 {
5020 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5021 char *contents, *p;
5022 bfd_size_type len;
5023
5024 if (sec == NULL)
5025 continue;
5026
5027 len = bfd_section_size (is->the_bfd, sec);
5028 contents = xmalloc (len);
5029 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5030 einfo (_("%X%P: unable to read .exports section contents"), sec);
5031
5032 p = contents;
5033 while (p < contents+len)
5034 {
5035 greg = lang_new_vers_regex (greg, p, NULL);
5036 p = strchr (p, '\0') + 1;
5037 }
5038
5039 /* Do not free the contents, as we used them creating the regex. */
5040
5041 /* Do not include this section in the link. */
5042 bfd_set_section_flags (is->the_bfd, sec,
5043 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5044 }
5045
5046 lreg = lang_new_vers_regex (NULL, "*", NULL);
5047 lang_register_vers_node (command_line.version_exports_section,
5048 lang_new_vers_node (greg, lreg), NULL);
5049}