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