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