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