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