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