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