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