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