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