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