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