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