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