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