]> Git Repo - binutils.git/blame - ld/ldlang.c
Cleaned up a load of declarations
[binutils.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 }
1418c83b
SC
646 if (strcmp(section,"COMMON") == 0
647 && default_common_section == (lang_output_section_statement_type*)NULL)
648 {
649 /* Remember the section that common is going to incase we later
650 get something which doesn't know where to put it */
651 default_common_section = output;
652 }
2fa0b342
DHW
653}
654
655/*
656 read in all the files
657 */
658static bfd *
097879bc
SC
659DEFUN(open_output,(name),
660 CONST char *CONST name)
2fa0b342 661{
1418c83b 662 extern CONST char *output_filename;
097879bc
SC
663 bfd *output;
664 if (output_target == (char *)NULL) {
665 if (current_target != (char *)NULL)
666 output_target = current_target;
667 else
668 output_target = default_target;
669 }
670 output = bfd_openw(name, output_target);
2fa0b342 671 output_filename = name;
097879bc 672
2fa0b342 673 if (output == (bfd *)NULL)
1418c83b
SC
674 {
675 if (bfd_error == invalid_target) {
097879bc 676 info("%P%F target %s not found\n", output_target);
1418c83b
SC
677 }
678 info("%P%F problem opening output file %s, %E", name);
2fa0b342 679 }
2fa0b342
DHW
680
681 output->flags |= D_PAGED;
682 bfd_set_format(output, bfd_object);
683 return output;
684}
1418c83b
SC
685
686
097879bc 687
1418c83b 688
2fa0b342 689static void
1418c83b
SC
690DEFUN(ldlang_open_output,(statement),
691 lang_statement_union_type *statement)
2fa0b342 692{
1418c83b
SC
693 switch (statement->header.type)
694 {
695 case lang_output_statement_enum:
097879bc 696 output_bfd = open_output(statement->output_statement.name);
2fa0b342 697 ldemul_set_output_arch();
2fa0b342 698 break;
1418c83b 699
2fa0b342 700 case lang_target_statement_enum:
1418c83b 701 current_target = statement->target_statement.target;
2fa0b342 702 break;
1418c83b 703 default:
2fa0b342 704 break;
1418c83b
SC
705 }
706}
2fa0b342 707
1418c83b
SC
708static void
709DEFUN(open_input_bfds,(statement),
710 lang_statement_union_type *statement)
711{
712 switch (statement->header.type)
713 {
714 case lang_target_statement_enum:
715 current_target = statement->target_statement.target;
2fa0b342 716 break;
1418c83b
SC
717 case lang_wild_statement_enum:
718 /* Maybe we should load the file's symbols */
719 if (statement->wild_statement.filename)
720 {
721 (void) lookup_name(statement->wild_statement.filename);
722 }
2fa0b342 723 break;
1418c83b
SC
724 case lang_input_statement_enum:
725 if (statement->input_statement.real == true)
726 {
727 statement->input_statement.target = current_target;
728 lookup_name(statement->input_statement.filename);
729 }
2fa0b342 730 break;
1418c83b 731 default:
2fa0b342
DHW
732 break;
733 }
2fa0b342 734}
2fa0b342
DHW
735/* If there are [COMMONS] statements, put a wild one into the bss section */
736
737static void
738lang_reasonable_defaults()
739{
1418c83b 740#if 0
8cb5eb31
SC
741 lang_output_section_statement_lookup(".text");
742 lang_output_section_statement_lookup(".data");
743
2fa0b342
DHW
744 default_common_section =
745 lang_output_section_statement_lookup(".bss");
8cb5eb31 746
1418c83b 747
2fa0b342
DHW
748 if (placed_commons == false) {
749 lang_wild_statement_type *new =
750 new_stat(lang_wild_statement,
751 &default_common_section->children);
752 new->section_name = "COMMON";
753 new->filename = (char *)NULL;
754 lang_list_init(&new->children);
755 }
1418c83b 756#endif
8cb5eb31 757
2fa0b342
DHW
758}
759
1418c83b
SC
760/*
761 Add the supplied name to the symbol table as an undefined reference.
762 Remove items from the chain as we open input bfds
763 */
764typedef struct ldlang_undef_chain_list_struct {
765 struct ldlang_undef_chain_list_struct *next;
766 char *name;
767} ldlang_undef_chain_list_type;
768
769static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
770
771void
772DEFUN(ldlang_add_undef,(name),
773 CONST char *CONST name)
2fa0b342 774{
1418c83b
SC
775 ldlang_undef_chain_list_type *new =
776 (ldlang_undef_chain_list_type
777 *)ldmalloc(sizeof(ldlang_undef_chain_list_type));
778
779 new->next = ldlang_undef_chain_list_head;
780 ldlang_undef_chain_list_head = new;
781
782 new->name = buystring(name);
783}
784/* Run through the list of undefineds created above and place them
785 into the linker hash table as undefined symbols belonging to the
786 script file.
787*/
788static void
789DEFUN_VOID(lang_place_undefineds)
790{
791 ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
792 while (ptr != (ldlang_undef_chain_list_type*)NULL) {
793 ldsym_type *sy = ldsym_get(ptr->name);
794 asymbol *def;
795 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
796 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
797 *def_ptr= def;
798 def->name = ptr->name;
799 def->flags = BSF_UNDEFINED;
800 def->section = (asection *)NULL;
801 Q_enter_global_ref(def_ptr);
802 ptr = ptr->next;
2fa0b342 803 }
1418c83b 804}
2fa0b342 805
1418c83b
SC
806
807
808/* Copy important data from out internal form to the bfd way. Also
809 create a section for the dummy file
810 */
811
812static void
813DEFUN_VOID(lang_create_output_section_statements)
814{
815 lang_statement_union_type*os;
816 for (os = lang_output_section_statement.head;
817 os != (lang_statement_union_type*)NULL;
818 os = os->output_section_statement.next) {
819 lang_output_section_statement_type *s =
820 &os->output_section_statement;
821 init_os(s);
822 }
823
824}
825
826static void
827DEFUN_VOID(lang_init_script_file)
828{
829 script_file = lang_add_input_file("script file",
830 lang_input_file_is_fake_enum,
831 (char *)NULL);
832 script_file->the_bfd = bfd_create("script file", output_bfd);
833 script_file->symbol_count = 0;
834 script_file->the_bfd->sections = output_bfd->sections;
2fa0b342
DHW
835}
836
837
1418c83b
SC
838
839
2fa0b342
DHW
840/* Open input files and attatch to output sections */
841static void
1418c83b
SC
842DEFUN(map_input_to_output_sections,(s, target, output_section_statement),
843 lang_statement_union_type *s AND
844 CONST char *target AND
845 lang_output_section_statement_type *output_section_statement)
2fa0b342
DHW
846{
847 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
848 {
849 switch (s->header.type) {
850 case lang_wild_statement_enum:
851 wild(&s->wild_statement, s->wild_statement.section_name,
852 s->wild_statement.filename, target,
853 output_section_statement);
854
855 break;
856
857 case lang_output_section_statement_enum:
1418c83b 858 map_input_to_output_sections(s->output_section_statement.children.head,
2fa0b342
DHW
859 target,
860 &s->output_section_statement);
861 break;
862 case lang_output_statement_enum:
863 break;
864 case lang_target_statement_enum:
865 target = s->target_statement.target;
866 break;
2fa0b342
DHW
867 case lang_fill_statement_enum:
868 case lang_input_section_enum:
869 case lang_object_symbols_statement_enum:
870 case lang_data_statement_enum:
1418c83b
SC
871 case lang_assignment_statement_enum:
872 case lang_padding_statement_enum:
2fa0b342
DHW
873 break;
874 case lang_afile_asection_pair_statement_enum:
875 FAIL();
876 break;
2fa0b342
DHW
877 case lang_address_statement_enum:
878 /* Mark the specified section with the supplied address */
879 {
880 lang_output_section_statement_type *os =
881 lang_output_section_statement_lookup
882 (s->address_statement.section_name);
883 os->addr_tree = s->address_statement.address;
884 }
885 break;
886 case lang_input_statement_enum:
887 /* A standard input statement, has no wildcards */
1418c83b 888 /* ldmain_open_file_read_symbol(&s->input_statement);*/
2fa0b342
DHW
889 break;
890 }
891 }
892}
893
894
895
896
897
898static void
1418c83b
SC
899DEFUN(print_output_section_statement,(output_section_statement),
900 lang_output_section_statement_type *output_section_statement)
2fa0b342
DHW
901{
902 asection *section = output_section_statement->bfd_section;
903 print_nl();
904 print_section(output_section_statement->name);
905
906 if (section) {
907 print_dot = section->vma;
908 print_space();
909 print_section("");
910 print_space();
911 print_address(section->vma);
912 print_space();
913 print_size(section->size);
914 print_space();
915 print_alignment(section->alignment_power);
916 print_space();
917#if 0
918 printf("%s flags", output_section_statement->region->name);
919 print_flags(stdout, &output_section_statement->flags);
920#endif
921
922 }
923 else {
924 printf("No attached output section");
925 }
926 print_nl();
927 print_statement(output_section_statement->children.head,
928 output_section_statement);
929
930}
931
932static void
1418c83b
SC
933DEFUN(print_assignment,(assignment, output_section),
934 lang_assignment_statement_type *assignment AND
935 lang_output_section_statement_type *output_section)
2fa0b342
DHW
936{
937 etree_value_type result;
938 print_section("");
939 print_space();
940 print_section("");
941 print_space();
942 print_address(print_dot);
943 print_space();
944 result = exp_fold_tree(assignment->exp->assign.src,
945 output_section,
946 lang_final_phase_enum,
947 print_dot,
948 &print_dot);
949
950 if (result.valid) {
951 print_address(result.value);
952 }
953 else
1418c83b
SC
954 {
955 printf("*undefined*");
956 }
2fa0b342
DHW
957 print_space();
958 exp_print_tree(stdout, assignment->exp);
959 printf("\n");
960}
961
962static void
1418c83b
SC
963DEFUN(print_input_statement,(statm),
964 lang_input_statement_type *statm)
2fa0b342 965{
070aa819
SC
966 if (statm->filename != (char *)NULL) {
967 printf("LOAD %s\n",statm->filename);
968 }
2fa0b342
DHW
969}
970
1418c83b
SC
971static void
972DEFUN(print_symbol,(q),
973 asymbol *q)
2fa0b342
DHW
974{
975 print_section("");
976 printf(" ");
977 print_section("");
978 printf(" ");
979 print_address(outside_symbol_address(q));
980 printf(" %s", q->name ? q->name : " ");
981 print_nl();
982}
1418c83b 983
2fa0b342 984static void
1418c83b
SC
985DEFUN(print_input_section,(in),
986 lang_input_section_type *in)
2fa0b342
DHW
987{
988 asection *i = in->section;
989
990 if(i->size != 0) {
991 print_section("");
992 printf(" ");
993 print_section(i->name);
994 printf(" ");
995 if (i->output_section) {
996 print_address(i->output_section->vma + i->output_offset);
997 printf(" ");
998 print_size(i->size);
999 printf(" ");
1000 print_alignment(i->alignment_power);
1001 printf(" ");
1002 if (in->ifile) {
1418c83b 1003
2fa0b342 1004 bfd *abfd = in->ifile->the_bfd;
1418c83b
SC
1005 if (in->ifile->just_syms_flag == true) {
1006 printf("symbols only ");
1007 }
1008
2fa0b342
DHW
1009 printf(" %s ",abfd->xvec->name);
1010 if(abfd->my_archive != (bfd *)NULL) {
1011 printf("[%s]%s", abfd->my_archive->filename,
1012 abfd->filename);
1013 }
1014 else {
1015 printf("%s", abfd->filename);
1016 }
1017 print_nl();
1018
1019 /* Find all the symbols in this file defined in this section */
1418c83b
SC
1020 {
1021 asymbol **p;
1022 for (p = in->ifile->asymbols; *p; p++) {
1023 asymbol *q = *p;
2fa0b342 1024
1418c83b
SC
1025 if (bfd_get_section(q) == i && q->flags & BSF_GLOBAL) {
1026 print_symbol(q);
1027 }
2fa0b342
DHW
1028 }
1029 }
2fa0b342
DHW
1030 }
1031 else {
1032 print_nl();
1033 }
1034
1035
1036 print_dot = outside_section_address(i) + i->size;
1037 }
1038 else {
1039 printf("No output section allocated\n");
1040 }
1041 }
1042}
2fa0b342 1043
2fa0b342 1044static void
1418c83b
SC
1045DEFUN(print_fill_statement,(fill),
1046 lang_fill_statement_type *fill)
2fa0b342
DHW
1047{
1048 printf("FILL mask ");
1049 print_fill( fill->fill);
1050}
1051
1052static void
1418c83b
SC
1053DEFUN(print_data_statement,(data),
1054 lang_data_statement_type *data)
2fa0b342
DHW
1055{
1056/* bfd_vma value; */
1057 print_section("");
1058 print_space();
1059 print_section("");
1060 print_space();
1061 ASSERT(print_dot == data->output_vma);
1062
1063 print_address(data->output_vma);
1064 print_space();
1065 print_address(data->value);
1066 print_space();
1067 switch (data->type) {
1068 case BYTE :
1069 printf("BYTE ");
1070 print_dot += BYTE_SIZE;
1071 break;
1072 case SHORT:
1073 printf("SHORT ");
1074 print_dot += SHORT_SIZE;
1075 break;
1076 case LONG:
1077 printf("LONG ");
1078 print_dot += LONG_SIZE;
1079 break;
1080 }
1081
1082 exp_print_tree(stdout, data->exp);
1083
1084 printf("\n");
1085}
1086
1087
1088static void
1418c83b
SC
1089DEFUN(print_padding_statement,(s),
1090 lang_padding_statement_type *s)
2fa0b342
DHW
1091{
1092 print_section("");
1093 print_space();
1094 print_section("*fill*");
1095 print_space();
1096 print_address(s->output_offset + s->output_section->vma);
1097 print_space();
1098 print_size(s->size);
1099 print_space();
1100 print_fill(s->fill);
1101 print_nl();
1102}
1103
1418c83b
SC
1104static void
1105DEFUN(print_wild_statement,(w,os),
1106 lang_wild_statement_type *w AND
1107 lang_output_section_statement_type *os)
2fa0b342
DHW
1108{
1109 if (w->filename != (char *)NULL) {
1110 printf("%s",w->filename);
1111 }
1112 else {
1113 printf("*");
1114 }
1115 if (w->section_name != (char *)NULL) {
1116 printf("(%s)",w->section_name);
1117 }
1118 else {
1119 printf("(*)");
1120 }
1121 print_nl();
1122 print_statement(w->children.head, os);
1123
1124}
1125static void
1418c83b
SC
1126DEFUN(print_statement,(s, os),
1127 lang_statement_union_type *s AND
1128 lang_output_section_statement_type *os)
2fa0b342
DHW
1129{
1130 while (s) {
1131 switch (s->header.type) {
1132 case lang_wild_statement_enum:
1418c83b
SC
1133 print_wild_statement(&s->wild_statement, os);
1134 break;
2fa0b342 1135 default:
1418c83b 1136 printf("Fail with %d\n",s->header.type);
2fa0b342
DHW
1137 FAIL();
1138 break;
1418c83b
SC
1139 case lang_address_statement_enum:
1140 printf("address\n");
1141 break;
2fa0b342
DHW
1142 break;
1143 case lang_object_symbols_statement_enum:
1144 printf("object symbols\n");
1145 break;
1146 case lang_fill_statement_enum:
1147 print_fill_statement(&s->fill_statement);
1148 break;
1149 case lang_data_statement_enum:
1150 print_data_statement(&s->data_statement);
1151 break;
2fa0b342
DHW
1152 case lang_input_section_enum:
1153 print_input_section(&s->input_section);
1154 break;
1155 case lang_padding_statement_enum:
1156 print_padding_statement(&s->padding_statement);
1157 break;
1158 case lang_output_section_statement_enum:
1159 print_output_section_statement(&s->output_section_statement);
1160 break;
1161 case lang_assignment_statement_enum:
1162 print_assignment(&s->assignment_statement,
1418c83b 1163 os);
2fa0b342
DHW
1164 break;
1165
1166
1167 case lang_target_statement_enum:
1168 printf("TARGET(%s)\n", s->target_statement.target);
1169 break;
1170 case lang_output_statement_enum:
097879bc
SC
1171 printf("OUTPUT(%s %s)\n",
1172 s->output_statement.name,
1173 output_target);
2fa0b342
DHW
1174 break;
1175 case lang_input_statement_enum:
1176 print_input_statement(&s->input_statement);
1177 break;
1178 case lang_afile_asection_pair_statement_enum:
1179 FAIL();
1180 break;
1181 }
1182 s = s->next;
1183 }
1184}
1185
1186
1187static void
1418c83b 1188DEFUN_VOID(print_statements)
2fa0b342
DHW
1189{
1190 print_statement(statement_list.head,
1191 (lang_output_section_statement_type *)NULL);
1192}
1193
1194static bfd_vma
1418c83b
SC
1195DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot),
1196 lang_statement_union_type **this_ptr AND
1197 fill_type fill AND
1198 unsigned int power AND
1199 asection * output_section_statement AND
1200 bfd_vma dot)
2fa0b342
DHW
1201{
1202 /* Align this section first to the
1203 input sections requirement, then
1204 to the output section's requirement.
1205 If this alignment is > than any seen before,
1206 then record it too. Perform the alignment by
1207 inserting a magic 'padding' statement.
1208 */
1209
1210 unsigned int alignment_needed = align_power(dot, power) - dot;
1211
1212 if (alignment_needed != 0)
1213 {
1214 lang_statement_union_type *new =
1215 (lang_statement_union_type *)
1216 ldmalloc(sizeof(lang_padding_statement_type));
1217 /* Link into existing chain */
1218 new->header.next = *this_ptr;
1219 *this_ptr = new;
1220 new->header.type = lang_padding_statement_enum;
1221 new->padding_statement.output_section = output_section_statement;
1222 new->padding_statement.output_offset =
1223 dot - output_section_statement->vma;
1224 new->padding_statement.fill = fill;
1225 new->padding_statement.size = alignment_needed;
1226 }
1227
1228
1229 /* Remember the most restrictive alignment */
1230 if (power > output_section_statement->alignment_power) {
1231 output_section_statement->alignment_power = power;
1232 }
1233 output_section_statement->size += alignment_needed;
1234 return alignment_needed + dot;
1235
1236}
1237
1418c83b 1238/* Work out how much this section will move the dot point */
2fa0b342 1239static bfd_vma
1418c83b
SC
1240DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot),
1241 lang_statement_union_type **this_ptr AND
1242 lang_output_section_statement_type*output_section_statement AND
1243 unsigned short fill AND
1244 bfd_vma dot)
2fa0b342
DHW
1245{
1246 lang_input_section_type *is = &((*this_ptr)->input_section);
1247 asection *i = is->section;
1418c83b
SC
1248
1249 if (is->ifile->just_syms_flag == false) {
2fa0b342
DHW
1250 dot = insert_pad(this_ptr, fill, i->alignment_power,
1251 output_section_statement->bfd_section, dot);
1252
1253 /* remember the largest size so we can malloc the largest area */
1254 /* needed for the output stage */
1255 if (i->size > largest_section) {
1256 largest_section = i->size;
1257 }
1258
1259 /* Remember where in the output section this input section goes */
1260 i->output_offset = dot - output_section_statement->bfd_section->vma;
1261
1262 /* Mark how big the output section must be to contain this now */
1263 dot += i->size;
1264 output_section_statement->bfd_section->size =
1265 dot - output_section_statement->bfd_section->vma;
1418c83b 1266}
2fa0b342
DHW
1267
1268 return dot ;
1269}
1270
1271
1272/* Work out the size of the output sections
1273 from the sizes of the input sections */
1274static bfd_vma
1418c83b
SC
1275DEFUN(lang_size_sections,(s, output_section_statement, prev, fill, dot),
1276 lang_statement_union_type *s AND
1277 lang_output_section_statement_type * output_section_statement AND
1278 lang_statement_union_type **prev AND
1279 unsigned short fill AND
1280 bfd_vma dot)
2fa0b342
DHW
1281{
1282 /* Size up the sections from their constituent parts */
1283 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1418c83b
SC
1284 {
1285 switch (s->header.type) {
1286 case lang_output_section_statement_enum:
1287 {
1288 bfd_vma after;
1289 lang_output_section_statement_type *os =
1290 &(s->output_section_statement);
1291 /* The start of a section */
2fa0b342 1292
1418c83b
SC
1293 if (os->addr_tree == (etree_type *)NULL) {
1294 /* No address specified for this section, get one
1295 from the region specification
1296 */
1297 if (os->region == (lang_memory_region_type *)NULL) {
2fa0b342 1298 os->region = lang_memory_region_lookup("*default*");
1418c83b
SC
1299 }
1300 dot = os->region->current;
2fa0b342 1301 }
1418c83b
SC
1302 else {
1303 etree_value_type r ;
1304 r = exp_fold_tree(os->addr_tree,
1305 (lang_output_section_statement_type *)NULL,
1306 lang_allocating_phase_enum,
1307 dot, &dot);
1308 if (r.valid == false) {
1309 info("%F%S: non constant address expression for section %s\n",
1310 os->name);
1311 }
1312 dot = r.value;
1313 }
1314 /* The section starts here */
1315 /* First, align to what the section needs */
2fa0b342 1316
1418c83b
SC
1317 dot = align_power(dot, os->bfd_section->alignment_power);
1318 os->bfd_section->vma = dot;
1319 os->bfd_section->output_offset = 0;
2fa0b342 1320
1418c83b
SC
1321 (void) lang_size_sections(os->children.head, os, &os->children.head,
1322 os->fill, dot);
1323 /* Ignore the size of the input sections, use the vma and size to */
1324 /* align against */
2fa0b342
DHW
1325
1326
1418c83b
SC
1327 after = ALIGN(os->bfd_section->vma +
1328 os->bfd_section->size,
1329 os->block_value) ;
2fa0b342
DHW
1330
1331
1418c83b
SC
1332 os->bfd_section->size = after - os->bfd_section->vma;
1333 dot = os->bfd_section->vma + os->bfd_section->size;
1334 os->processed = true;
2fa0b342 1335
1418c83b
SC
1336 /* Replace into region ? */
1337 if (os->addr_tree == (etree_type *)NULL
1338 && os->region !=(lang_memory_region_type*)NULL ) {
1339 os->region->current = dot;
1340 }
1341 }
2fa0b342 1342
1418c83b 1343 break;
2fa0b342 1344
1418c83b
SC
1345 case lang_data_statement_enum:
1346 {
1347 unsigned int size;
1348 s->data_statement.output_vma = dot;
1349 s->data_statement.output_section =
1350 output_section_statement->bfd_section;
2fa0b342 1351
1418c83b
SC
1352 switch (s->data_statement.type) {
1353 case LONG:
1354 size = LONG_SIZE;
1355 break;
1356 case SHORT:
1357 size = SHORT_SIZE;
1358 break;
1359 case BYTE:
1360 size = BYTE_SIZE;
1361 break;
2fa0b342 1362
1418c83b
SC
1363 }
1364 dot += size;
1365 output_section_statement->bfd_section->size += size;
1366 }
1367 break;
2fa0b342 1368
1418c83b 1369 case lang_wild_statement_enum:
2fa0b342 1370
1418c83b 1371 dot = lang_size_sections(s->wild_statement.children.head,
2fa0b342
DHW
1372 output_section_statement,
1373 &s->wild_statement.children.head,
1374
1375 fill, dot);
1376
1418c83b 1377 break;
2fa0b342 1378
1418c83b
SC
1379 case lang_object_symbols_statement_enum:
1380 create_object_symbols = output_section_statement;
1381 break;
1382 case lang_output_statement_enum:
1383 case lang_target_statement_enum:
1384 break;
1385 case lang_input_section_enum:
1386 dot = size_input_section(prev,
2fa0b342
DHW
1387 output_section_statement,
1388 output_section_statement->fill, dot);
1418c83b
SC
1389 break;
1390 case lang_input_statement_enum:
1391 break;
1392 case lang_fill_statement_enum:
1393 fill = s->fill_statement.fill;
1394 break;
1395 case lang_assignment_statement_enum:
2fa0b342 1396 {
1418c83b
SC
1397 bfd_vma newdot = dot;
1398 exp_fold_tree(s->assignment_statement.exp,
1399 output_section_statement,
1400 lang_allocating_phase_enum,
1401 dot,
1402 &newdot);
1403
1404 if (newdot != dot)
1405 /* We've been moved ! so insert a pad */
1406 {
1407 lang_statement_union_type *new =
1408 (lang_statement_union_type *)
1409 ldmalloc(sizeof(lang_padding_statement_type));
1410 /* Link into existing chain */
1411 new->header.next = *prev;
1412 *prev = new;
1413 new->header.type = lang_padding_statement_enum;
1414 new->padding_statement.output_section =
1415 output_section_statement->bfd_section;
1416 new->padding_statement.output_offset =
1417 dot - output_section_statement->bfd_section->vma;
1418 new->padding_statement.fill = fill;
1419 new->padding_statement.size = newdot - dot;
1420 output_section_statement->bfd_section->size +=
1421 new->padding_statement.size;
1422 dot = newdot;
1423 }
2fa0b342 1424 }
2fa0b342 1425
1418c83b
SC
1426 break;
1427 case lang_padding_statement_enum:
1428 FAIL();
1429 break;
1430 default:
1431 FAIL();
1432 break;
1433 case lang_address_statement_enum:
1434 break;
1435 }
1436 prev = &s->header.next;
2fa0b342 1437 }
2fa0b342
DHW
1438 return dot;
1439}
1440
1441
1442static bfd_vma
1418c83b
SC
1443DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot),
1444 lang_statement_union_type *s AND
1445 lang_output_section_statement_type * output_section_statement AND
1446 unsigned short fill AND
1447 bfd_vma dot)
2fa0b342
DHW
1448{
1449
1450 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1451 {
1452 switch (s->header.type) {
1453 case lang_output_section_statement_enum:
1454 {
1455 lang_output_section_statement_type *os =
1456 &(s->output_section_statement);
1457 dot = os->bfd_section->vma;
1458 (void) lang_do_assignments(os->children.head, os, os->fill, dot);
1459 dot = os->bfd_section->vma + os->bfd_section->size;
1460 }
1461 break;
1462 case lang_wild_statement_enum:
1463
1464 dot = lang_do_assignments(s->wild_statement.children.head,
1465 output_section_statement,
1466 fill, dot);
1467
1468 break;
1469
1470 case lang_object_symbols_statement_enum:
1471 case lang_output_statement_enum:
1472 case lang_target_statement_enum:
1418c83b 1473#if 0
2fa0b342 1474 case lang_common_statement_enum:
1418c83b 1475#endif
2fa0b342
DHW
1476 break;
1477 case lang_data_statement_enum:
1478 {
1479 etree_value_type value ;
1480 value = exp_fold_tree(s->data_statement.exp,
1481 0, lang_final_phase_enum, dot, &dot);
1482 s->data_statement.value = value.value;
1483 if (value.valid == false) info("%F%P: Invalid data statement\n");
1484 }
1485 switch (s->data_statement.type) {
1486 case LONG:
1487 dot += LONG_SIZE;
1488 break;
1489 case SHORT:
1490 dot += SHORT_SIZE;
1491 break;
1492 case BYTE:
1493 dot += BYTE_SIZE;
1494 break;
1495 }
1496 break;
1497 case lang_input_section_enum:
1498 {
1499 asection *in = s->input_section.section;
1500 dot += in->size;
1501 }
1502 break;
1503
1504 case lang_input_statement_enum:
1505 break;
1506 case lang_fill_statement_enum:
1507 fill = s->fill_statement.fill;
1508 break;
1509 case lang_assignment_statement_enum:
1510 {
1511 exp_fold_tree(s->assignment_statement.exp,
1512 output_section_statement,
1513 lang_final_phase_enum,
1514 dot,
1515 &dot);
1516 }
1517
1518 break;
1519 case lang_padding_statement_enum:
1520 dot += s->padding_statement.size;
1521 break;
1522 default:
1523 FAIL();
1524 break;
1525 case lang_address_statement_enum:
1526 break;
1527 }
1528
1529 }
1530 return dot;
1531}
1532
1533
1534
1418c83b
SC
1535static void
1536DEFUN_VOID(lang_relocate_globals)
2fa0b342
DHW
1537{
1538
1539 /*
1418c83b
SC
1540 Each ldsym_type maintains a chain of pointers to asymbols which
1541 references the definition. Replace each pointer to the referenence
1542 with a pointer to only one place, preferably the definition. If
1543 the defintion isn't available then the common symbol, and if
1544 there isn't one of them then choose one reference.
1545 */
2fa0b342
DHW
1546
1547 FOR_EACH_LDSYM(lgs) {
1548 asymbol *it;
1549 if (lgs->sdefs_chain) {
1550 it = *(lgs->sdefs_chain);
1551 }
1552 else if (lgs->scoms_chain != (asymbol **)NULL) {
1553 it = *(lgs->scoms_chain);
1554 }
1555 else if (lgs->srefs_chain != (asymbol **)NULL) {
1556 it = *(lgs->srefs_chain);
1557 }
1558 else {
1418c83b
SC
1559 /* This can happen when the command line asked for a symbol to
1560 be -u */
1561 it = (asymbol *)NULL;
2fa0b342
DHW
1562 }
1563 if (it != (asymbol *)NULL)
1418c83b
SC
1564 {
1565 asymbol **ptr= lgs->srefs_chain;
2fa0b342 1566
1418c83b
SC
1567 while (ptr != (asymbol **)NULL) {
1568 asymbol *ref = *ptr;
1569 *ptr = it;
1570 ptr = (asymbol **)(ref->udata);
1571 }
2fa0b342 1572 }
2fa0b342
DHW
1573 }
1574}
1575
1576
1577
2fa0b342 1578static void
1418c83b 1579DEFUN_VOID(lang_finish)
2fa0b342
DHW
1580{
1581 ldsym_type *lgs;
1582
1583 if (entry_symbol == (char *)NULL) {
1584 /* No entry has been specified, look for start */
1585 entry_symbol = "start";
1586 }
1587 lgs = ldsym_get_soft(entry_symbol);
1588 if (lgs && lgs->sdefs_chain) {
1589 asymbol *sy = *(lgs->sdefs_chain);
1590 /* We can set the entry address*/
1591 bfd_set_start_address(output_bfd,
1592 outside_symbol_address(sy));
1593
1594 }
1595 else {
1596 /* Can't find anything reasonable,
1597 use the first address in the text section
1598 */
1599 asection *ts = bfd_get_section_by_name(output_bfd, ".text");
1600 if (ts) {
1601 bfd_set_start_address(output_bfd, ts->vma);
1602 }
1603 }
1604}
1605
1606/* By now we know the target architecture, and we may have an */
1607/* ldfile_output_machine_name */
1608static void
1418c83b 1609DEFUN_VOID(lang_check)
2fa0b342
DHW
1610{
1611 lang_statement_union_type *file;
1612
1613
1614 for (file = file_chain.head;
1615 file != (lang_statement_union_type *)NULL;
1616 file=file->input_statement.next)
1418c83b
SC
1617 {
1618 /* Inspect the architecture and ensure we're linking like
1619 with like
1620 */
1621
1622 if (bfd_arch_compatible( file->input_statement.the_bfd,
1623 output_bfd,
1624 &ldfile_output_architecture,
1625 &ldfile_output_machine)) {
1626 bfd_set_arch_mach(output_bfd,
1627 ldfile_output_architecture, ldfile_output_machine);
1628 }
1629 else {
1630 enum bfd_architecture this_architecture =
1631 bfd_get_architecture(file->input_statement.the_bfd);
1632 unsigned long this_machine =
1633 bfd_get_machine(file->input_statement.the_bfd);
2fa0b342 1634
1418c83b
SC
1635 info("%I: architecture %s",
1636 file,
1637 bfd_printable_arch_mach(this_architecture, this_machine));
1638 info(" incompatible with output %s\n",
1639 bfd_printable_arch_mach(ldfile_output_architecture,
1640 ldfile_output_machine));
1641 ldfile_output_architecture = this_architecture;
1642 ldfile_output_machine = this_machine;
1643 bfd_set_arch_mach(output_bfd,
1644 ldfile_output_architecture,
1645 ldfile_output_machine);
2fa0b342
DHW
1646
1647
1418c83b 1648 }
2fa0b342 1649 }
2fa0b342
DHW
1650}
1651
1652
1653/*
1654 * run through all the global common symbols and tie them
1655 * to the output section requested.
1656 */
1657
1658static void
1418c83b 1659DEFUN_VOID(lang_common)
2fa0b342
DHW
1660{
1661 ldsym_type *lgs;
1662 if (config.relocateable_output == false ||
1663 command_line.force_common_definition== true) {
1664 for (lgs = symbol_head;
1665 lgs != (ldsym_type *)NULL;
1666 lgs=lgs->next)
1418c83b
SC
1667 {
1668 asymbol *com ;
1669 unsigned int power_of_two;
1670 size_t size;
1671 size_t align;
1672 if (lgs->scoms_chain != (asymbol **)NULL) {
1673 com = *(lgs->scoms_chain);
1674 size = com->value;
1675 switch (size) {
1676 case 0:
1677 case 1:
1678 align = 1;
1679 power_of_two = 0;
1680 break;
1681 case 2:
1682 power_of_two = 1;
1683 align = 2;
1684 break;
1685 case 3:
1686 case 4:
1687 power_of_two =2;
1688 align = 4;
1689 break;
1690 case 5:
1691 case 6:
1692 case 7:
1693 case 8:
1694 power_of_two = 3;
2fa0b342 1695 align = 8;
1418c83b
SC
1696 break;
1697 default:
1698 power_of_two = 4;
1699 align = 16;
1700 break;
2fa0b342 1701 }
2fa0b342
DHW
1702
1703
1418c83b
SC
1704 /* Change from a common symbol into a definition of
1705 a symbol */
1706 lgs->sdefs_chain = lgs->scoms_chain;
1707 lgs->scoms_chain = (asymbol **)NULL;
1708 commons_pending--;
1709 /* Point to the correct common section */
1710 com->section =
1711 ((lang_input_statement_type *)
1712 (com->the_bfd->usrdata))->common_section;
1713 /* Fix the size of the common section */
1714 com->section->size = ALIGN(com->section->size, align);
1715
1716 /* Remember if this is the biggest alignment ever seen */
1717 if (power_of_two > com->section->alignment_power) {
1718 com->section->alignment_power = power_of_two;
2fa0b342 1719 }
1418c83b
SC
1720
1721
097879bc
SC
1722 com->flags = BSF_EXPORT | BSF_GLOBAL ;
1723
1418c83b
SC
1724
1725 if (write_map)
1726 {
1727 printf ("Allocating common %s: %x at %x\n",
1728 lgs->name,
1729 (unsigned) size,
1730 (unsigned) com->section->size);
1731 }
1732 com->value = com->section->size;
1733 com->section->size += size;
097879bc
SC
1734 com->the_bfd = output_bfd;
1735
1418c83b
SC
1736
1737 }
2fa0b342 1738 }
2fa0b342 1739 }
1418c83b
SC
1740
1741
2fa0b342
DHW
1742}
1743
1744/*
1745run through the input files and ensure that every input
1746section has somewhere to go. If one is found without
1747a destination then create an input request and place it
1748into the statement tree.
1749*/
1750
1418c83b
SC
1751static void
1752DEFUN_VOID(lang_place_orphans)
2fa0b342
DHW
1753{
1754 lang_input_statement_type *file;
1755 for (file = (lang_input_statement_type*)file_chain.head;
1756 file != (lang_input_statement_type*)NULL;
1757 file = (lang_input_statement_type*)file->next) {
1758 asection *s;
1759 for (s = file->the_bfd->sections;
1760 s != (asection *)NULL;
1761 s = s->next) {
1762 if ( s->output_section == (asection *)NULL) {
1763 /* This section of the file is not attatched, root
1764 around for a sensible place for it to go */
1765
1766 if (file->common_section == s) {
1767 /* This is a lonely common section which must
1768 have come from an archive. We attatch to the
1769 section with the wildcard */
1418c83b
SC
1770 if (config.relocateable_output != true
1771 && command_line.force_common_definition == false) {
1772 if (default_common_section ==
1773 (lang_output_section_statement_type *)NULL) {
1774 info("%P: No [COMMON] command, defaulting to .bss\n");
1775
1776 default_common_section =
1777 lang_output_section_statement_lookup(".bss");
1778
1779 }
1780 wild_doit(&default_common_section->children, s,
1781 default_common_section, file);
1782 }
2fa0b342
DHW
1783 }
1784 else {
1785 lang_output_section_statement_type *os =
1786 lang_output_section_statement_lookup(s->name);
1787
1788 wild_doit(&os->children, s, os, file);
1789 }
1790 }
1791 }
2fa0b342
DHW
1792 }
1793}
1794
1795
2fa0b342 1796void
1418c83b
SC
1797DEFUN(lang_set_flags,(ptr, flags),
1798 lang_section_flags_type *ptr AND
1799 CONST char *flags)
2fa0b342
DHW
1800{
1801 boolean state = true;
1802 ptr->flag_read = false;
1803 ptr->flag_write = false;
1804 ptr->flag_executable = false;
1805 ptr->flag_loadable= false;
1806 while (*flags)
1418c83b
SC
1807 {
1808 if (*flags == '!') {
1809 state = false;
1810 flags++;
1811 }
1812 else state = true;
1813 switch (*flags) {
1814 case 'R':
1815 ptr->flag_read = state;
1816 break;
1817 case 'W':
1818 ptr->flag_write = state;
1819 break;
1820 case 'X':
1821 ptr->flag_executable= state;
1822 break;
1823 case 'L':
1824 ptr->flag_loadable= state;
1825 break;
1826 default:
1827 info("%P%F illegal syntax in flags\n");
1828 break;
1829 }
2fa0b342
DHW
1830 flags++;
1831 }
2fa0b342
DHW
1832}
1833
1834
1835
1836void
1418c83b
SC
1837DEFUN(lang_for_each_file,(func),
1838 PROTO(void, (*func),(lang_input_statement_type *)))
2fa0b342
DHW
1839{
1840 lang_input_statement_type *f;
1841 for (f = (lang_input_statement_type *)file_chain.head;
1842 f != (lang_input_statement_type *)NULL;
1843 f = (lang_input_statement_type *)f->next)
1418c83b
SC
1844 {
1845 func(f);
1846 }
2fa0b342
DHW
1847}
1848
1849
1850void
1418c83b
SC
1851DEFUN(lang_for_each_input_section, (func),
1852 PROTO(void ,(*func),(bfd *ab, asection*as)))
2fa0b342
DHW
1853{
1854 lang_input_statement_type *f;
1855 for (f = (lang_input_statement_type *)file_chain.head;
1856 f != (lang_input_statement_type *)NULL;
1857 f = (lang_input_statement_type *)f->next)
1858 {
1859 asection *s;
1860 for (s = f->the_bfd->sections;
1861 s != (asection *)NULL;
1862 s = s->next) {
1863 func(f->the_bfd, s);
1864 }
1865 }
1866}
1867
1868
1869
1870void
1418c83b
SC
1871DEFUN(ldlang_add_file,(entry),
1872 lang_input_statement_type *entry)
2fa0b342 1873{
1418c83b 1874
2fa0b342
DHW
1875 lang_statement_append(&file_chain,
1876 (lang_statement_union_type *)entry,
1877 &entry->next);
1878}
1879
1880
1881
1882void
1418c83b
SC
1883DEFUN(lang_add_output,(name),
1884 CONST char *name)
2fa0b342
DHW
1885{
1886 lang_output_statement_type *new = new_stat(lang_output_statement,
1887 stat_ptr);
1888 new->name = name;
1889 had_output_filename = true;
1890}
1891
1892
1893static lang_output_section_statement_type *current_section;
1894
1895void
1418c83b
SC
1896DEFUN(lang_enter_output_section_statement,
1897 (output_section_statement_name,
1898 address_exp,
1899 block_value),
1900 char *output_section_statement_name AND
1901 etree_type *address_exp AND
1902 bfd_vma block_value)
2fa0b342
DHW
1903{
1904 lang_output_section_statement_type *os;
1905 current_section =
1906 os =
1907 lang_output_section_statement_lookup(output_section_statement_name);
1908
1909
1910 /* Add this statement to tree */
1911 /* add_statement(lang_output_section_statement_enum,
1912 output_section_statement);*/
1913 /* Make next things chain into subchain of this */
1914
1915 if (os->addr_tree ==
1916 (etree_type *)NULL) {
1917 os->addr_tree =
1918 address_exp;
1919 }
1920 os->block_value = block_value;
1921 stat_ptr = & os->children;
1922
1923}
1924
1925
1926void
1418c83b 1927DEFUN_VOID(lang_final)
2fa0b342
DHW
1928{
1929 if (had_output_filename == false) {
1930 lang_add_output("a.out");
1931 }
2fa0b342
DHW
1932}
1933
1934
1935
1936
1937
1418c83b
SC
1938asymbol *
1939DEFUN(create_symbol,(name, flags, section),
1940 CONST char *name AND
1941 flagword flags AND
1942 asection *section)
2fa0b342
DHW
1943{
1944 extern lang_input_statement_type *script_file;
1945 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
1946 /* Add this definition to script file */
1947 asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
1418c83b 1948 def->name = buystring(name);
2fa0b342
DHW
1949 def->udata = 0;
1950 def->flags = flags;
1951 def->section = section;
1952
1953 *def_ptr = def;
1954 Q_enter_global_ref(def_ptr);
1955 return def;
1956}
1957
1958
1959void
1418c83b
SC
1960DEFUN_VOID(lang_process)
1961{
1962 if (had_script == false) {
1963 parse_line(ldemul_get_script());
1964 }
1965 lang_reasonable_defaults();
1966 current_target = default_target;
1967
1968 lang_for_each_statement(ldlang_open_output); /* Open the output file */
1969 /* For each output section statement, create a section in the output
1970 file */
1971 lang_create_output_section_statements();
1972
1973 /* Create a dummy bfd for the script */
1974 lang_init_script_file();
1975
1976 /* Add to the hash table all undefineds on the command line */
1977 lang_place_undefineds();
1978
1979 /* Create a bfd for each input file */
1980 current_target = default_target;
1981 lang_for_each_statement(open_input_bfds);
1982
1983 common_section.userdata = &common_section_userdata;
1984
1985 /* Run through the contours of the script and attatch input sections
1986 to the correct output sections
1987 */
1988 map_input_to_output_sections(statement_list.head, (char *)NULL,
1989 ( lang_output_section_statement_type *)NULL);
1990
1991 /* Find any sections not attatched explicitly and handle them */
1992 lang_place_orphans();
1993
1994 /* Size up the common data */
1995 lang_common();
1996
1997 ldemul_before_allocation();
1998
1999 /* Size up the sections */
2000 lang_size_sections(statement_list.head,
2001 (lang_output_section_statement_type *)NULL,
2002 &(statement_list.head), 0, (bfd_vma)0);
2003
2004 /* See if anything special should be done now we know how big
2005 everything is */
2006 ldemul_after_allocation();
2007
2008 /* Do all the assignments, now that we know the final restingplaces
2009 of all the symbols */
2010
2011 lang_do_assignments(statement_list.head,
2012 (lang_output_section_statement_type *)NULL,
2013 0, (bfd_vma)0);
2014
2015 /* Make sure that we're not mixing architectures */
2016
2017 lang_check();
2018
2019 /* Move the global symbols around */
2020 lang_relocate_globals();
2021
2022 /* Final stuffs */
2023 lang_finish();
2fa0b342
DHW
2024}
2025
2026
2027/* EXPORTED TO YACC */
1418c83b 2028
2fa0b342 2029void
1418c83b
SC
2030DEFUN(lang_add_wild,(section_name, filename),
2031 CONST char *CONST section_name AND
2032 CONST char *CONST filename)
2033{
2034 lang_wild_statement_type *new = new_stat(lang_wild_statement,
2035 stat_ptr);
2036
2037 if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0)
2038 {
2039 placed_commons = true;
2040 }
2041 if (filename != (char *)NULL) {
2042 lang_has_input_file = true;
2043 }
2044 new->section_name = section_name;
2045 new->filename = filename;
2046 lang_list_init(&new->children);
2047}
2048void
2049DEFUN(lang_section_start,(name, address),
2050 CONST char *name AND
2051 etree_type *address)
2fa0b342
DHW
2052{
2053 lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr);
2054 ad->section_name = name;
2055 ad->address = address;
2056}
1418c83b
SC
2057
2058void
2059DEFUN(lang_add_entry,(name),
2060 CONST char *name)
2fa0b342
DHW
2061{
2062 entry_symbol = name;
2063}
2064
2065void
1418c83b
SC
2066DEFUN(lang_add_target,(name),
2067 CONST char *name)
2fa0b342
DHW
2068{
2069 lang_target_statement_type *new = new_stat(lang_target_statement,
2070 stat_ptr);
2071 new->target = name;
2072
2073}
2fa0b342 2074
1418c83b
SC
2075
2076
2fa0b342
DHW
2077
2078void
1418c83b
SC
2079DEFUN(lang_add_map,(name),
2080 CONST char *name)
2fa0b342
DHW
2081{
2082 while (*name) {
2083 switch (*name) {
2084 case 'F':
2085 map_option_f = true;
2086 break;
2087 }
2088 name++;
2089 }
2090}
2091
1418c83b
SC
2092void
2093DEFUN(lang_add_fill,(exp),
2094 int exp)
2fa0b342
DHW
2095{
2096 lang_fill_statement_type *new = new_stat(lang_fill_statement,
2097 stat_ptr);
2098 new->fill = exp;
2099}
2100
1418c83b
SC
2101void
2102DEFUN(lang_add_data,(type, exp),
2103 int type AND
2104 union etree_union *exp)
2fa0b342
DHW
2105{
2106
2107 lang_data_statement_type *new = new_stat(lang_data_statement,
2108 stat_ptr);
2109 new->exp = exp;
2110 new->type = type;
2111
2112}
2113void
1418c83b
SC
2114DEFUN(lang_add_assignment,(exp),
2115 etree_type *exp)
2fa0b342
DHW
2116{
2117 lang_assignment_statement_type *new = new_stat(lang_assignment_statement,
2118 stat_ptr);
2119 new->exp = exp;
2120}
2121
2122void
1418c83b
SC
2123DEFUN(lang_add_attribute,(attribute),
2124 enum statement_enum attribute)
2fa0b342
DHW
2125{
2126 new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr);
2127}
2128
2129
2130
2131void
1418c83b
SC
2132DEFUN(lang_startup,(name),
2133 CONST char *name)
2fa0b342
DHW
2134{
2135 if (startup_file != (char *)NULL) {
2136 info("%P%FMultiple STARTUP files\n");
2137 }
2138 first_file->filename = name;
2139 first_file->local_sym_name = name;
2140
2141 startup_file= name;
2142}
2143void
1418c83b
SC
2144DEFUN(lang_float,(maybe),
2145 boolean maybe)
2fa0b342
DHW
2146{
2147 lang_float_flag = maybe;
2148}
2149
2150void
1418c83b
SC
2151DEFUN(lang_leave_output_section_statement,(fill, memspec),
2152 bfd_vma fill AND
2153 CONST char *memspec)
2fa0b342
DHW
2154{
2155 current_section->fill = fill;
2156 current_section->region = lang_memory_region_lookup(memspec);
2157 stat_ptr = &statement_list;
2158}
9f32f7c2
SC
2159/*
2160 Create an absolute symbol with the given name with the value of the
2161 address of first byte of the section named.
2fa0b342 2162
9f32f7c2
SC
2163 If the symbol already exists, then do nothing.
2164*/
8cb5eb31 2165void
1418c83b
SC
2166DEFUN(lang_abs_symbol_at_beginning_of,(section, name),
2167 CONST char *section AND
2168 CONST char *name)
8cb5eb31 2169{
c660714f 2170 if (ldsym_undefined(name)) {
9f32f7c2
SC
2171 extern bfd *output_bfd;
2172 extern asymbol *create_symbol();
2173 asection *s = bfd_get_section_by_name(output_bfd, section);
2174 asymbol *def = create_symbol(name,
2175 BSF_GLOBAL | BSF_EXPORT |
2176 BSF_ABSOLUTE,
2177 (asection *)NULL);
2178 if (s != (asection *)NULL) {
2179 def->value = s->vma;
2180 }
2181 else {
2182 def->value = 0;
2183 }
8cb5eb31
SC
2184 }
2185}
2186
9f32f7c2
SC
2187/*
2188 Create an absolute symbol with the given name with the value of the
2189 address of the first byte after the end of the section named.
2190
2191 If the symbol already exists, then do nothing.
2192*/
2fa0b342 2193void
1418c83b
SC
2194DEFUN(lang_abs_symbol_at_end_of,(section, name),
2195 CONST char *section AND
2196 CONST char *name)
2fa0b342 2197{
c660714f 2198 if (ldsym_undefined(name)){
9f32f7c2
SC
2199 extern bfd *output_bfd;
2200 extern asymbol *create_symbol();
2201 asection *s = bfd_get_section_by_name(output_bfd, section);
2202 /* Add a symbol called _end */
2203 asymbol *def = create_symbol(name,
2204 BSF_GLOBAL | BSF_EXPORT |
2205 BSF_ABSOLUTE,
2206 (asection *)NULL);
2207 if (s != (asection *)NULL) {
2208 def->value = s->vma + s->size;
2209 }
2210 else {
2211 def->value = 0;
2212 }
2fa0b342
DHW
2213 }
2214}
2215
2216void
1418c83b
SC
2217DEFUN(lang_statement_append,(list, element, field),
2218 lang_statement_list_type *list AND
2219 lang_statement_union_type *element AND
2220 lang_statement_union_type **field)
2fa0b342
DHW
2221{
2222 *(list->tail) = element;
2223 list->tail = field;
2224}
2225
097879bc
SC
2226/* Set the output format type */
2227void
2228DEFUN(lang_add_output_format,(format),
2229CONST char *format)
2230{
2231 output_target = format;
2232}
This page took 0.28768 seconds and 4 git commands to generate.