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