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