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