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