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