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