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