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