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