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