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