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