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