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