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