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