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