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