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