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