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