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