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