]> Git Repo - binutils.git/blame - ld/ldlang.c
Make undefined refs not make an executable
[binutils.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;
aa34a7c3 1021 int size = i->reloc_done ?
c611e285
SC
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 1099
aa34a7c3 1100 print_address(data->output_vma + data->output_section->vma);
2fa0b342
DHW
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();
aa34a7c3 1139 print_dot = s->output_offset + s->output_section->vma + s->size;
2fa0b342
DHW
1140}
1141
1418c83b
SC
1142static void
1143DEFUN(print_wild_statement,(w,os),
1144 lang_wild_statement_type *w AND
1145 lang_output_section_statement_type *os)
2fa0b342 1146{
6fd50a20 1147 fprintf(config.map_file, " from ");
2fa0b342 1148 if (w->filename != (char *)NULL) {
6fd50a20 1149 fprintf(config.map_file, "%s",w->filename);
2fa0b342
DHW
1150 }
1151 else {
6fd50a20 1152 fprintf(config.map_file, "*");
2fa0b342
DHW
1153 }
1154 if (w->section_name != (char *)NULL) {
6fd50a20 1155 fprintf(config.map_file, "(%s)",w->section_name);
2fa0b342
DHW
1156 }
1157 else {
6fd50a20 1158 fprintf(config.map_file, "(*)");
2fa0b342
DHW
1159 }
1160 print_nl();
1161 print_statement(w->children.head, os);
1162
1163}
1164static void
1418c83b
SC
1165DEFUN(print_statement,(s, os),
1166 lang_statement_union_type *s AND
1167 lang_output_section_statement_type *os)
2fa0b342 1168{
c611e285
SC
1169 while (s)
1170 {
1171 switch (s->header.type)
1172 {
1173 case lang_constructors_statement_enum:
6fd50a20 1174 fprintf(config.map_file, "constructors:\n");
c611e285
SC
1175 print_statement(constructor_list.head, os);
1176 break;
1177 case lang_wild_statement_enum:
1178 print_wild_statement(&s->wild_statement, os);
1179 break;
1180 default:
6fd50a20 1181 fprintf(config.map_file, "Fail with %d\n",s->header.type);
c611e285
SC
1182 FAIL();
1183 break;
1184 case lang_address_statement_enum:
6fd50a20 1185 fprintf(config.map_file, "address\n");
c611e285
SC
1186 break;
1187 break;
1188 case lang_object_symbols_statement_enum:
6fd50a20 1189 fprintf(config.map_file, "object symbols\n");
c611e285
SC
1190 break;
1191 case lang_fill_statement_enum:
1192 print_fill_statement(&s->fill_statement);
1193 break;
1194 case lang_data_statement_enum:
1195 print_data_statement(&s->data_statement);
1196 break;
1197 case lang_input_section_enum:
1198 print_input_section(&s->input_section);
1199 break;
1200 case lang_padding_statement_enum:
1201 print_padding_statement(&s->padding_statement);
1202 break;
1203 case lang_output_section_statement_enum:
1204 print_output_section_statement(&s->output_section_statement);
1205 break;
1206 case lang_assignment_statement_enum:
1207 print_assignment(&s->assignment_statement,
1208 os);
1209 break;
1210 case lang_target_statement_enum:
6fd50a20 1211 fprintf(config.map_file, "TARGET(%s)\n", s->target_statement.target);
c611e285
SC
1212 break;
1213 case lang_output_statement_enum:
6fd50a20 1214 fprintf(config.map_file, "OUTPUT(%s %s)\n",
c611e285
SC
1215 s->output_statement.name,
1216 output_target);
1217 break;
1218 case lang_input_statement_enum:
1219 print_input_statement(&s->input_statement);
1220 break;
1221 case lang_afile_asection_pair_statement_enum:
1222 FAIL();
1223 break;
1224 }
1225 s = s->next;
2fa0b342 1226 }
2fa0b342
DHW
1227}
1228
1229
1230static void
1418c83b 1231DEFUN_VOID(print_statements)
2fa0b342
DHW
1232{
1233 print_statement(statement_list.head,
1234 (lang_output_section_statement_type *)NULL);
1235}
1236
1237static bfd_vma
1418c83b
SC
1238DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot),
1239 lang_statement_union_type **this_ptr AND
1240 fill_type fill AND
1241 unsigned int power AND
1242 asection * output_section_statement AND
1243 bfd_vma dot)
2fa0b342
DHW
1244{
1245 /* Align this section first to the
1246 input sections requirement, then
1247 to the output section's requirement.
1248 If this alignment is > than any seen before,
1249 then record it too. Perform the alignment by
1250 inserting a magic 'padding' statement.
1251 */
1252
1253 unsigned int alignment_needed = align_power(dot, power) - dot;
1254
1255 if (alignment_needed != 0)
1256 {
1257 lang_statement_union_type *new =
1258 (lang_statement_union_type *)
3f8d46e7 1259 ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type)));
2fa0b342
DHW
1260 /* Link into existing chain */
1261 new->header.next = *this_ptr;
1262 *this_ptr = new;
1263 new->header.type = lang_padding_statement_enum;
1264 new->padding_statement.output_section = output_section_statement;
1265 new->padding_statement.output_offset =
1266 dot - output_section_statement->vma;
1267 new->padding_statement.fill = fill;
1268 new->padding_statement.size = alignment_needed;
1269 }
1270
1271
1272 /* Remember the most restrictive alignment */
1273 if (power > output_section_statement->alignment_power) {
1274 output_section_statement->alignment_power = power;
1275 }
c611e285 1276 output_section_statement->_raw_size += alignment_needed;
2fa0b342
DHW
1277 return alignment_needed + dot;
1278
1279}
1280
1418c83b 1281/* Work out how much this section will move the dot point */
2fa0b342 1282static bfd_vma
1418c83b
SC
1283DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot),
1284 lang_statement_union_type **this_ptr AND
1285 lang_output_section_statement_type*output_section_statement AND
1286 unsigned short fill AND
1287 bfd_vma dot)
2fa0b342
DHW
1288{
1289 lang_input_section_type *is = &((*this_ptr)->input_section);
1290 asection *i = is->section;
1418c83b
SC
1291
1292 if (is->ifile->just_syms_flag == false) {
ac004870
SC
1293 dot = insert_pad(this_ptr, fill, i->alignment_power,
1294 output_section_statement->bfd_section, dot);
2fa0b342 1295
7bc4a0d7
SC
1296 /* remember the largest size so we can malloc the largest area
1297 needed for the output stage. Only remember the size of sections
1298 which we will actually allocate */
1299 if (((i->flags &
1300 (SEC_HAS_CONTENTS | SEC_LOAD)) == (SEC_HAS_CONTENTS | SEC_LOAD))
c611e285
SC
1301 && (bfd_get_section_size_before_reloc(i) > largest_section)) {
1302 largest_section = bfd_get_section_size_before_reloc(i);
ac004870 1303 }
2fa0b342 1304
ac004870 1305 /* Remember where in the output section this input section goes */
2fa0b342 1306
ac004870
SC
1307 i->output_offset = dot - output_section_statement->bfd_section->vma;
1308
1309 /* Mark how big the output section must be to contain this now */
c611e285
SC
1310 dot += bfd_get_section_size_before_reloc(i);
1311output_section_statement->bfd_section->_raw_size =
ac004870
SC
1312 dot - output_section_statement->bfd_section->vma;
1313 }
1314 else
1315 {
1316 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1317 }
2fa0b342
DHW
1318
1319 return dot ;
1320}
1321
c611e285 1322#if 0
2fa0b342 1323/* Work out the size of the output sections
c611e285 1324 from the sizes of the input sections */
2fa0b342 1325static bfd_vma
c611e285
SC
1326DEFUN(lang_size_sections,(s, output_section_statement, prev, fill,
1327 dot),
1418c83b
SC
1328 lang_statement_union_type *s AND
1329 lang_output_section_statement_type * output_section_statement AND
1330 lang_statement_union_type **prev AND
1331 unsigned short fill AND
1332 bfd_vma dot)
c611e285 1333
2fa0b342
DHW
1334{
1335 /* Size up the sections from their constituent parts */
1336 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1418c83b
SC
1337 {
1338 switch (s->header.type) {
81016051
SC
1339
1340
1418c83b
SC
1341 case lang_output_section_statement_enum:
1342 {
1343 bfd_vma after;
1344 lang_output_section_statement_type *os =
1345 &(s->output_section_statement);
1346 /* The start of a section */
2fa0b342 1347
1418c83b
SC
1348 if (os->addr_tree == (etree_type *)NULL) {
1349 /* No address specified for this section, get one
1350 from the region specification
1351 */
1352 if (os->region == (lang_memory_region_type *)NULL) {
2fa0b342 1353 os->region = lang_memory_region_lookup("*default*");
1418c83b
SC
1354 }
1355 dot = os->region->current;
2fa0b342 1356 }
1418c83b
SC
1357 else {
1358 etree_value_type r ;
1359 r = exp_fold_tree(os->addr_tree,
1360 (lang_output_section_statement_type *)NULL,
1361 lang_allocating_phase_enum,
1362 dot, &dot);
1363 if (r.valid == false) {
c611e285 1364 einfo("%F%S: non constant address expression for section %s\n",
1418c83b
SC
1365 os->name);
1366 }
1367 dot = r.value;
1368 }
1369 /* The section starts here */
1370 /* First, align to what the section needs */
2fa0b342 1371
1418c83b
SC
1372 dot = align_power(dot, os->bfd_section->alignment_power);
1373 os->bfd_section->vma = dot;
1374 os->bfd_section->output_offset = 0;
2fa0b342 1375
1418c83b
SC
1376 (void) lang_size_sections(os->children.head, os, &os->children.head,
1377 os->fill, dot);
1378 /* Ignore the size of the input sections, use the vma and size to */
1379 /* align against */
2fa0b342
DHW
1380
1381
1418c83b
SC
1382 after = ALIGN(os->bfd_section->vma +
1383 os->bfd_section->size,
1384 os->block_value) ;
2fa0b342
DHW
1385
1386
1418c83b
SC
1387 os->bfd_section->size = after - os->bfd_section->vma;
1388 dot = os->bfd_section->vma + os->bfd_section->size;
1389 os->processed = true;
2fa0b342 1390
1418c83b
SC
1391 /* Replace into region ? */
1392 if (os->addr_tree == (etree_type *)NULL
1393 && os->region !=(lang_memory_region_type*)NULL ) {
1394 os->region->current = dot;
1395 }
1396 }
2fa0b342 1397
1418c83b 1398 break;
81016051
SC
1399 case lang_constructors_statement_enum:
1400 dot = lang_size_sections(constructor_list.head,
1401 output_section_statement,
1402 &s->wild_statement.children.head,
1403 fill,
1404 dot);
1405 break;
1406
1418c83b
SC
1407 case lang_data_statement_enum:
1408 {
c611e285 1409 unsigned int size = 0;
65c552e3 1410 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1418c83b
SC
1411 s->data_statement.output_section =
1412 output_section_statement->bfd_section;
2fa0b342 1413
1418c83b
SC
1414 switch (s->data_statement.type) {
1415 case LONG:
1416 size = LONG_SIZE;
1417 break;
1418 case SHORT:
1419 size = SHORT_SIZE;
1420 break;
1421 case BYTE:
1422 size = BYTE_SIZE;
1423 break;
2fa0b342 1424
1418c83b
SC
1425 }
1426 dot += size;
1427 output_section_statement->bfd_section->size += size;
1428 }
1429 break;
2fa0b342 1430
1418c83b 1431 case lang_wild_statement_enum:
2fa0b342 1432
1418c83b 1433 dot = lang_size_sections(s->wild_statement.children.head,
2fa0b342
DHW
1434 output_section_statement,
1435 &s->wild_statement.children.head,
1436
1437 fill, dot);
1438
1418c83b 1439 break;
2fa0b342 1440
1418c83b
SC
1441 case lang_object_symbols_statement_enum:
1442 create_object_symbols = output_section_statement;
1443 break;
1444 case lang_output_statement_enum:
1445 case lang_target_statement_enum:
1446 break;
1447 case lang_input_section_enum:
1448 dot = size_input_section(prev,
2fa0b342
DHW
1449 output_section_statement,
1450 output_section_statement->fill, dot);
1418c83b
SC
1451 break;
1452 case lang_input_statement_enum:
1453 break;
1454 case lang_fill_statement_enum:
1455 fill = s->fill_statement.fill;
1456 break;
1457 case lang_assignment_statement_enum:
2fa0b342 1458 {
1418c83b
SC
1459 bfd_vma newdot = dot;
1460 exp_fold_tree(s->assignment_statement.exp,
1461 output_section_statement,
1462 lang_allocating_phase_enum,
1463 dot,
1464 &newdot);
1465
1466 if (newdot != dot)
1467 /* We've been moved ! so insert a pad */
1468 {
1469 lang_statement_union_type *new =
1470 (lang_statement_union_type *)
3f8d46e7 1471 ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type)));
1418c83b
SC
1472 /* Link into existing chain */
1473 new->header.next = *prev;
1474 *prev = new;
1475 new->header.type = lang_padding_statement_enum;
1476 new->padding_statement.output_section =
1477 output_section_statement->bfd_section;
1478 new->padding_statement.output_offset =
1479 dot - output_section_statement->bfd_section->vma;
1480 new->padding_statement.fill = fill;
1481 new->padding_statement.size = newdot - dot;
1482 output_section_statement->bfd_section->size +=
1483 new->padding_statement.size;
1484 dot = newdot;
1485 }
2fa0b342 1486 }
2fa0b342 1487
1418c83b
SC
1488 break;
1489 case lang_padding_statement_enum:
1490 FAIL();
1491 break;
1492 default:
1493 FAIL();
1494 break;
1495 case lang_address_statement_enum:
1496 break;
1497 }
1498 prev = &s->header.next;
2fa0b342 1499 }
2fa0b342 1500 return dot;
c611e285
SC
1501}
1502#else
1503/* Sizing happens in two passes, first pass we allocate worst case
1504 stuff. The second pass (if relaxing), we use what we learnt to
1505 change the size of some relocs from worst case to better
1506 */
1507static boolean had_relax;
1508
1509static bfd_vma
1510DEFUN(lang_size_sections,(s, output_section_statement, prev, fill,
1511 dot, relax),
1512 lang_statement_union_type *s AND
1513 lang_output_section_statement_type * output_section_statement AND
1514 lang_statement_union_type **prev AND
1515 unsigned short fill AND
1516 bfd_vma dot AND
1517 boolean relax)
1518{
1519 /* Size up the sections from their constituent parts */
1520 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1521 {
1522 switch (s->header.type) {
1523
1524 case lang_output_section_statement_enum:
1525 {
1526 bfd_vma after;
1527 lang_output_section_statement_type *os =
1528 &(s->output_section_statement);
1529 /* The start of a section */
1530
1531 if (os->addr_tree == (etree_type *)NULL) {
1532 /* No address specified for this section, get one
1533 from the region specification
1534 */
1535 if (os->region == (lang_memory_region_type *)NULL) {
1536 os->region = lang_memory_region_lookup("*default*");
1537 }
1538 dot = os->region->current;
1539 }
1540 else {
1541 etree_value_type r ;
1542 r = exp_fold_tree(os->addr_tree,
1543 (lang_output_section_statement_type *)NULL,
1544 lang_allocating_phase_enum,
1545 dot, &dot);
1546 if (r.valid == false) {
1547 einfo("%F%S: non constant address expression for section %s\n",
1548 os->name);
1549 }
1550 dot = r.value;
1551 }
1552 /* The section starts here */
1553 /* First, align to what the section needs */
1554
1555 dot = align_power(dot, os->bfd_section->alignment_power);
1556 os->bfd_section->vma = dot;
1557 os->bfd_section->output_offset = 0;
1558
1559 (void) lang_size_sections(os->children.head, os, &os->children.head,
1560 os->fill, dot, relax);
1561 /* Ignore the size of the input sections, use the vma and size to */
1562 /* align against */
1563
1564
1565 after = ALIGN(os->bfd_section->vma +
1566 os->bfd_section->_raw_size,
1567 os->block_value) ;
1568
1569
1570 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1571 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1572 os->processed = true;
1573
1574 /* Replace into region ? */
1575 if (os->addr_tree == (etree_type *)NULL
1576 && os->region !=(lang_memory_region_type*)NULL ) {
1577 os->region->current = dot;
1578 }
1579 }
1580
1581 break;
1582 case lang_constructors_statement_enum:
1583 dot = lang_size_sections(constructor_list.head,
1584 output_section_statement,
1585 &s->wild_statement.children.head,
1586 fill,
1587 dot, relax);
1588 break;
1589
1590 case lang_data_statement_enum:
1591 {
1592 unsigned int size = 0;
1593 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1594 s->data_statement.output_section =
1595 output_section_statement->bfd_section;
1596
1597 switch (s->data_statement.type) {
1598 case LONG:
1599 size = LONG_SIZE;
1600 break;
1601 case SHORT:
1602 size = SHORT_SIZE;
1603 break;
1604 case BYTE:
1605 size = BYTE_SIZE;
1606 break;
1607
1608 }
1609 dot += size;
1610 output_section_statement->bfd_section->_raw_size += size;
1611 }
1612 break;
1613
1614 case lang_wild_statement_enum:
1615
1616 dot = lang_size_sections(s->wild_statement.children.head,
1617 output_section_statement,
1618 &s->wild_statement.children.head,
1619
1620 fill, dot, relax);
1621
1622 break;
1623
1624 case lang_object_symbols_statement_enum:
1625 create_object_symbols = output_section_statement;
1626 break;
1627 case lang_output_statement_enum:
1628 case lang_target_statement_enum:
1629 break;
1630 case lang_input_section_enum:
1631if (relax)
1632{
1633 relaxing = true;
1634
1635
1636had_relax |= relax_section(prev);
1637 relaxing = false;
1638
2fa0b342
DHW
1639}
1640
c611e285
SC
1641 dot = size_input_section(prev,
1642 output_section_statement,
1643 output_section_statement->fill, dot);
1644 break;
1645 case lang_input_statement_enum:
1646 break;
1647 case lang_fill_statement_enum:
1648 fill = s->fill_statement.fill;
1649 break;
1650 case lang_assignment_statement_enum:
1651 {
1652 bfd_vma newdot = dot;
1653 exp_fold_tree(s->assignment_statement.exp,
1654 output_section_statement,
1655 lang_allocating_phase_enum,
1656 dot,
1657 &newdot);
1658
1659 if (newdot != dot)
1660 /* We've been moved ! so insert a pad */
1661 {
1662 lang_statement_union_type *new =
1663 (lang_statement_union_type *)
1664 ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type)));
1665 /* Link into existing chain */
1666 new->header.next = *prev;
1667 *prev = new;
1668 new->header.type = lang_padding_statement_enum;
1669 new->padding_statement.output_section =
1670 output_section_statement->bfd_section;
1671 new->padding_statement.output_offset =
1672 dot - output_section_statement->bfd_section->vma;
1673 new->padding_statement.fill = fill;
1674 new->padding_statement.size = newdot - dot;
1675 output_section_statement->bfd_section->_raw_size +=
1676 new->padding_statement.size;
1677 dot = newdot;
1678 }
1679 }
1680
1681 break;
1682 default:
1683 FAIL();
1684 break;
1685/* This can only get here when relaxing is turned on */
1686 case lang_padding_statement_enum:
1687
1688 case lang_address_statement_enum:
1689 break;
1690 }
1691 prev = &s->header.next;
1692 }
1693 return dot;
1694}
1695#endif
2fa0b342
DHW
1696
1697static bfd_vma
1418c83b
SC
1698DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot),
1699 lang_statement_union_type *s AND
1700 lang_output_section_statement_type * output_section_statement AND
1701 unsigned short fill AND
1702 bfd_vma dot)
2fa0b342
DHW
1703{
1704
1705 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1706 {
1707 switch (s->header.type) {
81016051
SC
1708 case lang_constructors_statement_enum:
1709 dot = lang_do_assignments(constructor_list.head,
1710 output_section_statement,
1711 fill,
1712 dot);
1713 break;
1714
2fa0b342
DHW
1715 case lang_output_section_statement_enum:
1716 {
1717 lang_output_section_statement_type *os =
1718 &(s->output_section_statement);
1719 dot = os->bfd_section->vma;
1720 (void) lang_do_assignments(os->children.head, os, os->fill, dot);
c611e285 1721 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2fa0b342
DHW
1722 }
1723 break;
1724 case lang_wild_statement_enum:
1725
1726 dot = lang_do_assignments(s->wild_statement.children.head,
1727 output_section_statement,
1728 fill, dot);
1729
1730 break;
1731
1732 case lang_object_symbols_statement_enum:
1733 case lang_output_statement_enum:
1734 case lang_target_statement_enum:
1418c83b 1735#if 0
2fa0b342 1736 case lang_common_statement_enum:
1418c83b 1737#endif
2fa0b342
DHW
1738 break;
1739 case lang_data_statement_enum:
1740 {
1741 etree_value_type value ;
1742 value = exp_fold_tree(s->data_statement.exp,
1743 0, lang_final_phase_enum, dot, &dot);
1744 s->data_statement.value = value.value;
c611e285 1745 if (value.valid == false) einfo("%F%P: Invalid data statement\n");
2fa0b342
DHW
1746 }
1747 switch (s->data_statement.type) {
1748 case LONG:
1749 dot += LONG_SIZE;
1750 break;
1751 case SHORT:
1752 dot += SHORT_SIZE;
1753 break;
1754 case BYTE:
1755 dot += BYTE_SIZE;
1756 break;
1757 }
1758 break;
1759 case lang_input_section_enum:
1760 {
1761 asection *in = s->input_section.section;
c611e285 1762 dot += bfd_get_section_size_before_reloc(in);
2fa0b342
DHW
1763 }
1764 break;
1765
1766 case lang_input_statement_enum:
1767 break;
1768 case lang_fill_statement_enum:
1769 fill = s->fill_statement.fill;
1770 break;
1771 case lang_assignment_statement_enum:
1772 {
1773 exp_fold_tree(s->assignment_statement.exp,
1774 output_section_statement,
1775 lang_final_phase_enum,
1776 dot,
1777 &dot);
1778 }
1779
1780 break;
1781 case lang_padding_statement_enum:
1782 dot += s->padding_statement.size;
1783 break;
1784 default:
1785 FAIL();
1786 break;
1787 case lang_address_statement_enum:
1788 break;
1789 }
1790
1791 }
1792 return dot;
1793}
1794
1795
1796
1418c83b
SC
1797static void
1798DEFUN_VOID(lang_relocate_globals)
2fa0b342
DHW
1799{
1800
1801 /*
1418c83b
SC
1802 Each ldsym_type maintains a chain of pointers to asymbols which
1803 references the definition. Replace each pointer to the referenence
1804 with a pointer to only one place, preferably the definition. If
1805 the defintion isn't available then the common symbol, and if
1806 there isn't one of them then choose one reference.
1807 */
2fa0b342
DHW
1808
1809 FOR_EACH_LDSYM(lgs) {
1810 asymbol *it;
1811 if (lgs->sdefs_chain) {
1812 it = *(lgs->sdefs_chain);
1813 }
1814 else if (lgs->scoms_chain != (asymbol **)NULL) {
1815 it = *(lgs->scoms_chain);
1816 }
1817 else if (lgs->srefs_chain != (asymbol **)NULL) {
1818 it = *(lgs->srefs_chain);
1819 }
1820 else {
1418c83b
SC
1821 /* This can happen when the command line asked for a symbol to
1822 be -u */
1823 it = (asymbol *)NULL;
2fa0b342
DHW
1824 }
1825 if (it != (asymbol *)NULL)
1418c83b 1826 {
81016051
SC
1827 asymbol **ptr = lgs->srefs_chain;;
1828 if (lgs->flags & SYM_WARNING)
1829 {
1830 produce_warnings(lgs, it);
1831 }
2fa0b342 1832
1418c83b
SC
1833 while (ptr != (asymbol **)NULL) {
1834 asymbol *ref = *ptr;
1835 *ptr = it;
1836 ptr = (asymbol **)(ref->udata);
1837 }
2fa0b342 1838 }
2fa0b342
DHW
1839 }
1840}
1841
1842
1843
2fa0b342 1844static void
1418c83b 1845DEFUN_VOID(lang_finish)
2fa0b342
DHW
1846{
1847 ldsym_type *lgs;
1848
1849 if (entry_symbol == (char *)NULL) {
1850 /* No entry has been specified, look for start */
1851 entry_symbol = "start";
1852 }
1853 lgs = ldsym_get_soft(entry_symbol);
1854 if (lgs && lgs->sdefs_chain) {
1855 asymbol *sy = *(lgs->sdefs_chain);
1856 /* We can set the entry address*/
1857 bfd_set_start_address(output_bfd,
1858 outside_symbol_address(sy));
1859
1860 }
1861 else {
1862 /* Can't find anything reasonable,
1863 use the first address in the text section
1864 */
1865 asection *ts = bfd_get_section_by_name(output_bfd, ".text");
1866 if (ts) {
1867 bfd_set_start_address(output_bfd, ts->vma);
1868 }
1869 }
1870}
1871
1872/* By now we know the target architecture, and we may have an */
1873/* ldfile_output_machine_name */
1874static void
1418c83b 1875DEFUN_VOID(lang_check)
2fa0b342
DHW
1876{
1877 lang_statement_union_type *file;
7bc4a0d7 1878
3f8d46e7
JG
1879 bfd * input_bfd;
1880 unsigned long input_machine;
7bc4a0d7 1881 enum bfd_architecture input_architecture;
2fa0b342 1882
f177a611 1883 CONST bfd_arch_info_type *compatible;
7bc4a0d7 1884
2fa0b342
DHW
1885 for (file = file_chain.head;
1886 file != (lang_statement_union_type *)NULL;
1887 file=file->input_statement.next)
1418c83b 1888 {
06850bea
SC
1889 unsigned long ldfile_new_output_machine = 0;
1890 enum bfd_architecture ldfile_new_output_architecture = bfd_arch_unknown;
7bc4a0d7 1891
3f8d46e7 1892 input_bfd = file->input_statement.the_bfd;
7bc4a0d7 1893
f177a611
JG
1894 input_machine = bfd_get_mach(input_bfd);
1895 input_architecture = bfd_get_arch(input_bfd);
7bc4a0d7 1896
3f8d46e7 1897
f177a611
JG
1898 /* Inspect the architecture and ensure we're linking like with
1899 like */
7bc4a0d7 1900
f177a611
JG
1901 compatible=bfd_arch_get_compatible(input_bfd,
1902 output_bfd);
7bc4a0d7 1903
f177a611
JG
1904 if (compatible)
1905 {
1906 ldfile_output_machine = compatible->mach;
1907 ldfile_output_architecture = compatible->arch;
1908 }
1909 else
1910 {
7bc4a0d7 1911
f177a611
JG
1912 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1913 bfd_printable_name(input_bfd), input_bfd,
1914 bfd_printable_name(output_bfd));
7bc4a0d7
SC
1915
1916 bfd_set_arch_mach(output_bfd,
1917 ldfile_new_output_architecture,
1918 ldfile_new_output_machine);
1919 }
7bc4a0d7 1920
f177a611 1921 }
2fa0b342
DHW
1922}
1923
1924
1925/*
1926 * run through all the global common symbols and tie them
b6316534
SC
1927 * to the output section requested.
1928 *
1929 As an experiment we do this 4 times, once for all the byte sizes,
1930 then all the two bytes, all the four bytes and then everything else
1931 */
2fa0b342
DHW
1932
1933static void
1418c83b 1934DEFUN_VOID(lang_common)
2fa0b342
DHW
1935{
1936 ldsym_type *lgs;
b6316534 1937 size_t power;
2fa0b342
DHW
1938 if (config.relocateable_output == false ||
1939 command_line.force_common_definition== true) {
b6316534
SC
1940 for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<=1) {
1941 for (lgs = symbol_head;
1942 lgs != (ldsym_type *)NULL;
1943 lgs=lgs->next)
1418c83b
SC
1944 {
1945 asymbol *com ;
1946 unsigned int power_of_two;
1947 size_t size;
1948 size_t align;
1949 if (lgs->scoms_chain != (asymbol **)NULL) {
1950 com = *(lgs->scoms_chain);
1951 size = com->value;
1952 switch (size) {
1953 case 0:
1954 case 1:
1955 align = 1;
1956 power_of_two = 0;
1957 break;
1958 case 2:
1959 power_of_two = 1;
1960 align = 2;
1961 break;
1962 case 3:
1963 case 4:
1964 power_of_two =2;
1965 align = 4;
1966 break;
1967 case 5:
1968 case 6:
1969 case 7:
1970 case 8:
1971 power_of_two = 3;
2fa0b342 1972 align = 8;
1418c83b
SC
1973 break;
1974 default:
1975 power_of_two = 4;
1976 align = 16;
1977 break;
2fa0b342 1978 }
b6316534
SC
1979 if (config.sort_common == false || align == power) {
1980 /* Change from a common symbol into a definition of
1981 a symbol */
1982 lgs->sdefs_chain = lgs->scoms_chain;
1983 lgs->scoms_chain = (asymbol **)NULL;
1984 commons_pending--;
1985 /* Point to the correct common section */
1986 com->section =
1987 ((lang_input_statement_type *)
1988 (com->the_bfd->usrdata))->common_section;
1989 /* Fix the size of the common section */
c611e285
SC
1990
1991 com->section->_raw_size =
1992 ALIGN(com->section->_raw_size, align);
b6316534
SC
1993
1994 /* Remember if this is the biggest alignment ever seen */
1995 if (power_of_two > com->section->alignment_power) {
1996 com->section->alignment_power = power_of_two;
1997 }
2fa0b342 1998
b6316534
SC
1999 /* Symbol stops being common and starts being global, but
2000 we remember that it was common once. */
2fa0b342 2001
b6316534 2002 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
c611e285 2003 com->value = com->section->_raw_size;
1418c83b 2004
b6316534 2005 if (write_map)
1418c83b 2006 {
6fd50a20 2007 fprintf (config.map_file, "Allocating common %s: %x at %x %s\n",
1418c83b
SC
2008 lgs->name,
2009 (unsigned) size,
b6316534
SC
2010 (unsigned) com->value,
2011 com->the_bfd->filename);
1418c83b 2012 }
097879bc 2013
c611e285 2014 com->section->_raw_size += size;
1418c83b 2015
b6316534 2016 }
1418c83b 2017 }
b6316534 2018
2fa0b342 2019 }
b6316534 2020 }
2fa0b342 2021 }
1418c83b
SC
2022
2023
2fa0b342
DHW
2024}
2025
2026/*
2027run through the input files and ensure that every input
2028section has somewhere to go. If one is found without
2029a destination then create an input request and place it
2030into the statement tree.
2031*/
2032
1418c83b
SC
2033static void
2034DEFUN_VOID(lang_place_orphans)
2fa0b342
DHW
2035{
2036 lang_input_statement_type *file;
2037 for (file = (lang_input_statement_type*)file_chain.head;
2038 file != (lang_input_statement_type*)NULL;
2039 file = (lang_input_statement_type*)file->next) {
2040 asection *s;
2041 for (s = file->the_bfd->sections;
2042 s != (asection *)NULL;
2043 s = s->next) {
2044 if ( s->output_section == (asection *)NULL) {
2045 /* This section of the file is not attatched, root
2046 around for a sensible place for it to go */
2047
2048 if (file->common_section == s) {
2049 /* This is a lonely common section which must
2050 have come from an archive. We attatch to the
2051 section with the wildcard */
1418c83b
SC
2052 if (config.relocateable_output != true
2053 && command_line.force_common_definition == false) {
2054 if (default_common_section ==
2055 (lang_output_section_statement_type *)NULL) {
2056 info("%P: No [COMMON] command, defaulting to .bss\n");
2057
2058 default_common_section =
2059 lang_output_section_statement_lookup(".bss");
2060
2061 }
2062 wild_doit(&default_common_section->children, s,
2063 default_common_section, file);
2064 }
2fa0b342
DHW
2065 }
2066 else {
2067 lang_output_section_statement_type *os =
2068 lang_output_section_statement_lookup(s->name);
2069
2070 wild_doit(&os->children, s, os, file);
2071 }
2072 }
2073 }
2fa0b342
DHW
2074 }
2075}
2076
2077
2fa0b342 2078void
1418c83b 2079DEFUN(lang_set_flags,(ptr, flags),
7bc4a0d7 2080 int *ptr AND
1418c83b 2081 CONST char *flags)
2fa0b342
DHW
2082{
2083 boolean state = true;
7bc4a0d7 2084*ptr= 0;
2fa0b342 2085 while (*flags)
1418c83b
SC
2086 {
2087 if (*flags == '!') {
2088 state = false;
2089 flags++;
2090 }
2091 else state = true;
2092 switch (*flags) {
2093 case 'R':
7bc4a0d7 2094/* ptr->flag_read = state; */
1418c83b
SC
2095 break;
2096 case 'W':
7bc4a0d7 2097/* ptr->flag_write = state; */
1418c83b
SC
2098 break;
2099 case 'X':
7bc4a0d7 2100/* ptr->flag_executable= state;*/
1418c83b
SC
2101 break;
2102 case 'L':
b6316534 2103 case 'I':
7bc4a0d7 2104/* ptr->flag_loadable= state;*/
1418c83b
SC
2105 break;
2106 default:
c611e285 2107 einfo("%P%F illegal syntax in flags\n");
1418c83b
SC
2108 break;
2109 }
2fa0b342
DHW
2110 flags++;
2111 }
2fa0b342
DHW
2112}
2113
2114
2115
2116void
1418c83b
SC
2117DEFUN(lang_for_each_file,(func),
2118 PROTO(void, (*func),(lang_input_statement_type *)))
2fa0b342
DHW
2119{
2120 lang_input_statement_type *f;
2121 for (f = (lang_input_statement_type *)file_chain.head;
2122 f != (lang_input_statement_type *)NULL;
2123 f = (lang_input_statement_type *)f->next)
1418c83b
SC
2124 {
2125 func(f);
2126 }
2fa0b342
DHW
2127}
2128
2129
2130void
1418c83b
SC
2131DEFUN(lang_for_each_input_section, (func),
2132 PROTO(void ,(*func),(bfd *ab, asection*as)))
2fa0b342
DHW
2133{
2134 lang_input_statement_type *f;
2135 for (f = (lang_input_statement_type *)file_chain.head;
2136 f != (lang_input_statement_type *)NULL;
2137 f = (lang_input_statement_type *)f->next)
2138 {
2139 asection *s;
2140 for (s = f->the_bfd->sections;
2141 s != (asection *)NULL;
2142 s = s->next) {
2143 func(f->the_bfd, s);
2144 }
2145 }
2146}
2147
2148
2149
2150void
1418c83b
SC
2151DEFUN(ldlang_add_file,(entry),
2152 lang_input_statement_type *entry)
2fa0b342 2153{
1418c83b 2154
2fa0b342
DHW
2155 lang_statement_append(&file_chain,
2156 (lang_statement_union_type *)entry,
2157 &entry->next);
2158}
2159
2160
2161
2162void
1418c83b
SC
2163DEFUN(lang_add_output,(name),
2164 CONST char *name)
2fa0b342
DHW
2165{
2166 lang_output_statement_type *new = new_stat(lang_output_statement,
2167 stat_ptr);
2168 new->name = name;
2169 had_output_filename = true;
2170}
2171
2172
2173static lang_output_section_statement_type *current_section;
2174
2175void
1418c83b
SC
2176DEFUN(lang_enter_output_section_statement,
2177 (output_section_statement_name,
2178 address_exp,
7bc4a0d7 2179 flags,
1418c83b
SC
2180 block_value),
2181 char *output_section_statement_name AND
2182 etree_type *address_exp AND
7bc4a0d7 2183 int flags AND
1418c83b 2184 bfd_vma block_value)
2fa0b342
DHW
2185{
2186 lang_output_section_statement_type *os;
2187 current_section =
2188 os =
2189 lang_output_section_statement_lookup(output_section_statement_name);
2190
81016051
SC
2191
2192
2fa0b342
DHW
2193 /* Add this statement to tree */
2194 /* add_statement(lang_output_section_statement_enum,
2195 output_section_statement);*/
2196 /* Make next things chain into subchain of this */
2197
2198 if (os->addr_tree ==
2199 (etree_type *)NULL) {
2200 os->addr_tree =
2201 address_exp;
2202 }
7bc4a0d7 2203 os->flags = flags;
2fa0b342
DHW
2204 os->block_value = block_value;
2205 stat_ptr = & os->children;
2206
2207}
2208
2209
2210void
1418c83b 2211DEFUN_VOID(lang_final)
2fa0b342
DHW
2212{
2213 if (had_output_filename == false) {
e89432c7
SC
2214 extern CONST char *output_filename;
2215 lang_add_output(output_filename);
2fa0b342 2216 }
2fa0b342
DHW
2217}
2218
2219
2220
c611e285
SC
2221/* Reset the current counters in the regions */
2222static void
2223 DEFUN_VOID(reset_memory_regions)
2224{
2225 lang_memory_region_type *p = lang_memory_region_list;
2226 for (p = lang_memory_region_list;
2227 p != ( lang_memory_region_type *)NULL;
2228 p = p->next)
2229 {
2230 p->current = p->origin;
2231 }
2232}
2fa0b342
DHW
2233
2234
1418c83b
SC
2235asymbol *
2236DEFUN(create_symbol,(name, flags, section),
2237 CONST char *name AND
2238 flagword flags AND
2239 asection *section)
2fa0b342
DHW
2240{
2241 extern lang_input_statement_type *script_file;
3f8d46e7 2242 asymbol **def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **)));
2fa0b342
DHW
2243 /* Add this definition to script file */
2244 asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
1418c83b 2245 def->name = buystring(name);
2fa0b342
DHW
2246 def->udata = 0;
2247 def->flags = flags;
2248 def->section = section;
2249
2250 *def_ptr = def;
2251 Q_enter_global_ref(def_ptr);
2252 return def;
2253}
2254
81016051
SC
2255
2256
2fa0b342 2257void
1418c83b
SC
2258DEFUN_VOID(lang_process)
2259{
2260 if (had_script == false) {
c611e285
SC
2261 parse_line(ldemul_get_script());
2262 }
1418c83b
SC
2263 lang_reasonable_defaults();
2264 current_target = default_target;
2265
2266 lang_for_each_statement(ldlang_open_output); /* Open the output file */
2267 /* For each output section statement, create a section in the output
2268 file */
2269 lang_create_output_section_statements();
2270
2271 /* Create a dummy bfd for the script */
2272 lang_init_script_file();
2273
2274 /* Add to the hash table all undefineds on the command line */
2275 lang_place_undefineds();
2276
2277 /* Create a bfd for each input file */
2278 current_target = default_target;
2279 lang_for_each_statement(open_input_bfds);
2280
a0ad3035 2281 common_section.userdata = (PTR)&common_section_userdata;
1418c83b 2282
81016051 2283
1418c83b
SC
2284 /* Run through the contours of the script and attatch input sections
2285 to the correct output sections
2286 */
81016051 2287 find_constructors();
1418c83b
SC
2288 map_input_to_output_sections(statement_list.head, (char *)NULL,
2289 ( lang_output_section_statement_type *)NULL);
2290
81016051 2291
1418c83b
SC
2292 /* Find any sections not attatched explicitly and handle them */
2293 lang_place_orphans();
2294
2295 /* Size up the common data */
2296 lang_common();
2297
2298 ldemul_before_allocation();
2299
c611e285 2300
1418c83b
SC
2301 /* Size up the sections */
2302 lang_size_sections(statement_list.head,
2303 (lang_output_section_statement_type *)NULL,
c611e285
SC
2304 &(statement_list.head), 0, (bfd_vma)0, false);
2305
2306
2307 /* Move the global symbols around */
2308 lang_relocate_globals();
2309
2310 /* Now run around and relax if we can */
2311 if (command_line.relax)
2312 {
2313 reset_memory_regions();
2314
2315 had_relax = true;
2316 while (had_relax)
2317 {
2318
2319 had_relax = false;
2320
2321 lang_size_sections(statement_list.head,
2322 (lang_output_section_statement_type *)NULL,
2323 &(statement_list.head), 0, (bfd_vma)0, true);
2324 /* FIXME. Until the code in relax is fixed so that it only reads in
2325 stuff once, we cant iterate since there is no way for the linker to
2326 know what has been patched and what hasn't */
2327 break;
2328
2329 }
2330
2331
2332
2333
2334 }
2335
1418c83b
SC
2336
2337 /* See if anything special should be done now we know how big
2338 everything is */
2339 ldemul_after_allocation();
2340
2341 /* Do all the assignments, now that we know the final restingplaces
2342 of all the symbols */
2343
2344 lang_do_assignments(statement_list.head,
2345 (lang_output_section_statement_type *)NULL,
2346 0, (bfd_vma)0);
2347
c611e285 2348
1418c83b
SC
2349 /* Make sure that we're not mixing architectures */
2350
2351 lang_check();
2352
1418c83b
SC
2353 /* Final stuffs */
2354 lang_finish();
2fa0b342
DHW
2355}
2356
2357
2358/* EXPORTED TO YACC */
1418c83b 2359
2fa0b342 2360void
1418c83b
SC
2361DEFUN(lang_add_wild,(section_name, filename),
2362 CONST char *CONST section_name AND
2363 CONST char *CONST filename)
2364{
2365 lang_wild_statement_type *new = new_stat(lang_wild_statement,
2366 stat_ptr);
2367
2368 if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0)
2369 {
2370 placed_commons = true;
2371 }
2372 if (filename != (char *)NULL) {
2373 lang_has_input_file = true;
2374 }
2375 new->section_name = section_name;
2376 new->filename = filename;
2377 lang_list_init(&new->children);
2378}
2379void
2380DEFUN(lang_section_start,(name, address),
2381 CONST char *name AND
2382 etree_type *address)
2fa0b342
DHW
2383{
2384 lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr);
2385 ad->section_name = name;
2386 ad->address = address;
2387}
1418c83b
SC
2388
2389void
2390DEFUN(lang_add_entry,(name),
2391 CONST char *name)
2fa0b342
DHW
2392{
2393 entry_symbol = name;
2394}
2395
2396void
1418c83b
SC
2397DEFUN(lang_add_target,(name),
2398 CONST char *name)
2fa0b342
DHW
2399{
2400 lang_target_statement_type *new = new_stat(lang_target_statement,
2401 stat_ptr);
2402 new->target = name;
2403
2404}
2fa0b342 2405
1418c83b
SC
2406
2407
2fa0b342
DHW
2408
2409void
1418c83b
SC
2410DEFUN(lang_add_map,(name),
2411 CONST char *name)
2fa0b342
DHW
2412{
2413 while (*name) {
2414 switch (*name) {
2415 case 'F':
2416 map_option_f = true;
2417 break;
2418 }
2419 name++;
2420 }
2421}
2422
1418c83b
SC
2423void
2424DEFUN(lang_add_fill,(exp),
2425 int exp)
2fa0b342
DHW
2426{
2427 lang_fill_statement_type *new = new_stat(lang_fill_statement,
2428 stat_ptr);
2429 new->fill = exp;
2430}
2431
1418c83b
SC
2432void
2433DEFUN(lang_add_data,(type, exp),
2434 int type AND
2435 union etree_union *exp)
2fa0b342
DHW
2436{
2437
2438 lang_data_statement_type *new = new_stat(lang_data_statement,
2439 stat_ptr);
2440 new->exp = exp;
2441 new->type = type;
2442
2443}
2444void
1418c83b
SC
2445DEFUN(lang_add_assignment,(exp),
2446 etree_type *exp)
2fa0b342
DHW
2447{
2448 lang_assignment_statement_type *new = new_stat(lang_assignment_statement,
2449 stat_ptr);
2450 new->exp = exp;
2451}
2452
2453void
1418c83b
SC
2454DEFUN(lang_add_attribute,(attribute),
2455 enum statement_enum attribute)
2fa0b342
DHW
2456{
2457 new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr);
2458}
2459
2460
2461
2462void
1418c83b
SC
2463DEFUN(lang_startup,(name),
2464 CONST char *name)
2fa0b342
DHW
2465{
2466 if (startup_file != (char *)NULL) {
c611e285 2467 einfo("%P%FMultiple STARTUP files\n");
2fa0b342
DHW
2468 }
2469 first_file->filename = name;
2470 first_file->local_sym_name = name;
2471
2472 startup_file= name;
2473}
2474void
1418c83b
SC
2475DEFUN(lang_float,(maybe),
2476 boolean maybe)
2fa0b342
DHW
2477{
2478 lang_float_flag = maybe;
2479}
2480
2481void
1418c83b
SC
2482DEFUN(lang_leave_output_section_statement,(fill, memspec),
2483 bfd_vma fill AND
2484 CONST char *memspec)
2fa0b342
DHW
2485{
2486 current_section->fill = fill;
2487 current_section->region = lang_memory_region_lookup(memspec);
2488 stat_ptr = &statement_list;
81016051
SC
2489
2490 /* We remember if we are closing a .data section, since we use it to
2491 store constructors in */
2492 if (strcmp(current_section->name, ".data") ==0) {
2493 end_of_data_section_statement_list = statement_list;
2494
2495 }
2fa0b342 2496}
9f32f7c2
SC
2497/*
2498 Create an absolute symbol with the given name with the value of the
2499 address of first byte of the section named.
2fa0b342 2500
9f32f7c2
SC
2501 If the symbol already exists, then do nothing.
2502*/
8cb5eb31 2503void
1418c83b
SC
2504DEFUN(lang_abs_symbol_at_beginning_of,(section, name),
2505 CONST char *section AND
2506 CONST char *name)
8cb5eb31 2507{
c660714f 2508 if (ldsym_undefined(name)) {
9f32f7c2
SC
2509 asection *s = bfd_get_section_by_name(output_bfd, section);
2510 asymbol *def = create_symbol(name,
c611e285
SC
2511 BSF_GLOBAL | BSF_EXPORT ,
2512 &bfd_abs_section);
2513
2514
9f32f7c2
SC
2515 if (s != (asection *)NULL) {
2516 def->value = s->vma;
2517 }
2518 else {
2519 def->value = 0;
2520 }
8cb5eb31
SC
2521 }
2522}
2523
9f32f7c2
SC
2524/*
2525 Create an absolute symbol with the given name with the value of the
2526 address of the first byte after the end of the section named.
2527
2528 If the symbol already exists, then do nothing.
2529*/
2fa0b342 2530void
1418c83b
SC
2531DEFUN(lang_abs_symbol_at_end_of,(section, name),
2532 CONST char *section AND
2533 CONST char *name)
2fa0b342 2534{
c660714f 2535 if (ldsym_undefined(name)){
9f32f7c2
SC
2536 asection *s = bfd_get_section_by_name(output_bfd, section);
2537 /* Add a symbol called _end */
2538 asymbol *def = create_symbol(name,
c611e285
SC
2539 BSF_GLOBAL | BSF_EXPORT ,
2540 &bfd_abs_section);
2541
9f32f7c2 2542 if (s != (asection *)NULL) {
c611e285 2543 def->value = s->vma + s->_raw_size;
9f32f7c2
SC
2544 }
2545 else {
2546 def->value = 0;
2547 }
2fa0b342
DHW
2548 }
2549}
2550
2551void
1418c83b
SC
2552DEFUN(lang_statement_append,(list, element, field),
2553 lang_statement_list_type *list AND
2554 lang_statement_union_type *element AND
2555 lang_statement_union_type **field)
2fa0b342
DHW
2556{
2557 *(list->tail) = element;
2558 list->tail = field;
2559}
2560
097879bc
SC
2561/* Set the output format type */
2562void
2563DEFUN(lang_add_output_format,(format),
2564CONST char *format)
2565{
2566 output_target = format;
2567}
a37cc0c0 2568
This page took 0.389552 seconds and 4 git commands to generate.