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