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