]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldlang.c
* scripttempl/pe.sc: Use SORT to sort sections appropriately.
[thirdparty/binutils-gdb.git] / ld / ldlang.c
CommitLineData
e20873a7 1/* Linker command language support.
26c61946
ILT
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
075d7359 4
2fa0b342
DHW
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
26c7ca95 9the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
d22ec3b0
ILT
18along with GLD; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
2fa0b342 21
2fa0b342 22#include "bfd.h"
075d7359 23#include "sysdep.h"
4c2123b6 24#include "libiberty.h"
9f19768a 25#include "obstack.h"
c477527c 26#include "bfdlink.h"
2fa0b342
DHW
27
28#include "ld.h"
fcf276c4 29#include "ldmain.h"
7bc4a0d7 30#include "ldgram.h"
2fa0b342 31#include "ldexp.h"
fcf276c4 32#include "ldlang.h"
8c514453 33#include "ldemul.h"
2fa0b342 34#include "ldlex.h"
3f8d46e7 35#include "ldmisc.h"
c611e285 36#include "ldctor.h"
fcf276c4 37#include "ldfile.h"
86bc0974 38#include "fnmatch.h"
b98bd3db 39
d22ec3b0
ILT
40#include <ctype.h>
41
2fa0b342 42/* FORWARDS */
5af8e9d6
KR
43static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
44 size_t,
45 lang_statement_list_type*));
46
2fa0b342 47
d4c02e29 48/* LOCALS */
bfbdc80f 49static struct obstack stat_obstack;
075d7359 50
0b2f8d2e 51#define obstack_chunk_alloc xmalloc
bfbdc80f 52#define obstack_chunk_free free
075d7359 53static CONST char *startup_file;
d4c02e29
SC
54static lang_statement_list_type input_file_chain;
55static boolean placed_commons = false;
56static lang_output_section_statement_type *default_common_section;
57static boolean map_option_f;
58static bfd_vma print_dot;
59static lang_input_statement_type *first_file;
60static lang_statement_list_type lang_output_section_statement;
61static CONST char *current_target;
62static CONST char *output_target;
d4c02e29 63static lang_statement_list_type statement_list;
061e65f8 64static struct lang_phdr *lang_phdr_list;
ffc50032 65
9f629407
ILT
66static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
193c5f93 71 const char *target, boolean add_to_list));
9f629407 72static void init_os PARAMS ((lang_output_section_statement_type *s));
86bc0974 73static void exp_init_os PARAMS ((etree_type *));
23595ba5 74static void section_already_linked PARAMS ((bfd *, asection *, PTR));
86bc0974 75static boolean wildcardp PARAMS ((const char *));
26c7ca95
ILT
76static lang_statement_union_type *wild_sort
77 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
78 asection *));
9f629407
ILT
79static void wild_section PARAMS ((lang_wild_statement_type *ptr,
80 const char *section,
81 lang_input_statement_type *file,
82 lang_output_section_statement_type *output));
193c5f93 83static lang_input_statement_type *lookup_name PARAMS ((const char *name));
6a02a973
ILT
84static void load_symbols PARAMS ((lang_input_statement_type *entry,
85 lang_statement_list_type *));
86bc0974
ILT
86static void wild_file PARAMS ((lang_wild_statement_type *, const char *,
87 lang_input_statement_type *,
88 lang_output_section_statement_type *));
9f629407
ILT
89static void wild PARAMS ((lang_wild_statement_type *s,
90 const char *section, const char *file,
91 const char *target,
92 lang_output_section_statement_type *output));
93static bfd *open_output PARAMS ((const char *name));
94static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
309c8153
ILT
95static void open_input_bfds
96 PARAMS ((lang_statement_union_type *statement, boolean));
9f629407
ILT
97static void lang_reasonable_defaults PARAMS ((void));
98static void lang_place_undefineds PARAMS ((void));
9f629407
ILT
99static void map_input_to_output_sections
100 PARAMS ((lang_statement_union_type *s,
101 const char *target,
102 lang_output_section_statement_type *output_section_statement));
103static void print_output_section_statement
104 PARAMS ((lang_output_section_statement_type *output_section_statement));
105static void print_assignment
106 PARAMS ((lang_assignment_statement_type *assignment,
107 lang_output_section_statement_type *output_section));
108static void print_input_statement PARAMS ((lang_input_statement_type *statm));
c5a0d31a 109static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
9f629407
ILT
110static void print_input_section PARAMS ((lang_input_section_type *in));
111static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
112static void print_data_statement PARAMS ((lang_data_statement_type *data));
0cacbcbe 113static void print_address_statement PARAMS ((lang_address_statement_type *));
4fdbafb2 114static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
9f629407
ILT
115static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
116static void print_wild_statement
117 PARAMS ((lang_wild_statement_type *w,
118 lang_output_section_statement_type *os));
309c8153
ILT
119static void print_group
120 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
9f629407
ILT
121static void print_statement PARAMS ((lang_statement_union_type *s,
122 lang_output_section_statement_type *os));
708f7fbc
ILT
123static void print_statement_list PARAMS ((lang_statement_union_type *s,
124 lang_output_section_statement_type *os));
9f629407
ILT
125static void print_statements PARAMS ((void));
126static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
127 fill_type fill, unsigned int power,
128 asection *output_section_statement,
129 bfd_vma dot));
130static bfd_vma size_input_section
131 PARAMS ((lang_statement_union_type **this_ptr,
132 lang_output_section_statement_type *output_section_statement,
c477527c 133 fill_type fill, bfd_vma dot, boolean relax));
9f629407 134static void lang_finish PARAMS ((void));
d3c324e5 135static void ignore_bfd_errors PARAMS ((const char *, ...));
9f629407
ILT
136static void lang_check PARAMS ((void));
137static void lang_common PARAMS ((void));
c477527c 138static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
9f629407
ILT
139static void lang_place_orphans PARAMS ((void));
140static int topower PARAMS ((int));
1d169acc 141static void lang_set_startof PARAMS ((void));
9f629407 142static void reset_memory_regions PARAMS ((void));
061e65f8 143static void lang_record_phdrs PARAMS ((void));
71b012a8
RH
144static void lang_gc_wild_section
145 PARAMS ((lang_wild_statement_type *, const char *,
146 lang_input_statement_type *));
147static void lang_gc_wild_file
148 PARAMS ((lang_wild_statement_type *, const char *,
149 lang_input_statement_type *));
150static void lang_gc_wild
151 PARAMS ((lang_wild_statement_type *, const char *, const char *));
152static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
153static void lang_gc_sections PARAMS ((void));
154
9f629407 155
2fa0b342 156/* EXPORTS */
ffc50032 157lang_output_section_statement_type *abs_output_section;
d4c02e29 158lang_statement_list_type *stat_ptr = &statement_list;
a4d2a48e 159lang_statement_list_type file_chain = { 0 };
061e65f8 160const char *entry_symbol = NULL;
f054ce20 161boolean entry_from_cmdline;
d4c02e29 162boolean lang_has_input_file = false;
d4c02e29
SC
163boolean had_output_filename = false;
164boolean lang_float_flag = false;
97fbbaca 165boolean delete_output_file_on_failure = false;
582dd77f 166struct lang_nocrossrefs *nocrossref_list;
075d7359 167
9fce28ed
SC
168etree_type *base; /* Relocation base - or null */
169
170
b897ed67 171#if defined(__STDC__) || defined(ALMOST_STDC)
2fa0b342
DHW
172#define cat(a,b) a##b
173#else
174#define cat(a,b) a/**/b
175#endif
176
177#define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
178
179#define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
180
181#define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
182
0cacbcbe
ILT
183#define SECTION_NAME_MAP_LENGTH (16)
184
bfbdc80f 185PTR
8ddef552
DM
186stat_alloc (size)
187 size_t size;
bfbdc80f 188{
075d7359 189 return obstack_alloc (&stat_obstack, size);
bfbdc80f 190}
9f629407 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:
0cacbcbe 235 case lang_fill_statement_enum:
1418c83b
SC
236 break;
237 default:
075d7359 238 FAIL ();
1418c83b
SC
239 break;
240 }
075d7359 241 }
1418c83b
SC
242}
243
244void
8ddef552 245lang_for_each_statement (func)
c477527c 246 void (*func) PARAMS ((lang_statement_union_type *));
1418c83b 247{
075d7359
SC
248 lang_for_each_statement_worker (func,
249 statement_list.head);
1418c83b 250}
075d7359 251
1418c83b 252/*----------------------------------------------------------------------*/
075d7359 253void
8ddef552
DM
254lang_list_init (list)
255 lang_statement_list_type *list;
2fa0b342 256{
075d7359
SC
257 list->head = (lang_statement_union_type *) NULL;
258 list->tail = &list->head;
2fa0b342
DHW
259}
260
1418c83b 261/*----------------------------------------------------------------------
075d7359 262
1418c83b
SC
263 build a new statement node for the parse tree
264
265 */
2fa0b342
DHW
266
267static
075d7359 268lang_statement_union_type *
8ddef552
DM
269new_statement (type, size, list)
270 enum statement_enum type;
5af8e9d6 271 size_t size;
8ddef552 272 lang_statement_list_type * list;
2fa0b342
DHW
273{
274 lang_statement_union_type *new = (lang_statement_union_type *)
075d7359
SC
275 stat_alloc (size);
276
2fa0b342 277 new->header.type = type;
075d7359
SC
278 new->header.next = (lang_statement_union_type *) NULL;
279 lang_statement_append (list, new, &new->header.next);
2fa0b342
DHW
280 return new;
281}
282
1418c83b
SC
283/*
284 Build a new input file node for the language. There are several ways
285 in which we treat an input file, eg, we only look at symbols, or
286 prefix it with a -l etc.
287
288 We can be supplied with requests for input files more than once;
289 they may, for example be split over serveral lines like foo.o(.text)
290 foo.o(.data) etc, so when asked for a file we check that we havn't
291 got it already so we don't duplicate the bfd.
292
293 */
2fa0b342 294static lang_input_statement_type *
193c5f93 295new_afile (name, file_type, target, add_to_list)
9f629407
ILT
296 CONST char *name;
297 lang_input_file_enum_type file_type;
298 CONST char *target;
193c5f93 299 boolean add_to_list;
2fa0b342 300{
193c5f93 301 lang_input_statement_type *p;
dc4726c2 302
193c5f93
ILT
303 if (add_to_list)
304 p = new_stat (lang_input_statement, stat_ptr);
305 else
306 {
307 p = ((lang_input_statement_type *)
308 stat_alloc (sizeof (lang_input_statement_type)));
309 p->header.next = NULL;
310 }
075d7359 311
2fa0b342
DHW
312 lang_has_input_file = true;
313 p->target = target;
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;
1d169acc 372 p->dynamic = config.dynamic_link;
3c8deccc 373 p->whole_archive = whole_archive;
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 430{
b038ec7b 431 lang_memory_region_type *p;
075d7359 432
1418c83b 433 for (p = lang_memory_region_list;
075d7359
SC
434 p != (lang_memory_region_type *) NULL;
435 p = p->next)
436 {
437 if (strcmp (p->name, name) == 0)
438 {
439 return p;
440 }
2fa0b342 441 }
36ea6198
ILT
442
443#if 0
444 /* This code used to always use the first region in the list as the
445 default region. I changed it to instead use a region
446 encompassing all of memory as the default region. This permits
447 NOLOAD sections to work reasonably without requiring a region.
448 People should specify what region they mean, if they really want
449 a region. */
075d7359 450 if (strcmp (name, "*default*") == 0)
2fa0b342 451 {
075d7359
SC
452 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
453 {
454 return lang_memory_region_list;
455 }
2fa0b342 456 }
36ea6198
ILT
457#endif
458
075d7359
SC
459 {
460 lang_memory_region_type *new =
1d169acc 461 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
075d7359
SC
462
463 new->name = buystring (name);
464 new->next = (lang_memory_region_type *) NULL;
465
466 *lang_memory_region_list_tail = new;
467 lang_memory_region_list_tail = &new->next;
468 new->origin = 0;
d3c324e5
MM
469 new->flags = 0;
470 new->not_flags = 0;
8ddef552 471 new->length = ~(bfd_size_type)0;
075d7359
SC
472 new->current = 0;
473 new->had_full_message = false;
474
475 return new;
476 }
2fa0b342
DHW
477}
478
479
d3c324e5
MM
480lang_memory_region_type *
481lang_memory_default (section)
482 asection *section;
483{
484 lang_memory_region_type *p;
485
486 flagword sec_flags = section->flags;
487
488 /* Override SEC_DATA to mean a writable section. */
26c61946 489 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
d3c324e5
MM
490 sec_flags |= SEC_DATA;
491
492 for (p = lang_memory_region_list;
493 p != (lang_memory_region_type *) NULL;
494 p = p->next)
495 {
496 if ((p->flags & sec_flags) != 0
497 && (p->not_flags & sec_flags) == 0)
498 {
499 return p;
500 }
501 }
502 return lang_memory_region_lookup ("*default*");
503}
504
2fa0b342 505lang_output_section_statement_type *
8ddef552
DM
506lang_output_section_find (name)
507 CONST char *CONST name;
2fa0b342
DHW
508{
509 lang_statement_union_type *u;
510 lang_output_section_statement_type *lookup;
511
512 for (u = lang_output_section_statement.head;
075d7359 513 u != (lang_statement_union_type *) NULL;
2fa0b342 514 u = lookup->next)
075d7359
SC
515 {
516 lookup = &u->output_section_statement;
517 if (strcmp (name, lookup->name) == 0)
518 {
1418c83b
SC
519 return lookup;
520 }
075d7359
SC
521 }
522 return (lang_output_section_statement_type *) NULL;
2fa0b342
DHW
523}
524
525lang_output_section_statement_type *
8ddef552
DM
526lang_output_section_statement_lookup (name)
527 CONST char *CONST name;
2fa0b342
DHW
528{
529 lang_output_section_statement_type *lookup;
2fa0b342 530
075d7359
SC
531 lookup = lang_output_section_find (name);
532 if (lookup == (lang_output_section_statement_type *) NULL)
533 {
2fa0b342 534
075d7359
SC
535 lookup = (lang_output_section_statement_type *)
536 new_stat (lang_output_section_statement, stat_ptr);
537 lookup->region = (lang_memory_region_type *) NULL;
538 lookup->fill = 0;
539 lookup->block_value = 1;
540 lookup->name = name;
541
542 lookup->next = (lang_statement_union_type *) NULL;
543 lookup->bfd_section = (asection *) NULL;
544 lookup->processed = false;
86bc0974 545 lookup->sectype = normal_section;
075d7359
SC
546 lookup->addr_tree = (etree_type *) NULL;
547 lang_list_init (&lookup->children);
548
1b8a42f3
ILT
549 lookup->memspec = (CONST char *) NULL;
550 lookup->flags = 0;
551 lookup->subsection_alignment = -1;
552 lookup->section_alignment = -1;
553 lookup->load_base = (union etree_union *) NULL;
061e65f8 554 lookup->phdrs = NULL;
1b8a42f3 555
075d7359
SC
556 lang_statement_append (&lang_output_section_statement,
557 (lang_statement_union_type *) lookup,
558 &lookup->next);
559 }
560 return lookup;
561}
2fa0b342 562
d3c324e5
MM
563static void
564lang_map_flags (flag)
565 flagword flag;
566{
567 if (flag & SEC_ALLOC)
568 minfo ("a");
569
570 if (flag & SEC_CODE)
571 minfo ("x");
572
573 if (flag & SEC_READONLY)
574 minfo ("r");
575
576 if (flag & SEC_DATA)
577 minfo ("w");
578
579 if (flag & SEC_LOAD)
580 minfo ("l");
581}
582
2fa0b342 583void
8ddef552 584lang_map ()
2fa0b342
DHW
585{
586 lang_memory_region_type *m;
075d7359 587
26c7ca95 588 minfo (_("\nMemory Configuration\n\n"));
d3c324e5 589 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
26c7ca95 590 _("Name"), _("Origin"), _("Length"), _("Attributes"));
ae475b39 591
2fa0b342 592 for (m = lang_memory_region_list;
075d7359
SC
593 m != (lang_memory_region_type *) NULL;
594 m = m->next)
2fa0b342 595 {
0cacbcbe
ILT
596 char buf[100];
597 int len;
598
599 fprintf (config.map_file, "%-16s ", m->name);
600
601 sprintf_vma (buf, m->origin);
602 minfo ("0x%s ", buf);
603 len = strlen (buf);
604 while (len < 16)
605 {
606 print_space ();
607 ++len;
608 }
609
d3c324e5
MM
610 minfo ("0x%V", m->length);
611 if (m->flags || m->not_flags)
612 {
613#ifndef BFD64
614 minfo (" ");
615#endif
616 if (m->flags)
617 {
618 print_space ();
619 lang_map_flags (m->flags);
620 }
621
622 if (m->not_flags)
623 {
624 minfo (" !");
625 lang_map_flags (m->not_flags);
626 }
627 }
628
629 print_nl ();
2fa0b342 630 }
2fa0b342 631
26c7ca95 632 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
2fa0b342 633
0cacbcbe 634 print_statements ();
2fa0b342
DHW
635}
636
86bc0974
ILT
637/* Initialize an output section. */
638
075d7359 639static void
8ddef552 640init_os (s)
86bc0974 641 lang_output_section_statement_type *s;
2fa0b342 642{
cf2e4f5f
ILT
643 section_userdata_type *new;
644
86bc0974
ILT
645 if (s->bfd_section != NULL)
646 return;
647
cf2e4f5f 648 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
26c7ca95 649 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
cf2e4f5f
ILT
650
651 new = ((section_userdata_type *)
652 stat_alloc (sizeof (section_userdata_type)));
075d7359
SC
653
654 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
655 if (s->bfd_section == (asection *) NULL)
656 s->bfd_section = bfd_make_section (output_bfd, s->name);
657 if (s->bfd_section == (asection *) NULL)
658 {
26c7ca95 659 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
075d7359 660 output_bfd->xvec->name, s->name);
9d1fe8a4 661 }
2fa0b342 662 s->bfd_section->output_section = s->bfd_section;
7bc4a0d7 663
2fa0b342
DHW
664 /* We initialize an output sections output offset to minus its own */
665 /* vma to allow us to output a section through itself */
666 s->bfd_section->output_offset = 0;
075d7359 667 get_userdata (s->bfd_section) = (PTR) new;
86bc0974
ILT
668
669 /* If there is a base address, make sure that any sections it might
670 mention are initialized. */
671 if (s->addr_tree != NULL)
672 exp_init_os (s->addr_tree);
673}
674
675/* Make sure that all output sections mentioned in an expression are
676 initialized. */
677
678static void
679exp_init_os (exp)
680 etree_type *exp;
681{
682 switch (exp->type.node_class)
683 {
684 case etree_assign:
685 exp_init_os (exp->assign.src);
686 break;
687
688 case etree_binary:
689 exp_init_os (exp->binary.lhs);
690 exp_init_os (exp->binary.rhs);
691 break;
692
693 case etree_trinary:
694 exp_init_os (exp->trinary.cond);
695 exp_init_os (exp->trinary.lhs);
696 exp_init_os (exp->trinary.rhs);
697 break;
698
699 case etree_unary:
700 exp_init_os (exp->unary.child);
701 break;
702
703 case etree_name:
704 switch (exp->type.node_code)
705 {
706 case ADDR:
5735ac9e 707 case LOADADDR:
86bc0974
ILT
708 case SIZEOF:
709 {
710 lang_output_section_statement_type *os;
711
712 os = lang_output_section_find (exp->name.name);
713 if (os != NULL && os->bfd_section == NULL)
714 init_os (os);
715 }
716 }
717 break;
718
719 default:
720 break;
721 }
2fa0b342 722}
23595ba5
ILT
723
724/* Sections marked with the SEC_LINK_ONCE flag should only be linked
725 once into the output. This routine checks each sections, and
726 arranges to discard it if a section of the same name has already
727 been linked. This code assumes that all relevant sections have the
728 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
729 section name. This is called via bfd_map_over_sections. */
730
731/*ARGSUSED*/
732static void
c5a0d31a 733section_already_linked (abfd, sec, data)
23595ba5
ILT
734 bfd *abfd;
735 asection *sec;
c5a0d31a 736 PTR data;
23595ba5 737{
c5a0d31a 738 lang_input_statement_type *entry = (lang_input_statement_type *) data;
23595ba5
ILT
739 struct sec_link_once
740 {
741 struct sec_link_once *next;
742 asection *sec;
743 };
744 static struct sec_link_once *sec_link_once_list;
745 flagword flags;
746 const char *name;
747 struct sec_link_once *l;
748
c5a0d31a
ILT
749 /* If we are only reading symbols from this object, then we want to
750 discard all sections. */
751 if (entry->just_syms_flag)
752 {
753 sec->output_section = bfd_abs_section_ptr;
754 sec->output_offset = sec->vma;
755 return;
756 }
757
23595ba5
ILT
758 flags = bfd_get_section_flags (abfd, sec);
759
760 if ((flags & SEC_LINK_ONCE) == 0)
761 return;
762
763 name = bfd_get_section_name (abfd, sec);
764
765 for (l = sec_link_once_list; l != NULL; l = l->next)
766 {
767 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
768 {
769 /* The section has already been linked. See if we should
770 issue a warning. */
771 switch (flags & SEC_LINK_DUPLICATES)
772 {
773 default:
774 abort ();
775
776 case SEC_LINK_DUPLICATES_DISCARD:
777 break;
778
779 case SEC_LINK_DUPLICATES_ONE_ONLY:
26c7ca95 780 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
23595ba5
ILT
781 abfd, name);
782 break;
783
784 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
785 /* FIXME: We should really dig out the contents of both
786 sections and memcmp them. The COFF/PE spec says that
787 the Microsoft linker does not implement this
788 correctly, so I'm not going to bother doing it
789 either. */
790 /* Fall through. */
791 case SEC_LINK_DUPLICATES_SAME_SIZE:
792 if (bfd_section_size (abfd, sec)
793 != bfd_section_size (l->sec->owner, l->sec))
26c7ca95 794 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
0a0d1bdd 795 abfd, name);
23595ba5
ILT
796 break;
797 }
798
799 /* Set the output_section field so that wild_doit does not
800 create a lang_input_section structure for this section. */
801 sec->output_section = bfd_abs_section_ptr;
802
803 return;
804 }
805 }
806
807 /* This is the first section with this name. Record it. */
808
809 l = (struct sec_link_once *) xmalloc (sizeof *l);
810 l->sec = sec;
811 l->next = sec_link_once_list;
812 sec_link_once_list = l;
813}
64664e69
ILT
814\f
815/* The wild routines.
2fa0b342 816
64664e69
ILT
817 These expand statements like *(.text) and foo.o to a list of
818 explicit actions, like foo.o(.text), bar.o(.text) and
819 foo.o(.text, .data). */
1418c83b 820
9f19768a
ILT
821/* Return true if the PATTERN argument is a wildcard pattern.
822 Although backslashes are treated specially if a pattern contains
823 wildcards, we do not consider the mere presence of a backslash to
824 be enough to cause the the pattern to be treated as a wildcard.
825 That lets us handle DOS filenames more naturally. */
86bc0974
ILT
826
827static boolean
828wildcardp (pattern)
829 const char *pattern;
830{
831 const char *s;
832
833 for (s = pattern; *s != '\0'; ++s)
834 if (*s == '?'
86bc0974
ILT
835 || *s == '*'
836 || *s == '[')
837 return true;
838 return false;
839}
840
64664e69
ILT
841/* Add SECTION to the output section OUTPUT. Do this by creating a
842 lang_input_section statement which is placed at PTR. FILE is the
843 input file which holds SECTION. */
075d7359 844
0b3499f6 845void
8ddef552 846wild_doit (ptr, section, output, file)
64664e69
ILT
847 lang_statement_list_type *ptr;
848 asection *section;
849 lang_output_section_statement_type *output;
850 lang_input_statement_type *file;
2fa0b342 851{
6429ad7e 852 flagword flags;
23595ba5
ILT
853 boolean discard;
854
6429ad7e
ILT
855 flags = bfd_get_section_flags (section->owner, section);
856
23595ba5
ILT
857 discard = false;
858
859 /* If we are doing a final link, discard sections marked with
860 SEC_EXCLUDE. */
861 if (! link_info.relocateable
6429ad7e 862 && (flags & SEC_EXCLUDE) != 0)
23595ba5
ILT
863 discard = true;
864
865 /* Discard input sections which are assigned to a section named
866 DISCARD_SECTION_NAME. */
cf2e4f5f 867 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
23595ba5
ILT
868 discard = true;
869
6429ad7e
ILT
870 /* Discard debugging sections if we are stripping debugging
871 information. */
872 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
873 && (flags & SEC_DEBUGGING) != 0)
874 discard = true;
875
23595ba5 876 if (discard)
cf2e4f5f 877 {
23595ba5 878 if (section->output_section == NULL)
cf2e4f5f
ILT
879 {
880 /* This prevents future calls from assigning this section. */
881 section->output_section = bfd_abs_section_ptr;
882 }
883 return;
884 }
885
23595ba5 886 if (section->output_section == NULL)
ae475b39 887 {
b038ec7b 888 boolean first;
23595ba5 889 lang_input_section_type *new;
b038ec7b 890 flagword flags;
23595ba5
ILT
891
892 if (output->bfd_section == NULL)
b038ec7b
RH
893 {
894 init_os (output);
895 first = true;
896 }
897 else
898 first = false;
23595ba5 899
64664e69 900 /* Add a section reference to the list */
23595ba5 901 new = new_stat (lang_input_section, ptr);
64664e69
ILT
902
903 new->section = section;
904 new->ifile = file;
905 section->output_section = output->bfd_section;
906
b038ec7b
RH
907 flags = section->flags;
908
64664e69
ILT
909 /* We don't copy the SEC_NEVER_LOAD flag from an input section
910 to an output section, because we want to be able to include a
911 SEC_NEVER_LOAD section in the middle of an otherwise loaded
912 section (I don't know why we want to do this, but we do).
913 build_link_order in ldwrite.c handles this case by turning
b038ec7b
RH
914 the embedded SEC_NEVER_LOAD section into a fill. */
915
916 flags &= ~ SEC_NEVER_LOAD;
917
918 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
919 already been processed. One reason to do this is that on pe
920 format targets, .text$foo sections go into .text and it's odd
921 to see .text with SEC_LINK_ONCE set. */
922
923 if (! link_info.relocateable)
924 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
925
926 /* If this is not the first input section, and the SEC_READONLY
927 flag is not currently set, then don't set it just because the
928 input section has it set. */
929
930 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
931 flags &= ~ SEC_READONLY;
86bc0974 932
b038ec7b 933 section->output_section->flags |= flags;
86bc0974 934
b038ec7b
RH
935 /* If SEC_READONLY is not set in the input section, then clear
936 it from the output section. */
937 if ((section->flags & SEC_READONLY) == 0)
938 section->output_section->flags &= ~SEC_READONLY;
64664e69 939
86bc0974 940 switch (output->sectype)
64664e69 941 {
86bc0974
ILT
942 case normal_section:
943 break;
944 case dsect_section:
945 case copy_section:
946 case info_section:
947 case overlay_section:
948 output->bfd_section->flags &= ~SEC_ALLOC;
949 break;
950 case noload_section:
b038ec7b 951 output->bfd_section->flags &= ~SEC_LOAD;
64664e69 952 output->bfd_section->flags |= SEC_NEVER_LOAD;
86bc0974 953 break;
64664e69
ILT
954 }
955
956 if (section->alignment_power > output->bfd_section->alignment_power)
957 output->bfd_section->alignment_power = section->alignment_power;
958
cf2e4f5f 959 /* If supplied an aligment, then force it. */
64664e69
ILT
960 if (output->section_alignment != -1)
961 output->bfd_section->alignment_power = output->section_alignment;
e20873a7 962 }
2fa0b342
DHW
963}
964
26c7ca95
ILT
965/* Handle wildcard sorting. This returns the lang_input_section which
966 should follow the one we are going to create for SECTION and FILE,
967 based on the sorting requirements of WILD. It returns NULL if the
968 new section should just go at the end of the current list. */
969
970static lang_statement_union_type *
971wild_sort (wild, file, section)
972 lang_wild_statement_type *wild;
973 lang_input_statement_type *file;
974 asection *section;
975{
976 const char *section_name;
977 lang_statement_union_type *l;
978
979 if (! wild->filenames_sorted && ! wild->sections_sorted)
980 return NULL;
981
982 section_name = bfd_get_section_name (file->the_bfd, section);
983 for (l = wild->children.head; l != NULL; l = l->next)
984 {
985 lang_input_section_type *ls;
986
987 if (l->header.type != lang_input_section_enum)
988 continue;
989 ls = &l->input_section;
990
991 /* Sorting by filename takes precedence over sorting by section
992 name. */
993
994 if (wild->filenames_sorted)
995 {
6ccce8e8
ILT
996 const char *fn, *ln;
997 boolean fa, la;
26c7ca95
ILT
998 int i;
999
6ccce8e8
ILT
1000 /* The PE support for the .idata section as generated by
1001 dlltool assumes that files will be sorted by the name of
1002 the archive and then the name of the file within the
1003 archive. */
1004
1005 if (file->the_bfd != NULL
1006 && bfd_my_archive (file->the_bfd) != NULL)
1007 {
1008 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1009 fa = true;
1010 }
1011 else
1012 {
1013 fn = file->filename;
1014 fa = false;
1015 }
1016
1017 if (ls->ifile->the_bfd != NULL
1018 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1019 {
1020 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1021 la = true;
1022 }
1023 else
1024 {
1025 ln = ls->ifile->filename;
1026 la = false;
1027 }
1028
1029 i = strcmp (fn, ln);
d2256aeb 1030 if (i > 0)
26c7ca95 1031 continue;
d2256aeb 1032 else if (i < 0)
26c7ca95 1033 break;
6ccce8e8
ILT
1034
1035 if (fa || la)
1036 {
1037 if (fa)
1038 fn = file->filename;
1039 if (la)
1040 ln = ls->ifile->filename;
1041
1042 i = strcmp (fn, ln);
1043 if (i > 0)
1044 continue;
1045 else if (i < 0)
1046 break;
1047 }
26c7ca95
ILT
1048 }
1049
1050 /* Here either the files are not sorted by name, or we are
1051 looking at the sections for this file. */
1052
1053 if (wild->sections_sorted)
1054 {
1055 if (strcmp (section_name,
1056 bfd_get_section_name (ls->ifile->the_bfd,
1057 ls->section))
d2256aeb 1058 < 0)
26c7ca95
ILT
1059 break;
1060 }
1061 }
1062
1063 return l;
1064}
1065
64664e69
ILT
1066/* Expand a wild statement for a particular FILE. SECTION may be
1067 NULL, in which case it is a wild card. */
1068
075d7359 1069static void
8ddef552 1070wild_section (ptr, section, file, output)
309c8153
ILT
1071 lang_wild_statement_type *ptr;
1072 const char *section;
1073 lang_input_statement_type *file;
1074 lang_output_section_statement_type *output;
2fa0b342 1075{
075d7359
SC
1076 if (file->just_syms_flag == false)
1077 {
309c8153 1078 register asection *s;
86bc0974
ILT
1079 boolean wildcard;
1080
1081 if (section == NULL)
1082 wildcard = false;
1083 else
1084 wildcard = wildcardp (section);
309c8153
ILT
1085
1086 for (s = file->the_bfd->sections; s != NULL; s = s->next)
075d7359 1087 {
86bc0974
ILT
1088 boolean match;
1089
309c8153
ILT
1090 /* Attach all sections named SECTION. If SECTION is NULL,
1091 then attach all sections.
1092
1093 Previously, if SECTION was NULL, this code did not call
1094 wild_doit if the SEC_IS_COMMON flag was set for the
1095 section. I did not understand that, and I took it out.
1096 --ian@cygnus.com. */
1097
86bc0974
ILT
1098 if (section == NULL)
1099 match = true;
1100 else
1101 {
1102 const char *name;
1103
1104 name = bfd_get_section_name (file->the_bfd, s);
1105 if (wildcard)
1106 match = fnmatch (section, name, 0) == 0 ? true : false;
1107 else
1108 match = strcmp (section, name) == 0 ? true : false;
1109 }
26c7ca95 1110
86bc0974 1111 if (match)
26c7ca95
ILT
1112 {
1113 lang_statement_union_type *before;
1114
71b012a8
RH
1115 /* If the wild pattern was marked KEEP, the member sections
1116 should be as well. */
1117 if (ptr->keep_sections)
1118 s->flags |= SEC_KEEP;
1119
26c7ca95
ILT
1120 before = wild_sort (ptr, file, s);
1121
1122 /* Here BEFORE points to the lang_input_section which
1123 should follow the one we are about to add. If BEFORE
1124 is NULL, then the section should just go at the end
1125 of the current list. */
1126
1127 if (before == NULL)
1128 wild_doit (&ptr->children, s, output, file);
1129 else
1130 {
1131 lang_statement_list_type list;
1132 lang_statement_union_type **pp;
1133
1134 lang_list_init (&list);
1135 wild_doit (&list, s, output, file);
1136 ASSERT (list.head != NULL && list.head->next == NULL);
1137
1138 for (pp = &ptr->children.head;
1139 *pp != before;
1140 pp = &(*pp)->next)
1141 ASSERT (*pp != NULL);
1142
1143 list.head->next = *pp;
1144 *pp = list.head;
1145 }
1146 }
075d7359 1147 }
2fa0b342 1148 }
2fa0b342
DHW
1149}
1150
64664e69
ILT
1151/* This is passed a file name which must have been seen already and
1152 added to the statement tree. We will see if it has been opened
1153 already and had its symbols read. If not then we'll read it. */
2fa0b342 1154
193c5f93
ILT
1155static lang_input_statement_type *
1156lookup_name (name)
64664e69 1157 const char *name;
2fa0b342
DHW
1158{
1159 lang_input_statement_type *search;
075d7359
SC
1160
1161 for (search = (lang_input_statement_type *) input_file_chain.head;
1162 search != (lang_input_statement_type *) NULL;
1163 search = (lang_input_statement_type *) search->next_real_file)
1164 {
1165 if (search->filename == (char *) NULL && name == (char *) NULL)
c477527c
ILT
1166 return search;
1167 if (search->filename != (char *) NULL
1168 && name != (char *) NULL
1169 && strcmp (search->filename, name) == 0)
1170 break;
075d7359 1171 }
2fa0b342 1172
c477527c 1173 if (search == (lang_input_statement_type *) NULL)
193c5f93
ILT
1174 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1175 false);
c477527c 1176
5e6cd559
ILT
1177 /* If we have already added this file, or this file is not real
1178 (FIXME: can that ever actually happen?) or the name is NULL
1179 (FIXME: can that ever actually happen?) don't add this file. */
193c5f93 1180 if (search->loaded
5e6cd559
ILT
1181 || ! search->real
1182 || search->filename == (const char *) NULL)
1183 return search;
1184
6a02a973 1185 load_symbols (search, (lang_statement_list_type *) NULL);
193c5f93
ILT
1186
1187 return search;
1188}
1189
1190/* Get the symbols for an input file. */
c477527c 1191
193c5f93 1192static void
6a02a973 1193load_symbols (entry, place)
193c5f93 1194 lang_input_statement_type *entry;
6a02a973 1195 lang_statement_list_type *place;
193c5f93 1196{
0b3499f6
ILT
1197 char **matching;
1198
193c5f93
ILT
1199 if (entry->loaded)
1200 return;
1201
1202 ldfile_open_file (entry);
1203
0b3499f6
ILT
1204 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1205 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1206 {
1207 bfd_error_type err;
6a02a973 1208 lang_statement_list_type *hold;
0b3499f6
ILT
1209
1210 err = bfd_get_error ();
1211 if (err == bfd_error_file_ambiguously_recognized)
1212 {
1213 char **p;
1214
26c7ca95
ILT
1215 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1216 einfo (_("%B: matching formats:"), entry->the_bfd);
0b3499f6
ILT
1217 for (p = matching; *p != NULL; p++)
1218 einfo (" %s", *p);
1219 einfo ("%F\n");
1220 }
6a02a973
ILT
1221 else if (err != bfd_error_file_not_recognized
1222 || place == NULL)
26c7ca95 1223 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
0b3499f6 1224
0b3499f6
ILT
1225 bfd_close (entry->the_bfd);
1226 entry->the_bfd = NULL;
1227
64664e69
ILT
1228 /* See if the emulation has some special knowledge. */
1229
1230 if (ldemul_unrecognized_file (entry))
1231 return;
1232
1233 /* Try to interpret the file as a linker script. */
1234
0b3499f6
ILT
1235 ldfile_open_command_file (entry->filename);
1236
6a02a973
ILT
1237 hold = stat_ptr;
1238 stat_ptr = place;
1239
0b3499f6
ILT
1240 ldfile_assumed_script = true;
1241 parser_input = input_script;
1242 yyparse ();
1243 ldfile_assumed_script = false;
1244
6a02a973
ILT
1245 stat_ptr = hold;
1246
0b3499f6
ILT
1247 return;
1248 }
1249
1250 /* We don't call ldlang_add_file for an archive. Instead, the
1251 add_symbols entry point will call ldlang_add_file, via the
1252 add_archive_element callback, for each element of the archive
1253 which is used. */
f400bbbb 1254 switch (bfd_get_format (entry->the_bfd))
5e6cd559 1255 {
f400bbbb
ILT
1256 default:
1257 break;
1258
1259 case bfd_object:
193c5f93 1260 ldlang_add_file (entry);
5e6cd559 1261 if (trace_files || trace_file_tries)
193c5f93 1262 info_msg ("%I\n", entry);
f400bbbb
ILT
1263 break;
1264
1265 case bfd_archive:
3c8deccc 1266 if (entry->whole_archive)
f400bbbb
ILT
1267 {
1268 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1269 (bfd *) NULL);
1270 while (member != NULL)
1271 {
1272 if (! bfd_check_format (member, bfd_object))
26c7ca95 1273 einfo (_("%F%B: object %B in archive is not object\n"),
f400bbbb
ILT
1274 entry->the_bfd, member);
1275 if (! ((*link_info.callbacks->add_archive_element)
0cacbcbe 1276 (&link_info, member, "--whole-archive")))
f400bbbb
ILT
1277 abort ();
1278 if (! bfd_link_add_symbols (member, &link_info))
26c7ca95 1279 einfo (_("%F%B: could not read symbols: %E\n"), member);
f400bbbb
ILT
1280 member = bfd_openr_next_archived_file (entry->the_bfd,
1281 member);
1282 }
1283
1284 entry->loaded = true;
1285
1286 return;
1287 }
5e6cd559 1288 }
1418c83b 1289
0b3499f6 1290 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
26c7ca95 1291 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
5e6cd559 1292
193c5f93 1293 entry->loaded = true;
2fa0b342
DHW
1294}
1295
86bc0974
ILT
1296/* Handle a wild statement for a single file F. */
1297
1298static void
1299wild_file (s, section, f, output)
1300 lang_wild_statement_type *s;
1301 const char *section;
1302 lang_input_statement_type *f;
1303 lang_output_section_statement_type *output;
1304{
1305 if (f->the_bfd == NULL
1306 || ! bfd_check_format (f->the_bfd, bfd_archive))
1307 wild_section (s, section, f, output);
1308 else
1309 {
1310 bfd *member;
1311
1312 /* This is an archive file. We must map each member of the
1313 archive separately. */
1314 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1315 while (member != NULL)
1316 {
1317 /* When lookup_name is called, it will call the add_symbols
1318 entry point for the archive. For each element of the
1319 archive which is included, BFD will call ldlang_add_file,
1320 which will set the usrdata field of the member to the
1321 lang_input_statement. */
1322 if (member->usrdata != NULL)
1323 {
1324 wild_section (s, section,
1325 (lang_input_statement_type *) member->usrdata,
1326 output);
1327 }
1328
1329 member = bfd_openr_next_archived_file (f->the_bfd, member);
1330 }
1331 }
1332}
1333
64664e69
ILT
1334/* Handle a wild statement. SECTION or FILE or both may be NULL,
1335 indicating that it is a wildcard. Separate lang_input_section
1336 statements are created for each part of the expansion; they are
1337 added after the wild statement S. OUTPUT is the output section. */
1338
2fa0b342 1339static void
8ddef552 1340wild (s, section, file, target, output)
64664e69
ILT
1341 lang_wild_statement_type *s;
1342 const char *section;
1343 const char *file;
1344 const char *target;
1345 lang_output_section_statement_type *output;
2fa0b342
DHW
1346{
1347 lang_input_statement_type *f;
075d7359
SC
1348
1349 if (file == (char *) NULL)
1350 {
1351 /* Perform the iteration over all files in the list */
1352 for (f = (lang_input_statement_type *) file_chain.head;
1353 f != (lang_input_statement_type *) NULL;
1354 f = (lang_input_statement_type *) f->next)
1355 {
86bc0974
ILT
1356 wild_file (s, section, f, output);
1357 }
1358 }
1359 else if (wildcardp (file))
1360 {
1361 for (f = (lang_input_statement_type *) file_chain.head;
1362 f != (lang_input_statement_type *) NULL;
1363 f = (lang_input_statement_type *) f->next)
1364 {
1365 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1366 wild_file (s, section, f, output);
075d7359
SC
1367 }
1368 }
1369 else
1370 {
1371 /* Perform the iteration over a single file */
3d0c8fa1 1372 f = lookup_name (file);
86bc0974 1373 wild_file (s, section, f, output);
075d7359 1374 }
64664e69 1375
075d7359
SC
1376 if (section != (char *) NULL
1377 && strcmp (section, "COMMON") == 0
64664e69 1378 && default_common_section == NULL)
075d7359 1379 {
64664e69
ILT
1380 /* Remember the section that common is going to in case we later
1381 get something which doesn't know where to put it. */
075d7359 1382 default_common_section = output;
2fa0b342 1383 }
2fa0b342
DHW
1384}
1385
64664e69 1386/* Open the output file. */
97fbbaca 1387
075d7359 1388static bfd *
8ddef552 1389open_output (name)
64664e69 1390 const char *name;
2fa0b342 1391{
097879bc 1392 bfd *output;
097879bc 1393
075d7359 1394 if (output_target == (char *) NULL)
ae475b39
SC
1395 {
1396 if (current_target != (char *) NULL)
1397 output_target = current_target;
1398 else
1399 output_target = default_target;
1400 }
075d7359 1401 output = bfd_openw (name, output_target);
075d7359
SC
1402
1403 if (output == (bfd *) NULL)
1404 {
5bcb7f28 1405 if (bfd_get_error () == bfd_error_invalid_target)
ae475b39 1406 {
26c7ca95 1407 einfo (_("%P%F: target %s not found\n"), output_target);
ae475b39 1408 }
26c7ca95 1409 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
075d7359 1410 }
30d1a390 1411
86bc0974 1412 delete_output_file_on_failure = true;
97fbbaca 1413
30d1a390 1414 /* output->flags |= D_PAGED;*/
075d7359 1415
ddddcdf0 1416 if (! bfd_set_format (output, bfd_object))
26c7ca95 1417 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
ddddcdf0
ILT
1418 if (! bfd_set_arch_mach (output,
1419 ldfile_output_architecture,
1420 ldfile_output_machine))
26c7ca95 1421 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
ddddcdf0 1422
c477527c
ILT
1423 link_info.hash = bfd_link_hash_table_create (output);
1424 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
26c7ca95 1425 einfo (_("%P%F: can not create link hash table: %E\n"));
c477527c 1426
1b8a42f3 1427 bfd_set_gp_size (output, g_switch_value);
2fa0b342
DHW
1428 return output;
1429}
1418c83b
SC
1430
1431
097879bc 1432
1418c83b 1433
2fa0b342 1434static void
8ddef552
DM
1435ldlang_open_output (statement)
1436 lang_statement_union_type * statement;
2fa0b342 1437{
075d7359
SC
1438 switch (statement->header.type)
1439 {
c477527c
ILT
1440 case lang_output_statement_enum:
1441 ASSERT (output_bfd == (bfd *) NULL);
075d7359
SC
1442 output_bfd = open_output (statement->output_statement.name);
1443 ldemul_set_output_arch ();
c477527c 1444 if (config.magic_demand_paged && !link_info.relocateable)
075d7359
SC
1445 output_bfd->flags |= D_PAGED;
1446 else
1447 output_bfd->flags &= ~D_PAGED;
1448 if (config.text_read_only)
1449 output_bfd->flags |= WP_TEXT;
1450 else
1451 output_bfd->flags &= ~WP_TEXT;
6799c638 1452 if (link_info.traditional_format)
c96386c4
ILT
1453 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1454 else
1455 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
075d7359 1456 break;
1418c83b 1457
075d7359
SC
1458 case lang_target_statement_enum:
1459 current_target = statement->target_statement.target;
1460 break;
1461 default:
1462 break;
1463 }
1418c83b 1464}
2fa0b342 1465
309c8153
ILT
1466/* Open all the input files. */
1467
1418c83b 1468static void
309c8153
ILT
1469open_input_bfds (s, force)
1470 lang_statement_union_type *s;
1471 boolean force;
1418c83b 1472{
309c8153 1473 for (; s != (lang_statement_union_type *) NULL; s = s->next)
075d7359 1474 {
309c8153 1475 switch (s->header.type)
075d7359 1476 {
309c8153
ILT
1477 case lang_constructors_statement_enum:
1478 open_input_bfds (constructor_list.head, force);
1479 break;
1480 case lang_output_section_statement_enum:
1481 open_input_bfds (s->output_section_statement.children.head, force);
1482 break;
1483 case lang_wild_statement_enum:
1484 /* Maybe we should load the file's symbols */
86bc0974
ILT
1485 if (s->wild_statement.filename
1486 && ! wildcardp (s->wild_statement.filename))
309c8153
ILT
1487 (void) lookup_name (s->wild_statement.filename);
1488 open_input_bfds (s->wild_statement.children.head, force);
1489 break;
1490 case lang_group_statement_enum:
1491 {
1492 struct bfd_link_hash_entry *undefs;
1493
1494 /* We must continually search the entries in the group
1495 until no new symbols are added to the list of undefined
1496 symbols. */
1497
1498 do
1499 {
1500 undefs = link_info.hash->undefs_tail;
1501 open_input_bfds (s->group_statement.children.head, true);
1502 }
1503 while (undefs != link_info.hash->undefs_tail);
1504 }
1505 break;
1506 case lang_target_statement_enum:
1507 current_target = s->target_statement.target;
1508 break;
1509 case lang_input_statement_enum:
1510 if (s->input_statement.real == true)
1511 {
6a02a973
ILT
1512 lang_statement_list_type add;
1513
309c8153
ILT
1514 s->input_statement.target = current_target;
1515
1516 /* If we are being called from within a group, and this
1517 is an archive which has already been searched, then
1518 force it to be researched. */
1519 if (force
1520 && s->input_statement.loaded
1521 && bfd_check_format (s->input_statement.the_bfd,
1522 bfd_archive))
1523 s->input_statement.loaded = false;
1524
6a02a973
ILT
1525 lang_list_init (&add);
1526
1527 load_symbols (&s->input_statement, &add);
1528
1529 if (add.head != NULL)
1530 {
1531 *add.tail = s->next;
1532 s->next = add.head;
1533 }
309c8153
ILT
1534 }
1535 break;
1536 default:
1537 break;
075d7359 1538 }
075d7359 1539 }
2fa0b342 1540}
075d7359 1541
2fa0b342
DHW
1542/* If there are [COMMONS] statements, put a wild one into the bss section */
1543
1544static void
075d7359 1545lang_reasonable_defaults ()
2fa0b342 1546{
1418c83b 1547#if 0
075d7359
SC
1548 lang_output_section_statement_lookup (".text");
1549 lang_output_section_statement_lookup (".data");
8cb5eb31 1550
075d7359
SC
1551 default_common_section =
1552 lang_output_section_statement_lookup (".bss");
8cb5eb31 1553
1418c83b 1554
075d7359
SC
1555 if (placed_commons == false)
1556 {
1557 lang_wild_statement_type *new =
1558 new_stat (lang_wild_statement,
1559 &default_common_section->children);
1560
1561 new->section_name = "COMMON";
1562 new->filename = (char *) NULL;
1563 lang_list_init (&new->children);
1564 }
1418c83b 1565#endif
8cb5eb31 1566
2fa0b342
DHW
1567}
1568
1418c83b
SC
1569/*
1570 Add the supplied name to the symbol table as an undefined reference.
1571 Remove items from the chain as we open input bfds
1572 */
075d7359
SC
1573typedef struct ldlang_undef_chain_list
1574{
f177a611 1575 struct ldlang_undef_chain_list *next;
1418c83b 1576 char *name;
075d7359 1577} ldlang_undef_chain_list_type;
1418c83b
SC
1578
1579static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1580
1581void
8ddef552
DM
1582ldlang_add_undef (name)
1583 CONST char *CONST name;
2fa0b342 1584{
1418c83b 1585 ldlang_undef_chain_list_type *new =
1d169acc
ILT
1586 ((ldlang_undef_chain_list_type *)
1587 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1418c83b
SC
1588
1589 new->next = ldlang_undef_chain_list_head;
1590 ldlang_undef_chain_list_head = new;
1591
075d7359 1592 new->name = buystring (name);
1418c83b 1593}
075d7359 1594
1418c83b
SC
1595/* Run through the list of undefineds created above and place them
1596 into the linker hash table as undefined symbols belonging to the
1597 script file.
1598*/
1599static void
8ddef552 1600lang_place_undefineds ()
1418c83b 1601{
c477527c 1602 ldlang_undef_chain_list_type *ptr;
1418c83b 1603
c477527c
ILT
1604 for (ptr = ldlang_undef_chain_list_head;
1605 ptr != (ldlang_undef_chain_list_type *) NULL;
1606 ptr = ptr->next)
075d7359 1607 {
c477527c 1608 struct bfd_link_hash_entry *h;
075d7359 1609
c477527c
ILT
1610 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1611 if (h == (struct bfd_link_hash_entry *) NULL)
26c7ca95 1612 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
c477527c
ILT
1613 if (h->type == bfd_link_hash_new)
1614 {
1615 h->type = bfd_link_hash_undefined;
1616 h->u.undef.abfd = NULL;
1617 bfd_link_add_undef (link_info.hash, h);
1618 }
075d7359
SC
1619 }
1620}
1418c83b 1621
2fa0b342
DHW
1622/* Open input files and attatch to output sections */
1623static void
8ddef552
DM
1624map_input_to_output_sections (s, target, output_section_statement)
1625 lang_statement_union_type * s;
1626 CONST char *target;
1627 lang_output_section_statement_type * output_section_statement;
2fa0b342 1628{
075d7359 1629 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 1630 {
075d7359 1631 switch (s->header.type)
2fa0b342 1632 {
075d7359
SC
1633
1634
1635 case lang_wild_statement_enum:
1636 wild (&s->wild_statement, s->wild_statement.section_name,
1637 s->wild_statement.filename, target,
1638 output_section_statement);
1639
1640 break;
1641 case lang_constructors_statement_enum:
1642 map_input_to_output_sections (constructor_list.head,
1643 target,
1644 output_section_statement);
1645 break;
1646 case lang_output_section_statement_enum:
1647 map_input_to_output_sections (s->output_section_statement.children.head,
1648 target,
1649 &s->output_section_statement);
1650 break;
1651 case lang_output_statement_enum:
1652 break;
1653 case lang_target_statement_enum:
1654 target = s->target_statement.target;
1655 break;
309c8153
ILT
1656 case lang_group_statement_enum:
1657 map_input_to_output_sections (s->group_statement.children.head,
1658 target,
1659 output_section_statement);
1660 break;
075d7359
SC
1661 case lang_fill_statement_enum:
1662 case lang_input_section_enum:
1663 case lang_object_symbols_statement_enum:
1664 case lang_data_statement_enum:
4fdbafb2 1665 case lang_reloc_statement_enum:
075d7359 1666 case lang_padding_statement_enum:
f9d3d71a
ILT
1667 case lang_input_statement_enum:
1668 if (output_section_statement != NULL
1669 && output_section_statement->bfd_section == NULL)
1670 init_os (output_section_statement);
075d7359 1671 break;
86bc0974
ILT
1672 case lang_assignment_statement_enum:
1673 if (output_section_statement != NULL
1674 && output_section_statement->bfd_section == NULL)
1675 init_os (output_section_statement);
1676
1677 /* Make sure that any sections mentioned in the assignment
1678 are initialized. */
1679 exp_init_os (s->assignment_statement.exp);
1680 break;
075d7359
SC
1681 case lang_afile_asection_pair_statement_enum:
1682 FAIL ();
1683 break;
1684 case lang_address_statement_enum:
1685 /* Mark the specified section with the supplied address */
1686 {
1687 lang_output_section_statement_type *os =
ef01a8a8
ILT
1688 lang_output_section_statement_lookup
1689 (s->address_statement.section_name);
075d7359 1690
ef01a8a8
ILT
1691 if (os->bfd_section == NULL)
1692 init_os (os);
075d7359 1693 os->addr_tree = s->address_statement.address;
48491e2e 1694 }
075d7359 1695 break;
2fa0b342 1696 }
2fa0b342
DHW
1697 }
1698}
1699
075d7359 1700static void
8ddef552
DM
1701print_output_section_statement (output_section_statement)
1702 lang_output_section_statement_type * output_section_statement;
2fa0b342
DHW
1703{
1704 asection *section = output_section_statement->bfd_section;
0cacbcbe 1705 int len;
075d7359 1706
0cacbcbe
ILT
1707 if (output_section_statement != abs_output_section)
1708 {
1709 minfo ("\n%s", output_section_statement->name);
075d7359 1710
0cacbcbe
ILT
1711 if (section != NULL)
1712 {
1713 print_dot = section->vma;
e20873a7 1714
0cacbcbe
ILT
1715 len = strlen (output_section_statement->name);
1716 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1717 {
1718 print_nl ();
1719 len = 0;
1720 }
1721 while (len < SECTION_NAME_MAP_LENGTH)
1722 {
1723 print_space ();
1724 ++len;
1725 }
2fa0b342 1726
0cacbcbe
ILT
1727 minfo ("0x%V %W", section->vma, section->_raw_size);
1728
1729 if (output_section_statement->load_base != NULL)
1730 {
1731 bfd_vma addr;
1732
1733 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1734 "load base", lang_final_phase_enum);
26c7ca95 1735 minfo (_(" load address 0x%V"), addr);
0cacbcbe
ILT
1736 }
1737 }
1738
1739 print_nl ();
e20873a7 1740 }
0cacbcbe 1741
708f7fbc
ILT
1742 print_statement_list (output_section_statement->children.head,
1743 output_section_statement);
2fa0b342
DHW
1744}
1745
075d7359 1746static void
8ddef552
DM
1747print_assignment (assignment, output_section)
1748 lang_assignment_statement_type * assignment;
1749 lang_output_section_statement_type * output_section;
2fa0b342 1750{
0cacbcbe 1751 int i;
2fa0b342 1752 etree_value_type result;
075d7359 1753
0cacbcbe
ILT
1754 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1755 print_space ();
075d7359 1756
0cacbcbe
ILT
1757 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1758 lang_final_phase_enum, print_dot, &print_dot);
075d7359 1759 if (result.valid)
0cacbcbe 1760 minfo ("0x%V", result.value + result.section->bfd_section->vma);
075d7359
SC
1761 else
1762 {
0cacbcbe
ILT
1763 minfo ("*undef* ");
1764#ifdef BFD64
1765 minfo (" ");
1766#endif
075d7359 1767 }
0cacbcbe
ILT
1768
1769 minfo (" ");
1770
075d7359
SC
1771 exp_print_tree (assignment->exp);
1772
0cacbcbe 1773 print_nl ();
2fa0b342
DHW
1774}
1775
1776static void
8ddef552
DM
1777print_input_statement (statm)
1778 lang_input_statement_type * statm;
2fa0b342 1779{
075d7359
SC
1780 if (statm->filename != (char *) NULL)
1781 {
1782 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1783 }
2fa0b342
DHW
1784}
1785
0cacbcbe
ILT
1786/* Print all symbols defined in a particular section. This is called
1787 via bfd_link_hash_traverse. */
804c8601
SC
1788
1789static boolean
1790print_one_symbol (hash_entry, ptr)
0cacbcbe
ILT
1791 struct bfd_link_hash_entry *hash_entry;
1792 PTR ptr;
2fa0b342 1793{
0cacbcbe 1794 asection *sec = (asection *) ptr;
804c8601 1795
0cacbcbe
ILT
1796 if ((hash_entry->type == bfd_link_hash_defined
1797 || hash_entry->type == bfd_link_hash_defweak)
1798 && sec == hash_entry->u.def.section)
804c8601 1799 {
0cacbcbe
ILT
1800 int i;
1801
1802 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1803 print_space ();
1804 minfo ("0x%V ",
1805 (hash_entry->u.def.value
1806 + hash_entry->u.def.section->output_offset
1807 + hash_entry->u.def.section->output_section->vma));
1808
1809 minfo (" %T\n", hash_entry->root.string);
804c8601 1810 }
09a5aa5e
KR
1811
1812 return true;
2fa0b342 1813}
1418c83b 1814
0cacbcbe
ILT
1815/* Print information about an input section to the map file. */
1816
075d7359 1817static void
8ddef552
DM
1818print_input_section (in)
1819 lang_input_section_type * in;
2fa0b342
DHW
1820{
1821 asection *i = in->section;
f078dc7c 1822 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1418c83b 1823
075d7359
SC
1824 if (size != 0)
1825 {
0cacbcbe
ILT
1826 print_space ();
1827
1828 minfo ("%s", i->name);
1829
1830 if (i->output_section != NULL)
075d7359 1831 {
0cacbcbe 1832 int len;
2fa0b342 1833
0cacbcbe
ILT
1834 len = 1 + strlen (i->name);
1835 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1836 {
1837 print_nl ();
1838 len = 0;
1839 }
1840 while (len < SECTION_NAME_MAP_LENGTH)
1841 {
1842 print_space ();
1843 ++len;
1844 }
2fa0b342 1845
0cacbcbe
ILT
1846 minfo ("0x%V %W %B\n",
1847 i->output_section->vma + i->output_offset, size,
1848 i->owner);
2fa0b342 1849
0cacbcbe
ILT
1850 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1851 {
1852 len = SECTION_NAME_MAP_LENGTH + 3;
1853#ifdef BFD64
1854 len += 16;
1855#else
1856 len += 8;
1857#endif
1858 while (len > 0)
075d7359 1859 {
0cacbcbe
ILT
1860 print_space ();
1861 --len;
075d7359 1862 }
2fa0b342 1863
26c7ca95 1864 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
075d7359
SC
1865 }
1866
0cacbcbe 1867 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
075d7359 1868
0cacbcbe 1869 print_dot = i->output_section->vma + i->output_offset + size;
075d7359
SC
1870 }
1871 }
1872}
2fa0b342
DHW
1873
1874static void
8ddef552
DM
1875print_fill_statement (fill)
1876 lang_fill_statement_type * fill;
075d7359 1877{
0cacbcbe 1878 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
075d7359
SC
1879}
1880
1881static void
8ddef552
DM
1882print_data_statement (data)
1883 lang_data_statement_type * data;
2fa0b342 1884{
0cacbcbe
ILT
1885 int i;
1886 bfd_vma addr;
1887 bfd_size_type size;
1888 const char *name;
1889
1890 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1891 print_space ();
1892
1893 addr = data->output_vma;
1894 if (data->output_section != NULL)
1895 addr += data->output_section->vma;
2fa0b342 1896
075d7359
SC
1897 switch (data->type)
1898 {
0cacbcbe
ILT
1899 default:
1900 abort ();
075d7359 1901 case BYTE:
0cacbcbe
ILT
1902 size = BYTE_SIZE;
1903 name = "BYTE";
075d7359
SC
1904 break;
1905 case SHORT:
0cacbcbe
ILT
1906 size = SHORT_SIZE;
1907 name = "SHORT";
075d7359
SC
1908 break;
1909 case LONG:
0cacbcbe
ILT
1910 size = LONG_SIZE;
1911 name = "LONG";
075d7359 1912 break;
c477527c 1913 case QUAD:
0cacbcbe
ILT
1914 size = QUAD_SIZE;
1915 name = "QUAD";
c477527c 1916 break;
9f19768a
ILT
1917 case SQUAD:
1918 size = QUAD_SIZE;
1919 name = "SQUAD";
1920 break;
075d7359
SC
1921 }
1922
0cacbcbe
ILT
1923 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1924
1925 if (data->exp->type.node_class != etree_value)
1926 {
1927 print_space ();
1928 exp_print_tree (data->exp);
1929 }
1930
1931 print_nl ();
1932
1933 print_dot = addr + size;
1934}
1935
1936/* Print an address statement. These are generated by options like
1937 -Ttext. */
2fa0b342 1938
0cacbcbe
ILT
1939static void
1940print_address_statement (address)
1941 lang_address_statement_type *address;
1942{
26c7ca95 1943 minfo (_("Address of section %s set to "), address->section_name);
0cacbcbe
ILT
1944 exp_print_tree (address->address);
1945 print_nl ();
2fa0b342
DHW
1946}
1947
4fdbafb2
ILT
1948/* Print a reloc statement. */
1949
1950static void
1951print_reloc_statement (reloc)
1952 lang_reloc_statement_type *reloc;
1953{
0cacbcbe
ILT
1954 int i;
1955 bfd_vma addr;
1956 bfd_size_type size;
1957
1958 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1959 print_space ();
4fdbafb2 1960
0cacbcbe
ILT
1961 addr = reloc->output_vma;
1962 if (reloc->output_section != NULL)
1963 addr += reloc->output_section->vma;
4fdbafb2 1964
0cacbcbe 1965 size = bfd_get_reloc_size (reloc->howto);
4fdbafb2 1966
0cacbcbe 1967 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
4fdbafb2 1968
0cacbcbe
ILT
1969 if (reloc->name != NULL)
1970 minfo ("%s+", reloc->name);
1971 else
1972 minfo ("%s+", reloc->section->name);
4fdbafb2
ILT
1973
1974 exp_print_tree (reloc->addend_exp);
1975
0cacbcbe
ILT
1976 print_nl ();
1977
1978 print_dot = addr + size;
4fdbafb2 1979}
2fa0b342
DHW
1980
1981static void
8ddef552 1982print_padding_statement (s)
0cacbcbe 1983 lang_padding_statement_type *s;
075d7359 1984{
0cacbcbe
ILT
1985 int len;
1986 bfd_vma addr;
1987
1988 minfo (" *fill*");
1989
1990 len = sizeof " *fill*" - 1;
1991 while (len < SECTION_NAME_MAP_LENGTH)
1992 {
1993 print_space ();
1994 ++len;
1995 }
1996
1997 addr = s->output_offset;
1998 if (s->output_section != NULL)
1999 addr += s->output_section->vma;
2000 minfo ("0x%V %W", addr, s->size);
2001
2002 if (s->fill != 0)
86bc0974 2003 minfo (" %u", s->fill);
0cacbcbe 2004
075d7359 2005 print_nl ();
ffc50032 2006
0cacbcbe 2007 print_dot = addr + s->size;
2fa0b342
DHW
2008}
2009
075d7359 2010static void
8ddef552
DM
2011print_wild_statement (w, os)
2012 lang_wild_statement_type * w;
2013 lang_output_section_statement_type * os;
2fa0b342 2014{
0cacbcbe
ILT
2015 print_space ();
2016
26c7ca95
ILT
2017 if (w->filenames_sorted)
2018 minfo ("SORT(");
0cacbcbe
ILT
2019 if (w->filename != NULL)
2020 minfo ("%s", w->filename);
075d7359 2021 else
0cacbcbe 2022 minfo ("*");
26c7ca95
ILT
2023 if (w->filenames_sorted)
2024 minfo (")");
0cacbcbe 2025
26c7ca95
ILT
2026 minfo ("(");
2027 if (w->sections_sorted)
2028 minfo ("SORT(");
0cacbcbe 2029 if (w->section_name != NULL)
26c7ca95 2030 minfo ("%s", w->section_name);
075d7359 2031 else
26c7ca95
ILT
2032 minfo ("*");
2033 if (w->sections_sorted)
2034 minfo (")");
2035 minfo (")");
0cacbcbe 2036
075d7359 2037 print_nl ();
2fa0b342 2038
0cacbcbe 2039 print_statement_list (w->children.head, os);
2fa0b342 2040}
309c8153
ILT
2041
2042/* Print a group statement. */
2043
2044static void
2045print_group (s, os)
2046 lang_group_statement_type *s;
2047 lang_output_section_statement_type *os;
2048{
2049 fprintf (config.map_file, "START GROUP\n");
708f7fbc 2050 print_statement_list (s->children.head, os);
309c8153
ILT
2051 fprintf (config.map_file, "END GROUP\n");
2052}
2053
708f7fbc
ILT
2054/* Print the list of statements in S.
2055 This can be called for any statement type. */
2056
2fa0b342 2057static void
708f7fbc 2058print_statement_list (s, os)
0cacbcbe
ILT
2059 lang_statement_union_type *s;
2060 lang_output_section_statement_type *os;
2fa0b342 2061{
0cacbcbe 2062 while (s != NULL)
c611e285 2063 {
708f7fbc 2064 print_statement (s, os);
075d7359 2065 s = s->next;
2fa0b342 2066 }
2fa0b342
DHW
2067}
2068
708f7fbc
ILT
2069/* Print the first statement in statement list S.
2070 This can be called for any statement type. */
2071
2072static void
2073print_statement (s, os)
0cacbcbe
ILT
2074 lang_statement_union_type *s;
2075 lang_output_section_statement_type *os;
708f7fbc
ILT
2076{
2077 switch (s->header.type)
2078 {
0cacbcbe 2079 default:
26c7ca95 2080 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
0cacbcbe
ILT
2081 FAIL ();
2082 break;
708f7fbc 2083 case lang_constructors_statement_enum:
0cacbcbe
ILT
2084 if (constructor_list.head != NULL)
2085 {
2086 minfo (" CONSTRUCTORS\n");
2087 print_statement_list (constructor_list.head, os);
2088 }
708f7fbc
ILT
2089 break;
2090 case lang_wild_statement_enum:
2091 print_wild_statement (&s->wild_statement, os);
2092 break;
708f7fbc 2093 case lang_address_statement_enum:
0cacbcbe 2094 print_address_statement (&s->address_statement);
708f7fbc
ILT
2095 break;
2096 case lang_object_symbols_statement_enum:
0cacbcbe 2097 minfo (" CREATE_OBJECT_SYMBOLS\n");
708f7fbc
ILT
2098 break;
2099 case lang_fill_statement_enum:
2100 print_fill_statement (&s->fill_statement);
2101 break;
2102 case lang_data_statement_enum:
2103 print_data_statement (&s->data_statement);
2104 break;
2105 case lang_reloc_statement_enum:
2106 print_reloc_statement (&s->reloc_statement);
2107 break;
2108 case lang_input_section_enum:
2109 print_input_section (&s->input_section);
2110 break;
2111 case lang_padding_statement_enum:
2112 print_padding_statement (&s->padding_statement);
2113 break;
2114 case lang_output_section_statement_enum:
2115 print_output_section_statement (&s->output_section_statement);
2116 break;
2117 case lang_assignment_statement_enum:
0cacbcbe 2118 print_assignment (&s->assignment_statement, os);
708f7fbc
ILT
2119 break;
2120 case lang_target_statement_enum:
2121 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2122 break;
2123 case lang_output_statement_enum:
0cacbcbe
ILT
2124 minfo ("OUTPUT(%s", s->output_statement.name);
2125 if (output_target != NULL)
2126 minfo (" %s", output_target);
2127 minfo (")\n");
708f7fbc
ILT
2128 break;
2129 case lang_input_statement_enum:
2130 print_input_statement (&s->input_statement);
2131 break;
2132 case lang_group_statement_enum:
2133 print_group (&s->group_statement, os);
2134 break;
2135 case lang_afile_asection_pair_statement_enum:
2136 FAIL ();
2137 break;
2138 }
2139}
2140
2fa0b342 2141static void
8ddef552 2142print_statements ()
2fa0b342 2143{
0cacbcbe 2144 print_statement_list (statement_list.head, abs_output_section);
2fa0b342
DHW
2145}
2146
708f7fbc
ILT
2147/* Print the first N statements in statement list S to STDERR.
2148 If N == 0, nothing is printed.
2149 If N < 0, the entire list is printed.
2150 Intended to be called from GDB. */
2151
2152void
2153dprint_statement (s, n)
2154 lang_statement_union_type * s;
2155 int n;
2156{
2157 FILE *map_save = config.map_file;
2158
2159 config.map_file = stderr;
2160
2161 if (n < 0)
2162 print_statement_list (s, abs_output_section);
2163 else
2164 {
86bc0974 2165 while (s && --n >= 0)
708f7fbc
ILT
2166 {
2167 print_statement (s, abs_output_section);
2168 s = s->next;
2169 }
2170 }
2171
2172 config.map_file = map_save;
2173}
2174
2fa0b342 2175static bfd_vma
9f629407
ILT
2176insert_pad (this_ptr, fill, power, output_section_statement, dot)
2177 lang_statement_union_type ** this_ptr;
2178 fill_type fill;
2179 unsigned int power;
2180 asection * output_section_statement;
2181 bfd_vma dot;
075d7359
SC
2182{
2183 /* Align this section first to the
2fa0b342
DHW
2184 input sections requirement, then
2185 to the output section's requirement.
075d7359 2186 If this alignment is > than any seen before,
2fa0b342
DHW
2187 then record it too. Perform the alignment by
2188 inserting a magic 'padding' statement.
2189 */
2190
075d7359 2191 unsigned int alignment_needed = align_power (dot, power) - dot;
2fa0b342 2192
075d7359 2193 if (alignment_needed != 0)
2fa0b342 2194 {
075d7359 2195 lang_statement_union_type *new =
1d169acc
ILT
2196 ((lang_statement_union_type *)
2197 stat_alloc (sizeof (lang_padding_statement_type)));
075d7359 2198
2fa0b342
DHW
2199 /* Link into existing chain */
2200 new->header.next = *this_ptr;
2201 *this_ptr = new;
2202 new->header.type = lang_padding_statement_enum;
2203 new->padding_statement.output_section = output_section_statement;
2204 new->padding_statement.output_offset =
2205 dot - output_section_statement->vma;
2206 new->padding_statement.fill = fill;
2207 new->padding_statement.size = alignment_needed;
2208 }
2209
2210
2211 /* Remember the most restrictive alignment */
075d7359
SC
2212 if (power > output_section_statement->alignment_power)
2213 {
2214 output_section_statement->alignment_power = power;
2215 }
c611e285 2216 output_section_statement->_raw_size += alignment_needed;
2fa0b342
DHW
2217 return alignment_needed + dot;
2218
2219}
2220
1418c83b 2221/* Work out how much this section will move the dot point */
075d7359 2222static bfd_vma
9f629407
ILT
2223size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2224 lang_statement_union_type ** this_ptr;
2225 lang_output_section_statement_type * output_section_statement;
c477527c 2226 fill_type fill;
9f629407
ILT
2227 bfd_vma dot;
2228 boolean relax;
2fa0b342
DHW
2229{
2230 lang_input_section_type *is = &((*this_ptr)->input_section);
2231 asection *i = is->section;
2fa0b342 2232
075d7359
SC
2233 if (is->ifile->just_syms_flag == false)
2234 {
e20873a7
JG
2235 if (output_section_statement->subsection_alignment != -1)
2236 i->alignment_power =
2237 output_section_statement->subsection_alignment;
2238
075d7359
SC
2239 dot = insert_pad (this_ptr, fill, i->alignment_power,
2240 output_section_statement->bfd_section, dot);
2241
075d7359 2242 /* Remember where in the output section this input section goes */
ac004870 2243
075d7359
SC
2244 i->output_offset = dot - output_section_statement->bfd_section->vma;
2245
ae475b39
SC
2246 /* Mark how big the output section must be to contain this now
2247 */
f078dc7c
ILT
2248 if (i->_cooked_size != 0)
2249 dot += i->_cooked_size;
ae475b39 2250 else
f078dc7c 2251 dot += i->_raw_size;
ae475b39 2252 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
075d7359 2253 }
ac004870 2254 else
075d7359
SC
2255 {
2256 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2257 }
2fa0b342 2258
075d7359 2259 return dot;
2fa0b342
DHW
2260}
2261
193c5f93
ILT
2262/* This variable indicates whether bfd_relax_section should be called
2263 again. */
2264
2265static boolean relax_again;
2266
2267/* Set the sizes for all the output sections. */
c611e285 2268
a62494c4 2269bfd_vma
9f629407
ILT
2270lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2271 lang_statement_union_type * s;
2272 lang_output_section_statement_type * output_section_statement;
2273 lang_statement_union_type ** prev;
c477527c 2274 fill_type fill;
9f629407
ILT
2275 bfd_vma dot;
2276 boolean relax;
c611e285
SC
2277{
2278 /* Size up the sections from their constituent parts */
075d7359 2279 for (; s != (lang_statement_union_type *) NULL; s = s->next)
ae475b39
SC
2280 {
2281 switch (s->header.type)
075d7359 2282 {
c611e285 2283
ae475b39
SC
2284 case lang_output_section_statement_enum:
2285 {
2286 bfd_vma after;
2287 lang_output_section_statement_type *os = &s->output_section_statement;
2288
f9d3d71a
ILT
2289 if (os->bfd_section == NULL)
2290 {
2291 /* This section was never actually created. */
2292 break;
2293 }
2294
27f7237e
ILT
2295 /* If this is a COFF shared library section, use the size and
2296 address from the input section. FIXME: This is COFF
2297 specific; it would be cleaner if there were some other way
2298 to do this, but nothing simple comes to mind. */
2299 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2300 {
2301 asection *input;
2302
2303 if (os->children.head == NULL
2304 || os->children.head->next != NULL
2305 || os->children.head->header.type != lang_input_section_enum)
26c7ca95 2306 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
27f7237e
ILT
2307 os->name);
2308
2309 input = os->children.head->input_section.section;
2310 bfd_set_section_vma (os->bfd_section->owner,
2311 os->bfd_section,
2312 bfd_section_vma (input->owner, input));
2313 os->bfd_section->_raw_size = input->_raw_size;
2314 break;
2315 }
e9b63852 2316
686739e2 2317 if (bfd_is_abs_section (os->bfd_section))
ae475b39
SC
2318 {
2319 /* No matter what happens, an abs section starts at zero */
686739e2 2320 ASSERT (os->bfd_section->vma == 0);
ae475b39
SC
2321 }
2322 else
2323 {
2324 if (os->addr_tree == (etree_type *) NULL)
2325 {
2326 /* No address specified for this section, get one
2327 from the region specification
2328 */
26c61946
ILT
2329 if (os->region == (lang_memory_region_type *) NULL
2330 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2331 & (SEC_ALLOC | SEC_LOAD)) != 0)
2332 && os->region->name[0] == '*'
2333 && strcmp (os->region->name, "*default*") == 0))
ae475b39 2334 {
d3c324e5 2335 os->region = lang_memory_default (os->bfd_section);
ae475b39 2336 }
26c61946
ILT
2337
2338 /* If a loadable section is using the default memory
2339 region, and some non default memory regions were
2340 defined, issue a warning. */
2341 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2342 & (SEC_ALLOC | SEC_LOAD)) != 0
2343 && ! link_info.relocateable
2344 && strcmp (os->region->name, "*default*") == 0
2345 && lang_memory_region_list != NULL
2346 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2347 || lang_memory_region_list->next != NULL))
26c7ca95 2348 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
26c61946
ILT
2349 bfd_get_section_name (output_bfd, os->bfd_section));
2350
ae475b39 2351 dot = os->region->current;
feaa9c4b 2352 if (os->section_alignment == -1)
c5a0d31a
ILT
2353 {
2354 bfd_vma olddot;
2355
2356 olddot = dot;
2357 dot = align_power (dot, os->bfd_section->alignment_power);
2358 if (dot != olddot && config.warn_section_align)
26c7ca95 2359 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
c5a0d31a
ILT
2360 os->name, (unsigned int) (dot - olddot));
2361 }
ae475b39
SC
2362 }
2363 else
2364 {
2365 etree_value_type r;
2366
2367 r = exp_fold_tree (os->addr_tree,
2368 abs_output_section,
2369 lang_allocating_phase_enum,
2370 dot, &dot);
2371 if (r.valid == false)
2372 {
26c7ca95 2373 einfo (_("%F%S: non constant address expression for section %s\n"),
ae475b39
SC
2374 os->name);
2375 }
72df7117 2376 dot = r.value + r.section->bfd_section->vma;
ae475b39
SC
2377 }
2378 /* The section starts here */
2379 /* First, align to what the section needs */
2380
a62494c4
JL
2381 if (os->section_alignment != -1)
2382 dot = align_power (dot, os->section_alignment);
ae475b39 2383
ae475b39 2384 bfd_set_section_vma (0, os->bfd_section, dot);
9fce28ed 2385
686739e2
ILT
2386 os->bfd_section->output_offset = 0;
2387 }
ae475b39
SC
2388
2389 (void) lang_size_sections (os->children.head, os, &os->children.head,
2390 os->fill, dot, relax);
2391 /* Ignore the size of the input sections, use the vma and size to */
2392 /* align against */
2393
8ddef552 2394 after = ALIGN_N (os->bfd_section->vma +
97fbbaca
JL
2395 os->bfd_section->_raw_size,
2396 /* The coercion here is important, see ld.h. */
2397 (bfd_vma) os->block_value);
ae475b39 2398
686739e2
ILT
2399 if (bfd_is_abs_section (os->bfd_section))
2400 ASSERT (after == os->bfd_section->vma);
2401 else
2402 os->bfd_section->_raw_size = after - os->bfd_section->vma;
ae475b39
SC
2403 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2404 os->processed = true;
2405
2406 /* Replace into region ? */
686739e2 2407 if (os->region != (lang_memory_region_type *) NULL)
e802f0be
DE
2408 {
2409 os->region->current = dot;
2410 /* Make sure this isn't silly. */
09328690
ILT
2411 if (os->region->current < os->region->origin
2412 || (os->region->current - os->region->origin
2413 > os->region->length))
e802f0be
DE
2414 {
2415 if (os->addr_tree != (etree_type *) NULL)
2416 {
26c7ca95 2417 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
e802f0be
DE
2418 os->region->current,
2419 os->bfd_section->owner,
2420 os->bfd_section->name,
2421 os->region->name);
2422 }
2423 else
2424 {
26c7ca95 2425 einfo (_("%X%P: region %s is full (%B section %s)\n"),
e802f0be
DE
2426 os->region->name,
2427 os->bfd_section->owner,
2428 os->bfd_section->name);
2429 }
2430 /* Reset the region pointer. */
2431 os->region->current = os->region->origin;
2432 }
2433 }
ae475b39 2434 }
e802f0be 2435 break;
9d1fe8a4 2436
ae475b39
SC
2437 case lang_constructors_statement_enum:
2438 dot = lang_size_sections (constructor_list.head,
2439 output_section_statement,
2440 &s->wild_statement.children.head,
2441 fill,
2442 dot, relax);
2443 break;
9d1fe8a4 2444
ae475b39
SC
2445 case lang_data_statement_enum:
2446 {
2447 unsigned int size = 0;
2448
2449 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2450 s->data_statement.output_section =
2451 output_section_statement->bfd_section;
2452
2453 switch (s->data_statement.type)
9f19768a
ILT
2454 {
2455 case QUAD:
2456 case SQUAD:
2457 size = QUAD_SIZE;
2458 break;
2459 case LONG:
2460 size = LONG_SIZE;
2461 break;
2462 case SHORT:
2463 size = SHORT_SIZE;
2464 break;
2465 case BYTE:
2466 size = BYTE_SIZE;
2467 break;
2468 }
ae475b39 2469
ae475b39
SC
2470 dot += size;
2471 output_section_statement->bfd_section->_raw_size += size;
36ea6198
ILT
2472 /* The output section gets contents, and then we inspect for
2473 any flags set in the input script which override any ALLOC */
943fbd5b 2474 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
36ea6198
ILT
2475 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2476 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2477 }
ae475b39
SC
2478 }
2479 break;
c611e285 2480
4fdbafb2
ILT
2481 case lang_reloc_statement_enum:
2482 {
2483 int size;
2484
2485 s->reloc_statement.output_vma =
2486 dot - output_section_statement->bfd_section->vma;
2487 s->reloc_statement.output_section =
2488 output_section_statement->bfd_section;
2489 size = bfd_get_reloc_size (s->reloc_statement.howto);
2490 dot += size;
2491 output_section_statement->bfd_section->_raw_size += size;
2492 }
2493 break;
2494
ae475b39 2495 case lang_wild_statement_enum:
c611e285 2496
ae475b39
SC
2497 dot = lang_size_sections (s->wild_statement.children.head,
2498 output_section_statement,
2499 &s->wild_statement.children.head,
c611e285 2500
ae475b39 2501 fill, dot, relax);
c611e285 2502
ae475b39 2503 break;
c611e285 2504
ae475b39 2505 case lang_object_symbols_statement_enum:
c477527c
ILT
2506 link_info.create_object_symbols_section =
2507 output_section_statement->bfd_section;
ae475b39
SC
2508 break;
2509 case lang_output_statement_enum:
2510 case lang_target_statement_enum:
2511 break;
2512 case lang_input_section_enum:
ae475b39 2513 {
c477527c
ILT
2514 asection *i;
2515
193c5f93
ILT
2516 i = (*prev)->input_section.section;
2517 if (! relax)
6799c638
ILT
2518 {
2519 if (i->_cooked_size == 0)
2520 i->_cooked_size = i->_raw_size;
2521 }
193c5f93 2522 else
755f42fe 2523 {
193c5f93 2524 boolean again;
075d7359 2525
193c5f93 2526 if (! bfd_relax_section (i->owner, i, &link_info, &again))
26c7ca95 2527 einfo (_("%P%F: can't relax section: %E\n"));
193c5f93
ILT
2528 if (again)
2529 relax_again = true;
2530 }
2531 dot = size_input_section (prev,
2532 output_section_statement,
2533 output_section_statement->fill,
2534 dot, relax);
ae475b39 2535 }
ae475b39
SC
2536 break;
2537 case lang_input_statement_enum:
2538 break;
2539 case lang_fill_statement_enum:
2540 s->fill_statement.output_section = output_section_statement->bfd_section;
075d7359 2541
ae475b39
SC
2542 fill = s->fill_statement.fill;
2543 break;
2544 case lang_assignment_statement_enum:
2545 {
2546 bfd_vma newdot = dot;
2547
2548 exp_fold_tree (s->assignment_statement.exp,
2549 output_section_statement,
2550 lang_allocating_phase_enum,
2551 dot,
2552 &newdot);
2553
26c7ca95 2554 if (newdot != dot)
27f7237e
ILT
2555 {
2556 /* The assignment changed dot. Insert a pad. */
2557 if (output_section_statement == abs_output_section)
2558 {
2559 /* If we don't have an output section, then just adjust
2560 the default memory address. */
2561 lang_memory_region_lookup ("*default*")->current = newdot;
2562 }
26c7ca95 2563 else if (!relax)
27f7237e
ILT
2564 {
2565 lang_statement_union_type *new =
2566 ((lang_statement_union_type *)
2567 stat_alloc (sizeof (lang_padding_statement_type)));
2568
2569 /* Link into existing chain */
2570 new->header.next = *prev;
2571 *prev = new;
2572 new->header.type = lang_padding_statement_enum;
2573 new->padding_statement.output_section =
2574 output_section_statement->bfd_section;
2575 new->padding_statement.output_offset =
2576 dot - output_section_statement->bfd_section->vma;
2577 new->padding_statement.fill = fill;
2578 new->padding_statement.size = newdot - dot;
2579 output_section_statement->bfd_section->_raw_size +=
2580 new->padding_statement.size;
2581 }
2582
2583 dot = newdot;
2584 }
ae475b39 2585 }
27f7237e 2586 break;
9d87af56
ILT
2587
2588 case lang_padding_statement_enum:
2589 /* If we are relaxing, and this is not the first pass, some
2590 padding statements may have been inserted during previous
2591 passes. We may have to move the padding statement to a new
2592 location if dot has a different value at this point in this
2593 pass than it did at this point in the previous pass. */
2594 s->padding_statement.output_offset =
2595 dot - output_section_statement->bfd_section->vma;
2596 dot += s->padding_statement.size;
708f7fbc
ILT
2597 output_section_statement->bfd_section->_raw_size +=
2598 s->padding_statement.size;
9d87af56
ILT
2599 break;
2600
309c8153
ILT
2601 case lang_group_statement_enum:
2602 dot = lang_size_sections (s->group_statement.children.head,
2603 output_section_statement,
2604 &s->group_statement.children.head,
2605 fill, dot, relax);
2606 break;
2607
ae475b39
SC
2608 default:
2609 FAIL ();
2610 break;
9d87af56 2611
ae475b39 2612 /* This can only get here when relaxing is turned on */
075d7359 2613
ae475b39
SC
2614 case lang_address_statement_enum:
2615 break;
075d7359 2616 }
ae475b39
SC
2617 prev = &s->header.next;
2618 }
c611e285
SC
2619 return dot;
2620}
9d1fe8a4 2621
309c8153 2622bfd_vma
9f629407
ILT
2623lang_do_assignments (s, output_section_statement, fill, dot)
2624 lang_statement_union_type * s;
2625 lang_output_section_statement_type * output_section_statement;
c477527c 2626 fill_type fill;
9f629407 2627 bfd_vma dot;
2fa0b342 2628{
075d7359 2629 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 2630 {
075d7359 2631 switch (s->header.type)
2fa0b342 2632 {
075d7359
SC
2633 case lang_constructors_statement_enum:
2634 dot = lang_do_assignments (constructor_list.head,
2635 output_section_statement,
2636 fill,
2637 dot);
2638 break;
2639
2640 case lang_output_section_statement_enum:
2641 {
2642 lang_output_section_statement_type *os =
269773c1 2643 &(s->output_section_statement);
2fa0b342 2644
269773c1
ILT
2645 if (os->bfd_section != NULL)
2646 {
2647 dot = os->bfd_section->vma;
2648 (void) lang_do_assignments (os->children.head, os,
2649 os->fill, dot);
2650 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2651 }
0b3499f6
ILT
2652 if (os->load_base)
2653 {
467a0380
SC
2654 /* If nothing has been placed into the output section then
2655 it won't have a bfd_section. */
2656 if (os->bfd_section)
2657 {
2658 os->bfd_section->lma
2659 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2660 }
0b3499f6 2661 }
075d7359
SC
2662 }
2663 break;
2664 case lang_wild_statement_enum:
2fa0b342 2665
075d7359
SC
2666 dot = lang_do_assignments (s->wild_statement.children.head,
2667 output_section_statement,
2668 fill, dot);
2fa0b342 2669
075d7359
SC
2670 break;
2671
2672 case lang_object_symbols_statement_enum:
2673 case lang_output_statement_enum:
2674 case lang_target_statement_enum:
1418c83b 2675#if 0
075d7359 2676 case lang_common_statement_enum:
1418c83b 2677#endif
2fa0b342 2678 break;
075d7359
SC
2679 case lang_data_statement_enum:
2680 {
2681 etree_value_type value;
2682
2683 value = exp_fold_tree (s->data_statement.exp,
2684 abs_output_section,
2685 lang_final_phase_enum, dot, &dot);
2686 s->data_statement.value = value.value;
2687 if (value.valid == false)
26c7ca95 2688 einfo (_("%F%P: invalid data statement\n"));
075d7359
SC
2689 }
2690 switch (s->data_statement.type)
2691 {
c477527c 2692 case QUAD:
9f19768a 2693 case SQUAD:
c477527c
ILT
2694 dot += QUAD_SIZE;
2695 break;
075d7359
SC
2696 case LONG:
2697 dot += LONG_SIZE;
2698 break;
2699 case SHORT:
2700 dot += SHORT_SIZE;
2701 break;
2702 case BYTE:
2703 dot += BYTE_SIZE;
2704 break;
2705 }
2fa0b342 2706 break;
4fdbafb2
ILT
2707
2708 case lang_reloc_statement_enum:
2709 {
2710 etree_value_type value;
2711
2712 value = exp_fold_tree (s->reloc_statement.addend_exp,
2713 abs_output_section,
2714 lang_final_phase_enum, dot, &dot);
2715 s->reloc_statement.addend_value = value.value;
2716 if (value.valid == false)
26c7ca95 2717 einfo (_("%F%P: invalid reloc statement\n"));
4fdbafb2
ILT
2718 }
2719 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2720 break;
2721
075d7359
SC
2722 case lang_input_section_enum:
2723 {
2724 asection *in = s->input_section.section;
2725
f078dc7c
ILT
2726 if (in->_cooked_size != 0)
2727 dot += in->_cooked_size;
2728 else
2729 dot += in->_raw_size;
075d7359 2730 }
2fa0b342 2731 break;
2fa0b342 2732
075d7359
SC
2733 case lang_input_statement_enum:
2734 break;
2735 case lang_fill_statement_enum:
2736 fill = s->fill_statement.fill;
2737 break;
2738 case lang_assignment_statement_enum:
2739 {
2740 exp_fold_tree (s->assignment_statement.exp,
2741 output_section_statement,
2742 lang_final_phase_enum,
2743 dot,
2744 &dot);
2745 }
2746
2747 break;
2748 case lang_padding_statement_enum:
2749 dot += s->padding_statement.size;
2750 break;
309c8153
ILT
2751
2752 case lang_group_statement_enum:
2753 dot = lang_do_assignments (s->group_statement.children.head,
2754 output_section_statement,
2755 fill, dot);
2756
2757 break;
2758
075d7359
SC
2759 default:
2760 FAIL ();
2761 break;
2762 case lang_address_statement_enum:
2763 break;
2764 }
2fa0b342
DHW
2765
2766 }
2767 return dot;
2768}
2769
1d169acc
ILT
2770/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2771 operator .startof. (section_name), it produces an undefined symbol
2772 .startof.section_name. Similarly, when it sees
2773 .sizeof. (section_name), it produces an undefined symbol
2774 .sizeof.section_name. For all the output sections, we look for
2775 such symbols, and set them to the correct value. */
2776
2777static void
2778lang_set_startof ()
2779{
2780 asection *s;
2781
307585f1
ILT
2782 if (link_info.relocateable)
2783 return;
2784
1d169acc
ILT
2785 for (s = output_bfd->sections; s != NULL; s = s->next)
2786 {
2787 const char *secname;
2788 char *buf;
2789 struct bfd_link_hash_entry *h;
2790
2791 secname = bfd_get_section_name (output_bfd, s);
2792 buf = xmalloc (10 + strlen (secname));
2793
2794 sprintf (buf, ".startof.%s", secname);
2795 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2796 if (h != NULL && h->type == bfd_link_hash_undefined)
2797 {
2798 h->type = bfd_link_hash_defined;
2799 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2800 h->u.def.section = bfd_abs_section_ptr;
2801 }
2802
2803 sprintf (buf, ".sizeof.%s", secname);
2804 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2805 if (h != NULL && h->type == bfd_link_hash_undefined)
2806 {
2807 h->type = bfd_link_hash_defined;
2808 if (s->_cooked_size != 0)
2809 h->u.def.value = s->_cooked_size;
2810 else
2811 h->u.def.value = s->_raw_size;
2812 h->u.def.section = bfd_abs_section_ptr;
2813 }
2814
2815 free (buf);
2816 }
2817}
2818
2fa0b342 2819static void
8ddef552 2820lang_finish ()
2fa0b342 2821{
c477527c 2822 struct bfd_link_hash_entry *h;
cc38364d
KR
2823 boolean warn;
2824
2825 if (link_info.relocateable || link_info.shared)
2826 warn = false;
2827 else
2828 warn = true;
075d7359 2829
c477527c
ILT
2830 if (entry_symbol == (char *) NULL)
2831 {
2832 /* No entry has been specified. Look for start, but don't warn
2833 if we don't find it. */
2834 entry_symbol = "start";
2835 warn = false;
2836 }
2fa0b342 2837
c477527c
ILT
2838 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2839 if (h != (struct bfd_link_hash_entry *) NULL
8ed88239 2840 && (h->type == bfd_link_hash_defined
061e65f8
ILT
2841 || h->type == bfd_link_hash_defweak)
2842 && h->u.def.section->output_section != NULL)
075d7359 2843 {
c477527c 2844 bfd_vma val;
075d7359 2845
c477527c
ILT
2846 val = (h->u.def.value
2847 + bfd_get_section_vma (output_bfd,
2848 h->u.def.section->output_section)
2849 + h->u.def.section->output_offset);
2850 if (! bfd_set_start_address (output_bfd, val))
26c7ca95 2851 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
075d7359 2852 }
c477527c 2853 else
22a78f0d 2854 {
d2256aeb 2855 bfd_vma val;
71b012a8 2856 CONST char *send;
c477527c 2857
d2256aeb
ILT
2858 /* We couldn't find the entry symbol. Try parsing it as a
2859 number. */
2860 val = bfd_scan_vma (entry_symbol, &send, 0);
2861 if (*send == '\0')
c477527c 2862 {
d2256aeb 2863 if (! bfd_set_start_address (output_bfd, val))
26c7ca95 2864 einfo (_("%P%F: can't set start address\n"));
c477527c
ILT
2865 }
2866 else
2867 {
d2256aeb
ILT
2868 asection *ts;
2869
2870 /* Can't find the entry symbol, and it's not a number. Use
2871 the first address in the text section. */
2872 ts = bfd_get_section_by_name (output_bfd, ".text");
2873 if (ts != (asection *) NULL)
2874 {
2875 if (warn)
2876 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
2877 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2878 if (! bfd_set_start_address (output_bfd,
2879 bfd_get_section_vma (output_bfd,
2880 ts)))
2881 einfo (_("%P%F: can't set start address\n"));
2882 }
2883 else
2884 {
2885 if (warn)
2886 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
2887 entry_symbol);
2888 }
c477527c 2889 }
22a78f0d 2890 }
2fa0b342
DHW
2891}
2892
d3c324e5
MM
2893/* This is a small function used when we want to ignore errors from
2894 BFD. */
2895
2896static void
2897#ifdef ANSI_PROTOTYPES
2898ignore_bfd_errors (const char *s, ...)
2899#else
2900ignore_bfd_errors (s)
2901 const char *s;
2902#endif
2903{
2904 /* Don't do anything. */
2905}
2906
0b3499f6 2907/* Check that the architecture of all the input files is compatible
f400bbbb
ILT
2908 with the output file. Also call the backend to let it do any
2909 other checking that is needed. */
0b3499f6 2910
2fa0b342 2911static void
8ddef552 2912lang_check ()
2fa0b342
DHW
2913{
2914 lang_statement_union_type *file;
075d7359 2915 bfd *input_bfd;
075d7359 2916 CONST bfd_arch_info_type *compatible;
7bc4a0d7 2917
2fa0b342 2918 for (file = file_chain.head;
075d7359
SC
2919 file != (lang_statement_union_type *) NULL;
2920 file = file->input_statement.next)
2921 {
075d7359 2922 input_bfd = file->input_statement.the_bfd;
075d7359
SC
2923 compatible = bfd_arch_get_compatible (input_bfd,
2924 output_bfd);
0b3499f6 2925 if (compatible == NULL)
9f19768a 2926 {
d3c324e5 2927 if (command_line.warn_mismatch)
26c7ca95 2928 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
d3c324e5
MM
2929 bfd_printable_name (input_bfd), input_bfd,
2930 bfd_printable_name (output_bfd));
2931 }
2932 else
2933 {
26c61946 2934 bfd_error_handler_type pfn = NULL;
d3c324e5
MM
2935
2936 /* If we aren't supposed to warn about mismatched input
2937 files, temporarily set the BFD error handler to a
2938 function which will do nothing. We still want to call
2939 bfd_merge_private_bfd_data, since it may set up
2940 information which is needed in the output file. */
2941 if (! command_line.warn_mismatch)
2942 pfn = bfd_set_error_handler (ignore_bfd_errors);
2943 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2944 {
2945 if (command_line.warn_mismatch)
26c7ca95 2946 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
d3c324e5
MM
2947 input_bfd);
2948 }
2949 if (! command_line.warn_mismatch)
2950 bfd_set_error_handler (pfn);
9f19768a 2951 }
075d7359
SC
2952 }
2953}
2fa0b342 2954
c477527c
ILT
2955/* Look through all the global common symbols and attach them to the
2956 correct section. The -sort-common command line switch may be used
2957 to roughly sort the entries by size. */
2fa0b342
DHW
2958
2959static void
8ddef552 2960lang_common ()
2fa0b342 2961{
c477527c
ILT
2962 if (link_info.relocateable
2963 && ! command_line.force_common_definition)
2964 return;
075d7359 2965
c477527c
ILT
2966 if (! config.sort_common)
2967 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2968 else
075d7359 2969 {
fa9dea80 2970 int power;
1418c83b 2971
fa9dea80 2972 for (power = 4; power >= 0; power--)
c477527c
ILT
2973 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2974 (PTR) &power);
2975 }
2976}
29f33467 2977
c477527c 2978/* Place one common symbol in the correct section. */
075d7359 2979
c477527c
ILT
2980static boolean
2981lang_one_common (h, info)
2982 struct bfd_link_hash_entry *h;
2983 PTR info;
2984{
2985 unsigned int power_of_two;
2986 bfd_vma size;
c477527c 2987 asection *section;
075d7359 2988
c477527c
ILT
2989 if (h->type != bfd_link_hash_common)
2990 return true;
075d7359 2991
c477527c 2992 size = h->u.c.size;
f400bbbb 2993 power_of_two = h->u.c.p->alignment_power;
36c6e8c3
ILT
2994
2995 if (config.sort_common
f054ce20 2996 && power_of_two < (unsigned int) *(int *) info)
c477527c 2997 return true;
075d7359 2998
f400bbbb 2999 section = h->u.c.p->section;
075d7359 3000
c477527c 3001 /* Increase the size of the section. */
79cee2b7
RH
3002 section->_cooked_size = ALIGN_N (section->_cooked_size,
3003 (bfd_size_type) (1 << power_of_two));
075d7359 3004
c477527c
ILT
3005 /* Adjust the alignment if necessary. */
3006 if (power_of_two > section->alignment_power)
3007 section->alignment_power = power_of_two;
075d7359 3008
c477527c
ILT
3009 /* Change the symbol from common to defined. */
3010 h->type = bfd_link_hash_defined;
3011 h->u.def.section = section;
79cee2b7 3012 h->u.def.value = section->_cooked_size;
097879bc 3013
c477527c 3014 /* Increase the size of the section. */
79cee2b7 3015 section->_cooked_size += size;
1418c83b 3016
c5a0d31a
ILT
3017 /* Make sure the section is allocated in memory, and make sure that
3018 it is no longer a common section. */
809391bd 3019 section->flags |= SEC_ALLOC;
c5a0d31a 3020 section->flags &= ~ SEC_IS_COMMON;
809391bd 3021
b495c314 3022 if (config.map_file != NULL)
efa6c497
ILT
3023 {
3024 static boolean header_printed;
3025 int len;
3026 char *name;
3027 char buf[50];
3028
3029 if (! header_printed)
3030 {
26c7ca95
ILT
3031 minfo (_("\nAllocating common symbols\n"));
3032 minfo (_("Common symbol size file\n\n"));
efa6c497
ILT
3033 header_printed = true;
3034 }
3035
3036 name = demangle (h->root.string);
3037 minfo ("%s", name);
3038 len = strlen (name);
3039 free (name);
3040
3041 if (len >= 19)
3042 {
3043 print_nl ();
3044 len = 0;
3045 }
3046 while (len < 20)
3047 {
3048 print_space ();
3049 ++len;
3050 }
3051
3052 minfo ("0x");
3053 if (size <= 0xffffffff)
3054 sprintf (buf, "%lx", (unsigned long) size);
3055 else
3056 sprintf_vma (buf, size);
3057 minfo ("%s", buf);
3058 len = strlen (buf);
3059
3060 while (len < 16)
3061 {
3062 print_space ();
3063 ++len;
3064 }
3065
3066 minfo ("%B\n", section->owner);
3067 }
1418c83b 3068
c477527c 3069 return true;
2fa0b342
DHW
3070}
3071
3072/*
075d7359 3073run through the input files and ensure that every input
2fa0b342
DHW
3074section has somewhere to go. If one is found without
3075a destination then create an input request and place it
3076into the statement tree.
3077*/
3078
075d7359 3079static void
8ddef552 3080lang_place_orphans ()
2fa0b342
DHW
3081{
3082 lang_input_statement_type *file;
1418c83b 3083
075d7359
SC
3084 for (file = (lang_input_statement_type *) file_chain.head;
3085 file != (lang_input_statement_type *) NULL;
3086 file = (lang_input_statement_type *) file->next)
3087 {
3088 asection *s;
3089
3090 for (s = file->the_bfd->sections;
3091 s != (asection *) NULL;
3092 s = s->next)
3093 {
3094 if (s->output_section == (asection *) NULL)
3095 {
3096 /* This section of the file is not attatched, root
3097 around for a sensible place for it to go */
3098
e802f0be
DE
3099 if (file->just_syms_flag)
3100 {
3101 /* We are only retrieving symbol values from this
3102 file. We want the symbols to act as though the
3103 values in the file are absolute. */
3104 s->output_section = bfd_abs_section_ptr;
3105 s->output_offset = s->vma;
3106 }
86bc0974 3107 else if (strcmp (s->name, "COMMON") == 0)
075d7359 3108 {
86bc0974
ILT
3109 /* This is a lonely common section which must have
3110 come from an archive. We attach to the section
3111 with the wildcard. */
c477527c 3112 if (! link_info.relocateable
86bc0974 3113 || command_line.force_common_definition)
075d7359 3114 {
86bc0974 3115 if (default_common_section == NULL)
075d7359 3116 {
86bc0974
ILT
3117#if 0
3118 /* This message happens when using the
3119 svr3.ifile linker script, so I have
3120 disabled it. */
26c7ca95 3121 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
86bc0974 3122#endif
075d7359
SC
3123 default_common_section =
3124 lang_output_section_statement_lookup (".bss");
3125
3126 }
3127 wild_doit (&default_common_section->children, s,
3128 default_common_section, file);
3129 }
3130 }
0b3499f6
ILT
3131 else if (ldemul_place_orphan (file, s))
3132 ;
075d7359
SC
3133 else
3134 {
3135 lang_output_section_statement_type *os =
3136 lang_output_section_statement_lookup (s->name);
2fa0b342 3137
075d7359
SC
3138 wild_doit (&os->children, s, os, file);
3139 }
3140 }
2fa0b342 3141 }
2fa0b342 3142 }
2fa0b342
DHW
3143}
3144
3145
2fa0b342 3146void
8ddef552 3147lang_set_flags (ptr, flags)
d3c324e5 3148 lang_memory_region_type *ptr;
8ddef552 3149 CONST char *flags;
2fa0b342 3150{
d3c324e5 3151 flagword *ptr_flags = &ptr->flags;
075d7359 3152
d3c324e5 3153 ptr->flags = ptr->not_flags = 0;
2fa0b342 3154 while (*flags)
075d7359 3155 {
075d7359
SC
3156 switch (*flags)
3157 {
d3c324e5
MM
3158 case '!':
3159 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
075d7359 3160 break;
d3c324e5
MM
3161
3162 case 'A': case 'a':
3163 *ptr_flags |= SEC_ALLOC;
075d7359 3164 break;
d3c324e5
MM
3165
3166 case 'R': case 'r':
3167 *ptr_flags |= SEC_READONLY;
3168 break;
3169
3170 case 'W': case 'w':
3171 *ptr_flags |= SEC_DATA;
075d7359 3172 break;
d3c324e5
MM
3173
3174 case 'X': case 'x':
3175 *ptr_flags |= SEC_CODE;
075d7359 3176 break;
d3c324e5
MM
3177
3178 case 'L': case 'l':
3179 case 'I': case 'i':
3180 *ptr_flags |= SEC_LOAD;
3181 break;
3182
075d7359 3183 default:
26c7ca95 3184 einfo (_("%P%F: invalid syntax in flags\n"));
075d7359
SC
3185 break;
3186 }
dc4726c2
SC
3187 flags++;
3188 }
2fa0b342
DHW
3189}
3190
a4d2a48e
ILT
3191/* Call a function on each input file. This function will be called
3192 on an archive, but not on the elements. */
3193
3194void
3195lang_for_each_input_file (func)
3196 void (*func) PARAMS ((lang_input_statement_type *));
3197{
3198 lang_input_statement_type *f;
3199
3200 for (f = (lang_input_statement_type *) input_file_chain.head;
3201 f != NULL;
3202 f = (lang_input_statement_type *) f->next_real_file)
3203 func (f);
3204}
2fa0b342 3205
a4d2a48e
ILT
3206/* Call a function on each file. The function will be called on all
3207 the elements of an archive which are included in the link, but will
3208 not be called on the archive file itself. */
2fa0b342
DHW
3209
3210void
8ddef552
DM
3211lang_for_each_file (func)
3212 void (*func) PARAMS ((lang_input_statement_type *));
2fa0b342
DHW
3213{
3214 lang_input_statement_type *f;
075d7359
SC
3215
3216 for (f = (lang_input_statement_type *) file_chain.head;
3217 f != (lang_input_statement_type *) NULL;
3218 f = (lang_input_statement_type *) f->next)
3219 {
3220 func (f);
3221 }
2fa0b342
DHW
3222}
3223
9f629407
ILT
3224#if 0
3225
3226/* Not used. */
2fa0b342
DHW
3227
3228void
8ddef552
DM
3229lang_for_each_input_section (func)
3230 void (*func) PARAMS ((bfd * ab, asection * as));
2fa0b342
DHW
3231{
3232 lang_input_statement_type *f;
075d7359
SC
3233
3234 for (f = (lang_input_statement_type *) file_chain.head;
3235 f != (lang_input_statement_type *) NULL;
3236 f = (lang_input_statement_type *) f->next)
2fa0b342
DHW
3237 {
3238 asection *s;
075d7359 3239
2fa0b342 3240 for (s = f->the_bfd->sections;
075d7359
SC
3241 s != (asection *) NULL;
3242 s = s->next)
3243 {
3244 func (f->the_bfd, s);
3245 }
2fa0b342
DHW
3246 }
3247}
3248
9f629407 3249#endif
2fa0b342 3250
075d7359 3251void
8ddef552
DM
3252ldlang_add_file (entry)
3253 lang_input_statement_type * entry;
2fa0b342 3254{
5e6cd559
ILT
3255 bfd **pp;
3256
075d7359
SC
3257 lang_statement_append (&file_chain,
3258 (lang_statement_union_type *) entry,
3259 &entry->next);
c477527c
ILT
3260
3261 /* The BFD linker needs to have a list of all input BFDs involved in
3262 a link. */
3263 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3264 ASSERT (entry->the_bfd != output_bfd);
5e6cd559
ILT
3265 for (pp = &link_info.input_bfds;
3266 *pp != (bfd *) NULL;
3267 pp = &(*pp)->link_next)
3268 ;
3269 *pp = entry->the_bfd;
c477527c 3270 entry->the_bfd->usrdata = (PTR) entry;
fc1dfb71 3271 bfd_set_gp_size (entry->the_bfd, g_switch_value);
23595ba5
ILT
3272
3273 /* Look through the sections and check for any which should not be
3274 included in the link. We need to do this now, so that we can
3275 notice when the backend linker tries to report multiple
3276 definition errors for symbols which are in sections we aren't
3277 going to link. FIXME: It might be better to entirely ignore
3278 symbols which are defined in sections which are going to be
3279 discarded. This would require modifying the backend linker for
3280 each backend which might set the SEC_LINK_ONCE flag. If we do
3281 this, we should probably handle SEC_EXCLUDE in the same way. */
3282
c5a0d31a 3283 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
2fa0b342
DHW
3284}
3285
2fa0b342 3286void
2c6635a4 3287lang_add_output (name, from_script)
8ddef552 3288 CONST char *name;
2c6635a4 3289 int from_script;
2fa0b342 3290{
2c6635a4
ILT
3291 /* Make -o on command line override OUTPUT in script. */
3292 if (had_output_filename == false || !from_script)
3293 {
3294 output_filename = name;
3295 had_output_filename = true;
3296 }
2fa0b342
DHW
3297}
3298
3299
3300static lang_output_section_statement_type *current_section;
3301
f054ce20
ILT
3302static int
3303topower (x)
9f629407 3304 int x;
e20873a7 3305{
f054ce20 3306 unsigned int i = 1;
e20873a7 3307 int l;
f054ce20
ILT
3308
3309 if (x < 0)
3310 return -1;
3311
e20873a7 3312 for (l = 0; l < 32; l++)
f054ce20
ILT
3313 {
3314 if (i >= (unsigned int) x)
3315 return l;
3316 i <<= 1;
3317 }
3318
29f33467 3319 return 0;
e20873a7 3320}
86bc0974 3321
2fa0b342 3322void
8ddef552 3323lang_enter_output_section_statement (output_section_statement_name,
86bc0974 3324 address_exp, sectype, block_value,
9f629407 3325 align, subalign, ebase)
fcf276c4 3326 const char *output_section_statement_name;
8ddef552 3327 etree_type * address_exp;
86bc0974 3328 enum section_type sectype;
8ddef552
DM
3329 bfd_vma block_value;
3330 etree_type *align;
3331 etree_type *subalign;
9f629407 3332 etree_type *ebase;
2fa0b342
DHW
3333{
3334 lang_output_section_statement_type *os;
075d7359
SC
3335
3336 current_section =
e20873a7 3337 os =
075d7359
SC
3338 lang_output_section_statement_lookup (output_section_statement_name);
3339
3340
2fa0b342 3341
2fa0b342
DHW
3342 /* Add this statement to tree */
3343 /* add_statement(lang_output_section_statement_enum,
3344 output_section_statement);*/
3345 /* Make next things chain into subchain of this */
3346
3347 if (os->addr_tree ==
075d7359 3348 (etree_type *) NULL)
e20873a7
JG
3349 {
3350 os->addr_tree =
3351 address_exp;
3352 }
86bc0974
ILT
3353 os->sectype = sectype;
3354 if (sectype != noload_section)
3355 os->flags = SEC_NO_FLAGS;
ae475b39 3356 else
86bc0974 3357 os->flags = SEC_NEVER_LOAD;
29f33467 3358 os->block_value = block_value ? block_value : 1;
075d7359 3359 stat_ptr = &os->children;
2fa0b342 3360
e20873a7
JG
3361 os->subsection_alignment = topower(
3362 exp_get_value_int(subalign, -1,
3363 "subsection alignment",
3364 0));
3365 os->section_alignment = topower(
3366 exp_get_value_int(align, -1,
3367 "section alignment", 0));
9fce28ed 3368
9f629407 3369 os->load_base = ebase;
2fa0b342
DHW
3370}
3371
9fce28ed 3372
075d7359 3373void
8ddef552 3374lang_final ()
2fa0b342 3375{
2c6635a4
ILT
3376 lang_output_statement_type *new =
3377 new_stat (lang_output_statement, stat_ptr);
2fa0b342 3378
2c6635a4 3379 new->name = output_filename;
075d7359 3380}
2fa0b342 3381
c611e285
SC
3382/* Reset the current counters in the regions */
3383static void
8ddef552 3384reset_memory_regions ()
c611e285 3385{
075d7359
SC
3386 lang_memory_region_type *p = lang_memory_region_list;
3387
c611e285 3388 for (p = lang_memory_region_list;
075d7359
SC
3389 p != (lang_memory_region_type *) NULL;
3390 p = p->next)
3391 {
8ddef552 3392 p->old_length = (bfd_size_type) (p->current - p->origin);
075d7359
SC
3393 p->current = p->origin;
3394 }
c611e285 3395}
2fa0b342 3396
71b012a8
RH
3397/* ??? At some point this traversal for GC should share code with the
3398 traversal for manipulating the output file. */
3399
3400/* Expand a wild statement for a particular FILE, marking its sections KEEP
3401 as needed. SECTION may be NULL, in which case it is a wild card. */
3402
3403static void
3404lang_gc_wild_section (ptr, section, file)
3405 lang_wild_statement_type *ptr;
3406 const char *section;
3407 lang_input_statement_type *file;
3408{
3409 if (file->just_syms_flag == false)
3410 {
3411 register asection *s;
3412 boolean wildcard;
3413
3414 if (section == NULL)
3415 wildcard = false;
3416 else
3417 wildcard = wildcardp (section);
3418
3419 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3420 {
3421 boolean match;
3422
3423 if (section == NULL)
3424 match = true;
3425 else
3426 {
3427 const char *name;
3428
3429 name = bfd_get_section_name (file->the_bfd, s);
3430 if (wildcard)
3431 match = fnmatch (section, name, 0) == 0 ? true : false;
3432 else
3433 match = strcmp (section, name) == 0 ? true : false;
3434 }
3435
3436 if (match)
3437 {
3438 /* If the wild pattern was marked KEEP, the member sections
3439 should be as well. */
3440 if (ptr->keep_sections)
3441 s->flags |= SEC_KEEP;
3442 }
3443 }
3444 }
3445}
3446
3447/* Handle a wild statement for a single file F. */
3448
3449static void
3450lang_gc_wild_file (s, section, f)
3451 lang_wild_statement_type *s;
3452 const char *section;
3453 lang_input_statement_type *f;
3454{
3455 if (f->the_bfd == NULL
3456 || ! bfd_check_format (f->the_bfd, bfd_archive))
3457 lang_gc_wild_section (s, section, f);
3458 else
3459 {
3460 bfd *member;
3461
3462 /* This is an archive file. We must map each member of the
3463 archive separately. */
3464 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
3465 while (member != NULL)
3466 {
3467 /* When lookup_name is called, it will call the add_symbols
3468 entry point for the archive. For each element of the
3469 archive which is included, BFD will call ldlang_add_file,
3470 which will set the usrdata field of the member to the
3471 lang_input_statement. */
3472 if (member->usrdata != NULL)
3473 {
3474 lang_gc_wild_section (s, section,
3475 (lang_input_statement_type *) member->usrdata);
3476 }
3477
3478 member = bfd_openr_next_archived_file (f->the_bfd, member);
3479 }
3480 }
3481}
3482
3483/* Handle a wild statement, marking it against GC. SECTION or FILE or both
3484 may be NULL, indicating that it is a wildcard. */
3485
3486static void
3487lang_gc_wild (s, section, file)
3488 lang_wild_statement_type *s;
3489 const char *section;
3490 const char *file;
3491{
3492 lang_input_statement_type *f;
3493
3494 if (file == (char *) NULL)
3495 {
3496 /* Perform the iteration over all files in the list */
3497 for (f = (lang_input_statement_type *) file_chain.head;
3498 f != (lang_input_statement_type *) NULL;
3499 f = (lang_input_statement_type *) f->next)
3500 {
3501 lang_gc_wild_file (s, section, f);
3502 }
3503 }
3504 else if (wildcardp (file))
3505 {
3506 for (f = (lang_input_statement_type *) file_chain.head;
3507 f != (lang_input_statement_type *) NULL;
3508 f = (lang_input_statement_type *) f->next)
3509 {
3510 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
3511 lang_gc_wild_file (s, section, f);
3512 }
3513 }
3514 else
3515 {
3516 /* Perform the iteration over a single file */
3517 f = lookup_name (file);
3518 lang_gc_wild_file (s, section, f);
3519 }
3520}
3521
3522/* Iterate over sections marking them against GC. */
3523
3524static void
3525lang_gc_sections_1 (s)
3526 lang_statement_union_type * s;
3527{
3528 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3529 {
3530 switch (s->header.type)
3531 {
3532 case lang_wild_statement_enum:
3533 lang_gc_wild (&s->wild_statement,
3534 s->wild_statement.section_name,
3535 s->wild_statement.filename);
3536 break;
3537 case lang_constructors_statement_enum:
3538 lang_gc_sections_1 (constructor_list.head);
3539 break;
3540 case lang_output_section_statement_enum:
3541 lang_gc_sections_1 (s->output_section_statement.children.head);
3542 break;
3543 case lang_group_statement_enum:
3544 lang_gc_sections_1 (s->group_statement.children.head);
3545 break;
3546 }
3547 }
3548}
3549
3550static void
3551lang_gc_sections ()
3552{
3553 struct bfd_link_hash_entry *h;
3554 ldlang_undef_chain_list_type *ulist, fake_list_start;
3555
3556 /* Keep all sections so marked in the link script. */
3557
3558 lang_gc_sections_1 (statement_list.head);
3559
3560 /* Keep all sections containing symbols undefined on the command-line.
3561 Handle the entry symbol at the same time. */
3562
3563 fake_list_start.next = ldlang_undef_chain_list_head;
3564 fake_list_start.name = entry_symbol;
3565
3566 for (ulist = &fake_list_start; ulist; ulist = ulist->next)
3567 {
3568 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3569 false, false, false);
3570
3571 if (h != (struct bfd_link_hash_entry *) NULL
3572 && (h->type == bfd_link_hash_defined
3573 || h->type == bfd_link_hash_defweak)
3574 && ! bfd_is_abs_section (h->u.def.section))
3575 {
3576 h->u.def.section->flags |= SEC_KEEP;
3577 }
3578 }
3579
3580 bfd_gc_sections (output_bfd, &link_info);
3581}
3582
2fa0b342 3583void
8ddef552 3584lang_process ()
075d7359 3585{
075d7359 3586 lang_reasonable_defaults ();
1418c83b
SC
3587 current_target = default_target;
3588
075d7359 3589 lang_for_each_statement (ldlang_open_output); /* Open the output file */
1418c83b 3590
97fbbaca
JL
3591 ldemul_create_output_section_statements ();
3592
1418c83b 3593 /* Add to the hash table all undefineds on the command line */
075d7359 3594 lang_place_undefineds ();
1418c83b
SC
3595
3596 /* Create a bfd for each input file */
3597 current_target = default_target;
309c8153 3598 open_input_bfds (statement_list.head, false);
1418c83b 3599
cc38364d
KR
3600 ldemul_after_open ();
3601
9f19768a
ILT
3602 /* Make sure that we're not mixing architectures. We call this
3603 after all the input files have been opened, but before we do any
3604 other processing, so that any operations merge_private_bfd_data
3605 does on the output file will be known during the rest of the
3606 link. */
3607 lang_check ();
3608
c477527c
ILT
3609 /* Build all sets based on the information gathered from the input
3610 files. */
3611 ldctor_build_sets ();
3612
71b012a8
RH
3613 /* Remove unreferenced sections if asked to. */
3614 if (command_line.gc_sections)
3615 lang_gc_sections ();
3616
4fdbafb2
ILT
3617 /* Size up the common data */
3618 lang_common ();
3619
9f19768a 3620 /* Run through the contours of the script and attach input sections
075d7359 3621 to the correct output sections
1418c83b 3622 */
075d7359
SC
3623 map_input_to_output_sections (statement_list.head, (char *) NULL,
3624 (lang_output_section_statement_type *) NULL);
1418c83b 3625
81016051 3626
9f19768a 3627 /* Find any sections not attached explicitly and handle them */
075d7359 3628 lang_place_orphans ();
1418c83b 3629
075d7359 3630 ldemul_before_allocation ();
1418c83b 3631
061e65f8
ILT
3632 /* We must record the program headers before we try to fix the
3633 section positions, since they will affect SIZEOF_HEADERS. */
3634 lang_record_phdrs ();
3635
c611e285 3636 /* Now run around and relax if we can */
075d7359 3637 if (command_line.relax)
c611e285 3638 {
97fbbaca
JL
3639 /* First time round is a trial run to get the 'worst case'
3640 addresses of the objects if there was no relaxing. */
ae475b39 3641 lang_size_sections (statement_list.head,
193c5f93 3642 abs_output_section,
ae475b39 3643 &(statement_list.head), 0, (bfd_vma) 0, false);
c611e285 3644
193c5f93
ILT
3645 /* Keep relaxing until bfd_relax_section gives up. */
3646 do
3647 {
1d169acc
ILT
3648 reset_memory_regions ();
3649
193c5f93 3650 relax_again = false;
ae475b39 3651
193c5f93
ILT
3652 /* Do all the assignments with our current guesses as to
3653 section sizes. */
3654 lang_do_assignments (statement_list.head,
3655 abs_output_section,
3656 (fill_type) 0, (bfd_vma) 0);
ae475b39 3657
193c5f93
ILT
3658 /* Perform another relax pass - this time we know where the
3659 globals are, so can make better guess. */
3660 lang_size_sections (statement_list.head,
3661 abs_output_section,
3662 &(statement_list.head), 0, (bfd_vma) 0, true);
3663 }
3664 while (relax_again);
ae475b39 3665 }
ae475b39
SC
3666 else
3667 {
97fbbaca 3668 /* Size up the sections. */
ae475b39
SC
3669 lang_size_sections (statement_list.head,
3670 abs_output_section,
3671 &(statement_list.head), 0, (bfd_vma) 0, false);
075d7359 3672 }
c611e285 3673
1418c83b 3674 /* See if anything special should be done now we know how big
97fbbaca 3675 everything is. */
075d7359 3676 ldemul_after_allocation ();
1418c83b 3677
1d169acc
ILT
3678 /* Fix any .startof. or .sizeof. symbols. */
3679 lang_set_startof ();
3680
1418c83b
SC
3681 /* Do all the assignments, now that we know the final restingplaces
3682 of all the symbols */
3683
075d7359
SC
3684 lang_do_assignments (statement_list.head,
3685 abs_output_section,
c477527c 3686 (fill_type) 0, (bfd_vma) 0);
ffc50032 3687
1418c83b 3688 /* Final stuffs */
97fbbaca
JL
3689
3690 ldemul_finish ();
075d7359 3691 lang_finish ();
2fa0b342
DHW
3692}
3693
2fa0b342 3694/* EXPORTED TO YACC */
1418c83b 3695
2fa0b342 3696void
71b012a8
RH
3697lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
3698 keep_sections)
26c7ca95
ILT
3699 const char *const section_name;
3700 boolean sections_sorted;
3701 const char *const filename;
3702 boolean filenames_sorted;
71b012a8 3703 boolean keep_sections;
1418c83b 3704{
075d7359
SC
3705 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3706 stat_ptr);
1418c83b 3707
075d7359
SC
3708 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3709 {
3710 placed_commons = true;
3711 }
3712 if (filename != (char *) NULL)
3713 {
3714 lang_has_input_file = true;
3715 }
1418c83b 3716 new->section_name = section_name;
26c7ca95 3717 new->sections_sorted = sections_sorted;
1418c83b 3718 new->filename = filename;
26c7ca95 3719 new->filenames_sorted = filenames_sorted;
71b012a8 3720 new->keep_sections = keep_sections;
075d7359 3721 lang_list_init (&new->children);
1418c83b 3722}
075d7359 3723
1418c83b 3724void
8ddef552
DM
3725lang_section_start (name, address)
3726 CONST char *name;
3727 etree_type * address;
2fa0b342 3728{
075d7359
SC
3729 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3730
2fa0b342
DHW
3731 ad->section_name = name;
3732 ad->address = address;
3733}
1418c83b 3734
60e8a534
ILT
3735/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3736 because of a -e argument on the command line, or zero if this is
3737 called by ENTRY in a linker script. Command line arguments take
3738 precedence. */
3739
075d7359 3740void
60e8a534 3741lang_add_entry (name, cmdline)
8ddef552 3742 CONST char *name;
f054ce20 3743 boolean cmdline;
2fa0b342 3744{
60e8a534
ILT
3745 if (entry_symbol == NULL
3746 || cmdline
f054ce20 3747 || ! entry_from_cmdline)
60e8a534
ILT
3748 {
3749 entry_symbol = name;
f054ce20 3750 entry_from_cmdline = cmdline;
60e8a534 3751 }
2fa0b342
DHW
3752}
3753
3754void
8ddef552
DM
3755lang_add_target (name)
3756 CONST char *name;
2fa0b342 3757{
075d7359
SC
3758 lang_target_statement_type *new = new_stat (lang_target_statement,
3759 stat_ptr);
3760
2fa0b342
DHW
3761 new->target = name;
3762
3763}
2fa0b342 3764
2fa0b342 3765void
8ddef552
DM
3766lang_add_map (name)
3767 CONST char *name;
2fa0b342 3768{
075d7359
SC
3769 while (*name)
3770 {
3771 switch (*name)
3772 {
3773 case 'F':
3774 map_option_f = true;
3775 break;
3776 }
3777 name++;
2fa0b342 3778 }
2fa0b342
DHW
3779}
3780
075d7359 3781void
8ddef552
DM
3782lang_add_fill (exp)
3783 int exp;
2fa0b342 3784{
075d7359
SC
3785 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3786 stat_ptr);
3787
2fa0b342
DHW
3788 new->fill = exp;
3789}
3790
075d7359 3791void
8ddef552
DM
3792lang_add_data (type, exp)
3793 int type;
3794 union etree_union *exp;
2fa0b342
DHW
3795{
3796
075d7359 3797 lang_data_statement_type *new = new_stat (lang_data_statement,
2fa0b342 3798 stat_ptr);
075d7359
SC
3799
3800 new->exp = exp;
3801 new->type = type;
2fa0b342
DHW
3802
3803}
075d7359 3804
4fdbafb2
ILT
3805/* Create a new reloc statement. RELOC is the BFD relocation type to
3806 generate. HOWTO is the corresponding howto structure (we could
3807 look this up, but the caller has already done so). SECTION is the
3808 section to generate a reloc against, or NAME is the name of the
3809 symbol to generate a reloc against. Exactly one of SECTION and
3810 NAME must be NULL. ADDEND is an expression for the addend. */
3811
3812void
3813lang_add_reloc (reloc, howto, section, name, addend)
3814 bfd_reloc_code_real_type reloc;
cc38364d 3815 reloc_howto_type *howto;
4fdbafb2
ILT
3816 asection *section;
3817 const char *name;
3818 union etree_union *addend;
3819{
3820 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3821
3822 p->reloc = reloc;
3823 p->howto = howto;
3824 p->section = section;
3825 p->name = name;
3826 p->addend_exp = addend;
3827
3828 p->addend_value = 0;
3829 p->output_section = NULL;
3830 p->output_vma = 0;
3831}
3832
2fa0b342 3833void
8ddef552
DM
3834lang_add_assignment (exp)
3835 etree_type * exp;
2fa0b342 3836{
075d7359
SC
3837 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3838 stat_ptr);
3839
2fa0b342
DHW
3840 new->exp = exp;
3841}
3842
3843void
8ddef552
DM
3844lang_add_attribute (attribute)
3845 enum statement_enum attribute;
2fa0b342 3846{
075d7359 3847 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2fa0b342
DHW
3848}
3849
075d7359 3850void
8ddef552
DM
3851lang_startup (name)
3852 CONST char *name;
2fa0b342 3853{
075d7359
SC
3854 if (startup_file != (char *) NULL)
3855 {
26c7ca95 3856 einfo (_("%P%Fmultiple STARTUP files\n"));
075d7359 3857 }
2fa0b342
DHW
3858 first_file->filename = name;
3859 first_file->local_sym_name = name;
193c5f93 3860 first_file->real = true;
2fa0b342 3861
075d7359 3862 startup_file = name;
2fa0b342 3863}
075d7359
SC
3864
3865void
8ddef552
DM
3866lang_float (maybe)
3867 boolean maybe;
2fa0b342
DHW
3868{
3869 lang_float_flag = maybe;
3870}
3871
075d7359 3872void
d22ec3b0 3873lang_leave_output_section_statement (fill, memspec, phdrs)
8ddef552 3874 bfd_vma fill;
d22ec3b0
ILT
3875 const char *memspec;
3876 struct lang_output_section_phdr_list *phdrs;
2fa0b342
DHW
3877{
3878 current_section->fill = fill;
075d7359 3879 current_section->region = lang_memory_region_lookup (memspec);
d22ec3b0 3880 current_section->phdrs = phdrs;
2fa0b342
DHW
3881 stat_ptr = &statement_list;
3882}
075d7359 3883
9f32f7c2
SC
3884/*
3885 Create an absolute symbol with the given name with the value of the
3886 address of first byte of the section named.
2fa0b342 3887
9f32f7c2
SC
3888 If the symbol already exists, then do nothing.
3889*/
8cb5eb31 3890void
c477527c
ILT
3891lang_abs_symbol_at_beginning_of (secname, name)
3892 const char *secname;
3893 const char *name;
075d7359 3894{
c477527c
ILT
3895 struct bfd_link_hash_entry *h;
3896
3897 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3898 if (h == (struct bfd_link_hash_entry *) NULL)
26c7ca95 3899 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
c477527c
ILT
3900
3901 if (h->type == bfd_link_hash_new
3902 || h->type == bfd_link_hash_undefined)
075d7359 3903 {
c477527c 3904 asection *sec;
075d7359 3905
c477527c
ILT
3906 h->type = bfd_link_hash_defined;
3907
3908 sec = bfd_get_section_by_name (output_bfd, secname);
3909 if (sec == (asection *) NULL)
3910 h->u.def.value = 0;
075d7359 3911 else
c477527c
ILT
3912 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3913
686739e2 3914 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3915 }
8cb5eb31
SC
3916}
3917
9f32f7c2
SC
3918/*
3919 Create an absolute symbol with the given name with the value of the
3920 address of the first byte after the end of the section named.
3921
3922 If the symbol already exists, then do nothing.
3923*/
2fa0b342 3924void
c477527c
ILT
3925lang_abs_symbol_at_end_of (secname, name)
3926 const char *secname;
3927 const char *name;
075d7359 3928{
c477527c
ILT
3929 struct bfd_link_hash_entry *h;
3930
3931 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3932 if (h == (struct bfd_link_hash_entry *) NULL)
26c7ca95 3933 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
c477527c
ILT
3934
3935 if (h->type == bfd_link_hash_new
3936 || h->type == bfd_link_hash_undefined)
075d7359 3937 {
c477527c 3938 asection *sec;
075d7359 3939
c477527c 3940 h->type = bfd_link_hash_defined;
075d7359 3941
c477527c
ILT
3942 sec = bfd_get_section_by_name (output_bfd, secname);
3943 if (sec == (asection *) NULL)
3944 h->u.def.value = 0;
075d7359 3945 else
c477527c
ILT
3946 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3947 + bfd_section_size (output_bfd, sec));
3948
686739e2 3949 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3950 }
2fa0b342
DHW
3951}
3952
075d7359 3953void
8ddef552
DM
3954lang_statement_append (list, element, field)
3955 lang_statement_list_type * list;
3956 lang_statement_union_type * element;
3957 lang_statement_union_type ** field;
2fa0b342
DHW
3958{
3959 *(list->tail) = element;
3960 list->tail = field;
3961}
3962
173a0c3d 3963/* Set the output format type. -oformat overrides scripts. */
6a02a973 3964
097879bc 3965void
6a02a973
ILT
3966lang_add_output_format (format, big, little, from_script)
3967 const char *format;
3968 const char *big;
3969 const char *little;
173a0c3d 3970 int from_script;
097879bc 3971{
2c6635a4 3972 if (output_target == NULL || !from_script)
6a02a973
ILT
3973 {
3974 if (command_line.endian == ENDIAN_BIG
3975 && big != NULL)
3976 format = big;
3977 else if (command_line.endian == ENDIAN_LITTLE
3978 && little != NULL)
3979 format = little;
3980
3981 output_target = format;
3982 }
097879bc 3983}
309c8153
ILT
3984
3985/* Enter a group. This creates a new lang_group_statement, and sets
3986 stat_ptr to build new statements within the group. */
3987
3988void
3989lang_enter_group ()
3990{
3991 lang_group_statement_type *g;
3992
3993 g = new_stat (lang_group_statement, stat_ptr);
3994 lang_list_init (&g->children);
3995 stat_ptr = &g->children;
3996}
3997
3998/* Leave a group. This just resets stat_ptr to start writing to the
3999 regular list of statements again. Note that this will not work if
4000 groups can occur inside anything else which can adjust stat_ptr,
4001 but currently they can't. */
4002
4003void
4004lang_leave_group ()
4005{
4006 stat_ptr = &statement_list;
4007}
061e65f8
ILT
4008
4009/* Add a new program header. This is called for each entry in a PHDRS
4010 command in a linker script. */
4011
4012void
708f7fbc 4013lang_new_phdr (name, type, filehdr, phdrs, at, flags)
061e65f8
ILT
4014 const char *name;
4015 etree_type *type;
708f7fbc
ILT
4016 boolean filehdr;
4017 boolean phdrs;
061e65f8 4018 etree_type *at;
708f7fbc 4019 etree_type *flags;
061e65f8
ILT
4020{
4021 struct lang_phdr *n, **pp;
4022
4023 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4024 n->next = NULL;
4025 n->name = name;
4026 n->type = exp_get_value_int (type, 0, "program header type",
4027 lang_final_phase_enum);
708f7fbc
ILT
4028 n->filehdr = filehdr;
4029 n->phdrs = phdrs;
061e65f8 4030 n->at = at;
708f7fbc 4031 n->flags = flags;
061e65f8
ILT
4032
4033 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4034 ;
4035 *pp = n;
4036}
4037
061e65f8
ILT
4038/* Record the program header information in the output BFD. FIXME: We
4039 should not be calling an ELF specific function here. */
4040
4041static void
4042lang_record_phdrs ()
4043{
4044 unsigned int alc;
4045 asection **secs;
4046 struct lang_output_section_phdr_list *last;
4047 struct lang_phdr *l;
4048 lang_statement_union_type *u;
4049
4050 alc = 10;
c5a0d31a 4051 secs = (asection **) xmalloc (alc * sizeof (asection *));
061e65f8
ILT
4052 last = NULL;
4053 for (l = lang_phdr_list; l != NULL; l = l->next)
4054 {
4055 unsigned int c;
708f7fbc 4056 flagword flags;
061e65f8
ILT
4057 bfd_vma at;
4058
4059 c = 0;
4060 for (u = lang_output_section_statement.head;
4061 u != NULL;
4062 u = u->output_section_statement.next)
4063 {
4064 lang_output_section_statement_type *os;
4065 struct lang_output_section_phdr_list *pl;
4066
4067 os = &u->output_section_statement;
4068
708f7fbc 4069 pl = os->phdrs;
061e65f8
ILT
4070 if (pl != NULL)
4071 last = pl;
4072 else
4073 {
86bc0974 4074 if (os->sectype == noload_section
061e65f8
ILT
4075 || os->bfd_section == NULL
4076 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4077 continue;
4078 pl = last;
4079 }
4080
4081 if (os->bfd_section == NULL)
4082 continue;
4083
4084 for (; pl != NULL; pl = pl->next)
4085 {
4086 if (strcmp (pl->name, l->name) == 0)
4087 {
4088 if (c >= alc)
4089 {
4090 alc *= 2;
c5a0d31a
ILT
4091 secs = ((asection **)
4092 xrealloc (secs, alc * sizeof (asection *)));
061e65f8
ILT
4093 }
4094 secs[c] = os->bfd_section;
4095 ++c;
4096 pl->used = true;
4097 }
4098 }
4099 }
4100
708f7fbc
ILT
4101 if (l->flags == NULL)
4102 flags = 0;
4103 else
4104 flags = exp_get_vma (l->flags, 0, "phdr flags",
4105 lang_final_phase_enum);
4106
061e65f8
ILT
4107 if (l->at == NULL)
4108 at = 0;
4109 else
4110 at = exp_get_vma (l->at, 0, "phdr load address",
4111 lang_final_phase_enum);
708f7fbc
ILT
4112
4113 if (! bfd_record_phdr (output_bfd, l->type,
4114 l->flags == NULL ? false : true,
4115 flags,
061e65f8 4116 l->at == NULL ? false : true,
708f7fbc 4117 at, l->filehdr, l->phdrs, c, secs))
26c7ca95 4118 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
061e65f8
ILT
4119 }
4120
4121 free (secs);
4122
4123 /* Make sure all the phdr assignments succeeded. */
4124 for (u = lang_output_section_statement.head;
4125 u != NULL;
4126 u = u->output_section_statement.next)
4127 {
4128 struct lang_output_section_phdr_list *pl;
4129
4130 if (u->output_section_statement.bfd_section == NULL)
4131 continue;
4132
4133 for (pl = u->output_section_statement.phdrs;
4134 pl != NULL;
4135 pl = pl->next)
4136 if (! pl->used && strcmp (pl->name, "NONE") != 0)
26c7ca95 4137 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
061e65f8
ILT
4138 u->output_section_statement.name, pl->name);
4139 }
4140}
582dd77f
ILT
4141
4142/* Record a list of sections which may not be cross referenced. */
4143
4144void
4145lang_add_nocrossref (l)
4146 struct lang_nocrossref *l;
4147{
4148 struct lang_nocrossrefs *n;
4149
4150 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4151 n->next = nocrossref_list;
4152 n->list = l;
4153 nocrossref_list = n;
4154
4155 /* Set notice_all so that we get informed about all symbols. */
4156 link_info.notice_all = true;
4157}
d22ec3b0
ILT
4158\f
4159/* Overlay handling. We handle overlays with some static variables. */
4160
4161/* The overlay virtual address. */
4162static etree_type *overlay_vma;
4163
4164/* The overlay load address. */
4165static etree_type *overlay_lma;
4166
c5a0d31a
ILT
4167/* Whether nocrossrefs is set for this overlay. */
4168static int overlay_nocrossrefs;
4169
d22ec3b0
ILT
4170/* An expression for the maximum section size seen so far. */
4171static etree_type *overlay_max;
4172
4173/* A list of all the sections in this overlay. */
4174
4175struct overlay_list
4176{
4177 struct overlay_list *next;
4178 lang_output_section_statement_type *os;
4179};
4180
4181static struct overlay_list *overlay_list;
4182
4183/* Start handling an overlay. */
4184
4185void
c5a0d31a 4186lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
d22ec3b0
ILT
4187 etree_type *vma_expr;
4188 etree_type *lma_expr;
c5a0d31a 4189 int nocrossrefs;
d22ec3b0
ILT
4190{
4191 /* The grammar should prevent nested overlays from occurring. */
4192 ASSERT (overlay_vma == NULL
4193 && overlay_lma == NULL
4194 && overlay_list == NULL
4195 && overlay_max == NULL);
4196
4197 overlay_vma = vma_expr;
4198 overlay_lma = lma_expr;
c5a0d31a 4199 overlay_nocrossrefs = nocrossrefs;
d22ec3b0
ILT
4200}
4201
4202/* Start a section in an overlay. We handle this by calling
4203 lang_enter_output_section_statement with the correct VMA and LMA. */
4204
4205void
4206lang_enter_overlay_section (name)
4207 const char *name;
4208{
4209 struct overlay_list *n;
4210 etree_type *size;
4211
4212 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4213 0, 0, 0, overlay_lma);
4214
4215 /* If this is the first section, then base the VMA and LMA of future
4216 sections on this one. This will work correctly even if `.' is
4217 used in the addresses. */
4218 if (overlay_list == NULL)
4219 {
4220 overlay_vma = exp_nameop (ADDR, name);
4221 overlay_lma = exp_nameop (LOADADDR, name);
4222 }
4223
4224 /* Remember the section. */
4225 n = (struct overlay_list *) xmalloc (sizeof *n);
4226 n->os = current_section;
4227 n->next = overlay_list;
4228 overlay_list = n;
4229
4230 size = exp_nameop (SIZEOF, name);
4231
4232 /* Adjust the LMA for the next section. */
4233 overlay_lma = exp_binop ('+', overlay_lma, size);
4234
4235 /* Arrange to work out the maximum section end address. */
4236 if (overlay_max == NULL)
4237 overlay_max = size;
4238 else
4239 overlay_max = exp_binop (MAX, overlay_max, size);
4240}
4241
4242/* Finish a section in an overlay. There isn't any special to do
4243 here. */
4244
4245void
4246lang_leave_overlay_section (fill, phdrs)
4247 bfd_vma fill;
4248 struct lang_output_section_phdr_list *phdrs;
4249{
4250 const char *name;
4251 char *clean, *s2;
4252 const char *s1;
4253 char *buf;
4254
4255 name = current_section->name;
4256
4257 lang_leave_output_section_statement (fill, "*default*", phdrs);
4258
4259 /* Define the magic symbols. */
4260
4261 clean = xmalloc (strlen (name) + 1);
4262 s2 = clean;
4263 for (s1 = name; *s1 != '\0'; s1++)
26c61946 4264 if (isalnum ((unsigned char) *s1) || *s1 == '_')
d22ec3b0
ILT
4265 *s2++ = *s1;
4266 *s2 = '\0';
4267
4268 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4269 sprintf (buf, "__load_start_%s", clean);
4270 lang_add_assignment (exp_assop ('=', buf,
4271 exp_nameop (LOADADDR, name)));
4272
4273 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4274 sprintf (buf, "__load_stop_%s", clean);
4275 lang_add_assignment (exp_assop ('=', buf,
4276 exp_binop ('+',
4277 exp_nameop (LOADADDR, name),
4278 exp_nameop (SIZEOF, name))));
4279
4280 free (clean);
4281}
4282
4283/* Finish an overlay. If there are any overlay wide settings, this
4284 looks through all the sections in the overlay and sets them. */
4285
4286void
4287lang_leave_overlay (fill, memspec, phdrs)
4288 bfd_vma fill;
4289 const char *memspec;
4290 struct lang_output_section_phdr_list *phdrs;
4291{
4292 lang_memory_region_type *region;
4293 struct overlay_list *l;
4294 struct lang_nocrossref *nocrossref;
4295
4296 if (memspec == NULL)
4297 region = NULL;
4298 else
4299 region = lang_memory_region_lookup (memspec);
4300
4301 nocrossref = NULL;
4302
4303 l = overlay_list;
4304 while (l != NULL)
4305 {
d22ec3b0
ILT
4306 struct overlay_list *next;
4307
4308 if (fill != 0 && l->os->fill == 0)
4309 l->os->fill = fill;
4310 if (region != NULL && l->os->region == NULL)
4311 l->os->region = region;
4312 if (phdrs != NULL && l->os->phdrs == NULL)
4313 l->os->phdrs = phdrs;
4314
c5a0d31a
ILT
4315 if (overlay_nocrossrefs)
4316 {
4317 struct lang_nocrossref *nc;
4318
4319 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4320 nc->name = l->os->name;
4321 nc->next = nocrossref;
4322 nocrossref = nc;
4323 }
d22ec3b0
ILT
4324
4325 next = l->next;
4326 free (l);
4327 l = next;
4328 }
4329
4330 if (nocrossref != NULL)
4331 lang_add_nocrossref (nocrossref);
4332
4333 /* Update . for the end of the overlay. */
4334 lang_add_assignment (exp_assop ('=', ".",
4335 exp_binop ('+', overlay_vma, overlay_max)));
4336
4337 overlay_vma = NULL;
4338 overlay_lma = NULL;
c5a0d31a 4339 overlay_nocrossrefs = 0;
d22ec3b0
ILT
4340 overlay_list = NULL;
4341 overlay_max = NULL;
4342}
c5a0d31a
ILT
4343\f
4344/* Version handling. This is only useful for ELF. */
4345
4346/* This global variable holds the version tree that we build. */
4347
4348struct bfd_elf_version_tree *lang_elf_version_info;
4349
4350/* This is called for each variable name or match expression. */
4351
4352struct bfd_elf_version_expr *
4353lang_new_vers_regex (orig, new)
4354 struct bfd_elf_version_expr *orig;
4355 const char *new;
4356{
4357 struct bfd_elf_version_expr *ret;
4358
4359 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4360 ret->next = orig;
4361 ret->match = new;
4362 return ret;
4363}
4364
4365/* This is called for each set of variable names and match
4366 expressions. */
4367
4368struct bfd_elf_version_tree *
4369lang_new_vers_node (globals, locals)
4370 struct bfd_elf_version_expr *globals;
4371 struct bfd_elf_version_expr *locals;
4372{
4373 struct bfd_elf_version_tree *ret;
4374
4375 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4376 ret->next = NULL;
4377 ret->name = NULL;
4378 ret->vernum = 0;
4379 ret->globals = globals;
4380 ret->locals = locals;
4381 ret->deps = NULL;
4382 ret->name_indx = (unsigned int) -1;
4383 ret->used = 0;
4384 return ret;
4385}
4386
4387/* This static variable keeps track of version indices. */
4388
4389static int version_index;
4390
4391/* This is called when we know the name and dependencies of the
4392 version. */
4393
4394void
4395lang_register_vers_node (name, version, deps)
4396 const char *name;
4397 struct bfd_elf_version_tree *version;
4398 struct bfd_elf_version_deps *deps;
4399{
4400 struct bfd_elf_version_tree *t, **pp;
4401 struct bfd_elf_version_expr *e1;
4402
4403 /* Make sure this node has a unique name. */
4404 for (t = lang_elf_version_info; t != NULL; t = t->next)
4405 if (strcmp (t->name, name) == 0)
26c7ca95 4406 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
c5a0d31a
ILT
4407
4408 /* Check the global and local match names, and make sure there
4409 aren't any duplicates. */
4410
4411 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4412 {
4413 for (t = lang_elf_version_info; t != NULL; t = t->next)
4414 {
4415 struct bfd_elf_version_expr *e2;
4416
c5a0d31a
ILT
4417 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4418 if (strcmp (e1->match, e2->match) == 0)
26c7ca95 4419 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
c5a0d31a
ILT
4420 e1->match);
4421 }
4422 }
4423
4424 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4425 {
4426 for (t = lang_elf_version_info; t != NULL; t = t->next)
4427 {
4428 struct bfd_elf_version_expr *e2;
4429
4430 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4431 if (strcmp (e1->match, e2->match) == 0)
26c7ca95 4432 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
c5a0d31a 4433 e1->match);
c5a0d31a
ILT
4434 }
4435 }
4436
4437 version->deps = deps;
4438 version->name = name;
4439 ++version_index;
4440 version->vernum = version_index;
4441
4442 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4443 ;
4444 *pp = version;
4445}
4446
4447/* This is called when we see a version dependency. */
4448
4449struct bfd_elf_version_deps *
4450lang_add_vers_depend (list, name)
4451 struct bfd_elf_version_deps *list;
4452 const char *name;
4453{
4454 struct bfd_elf_version_deps *ret;
4455 struct bfd_elf_version_tree *t;
4456
4457 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4458 ret->next = list;
4459
4460 for (t = lang_elf_version_info; t != NULL; t = t->next)
4461 {
4462 if (strcmp (t->name, name) == 0)
4463 {
4464 ret->version_needed = t;
4465 return ret;
4466 }
4467 }
4468
26c7ca95 4469 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
c5a0d31a
ILT
4470
4471 return ret;
4472}