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