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