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