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