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