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