* values.c, value.h: New functions value_{headof,from_vtable_info}.
[deliverable/binutils-gdb.git] / ld / ldlang.c
CommitLineData
2fa0b342
DHW
1/* Copyright (C) 1991 Free Software Foundation, Inc.
2
3This file is part of GLD, the Gnu Linker.
4
5GLD is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 1, or (at your option)
8any later version.
9
10GLD is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GLD; see the file COPYING. If not, write to
17the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19/* $Id$
20 *
2fa0b342
DHW
21*/
22
23
24
25#include "sysdep.h"
26#include "bfd.h"
27
28#include "ld.h"
29#include "ldmain.h"
30#include "ldsym.h"
31#include "ldgram.tab.h"
32#include "ldmisc.h"
33#include "ldlang.h"
34#include "ldexp.h"
35#include "ld-emul.h"
36#include "ldlex.h"
37
2fa0b342
DHW
38/* FORWARDS */
39PROTO(static void, print_statements,(void));
40PROTO(static void, print_statement,(lang_statement_union_type *,
41 lang_output_section_statement_type *));
42
43
d4c02e29
SC
44/* LOCALS */
45static CONST char *startup_file;
46static lang_statement_list_type input_file_chain;
47static boolean placed_commons = false;
48static lang_output_section_statement_type *default_common_section;
49static boolean map_option_f;
50static bfd_vma print_dot;
51static lang_input_statement_type *first_file;
52static lang_statement_list_type lang_output_section_statement;
53static CONST char *current_target;
54static CONST char *output_target;
55static size_t longest_section_name = 8;
56static asection common_section;
57static section_userdata_type common_section_userdata;
58static lang_statement_list_type statement_list;
2fa0b342 59/* EXPORTS */
2fa0b342 60
d4c02e29
SC
61lang_statement_list_type *stat_ptr = &statement_list;
62lang_input_statement_type *script_file = 0;
63boolean option_longmap = false;
64lang_statement_list_type file_chain = {0};
65CONST char *entry_symbol = 0;
66size_t largest_section = 0;
67boolean lang_has_input_file = false;
68lang_output_section_statement_type *create_object_symbols = 0;
69boolean had_output_filename = false;
70boolean lang_float_flag = false;
71/* IMPORTS */
72extern char *default_target;
2fa0b342 73
d4c02e29
SC
74extern unsigned int undefined_global_sym_count;
75extern char *current_file;
2fa0b342 76extern bfd *output_bfd;
2fa0b342
DHW
77extern enum bfd_architecture ldfile_output_architecture;
78extern unsigned long ldfile_output_machine;
79extern char *ldfile_output_machine_name;
2fa0b342 80extern ldsym_type *symbol_head;
d4c02e29 81extern unsigned int commons_pending;
2fa0b342
DHW
82extern args_type command_line;
83extern ld_config_type config;
2fa0b342 84extern boolean had_script;
2fa0b342
DHW
85extern boolean write_map;
86
87
2fa0b342
DHW
88#ifdef __STDC__
89#define cat(a,b) a##b
90#else
91#define cat(a,b) a/**/b
92#endif
93
94#define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
95
96#define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
97
98#define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
99
d4c02e29 100
2fa0b342 101
1418c83b
SC
102/*----------------------------------------------------------------------
103 lang_for_each_statement walks the parse tree and calls the provided
104 function for each node
105*/
106
107static void
108DEFUN(lang_for_each_statement_worker,(func, s),
109 void (*func)() AND
110 lang_statement_union_type *s)
111{
112 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
113 {
114 func(s);
115
116 switch (s->header.type) {
117 case lang_output_section_statement_enum:
118 lang_for_each_statement_worker
119 (func,
120 s->output_section_statement.children.head);
121 break;
122 case lang_wild_statement_enum:
123 lang_for_each_statement_worker
124 (func,
125 s->wild_statement.children.head);
126 break;
127 case lang_data_statement_enum:
128 case lang_object_symbols_statement_enum:
129 case lang_output_statement_enum:
130 case lang_target_statement_enum:
131 case lang_input_section_enum:
132 case lang_input_statement_enum:
133 case lang_fill_statement_enum:
134 case lang_assignment_statement_enum:
135 case lang_padding_statement_enum:
136 case lang_address_statement_enum:
137 break;
138 default:
139 FAIL();
140 break;
141 }
142 }
143}
144
145void
146DEFUN(lang_for_each_statement,(func),
147 void (*func)())
148{
149 lang_for_each_statement_worker(func,
150 statement_list.head);
151}
152/*----------------------------------------------------------------------*/
153static void
154DEFUN(lang_list_init,(list),
155 lang_statement_list_type *list)
2fa0b342
DHW
156{
157list->head = (lang_statement_union_type *)NULL;
158list->tail = &list->head;
159}
160
1418c83b
SC
161/*----------------------------------------------------------------------
162 Functions to print the link map
163 */
164
2fa0b342 165static void
1418c83b
SC
166DEFUN(print_section,(name),
167 CONST char *CONST name)
2fa0b342
DHW
168{
169 printf("%*s", -longest_section_name, name);
170}
171static void
1418c83b 172DEFUN_VOID(print_space)
2fa0b342
DHW
173{
174 printf(" ");
175}
176static void
1418c83b 177DEFUN_VOID(print_nl)
2fa0b342
DHW
178{
179 printf("\n");
180}
181static void
1418c83b
SC
182DEFUN(print_address,(value),
183 bfd_vma value)
2fa0b342
DHW
184{
185 printf("%8lx", value);
186}
187static void
1418c83b
SC
188DEFUN(print_size,(value),
189 size_t value)
2fa0b342
DHW
190{
191 printf("%5x", (unsigned)value);
192}
193static void
1418c83b
SC
194DEFUN(print_alignment,(value),
195 unsigned int value)
2fa0b342
DHW
196{
197 printf("2**%2u",value);
198}
1418c83b 199
2fa0b342 200static void
1418c83b
SC
201DEFUN(print_fill,(value),
202 fill_type value)
2fa0b342
DHW
203{
204 printf("%04x",(unsigned)value);
205}
206
1418c83b
SC
207/*----------------------------------------------------------------------
208
209 build a new statement node for the parse tree
210
211 */
2fa0b342
DHW
212
213static
1418c83b
SC
214lang_statement_union_type*
215DEFUN(new_statement,(type, size, list),
216 enum statement_enum type AND
217 size_t size AND
218 lang_statement_list_type *list)
2fa0b342
DHW
219{
220 lang_statement_union_type *new = (lang_statement_union_type *)
221 ldmalloc(size);
222 new->header.type = type;
223 new->header.next = (lang_statement_union_type *)NULL;
1418c83b 224 lang_statement_append(list, new, &new->header.next);
2fa0b342
DHW
225 return new;
226}
227
1418c83b
SC
228/*
229 Build a new input file node for the language. There are several ways
230 in which we treat an input file, eg, we only look at symbols, or
231 prefix it with a -l etc.
232
233 We can be supplied with requests for input files more than once;
234 they may, for example be split over serveral lines like foo.o(.text)
235 foo.o(.data) etc, so when asked for a file we check that we havn't
236 got it already so we don't duplicate the bfd.
237
238 */
2fa0b342 239static lang_input_statement_type *
1418c83b
SC
240DEFUN(new_afile, (name, file_type, target),
241 CONST char *CONST name AND
242 CONST lang_input_file_enum_type file_type AND
243 CONST char *CONST target)
2fa0b342
DHW
244{
245 lang_input_statement_type *p = new_stat(lang_input_statement,
246 stat_ptr);
247 lang_has_input_file = true;
248 p->target = target;
249 switch (file_type) {
250 case lang_input_file_is_symbols_only_enum:
251 p->filename = name;
252 p->is_archive =false;
253 p->real = true;
254 p->local_sym_name= name;
255 p->just_syms_flag = true;
256 p->search_dirs_flag = false;
257 break;
258 case lang_input_file_is_fake_enum:
259 p->filename = name;
260 p->is_archive =false;
261 p->real = false;
262 p->local_sym_name= name;
263 p->just_syms_flag = false;
264 p->search_dirs_flag =false;
2fa0b342
DHW
265 break;
266 case lang_input_file_is_l_enum:
267 p->is_archive = true;
268 p->filename = name;
269 p->real = true;
270 p->local_sym_name = concat("-l",name,"");
271 p->just_syms_flag = false;
272 p->search_dirs_flag = true;
273 break;
2fa0b342
DHW
274 case lang_input_file_is_search_file_enum:
275 case lang_input_file_is_marker_enum:
276 p->filename = name;
277 p->is_archive =false;
278 p->real = true;
279 p->local_sym_name= name;
280 p->just_syms_flag = false;
281 p->search_dirs_flag =true;
282 break;
2fa0b342
DHW
283 case lang_input_file_is_file_enum:
284 p->filename = name;
285 p->is_archive =false;
286 p->real = true;
287 p->local_sym_name= name;
288 p->just_syms_flag = false;
289 p->search_dirs_flag =false;
290 break;
2fa0b342
DHW
291 default:
292 FAIL();
293 }
294 p->asymbols = (asymbol **)NULL;
295 p->superfile = (lang_input_statement_type *)NULL;
2fa0b342
DHW
296 p->next_real_file = (lang_statement_union_type*)NULL;
297 p->next = (lang_statement_union_type*)NULL;
298 p->symbol_count = 0;
299 p->common_output_section = (asection *)NULL;
2fa0b342
DHW
300 lang_statement_append(&input_file_chain,
301 (lang_statement_union_type *)p,
302 &p->next_real_file);
303 return p;
304}
305
1418c83b
SC
306
307
2fa0b342 308lang_input_statement_type *
1418c83b
SC
309DEFUN(lang_add_input_file,(name, file_type, target),
310 char *name AND
311 lang_input_file_enum_type file_type AND
312 char *target)
2fa0b342
DHW
313{
314 /* Look it up or build a new one */
1418c83b
SC
315 lang_has_input_file = true;
316#if 0
2fa0b342
DHW
317 lang_input_statement_type *p;
318
319 for (p = (lang_input_statement_type *)input_file_chain.head;
320 p != (lang_input_statement_type *)NULL;
321 p = (lang_input_statement_type *)(p->next_real_file))
1418c83b
SC
322 {
323 /* Sometimes we have incomplete entries in here */
324 if (p->filename != (char *)NULL) {
325 if(strcmp(name,p->filename) == 0) return p;
326 }
327
328 }
329#endif
2fa0b342
DHW
330 return new_afile(name, file_type, target);
331}
332
333
1418c83b 334/* Build enough state so that the parser can build its tree */
2fa0b342 335void
1418c83b 336DEFUN_VOID(lang_init)
2fa0b342
DHW
337{
338
339 stat_ptr= &statement_list;
340 lang_list_init(stat_ptr);
341
342 lang_list_init(&input_file_chain);
343 lang_list_init(&lang_output_section_statement);
344 lang_list_init(&file_chain);
345 first_file = lang_add_input_file((char *)NULL,
346 lang_input_file_is_marker_enum,
347 (char *)NULL);
2fa0b342
DHW
348}
349
2fa0b342 350
1418c83b
SC
351/*----------------------------------------------------------------------
352 A region is an area of memory declared with the
353 MEMORY { name:org=exp, len=exp ... }
354 syntax.
2fa0b342 355
1418c83b 356 We maintain a list of all the regions here
2fa0b342 357
1418c83b
SC
358 If no regions are specified in the script, then the default is used
359 which is created when looked up to be the entire data space
2fa0b342
DHW
360*/
361
362static lang_memory_region_type *lang_memory_region_list;
363static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
364
365lang_memory_region_type *
1418c83b
SC
366DEFUN(lang_memory_region_lookup,(name),
367 CONST char *CONST name)
2fa0b342
DHW
368{
369
1418c83b
SC
370 lang_memory_region_type *p = lang_memory_region_list;
371 for (p = lang_memory_region_list;
372 p != ( lang_memory_region_type *)NULL;
373 p = p->next) {
374 if (strcmp(p->name, name) == 0) {
375 return p;
2fa0b342 376 }
1418c83b
SC
377 }
378 if (strcmp(name,"*default*")==0) {
379 /* This is the default region, dig out first one on the list */
380 if (lang_memory_region_list != (lang_memory_region_type*)NULL){
381 return lang_memory_region_list;
2fa0b342 382 }
1418c83b 383 }
2fa0b342 384 {
1418c83b
SC
385 lang_memory_region_type *new =
386 (lang_memory_region_type *)ldmalloc(sizeof(lang_memory_region_type));
387 new->name = buystring(name);
388 new->next = (lang_memory_region_type *)NULL;
389
390 *lang_memory_region_list_tail = new;
391 lang_memory_region_list_tail = &new->next;
392 new->origin = 0;
393 new->length = ~0;
394 new->current = 0;
395 return new;
2fa0b342
DHW
396 }
397}
398
399
2fa0b342 400lang_output_section_statement_type *
1418c83b
SC
401DEFUN(lang_output_section_find,(name),
402 CONST char * CONST name)
2fa0b342
DHW
403{
404 lang_statement_union_type *u;
405 lang_output_section_statement_type *lookup;
406
407 for (u = lang_output_section_statement.head;
408 u != (lang_statement_union_type *)NULL;
409 u = lookup->next)
1418c83b
SC
410 {
411 lookup = &u->output_section_statement;
412 if (strcmp(name, lookup->name)==0) {
413 return lookup;
414 }
2fa0b342 415 }
2fa0b342
DHW
416 return (lang_output_section_statement_type *)NULL;
417}
418
419lang_output_section_statement_type *
1418c83b
SC
420DEFUN(lang_output_section_statement_lookup,(name),
421 CONST char * CONST name)
2fa0b342
DHW
422{
423 lang_output_section_statement_type *lookup;
424 lookup =lang_output_section_find(name);
425 if (lookup == (lang_output_section_statement_type *)NULL) {
426
427 lookup =(lang_output_section_statement_type *)
428 new_stat(lang_output_section_statement, stat_ptr);
429 lookup->region = (lang_memory_region_type *)NULL;
430 lookup->fill = 0;
431 lookup->block_value = 1;
432 lookup->name = name;
433
434 lookup->next = (lang_statement_union_type*)NULL;
435 lookup->bfd_section = (asection *)NULL;
436 lookup->processed = false;
437 lookup->addr_tree = (etree_type *)NULL;
438 lang_list_init(&lookup->children);
439
440 lang_statement_append(&lang_output_section_statement,
441 (lang_statement_union_type *)lookup,
442 &lookup->next);
443 }
444 return lookup;
445}
446
447
448
449
450
451static void
1418c83b
SC
452DEFUN(print_flags, (outfile, ignore_flags),
453 FILE *outfile AND
454 lang_section_flags_type *ignore_flags)
2fa0b342
DHW
455{
456 fprintf(outfile,"(");
457#if 0
458 if (flags->flag_read) fprintf(outfile,"R");
459 if (flags->flag_write) fprintf(outfile,"W");
460 if (flags->flag_executable) fprintf(outfile,"X");
461 if (flags->flag_loadable) fprintf(outfile,"L");
462#endif
463 fprintf(outfile,")");
464}
465
466void
1418c83b
SC
467DEFUN(lang_map,(outfile),
468 FILE *outfile)
2fa0b342
DHW
469{
470 lang_memory_region_type *m;
471 fprintf(outfile,"**MEMORY CONFIGURATION**\n\n");
472
473 fprintf(outfile,"name\t\torigin\t\tlength\t\tattributes\n");
474 for (m = lang_memory_region_list;
475 m != (lang_memory_region_type *)NULL;
476 m = m->next)
477 {
478 fprintf(outfile,"%-16s", m->name);
479
480 fprintf(outfile,"%08lx\t%08lx\t", m->origin, m->length);
481 print_flags(outfile, &m->flags);
482 fprintf(outfile,"\n");
483 }
484 fprintf(outfile,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
485 fprintf(outfile,"output\t\tinput\t\tvirtual\n");
486 fprintf(outfile,"section\t\tsection\t\taddress\tsize\n\n");
487
488 print_statements();
489
490}
491
492/*
493 *
494 */
1418c83b
SC
495static void
496DEFUN(init_os,(s),
497 lang_output_section_statement_type *s)
2fa0b342
DHW
498{
499 section_userdata_type *new =
500 (section_userdata_type *)
501 ldmalloc(sizeof(section_userdata_type));
502
503 s->bfd_section = bfd_make_section(output_bfd, s->name);
504 s->bfd_section->output_section = s->bfd_section;
505 s->bfd_section->flags = SEC_NO_FLAGS;
506 /* We initialize an output sections output offset to minus its own */
507 /* vma to allow us to output a section through itself */
508 s->bfd_section->output_offset = 0;
509 get_userdata( s->bfd_section) = new;
510}
511
1418c83b
SC
512/***********************************************************************
513 The wild routines.
514
515 These expand statements like *(.text) and foo.o to a list of
516 explicit actions, like foo.o(.text), bar.o(.text) and
517 foo.o(.text,.data) .
518
519 The toplevel routine, wild, takes a statement, section, file and
520 target. If either the section or file is null it is taken to be the
521 wildcard. Seperate lang_input_section statements are created for
522 each part of the expanstion, and placed after the statement provided.
523
524*/
525
2fa0b342 526static void
1418c83b
SC
527DEFUN(wild_doit,(ptr, section, output, file),
528 lang_statement_list_type *ptr AND
529 asection *section AND
530 lang_output_section_statement_type *output AND
531 lang_input_statement_type *file)
2fa0b342
DHW
532{
533 if(output->bfd_section == (asection *)NULL)
534 {
535 init_os(output);
536 }
537
538 if (section != (asection *)NULL
539 && section->output_section == (asection *)NULL) {
540 /* Add a section reference to the list */
541 lang_input_section_type *new = new_stat(lang_input_section, ptr);
542
543 new->section = section;
544 new->ifile = file;
545 section->output_section = output->bfd_section;
546 section->output_section->flags |= section->flags;
547 if (section->alignment_power > output->bfd_section->alignment_power) {
548 output->bfd_section->alignment_power = section->alignment_power;
549 }
2fa0b342
DHW
550 }
551}
552
553static asection *
1418c83b
SC
554DEFUN(our_bfd_get_section_by_name,(abfd, section),
555bfd *abfd AND
556CONST char *section)
2fa0b342
DHW
557{
558 return bfd_get_section_by_name(abfd, section);
2fa0b342 559}
1418c83b 560
2fa0b342 561static void
1418c83b
SC
562DEFUN(wild_section,(ptr, section, file , output),
563 lang_wild_statement_type *ptr AND
564 CONST char *section AND
565 lang_input_statement_type *file AND
566 lang_output_section_statement_type *output)
2fa0b342
DHW
567{
568 asection *s;
1418c83b
SC
569 if (file->just_syms_flag == false) {
570 if (section == (char *)NULL) {
571 /* Do the creation to all sections in the file */
572 for (s = file->the_bfd->sections; s != (asection *)NULL; s=s->next) {
573 wild_doit(&ptr->children, s, output, file);
574 }
575 }
576 else {
577 /* Do the creation to the named section only */
578 wild_doit(&ptr->children,
579 our_bfd_get_section_by_name(file->the_bfd, section),
580 output, file);
2fa0b342
DHW
581 }
582 }
2fa0b342
DHW
583}
584
585
1418c83b
SC
586/* passed a file name (which must have been seen already and added to
587 the statement tree. We will see if it has been opened already and
588 had its symbols read. If not then we'll read it.
2fa0b342 589
1418c83b
SC
590 Archives are pecuilar here. We may open them once, but if they do
591 not define anything we need at the time, they won't have all their
592 symbols read. If we need them later, we'll have to redo it.
593 */
2fa0b342 594static
1418c83b
SC
595lang_input_statement_type *
596DEFUN(lookup_name,(name),
597 CONST char * CONST name)
2fa0b342
DHW
598{
599 lang_input_statement_type *search;
600 for(search = (lang_input_statement_type *)input_file_chain.head;
601 search != (lang_input_statement_type *)NULL;
602 search = (lang_input_statement_type *)search->next_real_file)
1418c83b
SC
603 {
604 if (search->filename == (char *)NULL && name == (char *)NULL) {
2fa0b342
DHW
605 return search;
606 }
1418c83b
SC
607 if (search->filename != (char *)NULL && name != (char *)NULL) {
608 if (strcmp(search->filename, name) == 0) {
609 ldmain_open_file_read_symbol(search);
610 return search;
611 }
612 }
2fa0b342 613 }
2fa0b342 614
1418c83b
SC
615 /* There isn't an afile entry for this file yet, this must be
616 because the name has only appeared inside a load script and not
617 on the command line */
618 search = new_afile(name, lang_input_file_is_file_enum, default_target);
619 ldmain_open_file_read_symbol(search);
2fa0b342 620 return search;
1418c83b
SC
621
622
2fa0b342
DHW
623}
624
625static void
1418c83b
SC
626DEFUN(wild,(s, section, file, target, output),
627 lang_wild_statement_type *s AND
628 CONST char *CONST section AND
629 CONST char *CONST file AND
630 CONST char *CONST target AND
631 lang_output_section_statement_type *output)
2fa0b342
DHW
632{
633 lang_input_statement_type *f;
634 if (file == (char *)NULL) {
635 /* Perform the iteration over all files in the list */
636 for (f = (lang_input_statement_type *)file_chain.head;
637 f != (lang_input_statement_type *)NULL;
638 f = (lang_input_statement_type *)f->next) {
639 wild_section(s, section, f, output);
640 }
641 }
642 else {
643 /* Perform the iteration over a single file */
1418c83b 644 wild_section( s, section, lookup_name(file), output);
2fa0b342 645 }
b0f36869
SC
646 if (section != (char *)NULL
647 && strcmp(section,"COMMON") == 0
1418c83b
SC
648 && default_common_section == (lang_output_section_statement_type*)NULL)
649 {
650 /* Remember the section that common is going to incase we later
651 get something which doesn't know where to put it */
652 default_common_section = output;
653 }
2fa0b342
DHW
654}
655
656/*
657 read in all the files
658 */
659static bfd *
097879bc
SC
660DEFUN(open_output,(name),
661 CONST char *CONST name)
2fa0b342 662{
1418c83b 663 extern CONST char *output_filename;
097879bc
SC
664 bfd *output;
665 if (output_target == (char *)NULL) {
666 if (current_target != (char *)NULL)
667 output_target = current_target;
668 else
669 output_target = default_target;
670 }
671 output = bfd_openw(name, output_target);
2fa0b342 672 output_filename = name;
097879bc 673
2fa0b342 674 if (output == (bfd *)NULL)
1418c83b
SC
675 {
676 if (bfd_error == invalid_target) {
097879bc 677 info("%P%F target %s not found\n", output_target);
1418c83b
SC
678 }
679 info("%P%F problem opening output file %s, %E", name);
2fa0b342 680 }
2fa0b342
DHW
681
682 output->flags |= D_PAGED;
683 bfd_set_format(output, bfd_object);
684 return output;
685}
1418c83b
SC
686
687
097879bc 688
1418c83b 689
2fa0b342 690static void
1418c83b
SC
691DEFUN(ldlang_open_output,(statement),
692 lang_statement_union_type *statement)
2fa0b342 693{
1418c83b
SC
694 switch (statement->header.type)
695 {
696 case lang_output_statement_enum:
097879bc 697 output_bfd = open_output(statement->output_statement.name);
2fa0b342 698 ldemul_set_output_arch();
2fa0b342 699 break;
1418c83b 700
2fa0b342 701 case lang_target_statement_enum:
1418c83b 702 current_target = statement->target_statement.target;
2fa0b342 703 break;
1418c83b 704 default:
2fa0b342 705 break;
1418c83b
SC
706 }
707}
2fa0b342 708
1418c83b
SC
709static void
710DEFUN(open_input_bfds,(statement),
711 lang_statement_union_type *statement)
712{
713 switch (statement->header.type)
714 {
715 case lang_target_statement_enum:
716 current_target = statement->target_statement.target;
2fa0b342 717 break;
1418c83b
SC
718 case lang_wild_statement_enum:
719 /* Maybe we should load the file's symbols */
720 if (statement->wild_statement.filename)
721 {
722 (void) lookup_name(statement->wild_statement.filename);
723 }
2fa0b342 724 break;
1418c83b
SC
725 case lang_input_statement_enum:
726 if (statement->input_statement.real == true)
727 {
728 statement->input_statement.target = current_target;
729 lookup_name(statement->input_statement.filename);
730 }
2fa0b342 731 break;
1418c83b 732 default:
2fa0b342
DHW
733 break;
734 }
2fa0b342 735}
2fa0b342
DHW
736/* If there are [COMMONS] statements, put a wild one into the bss section */
737
738static void
739lang_reasonable_defaults()
740{
1418c83b 741#if 0
8cb5eb31
SC
742 lang_output_section_statement_lookup(".text");
743 lang_output_section_statement_lookup(".data");
744
2fa0b342
DHW
745 default_common_section =
746 lang_output_section_statement_lookup(".bss");
8cb5eb31 747
1418c83b 748
2fa0b342
DHW
749 if (placed_commons == false) {
750 lang_wild_statement_type *new =
751 new_stat(lang_wild_statement,
752 &default_common_section->children);
753 new->section_name = "COMMON";
754 new->filename = (char *)NULL;
755 lang_list_init(&new->children);
756 }
1418c83b 757#endif
8cb5eb31 758
2fa0b342
DHW
759}
760
1418c83b
SC
761/*
762 Add the supplied name to the symbol table as an undefined reference.
763 Remove items from the chain as we open input bfds
764 */
765typedef struct ldlang_undef_chain_list_struct {
766 struct ldlang_undef_chain_list_struct *next;
767 char *name;
768} ldlang_undef_chain_list_type;
769
770static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
771
772void
773DEFUN(ldlang_add_undef,(name),
774 CONST char *CONST name)
2fa0b342 775{
1418c83b
SC
776 ldlang_undef_chain_list_type *new =
777 (ldlang_undef_chain_list_type
778 *)ldmalloc(sizeof(ldlang_undef_chain_list_type));
779
780 new->next = ldlang_undef_chain_list_head;
781 ldlang_undef_chain_list_head = new;
782
783 new->name = buystring(name);
784}
785/* Run through the list of undefineds created above and place them
786 into the linker hash table as undefined symbols belonging to the
787 script file.
788*/
789static void
790DEFUN_VOID(lang_place_undefineds)
791{
792 ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
793 while (ptr != (ldlang_undef_chain_list_type*)NULL) {
794 ldsym_type *sy = ldsym_get(ptr->name);
795 asymbol *def;
796 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
797 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
798 *def_ptr= def;
799 def->name = ptr->name;
800 def->flags = BSF_UNDEFINED;
801 def->section = (asection *)NULL;
802 Q_enter_global_ref(def_ptr);
803 ptr = ptr->next;
2fa0b342 804 }
1418c83b 805}
2fa0b342 806
1418c83b
SC
807
808
809/* Copy important data from out internal form to the bfd way. Also
810 create a section for the dummy file
811 */
812
813static void
814DEFUN_VOID(lang_create_output_section_statements)
815{
816 lang_statement_union_type*os;
817 for (os = lang_output_section_statement.head;
818 os != (lang_statement_union_type*)NULL;
819 os = os->output_section_statement.next) {
820 lang_output_section_statement_type *s =
821 &os->output_section_statement;
822 init_os(s);
823 }
824
825}
826
827static void
828DEFUN_VOID(lang_init_script_file)
829{
830 script_file = lang_add_input_file("script file",
831 lang_input_file_is_fake_enum,
832 (char *)NULL);
833 script_file->the_bfd = bfd_create("script file", output_bfd);
834 script_file->symbol_count = 0;
835 script_file->the_bfd->sections = output_bfd->sections;
2fa0b342
DHW
836}
837
838
1418c83b
SC
839
840
2fa0b342
DHW
841/* Open input files and attatch to output sections */
842static void
1418c83b
SC
843DEFUN(map_input_to_output_sections,(s, target, output_section_statement),
844 lang_statement_union_type *s AND
845 CONST char *target AND
846 lang_output_section_statement_type *output_section_statement)
2fa0b342
DHW
847{
848 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
849 {
850 switch (s->header.type) {
851 case lang_wild_statement_enum:
852 wild(&s->wild_statement, s->wild_statement.section_name,
853 s->wild_statement.filename, target,
854 output_section_statement);
855
856 break;
857
858 case lang_output_section_statement_enum:
1418c83b 859 map_input_to_output_sections(s->output_section_statement.children.head,
2fa0b342
DHW
860 target,
861 &s->output_section_statement);
862 break;
863 case lang_output_statement_enum:
864 break;
865 case lang_target_statement_enum:
866 target = s->target_statement.target;
867 break;
2fa0b342
DHW
868 case lang_fill_statement_enum:
869 case lang_input_section_enum:
870 case lang_object_symbols_statement_enum:
871 case lang_data_statement_enum:
1418c83b
SC
872 case lang_assignment_statement_enum:
873 case lang_padding_statement_enum:
2fa0b342
DHW
874 break;
875 case lang_afile_asection_pair_statement_enum:
876 FAIL();
877 break;
2fa0b342
DHW
878 case lang_address_statement_enum:
879 /* Mark the specified section with the supplied address */
880 {
881 lang_output_section_statement_type *os =
882 lang_output_section_statement_lookup
883 (s->address_statement.section_name);
884 os->addr_tree = s->address_statement.address;
885 }
886 break;
887 case lang_input_statement_enum:
888 /* A standard input statement, has no wildcards */
1418c83b 889 /* ldmain_open_file_read_symbol(&s->input_statement);*/
2fa0b342
DHW
890 break;
891 }
892 }
893}
894
895
896
897
898
899static void
1418c83b
SC
900DEFUN(print_output_section_statement,(output_section_statement),
901 lang_output_section_statement_type *output_section_statement)
2fa0b342
DHW
902{
903 asection *section = output_section_statement->bfd_section;
904 print_nl();
905 print_section(output_section_statement->name);
906
907 if (section) {
908 print_dot = section->vma;
909 print_space();
910 print_section("");
911 print_space();
912 print_address(section->vma);
913 print_space();
914 print_size(section->size);
915 print_space();
916 print_alignment(section->alignment_power);
917 print_space();
918#if 0
919 printf("%s flags", output_section_statement->region->name);
920 print_flags(stdout, &output_section_statement->flags);
921#endif
922
923 }
924 else {
925 printf("No attached output section");
926 }
927 print_nl();
928 print_statement(output_section_statement->children.head,
929 output_section_statement);
930
931}
932
933static void
1418c83b
SC
934DEFUN(print_assignment,(assignment, output_section),
935 lang_assignment_statement_type *assignment AND
936 lang_output_section_statement_type *output_section)
2fa0b342
DHW
937{
938 etree_value_type result;
939 print_section("");
940 print_space();
941 print_section("");
942 print_space();
943 print_address(print_dot);
944 print_space();
945 result = exp_fold_tree(assignment->exp->assign.src,
946 output_section,
947 lang_final_phase_enum,
948 print_dot,
949 &print_dot);
950
951 if (result.valid) {
952 print_address(result.value);
953 }
954 else
1418c83b
SC
955 {
956 printf("*undefined*");
957 }
2fa0b342
DHW
958 print_space();
959 exp_print_tree(stdout, assignment->exp);
960 printf("\n");
961}
962
963static void
1418c83b
SC
964DEFUN(print_input_statement,(statm),
965 lang_input_statement_type *statm)
2fa0b342 966{
070aa819
SC
967 if (statm->filename != (char *)NULL) {
968 printf("LOAD %s\n",statm->filename);
969 }
2fa0b342
DHW
970}
971
1418c83b
SC
972static void
973DEFUN(print_symbol,(q),
974 asymbol *q)
2fa0b342
DHW
975{
976 print_section("");
977 printf(" ");
978 print_section("");
979 printf(" ");
980 print_address(outside_symbol_address(q));
981 printf(" %s", q->name ? q->name : " ");
982 print_nl();
983}
1418c83b 984
2fa0b342 985static void
1418c83b
SC
986DEFUN(print_input_section,(in),
987 lang_input_section_type *in)
2fa0b342
DHW
988{
989 asection *i = in->section;
990
991 if(i->size != 0) {
992 print_section("");
993 printf(" ");
994 print_section(i->name);
995 printf(" ");
996 if (i->output_section) {
997 print_address(i->output_section->vma + i->output_offset);
998 printf(" ");
999 print_size(i->size);
1000 printf(" ");
1001 print_alignment(i->alignment_power);
1002 printf(" ");
1003 if (in->ifile) {
1418c83b 1004
2fa0b342 1005 bfd *abfd = in->ifile->the_bfd;
1418c83b
SC
1006 if (in->ifile->just_syms_flag == true) {
1007 printf("symbols only ");
1008 }
1009
2fa0b342
DHW
1010 printf(" %s ",abfd->xvec->name);
1011 if(abfd->my_archive != (bfd *)NULL) {
1012 printf("[%s]%s", abfd->my_archive->filename,
1013 abfd->filename);
1014 }
1015 else {
1016 printf("%s", abfd->filename);
1017 }
a37cc0c0 1018 printf("(%d bytes)", bfd_alloc_size(abfd));
2fa0b342
DHW
1019 print_nl();
1020
1021 /* Find all the symbols in this file defined in this section */
1418c83b
SC
1022 {
1023 asymbol **p;
1024 for (p = in->ifile->asymbols; *p; p++) {
1025 asymbol *q = *p;
2fa0b342 1026
1418c83b
SC
1027 if (bfd_get_section(q) == i && q->flags & BSF_GLOBAL) {
1028 print_symbol(q);
1029 }
2fa0b342
DHW
1030 }
1031 }
2fa0b342
DHW
1032 }
1033 else {
1034 print_nl();
1035 }
1036
1037
1038 print_dot = outside_section_address(i) + i->size;
1039 }
1040 else {
1041 printf("No output section allocated\n");
1042 }
1043 }
1044}
2fa0b342 1045
2fa0b342 1046static void
1418c83b
SC
1047DEFUN(print_fill_statement,(fill),
1048 lang_fill_statement_type *fill)
2fa0b342
DHW
1049{
1050 printf("FILL mask ");
1051 print_fill( fill->fill);
1052}
1053
1054static void
1418c83b
SC
1055DEFUN(print_data_statement,(data),
1056 lang_data_statement_type *data)
2fa0b342
DHW
1057{
1058/* bfd_vma value; */
1059 print_section("");
1060 print_space();
1061 print_section("");
1062 print_space();
65c552e3 1063/* ASSERT(print_dot == data->output_vma);*/
2fa0b342
DHW
1064
1065 print_address(data->output_vma);
1066 print_space();
1067 print_address(data->value);
1068 print_space();
1069 switch (data->type) {
1070 case BYTE :
1071 printf("BYTE ");
1072 print_dot += BYTE_SIZE;
1073 break;
1074 case SHORT:
1075 printf("SHORT ");
1076 print_dot += SHORT_SIZE;
1077 break;
1078 case LONG:
1079 printf("LONG ");
1080 print_dot += LONG_SIZE;
1081 break;
1082 }
1083
1084 exp_print_tree(stdout, data->exp);
1085
1086 printf("\n");
1087}
1088
1089
1090static void
1418c83b
SC
1091DEFUN(print_padding_statement,(s),
1092 lang_padding_statement_type *s)
2fa0b342
DHW
1093{
1094 print_section("");
1095 print_space();
1096 print_section("*fill*");
1097 print_space();
1098 print_address(s->output_offset + s->output_section->vma);
1099 print_space();
1100 print_size(s->size);
1101 print_space();
1102 print_fill(s->fill);
1103 print_nl();
1104}
1105
1418c83b
SC
1106static void
1107DEFUN(print_wild_statement,(w,os),
1108 lang_wild_statement_type *w AND
1109 lang_output_section_statement_type *os)
2fa0b342
DHW
1110{
1111 if (w->filename != (char *)NULL) {
1112 printf("%s",w->filename);
1113 }
1114 else {
1115 printf("*");
1116 }
1117 if (w->section_name != (char *)NULL) {
1118 printf("(%s)",w->section_name);
1119 }
1120 else {
1121 printf("(*)");
1122 }
1123 print_nl();
1124 print_statement(w->children.head, os);
1125
1126}
1127static void
1418c83b
SC
1128DEFUN(print_statement,(s, os),
1129 lang_statement_union_type *s AND
1130 lang_output_section_statement_type *os)
2fa0b342
DHW
1131{
1132 while (s) {
1133 switch (s->header.type) {
1134 case lang_wild_statement_enum:
1418c83b
SC
1135 print_wild_statement(&s->wild_statement, os);
1136 break;
2fa0b342 1137 default:
1418c83b 1138 printf("Fail with %d\n",s->header.type);
2fa0b342
DHW
1139 FAIL();
1140 break;
1418c83b
SC
1141 case lang_address_statement_enum:
1142 printf("address\n");
1143 break;
2fa0b342
DHW
1144 break;
1145 case lang_object_symbols_statement_enum:
1146 printf("object symbols\n");
1147 break;
1148 case lang_fill_statement_enum:
1149 print_fill_statement(&s->fill_statement);
1150 break;
1151 case lang_data_statement_enum:
1152 print_data_statement(&s->data_statement);
1153 break;
2fa0b342
DHW
1154 case lang_input_section_enum:
1155 print_input_section(&s->input_section);
1156 break;
1157 case lang_padding_statement_enum:
1158 print_padding_statement(&s->padding_statement);
1159 break;
1160 case lang_output_section_statement_enum:
1161 print_output_section_statement(&s->output_section_statement);
1162 break;
1163 case lang_assignment_statement_enum:
1164 print_assignment(&s->assignment_statement,
1418c83b 1165 os);
2fa0b342
DHW
1166 break;
1167
1168
1169 case lang_target_statement_enum:
1170 printf("TARGET(%s)\n", s->target_statement.target);
1171 break;
1172 case lang_output_statement_enum:
097879bc
SC
1173 printf("OUTPUT(%s %s)\n",
1174 s->output_statement.name,
1175 output_target);
2fa0b342
DHW
1176 break;
1177 case lang_input_statement_enum:
1178 print_input_statement(&s->input_statement);
1179 break;
1180 case lang_afile_asection_pair_statement_enum:
1181 FAIL();
1182 break;
1183 }
1184 s = s->next;
1185 }
1186}
1187
1188
1189static void
1418c83b 1190DEFUN_VOID(print_statements)
2fa0b342
DHW
1191{
1192 print_statement(statement_list.head,
1193 (lang_output_section_statement_type *)NULL);
1194}
1195
1196static bfd_vma
1418c83b
SC
1197DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot),
1198 lang_statement_union_type **this_ptr AND
1199 fill_type fill AND
1200 unsigned int power AND
1201 asection * output_section_statement AND
1202 bfd_vma dot)
2fa0b342
DHW
1203{
1204 /* Align this section first to the
1205 input sections requirement, then
1206 to the output section's requirement.
1207 If this alignment is > than any seen before,
1208 then record it too. Perform the alignment by
1209 inserting a magic 'padding' statement.
1210 */
1211
1212 unsigned int alignment_needed = align_power(dot, power) - dot;
1213
1214 if (alignment_needed != 0)
1215 {
1216 lang_statement_union_type *new =
1217 (lang_statement_union_type *)
1218 ldmalloc(sizeof(lang_padding_statement_type));
1219 /* Link into existing chain */
1220 new->header.next = *this_ptr;
1221 *this_ptr = new;
1222 new->header.type = lang_padding_statement_enum;
1223 new->padding_statement.output_section = output_section_statement;
1224 new->padding_statement.output_offset =
1225 dot - output_section_statement->vma;
1226 new->padding_statement.fill = fill;
1227 new->padding_statement.size = alignment_needed;
1228 }
1229
1230
1231 /* Remember the most restrictive alignment */
1232 if (power > output_section_statement->alignment_power) {
1233 output_section_statement->alignment_power = power;
1234 }
1235 output_section_statement->size += alignment_needed;
1236 return alignment_needed + dot;
1237
1238}
1239
1418c83b 1240/* Work out how much this section will move the dot point */
2fa0b342 1241static bfd_vma
1418c83b
SC
1242DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot),
1243 lang_statement_union_type **this_ptr AND
1244 lang_output_section_statement_type*output_section_statement AND
1245 unsigned short fill AND
1246 bfd_vma dot)
2fa0b342
DHW
1247{
1248 lang_input_section_type *is = &((*this_ptr)->input_section);
1249 asection *i = is->section;
1418c83b
SC
1250
1251 if (is->ifile->just_syms_flag == false) {
ac004870
SC
1252 dot = insert_pad(this_ptr, fill, i->alignment_power,
1253 output_section_statement->bfd_section, dot);
2fa0b342 1254
ac004870
SC
1255 /* remember the largest size so we can malloc the largest area */
1256 /* needed for the output stage */
1257 if (i->size > largest_section) {
1258 largest_section = i->size;
1259 }
2fa0b342 1260
ac004870 1261 /* Remember where in the output section this input section goes */
2fa0b342 1262
ac004870
SC
1263 i->output_offset = dot - output_section_statement->bfd_section->vma;
1264
1265 /* Mark how big the output section must be to contain this now */
1266 dot += i->size;
1267 output_section_statement->bfd_section->size =
1268 dot - output_section_statement->bfd_section->vma;
1269 }
1270 else
1271 {
1272 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1273 }
2fa0b342
DHW
1274
1275 return dot ;
1276}
1277
1278
1279/* Work out the size of the output sections
1280 from the sizes of the input sections */
1281static bfd_vma
1418c83b
SC
1282DEFUN(lang_size_sections,(s, output_section_statement, prev, fill, dot),
1283 lang_statement_union_type *s AND
1284 lang_output_section_statement_type * output_section_statement AND
1285 lang_statement_union_type **prev AND
1286 unsigned short fill AND
1287 bfd_vma dot)
2fa0b342
DHW
1288{
1289 /* Size up the sections from their constituent parts */
1290 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1418c83b
SC
1291 {
1292 switch (s->header.type) {
1293 case lang_output_section_statement_enum:
1294 {
1295 bfd_vma after;
1296 lang_output_section_statement_type *os =
1297 &(s->output_section_statement);
1298 /* The start of a section */
2fa0b342 1299
1418c83b
SC
1300 if (os->addr_tree == (etree_type *)NULL) {
1301 /* No address specified for this section, get one
1302 from the region specification
1303 */
1304 if (os->region == (lang_memory_region_type *)NULL) {
2fa0b342 1305 os->region = lang_memory_region_lookup("*default*");
1418c83b
SC
1306 }
1307 dot = os->region->current;
2fa0b342 1308 }
1418c83b
SC
1309 else {
1310 etree_value_type r ;
1311 r = exp_fold_tree(os->addr_tree,
1312 (lang_output_section_statement_type *)NULL,
1313 lang_allocating_phase_enum,
1314 dot, &dot);
1315 if (r.valid == false) {
1316 info("%F%S: non constant address expression for section %s\n",
1317 os->name);
1318 }
1319 dot = r.value;
1320 }
1321 /* The section starts here */
1322 /* First, align to what the section needs */
2fa0b342 1323
1418c83b
SC
1324 dot = align_power(dot, os->bfd_section->alignment_power);
1325 os->bfd_section->vma = dot;
1326 os->bfd_section->output_offset = 0;
2fa0b342 1327
1418c83b
SC
1328 (void) lang_size_sections(os->children.head, os, &os->children.head,
1329 os->fill, dot);
1330 /* Ignore the size of the input sections, use the vma and size to */
1331 /* align against */
2fa0b342
DHW
1332
1333
1418c83b
SC
1334 after = ALIGN(os->bfd_section->vma +
1335 os->bfd_section->size,
1336 os->block_value) ;
2fa0b342
DHW
1337
1338
1418c83b
SC
1339 os->bfd_section->size = after - os->bfd_section->vma;
1340 dot = os->bfd_section->vma + os->bfd_section->size;
1341 os->processed = true;
2fa0b342 1342
1418c83b
SC
1343 /* Replace into region ? */
1344 if (os->addr_tree == (etree_type *)NULL
1345 && os->region !=(lang_memory_region_type*)NULL ) {
1346 os->region->current = dot;
1347 }
1348 }
2fa0b342 1349
1418c83b 1350 break;
2fa0b342 1351
1418c83b
SC
1352 case lang_data_statement_enum:
1353 {
1354 unsigned int size;
65c552e3 1355 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1418c83b
SC
1356 s->data_statement.output_section =
1357 output_section_statement->bfd_section;
2fa0b342 1358
1418c83b
SC
1359 switch (s->data_statement.type) {
1360 case LONG:
1361 size = LONG_SIZE;
1362 break;
1363 case SHORT:
1364 size = SHORT_SIZE;
1365 break;
1366 case BYTE:
1367 size = BYTE_SIZE;
1368 break;
2fa0b342 1369
1418c83b
SC
1370 }
1371 dot += size;
1372 output_section_statement->bfd_section->size += size;
1373 }
1374 break;
2fa0b342 1375
1418c83b 1376 case lang_wild_statement_enum:
2fa0b342 1377
1418c83b 1378 dot = lang_size_sections(s->wild_statement.children.head,
2fa0b342
DHW
1379 output_section_statement,
1380 &s->wild_statement.children.head,
1381
1382 fill, dot);
1383
1418c83b 1384 break;
2fa0b342 1385
1418c83b
SC
1386 case lang_object_symbols_statement_enum:
1387 create_object_symbols = output_section_statement;
1388 break;
1389 case lang_output_statement_enum:
1390 case lang_target_statement_enum:
1391 break;
1392 case lang_input_section_enum:
1393 dot = size_input_section(prev,
2fa0b342
DHW
1394 output_section_statement,
1395 output_section_statement->fill, dot);
1418c83b
SC
1396 break;
1397 case lang_input_statement_enum:
1398 break;
1399 case lang_fill_statement_enum:
1400 fill = s->fill_statement.fill;
1401 break;
1402 case lang_assignment_statement_enum:
2fa0b342 1403 {
1418c83b
SC
1404 bfd_vma newdot = dot;
1405 exp_fold_tree(s->assignment_statement.exp,
1406 output_section_statement,
1407 lang_allocating_phase_enum,
1408 dot,
1409 &newdot);
1410
1411 if (newdot != dot)
1412 /* We've been moved ! so insert a pad */
1413 {
1414 lang_statement_union_type *new =
1415 (lang_statement_union_type *)
1416 ldmalloc(sizeof(lang_padding_statement_type));
1417 /* Link into existing chain */
1418 new->header.next = *prev;
1419 *prev = new;
1420 new->header.type = lang_padding_statement_enum;
1421 new->padding_statement.output_section =
1422 output_section_statement->bfd_section;
1423 new->padding_statement.output_offset =
1424 dot - output_section_statement->bfd_section->vma;
1425 new->padding_statement.fill = fill;
1426 new->padding_statement.size = newdot - dot;
1427 output_section_statement->bfd_section->size +=
1428 new->padding_statement.size;
1429 dot = newdot;
1430 }
2fa0b342 1431 }
2fa0b342 1432
1418c83b
SC
1433 break;
1434 case lang_padding_statement_enum:
1435 FAIL();
1436 break;
1437 default:
1438 FAIL();
1439 break;
1440 case lang_address_statement_enum:
1441 break;
1442 }
1443 prev = &s->header.next;
2fa0b342 1444 }
2fa0b342
DHW
1445 return dot;
1446}
1447
1448
1449static bfd_vma
1418c83b
SC
1450DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot),
1451 lang_statement_union_type *s AND
1452 lang_output_section_statement_type * output_section_statement AND
1453 unsigned short fill AND
1454 bfd_vma dot)
2fa0b342
DHW
1455{
1456
1457 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1458 {
1459 switch (s->header.type) {
1460 case lang_output_section_statement_enum:
1461 {
1462 lang_output_section_statement_type *os =
1463 &(s->output_section_statement);
1464 dot = os->bfd_section->vma;
1465 (void) lang_do_assignments(os->children.head, os, os->fill, dot);
1466 dot = os->bfd_section->vma + os->bfd_section->size;
1467 }
1468 break;
1469 case lang_wild_statement_enum:
1470
1471 dot = lang_do_assignments(s->wild_statement.children.head,
1472 output_section_statement,
1473 fill, dot);
1474
1475 break;
1476
1477 case lang_object_symbols_statement_enum:
1478 case lang_output_statement_enum:
1479 case lang_target_statement_enum:
1418c83b 1480#if 0
2fa0b342 1481 case lang_common_statement_enum:
1418c83b 1482#endif
2fa0b342
DHW
1483 break;
1484 case lang_data_statement_enum:
1485 {
1486 etree_value_type value ;
1487 value = exp_fold_tree(s->data_statement.exp,
1488 0, lang_final_phase_enum, dot, &dot);
1489 s->data_statement.value = value.value;
1490 if (value.valid == false) info("%F%P: Invalid data statement\n");
1491 }
1492 switch (s->data_statement.type) {
1493 case LONG:
1494 dot += LONG_SIZE;
1495 break;
1496 case SHORT:
1497 dot += SHORT_SIZE;
1498 break;
1499 case BYTE:
1500 dot += BYTE_SIZE;
1501 break;
1502 }
1503 break;
1504 case lang_input_section_enum:
1505 {
1506 asection *in = s->input_section.section;
1507 dot += in->size;
1508 }
1509 break;
1510
1511 case lang_input_statement_enum:
1512 break;
1513 case lang_fill_statement_enum:
1514 fill = s->fill_statement.fill;
1515 break;
1516 case lang_assignment_statement_enum:
1517 {
1518 exp_fold_tree(s->assignment_statement.exp,
1519 output_section_statement,
1520 lang_final_phase_enum,
1521 dot,
1522 &dot);
1523 }
1524
1525 break;
1526 case lang_padding_statement_enum:
1527 dot += s->padding_statement.size;
1528 break;
1529 default:
1530 FAIL();
1531 break;
1532 case lang_address_statement_enum:
1533 break;
1534 }
1535
1536 }
1537 return dot;
1538}
1539
1540
1541
1418c83b
SC
1542static void
1543DEFUN_VOID(lang_relocate_globals)
2fa0b342
DHW
1544{
1545
1546 /*
1418c83b
SC
1547 Each ldsym_type maintains a chain of pointers to asymbols which
1548 references the definition. Replace each pointer to the referenence
1549 with a pointer to only one place, preferably the definition. If
1550 the defintion isn't available then the common symbol, and if
1551 there isn't one of them then choose one reference.
1552 */
2fa0b342
DHW
1553
1554 FOR_EACH_LDSYM(lgs) {
1555 asymbol *it;
1556 if (lgs->sdefs_chain) {
1557 it = *(lgs->sdefs_chain);
1558 }
1559 else if (lgs->scoms_chain != (asymbol **)NULL) {
1560 it = *(lgs->scoms_chain);
1561 }
1562 else if (lgs->srefs_chain != (asymbol **)NULL) {
1563 it = *(lgs->srefs_chain);
1564 }
1565 else {
1418c83b
SC
1566 /* This can happen when the command line asked for a symbol to
1567 be -u */
1568 it = (asymbol *)NULL;
2fa0b342
DHW
1569 }
1570 if (it != (asymbol *)NULL)
1418c83b
SC
1571 {
1572 asymbol **ptr= lgs->srefs_chain;
2fa0b342 1573
1418c83b
SC
1574 while (ptr != (asymbol **)NULL) {
1575 asymbol *ref = *ptr;
1576 *ptr = it;
1577 ptr = (asymbol **)(ref->udata);
1578 }
2fa0b342 1579 }
2fa0b342
DHW
1580 }
1581}
1582
1583
1584
2fa0b342 1585static void
1418c83b 1586DEFUN_VOID(lang_finish)
2fa0b342
DHW
1587{
1588 ldsym_type *lgs;
1589
1590 if (entry_symbol == (char *)NULL) {
1591 /* No entry has been specified, look for start */
1592 entry_symbol = "start";
1593 }
1594 lgs = ldsym_get_soft(entry_symbol);
1595 if (lgs && lgs->sdefs_chain) {
1596 asymbol *sy = *(lgs->sdefs_chain);
1597 /* We can set the entry address*/
1598 bfd_set_start_address(output_bfd,
1599 outside_symbol_address(sy));
1600
1601 }
1602 else {
1603 /* Can't find anything reasonable,
1604 use the first address in the text section
1605 */
1606 asection *ts = bfd_get_section_by_name(output_bfd, ".text");
1607 if (ts) {
1608 bfd_set_start_address(output_bfd, ts->vma);
1609 }
1610 }
1611}
1612
1613/* By now we know the target architecture, and we may have an */
1614/* ldfile_output_machine_name */
1615static void
1418c83b 1616DEFUN_VOID(lang_check)
2fa0b342
DHW
1617{
1618 lang_statement_union_type *file;
1619
1620
1621 for (file = file_chain.head;
1622 file != (lang_statement_union_type *)NULL;
1623 file=file->input_statement.next)
1418c83b
SC
1624 {
1625 /* Inspect the architecture and ensure we're linking like
1626 with like
1627 */
1628
1629 if (bfd_arch_compatible( file->input_statement.the_bfd,
1630 output_bfd,
1631 &ldfile_output_architecture,
1632 &ldfile_output_machine)) {
1633 bfd_set_arch_mach(output_bfd,
1634 ldfile_output_architecture, ldfile_output_machine);
1635 }
1636 else {
1637 enum bfd_architecture this_architecture =
1638 bfd_get_architecture(file->input_statement.the_bfd);
1639 unsigned long this_machine =
1640 bfd_get_machine(file->input_statement.the_bfd);
2fa0b342 1641
1418c83b
SC
1642 info("%I: architecture %s",
1643 file,
1644 bfd_printable_arch_mach(this_architecture, this_machine));
1645 info(" incompatible with output %s\n",
1646 bfd_printable_arch_mach(ldfile_output_architecture,
1647 ldfile_output_machine));
1648 ldfile_output_architecture = this_architecture;
1649 ldfile_output_machine = this_machine;
1650 bfd_set_arch_mach(output_bfd,
1651 ldfile_output_architecture,
1652 ldfile_output_machine);
2fa0b342
DHW
1653
1654
1418c83b 1655 }
2fa0b342 1656 }
2fa0b342
DHW
1657}
1658
1659
1660/*
1661 * run through all the global common symbols and tie them
1662 * to the output section requested.
1663 */
1664
1665static void
1418c83b 1666DEFUN_VOID(lang_common)
2fa0b342
DHW
1667{
1668 ldsym_type *lgs;
1669 if (config.relocateable_output == false ||
1670 command_line.force_common_definition== true) {
1671 for (lgs = symbol_head;
1672 lgs != (ldsym_type *)NULL;
1673 lgs=lgs->next)
1418c83b
SC
1674 {
1675 asymbol *com ;
1676 unsigned int power_of_two;
1677 size_t size;
1678 size_t align;
1679 if (lgs->scoms_chain != (asymbol **)NULL) {
1680 com = *(lgs->scoms_chain);
1681 size = com->value;
1682 switch (size) {
1683 case 0:
1684 case 1:
1685 align = 1;
1686 power_of_two = 0;
1687 break;
1688 case 2:
1689 power_of_two = 1;
1690 align = 2;
1691 break;
1692 case 3:
1693 case 4:
1694 power_of_two =2;
1695 align = 4;
1696 break;
1697 case 5:
1698 case 6:
1699 case 7:
1700 case 8:
1701 power_of_two = 3;
2fa0b342 1702 align = 8;
1418c83b
SC
1703 break;
1704 default:
1705 power_of_two = 4;
1706 align = 16;
1707 break;
2fa0b342 1708 }
2fa0b342
DHW
1709
1710
1418c83b
SC
1711 /* Change from a common symbol into a definition of
1712 a symbol */
1713 lgs->sdefs_chain = lgs->scoms_chain;
1714 lgs->scoms_chain = (asymbol **)NULL;
1715 commons_pending--;
1716 /* Point to the correct common section */
1717 com->section =
1718 ((lang_input_statement_type *)
1719 (com->the_bfd->usrdata))->common_section;
1720 /* Fix the size of the common section */
1721 com->section->size = ALIGN(com->section->size, align);
1722
1723 /* Remember if this is the biggest alignment ever seen */
1724 if (power_of_two > com->section->alignment_power) {
1725 com->section->alignment_power = power_of_two;
2fa0b342 1726 }
1418c83b 1727
ac004870
SC
1728 /* Symbol stops being common and starts being global, but
1729 we remember that it was common once. */
1418c83b 1730
ac004870 1731 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
097879bc 1732
1418c83b
SC
1733
1734 if (write_map)
1735 {
1736 printf ("Allocating common %s: %x at %x\n",
1737 lgs->name,
1738 (unsigned) size,
1739 (unsigned) com->section->size);
1740 }
1741 com->value = com->section->size;
1742 com->section->size += size;
097879bc 1743
1418c83b
SC
1744
1745 }
2fa0b342 1746 }
2fa0b342 1747 }
1418c83b
SC
1748
1749
2fa0b342
DHW
1750}
1751
1752/*
1753run through the input files and ensure that every input
1754section has somewhere to go. If one is found without
1755a destination then create an input request and place it
1756into the statement tree.
1757*/
1758
1418c83b
SC
1759static void
1760DEFUN_VOID(lang_place_orphans)
2fa0b342
DHW
1761{
1762 lang_input_statement_type *file;
1763 for (file = (lang_input_statement_type*)file_chain.head;
1764 file != (lang_input_statement_type*)NULL;
1765 file = (lang_input_statement_type*)file->next) {
1766 asection *s;
1767 for (s = file->the_bfd->sections;
1768 s != (asection *)NULL;
1769 s = s->next) {
1770 if ( s->output_section == (asection *)NULL) {
1771 /* This section of the file is not attatched, root
1772 around for a sensible place for it to go */
1773
1774 if (file->common_section == s) {
1775 /* This is a lonely common section which must
1776 have come from an archive. We attatch to the
1777 section with the wildcard */
1418c83b
SC
1778 if (config.relocateable_output != true
1779 && command_line.force_common_definition == false) {
1780 if (default_common_section ==
1781 (lang_output_section_statement_type *)NULL) {
1782 info("%P: No [COMMON] command, defaulting to .bss\n");
1783
1784 default_common_section =
1785 lang_output_section_statement_lookup(".bss");
1786
1787 }
1788 wild_doit(&default_common_section->children, s,
1789 default_common_section, file);
1790 }
2fa0b342
DHW
1791 }
1792 else {
1793 lang_output_section_statement_type *os =
1794 lang_output_section_statement_lookup(s->name);
1795
1796 wild_doit(&os->children, s, os, file);
1797 }
1798 }
1799 }
2fa0b342
DHW
1800 }
1801}
1802
1803
2fa0b342 1804void
1418c83b
SC
1805DEFUN(lang_set_flags,(ptr, flags),
1806 lang_section_flags_type *ptr AND
1807 CONST char *flags)
2fa0b342
DHW
1808{
1809 boolean state = true;
1810 ptr->flag_read = false;
1811 ptr->flag_write = false;
1812 ptr->flag_executable = false;
1813 ptr->flag_loadable= false;
1814 while (*flags)
1418c83b
SC
1815 {
1816 if (*flags == '!') {
1817 state = false;
1818 flags++;
1819 }
1820 else state = true;
1821 switch (*flags) {
1822 case 'R':
1823 ptr->flag_read = state;
1824 break;
1825 case 'W':
1826 ptr->flag_write = state;
1827 break;
1828 case 'X':
1829 ptr->flag_executable= state;
1830 break;
1831 case 'L':
1832 ptr->flag_loadable= state;
1833 break;
1834 default:
1835 info("%P%F illegal syntax in flags\n");
1836 break;
1837 }
2fa0b342
DHW
1838 flags++;
1839 }
2fa0b342
DHW
1840}
1841
1842
1843
1844void
1418c83b
SC
1845DEFUN(lang_for_each_file,(func),
1846 PROTO(void, (*func),(lang_input_statement_type *)))
2fa0b342
DHW
1847{
1848 lang_input_statement_type *f;
1849 for (f = (lang_input_statement_type *)file_chain.head;
1850 f != (lang_input_statement_type *)NULL;
1851 f = (lang_input_statement_type *)f->next)
1418c83b
SC
1852 {
1853 func(f);
1854 }
2fa0b342
DHW
1855}
1856
1857
1858void
1418c83b
SC
1859DEFUN(lang_for_each_input_section, (func),
1860 PROTO(void ,(*func),(bfd *ab, asection*as)))
2fa0b342
DHW
1861{
1862 lang_input_statement_type *f;
1863 for (f = (lang_input_statement_type *)file_chain.head;
1864 f != (lang_input_statement_type *)NULL;
1865 f = (lang_input_statement_type *)f->next)
1866 {
1867 asection *s;
1868 for (s = f->the_bfd->sections;
1869 s != (asection *)NULL;
1870 s = s->next) {
1871 func(f->the_bfd, s);
1872 }
1873 }
1874}
1875
1876
1877
1878void
1418c83b
SC
1879DEFUN(ldlang_add_file,(entry),
1880 lang_input_statement_type *entry)
2fa0b342 1881{
1418c83b 1882
2fa0b342
DHW
1883 lang_statement_append(&file_chain,
1884 (lang_statement_union_type *)entry,
1885 &entry->next);
1886}
1887
1888
1889
1890void
1418c83b
SC
1891DEFUN(lang_add_output,(name),
1892 CONST char *name)
2fa0b342
DHW
1893{
1894 lang_output_statement_type *new = new_stat(lang_output_statement,
1895 stat_ptr);
1896 new->name = name;
1897 had_output_filename = true;
1898}
1899
1900
1901static lang_output_section_statement_type *current_section;
1902
1903void
1418c83b
SC
1904DEFUN(lang_enter_output_section_statement,
1905 (output_section_statement_name,
1906 address_exp,
1907 block_value),
1908 char *output_section_statement_name AND
1909 etree_type *address_exp AND
1910 bfd_vma block_value)
2fa0b342
DHW
1911{
1912 lang_output_section_statement_type *os;
1913 current_section =
1914 os =
1915 lang_output_section_statement_lookup(output_section_statement_name);
1916
1917
1918 /* Add this statement to tree */
1919 /* add_statement(lang_output_section_statement_enum,
1920 output_section_statement);*/
1921 /* Make next things chain into subchain of this */
1922
1923 if (os->addr_tree ==
1924 (etree_type *)NULL) {
1925 os->addr_tree =
1926 address_exp;
1927 }
1928 os->block_value = block_value;
1929 stat_ptr = & os->children;
1930
1931}
1932
1933
1934void
1418c83b 1935DEFUN_VOID(lang_final)
2fa0b342
DHW
1936{
1937 if (had_output_filename == false) {
1938 lang_add_output("a.out");
1939 }
2fa0b342
DHW
1940}
1941
1942
1943
1944
1945
1418c83b
SC
1946asymbol *
1947DEFUN(create_symbol,(name, flags, section),
1948 CONST char *name AND
1949 flagword flags AND
1950 asection *section)
2fa0b342
DHW
1951{
1952 extern lang_input_statement_type *script_file;
1953 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
1954 /* Add this definition to script file */
1955 asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
1418c83b 1956 def->name = buystring(name);
2fa0b342
DHW
1957 def->udata = 0;
1958 def->flags = flags;
1959 def->section = section;
1960
1961 *def_ptr = def;
1962 Q_enter_global_ref(def_ptr);
1963 return def;
1964}
1965
1966
1967void
1418c83b
SC
1968DEFUN_VOID(lang_process)
1969{
1970 if (had_script == false) {
1971 parse_line(ldemul_get_script());
1972 }
1973 lang_reasonable_defaults();
1974 current_target = default_target;
1975
1976 lang_for_each_statement(ldlang_open_output); /* Open the output file */
1977 /* For each output section statement, create a section in the output
1978 file */
1979 lang_create_output_section_statements();
1980
1981 /* Create a dummy bfd for the script */
1982 lang_init_script_file();
1983
1984 /* Add to the hash table all undefineds on the command line */
1985 lang_place_undefineds();
1986
1987 /* Create a bfd for each input file */
1988 current_target = default_target;
1989 lang_for_each_statement(open_input_bfds);
1990
1991 common_section.userdata = &common_section_userdata;
1992
1993 /* Run through the contours of the script and attatch input sections
1994 to the correct output sections
1995 */
1996 map_input_to_output_sections(statement_list.head, (char *)NULL,
1997 ( lang_output_section_statement_type *)NULL);
1998
1999 /* Find any sections not attatched explicitly and handle them */
2000 lang_place_orphans();
2001
2002 /* Size up the common data */
2003 lang_common();
2004
2005 ldemul_before_allocation();
2006
2007 /* Size up the sections */
2008 lang_size_sections(statement_list.head,
2009 (lang_output_section_statement_type *)NULL,
2010 &(statement_list.head), 0, (bfd_vma)0);
2011
2012 /* See if anything special should be done now we know how big
2013 everything is */
2014 ldemul_after_allocation();
2015
2016 /* Do all the assignments, now that we know the final restingplaces
2017 of all the symbols */
2018
2019 lang_do_assignments(statement_list.head,
2020 (lang_output_section_statement_type *)NULL,
2021 0, (bfd_vma)0);
2022
2023 /* Make sure that we're not mixing architectures */
2024
2025 lang_check();
2026
2027 /* Move the global symbols around */
2028 lang_relocate_globals();
2029
2030 /* Final stuffs */
2031 lang_finish();
2fa0b342
DHW
2032}
2033
2034
2035/* EXPORTED TO YACC */
1418c83b 2036
2fa0b342 2037void
1418c83b
SC
2038DEFUN(lang_add_wild,(section_name, filename),
2039 CONST char *CONST section_name AND
2040 CONST char *CONST filename)
2041{
2042 lang_wild_statement_type *new = new_stat(lang_wild_statement,
2043 stat_ptr);
2044
2045 if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0)
2046 {
2047 placed_commons = true;
2048 }
2049 if (filename != (char *)NULL) {
2050 lang_has_input_file = true;
2051 }
2052 new->section_name = section_name;
2053 new->filename = filename;
2054 lang_list_init(&new->children);
2055}
2056void
2057DEFUN(lang_section_start,(name, address),
2058 CONST char *name AND
2059 etree_type *address)
2fa0b342
DHW
2060{
2061 lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr);
2062 ad->section_name = name;
2063 ad->address = address;
2064}
1418c83b
SC
2065
2066void
2067DEFUN(lang_add_entry,(name),
2068 CONST char *name)
2fa0b342
DHW
2069{
2070 entry_symbol = name;
2071}
2072
2073void
1418c83b
SC
2074DEFUN(lang_add_target,(name),
2075 CONST char *name)
2fa0b342
DHW
2076{
2077 lang_target_statement_type *new = new_stat(lang_target_statement,
2078 stat_ptr);
2079 new->target = name;
2080
2081}
2fa0b342 2082
1418c83b
SC
2083
2084
2fa0b342
DHW
2085
2086void
1418c83b
SC
2087DEFUN(lang_add_map,(name),
2088 CONST char *name)
2fa0b342
DHW
2089{
2090 while (*name) {
2091 switch (*name) {
2092 case 'F':
2093 map_option_f = true;
2094 break;
2095 }
2096 name++;
2097 }
2098}
2099
1418c83b
SC
2100void
2101DEFUN(lang_add_fill,(exp),
2102 int exp)
2fa0b342
DHW
2103{
2104 lang_fill_statement_type *new = new_stat(lang_fill_statement,
2105 stat_ptr);
2106 new->fill = exp;
2107}
2108
1418c83b
SC
2109void
2110DEFUN(lang_add_data,(type, exp),
2111 int type AND
2112 union etree_union *exp)
2fa0b342
DHW
2113{
2114
2115 lang_data_statement_type *new = new_stat(lang_data_statement,
2116 stat_ptr);
2117 new->exp = exp;
2118 new->type = type;
2119
2120}
2121void
1418c83b
SC
2122DEFUN(lang_add_assignment,(exp),
2123 etree_type *exp)
2fa0b342
DHW
2124{
2125 lang_assignment_statement_type *new = new_stat(lang_assignment_statement,
2126 stat_ptr);
2127 new->exp = exp;
2128}
2129
2130void
1418c83b
SC
2131DEFUN(lang_add_attribute,(attribute),
2132 enum statement_enum attribute)
2fa0b342
DHW
2133{
2134 new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr);
2135}
2136
2137
2138
2139void
1418c83b
SC
2140DEFUN(lang_startup,(name),
2141 CONST char *name)
2fa0b342
DHW
2142{
2143 if (startup_file != (char *)NULL) {
2144 info("%P%FMultiple STARTUP files\n");
2145 }
2146 first_file->filename = name;
2147 first_file->local_sym_name = name;
2148
2149 startup_file= name;
2150}
2151void
1418c83b
SC
2152DEFUN(lang_float,(maybe),
2153 boolean maybe)
2fa0b342
DHW
2154{
2155 lang_float_flag = maybe;
2156}
2157
2158void
1418c83b
SC
2159DEFUN(lang_leave_output_section_statement,(fill, memspec),
2160 bfd_vma fill AND
2161 CONST char *memspec)
2fa0b342
DHW
2162{
2163 current_section->fill = fill;
2164 current_section->region = lang_memory_region_lookup(memspec);
2165 stat_ptr = &statement_list;
2166}
9f32f7c2
SC
2167/*
2168 Create an absolute symbol with the given name with the value of the
2169 address of first byte of the section named.
2fa0b342 2170
9f32f7c2
SC
2171 If the symbol already exists, then do nothing.
2172*/
8cb5eb31 2173void
1418c83b
SC
2174DEFUN(lang_abs_symbol_at_beginning_of,(section, name),
2175 CONST char *section AND
2176 CONST char *name)
8cb5eb31 2177{
c660714f 2178 if (ldsym_undefined(name)) {
9f32f7c2
SC
2179 extern bfd *output_bfd;
2180 extern asymbol *create_symbol();
2181 asection *s = bfd_get_section_by_name(output_bfd, section);
2182 asymbol *def = create_symbol(name,
2183 BSF_GLOBAL | BSF_EXPORT |
2184 BSF_ABSOLUTE,
2185 (asection *)NULL);
2186 if (s != (asection *)NULL) {
2187 def->value = s->vma;
2188 }
2189 else {
2190 def->value = 0;
2191 }
8cb5eb31
SC
2192 }
2193}
2194
9f32f7c2
SC
2195/*
2196 Create an absolute symbol with the given name with the value of the
2197 address of the first byte after the end of the section named.
2198
2199 If the symbol already exists, then do nothing.
2200*/
2fa0b342 2201void
1418c83b
SC
2202DEFUN(lang_abs_symbol_at_end_of,(section, name),
2203 CONST char *section AND
2204 CONST char *name)
2fa0b342 2205{
c660714f 2206 if (ldsym_undefined(name)){
9f32f7c2
SC
2207 extern bfd *output_bfd;
2208 extern asymbol *create_symbol();
2209 asection *s = bfd_get_section_by_name(output_bfd, section);
2210 /* Add a symbol called _end */
2211 asymbol *def = create_symbol(name,
2212 BSF_GLOBAL | BSF_EXPORT |
2213 BSF_ABSOLUTE,
2214 (asection *)NULL);
2215 if (s != (asection *)NULL) {
2216 def->value = s->vma + s->size;
2217 }
2218 else {
2219 def->value = 0;
2220 }
2fa0b342
DHW
2221 }
2222}
2223
2224void
1418c83b
SC
2225DEFUN(lang_statement_append,(list, element, field),
2226 lang_statement_list_type *list AND
2227 lang_statement_union_type *element AND
2228 lang_statement_union_type **field)
2fa0b342
DHW
2229{
2230 *(list->tail) = element;
2231 list->tail = field;
2232}
2233
097879bc
SC
2234/* Set the output format type */
2235void
2236DEFUN(lang_add_output_format,(format),
2237CONST char *format)
2238{
2239 output_target = format;
2240}
a37cc0c0 2241
This page took 0.175014 seconds and 4 git commands to generate.