renamed hp9000.h to hp300.h
[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;
22a78f0d 1849 int warn = 1;
075d7359 1850 if (entry_symbol == (char *) NULL)
22a78f0d
SC
1851 {
1852 /* No entry has been specified, look for start, but don't warn */
1853 entry_symbol = "start";
1854 warn =0;
1855 }
075d7359
SC
1856 lgs = ldsym_get_soft (entry_symbol);
1857 if (lgs && lgs->sdefs_chain)
22a78f0d
SC
1858 {
1859 asymbol *sy = *(lgs->sdefs_chain);
075d7359 1860
22a78f0d
SC
1861 /* We can set the entry address*/
1862 bfd_set_start_address (output_bfd,
1863 outside_symbol_address (sy));
2fa0b342 1864
22a78f0d 1865 }
075d7359 1866 else
22a78f0d
SC
1867 {
1868 /* Can't find anything reasonable,
1869 use the first address in the text section
1870 */
1871 asection *ts = bfd_get_section_by_name (output_bfd, ".text");
1872 if (ts)
075d7359 1873 {
22a78f0d
SC
1874 if (warn)
1875 einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
1876 entry_symbol, ts->vma);
075d7359 1877
22a78f0d 1878 bfd_set_start_address (output_bfd, ts->vma);
075d7359 1879 }
22a78f0d
SC
1880 else
1881 {
1882 if (warn)
1883 einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
1884 entry_symbol);
1885 }
1886 }
2fa0b342
DHW
1887}
1888
1889/* By now we know the target architecture, and we may have an */
1890/* ldfile_output_machine_name */
1891static void
075d7359 1892DEFUN_VOID (lang_check)
2fa0b342
DHW
1893{
1894 lang_statement_union_type *file;
075d7359
SC
1895 bfd *input_bfd;
1896 unsigned long input_machine;
1897 enum bfd_architecture input_architecture;
1898 CONST bfd_arch_info_type *compatible;
7bc4a0d7 1899
2fa0b342 1900 for (file = file_chain.head;
075d7359
SC
1901 file != (lang_statement_union_type *) NULL;
1902 file = file->input_statement.next)
1903 {
1904 unsigned long ldfile_new_output_machine = 0;
1905 enum bfd_architecture ldfile_new_output_architecture = bfd_arch_unknown;
7bc4a0d7 1906
075d7359 1907 input_bfd = file->input_statement.the_bfd;
7bc4a0d7 1908
075d7359
SC
1909 input_machine = bfd_get_mach (input_bfd);
1910 input_architecture = bfd_get_arch (input_bfd);
7bc4a0d7 1911
7bc4a0d7 1912
075d7359
SC
1913 /* Inspect the architecture and ensure we're linking like with
1914 like */
7bc4a0d7 1915
075d7359
SC
1916 compatible = bfd_arch_get_compatible (input_bfd,
1917 output_bfd);
22a78f0d 1918
075d7359
SC
1919 if (compatible)
1920 {
1921 ldfile_output_machine = compatible->mach;
1922 ldfile_output_architecture = compatible->arch;
1923 }
1924 else
1925 {
7bc4a0d7 1926
075d7359
SC
1927 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1928 bfd_printable_name (input_bfd), input_bfd,
1929 bfd_printable_name (output_bfd));
7bc4a0d7 1930
075d7359 1931 bfd_set_arch_mach (output_bfd,
22a78f0d
SC
1932 input_architecture,
1933 input_machine);
075d7359 1934 }
2fa0b342 1935
075d7359
SC
1936 }
1937}
2fa0b342
DHW
1938
1939/*
075d7359
SC
1940 * run through all the global common symbols and tie them
1941 * to the output section requested.
b6316534
SC
1942 *
1943 As an experiment we do this 4 times, once for all the byte sizes,
1944 then all the two bytes, all the four bytes and then everything else
1945 */
2fa0b342
DHW
1946
1947static void
075d7359 1948DEFUN_VOID (lang_common)
2fa0b342
DHW
1949{
1950 ldsym_type *lgs;
b6316534 1951 size_t power;
075d7359 1952
2fa0b342 1953 if (config.relocateable_output == false ||
075d7359
SC
1954 command_line.force_common_definition == true)
1955 {
1956 for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1)
1418c83b 1957 {
075d7359
SC
1958 for (lgs = symbol_head;
1959 lgs != (ldsym_type *) NULL;
1960 lgs = lgs->next)
1961 {
1962 asymbol *com;
1963 unsigned int power_of_two;
1964 size_t size;
1965 size_t align;
1418c83b 1966
075d7359 1967 if (lgs->scoms_chain != (asymbol **) NULL)
1418c83b 1968 {
075d7359
SC
1969 com = *(lgs->scoms_chain);
1970 size = com->value;
1971 switch (size)
1972 {
1973 case 0:
1974 case 1:
1975 align = 1;
1976 power_of_two = 0;
1977 break;
1978 case 2:
1979 power_of_two = 1;
1980 align = 2;
1981 break;
1982 case 3:
1983 case 4:
1984 power_of_two = 2;
1985 align = 4;
1986 break;
1987 case 5:
1988 case 6:
1989 case 7:
1990 case 8:
1991 power_of_two = 3;
1992 align = 8;
1993 break;
1994 default:
1995 power_of_two = 4;
1996 align = 16;
1997 break;
1998 }
1999 if (config.sort_common == false || align == power)
2000 {
2001 /* Change from a common symbol into a definition of
2002 a symbol */
2003 lgs->sdefs_chain = lgs->scoms_chain;
2004 lgs->scoms_chain = (asymbol **) NULL;
2005 commons_pending--;
2006 /* Point to the correct common section */
2007 com->section =
2008 ((lang_input_statement_type *)
2009 (com->the_bfd->usrdata))->common_section;
2010 /* Fix the size of the common section */
2011
2012 com->section->_raw_size =
2013 ALIGN (com->section->_raw_size, align);
2014
2015 /* Remember if this is the biggest alignment ever seen */
2016 if (power_of_two > com->section->alignment_power)
2017 {
2018 com->section->alignment_power = power_of_two;
2019 }
2020
2021 /* Symbol stops being common and starts being global, but
2022 we remember that it was common once. */
2023
2024 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
2025 com->value = com->section->_raw_size;
2026
2027 if (write_map)
2028 {
2029 fprintf (config.map_file, "Allocating common %s: %x at %x %s\n",
2030 lgs->name,
2031 (unsigned) size,
2032 (unsigned) com->value,
2033 com->the_bfd->filename);
2034 }
2035
2036 com->section->_raw_size += size;
2037
2038 }
1418c83b 2039 }
097879bc 2040
b6316534 2041 }
2fa0b342 2042 }
b6316534 2043 }
1418c83b
SC
2044
2045
2fa0b342
DHW
2046}
2047
2048/*
075d7359 2049run through the input files and ensure that every input
2fa0b342
DHW
2050section has somewhere to go. If one is found without
2051a destination then create an input request and place it
2052into the statement tree.
2053*/
2054
075d7359
SC
2055static void
2056DEFUN_VOID (lang_place_orphans)
2fa0b342
DHW
2057{
2058 lang_input_statement_type *file;
1418c83b 2059
075d7359
SC
2060 for (file = (lang_input_statement_type *) file_chain.head;
2061 file != (lang_input_statement_type *) NULL;
2062 file = (lang_input_statement_type *) file->next)
2063 {
2064 asection *s;
2065
2066 for (s = file->the_bfd->sections;
2067 s != (asection *) NULL;
2068 s = s->next)
2069 {
2070 if (s->output_section == (asection *) NULL)
2071 {
2072 /* This section of the file is not attatched, root
2073 around for a sensible place for it to go */
2074
2075 if (file->common_section == s)
2076 {
2077 /* This is a lonely common section which must
2078 have come from an archive. We attatch to the
2079 section with the wildcard */
2080 if (config.relocateable_output != true
2081 && command_line.force_common_definition == false)
2082 {
2083 if (default_common_section ==
2084 (lang_output_section_statement_type *) NULL)
2085 {
2086 info ("%P: No [COMMON] command, defaulting to .bss\n");
2087
2088 default_common_section =
2089 lang_output_section_statement_lookup (".bss");
2090
2091 }
2092 wild_doit (&default_common_section->children, s,
2093 default_common_section, file);
2094 }
2095 }
2096 else
2097 {
2098 lang_output_section_statement_type *os =
2099 lang_output_section_statement_lookup (s->name);
2fa0b342 2100
075d7359
SC
2101 wild_doit (&os->children, s, os, file);
2102 }
2103 }
2fa0b342 2104 }
2fa0b342 2105 }
2fa0b342
DHW
2106}
2107
2108
2fa0b342 2109void
075d7359
SC
2110DEFUN (lang_set_flags, (ptr, flags),
2111 int *ptr AND
2112 CONST char *flags)
2fa0b342 2113{
075d7359
SC
2114 boolean state = false;
2115
2116 *ptr = 0;
2fa0b342 2117 while (*flags)
075d7359
SC
2118 {
2119 if (*flags == '!')
2120 {
2121 state = false;
2122 flags++;
2123 }
2124 else
2125 state = true;
2126 switch (*flags)
2127 {
2128 case 'R':
2129 /* ptr->flag_read = state; */
2130 break;
2131 case 'W':
2132 /* ptr->flag_write = state; */
2133 break;
2134 case 'X':
2135 /* ptr->flag_executable= state;*/
2136 break;
2137 case 'L':
2138 case 'I':
2139 /* ptr->flag_loadable= state;*/
2140 break;
2141 default:
2142 einfo ("%P%F illegal syntax in flags\n");
2143 break;
2144 }
dc4726c2
SC
2145 flags++;
2146 }
2fa0b342
DHW
2147}
2148
2149
2150
2151void
075d7359
SC
2152DEFUN (lang_for_each_file, (func),
2153 PROTO (void, (*func), (lang_input_statement_type *)))
2fa0b342
DHW
2154{
2155 lang_input_statement_type *f;
075d7359
SC
2156
2157 for (f = (lang_input_statement_type *) file_chain.head;
2158 f != (lang_input_statement_type *) NULL;
2159 f = (lang_input_statement_type *) f->next)
2160 {
2161 func (f);
2162 }
2fa0b342
DHW
2163}
2164
2165
2166void
075d7359
SC
2167DEFUN (lang_for_each_input_section, (func),
2168 PROTO (void, (*func), (bfd * ab, asection * as)))
2fa0b342
DHW
2169{
2170 lang_input_statement_type *f;
075d7359
SC
2171
2172 for (f = (lang_input_statement_type *) file_chain.head;
2173 f != (lang_input_statement_type *) NULL;
2174 f = (lang_input_statement_type *) f->next)
2fa0b342
DHW
2175 {
2176 asection *s;
075d7359 2177
2fa0b342 2178 for (s = f->the_bfd->sections;
075d7359
SC
2179 s != (asection *) NULL;
2180 s = s->next)
2181 {
2182 func (f->the_bfd, s);
2183 }
2fa0b342
DHW
2184 }
2185}
2186
2187
2188
075d7359
SC
2189void
2190DEFUN (ldlang_add_file, (entry),
2191 lang_input_statement_type * entry)
2fa0b342 2192{
1418c83b 2193
075d7359
SC
2194 lang_statement_append (&file_chain,
2195 (lang_statement_union_type *) entry,
2196 &entry->next);
2fa0b342
DHW
2197}
2198
2fa0b342 2199void
075d7359
SC
2200DEFUN (lang_add_output, (name),
2201 CONST char *name)
2fa0b342 2202{
075d7359
SC
2203 lang_output_statement_type *new = new_stat (lang_output_statement,
2204 stat_ptr);
2205
2fa0b342
DHW
2206 new->name = name;
2207 had_output_filename = true;
2208}
2209
2210
2211static lang_output_section_statement_type *current_section;
2212
2213void
075d7359
SC
2214DEFUN (lang_enter_output_section_statement,
2215 (output_section_statement_name,
2216 address_exp,
2217 flags,
2218 block_value),
2219 char *output_section_statement_name AND
2220 etree_type * address_exp AND
2221 int flags AND
2222 bfd_vma block_value)
2fa0b342
DHW
2223{
2224 lang_output_section_statement_type *os;
075d7359
SC
2225
2226 current_section =
2fa0b342 2227 os =
075d7359
SC
2228 lang_output_section_statement_lookup (output_section_statement_name);
2229
2230
2fa0b342 2231
2fa0b342
DHW
2232 /* Add this statement to tree */
2233 /* add_statement(lang_output_section_statement_enum,
2234 output_section_statement);*/
2235 /* Make next things chain into subchain of this */
2236
2237 if (os->addr_tree ==
075d7359
SC
2238 (etree_type *) NULL)
2239 {
2240 os->addr_tree =
2241 address_exp;
2242 }
7bc4a0d7 2243 os->flags = flags;
ae475b39
SC
2244 if (flags & SEC_NEVER_LOAD)
2245 os->loadable = 0;
2246 else
2247 os->loadable = 1;
2fa0b342 2248 os->block_value = block_value;
075d7359 2249 stat_ptr = &os->children;
2fa0b342
DHW
2250
2251}
2252
075d7359
SC
2253void
2254DEFUN_VOID (lang_final)
2fa0b342 2255{
075d7359
SC
2256 if (had_output_filename == false)
2257 {
2258 extern CONST char *output_filename;
2fa0b342 2259
075d7359
SC
2260 lang_add_output (output_filename);
2261 }
2262}
2fa0b342 2263
c611e285
SC
2264/* Reset the current counters in the regions */
2265static void
075d7359 2266DEFUN_VOID (reset_memory_regions)
c611e285 2267{
075d7359
SC
2268 lang_memory_region_type *p = lang_memory_region_list;
2269
c611e285 2270 for (p = lang_memory_region_list;
075d7359
SC
2271 p != (lang_memory_region_type *) NULL;
2272 p = p->next)
2273 {
ae475b39 2274 p->old_length = p->current - p->origin;
075d7359
SC
2275 p->current = p->origin;
2276 }
c611e285 2277}
2fa0b342
DHW
2278
2279
ae475b39 2280
1418c83b 2281asymbol *
075d7359
SC
2282DEFUN (create_symbol, (name, flags, section),
2283 CONST char *name AND
2284 flagword flags AND
2285 asection * section)
2fa0b342
DHW
2286{
2287 extern lang_input_statement_type *script_file;
075d7359
SC
2288 asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
2289
2fa0b342 2290 /* Add this definition to script file */
075d7359
SC
2291 asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
2292
2293 def->name = buystring (name);
2fa0b342
DHW
2294 def->udata = 0;
2295 def->flags = flags;
2296 def->section = section;
2297
2298 *def_ptr = def;
075d7359 2299 Q_enter_global_ref (def_ptr, name);
2fa0b342
DHW
2300 return def;
2301}
2302
2fa0b342 2303void
075d7359
SC
2304DEFUN_VOID (lang_process)
2305{
9d1fe8a4 2306
075d7359
SC
2307 if (had_script == false)
2308 {
2309 parse_line (ldemul_get_script (), 1);
c611e285 2310 }
075d7359 2311 lang_reasonable_defaults ();
1418c83b
SC
2312 current_target = default_target;
2313
075d7359 2314 lang_for_each_statement (ldlang_open_output); /* Open the output file */
1418c83b
SC
2315 /* For each output section statement, create a section in the output
2316 file */
075d7359 2317 lang_create_output_section_statements ();
1418c83b
SC
2318
2319 /* Create a dummy bfd for the script */
075d7359 2320 lang_init_script_file ();
1418c83b
SC
2321
2322 /* Add to the hash table all undefineds on the command line */
075d7359 2323 lang_place_undefineds ();
1418c83b
SC
2324
2325 /* Create a bfd for each input file */
2326 current_target = default_target;
075d7359 2327 lang_for_each_statement (open_input_bfds);
1418c83b 2328
075d7359 2329 common_section.userdata = (PTR) & common_section_userdata;
1418c83b 2330
81016051 2331
1418c83b 2332 /* Run through the contours of the script and attatch input sections
075d7359 2333 to the correct output sections
1418c83b 2334 */
075d7359
SC
2335 find_constructors ();
2336 map_input_to_output_sections (statement_list.head, (char *) NULL,
2337 (lang_output_section_statement_type *) NULL);
1418c83b 2338
81016051 2339
1418c83b 2340 /* Find any sections not attatched explicitly and handle them */
075d7359 2341 lang_place_orphans ();
1418c83b
SC
2342
2343 /* Size up the common data */
075d7359 2344 lang_common ();
1418c83b 2345
075d7359 2346 ldemul_before_allocation ();
1418c83b 2347
c611e285 2348
ae475b39
SC
2349#if 0
2350 had_relax = true;
2351 while (had_relax)
2352 {
2353
2354 had_relax = false;
c611e285 2355
ae475b39
SC
2356 lang_size_sections (statement_list.head,
2357 (lang_output_section_statement_type *) NULL,
2358 &(statement_list.head), 0, (bfd_vma) 0, true);
2359 /* FIXME. Until the code in relax is fixed so that it only reads in
2360 stuff once, we cant iterate since there is no way for the linker to
2361 know what has been patched and what hasn't */
2362 break;
2363
2364 }
2365#endif
c611e285 2366
c611e285 2367 /* Now run around and relax if we can */
075d7359 2368 if (command_line.relax)
c611e285 2369 {
ae475b39
SC
2370 /* First time round is a trial run to get the 'worst case' addresses of the
2371 objects if there was no relaxing */
2372 lang_size_sections (statement_list.head,
2373 (lang_output_section_statement_type *) NULL,
2374 &(statement_list.head), 0, (bfd_vma) 0, false);
c611e285 2375
075d7359 2376
075d7359 2377
ae475b39
SC
2378 /* Move the global symbols around so the second pass of relaxing can
2379 see them */
2380 lang_relocate_globals ();
075d7359 2381
ae475b39 2382 reset_memory_regions ();
075d7359 2383
ae475b39
SC
2384 /* Do all the assignments, now that we know the final restingplaces
2385 of all the symbols */
2386
2387 lang_do_assignments (statement_list.head,
2388 abs_output_section,
2389 0, (bfd_vma) 0);
2390
2391
2392 /* Perform another relax pass - this time we know where the
2393 globals are, so can make better guess */
2394 lang_size_sections (statement_list.head,
2395 (lang_output_section_statement_type *) NULL,
2396 &(statement_list.head), 0, (bfd_vma) 0, true);
075d7359
SC
2397
2398
2399
ae475b39
SC
2400 }
2401
2402 else
2403 {
2404 /* Size up the sections */
2405 lang_size_sections (statement_list.head,
2406 abs_output_section,
2407 &(statement_list.head), 0, (bfd_vma) 0, false);
075d7359
SC
2408
2409 }
c611e285 2410
1418c83b
SC
2411
2412 /* See if anything special should be done now we know how big
2413 everything is */
075d7359 2414 ldemul_after_allocation ();
1418c83b
SC
2415
2416 /* Do all the assignments, now that we know the final restingplaces
2417 of all the symbols */
2418
075d7359
SC
2419 lang_do_assignments (statement_list.head,
2420 abs_output_section,
2421 0, (bfd_vma) 0);
1418c83b 2422
c611e285 2423
ffc50032 2424 /* Move the global symbols around */
075d7359 2425 lang_relocate_globals ();
ffc50032 2426
1418c83b
SC
2427 /* Make sure that we're not mixing architectures */
2428
075d7359 2429 lang_check ();
1418c83b 2430
1418c83b 2431 /* Final stuffs */
075d7359 2432 lang_finish ();
2fa0b342
DHW
2433}
2434
2fa0b342 2435/* EXPORTED TO YACC */
1418c83b 2436
2fa0b342 2437void
075d7359
SC
2438DEFUN (lang_add_wild, (section_name, filename),
2439 CONST char *CONST section_name AND
2440 CONST char *CONST filename)
1418c83b 2441{
075d7359
SC
2442 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2443 stat_ptr);
1418c83b 2444
075d7359
SC
2445 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2446 {
2447 placed_commons = true;
2448 }
2449 if (filename != (char *) NULL)
2450 {
2451 lang_has_input_file = true;
2452 }
1418c83b
SC
2453 new->section_name = section_name;
2454 new->filename = filename;
075d7359 2455 lang_list_init (&new->children);
1418c83b 2456}
075d7359 2457
1418c83b 2458void
075d7359
SC
2459DEFUN (lang_section_start, (name, address),
2460 CONST char *name AND
2461 etree_type * address)
2fa0b342 2462{
075d7359
SC
2463 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2464
2fa0b342
DHW
2465 ad->section_name = name;
2466 ad->address = address;
2467}
1418c83b 2468
075d7359
SC
2469void
2470DEFUN (lang_add_entry, (name),
2471 CONST char *name)
2fa0b342
DHW
2472{
2473 entry_symbol = name;
2474}
2475
2476void
075d7359
SC
2477DEFUN (lang_add_target, (name),
2478 CONST char *name)
2fa0b342 2479{
075d7359
SC
2480 lang_target_statement_type *new = new_stat (lang_target_statement,
2481 stat_ptr);
2482
2fa0b342
DHW
2483 new->target = name;
2484
2485}
2fa0b342 2486
2fa0b342 2487void
075d7359
SC
2488DEFUN (lang_add_map, (name),
2489 CONST char *name)
2fa0b342 2490{
075d7359
SC
2491 while (*name)
2492 {
2493 switch (*name)
2494 {
2495 case 'F':
2496 map_option_f = true;
2497 break;
2498 }
2499 name++;
2fa0b342 2500 }
2fa0b342
DHW
2501}
2502
075d7359
SC
2503void
2504DEFUN (lang_add_fill, (exp),
2505 int exp)
2fa0b342 2506{
075d7359
SC
2507 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2508 stat_ptr);
2509
2fa0b342
DHW
2510 new->fill = exp;
2511}
2512
075d7359
SC
2513void
2514DEFUN (lang_add_data, (type, exp),
2515 int type AND
2516 union etree_union *exp)
2fa0b342
DHW
2517{
2518
075d7359 2519 lang_data_statement_type *new = new_stat (lang_data_statement,
2fa0b342 2520 stat_ptr);
075d7359
SC
2521
2522 new->exp = exp;
2523 new->type = type;
2fa0b342
DHW
2524
2525}
075d7359 2526
2fa0b342 2527void
075d7359
SC
2528DEFUN (lang_add_assignment, (exp),
2529 etree_type * exp)
2fa0b342 2530{
075d7359
SC
2531 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2532 stat_ptr);
2533
2fa0b342
DHW
2534 new->exp = exp;
2535}
2536
2537void
075d7359
SC
2538DEFUN (lang_add_attribute, (attribute),
2539 enum statement_enum attribute)
2fa0b342 2540{
075d7359 2541 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2fa0b342
DHW
2542}
2543
075d7359
SC
2544void
2545DEFUN (lang_startup, (name),
2546 CONST char *name)
2fa0b342 2547{
075d7359
SC
2548 if (startup_file != (char *) NULL)
2549 {
2550 einfo ("%P%FMultiple STARTUP files\n");
2551 }
2fa0b342
DHW
2552 first_file->filename = name;
2553 first_file->local_sym_name = name;
2554
075d7359 2555 startup_file = name;
2fa0b342 2556}
075d7359
SC
2557
2558void
2559DEFUN (lang_float, (maybe),
2560 boolean maybe)
2fa0b342
DHW
2561{
2562 lang_float_flag = maybe;
2563}
2564
075d7359
SC
2565void
2566DEFUN (lang_leave_output_section_statement, (fill, memspec),
2567 bfd_vma fill AND
2568 CONST char *memspec)
2fa0b342
DHW
2569{
2570 current_section->fill = fill;
075d7359 2571 current_section->region = lang_memory_region_lookup (memspec);
2fa0b342 2572 stat_ptr = &statement_list;
81016051
SC
2573
2574 /* We remember if we are closing a .data section, since we use it to
2575 store constructors in */
075d7359
SC
2576 if (strcmp (current_section->name, ".data") == 0)
2577 {
2578 end_of_data_section_statement_list = statement_list;
81016051 2579
075d7359 2580 }
2fa0b342 2581}
075d7359 2582
9f32f7c2
SC
2583/*
2584 Create an absolute symbol with the given name with the value of the
2585 address of first byte of the section named.
2fa0b342 2586
9f32f7c2
SC
2587 If the symbol already exists, then do nothing.
2588*/
8cb5eb31 2589void
075d7359
SC
2590DEFUN (lang_abs_symbol_at_beginning_of, (section, name),
2591 CONST char *section AND
2592 CONST char *name)
2593{
2594 if (ldsym_undefined (name))
2595 {
2596 asection *s = bfd_get_section_by_name (output_bfd, section);
2597 asymbol *def = create_symbol (name,
2598 BSF_GLOBAL | BSF_EXPORT,
2599 &bfd_abs_section);
2600
2601 if (s != (asection *) NULL)
2602 {
2603 def->value = s->vma;
2604 }
2605 else
2606 {
2607 def->value = 0;
2608 }
9f32f7c2 2609 }
8cb5eb31
SC
2610}
2611
9f32f7c2
SC
2612/*
2613 Create an absolute symbol with the given name with the value of the
2614 address of the first byte after the end of the section named.
2615
2616 If the symbol already exists, then do nothing.
2617*/
2fa0b342 2618void
075d7359
SC
2619DEFUN (lang_abs_symbol_at_end_of, (section, name),
2620 CONST char *section AND
2621 CONST char *name)
2622{
2623 if (ldsym_undefined (name))
2624 {
2625 asection *s = bfd_get_section_by_name (output_bfd, section);
2626
2627 /* Add a symbol called _end */
2628 asymbol *def = create_symbol (name,
2629 BSF_GLOBAL | BSF_EXPORT,
2630 &bfd_abs_section);
2631
2632 if (s != (asection *) NULL)
2633 {
2634 def->value = s->vma + s->_raw_size;
2635 }
2636 else
2637 {
2638 def->value = 0;
2639 }
9f32f7c2 2640 }
2fa0b342
DHW
2641}
2642
075d7359
SC
2643void
2644DEFUN (lang_statement_append, (list, element, field),
2645 lang_statement_list_type * list AND
2646 lang_statement_union_type * element AND
2647 lang_statement_union_type ** field)
2fa0b342
DHW
2648{
2649 *(list->tail) = element;
2650 list->tail = field;
2651}
2652
097879bc
SC
2653/* Set the output format type */
2654void
075d7359
SC
2655DEFUN (lang_add_output_format, (format),
2656 CONST char *format)
097879bc
SC
2657{
2658 output_target = format;
2659}
This page took 0.190111 seconds and 4 git commands to generate.