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