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