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