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