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