* Test case patch for more functional GPUIF implementation
[deliverable/binutils-gdb.git] / ld / ldlang.c
CommitLineData
e20873a7 1/* Linker command language support.
26c61946
ILT
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
075d7359 4
2fa0b342
DHW
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 1, or (at your option)
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
d22ec3b0
ILT
18along with GLD; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
2fa0b342 21
2fa0b342 22#include "bfd.h"
075d7359 23#include "sysdep.h"
4c2123b6 24#include "libiberty.h"
9f19768a 25#include "obstack.h"
c477527c 26#include "bfdlink.h"
2fa0b342
DHW
27
28#include "ld.h"
fcf276c4 29#include "ldmain.h"
7bc4a0d7 30#include "ldgram.h"
2fa0b342 31#include "ldexp.h"
fcf276c4 32#include "ldlang.h"
8c514453 33#include "ldemul.h"
2fa0b342 34#include "ldlex.h"
3f8d46e7 35#include "ldmisc.h"
c611e285 36#include "ldctor.h"
fcf276c4 37#include "ldfile.h"
86bc0974 38#include "fnmatch.h"
b98bd3db 39
d22ec3b0
ILT
40#include <ctype.h>
41
2fa0b342 42/* FORWARDS */
5af8e9d6
KR
43static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
44 size_t,
45 lang_statement_list_type*));
46
2fa0b342 47
d4c02e29 48/* LOCALS */
bfbdc80f 49static struct obstack stat_obstack;
075d7359 50
0b2f8d2e 51#define obstack_chunk_alloc xmalloc
bfbdc80f 52#define obstack_chunk_free free
075d7359 53static CONST char *startup_file;
d4c02e29
SC
54static lang_statement_list_type input_file_chain;
55static boolean placed_commons = false;
56static lang_output_section_statement_type *default_common_section;
57static boolean map_option_f;
58static bfd_vma print_dot;
59static lang_input_statement_type *first_file;
60static lang_statement_list_type lang_output_section_statement;
61static CONST char *current_target;
62static CONST char *output_target;
d4c02e29 63static lang_statement_list_type statement_list;
061e65f8 64static struct lang_phdr *lang_phdr_list;
ffc50032 65
9f629407
ILT
66static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
193c5f93 71 const char *target, boolean add_to_list));
9f629407 72static void init_os PARAMS ((lang_output_section_statement_type *s));
86bc0974 73static void exp_init_os PARAMS ((etree_type *));
23595ba5 74static void section_already_linked PARAMS ((bfd *, asection *, PTR));
86bc0974 75static boolean wildcardp PARAMS ((const char *));
9f629407
ILT
76static void wild_section PARAMS ((lang_wild_statement_type *ptr,
77 const char *section,
78 lang_input_statement_type *file,
79 lang_output_section_statement_type *output));
193c5f93 80static lang_input_statement_type *lookup_name PARAMS ((const char *name));
6a02a973
ILT
81static void load_symbols PARAMS ((lang_input_statement_type *entry,
82 lang_statement_list_type *));
86bc0974
ILT
83static void wild_file PARAMS ((lang_wild_statement_type *, const char *,
84 lang_input_statement_type *,
85 lang_output_section_statement_type *));
9f629407
ILT
86static void wild PARAMS ((lang_wild_statement_type *s,
87 const char *section, const char *file,
88 const char *target,
89 lang_output_section_statement_type *output));
90static bfd *open_output PARAMS ((const char *name));
91static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
309c8153
ILT
92static void open_input_bfds
93 PARAMS ((lang_statement_union_type *statement, boolean));
9f629407
ILT
94static void lang_reasonable_defaults PARAMS ((void));
95static void lang_place_undefineds PARAMS ((void));
9f629407
ILT
96static void map_input_to_output_sections
97 PARAMS ((lang_statement_union_type *s,
98 const char *target,
99 lang_output_section_statement_type *output_section_statement));
100static void print_output_section_statement
101 PARAMS ((lang_output_section_statement_type *output_section_statement));
102static void print_assignment
103 PARAMS ((lang_assignment_statement_type *assignment,
104 lang_output_section_statement_type *output_section));
105static void print_input_statement PARAMS ((lang_input_statement_type *statm));
c5a0d31a 106static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
9f629407
ILT
107static void print_input_section PARAMS ((lang_input_section_type *in));
108static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
109static void print_data_statement PARAMS ((lang_data_statement_type *data));
0cacbcbe 110static void print_address_statement PARAMS ((lang_address_statement_type *));
4fdbafb2 111static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
9f629407
ILT
112static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
113static void print_wild_statement
114 PARAMS ((lang_wild_statement_type *w,
115 lang_output_section_statement_type *os));
309c8153
ILT
116static void print_group
117 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
9f629407
ILT
118static void print_statement PARAMS ((lang_statement_union_type *s,
119 lang_output_section_statement_type *os));
708f7fbc
ILT
120static void print_statement_list PARAMS ((lang_statement_union_type *s,
121 lang_output_section_statement_type *os));
9f629407
ILT
122static void print_statements PARAMS ((void));
123static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
124 fill_type fill, unsigned int power,
125 asection *output_section_statement,
126 bfd_vma dot));
127static bfd_vma size_input_section
128 PARAMS ((lang_statement_union_type **this_ptr,
129 lang_output_section_statement_type *output_section_statement,
c477527c 130 fill_type fill, bfd_vma dot, boolean relax));
9f629407 131static void lang_finish PARAMS ((void));
d3c324e5 132static void ignore_bfd_errors PARAMS ((const char *, ...));
9f629407
ILT
133static void lang_check PARAMS ((void));
134static void lang_common PARAMS ((void));
c477527c 135static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
9f629407
ILT
136static void lang_place_orphans PARAMS ((void));
137static int topower PARAMS ((int));
1d169acc 138static void lang_set_startof PARAMS ((void));
9f629407 139static void reset_memory_regions PARAMS ((void));
061e65f8 140static void lang_record_phdrs PARAMS ((void));
9f629407 141
2fa0b342 142/* EXPORTS */
ffc50032 143lang_output_section_statement_type *abs_output_section;
d4c02e29 144lang_statement_list_type *stat_ptr = &statement_list;
a4d2a48e 145lang_statement_list_type file_chain = { 0 };
061e65f8 146const char *entry_symbol = NULL;
f054ce20 147boolean entry_from_cmdline;
d4c02e29 148boolean lang_has_input_file = false;
d4c02e29
SC
149boolean had_output_filename = false;
150boolean lang_float_flag = false;
97fbbaca 151boolean delete_output_file_on_failure = false;
582dd77f 152struct lang_nocrossrefs *nocrossref_list;
075d7359 153
9fce28ed
SC
154etree_type *base; /* Relocation base - or null */
155
156
b897ed67 157#if defined(__STDC__) || defined(ALMOST_STDC)
2fa0b342
DHW
158#define cat(a,b) a##b
159#else
160#define cat(a,b) a/**/b
161#endif
162
163#define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
164
165#define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
166
167#define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
168
0cacbcbe
ILT
169#define SECTION_NAME_MAP_LENGTH (16)
170
bfbdc80f 171PTR
8ddef552
DM
172stat_alloc (size)
173 size_t size;
bfbdc80f 174{
075d7359 175 return obstack_alloc (&stat_obstack, size);
bfbdc80f 176}
9f629407 177
1418c83b
SC
178/*----------------------------------------------------------------------
179 lang_for_each_statement walks the parse tree and calls the provided
180 function for each node
181*/
182
075d7359 183static void
8ddef552 184lang_for_each_statement_worker (func, s)
c477527c 185 void (*func) PARAMS ((lang_statement_union_type *));
8ddef552 186 lang_statement_union_type *s;
1418c83b 187{
075d7359
SC
188 for (; s != (lang_statement_union_type *) NULL; s = s->next)
189 {
190 func (s);
1418c83b 191
075d7359
SC
192 switch (s->header.type)
193 {
81016051 194 case lang_constructors_statement_enum:
075d7359 195 lang_for_each_statement_worker (func, constructor_list.head);
81016051 196 break;
1418c83b
SC
197 case lang_output_section_statement_enum:
198 lang_for_each_statement_worker
075d7359 199 (func,
1418c83b
SC
200 s->output_section_statement.children.head);
201 break;
202 case lang_wild_statement_enum:
203 lang_for_each_statement_worker
075d7359 204 (func,
1418c83b
SC
205 s->wild_statement.children.head);
206 break;
309c8153
ILT
207 case lang_group_statement_enum:
208 lang_for_each_statement_worker (func,
209 s->group_statement.children.head);
210 break;
1418c83b 211 case lang_data_statement_enum:
4fdbafb2 212 case lang_reloc_statement_enum:
1418c83b
SC
213 case lang_object_symbols_statement_enum:
214 case lang_output_statement_enum:
215 case lang_target_statement_enum:
216 case lang_input_section_enum:
217 case lang_input_statement_enum:
1418c83b
SC
218 case lang_assignment_statement_enum:
219 case lang_padding_statement_enum:
220 case lang_address_statement_enum:
0cacbcbe 221 case lang_fill_statement_enum:
1418c83b
SC
222 break;
223 default:
075d7359 224 FAIL ();
1418c83b
SC
225 break;
226 }
075d7359 227 }
1418c83b
SC
228}
229
230void
8ddef552 231lang_for_each_statement (func)
c477527c 232 void (*func) PARAMS ((lang_statement_union_type *));
1418c83b 233{
075d7359
SC
234 lang_for_each_statement_worker (func,
235 statement_list.head);
1418c83b 236}
075d7359 237
1418c83b 238/*----------------------------------------------------------------------*/
075d7359 239void
8ddef552
DM
240lang_list_init (list)
241 lang_statement_list_type *list;
2fa0b342 242{
075d7359
SC
243 list->head = (lang_statement_union_type *) NULL;
244 list->tail = &list->head;
2fa0b342
DHW
245}
246
1418c83b 247/*----------------------------------------------------------------------
075d7359 248
1418c83b
SC
249 build a new statement node for the parse tree
250
251 */
2fa0b342
DHW
252
253static
075d7359 254lang_statement_union_type *
8ddef552
DM
255new_statement (type, size, list)
256 enum statement_enum type;
5af8e9d6 257 size_t size;
8ddef552 258 lang_statement_list_type * list;
2fa0b342
DHW
259{
260 lang_statement_union_type *new = (lang_statement_union_type *)
075d7359
SC
261 stat_alloc (size);
262
2fa0b342 263 new->header.type = type;
075d7359
SC
264 new->header.next = (lang_statement_union_type *) NULL;
265 lang_statement_append (list, new, &new->header.next);
2fa0b342
DHW
266 return new;
267}
268
1418c83b
SC
269/*
270 Build a new input file node for the language. There are several ways
271 in which we treat an input file, eg, we only look at symbols, or
272 prefix it with a -l etc.
273
274 We can be supplied with requests for input files more than once;
275 they may, for example be split over serveral lines like foo.o(.text)
276 foo.o(.data) etc, so when asked for a file we check that we havn't
277 got it already so we don't duplicate the bfd.
278
279 */
2fa0b342 280static lang_input_statement_type *
193c5f93 281new_afile (name, file_type, target, add_to_list)
9f629407
ILT
282 CONST char *name;
283 lang_input_file_enum_type file_type;
284 CONST char *target;
193c5f93 285 boolean add_to_list;
2fa0b342 286{
193c5f93 287 lang_input_statement_type *p;
dc4726c2 288
193c5f93
ILT
289 if (add_to_list)
290 p = new_stat (lang_input_statement, stat_ptr);
291 else
292 {
293 p = ((lang_input_statement_type *)
294 stat_alloc (sizeof (lang_input_statement_type)));
295 p->header.next = NULL;
296 }
075d7359 297
2fa0b342
DHW
298 lang_has_input_file = true;
299 p->target = target;
075d7359
SC
300 switch (file_type)
301 {
302 case lang_input_file_is_symbols_only_enum:
303 p->filename = name;
304 p->is_archive = false;
305 p->real = true;
306 p->local_sym_name = name;
307 p->just_syms_flag = true;
308 p->search_dirs_flag = false;
309 break;
310 case lang_input_file_is_fake_enum:
311 p->filename = name;
312 p->is_archive = false;
313 p->real = false;
314 p->local_sym_name = name;
315 p->just_syms_flag = false;
316 p->search_dirs_flag = false;
317 break;
318 case lang_input_file_is_l_enum:
319 p->is_archive = true;
320 p->filename = name;
321 p->real = true;
ef76742f 322 p->local_sym_name = concat ("-l", name, (const char *) NULL);
075d7359
SC
323 p->just_syms_flag = false;
324 p->search_dirs_flag = true;
325 break;
075d7359 326 case lang_input_file_is_marker_enum:
193c5f93
ILT
327 p->filename = name;
328 p->is_archive = false;
329 p->real = false;
330 p->local_sym_name = name;
331 p->just_syms_flag = false;
332 p->search_dirs_flag = true;
333 break;
334 case lang_input_file_is_search_file_enum:
075d7359
SC
335 p->filename = name;
336 p->is_archive = false;
337 p->real = true;
338 p->local_sym_name = name;
339 p->just_syms_flag = false;
340 p->search_dirs_flag = true;
341 break;
342 case lang_input_file_is_file_enum:
343 p->filename = name;
344 p->is_archive = false;
345 p->real = true;
346 p->local_sym_name = name;
347 p->just_syms_flag = false;
348 p->search_dirs_flag = false;
349 break;
350 default:
351 FAIL ();
352 }
193c5f93 353 p->the_bfd = (bfd *) NULL;
075d7359 354 p->asymbols = (asymbol **) NULL;
075d7359
SC
355 p->next_real_file = (lang_statement_union_type *) NULL;
356 p->next = (lang_statement_union_type *) NULL;
2fa0b342 357 p->symbol_count = 0;
1d169acc 358 p->dynamic = config.dynamic_link;
3c8deccc 359 p->whole_archive = whole_archive;
5e6cd559 360 p->loaded = false;
075d7359
SC
361 lang_statement_append (&input_file_chain,
362 (lang_statement_union_type *) p,
363 &p->next_real_file);
2fa0b342
DHW
364 return p;
365}
366
367lang_input_statement_type *
8ddef552
DM
368lang_add_input_file (name, file_type, target)
369 CONST char *name;
370 lang_input_file_enum_type file_type;
371 CONST char *target;
2fa0b342 372{
1418c83b 373 lang_has_input_file = true;
193c5f93 374 return new_afile (name, file_type, target, true);
2fa0b342
DHW
375}
376
1418c83b 377/* Build enough state so that the parser can build its tree */
2fa0b342 378void
8ddef552 379lang_init ()
2fa0b342 380{
075d7359 381 obstack_begin (&stat_obstack, 1000);
2fa0b342 382
075d7359 383 stat_ptr = &statement_list;
bfbdc80f 384
075d7359 385 lang_list_init (stat_ptr);
2fa0b342 386
075d7359
SC
387 lang_list_init (&input_file_chain);
388 lang_list_init (&lang_output_section_statement);
389 lang_list_init (&file_chain);
390 first_file = lang_add_input_file ((char *) NULL,
391 lang_input_file_is_marker_enum,
392 (char *) NULL);
393 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
9d1fe8a4 394
686739e2 395 abs_output_section->bfd_section = bfd_abs_section_ptr;
9d1fe8a4 396
2fa0b342
DHW
397}
398
1418c83b 399/*----------------------------------------------------------------------
075d7359
SC
400 A region is an area of memory declared with the
401 MEMORY { name:org=exp, len=exp ... }
402 syntax.
2fa0b342 403
1418c83b 404 We maintain a list of all the regions here
2fa0b342 405
1418c83b
SC
406 If no regions are specified in the script, then the default is used
407 which is created when looked up to be the entire data space
2fa0b342
DHW
408*/
409
410static lang_memory_region_type *lang_memory_region_list;
411static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
412
413lang_memory_region_type *
8ddef552
DM
414lang_memory_region_lookup (name)
415 CONST char *CONST name;
2fa0b342 416{
b038ec7b 417 lang_memory_region_type *p;
075d7359 418
1418c83b 419 for (p = lang_memory_region_list;
075d7359
SC
420 p != (lang_memory_region_type *) NULL;
421 p = p->next)
422 {
423 if (strcmp (p->name, name) == 0)
424 {
425 return p;
426 }
2fa0b342 427 }
36ea6198
ILT
428
429#if 0
430 /* This code used to always use the first region in the list as the
431 default region. I changed it to instead use a region
432 encompassing all of memory as the default region. This permits
433 NOLOAD sections to work reasonably without requiring a region.
434 People should specify what region they mean, if they really want
435 a region. */
075d7359 436 if (strcmp (name, "*default*") == 0)
2fa0b342 437 {
075d7359
SC
438 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
439 {
440 return lang_memory_region_list;
441 }
2fa0b342 442 }
36ea6198
ILT
443#endif
444
075d7359
SC
445 {
446 lang_memory_region_type *new =
1d169acc 447 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
075d7359
SC
448
449 new->name = buystring (name);
450 new->next = (lang_memory_region_type *) NULL;
451
452 *lang_memory_region_list_tail = new;
453 lang_memory_region_list_tail = &new->next;
454 new->origin = 0;
d3c324e5
MM
455 new->flags = 0;
456 new->not_flags = 0;
8ddef552 457 new->length = ~(bfd_size_type)0;
075d7359
SC
458 new->current = 0;
459 new->had_full_message = false;
460
461 return new;
462 }
2fa0b342
DHW
463}
464
465
d3c324e5
MM
466lang_memory_region_type *
467lang_memory_default (section)
468 asection *section;
469{
470 lang_memory_region_type *p;
471
472 flagword sec_flags = section->flags;
473
474 /* Override SEC_DATA to mean a writable section. */
26c61946 475 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
d3c324e5
MM
476 sec_flags |= SEC_DATA;
477
478 for (p = lang_memory_region_list;
479 p != (lang_memory_region_type *) NULL;
480 p = p->next)
481 {
482 if ((p->flags & sec_flags) != 0
483 && (p->not_flags & sec_flags) == 0)
484 {
485 return p;
486 }
487 }
488 return lang_memory_region_lookup ("*default*");
489}
490
2fa0b342 491lang_output_section_statement_type *
8ddef552
DM
492lang_output_section_find (name)
493 CONST char *CONST name;
2fa0b342
DHW
494{
495 lang_statement_union_type *u;
496 lang_output_section_statement_type *lookup;
497
498 for (u = lang_output_section_statement.head;
075d7359 499 u != (lang_statement_union_type *) NULL;
2fa0b342 500 u = lookup->next)
075d7359
SC
501 {
502 lookup = &u->output_section_statement;
503 if (strcmp (name, lookup->name) == 0)
504 {
1418c83b
SC
505 return lookup;
506 }
075d7359
SC
507 }
508 return (lang_output_section_statement_type *) NULL;
2fa0b342
DHW
509}
510
511lang_output_section_statement_type *
8ddef552
DM
512lang_output_section_statement_lookup (name)
513 CONST char *CONST name;
2fa0b342
DHW
514{
515 lang_output_section_statement_type *lookup;
2fa0b342 516
075d7359
SC
517 lookup = lang_output_section_find (name);
518 if (lookup == (lang_output_section_statement_type *) NULL)
519 {
2fa0b342 520
075d7359
SC
521 lookup = (lang_output_section_statement_type *)
522 new_stat (lang_output_section_statement, stat_ptr);
523 lookup->region = (lang_memory_region_type *) NULL;
524 lookup->fill = 0;
525 lookup->block_value = 1;
526 lookup->name = name;
527
528 lookup->next = (lang_statement_union_type *) NULL;
529 lookup->bfd_section = (asection *) NULL;
530 lookup->processed = false;
86bc0974 531 lookup->sectype = normal_section;
075d7359
SC
532 lookup->addr_tree = (etree_type *) NULL;
533 lang_list_init (&lookup->children);
534
1b8a42f3
ILT
535 lookup->memspec = (CONST char *) NULL;
536 lookup->flags = 0;
537 lookup->subsection_alignment = -1;
538 lookup->section_alignment = -1;
539 lookup->load_base = (union etree_union *) NULL;
061e65f8 540 lookup->phdrs = NULL;
1b8a42f3 541
075d7359
SC
542 lang_statement_append (&lang_output_section_statement,
543 (lang_statement_union_type *) lookup,
544 &lookup->next);
545 }
546 return lookup;
547}
2fa0b342 548
d3c324e5
MM
549static void
550lang_map_flags (flag)
551 flagword flag;
552{
553 if (flag & SEC_ALLOC)
554 minfo ("a");
555
556 if (flag & SEC_CODE)
557 minfo ("x");
558
559 if (flag & SEC_READONLY)
560 minfo ("r");
561
562 if (flag & SEC_DATA)
563 minfo ("w");
564
565 if (flag & SEC_LOAD)
566 minfo ("l");
567}
568
2fa0b342 569void
8ddef552 570lang_map ()
2fa0b342
DHW
571{
572 lang_memory_region_type *m;
075d7359 573
0cacbcbe 574 minfo ("\nMemory Configuration\n\n");
d3c324e5
MM
575 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
576 "Name", "Origin", "Length", "Attributes");
ae475b39 577
2fa0b342 578 for (m = lang_memory_region_list;
075d7359
SC
579 m != (lang_memory_region_type *) NULL;
580 m = m->next)
2fa0b342 581 {
0cacbcbe
ILT
582 char buf[100];
583 int len;
584
585 fprintf (config.map_file, "%-16s ", m->name);
586
587 sprintf_vma (buf, m->origin);
588 minfo ("0x%s ", buf);
589 len = strlen (buf);
590 while (len < 16)
591 {
592 print_space ();
593 ++len;
594 }
595
d3c324e5
MM
596 minfo ("0x%V", m->length);
597 if (m->flags || m->not_flags)
598 {
599#ifndef BFD64
600 minfo (" ");
601#endif
602 if (m->flags)
603 {
604 print_space ();
605 lang_map_flags (m->flags);
606 }
607
608 if (m->not_flags)
609 {
610 minfo (" !");
611 lang_map_flags (m->not_flags);
612 }
613 }
614
615 print_nl ();
2fa0b342 616 }
2fa0b342 617
0cacbcbe 618 fprintf (config.map_file, "\nLinker script and memory map\n\n");
2fa0b342 619
0cacbcbe 620 print_statements ();
2fa0b342
DHW
621}
622
86bc0974
ILT
623/* Initialize an output section. */
624
075d7359 625static void
8ddef552 626init_os (s)
86bc0974 627 lang_output_section_statement_type *s;
2fa0b342 628{
cf2e4f5f
ILT
629 section_userdata_type *new;
630
86bc0974
ILT
631 if (s->bfd_section != NULL)
632 return;
633
cf2e4f5f
ILT
634 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
635 einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME);
636
637 new = ((section_userdata_type *)
638 stat_alloc (sizeof (section_userdata_type)));
075d7359
SC
639
640 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
641 if (s->bfd_section == (asection *) NULL)
642 s->bfd_section = bfd_make_section (output_bfd, s->name);
643 if (s->bfd_section == (asection *) NULL)
644 {
ddddcdf0 645 einfo ("%P%F: output format %s cannot represent section called %s\n",
075d7359 646 output_bfd->xvec->name, s->name);
9d1fe8a4 647 }
2fa0b342 648 s->bfd_section->output_section = s->bfd_section;
7bc4a0d7 649
2fa0b342
DHW
650 /* We initialize an output sections output offset to minus its own */
651 /* vma to allow us to output a section through itself */
652 s->bfd_section->output_offset = 0;
075d7359 653 get_userdata (s->bfd_section) = (PTR) new;
86bc0974
ILT
654
655 /* If there is a base address, make sure that any sections it might
656 mention are initialized. */
657 if (s->addr_tree != NULL)
658 exp_init_os (s->addr_tree);
659}
660
661/* Make sure that all output sections mentioned in an expression are
662 initialized. */
663
664static void
665exp_init_os (exp)
666 etree_type *exp;
667{
668 switch (exp->type.node_class)
669 {
670 case etree_assign:
671 exp_init_os (exp->assign.src);
672 break;
673
674 case etree_binary:
675 exp_init_os (exp->binary.lhs);
676 exp_init_os (exp->binary.rhs);
677 break;
678
679 case etree_trinary:
680 exp_init_os (exp->trinary.cond);
681 exp_init_os (exp->trinary.lhs);
682 exp_init_os (exp->trinary.rhs);
683 break;
684
685 case etree_unary:
686 exp_init_os (exp->unary.child);
687 break;
688
689 case etree_name:
690 switch (exp->type.node_code)
691 {
692 case ADDR:
5735ac9e 693 case LOADADDR:
86bc0974
ILT
694 case SIZEOF:
695 {
696 lang_output_section_statement_type *os;
697
698 os = lang_output_section_find (exp->name.name);
699 if (os != NULL && os->bfd_section == NULL)
700 init_os (os);
701 }
702 }
703 break;
704
705 default:
706 break;
707 }
2fa0b342 708}
23595ba5
ILT
709
710/* Sections marked with the SEC_LINK_ONCE flag should only be linked
711 once into the output. This routine checks each sections, and
712 arranges to discard it if a section of the same name has already
713 been linked. This code assumes that all relevant sections have the
714 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
715 section name. This is called via bfd_map_over_sections. */
716
717/*ARGSUSED*/
718static void
c5a0d31a 719section_already_linked (abfd, sec, data)
23595ba5
ILT
720 bfd *abfd;
721 asection *sec;
c5a0d31a 722 PTR data;
23595ba5 723{
c5a0d31a 724 lang_input_statement_type *entry = (lang_input_statement_type *) data;
23595ba5
ILT
725 struct sec_link_once
726 {
727 struct sec_link_once *next;
728 asection *sec;
729 };
730 static struct sec_link_once *sec_link_once_list;
731 flagword flags;
732 const char *name;
733 struct sec_link_once *l;
734
c5a0d31a
ILT
735 /* If we are only reading symbols from this object, then we want to
736 discard all sections. */
737 if (entry->just_syms_flag)
738 {
739 sec->output_section = bfd_abs_section_ptr;
740 sec->output_offset = sec->vma;
741 return;
742 }
743
23595ba5
ILT
744 flags = bfd_get_section_flags (abfd, sec);
745
746 if ((flags & SEC_LINK_ONCE) == 0)
747 return;
748
749 name = bfd_get_section_name (abfd, sec);
750
751 for (l = sec_link_once_list; l != NULL; l = l->next)
752 {
753 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
754 {
755 /* The section has already been linked. See if we should
756 issue a warning. */
757 switch (flags & SEC_LINK_DUPLICATES)
758 {
759 default:
760 abort ();
761
762 case SEC_LINK_DUPLICATES_DISCARD:
763 break;
764
765 case SEC_LINK_DUPLICATES_ONE_ONLY:
0a0d1bdd 766 einfo ("%P: %B: warning: ignoring duplicate section `%s'\n",
23595ba5
ILT
767 abfd, name);
768 break;
769
770 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
771 /* FIXME: We should really dig out the contents of both
772 sections and memcmp them. The COFF/PE spec says that
773 the Microsoft linker does not implement this
774 correctly, so I'm not going to bother doing it
775 either. */
776 /* Fall through. */
777 case SEC_LINK_DUPLICATES_SAME_SIZE:
778 if (bfd_section_size (abfd, sec)
779 != bfd_section_size (l->sec->owner, l->sec))
0a0d1bdd
DE
780 einfo ("%P: %B: warning: duplicate section `%s' has different size\n",
781 abfd, name);
23595ba5
ILT
782 break;
783 }
784
785 /* Set the output_section field so that wild_doit does not
786 create a lang_input_section structure for this section. */
787 sec->output_section = bfd_abs_section_ptr;
788
789 return;
790 }
791 }
792
793 /* This is the first section with this name. Record it. */
794
795 l = (struct sec_link_once *) xmalloc (sizeof *l);
796 l->sec = sec;
797 l->next = sec_link_once_list;
798 sec_link_once_list = l;
799}
64664e69
ILT
800\f
801/* The wild routines.
2fa0b342 802
64664e69
ILT
803 These expand statements like *(.text) and foo.o to a list of
804 explicit actions, like foo.o(.text), bar.o(.text) and
805 foo.o(.text, .data). */
1418c83b 806
9f19768a
ILT
807/* Return true if the PATTERN argument is a wildcard pattern.
808 Although backslashes are treated specially if a pattern contains
809 wildcards, we do not consider the mere presence of a backslash to
810 be enough to cause the the pattern to be treated as a wildcard.
811 That lets us handle DOS filenames more naturally. */
86bc0974
ILT
812
813static boolean
814wildcardp (pattern)
815 const char *pattern;
816{
817 const char *s;
818
819 for (s = pattern; *s != '\0'; ++s)
820 if (*s == '?'
86bc0974
ILT
821 || *s == '*'
822 || *s == '[')
823 return true;
824 return false;
825}
826
64664e69
ILT
827/* Add SECTION to the output section OUTPUT. Do this by creating a
828 lang_input_section statement which is placed at PTR. FILE is the
829 input file which holds SECTION. */
075d7359 830
0b3499f6 831void
8ddef552 832wild_doit (ptr, section, output, file)
64664e69
ILT
833 lang_statement_list_type *ptr;
834 asection *section;
835 lang_output_section_statement_type *output;
836 lang_input_statement_type *file;
2fa0b342 837{
6429ad7e 838 flagword flags;
23595ba5
ILT
839 boolean discard;
840
6429ad7e
ILT
841 flags = bfd_get_section_flags (section->owner, section);
842
23595ba5
ILT
843 discard = false;
844
845 /* If we are doing a final link, discard sections marked with
846 SEC_EXCLUDE. */
847 if (! link_info.relocateable
6429ad7e 848 && (flags & SEC_EXCLUDE) != 0)
23595ba5
ILT
849 discard = true;
850
851 /* Discard input sections which are assigned to a section named
852 DISCARD_SECTION_NAME. */
cf2e4f5f 853 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
23595ba5
ILT
854 discard = true;
855
6429ad7e
ILT
856 /* Discard debugging sections if we are stripping debugging
857 information. */
858 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
859 && (flags & SEC_DEBUGGING) != 0)
860 discard = true;
861
23595ba5 862 if (discard)
cf2e4f5f 863 {
23595ba5 864 if (section->output_section == NULL)
cf2e4f5f
ILT
865 {
866 /* This prevents future calls from assigning this section. */
867 section->output_section = bfd_abs_section_ptr;
868 }
869 return;
870 }
871
23595ba5 872 if (section->output_section == NULL)
ae475b39 873 {
b038ec7b 874 boolean first;
23595ba5 875 lang_input_section_type *new;
b038ec7b 876 flagword flags;
23595ba5
ILT
877
878 if (output->bfd_section == NULL)
b038ec7b
RH
879 {
880 init_os (output);
881 first = true;
882 }
883 else
884 first = false;
23595ba5 885
64664e69 886 /* Add a section reference to the list */
23595ba5 887 new = new_stat (lang_input_section, ptr);
64664e69
ILT
888
889 new->section = section;
890 new->ifile = file;
891 section->output_section = output->bfd_section;
892
b038ec7b
RH
893 flags = section->flags;
894
64664e69
ILT
895 /* We don't copy the SEC_NEVER_LOAD flag from an input section
896 to an output section, because we want to be able to include a
897 SEC_NEVER_LOAD section in the middle of an otherwise loaded
898 section (I don't know why we want to do this, but we do).
899 build_link_order in ldwrite.c handles this case by turning
b038ec7b
RH
900 the embedded SEC_NEVER_LOAD section into a fill. */
901
902 flags &= ~ SEC_NEVER_LOAD;
903
904 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
905 already been processed. One reason to do this is that on pe
906 format targets, .text$foo sections go into .text and it's odd
907 to see .text with SEC_LINK_ONCE set. */
908
909 if (! link_info.relocateable)
910 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
911
912 /* If this is not the first input section, and the SEC_READONLY
913 flag is not currently set, then don't set it just because the
914 input section has it set. */
915
916 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
917 flags &= ~ SEC_READONLY;
86bc0974 918
b038ec7b 919 section->output_section->flags |= flags;
86bc0974 920
b038ec7b
RH
921 /* If SEC_READONLY is not set in the input section, then clear
922 it from the output section. */
923 if ((section->flags & SEC_READONLY) == 0)
924 section->output_section->flags &= ~SEC_READONLY;
64664e69 925
86bc0974 926 switch (output->sectype)
64664e69 927 {
86bc0974
ILT
928 case normal_section:
929 break;
930 case dsect_section:
931 case copy_section:
932 case info_section:
933 case overlay_section:
934 output->bfd_section->flags &= ~SEC_ALLOC;
935 break;
936 case noload_section:
b038ec7b 937 output->bfd_section->flags &= ~SEC_LOAD;
64664e69 938 output->bfd_section->flags |= SEC_NEVER_LOAD;
86bc0974 939 break;
64664e69
ILT
940 }
941
942 if (section->alignment_power > output->bfd_section->alignment_power)
943 output->bfd_section->alignment_power = section->alignment_power;
944
cf2e4f5f 945 /* If supplied an aligment, then force it. */
64664e69
ILT
946 if (output->section_alignment != -1)
947 output->bfd_section->alignment_power = output->section_alignment;
e20873a7 948 }
2fa0b342
DHW
949}
950
64664e69
ILT
951/* Expand a wild statement for a particular FILE. SECTION may be
952 NULL, in which case it is a wild card. */
953
075d7359 954static void
8ddef552 955wild_section (ptr, section, file, output)
309c8153
ILT
956 lang_wild_statement_type *ptr;
957 const char *section;
958 lang_input_statement_type *file;
959 lang_output_section_statement_type *output;
2fa0b342 960{
075d7359
SC
961 if (file->just_syms_flag == false)
962 {
309c8153 963 register asection *s;
86bc0974
ILT
964 boolean wildcard;
965
966 if (section == NULL)
967 wildcard = false;
968 else
969 wildcard = wildcardp (section);
309c8153
ILT
970
971 for (s = file->the_bfd->sections; s != NULL; s = s->next)
075d7359 972 {
86bc0974
ILT
973 boolean match;
974
309c8153
ILT
975 /* Attach all sections named SECTION. If SECTION is NULL,
976 then attach all sections.
977
978 Previously, if SECTION was NULL, this code did not call
979 wild_doit if the SEC_IS_COMMON flag was set for the
980 section. I did not understand that, and I took it out.
981 --ian@cygnus.com. */
982
86bc0974
ILT
983 if (section == NULL)
984 match = true;
985 else
986 {
987 const char *name;
988
989 name = bfd_get_section_name (file->the_bfd, s);
990 if (wildcard)
991 match = fnmatch (section, name, 0) == 0 ? true : false;
992 else
993 match = strcmp (section, name) == 0 ? true : false;
994 }
995 if (match)
f9d3d71a 996 wild_doit (&ptr->children, s, output, file);
075d7359 997 }
2fa0b342 998 }
2fa0b342
DHW
999}
1000
64664e69
ILT
1001/* This is passed a file name which must have been seen already and
1002 added to the statement tree. We will see if it has been opened
1003 already and had its symbols read. If not then we'll read it. */
2fa0b342 1004
193c5f93
ILT
1005static lang_input_statement_type *
1006lookup_name (name)
64664e69 1007 const char *name;
2fa0b342
DHW
1008{
1009 lang_input_statement_type *search;
075d7359
SC
1010
1011 for (search = (lang_input_statement_type *) input_file_chain.head;
1012 search != (lang_input_statement_type *) NULL;
1013 search = (lang_input_statement_type *) search->next_real_file)
1014 {
1015 if (search->filename == (char *) NULL && name == (char *) NULL)
c477527c
ILT
1016 return search;
1017 if (search->filename != (char *) NULL
1018 && name != (char *) NULL
1019 && strcmp (search->filename, name) == 0)
1020 break;
075d7359 1021 }
2fa0b342 1022
c477527c 1023 if (search == (lang_input_statement_type *) NULL)
193c5f93
ILT
1024 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1025 false);
c477527c 1026
5e6cd559
ILT
1027 /* If we have already added this file, or this file is not real
1028 (FIXME: can that ever actually happen?) or the name is NULL
1029 (FIXME: can that ever actually happen?) don't add this file. */
193c5f93 1030 if (search->loaded
5e6cd559
ILT
1031 || ! search->real
1032 || search->filename == (const char *) NULL)
1033 return search;
1034
6a02a973 1035 load_symbols (search, (lang_statement_list_type *) NULL);
193c5f93
ILT
1036
1037 return search;
1038}
1039
1040/* Get the symbols for an input file. */
c477527c 1041
193c5f93 1042static void
6a02a973 1043load_symbols (entry, place)
193c5f93 1044 lang_input_statement_type *entry;
6a02a973 1045 lang_statement_list_type *place;
193c5f93 1046{
0b3499f6
ILT
1047 char **matching;
1048
193c5f93
ILT
1049 if (entry->loaded)
1050 return;
1051
1052 ldfile_open_file (entry);
1053
0b3499f6
ILT
1054 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1055 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1056 {
1057 bfd_error_type err;
6a02a973 1058 lang_statement_list_type *hold;
0b3499f6
ILT
1059
1060 err = bfd_get_error ();
1061 if (err == bfd_error_file_ambiguously_recognized)
1062 {
1063 char **p;
1064
1065 einfo ("%B: file not recognized: %E\n", entry->the_bfd);
1066 einfo ("%B: matching formats:", entry->the_bfd);
1067 for (p = matching; *p != NULL; p++)
1068 einfo (" %s", *p);
1069 einfo ("%F\n");
1070 }
6a02a973
ILT
1071 else if (err != bfd_error_file_not_recognized
1072 || place == NULL)
0b3499f6
ILT
1073 einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
1074
0b3499f6
ILT
1075 bfd_close (entry->the_bfd);
1076 entry->the_bfd = NULL;
1077
64664e69
ILT
1078 /* See if the emulation has some special knowledge. */
1079
1080 if (ldemul_unrecognized_file (entry))
1081 return;
1082
1083 /* Try to interpret the file as a linker script. */
1084
0b3499f6
ILT
1085 ldfile_open_command_file (entry->filename);
1086
6a02a973
ILT
1087 hold = stat_ptr;
1088 stat_ptr = place;
1089
0b3499f6
ILT
1090 ldfile_assumed_script = true;
1091 parser_input = input_script;
1092 yyparse ();
1093 ldfile_assumed_script = false;
1094
6a02a973
ILT
1095 stat_ptr = hold;
1096
0b3499f6
ILT
1097 return;
1098 }
1099
1100 /* We don't call ldlang_add_file for an archive. Instead, the
1101 add_symbols entry point will call ldlang_add_file, via the
1102 add_archive_element callback, for each element of the archive
1103 which is used. */
f400bbbb 1104 switch (bfd_get_format (entry->the_bfd))
5e6cd559 1105 {
f400bbbb
ILT
1106 default:
1107 break;
1108
1109 case bfd_object:
193c5f93 1110 ldlang_add_file (entry);
5e6cd559 1111 if (trace_files || trace_file_tries)
193c5f93 1112 info_msg ("%I\n", entry);
f400bbbb
ILT
1113 break;
1114
1115 case bfd_archive:
3c8deccc 1116 if (entry->whole_archive)
f400bbbb
ILT
1117 {
1118 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1119 (bfd *) NULL);
1120 while (member != NULL)
1121 {
1122 if (! bfd_check_format (member, bfd_object))
1123 einfo ("%F%B: object %B in archive is not object\n",
1124 entry->the_bfd, member);
1125 if (! ((*link_info.callbacks->add_archive_element)
0cacbcbe 1126 (&link_info, member, "--whole-archive")))
f400bbbb
ILT
1127 abort ();
1128 if (! bfd_link_add_symbols (member, &link_info))
1129 einfo ("%F%B: could not read symbols: %E\n", member);
1130 member = bfd_openr_next_archived_file (entry->the_bfd,
1131 member);
1132 }
1133
1134 entry->loaded = true;
1135
1136 return;
1137 }
5e6cd559 1138 }
1418c83b 1139
0b3499f6 1140 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
193c5f93 1141 einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
5e6cd559 1142
193c5f93 1143 entry->loaded = true;
2fa0b342
DHW
1144}
1145
86bc0974
ILT
1146/* Handle a wild statement for a single file F. */
1147
1148static void
1149wild_file (s, section, f, output)
1150 lang_wild_statement_type *s;
1151 const char *section;
1152 lang_input_statement_type *f;
1153 lang_output_section_statement_type *output;
1154{
1155 if (f->the_bfd == NULL
1156 || ! bfd_check_format (f->the_bfd, bfd_archive))
1157 wild_section (s, section, f, output);
1158 else
1159 {
1160 bfd *member;
1161
1162 /* This is an archive file. We must map each member of the
1163 archive separately. */
1164 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1165 while (member != NULL)
1166 {
1167 /* When lookup_name is called, it will call the add_symbols
1168 entry point for the archive. For each element of the
1169 archive which is included, BFD will call ldlang_add_file,
1170 which will set the usrdata field of the member to the
1171 lang_input_statement. */
1172 if (member->usrdata != NULL)
1173 {
1174 wild_section (s, section,
1175 (lang_input_statement_type *) member->usrdata,
1176 output);
1177 }
1178
1179 member = bfd_openr_next_archived_file (f->the_bfd, member);
1180 }
1181 }
1182}
1183
64664e69
ILT
1184/* Handle a wild statement. SECTION or FILE or both may be NULL,
1185 indicating that it is a wildcard. Separate lang_input_section
1186 statements are created for each part of the expansion; they are
1187 added after the wild statement S. OUTPUT is the output section. */
1188
2fa0b342 1189static void
8ddef552 1190wild (s, section, file, target, output)
64664e69
ILT
1191 lang_wild_statement_type *s;
1192 const char *section;
1193 const char *file;
1194 const char *target;
1195 lang_output_section_statement_type *output;
2fa0b342
DHW
1196{
1197 lang_input_statement_type *f;
075d7359
SC
1198
1199 if (file == (char *) NULL)
1200 {
1201 /* Perform the iteration over all files in the list */
1202 for (f = (lang_input_statement_type *) file_chain.head;
1203 f != (lang_input_statement_type *) NULL;
1204 f = (lang_input_statement_type *) f->next)
1205 {
86bc0974
ILT
1206 wild_file (s, section, f, output);
1207 }
1208 }
1209 else if (wildcardp (file))
1210 {
1211 for (f = (lang_input_statement_type *) file_chain.head;
1212 f != (lang_input_statement_type *) NULL;
1213 f = (lang_input_statement_type *) f->next)
1214 {
1215 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1216 wild_file (s, section, f, output);
075d7359
SC
1217 }
1218 }
1219 else
1220 {
1221 /* Perform the iteration over a single file */
3d0c8fa1 1222 f = lookup_name (file);
86bc0974 1223 wild_file (s, section, f, output);
075d7359 1224 }
64664e69 1225
075d7359
SC
1226 if (section != (char *) NULL
1227 && strcmp (section, "COMMON") == 0
64664e69 1228 && default_common_section == NULL)
075d7359 1229 {
64664e69
ILT
1230 /* Remember the section that common is going to in case we later
1231 get something which doesn't know where to put it. */
075d7359 1232 default_common_section = output;
2fa0b342 1233 }
2fa0b342
DHW
1234}
1235
64664e69 1236/* Open the output file. */
97fbbaca 1237
075d7359 1238static bfd *
8ddef552 1239open_output (name)
64664e69 1240 const char *name;
2fa0b342 1241{
097879bc 1242 bfd *output;
097879bc 1243
075d7359 1244 if (output_target == (char *) NULL)
ae475b39
SC
1245 {
1246 if (current_target != (char *) NULL)
1247 output_target = current_target;
1248 else
1249 output_target = default_target;
1250 }
075d7359 1251 output = bfd_openw (name, output_target);
075d7359
SC
1252
1253 if (output == (bfd *) NULL)
1254 {
5bcb7f28 1255 if (bfd_get_error () == bfd_error_invalid_target)
ae475b39 1256 {
ddddcdf0 1257 einfo ("%P%F: target %s not found\n", output_target);
ae475b39 1258 }
ddddcdf0 1259 einfo ("%P%F: cannot open output file %s: %E\n", name);
075d7359 1260 }
30d1a390 1261
86bc0974 1262 delete_output_file_on_failure = true;
97fbbaca 1263
30d1a390 1264 /* output->flags |= D_PAGED;*/
075d7359 1265
ddddcdf0
ILT
1266 if (! bfd_set_format (output, bfd_object))
1267 einfo ("%P%F:%s: can not make object file: %E\n", name);
1268 if (! bfd_set_arch_mach (output,
1269 ldfile_output_architecture,
1270 ldfile_output_machine))
1271 einfo ("%P%F:%s: can not set architecture: %E\n", name);
1272
c477527c
ILT
1273 link_info.hash = bfd_link_hash_table_create (output);
1274 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1275 einfo ("%P%F: can not create link hash table: %E\n");
1276
1b8a42f3 1277 bfd_set_gp_size (output, g_switch_value);
2fa0b342
DHW
1278 return output;
1279}
1418c83b
SC
1280
1281
097879bc 1282
1418c83b 1283
2fa0b342 1284static void
8ddef552
DM
1285ldlang_open_output (statement)
1286 lang_statement_union_type * statement;
2fa0b342 1287{
075d7359
SC
1288 switch (statement->header.type)
1289 {
c477527c
ILT
1290 case lang_output_statement_enum:
1291 ASSERT (output_bfd == (bfd *) NULL);
075d7359
SC
1292 output_bfd = open_output (statement->output_statement.name);
1293 ldemul_set_output_arch ();
c477527c 1294 if (config.magic_demand_paged && !link_info.relocateable)
075d7359
SC
1295 output_bfd->flags |= D_PAGED;
1296 else
1297 output_bfd->flags &= ~D_PAGED;
1298 if (config.text_read_only)
1299 output_bfd->flags |= WP_TEXT;
1300 else
1301 output_bfd->flags &= ~WP_TEXT;
6799c638 1302 if (link_info.traditional_format)
c96386c4
ILT
1303 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1304 else
1305 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
075d7359 1306 break;
1418c83b 1307
075d7359
SC
1308 case lang_target_statement_enum:
1309 current_target = statement->target_statement.target;
1310 break;
1311 default:
1312 break;
1313 }
1418c83b 1314}
2fa0b342 1315
309c8153
ILT
1316/* Open all the input files. */
1317
1418c83b 1318static void
309c8153
ILT
1319open_input_bfds (s, force)
1320 lang_statement_union_type *s;
1321 boolean force;
1418c83b 1322{
309c8153 1323 for (; s != (lang_statement_union_type *) NULL; s = s->next)
075d7359 1324 {
309c8153 1325 switch (s->header.type)
075d7359 1326 {
309c8153
ILT
1327 case lang_constructors_statement_enum:
1328 open_input_bfds (constructor_list.head, force);
1329 break;
1330 case lang_output_section_statement_enum:
1331 open_input_bfds (s->output_section_statement.children.head, force);
1332 break;
1333 case lang_wild_statement_enum:
1334 /* Maybe we should load the file's symbols */
86bc0974
ILT
1335 if (s->wild_statement.filename
1336 && ! wildcardp (s->wild_statement.filename))
309c8153
ILT
1337 (void) lookup_name (s->wild_statement.filename);
1338 open_input_bfds (s->wild_statement.children.head, force);
1339 break;
1340 case lang_group_statement_enum:
1341 {
1342 struct bfd_link_hash_entry *undefs;
1343
1344 /* We must continually search the entries in the group
1345 until no new symbols are added to the list of undefined
1346 symbols. */
1347
1348 do
1349 {
1350 undefs = link_info.hash->undefs_tail;
1351 open_input_bfds (s->group_statement.children.head, true);
1352 }
1353 while (undefs != link_info.hash->undefs_tail);
1354 }
1355 break;
1356 case lang_target_statement_enum:
1357 current_target = s->target_statement.target;
1358 break;
1359 case lang_input_statement_enum:
1360 if (s->input_statement.real == true)
1361 {
6a02a973
ILT
1362 lang_statement_list_type add;
1363
309c8153
ILT
1364 s->input_statement.target = current_target;
1365
1366 /* If we are being called from within a group, and this
1367 is an archive which has already been searched, then
1368 force it to be researched. */
1369 if (force
1370 && s->input_statement.loaded
1371 && bfd_check_format (s->input_statement.the_bfd,
1372 bfd_archive))
1373 s->input_statement.loaded = false;
1374
6a02a973
ILT
1375 lang_list_init (&add);
1376
1377 load_symbols (&s->input_statement, &add);
1378
1379 if (add.head != NULL)
1380 {
1381 *add.tail = s->next;
1382 s->next = add.head;
1383 }
309c8153
ILT
1384 }
1385 break;
1386 default:
1387 break;
075d7359 1388 }
075d7359 1389 }
2fa0b342 1390}
075d7359 1391
2fa0b342
DHW
1392/* If there are [COMMONS] statements, put a wild one into the bss section */
1393
1394static void
075d7359 1395lang_reasonable_defaults ()
2fa0b342 1396{
1418c83b 1397#if 0
075d7359
SC
1398 lang_output_section_statement_lookup (".text");
1399 lang_output_section_statement_lookup (".data");
8cb5eb31 1400
075d7359
SC
1401 default_common_section =
1402 lang_output_section_statement_lookup (".bss");
8cb5eb31 1403
1418c83b 1404
075d7359
SC
1405 if (placed_commons == false)
1406 {
1407 lang_wild_statement_type *new =
1408 new_stat (lang_wild_statement,
1409 &default_common_section->children);
1410
1411 new->section_name = "COMMON";
1412 new->filename = (char *) NULL;
1413 lang_list_init (&new->children);
1414 }
1418c83b 1415#endif
8cb5eb31 1416
2fa0b342
DHW
1417}
1418
1418c83b
SC
1419/*
1420 Add the supplied name to the symbol table as an undefined reference.
1421 Remove items from the chain as we open input bfds
1422 */
075d7359
SC
1423typedef struct ldlang_undef_chain_list
1424{
f177a611 1425 struct ldlang_undef_chain_list *next;
1418c83b 1426 char *name;
075d7359 1427} ldlang_undef_chain_list_type;
1418c83b
SC
1428
1429static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1430
1431void
8ddef552
DM
1432ldlang_add_undef (name)
1433 CONST char *CONST name;
2fa0b342 1434{
1418c83b 1435 ldlang_undef_chain_list_type *new =
1d169acc
ILT
1436 ((ldlang_undef_chain_list_type *)
1437 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1418c83b
SC
1438
1439 new->next = ldlang_undef_chain_list_head;
1440 ldlang_undef_chain_list_head = new;
1441
075d7359 1442 new->name = buystring (name);
1418c83b 1443}
075d7359 1444
1418c83b
SC
1445/* Run through the list of undefineds created above and place them
1446 into the linker hash table as undefined symbols belonging to the
1447 script file.
1448*/
1449static void
8ddef552 1450lang_place_undefineds ()
1418c83b 1451{
c477527c 1452 ldlang_undef_chain_list_type *ptr;
1418c83b 1453
c477527c
ILT
1454 for (ptr = ldlang_undef_chain_list_head;
1455 ptr != (ldlang_undef_chain_list_type *) NULL;
1456 ptr = ptr->next)
075d7359 1457 {
c477527c 1458 struct bfd_link_hash_entry *h;
075d7359 1459
c477527c
ILT
1460 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1461 if (h == (struct bfd_link_hash_entry *) NULL)
9f19768a 1462 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
c477527c
ILT
1463 if (h->type == bfd_link_hash_new)
1464 {
1465 h->type = bfd_link_hash_undefined;
1466 h->u.undef.abfd = NULL;
1467 bfd_link_add_undef (link_info.hash, h);
1468 }
075d7359
SC
1469 }
1470}
1418c83b 1471
2fa0b342
DHW
1472/* Open input files and attatch to output sections */
1473static void
8ddef552
DM
1474map_input_to_output_sections (s, target, output_section_statement)
1475 lang_statement_union_type * s;
1476 CONST char *target;
1477 lang_output_section_statement_type * output_section_statement;
2fa0b342 1478{
075d7359 1479 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 1480 {
075d7359 1481 switch (s->header.type)
2fa0b342 1482 {
075d7359
SC
1483
1484
1485 case lang_wild_statement_enum:
1486 wild (&s->wild_statement, s->wild_statement.section_name,
1487 s->wild_statement.filename, target,
1488 output_section_statement);
1489
1490 break;
1491 case lang_constructors_statement_enum:
1492 map_input_to_output_sections (constructor_list.head,
1493 target,
1494 output_section_statement);
1495 break;
1496 case lang_output_section_statement_enum:
1497 map_input_to_output_sections (s->output_section_statement.children.head,
1498 target,
1499 &s->output_section_statement);
1500 break;
1501 case lang_output_statement_enum:
1502 break;
1503 case lang_target_statement_enum:
1504 target = s->target_statement.target;
1505 break;
309c8153
ILT
1506 case lang_group_statement_enum:
1507 map_input_to_output_sections (s->group_statement.children.head,
1508 target,
1509 output_section_statement);
1510 break;
075d7359
SC
1511 case lang_fill_statement_enum:
1512 case lang_input_section_enum:
1513 case lang_object_symbols_statement_enum:
1514 case lang_data_statement_enum:
4fdbafb2 1515 case lang_reloc_statement_enum:
075d7359 1516 case lang_padding_statement_enum:
f9d3d71a
ILT
1517 case lang_input_statement_enum:
1518 if (output_section_statement != NULL
1519 && output_section_statement->bfd_section == NULL)
1520 init_os (output_section_statement);
075d7359 1521 break;
86bc0974
ILT
1522 case lang_assignment_statement_enum:
1523 if (output_section_statement != NULL
1524 && output_section_statement->bfd_section == NULL)
1525 init_os (output_section_statement);
1526
1527 /* Make sure that any sections mentioned in the assignment
1528 are initialized. */
1529 exp_init_os (s->assignment_statement.exp);
1530 break;
075d7359
SC
1531 case lang_afile_asection_pair_statement_enum:
1532 FAIL ();
1533 break;
1534 case lang_address_statement_enum:
1535 /* Mark the specified section with the supplied address */
1536 {
1537 lang_output_section_statement_type *os =
ef01a8a8
ILT
1538 lang_output_section_statement_lookup
1539 (s->address_statement.section_name);
075d7359 1540
ef01a8a8
ILT
1541 if (os->bfd_section == NULL)
1542 init_os (os);
075d7359 1543 os->addr_tree = s->address_statement.address;
48491e2e 1544 }
075d7359 1545 break;
2fa0b342 1546 }
2fa0b342
DHW
1547 }
1548}
1549
075d7359 1550static void
8ddef552
DM
1551print_output_section_statement (output_section_statement)
1552 lang_output_section_statement_type * output_section_statement;
2fa0b342
DHW
1553{
1554 asection *section = output_section_statement->bfd_section;
0cacbcbe 1555 int len;
075d7359 1556
0cacbcbe
ILT
1557 if (output_section_statement != abs_output_section)
1558 {
1559 minfo ("\n%s", output_section_statement->name);
075d7359 1560
0cacbcbe
ILT
1561 if (section != NULL)
1562 {
1563 print_dot = section->vma;
e20873a7 1564
0cacbcbe
ILT
1565 len = strlen (output_section_statement->name);
1566 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1567 {
1568 print_nl ();
1569 len = 0;
1570 }
1571 while (len < SECTION_NAME_MAP_LENGTH)
1572 {
1573 print_space ();
1574 ++len;
1575 }
2fa0b342 1576
0cacbcbe
ILT
1577 minfo ("0x%V %W", section->vma, section->_raw_size);
1578
1579 if (output_section_statement->load_base != NULL)
1580 {
1581 bfd_vma addr;
1582
1583 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1584 "load base", lang_final_phase_enum);
1585 minfo (" load address 0x%V", addr);
1586 }
1587 }
1588
1589 print_nl ();
e20873a7 1590 }
0cacbcbe 1591
708f7fbc
ILT
1592 print_statement_list (output_section_statement->children.head,
1593 output_section_statement);
2fa0b342
DHW
1594}
1595
075d7359 1596static void
8ddef552
DM
1597print_assignment (assignment, output_section)
1598 lang_assignment_statement_type * assignment;
1599 lang_output_section_statement_type * output_section;
2fa0b342 1600{
0cacbcbe 1601 int i;
2fa0b342 1602 etree_value_type result;
075d7359 1603
0cacbcbe
ILT
1604 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1605 print_space ();
075d7359 1606
0cacbcbe
ILT
1607 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1608 lang_final_phase_enum, print_dot, &print_dot);
075d7359 1609 if (result.valid)
0cacbcbe 1610 minfo ("0x%V", result.value + result.section->bfd_section->vma);
075d7359
SC
1611 else
1612 {
0cacbcbe
ILT
1613 minfo ("*undef* ");
1614#ifdef BFD64
1615 minfo (" ");
1616#endif
075d7359 1617 }
0cacbcbe
ILT
1618
1619 minfo (" ");
1620
075d7359
SC
1621 exp_print_tree (assignment->exp);
1622
0cacbcbe 1623 print_nl ();
2fa0b342
DHW
1624}
1625
1626static void
8ddef552
DM
1627print_input_statement (statm)
1628 lang_input_statement_type * statm;
2fa0b342 1629{
075d7359
SC
1630 if (statm->filename != (char *) NULL)
1631 {
1632 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1633 }
2fa0b342
DHW
1634}
1635
0cacbcbe
ILT
1636/* Print all symbols defined in a particular section. This is called
1637 via bfd_link_hash_traverse. */
804c8601
SC
1638
1639static boolean
1640print_one_symbol (hash_entry, ptr)
0cacbcbe
ILT
1641 struct bfd_link_hash_entry *hash_entry;
1642 PTR ptr;
2fa0b342 1643{
0cacbcbe 1644 asection *sec = (asection *) ptr;
804c8601 1645
0cacbcbe
ILT
1646 if ((hash_entry->type == bfd_link_hash_defined
1647 || hash_entry->type == bfd_link_hash_defweak)
1648 && sec == hash_entry->u.def.section)
804c8601 1649 {
0cacbcbe
ILT
1650 int i;
1651
1652 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1653 print_space ();
1654 minfo ("0x%V ",
1655 (hash_entry->u.def.value
1656 + hash_entry->u.def.section->output_offset
1657 + hash_entry->u.def.section->output_section->vma));
1658
1659 minfo (" %T\n", hash_entry->root.string);
804c8601 1660 }
09a5aa5e
KR
1661
1662 return true;
2fa0b342 1663}
1418c83b 1664
0cacbcbe
ILT
1665/* Print information about an input section to the map file. */
1666
075d7359 1667static void
8ddef552
DM
1668print_input_section (in)
1669 lang_input_section_type * in;
2fa0b342
DHW
1670{
1671 asection *i = in->section;
f078dc7c 1672 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1418c83b 1673
075d7359
SC
1674 if (size != 0)
1675 {
0cacbcbe
ILT
1676 print_space ();
1677
1678 minfo ("%s", i->name);
1679
1680 if (i->output_section != NULL)
075d7359 1681 {
0cacbcbe 1682 int len;
2fa0b342 1683
0cacbcbe
ILT
1684 len = 1 + strlen (i->name);
1685 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1686 {
1687 print_nl ();
1688 len = 0;
1689 }
1690 while (len < SECTION_NAME_MAP_LENGTH)
1691 {
1692 print_space ();
1693 ++len;
1694 }
2fa0b342 1695
0cacbcbe
ILT
1696 minfo ("0x%V %W %B\n",
1697 i->output_section->vma + i->output_offset, size,
1698 i->owner);
2fa0b342 1699
0cacbcbe
ILT
1700 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1701 {
1702 len = SECTION_NAME_MAP_LENGTH + 3;
1703#ifdef BFD64
1704 len += 16;
1705#else
1706 len += 8;
1707#endif
1708 while (len > 0)
075d7359 1709 {
0cacbcbe
ILT
1710 print_space ();
1711 --len;
075d7359 1712 }
2fa0b342 1713
0cacbcbe 1714 minfo ("%W (size before relaxing)\n", i->_raw_size);
075d7359
SC
1715 }
1716
0cacbcbe 1717 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
075d7359 1718
0cacbcbe 1719 print_dot = i->output_section->vma + i->output_offset + size;
075d7359
SC
1720 }
1721 }
1722}
2fa0b342
DHW
1723
1724static void
8ddef552
DM
1725print_fill_statement (fill)
1726 lang_fill_statement_type * fill;
075d7359 1727{
0cacbcbe 1728 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
075d7359
SC
1729}
1730
1731static void
8ddef552
DM
1732print_data_statement (data)
1733 lang_data_statement_type * data;
2fa0b342 1734{
0cacbcbe
ILT
1735 int i;
1736 bfd_vma addr;
1737 bfd_size_type size;
1738 const char *name;
1739
1740 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1741 print_space ();
1742
1743 addr = data->output_vma;
1744 if (data->output_section != NULL)
1745 addr += data->output_section->vma;
2fa0b342 1746
075d7359
SC
1747 switch (data->type)
1748 {
0cacbcbe
ILT
1749 default:
1750 abort ();
075d7359 1751 case BYTE:
0cacbcbe
ILT
1752 size = BYTE_SIZE;
1753 name = "BYTE";
075d7359
SC
1754 break;
1755 case SHORT:
0cacbcbe
ILT
1756 size = SHORT_SIZE;
1757 name = "SHORT";
075d7359
SC
1758 break;
1759 case LONG:
0cacbcbe
ILT
1760 size = LONG_SIZE;
1761 name = "LONG";
075d7359 1762 break;
c477527c 1763 case QUAD:
0cacbcbe
ILT
1764 size = QUAD_SIZE;
1765 name = "QUAD";
c477527c 1766 break;
9f19768a
ILT
1767 case SQUAD:
1768 size = QUAD_SIZE;
1769 name = "SQUAD";
1770 break;
075d7359
SC
1771 }
1772
0cacbcbe
ILT
1773 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1774
1775 if (data->exp->type.node_class != etree_value)
1776 {
1777 print_space ();
1778 exp_print_tree (data->exp);
1779 }
1780
1781 print_nl ();
1782
1783 print_dot = addr + size;
1784}
1785
1786/* Print an address statement. These are generated by options like
1787 -Ttext. */
2fa0b342 1788
0cacbcbe
ILT
1789static void
1790print_address_statement (address)
1791 lang_address_statement_type *address;
1792{
1793 minfo ("Address of section %s set to ", address->section_name);
1794 exp_print_tree (address->address);
1795 print_nl ();
2fa0b342
DHW
1796}
1797
4fdbafb2
ILT
1798/* Print a reloc statement. */
1799
1800static void
1801print_reloc_statement (reloc)
1802 lang_reloc_statement_type *reloc;
1803{
0cacbcbe
ILT
1804 int i;
1805 bfd_vma addr;
1806 bfd_size_type size;
1807
1808 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1809 print_space ();
4fdbafb2 1810
0cacbcbe
ILT
1811 addr = reloc->output_vma;
1812 if (reloc->output_section != NULL)
1813 addr += reloc->output_section->vma;
4fdbafb2 1814
0cacbcbe 1815 size = bfd_get_reloc_size (reloc->howto);
4fdbafb2 1816
0cacbcbe 1817 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
4fdbafb2 1818
0cacbcbe
ILT
1819 if (reloc->name != NULL)
1820 minfo ("%s+", reloc->name);
1821 else
1822 minfo ("%s+", reloc->section->name);
4fdbafb2
ILT
1823
1824 exp_print_tree (reloc->addend_exp);
1825
0cacbcbe
ILT
1826 print_nl ();
1827
1828 print_dot = addr + size;
4fdbafb2 1829}
2fa0b342
DHW
1830
1831static void
8ddef552 1832print_padding_statement (s)
0cacbcbe 1833 lang_padding_statement_type *s;
075d7359 1834{
0cacbcbe
ILT
1835 int len;
1836 bfd_vma addr;
1837
1838 minfo (" *fill*");
1839
1840 len = sizeof " *fill*" - 1;
1841 while (len < SECTION_NAME_MAP_LENGTH)
1842 {
1843 print_space ();
1844 ++len;
1845 }
1846
1847 addr = s->output_offset;
1848 if (s->output_section != NULL)
1849 addr += s->output_section->vma;
1850 minfo ("0x%V %W", addr, s->size);
1851
1852 if (s->fill != 0)
86bc0974 1853 minfo (" %u", s->fill);
0cacbcbe 1854
075d7359 1855 print_nl ();
ffc50032 1856
0cacbcbe 1857 print_dot = addr + s->size;
2fa0b342
DHW
1858}
1859
075d7359 1860static void
8ddef552
DM
1861print_wild_statement (w, os)
1862 lang_wild_statement_type * w;
1863 lang_output_section_statement_type * os;
2fa0b342 1864{
0cacbcbe
ILT
1865 print_space ();
1866
1867 if (w->filename != NULL)
1868 minfo ("%s", w->filename);
075d7359 1869 else
0cacbcbe
ILT
1870 minfo ("*");
1871
1872 if (w->section_name != NULL)
1873 minfo ("(%s)", w->section_name);
075d7359 1874 else
0cacbcbe
ILT
1875 minfo ("(*)");
1876
075d7359 1877 print_nl ();
2fa0b342 1878
0cacbcbe 1879 print_statement_list (w->children.head, os);
2fa0b342 1880}
309c8153
ILT
1881
1882/* Print a group statement. */
1883
1884static void
1885print_group (s, os)
1886 lang_group_statement_type *s;
1887 lang_output_section_statement_type *os;
1888{
1889 fprintf (config.map_file, "START GROUP\n");
708f7fbc 1890 print_statement_list (s->children.head, os);
309c8153
ILT
1891 fprintf (config.map_file, "END GROUP\n");
1892}
1893
708f7fbc
ILT
1894/* Print the list of statements in S.
1895 This can be called for any statement type. */
1896
2fa0b342 1897static void
708f7fbc 1898print_statement_list (s, os)
0cacbcbe
ILT
1899 lang_statement_union_type *s;
1900 lang_output_section_statement_type *os;
2fa0b342 1901{
0cacbcbe 1902 while (s != NULL)
c611e285 1903 {
708f7fbc 1904 print_statement (s, os);
075d7359 1905 s = s->next;
2fa0b342 1906 }
2fa0b342
DHW
1907}
1908
708f7fbc
ILT
1909/* Print the first statement in statement list S.
1910 This can be called for any statement type. */
1911
1912static void
1913print_statement (s, os)
0cacbcbe
ILT
1914 lang_statement_union_type *s;
1915 lang_output_section_statement_type *os;
708f7fbc
ILT
1916{
1917 switch (s->header.type)
1918 {
0cacbcbe
ILT
1919 default:
1920 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1921 FAIL ();
1922 break;
708f7fbc 1923 case lang_constructors_statement_enum:
0cacbcbe
ILT
1924 if (constructor_list.head != NULL)
1925 {
1926 minfo (" CONSTRUCTORS\n");
1927 print_statement_list (constructor_list.head, os);
1928 }
708f7fbc
ILT
1929 break;
1930 case lang_wild_statement_enum:
1931 print_wild_statement (&s->wild_statement, os);
1932 break;
708f7fbc 1933 case lang_address_statement_enum:
0cacbcbe 1934 print_address_statement (&s->address_statement);
708f7fbc
ILT
1935 break;
1936 case lang_object_symbols_statement_enum:
0cacbcbe 1937 minfo (" CREATE_OBJECT_SYMBOLS\n");
708f7fbc
ILT
1938 break;
1939 case lang_fill_statement_enum:
1940 print_fill_statement (&s->fill_statement);
1941 break;
1942 case lang_data_statement_enum:
1943 print_data_statement (&s->data_statement);
1944 break;
1945 case lang_reloc_statement_enum:
1946 print_reloc_statement (&s->reloc_statement);
1947 break;
1948 case lang_input_section_enum:
1949 print_input_section (&s->input_section);
1950 break;
1951 case lang_padding_statement_enum:
1952 print_padding_statement (&s->padding_statement);
1953 break;
1954 case lang_output_section_statement_enum:
1955 print_output_section_statement (&s->output_section_statement);
1956 break;
1957 case lang_assignment_statement_enum:
0cacbcbe 1958 print_assignment (&s->assignment_statement, os);
708f7fbc
ILT
1959 break;
1960 case lang_target_statement_enum:
1961 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1962 break;
1963 case lang_output_statement_enum:
0cacbcbe
ILT
1964 minfo ("OUTPUT(%s", s->output_statement.name);
1965 if (output_target != NULL)
1966 minfo (" %s", output_target);
1967 minfo (")\n");
708f7fbc
ILT
1968 break;
1969 case lang_input_statement_enum:
1970 print_input_statement (&s->input_statement);
1971 break;
1972 case lang_group_statement_enum:
1973 print_group (&s->group_statement, os);
1974 break;
1975 case lang_afile_asection_pair_statement_enum:
1976 FAIL ();
1977 break;
1978 }
1979}
1980
2fa0b342 1981static void
8ddef552 1982print_statements ()
2fa0b342 1983{
0cacbcbe 1984 print_statement_list (statement_list.head, abs_output_section);
2fa0b342
DHW
1985}
1986
708f7fbc
ILT
1987/* Print the first N statements in statement list S to STDERR.
1988 If N == 0, nothing is printed.
1989 If N < 0, the entire list is printed.
1990 Intended to be called from GDB. */
1991
1992void
1993dprint_statement (s, n)
1994 lang_statement_union_type * s;
1995 int n;
1996{
1997 FILE *map_save = config.map_file;
1998
1999 config.map_file = stderr;
2000
2001 if (n < 0)
2002 print_statement_list (s, abs_output_section);
2003 else
2004 {
86bc0974 2005 while (s && --n >= 0)
708f7fbc
ILT
2006 {
2007 print_statement (s, abs_output_section);
2008 s = s->next;
2009 }
2010 }
2011
2012 config.map_file = map_save;
2013}
2014
2fa0b342 2015static bfd_vma
9f629407
ILT
2016insert_pad (this_ptr, fill, power, output_section_statement, dot)
2017 lang_statement_union_type ** this_ptr;
2018 fill_type fill;
2019 unsigned int power;
2020 asection * output_section_statement;
2021 bfd_vma dot;
075d7359
SC
2022{
2023 /* Align this section first to the
2fa0b342
DHW
2024 input sections requirement, then
2025 to the output section's requirement.
075d7359 2026 If this alignment is > than any seen before,
2fa0b342
DHW
2027 then record it too. Perform the alignment by
2028 inserting a magic 'padding' statement.
2029 */
2030
075d7359 2031 unsigned int alignment_needed = align_power (dot, power) - dot;
2fa0b342 2032
075d7359 2033 if (alignment_needed != 0)
2fa0b342 2034 {
075d7359 2035 lang_statement_union_type *new =
1d169acc
ILT
2036 ((lang_statement_union_type *)
2037 stat_alloc (sizeof (lang_padding_statement_type)));
075d7359 2038
2fa0b342
DHW
2039 /* Link into existing chain */
2040 new->header.next = *this_ptr;
2041 *this_ptr = new;
2042 new->header.type = lang_padding_statement_enum;
2043 new->padding_statement.output_section = output_section_statement;
2044 new->padding_statement.output_offset =
2045 dot - output_section_statement->vma;
2046 new->padding_statement.fill = fill;
2047 new->padding_statement.size = alignment_needed;
2048 }
2049
2050
2051 /* Remember the most restrictive alignment */
075d7359
SC
2052 if (power > output_section_statement->alignment_power)
2053 {
2054 output_section_statement->alignment_power = power;
2055 }
c611e285 2056 output_section_statement->_raw_size += alignment_needed;
2fa0b342
DHW
2057 return alignment_needed + dot;
2058
2059}
2060
1418c83b 2061/* Work out how much this section will move the dot point */
075d7359 2062static bfd_vma
9f629407
ILT
2063size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2064 lang_statement_union_type ** this_ptr;
2065 lang_output_section_statement_type * output_section_statement;
c477527c 2066 fill_type fill;
9f629407
ILT
2067 bfd_vma dot;
2068 boolean relax;
2fa0b342
DHW
2069{
2070 lang_input_section_type *is = &((*this_ptr)->input_section);
2071 asection *i = is->section;
2fa0b342 2072
075d7359
SC
2073 if (is->ifile->just_syms_flag == false)
2074 {
e20873a7
JG
2075 if (output_section_statement->subsection_alignment != -1)
2076 i->alignment_power =
2077 output_section_statement->subsection_alignment;
2078
075d7359
SC
2079 dot = insert_pad (this_ptr, fill, i->alignment_power,
2080 output_section_statement->bfd_section, dot);
2081
075d7359 2082 /* Remember where in the output section this input section goes */
ac004870 2083
075d7359
SC
2084 i->output_offset = dot - output_section_statement->bfd_section->vma;
2085
ae475b39
SC
2086 /* Mark how big the output section must be to contain this now
2087 */
f078dc7c
ILT
2088 if (i->_cooked_size != 0)
2089 dot += i->_cooked_size;
ae475b39 2090 else
f078dc7c 2091 dot += i->_raw_size;
ae475b39 2092 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
075d7359 2093 }
ac004870 2094 else
075d7359
SC
2095 {
2096 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2097 }
2fa0b342 2098
075d7359 2099 return dot;
2fa0b342
DHW
2100}
2101
193c5f93
ILT
2102/* This variable indicates whether bfd_relax_section should be called
2103 again. */
2104
2105static boolean relax_again;
2106
2107/* Set the sizes for all the output sections. */
c611e285 2108
a62494c4 2109bfd_vma
9f629407
ILT
2110lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2111 lang_statement_union_type * s;
2112 lang_output_section_statement_type * output_section_statement;
2113 lang_statement_union_type ** prev;
c477527c 2114 fill_type fill;
9f629407
ILT
2115 bfd_vma dot;
2116 boolean relax;
c611e285
SC
2117{
2118 /* Size up the sections from their constituent parts */
075d7359 2119 for (; s != (lang_statement_union_type *) NULL; s = s->next)
ae475b39
SC
2120 {
2121 switch (s->header.type)
075d7359 2122 {
c611e285 2123
ae475b39
SC
2124 case lang_output_section_statement_enum:
2125 {
2126 bfd_vma after;
2127 lang_output_section_statement_type *os = &s->output_section_statement;
2128
f9d3d71a
ILT
2129 if (os->bfd_section == NULL)
2130 {
2131 /* This section was never actually created. */
2132 break;
2133 }
2134
27f7237e
ILT
2135 /* If this is a COFF shared library section, use the size and
2136 address from the input section. FIXME: This is COFF
2137 specific; it would be cleaner if there were some other way
2138 to do this, but nothing simple comes to mind. */
2139 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2140 {
2141 asection *input;
2142
2143 if (os->children.head == NULL
2144 || os->children.head->next != NULL
2145 || os->children.head->header.type != lang_input_section_enum)
9f19768a 2146 einfo ("%P%X: Internal error on COFF shared library section %s\n",
27f7237e
ILT
2147 os->name);
2148
2149 input = os->children.head->input_section.section;
2150 bfd_set_section_vma (os->bfd_section->owner,
2151 os->bfd_section,
2152 bfd_section_vma (input->owner, input));
2153 os->bfd_section->_raw_size = input->_raw_size;
2154 break;
2155 }
e9b63852 2156
686739e2 2157 if (bfd_is_abs_section (os->bfd_section))
ae475b39
SC
2158 {
2159 /* No matter what happens, an abs section starts at zero */
686739e2 2160 ASSERT (os->bfd_section->vma == 0);
ae475b39
SC
2161 }
2162 else
2163 {
2164 if (os->addr_tree == (etree_type *) NULL)
2165 {
2166 /* No address specified for this section, get one
2167 from the region specification
2168 */
26c61946
ILT
2169 if (os->region == (lang_memory_region_type *) NULL
2170 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2171 & (SEC_ALLOC | SEC_LOAD)) != 0)
2172 && os->region->name[0] == '*'
2173 && strcmp (os->region->name, "*default*") == 0))
ae475b39 2174 {
d3c324e5 2175 os->region = lang_memory_default (os->bfd_section);
ae475b39 2176 }
26c61946
ILT
2177
2178 /* If a loadable section is using the default memory
2179 region, and some non default memory regions were
2180 defined, issue a warning. */
2181 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2182 & (SEC_ALLOC | SEC_LOAD)) != 0
2183 && ! link_info.relocateable
2184 && strcmp (os->region->name, "*default*") == 0
2185 && lang_memory_region_list != NULL
2186 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2187 || lang_memory_region_list->next != NULL))
2188 einfo ("%P: warning: no memory region specified for section `%s'\n",
2189 bfd_get_section_name (output_bfd, os->bfd_section));
2190
ae475b39 2191 dot = os->region->current;
feaa9c4b 2192 if (os->section_alignment == -1)
c5a0d31a
ILT
2193 {
2194 bfd_vma olddot;
2195
2196 olddot = dot;
2197 dot = align_power (dot, os->bfd_section->alignment_power);
2198 if (dot != olddot && config.warn_section_align)
2199 einfo ("%P: warning: changing start of section %s by %u bytes\n",
2200 os->name, (unsigned int) (dot - olddot));
2201 }
ae475b39
SC
2202 }
2203 else
2204 {
2205 etree_value_type r;
2206
2207 r = exp_fold_tree (os->addr_tree,
2208 abs_output_section,
2209 lang_allocating_phase_enum,
2210 dot, &dot);
2211 if (r.valid == false)
2212 {
2213 einfo ("%F%S: non constant address expression for section %s\n",
2214 os->name);
2215 }
72df7117 2216 dot = r.value + r.section->bfd_section->vma;
ae475b39
SC
2217 }
2218 /* The section starts here */
2219 /* First, align to what the section needs */
2220
a62494c4
JL
2221 if (os->section_alignment != -1)
2222 dot = align_power (dot, os->section_alignment);
ae475b39 2223
ae475b39 2224 bfd_set_section_vma (0, os->bfd_section, dot);
9fce28ed 2225
686739e2
ILT
2226 os->bfd_section->output_offset = 0;
2227 }
ae475b39
SC
2228
2229 (void) lang_size_sections (os->children.head, os, &os->children.head,
2230 os->fill, dot, relax);
2231 /* Ignore the size of the input sections, use the vma and size to */
2232 /* align against */
2233
8ddef552 2234 after = ALIGN_N (os->bfd_section->vma +
97fbbaca
JL
2235 os->bfd_section->_raw_size,
2236 /* The coercion here is important, see ld.h. */
2237 (bfd_vma) os->block_value);
ae475b39 2238
686739e2
ILT
2239 if (bfd_is_abs_section (os->bfd_section))
2240 ASSERT (after == os->bfd_section->vma);
2241 else
2242 os->bfd_section->_raw_size = after - os->bfd_section->vma;
ae475b39
SC
2243 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2244 os->processed = true;
2245
2246 /* Replace into region ? */
686739e2 2247 if (os->region != (lang_memory_region_type *) NULL)
e802f0be
DE
2248 {
2249 os->region->current = dot;
2250 /* Make sure this isn't silly. */
09328690
ILT
2251 if (os->region->current < os->region->origin
2252 || (os->region->current - os->region->origin
2253 > os->region->length))
e802f0be
DE
2254 {
2255 if (os->addr_tree != (etree_type *) NULL)
2256 {
2257 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
2258 os->region->current,
2259 os->bfd_section->owner,
2260 os->bfd_section->name,
2261 os->region->name);
2262 }
2263 else
2264 {
2265 einfo ("%X%P: region %s is full (%B section %s)\n",
2266 os->region->name,
2267 os->bfd_section->owner,
2268 os->bfd_section->name);
2269 }
2270 /* Reset the region pointer. */
2271 os->region->current = os->region->origin;
2272 }
2273 }
ae475b39 2274 }
e802f0be 2275 break;
9d1fe8a4 2276
ae475b39
SC
2277 case lang_constructors_statement_enum:
2278 dot = lang_size_sections (constructor_list.head,
2279 output_section_statement,
2280 &s->wild_statement.children.head,
2281 fill,
2282 dot, relax);
2283 break;
9d1fe8a4 2284
ae475b39
SC
2285 case lang_data_statement_enum:
2286 {
2287 unsigned int size = 0;
2288
2289 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2290 s->data_statement.output_section =
2291 output_section_statement->bfd_section;
2292
2293 switch (s->data_statement.type)
9f19768a
ILT
2294 {
2295 case QUAD:
2296 case SQUAD:
2297 size = QUAD_SIZE;
2298 break;
2299 case LONG:
2300 size = LONG_SIZE;
2301 break;
2302 case SHORT:
2303 size = SHORT_SIZE;
2304 break;
2305 case BYTE:
2306 size = BYTE_SIZE;
2307 break;
2308 }
ae475b39 2309
ae475b39
SC
2310 dot += size;
2311 output_section_statement->bfd_section->_raw_size += size;
36ea6198
ILT
2312 /* The output section gets contents, and then we inspect for
2313 any flags set in the input script which override any ALLOC */
943fbd5b 2314 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
36ea6198
ILT
2315 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2316 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2317 }
ae475b39
SC
2318 }
2319 break;
c611e285 2320
4fdbafb2
ILT
2321 case lang_reloc_statement_enum:
2322 {
2323 int size;
2324
2325 s->reloc_statement.output_vma =
2326 dot - output_section_statement->bfd_section->vma;
2327 s->reloc_statement.output_section =
2328 output_section_statement->bfd_section;
2329 size = bfd_get_reloc_size (s->reloc_statement.howto);
2330 dot += size;
2331 output_section_statement->bfd_section->_raw_size += size;
2332 }
2333 break;
2334
ae475b39 2335 case lang_wild_statement_enum:
c611e285 2336
ae475b39
SC
2337 dot = lang_size_sections (s->wild_statement.children.head,
2338 output_section_statement,
2339 &s->wild_statement.children.head,
c611e285 2340
ae475b39 2341 fill, dot, relax);
c611e285 2342
ae475b39 2343 break;
c611e285 2344
ae475b39 2345 case lang_object_symbols_statement_enum:
c477527c
ILT
2346 link_info.create_object_symbols_section =
2347 output_section_statement->bfd_section;
ae475b39
SC
2348 break;
2349 case lang_output_statement_enum:
2350 case lang_target_statement_enum:
2351 break;
2352 case lang_input_section_enum:
ae475b39 2353 {
c477527c
ILT
2354 asection *i;
2355
193c5f93
ILT
2356 i = (*prev)->input_section.section;
2357 if (! relax)
6799c638
ILT
2358 {
2359 if (i->_cooked_size == 0)
2360 i->_cooked_size = i->_raw_size;
2361 }
193c5f93 2362 else
755f42fe 2363 {
193c5f93 2364 boolean again;
075d7359 2365
193c5f93
ILT
2366 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2367 einfo ("%P%F: can't relax section: %E\n");
2368 if (again)
2369 relax_again = true;
2370 }
2371 dot = size_input_section (prev,
2372 output_section_statement,
2373 output_section_statement->fill,
2374 dot, relax);
ae475b39 2375 }
ae475b39
SC
2376 break;
2377 case lang_input_statement_enum:
2378 break;
2379 case lang_fill_statement_enum:
2380 s->fill_statement.output_section = output_section_statement->bfd_section;
075d7359 2381
ae475b39
SC
2382 fill = s->fill_statement.fill;
2383 break;
2384 case lang_assignment_statement_enum:
2385 {
2386 bfd_vma newdot = dot;
2387
2388 exp_fold_tree (s->assignment_statement.exp,
2389 output_section_statement,
2390 lang_allocating_phase_enum,
2391 dot,
2392 &newdot);
2393
2394 if (newdot != dot && !relax)
27f7237e
ILT
2395 {
2396 /* The assignment changed dot. Insert a pad. */
2397 if (output_section_statement == abs_output_section)
2398 {
2399 /* If we don't have an output section, then just adjust
2400 the default memory address. */
2401 lang_memory_region_lookup ("*default*")->current = newdot;
2402 }
2403 else
2404 {
2405 lang_statement_union_type *new =
2406 ((lang_statement_union_type *)
2407 stat_alloc (sizeof (lang_padding_statement_type)));
2408
2409 /* Link into existing chain */
2410 new->header.next = *prev;
2411 *prev = new;
2412 new->header.type = lang_padding_statement_enum;
2413 new->padding_statement.output_section =
2414 output_section_statement->bfd_section;
2415 new->padding_statement.output_offset =
2416 dot - output_section_statement->bfd_section->vma;
2417 new->padding_statement.fill = fill;
2418 new->padding_statement.size = newdot - dot;
2419 output_section_statement->bfd_section->_raw_size +=
2420 new->padding_statement.size;
2421 }
2422
2423 dot = newdot;
2424 }
ae475b39 2425 }
27f7237e 2426 break;
9d87af56
ILT
2427
2428 case lang_padding_statement_enum:
2429 /* If we are relaxing, and this is not the first pass, some
2430 padding statements may have been inserted during previous
2431 passes. We may have to move the padding statement to a new
2432 location if dot has a different value at this point in this
2433 pass than it did at this point in the previous pass. */
2434 s->padding_statement.output_offset =
2435 dot - output_section_statement->bfd_section->vma;
2436 dot += s->padding_statement.size;
708f7fbc
ILT
2437 output_section_statement->bfd_section->_raw_size +=
2438 s->padding_statement.size;
9d87af56
ILT
2439 break;
2440
309c8153
ILT
2441 case lang_group_statement_enum:
2442 dot = lang_size_sections (s->group_statement.children.head,
2443 output_section_statement,
2444 &s->group_statement.children.head,
2445 fill, dot, relax);
2446 break;
2447
ae475b39
SC
2448 default:
2449 FAIL ();
2450 break;
9d87af56 2451
ae475b39 2452 /* This can only get here when relaxing is turned on */
075d7359 2453
ae475b39
SC
2454 case lang_address_statement_enum:
2455 break;
075d7359 2456 }
ae475b39
SC
2457 prev = &s->header.next;
2458 }
c611e285
SC
2459 return dot;
2460}
9d1fe8a4 2461
309c8153 2462bfd_vma
9f629407
ILT
2463lang_do_assignments (s, output_section_statement, fill, dot)
2464 lang_statement_union_type * s;
2465 lang_output_section_statement_type * output_section_statement;
c477527c 2466 fill_type fill;
9f629407 2467 bfd_vma dot;
2fa0b342 2468{
075d7359 2469 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 2470 {
075d7359 2471 switch (s->header.type)
2fa0b342 2472 {
075d7359
SC
2473 case lang_constructors_statement_enum:
2474 dot = lang_do_assignments (constructor_list.head,
2475 output_section_statement,
2476 fill,
2477 dot);
2478 break;
2479
2480 case lang_output_section_statement_enum:
2481 {
2482 lang_output_section_statement_type *os =
269773c1 2483 &(s->output_section_statement);
2fa0b342 2484
269773c1
ILT
2485 if (os->bfd_section != NULL)
2486 {
2487 dot = os->bfd_section->vma;
2488 (void) lang_do_assignments (os->children.head, os,
2489 os->fill, dot);
2490 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2491 }
0b3499f6
ILT
2492 if (os->load_base)
2493 {
467a0380
SC
2494 /* If nothing has been placed into the output section then
2495 it won't have a bfd_section. */
2496 if (os->bfd_section)
2497 {
2498 os->bfd_section->lma
2499 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2500 }
0b3499f6 2501 }
075d7359
SC
2502 }
2503 break;
2504 case lang_wild_statement_enum:
2fa0b342 2505
075d7359
SC
2506 dot = lang_do_assignments (s->wild_statement.children.head,
2507 output_section_statement,
2508 fill, dot);
2fa0b342 2509
075d7359
SC
2510 break;
2511
2512 case lang_object_symbols_statement_enum:
2513 case lang_output_statement_enum:
2514 case lang_target_statement_enum:
1418c83b 2515#if 0
075d7359 2516 case lang_common_statement_enum:
1418c83b 2517#endif
2fa0b342 2518 break;
075d7359
SC
2519 case lang_data_statement_enum:
2520 {
2521 etree_value_type value;
2522
2523 value = exp_fold_tree (s->data_statement.exp,
2524 abs_output_section,
2525 lang_final_phase_enum, dot, &dot);
2526 s->data_statement.value = value.value;
2527 if (value.valid == false)
ddddcdf0 2528 einfo ("%F%P: invalid data statement\n");
075d7359
SC
2529 }
2530 switch (s->data_statement.type)
2531 {
c477527c 2532 case QUAD:
9f19768a 2533 case SQUAD:
c477527c
ILT
2534 dot += QUAD_SIZE;
2535 break;
075d7359
SC
2536 case LONG:
2537 dot += LONG_SIZE;
2538 break;
2539 case SHORT:
2540 dot += SHORT_SIZE;
2541 break;
2542 case BYTE:
2543 dot += BYTE_SIZE;
2544 break;
2545 }
2fa0b342 2546 break;
4fdbafb2
ILT
2547
2548 case lang_reloc_statement_enum:
2549 {
2550 etree_value_type value;
2551
2552 value = exp_fold_tree (s->reloc_statement.addend_exp,
2553 abs_output_section,
2554 lang_final_phase_enum, dot, &dot);
2555 s->reloc_statement.addend_value = value.value;
2556 if (value.valid == false)
2557 einfo ("%F%P: invalid reloc statement\n");
2558 }
2559 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2560 break;
2561
075d7359
SC
2562 case lang_input_section_enum:
2563 {
2564 asection *in = s->input_section.section;
2565
f078dc7c
ILT
2566 if (in->_cooked_size != 0)
2567 dot += in->_cooked_size;
2568 else
2569 dot += in->_raw_size;
075d7359 2570 }
2fa0b342 2571 break;
2fa0b342 2572
075d7359
SC
2573 case lang_input_statement_enum:
2574 break;
2575 case lang_fill_statement_enum:
2576 fill = s->fill_statement.fill;
2577 break;
2578 case lang_assignment_statement_enum:
2579 {
2580 exp_fold_tree (s->assignment_statement.exp,
2581 output_section_statement,
2582 lang_final_phase_enum,
2583 dot,
2584 &dot);
2585 }
2586
2587 break;
2588 case lang_padding_statement_enum:
2589 dot += s->padding_statement.size;
2590 break;
309c8153
ILT
2591
2592 case lang_group_statement_enum:
2593 dot = lang_do_assignments (s->group_statement.children.head,
2594 output_section_statement,
2595 fill, dot);
2596
2597 break;
2598
075d7359
SC
2599 default:
2600 FAIL ();
2601 break;
2602 case lang_address_statement_enum:
2603 break;
2604 }
2fa0b342
DHW
2605
2606 }
2607 return dot;
2608}
2609
1d169acc
ILT
2610/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2611 operator .startof. (section_name), it produces an undefined symbol
2612 .startof.section_name. Similarly, when it sees
2613 .sizeof. (section_name), it produces an undefined symbol
2614 .sizeof.section_name. For all the output sections, we look for
2615 such symbols, and set them to the correct value. */
2616
2617static void
2618lang_set_startof ()
2619{
2620 asection *s;
2621
307585f1
ILT
2622 if (link_info.relocateable)
2623 return;
2624
1d169acc
ILT
2625 for (s = output_bfd->sections; s != NULL; s = s->next)
2626 {
2627 const char *secname;
2628 char *buf;
2629 struct bfd_link_hash_entry *h;
2630
2631 secname = bfd_get_section_name (output_bfd, s);
2632 buf = xmalloc (10 + strlen (secname));
2633
2634 sprintf (buf, ".startof.%s", secname);
2635 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2636 if (h != NULL && h->type == bfd_link_hash_undefined)
2637 {
2638 h->type = bfd_link_hash_defined;
2639 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2640 h->u.def.section = bfd_abs_section_ptr;
2641 }
2642
2643 sprintf (buf, ".sizeof.%s", secname);
2644 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2645 if (h != NULL && h->type == bfd_link_hash_undefined)
2646 {
2647 h->type = bfd_link_hash_defined;
2648 if (s->_cooked_size != 0)
2649 h->u.def.value = s->_cooked_size;
2650 else
2651 h->u.def.value = s->_raw_size;
2652 h->u.def.section = bfd_abs_section_ptr;
2653 }
2654
2655 free (buf);
2656 }
2657}
2658
2fa0b342 2659static void
8ddef552 2660lang_finish ()
2fa0b342 2661{
c477527c 2662 struct bfd_link_hash_entry *h;
cc38364d
KR
2663 boolean warn;
2664
2665 if (link_info.relocateable || link_info.shared)
2666 warn = false;
2667 else
2668 warn = true;
075d7359 2669
c477527c
ILT
2670 if (entry_symbol == (char *) NULL)
2671 {
2672 /* No entry has been specified. Look for start, but don't warn
2673 if we don't find it. */
2674 entry_symbol = "start";
2675 warn = false;
2676 }
2fa0b342 2677
c477527c
ILT
2678 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2679 if (h != (struct bfd_link_hash_entry *) NULL
8ed88239 2680 && (h->type == bfd_link_hash_defined
061e65f8
ILT
2681 || h->type == bfd_link_hash_defweak)
2682 && h->u.def.section->output_section != NULL)
075d7359 2683 {
c477527c 2684 bfd_vma val;
075d7359 2685
c477527c
ILT
2686 val = (h->u.def.value
2687 + bfd_get_section_vma (output_bfd,
2688 h->u.def.section->output_section)
2689 + h->u.def.section->output_offset);
2690 if (! bfd_set_start_address (output_bfd, val))
2691 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
075d7359 2692 }
c477527c 2693 else
22a78f0d 2694 {
c477527c
ILT
2695 asection *ts;
2696
2697 /* Can't find the entry symbol. Use the first address in the
2698 text section. */
2699 ts = bfd_get_section_by_name (output_bfd, ".text");
2700 if (ts != (asection *) NULL)
2701 {
2702 if (warn)
2703 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2704 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2705 if (! bfd_set_start_address (output_bfd,
2706 bfd_get_section_vma (output_bfd, ts)))
2707 einfo ("%P%F: can't set start address\n");
2708 }
2709 else
2710 {
2711 if (warn)
2712 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2713 entry_symbol);
2714 }
22a78f0d 2715 }
2fa0b342
DHW
2716}
2717
d3c324e5
MM
2718/* This is a small function used when we want to ignore errors from
2719 BFD. */
2720
2721static void
2722#ifdef ANSI_PROTOTYPES
2723ignore_bfd_errors (const char *s, ...)
2724#else
2725ignore_bfd_errors (s)
2726 const char *s;
2727#endif
2728{
2729 /* Don't do anything. */
2730}
2731
0b3499f6 2732/* Check that the architecture of all the input files is compatible
f400bbbb
ILT
2733 with the output file. Also call the backend to let it do any
2734 other checking that is needed. */
0b3499f6 2735
2fa0b342 2736static void
8ddef552 2737lang_check ()
2fa0b342
DHW
2738{
2739 lang_statement_union_type *file;
075d7359 2740 bfd *input_bfd;
075d7359 2741 CONST bfd_arch_info_type *compatible;
7bc4a0d7 2742
2fa0b342 2743 for (file = file_chain.head;
075d7359
SC
2744 file != (lang_statement_union_type *) NULL;
2745 file = file->input_statement.next)
2746 {
075d7359 2747 input_bfd = file->input_statement.the_bfd;
075d7359
SC
2748 compatible = bfd_arch_get_compatible (input_bfd,
2749 output_bfd);
0b3499f6 2750 if (compatible == NULL)
9f19768a 2751 {
d3c324e5
MM
2752 if (command_line.warn_mismatch)
2753 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2754 bfd_printable_name (input_bfd), input_bfd,
2755 bfd_printable_name (output_bfd));
2756 }
2757 else
2758 {
26c61946 2759 bfd_error_handler_type pfn = NULL;
d3c324e5
MM
2760
2761 /* If we aren't supposed to warn about mismatched input
2762 files, temporarily set the BFD error handler to a
2763 function which will do nothing. We still want to call
2764 bfd_merge_private_bfd_data, since it may set up
2765 information which is needed in the output file. */
2766 if (! command_line.warn_mismatch)
2767 pfn = bfd_set_error_handler (ignore_bfd_errors);
2768 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2769 {
2770 if (command_line.warn_mismatch)
2771 einfo ("%E%X: failed to merge target specific data of file %B\n",
2772 input_bfd);
2773 }
2774 if (! command_line.warn_mismatch)
2775 bfd_set_error_handler (pfn);
9f19768a 2776 }
075d7359
SC
2777 }
2778}
2fa0b342 2779
c477527c
ILT
2780/* Look through all the global common symbols and attach them to the
2781 correct section. The -sort-common command line switch may be used
2782 to roughly sort the entries by size. */
2fa0b342
DHW
2783
2784static void
8ddef552 2785lang_common ()
2fa0b342 2786{
c477527c
ILT
2787 if (link_info.relocateable
2788 && ! command_line.force_common_definition)
2789 return;
075d7359 2790
c477527c
ILT
2791 if (! config.sort_common)
2792 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2793 else
075d7359 2794 {
fa9dea80 2795 int power;
1418c83b 2796
fa9dea80 2797 for (power = 4; power >= 0; power--)
c477527c
ILT
2798 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2799 (PTR) &power);
2800 }
2801}
29f33467 2802
c477527c 2803/* Place one common symbol in the correct section. */
075d7359 2804
c477527c
ILT
2805static boolean
2806lang_one_common (h, info)
2807 struct bfd_link_hash_entry *h;
2808 PTR info;
2809{
2810 unsigned int power_of_two;
2811 bfd_vma size;
c477527c 2812 asection *section;
075d7359 2813
c477527c
ILT
2814 if (h->type != bfd_link_hash_common)
2815 return true;
075d7359 2816
c477527c 2817 size = h->u.c.size;
f400bbbb 2818 power_of_two = h->u.c.p->alignment_power;
36c6e8c3
ILT
2819
2820 if (config.sort_common
f054ce20 2821 && power_of_two < (unsigned int) *(int *) info)
c477527c 2822 return true;
075d7359 2823
f400bbbb 2824 section = h->u.c.p->section;
075d7359 2825
c477527c 2826 /* Increase the size of the section. */
36c6e8c3
ILT
2827 section->_raw_size = ALIGN_N (section->_raw_size,
2828 (bfd_size_type) (1 << power_of_two));
075d7359 2829
c477527c
ILT
2830 /* Adjust the alignment if necessary. */
2831 if (power_of_two > section->alignment_power)
2832 section->alignment_power = power_of_two;
075d7359 2833
c477527c
ILT
2834 /* Change the symbol from common to defined. */
2835 h->type = bfd_link_hash_defined;
2836 h->u.def.section = section;
2837 h->u.def.value = section->_raw_size;
097879bc 2838
c477527c
ILT
2839 /* Increase the size of the section. */
2840 section->_raw_size += size;
1418c83b 2841
c5a0d31a
ILT
2842 /* Make sure the section is allocated in memory, and make sure that
2843 it is no longer a common section. */
809391bd 2844 section->flags |= SEC_ALLOC;
c5a0d31a 2845 section->flags &= ~ SEC_IS_COMMON;
809391bd 2846
b495c314 2847 if (config.map_file != NULL)
efa6c497
ILT
2848 {
2849 static boolean header_printed;
2850 int len;
2851 char *name;
2852 char buf[50];
2853
2854 if (! header_printed)
2855 {
2856 minfo ("\nAllocating common symbols\n");
2857 minfo ("Common symbol size file\n\n");
2858 header_printed = true;
2859 }
2860
2861 name = demangle (h->root.string);
2862 minfo ("%s", name);
2863 len = strlen (name);
2864 free (name);
2865
2866 if (len >= 19)
2867 {
2868 print_nl ();
2869 len = 0;
2870 }
2871 while (len < 20)
2872 {
2873 print_space ();
2874 ++len;
2875 }
2876
2877 minfo ("0x");
2878 if (size <= 0xffffffff)
2879 sprintf (buf, "%lx", (unsigned long) size);
2880 else
2881 sprintf_vma (buf, size);
2882 minfo ("%s", buf);
2883 len = strlen (buf);
2884
2885 while (len < 16)
2886 {
2887 print_space ();
2888 ++len;
2889 }
2890
2891 minfo ("%B\n", section->owner);
2892 }
1418c83b 2893
c477527c 2894 return true;
2fa0b342
DHW
2895}
2896
2897/*
075d7359 2898run through the input files and ensure that every input
2fa0b342
DHW
2899section has somewhere to go. If one is found without
2900a destination then create an input request and place it
2901into the statement tree.
2902*/
2903
075d7359 2904static void
8ddef552 2905lang_place_orphans ()
2fa0b342
DHW
2906{
2907 lang_input_statement_type *file;
1418c83b 2908
075d7359
SC
2909 for (file = (lang_input_statement_type *) file_chain.head;
2910 file != (lang_input_statement_type *) NULL;
2911 file = (lang_input_statement_type *) file->next)
2912 {
2913 asection *s;
2914
2915 for (s = file->the_bfd->sections;
2916 s != (asection *) NULL;
2917 s = s->next)
2918 {
2919 if (s->output_section == (asection *) NULL)
2920 {
2921 /* This section of the file is not attatched, root
2922 around for a sensible place for it to go */
2923
e802f0be
DE
2924 if (file->just_syms_flag)
2925 {
2926 /* We are only retrieving symbol values from this
2927 file. We want the symbols to act as though the
2928 values in the file are absolute. */
2929 s->output_section = bfd_abs_section_ptr;
2930 s->output_offset = s->vma;
2931 }
86bc0974 2932 else if (strcmp (s->name, "COMMON") == 0)
075d7359 2933 {
86bc0974
ILT
2934 /* This is a lonely common section which must have
2935 come from an archive. We attach to the section
2936 with the wildcard. */
c477527c 2937 if (! link_info.relocateable
86bc0974 2938 || command_line.force_common_definition)
075d7359 2939 {
86bc0974 2940 if (default_common_section == NULL)
075d7359 2941 {
86bc0974
ILT
2942#if 0
2943 /* This message happens when using the
2944 svr3.ifile linker script, so I have
2945 disabled it. */
a4aeaacf 2946 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
86bc0974 2947#endif
075d7359
SC
2948 default_common_section =
2949 lang_output_section_statement_lookup (".bss");
2950
2951 }
2952 wild_doit (&default_common_section->children, s,
2953 default_common_section, file);
2954 }
2955 }
0b3499f6
ILT
2956 else if (ldemul_place_orphan (file, s))
2957 ;
075d7359
SC
2958 else
2959 {
2960 lang_output_section_statement_type *os =
2961 lang_output_section_statement_lookup (s->name);
2fa0b342 2962
075d7359
SC
2963 wild_doit (&os->children, s, os, file);
2964 }
2965 }
2fa0b342 2966 }
2fa0b342 2967 }
2fa0b342
DHW
2968}
2969
2970
2fa0b342 2971void
8ddef552 2972lang_set_flags (ptr, flags)
d3c324e5 2973 lang_memory_region_type *ptr;
8ddef552 2974 CONST char *flags;
2fa0b342 2975{
d3c324e5 2976 flagword *ptr_flags = &ptr->flags;
075d7359 2977
d3c324e5 2978 ptr->flags = ptr->not_flags = 0;
2fa0b342 2979 while (*flags)
075d7359 2980 {
075d7359
SC
2981 switch (*flags)
2982 {
d3c324e5
MM
2983 case '!':
2984 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
075d7359 2985 break;
d3c324e5
MM
2986
2987 case 'A': case 'a':
2988 *ptr_flags |= SEC_ALLOC;
075d7359 2989 break;
d3c324e5
MM
2990
2991 case 'R': case 'r':
2992 *ptr_flags |= SEC_READONLY;
2993 break;
2994
2995 case 'W': case 'w':
2996 *ptr_flags |= SEC_DATA;
075d7359 2997 break;
d3c324e5
MM
2998
2999 case 'X': case 'x':
3000 *ptr_flags |= SEC_CODE;
075d7359 3001 break;
d3c324e5
MM
3002
3003 case 'L': case 'l':
3004 case 'I': case 'i':
3005 *ptr_flags |= SEC_LOAD;
3006 break;
3007
075d7359 3008 default:
ddddcdf0 3009 einfo ("%P%F: invalid syntax in flags\n");
075d7359
SC
3010 break;
3011 }
dc4726c2
SC
3012 flags++;
3013 }
2fa0b342
DHW
3014}
3015
a4d2a48e
ILT
3016/* Call a function on each input file. This function will be called
3017 on an archive, but not on the elements. */
3018
3019void
3020lang_for_each_input_file (func)
3021 void (*func) PARAMS ((lang_input_statement_type *));
3022{
3023 lang_input_statement_type *f;
3024
3025 for (f = (lang_input_statement_type *) input_file_chain.head;
3026 f != NULL;
3027 f = (lang_input_statement_type *) f->next_real_file)
3028 func (f);
3029}
2fa0b342 3030
a4d2a48e
ILT
3031/* Call a function on each file. The function will be called on all
3032 the elements of an archive which are included in the link, but will
3033 not be called on the archive file itself. */
2fa0b342
DHW
3034
3035void
8ddef552
DM
3036lang_for_each_file (func)
3037 void (*func) PARAMS ((lang_input_statement_type *));
2fa0b342
DHW
3038{
3039 lang_input_statement_type *f;
075d7359
SC
3040
3041 for (f = (lang_input_statement_type *) file_chain.head;
3042 f != (lang_input_statement_type *) NULL;
3043 f = (lang_input_statement_type *) f->next)
3044 {
3045 func (f);
3046 }
2fa0b342
DHW
3047}
3048
9f629407
ILT
3049#if 0
3050
3051/* Not used. */
2fa0b342
DHW
3052
3053void
8ddef552
DM
3054lang_for_each_input_section (func)
3055 void (*func) PARAMS ((bfd * ab, asection * as));
2fa0b342
DHW
3056{
3057 lang_input_statement_type *f;
075d7359
SC
3058
3059 for (f = (lang_input_statement_type *) file_chain.head;
3060 f != (lang_input_statement_type *) NULL;
3061 f = (lang_input_statement_type *) f->next)
2fa0b342
DHW
3062 {
3063 asection *s;
075d7359 3064
2fa0b342 3065 for (s = f->the_bfd->sections;
075d7359
SC
3066 s != (asection *) NULL;
3067 s = s->next)
3068 {
3069 func (f->the_bfd, s);
3070 }
2fa0b342
DHW
3071 }
3072}
3073
9f629407 3074#endif
2fa0b342 3075
075d7359 3076void
8ddef552
DM
3077ldlang_add_file (entry)
3078 lang_input_statement_type * entry;
2fa0b342 3079{
5e6cd559
ILT
3080 bfd **pp;
3081
075d7359
SC
3082 lang_statement_append (&file_chain,
3083 (lang_statement_union_type *) entry,
3084 &entry->next);
c477527c
ILT
3085
3086 /* The BFD linker needs to have a list of all input BFDs involved in
3087 a link. */
3088 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3089 ASSERT (entry->the_bfd != output_bfd);
5e6cd559
ILT
3090 for (pp = &link_info.input_bfds;
3091 *pp != (bfd *) NULL;
3092 pp = &(*pp)->link_next)
3093 ;
3094 *pp = entry->the_bfd;
c477527c 3095 entry->the_bfd->usrdata = (PTR) entry;
fc1dfb71 3096 bfd_set_gp_size (entry->the_bfd, g_switch_value);
23595ba5
ILT
3097
3098 /* Look through the sections and check for any which should not be
3099 included in the link. We need to do this now, so that we can
3100 notice when the backend linker tries to report multiple
3101 definition errors for symbols which are in sections we aren't
3102 going to link. FIXME: It might be better to entirely ignore
3103 symbols which are defined in sections which are going to be
3104 discarded. This would require modifying the backend linker for
3105 each backend which might set the SEC_LINK_ONCE flag. If we do
3106 this, we should probably handle SEC_EXCLUDE in the same way. */
3107
c5a0d31a 3108 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
2fa0b342
DHW
3109}
3110
2fa0b342 3111void
2c6635a4 3112lang_add_output (name, from_script)
8ddef552 3113 CONST char *name;
2c6635a4 3114 int from_script;
2fa0b342 3115{
2c6635a4
ILT
3116 /* Make -o on command line override OUTPUT in script. */
3117 if (had_output_filename == false || !from_script)
3118 {
3119 output_filename = name;
3120 had_output_filename = true;
3121 }
2fa0b342
DHW
3122}
3123
3124
3125static lang_output_section_statement_type *current_section;
3126
f054ce20
ILT
3127static int
3128topower (x)
9f629407 3129 int x;
e20873a7 3130{
f054ce20 3131 unsigned int i = 1;
e20873a7 3132 int l;
f054ce20
ILT
3133
3134 if (x < 0)
3135 return -1;
3136
e20873a7 3137 for (l = 0; l < 32; l++)
f054ce20
ILT
3138 {
3139 if (i >= (unsigned int) x)
3140 return l;
3141 i <<= 1;
3142 }
3143
29f33467 3144 return 0;
e20873a7 3145}
86bc0974 3146
2fa0b342 3147void
8ddef552 3148lang_enter_output_section_statement (output_section_statement_name,
86bc0974 3149 address_exp, sectype, block_value,
9f629407 3150 align, subalign, ebase)
fcf276c4 3151 const char *output_section_statement_name;
8ddef552 3152 etree_type * address_exp;
86bc0974 3153 enum section_type sectype;
8ddef552
DM
3154 bfd_vma block_value;
3155 etree_type *align;
3156 etree_type *subalign;
9f629407 3157 etree_type *ebase;
2fa0b342
DHW
3158{
3159 lang_output_section_statement_type *os;
075d7359
SC
3160
3161 current_section =
e20873a7 3162 os =
075d7359
SC
3163 lang_output_section_statement_lookup (output_section_statement_name);
3164
3165
2fa0b342 3166
2fa0b342
DHW
3167 /* Add this statement to tree */
3168 /* add_statement(lang_output_section_statement_enum,
3169 output_section_statement);*/
3170 /* Make next things chain into subchain of this */
3171
3172 if (os->addr_tree ==
075d7359 3173 (etree_type *) NULL)
e20873a7
JG
3174 {
3175 os->addr_tree =
3176 address_exp;
3177 }
86bc0974
ILT
3178 os->sectype = sectype;
3179 if (sectype != noload_section)
3180 os->flags = SEC_NO_FLAGS;
ae475b39 3181 else
86bc0974 3182 os->flags = SEC_NEVER_LOAD;
29f33467 3183 os->block_value = block_value ? block_value : 1;
075d7359 3184 stat_ptr = &os->children;
2fa0b342 3185
e20873a7
JG
3186 os->subsection_alignment = topower(
3187 exp_get_value_int(subalign, -1,
3188 "subsection alignment",
3189 0));
3190 os->section_alignment = topower(
3191 exp_get_value_int(align, -1,
3192 "section alignment", 0));
9fce28ed 3193
9f629407 3194 os->load_base = ebase;
2fa0b342
DHW
3195}
3196
9fce28ed 3197
075d7359 3198void
8ddef552 3199lang_final ()
2fa0b342 3200{
2c6635a4
ILT
3201 lang_output_statement_type *new =
3202 new_stat (lang_output_statement, stat_ptr);
2fa0b342 3203
2c6635a4 3204 new->name = output_filename;
075d7359 3205}
2fa0b342 3206
c611e285
SC
3207/* Reset the current counters in the regions */
3208static void
8ddef552 3209reset_memory_regions ()
c611e285 3210{
075d7359
SC
3211 lang_memory_region_type *p = lang_memory_region_list;
3212
c611e285 3213 for (p = lang_memory_region_list;
075d7359
SC
3214 p != (lang_memory_region_type *) NULL;
3215 p = p->next)
3216 {
8ddef552 3217 p->old_length = (bfd_size_type) (p->current - p->origin);
075d7359
SC
3218 p->current = p->origin;
3219 }
c611e285 3220}
2fa0b342 3221
2fa0b342 3222void
8ddef552 3223lang_process ()
075d7359 3224{
075d7359 3225 lang_reasonable_defaults ();
1418c83b
SC
3226 current_target = default_target;
3227
075d7359 3228 lang_for_each_statement (ldlang_open_output); /* Open the output file */
1418c83b 3229
97fbbaca
JL
3230 ldemul_create_output_section_statements ();
3231
1418c83b 3232 /* Add to the hash table all undefineds on the command line */
075d7359 3233 lang_place_undefineds ();
1418c83b
SC
3234
3235 /* Create a bfd for each input file */
3236 current_target = default_target;
309c8153 3237 open_input_bfds (statement_list.head, false);
1418c83b 3238
cc38364d
KR
3239 ldemul_after_open ();
3240
9f19768a
ILT
3241 /* Make sure that we're not mixing architectures. We call this
3242 after all the input files have been opened, but before we do any
3243 other processing, so that any operations merge_private_bfd_data
3244 does on the output file will be known during the rest of the
3245 link. */
3246 lang_check ();
3247
c477527c
ILT
3248 /* Build all sets based on the information gathered from the input
3249 files. */
3250 ldctor_build_sets ();
3251
4fdbafb2
ILT
3252 /* Size up the common data */
3253 lang_common ();
3254
9f19768a 3255 /* Run through the contours of the script and attach input sections
075d7359 3256 to the correct output sections
1418c83b 3257 */
075d7359
SC
3258 map_input_to_output_sections (statement_list.head, (char *) NULL,
3259 (lang_output_section_statement_type *) NULL);
1418c83b 3260
81016051 3261
9f19768a 3262 /* Find any sections not attached explicitly and handle them */
075d7359 3263 lang_place_orphans ();
1418c83b 3264
075d7359 3265 ldemul_before_allocation ();
1418c83b 3266
061e65f8
ILT
3267 /* We must record the program headers before we try to fix the
3268 section positions, since they will affect SIZEOF_HEADERS. */
3269 lang_record_phdrs ();
3270
c611e285 3271 /* Now run around and relax if we can */
075d7359 3272 if (command_line.relax)
c611e285 3273 {
97fbbaca
JL
3274 /* First time round is a trial run to get the 'worst case'
3275 addresses of the objects if there was no relaxing. */
ae475b39 3276 lang_size_sections (statement_list.head,
193c5f93 3277 abs_output_section,
ae475b39 3278 &(statement_list.head), 0, (bfd_vma) 0, false);
c611e285 3279
193c5f93
ILT
3280 /* Keep relaxing until bfd_relax_section gives up. */
3281 do
3282 {
1d169acc
ILT
3283 reset_memory_regions ();
3284
193c5f93 3285 relax_again = false;
ae475b39 3286
193c5f93
ILT
3287 /* Do all the assignments with our current guesses as to
3288 section sizes. */
3289 lang_do_assignments (statement_list.head,
3290 abs_output_section,
3291 (fill_type) 0, (bfd_vma) 0);
ae475b39 3292
193c5f93
ILT
3293 /* Perform another relax pass - this time we know where the
3294 globals are, so can make better guess. */
3295 lang_size_sections (statement_list.head,
3296 abs_output_section,
3297 &(statement_list.head), 0, (bfd_vma) 0, true);
3298 }
3299 while (relax_again);
ae475b39 3300 }
ae475b39
SC
3301 else
3302 {
97fbbaca 3303 /* Size up the sections. */
ae475b39
SC
3304 lang_size_sections (statement_list.head,
3305 abs_output_section,
3306 &(statement_list.head), 0, (bfd_vma) 0, false);
075d7359 3307 }
c611e285 3308
1418c83b 3309 /* See if anything special should be done now we know how big
97fbbaca 3310 everything is. */
075d7359 3311 ldemul_after_allocation ();
1418c83b 3312
1d169acc
ILT
3313 /* Fix any .startof. or .sizeof. symbols. */
3314 lang_set_startof ();
3315
1418c83b
SC
3316 /* Do all the assignments, now that we know the final restingplaces
3317 of all the symbols */
3318
075d7359
SC
3319 lang_do_assignments (statement_list.head,
3320 abs_output_section,
c477527c 3321 (fill_type) 0, (bfd_vma) 0);
ffc50032 3322
1418c83b 3323 /* Final stuffs */
97fbbaca
JL
3324
3325 ldemul_finish ();
075d7359 3326 lang_finish ();
2fa0b342
DHW
3327}
3328
2fa0b342 3329/* EXPORTED TO YACC */
1418c83b 3330
2fa0b342 3331void
8ddef552
DM
3332lang_add_wild (section_name, filename)
3333 CONST char *CONST section_name;
3334 CONST char *CONST filename;
1418c83b 3335{
075d7359
SC
3336 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3337 stat_ptr);
1418c83b 3338
075d7359
SC
3339 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3340 {
3341 placed_commons = true;
3342 }
3343 if (filename != (char *) NULL)
3344 {
3345 lang_has_input_file = true;
3346 }
1418c83b
SC
3347 new->section_name = section_name;
3348 new->filename = filename;
075d7359 3349 lang_list_init (&new->children);
1418c83b 3350}
075d7359 3351
1418c83b 3352void
8ddef552
DM
3353lang_section_start (name, address)
3354 CONST char *name;
3355 etree_type * address;
2fa0b342 3356{
075d7359
SC
3357 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3358
2fa0b342
DHW
3359 ad->section_name = name;
3360 ad->address = address;
3361}
1418c83b 3362
60e8a534
ILT
3363/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3364 because of a -e argument on the command line, or zero if this is
3365 called by ENTRY in a linker script. Command line arguments take
3366 precedence. */
3367
3f38a017
SC
3368/* WINDOWS_NT. When an entry point has been specified, we will also force
3369 this symbol to be defined by calling ldlang_add_undef (equivalent to
3370 having switch -u entry_name on the command line). The reason we do
3371 this is so that the user doesn't have to because they would have to use
3372 the -u switch if they were specifying an entry point other than
3373 _mainCRTStartup. Specifically, if creating a windows application, entry
3374 point _WinMainCRTStartup must be specified.
3375 What I have found for non console applications (entry not _mainCRTStartup)
3376 is that the .obj that contains mainCRTStartup is brought in since it is
3377 the first encountered in libc.lib and it has other symbols in it which will
3378 be pulled in by the link process. To avoid this, adding -u with the entry
3379 point name specified forces the correct .obj to be used. We can avoid
3380 making the user do this by always adding the entry point name as an
3381 undefined symbol. */
3382
075d7359 3383void
60e8a534 3384lang_add_entry (name, cmdline)
8ddef552 3385 CONST char *name;
f054ce20 3386 boolean cmdline;
2fa0b342 3387{
60e8a534
ILT
3388 if (entry_symbol == NULL
3389 || cmdline
f054ce20 3390 || ! entry_from_cmdline)
60e8a534
ILT
3391 {
3392 entry_symbol = name;
f054ce20 3393 entry_from_cmdline = cmdline;
60e8a534 3394 }
943fbd5b 3395#if 0
3f38a017
SC
3396 /* don't do this yet. It seems to work (the executables run), but the
3397 image created is very different from what I was getting before indicating
3398 that something else is being pulled in. When everything else is working,
3399 then try to put this back in to see if it will do the right thing for
3400 other more complicated applications */
3401 ldlang_add_undef (name);
3402#endif
2fa0b342
DHW
3403}
3404
3405void
8ddef552
DM
3406lang_add_target (name)
3407 CONST char *name;
2fa0b342 3408{
075d7359
SC
3409 lang_target_statement_type *new = new_stat (lang_target_statement,
3410 stat_ptr);
3411
2fa0b342
DHW
3412 new->target = name;
3413
3414}
2fa0b342 3415
2fa0b342 3416void
8ddef552
DM
3417lang_add_map (name)
3418 CONST char *name;
2fa0b342 3419{
075d7359
SC
3420 while (*name)
3421 {
3422 switch (*name)
3423 {
3424 case 'F':
3425 map_option_f = true;
3426 break;
3427 }
3428 name++;
2fa0b342 3429 }
2fa0b342
DHW
3430}
3431
075d7359 3432void
8ddef552
DM
3433lang_add_fill (exp)
3434 int exp;
2fa0b342 3435{
075d7359
SC
3436 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3437 stat_ptr);
3438
2fa0b342
DHW
3439 new->fill = exp;
3440}
3441
075d7359 3442void
8ddef552
DM
3443lang_add_data (type, exp)
3444 int type;
3445 union etree_union *exp;
2fa0b342
DHW
3446{
3447
075d7359 3448 lang_data_statement_type *new = new_stat (lang_data_statement,
2fa0b342 3449 stat_ptr);
075d7359
SC
3450
3451 new->exp = exp;
3452 new->type = type;
2fa0b342
DHW
3453
3454}
075d7359 3455
4fdbafb2
ILT
3456/* Create a new reloc statement. RELOC is the BFD relocation type to
3457 generate. HOWTO is the corresponding howto structure (we could
3458 look this up, but the caller has already done so). SECTION is the
3459 section to generate a reloc against, or NAME is the name of the
3460 symbol to generate a reloc against. Exactly one of SECTION and
3461 NAME must be NULL. ADDEND is an expression for the addend. */
3462
3463void
3464lang_add_reloc (reloc, howto, section, name, addend)
3465 bfd_reloc_code_real_type reloc;
cc38364d 3466 reloc_howto_type *howto;
4fdbafb2
ILT
3467 asection *section;
3468 const char *name;
3469 union etree_union *addend;
3470{
3471 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3472
3473 p->reloc = reloc;
3474 p->howto = howto;
3475 p->section = section;
3476 p->name = name;
3477 p->addend_exp = addend;
3478
3479 p->addend_value = 0;
3480 p->output_section = NULL;
3481 p->output_vma = 0;
3482}
3483
2fa0b342 3484void
8ddef552
DM
3485lang_add_assignment (exp)
3486 etree_type * exp;
2fa0b342 3487{
075d7359
SC
3488 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3489 stat_ptr);
3490
2fa0b342
DHW
3491 new->exp = exp;
3492}
3493
3494void
8ddef552
DM
3495lang_add_attribute (attribute)
3496 enum statement_enum attribute;
2fa0b342 3497{
075d7359 3498 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2fa0b342
DHW
3499}
3500
075d7359 3501void
8ddef552
DM
3502lang_startup (name)
3503 CONST char *name;
2fa0b342 3504{
075d7359
SC
3505 if (startup_file != (char *) NULL)
3506 {
ddddcdf0 3507 einfo ("%P%Fmultiple STARTUP files\n");
075d7359 3508 }
2fa0b342
DHW
3509 first_file->filename = name;
3510 first_file->local_sym_name = name;
193c5f93 3511 first_file->real = true;
2fa0b342 3512
075d7359 3513 startup_file = name;
2fa0b342 3514}
075d7359
SC
3515
3516void
8ddef552
DM
3517lang_float (maybe)
3518 boolean maybe;
2fa0b342
DHW
3519{
3520 lang_float_flag = maybe;
3521}
3522
075d7359 3523void
d22ec3b0 3524lang_leave_output_section_statement (fill, memspec, phdrs)
8ddef552 3525 bfd_vma fill;
d22ec3b0
ILT
3526 const char *memspec;
3527 struct lang_output_section_phdr_list *phdrs;
2fa0b342
DHW
3528{
3529 current_section->fill = fill;
075d7359 3530 current_section->region = lang_memory_region_lookup (memspec);
d22ec3b0 3531 current_section->phdrs = phdrs;
2fa0b342
DHW
3532 stat_ptr = &statement_list;
3533}
075d7359 3534
9f32f7c2
SC
3535/*
3536 Create an absolute symbol with the given name with the value of the
3537 address of first byte of the section named.
2fa0b342 3538
9f32f7c2
SC
3539 If the symbol already exists, then do nothing.
3540*/
8cb5eb31 3541void
c477527c
ILT
3542lang_abs_symbol_at_beginning_of (secname, name)
3543 const char *secname;
3544 const char *name;
075d7359 3545{
c477527c
ILT
3546 struct bfd_link_hash_entry *h;
3547
3548 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3549 if (h == (struct bfd_link_hash_entry *) NULL)
3550 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3551
3552 if (h->type == bfd_link_hash_new
3553 || h->type == bfd_link_hash_undefined)
075d7359 3554 {
c477527c 3555 asection *sec;
075d7359 3556
c477527c
ILT
3557 h->type = bfd_link_hash_defined;
3558
3559 sec = bfd_get_section_by_name (output_bfd, secname);
3560 if (sec == (asection *) NULL)
3561 h->u.def.value = 0;
075d7359 3562 else
c477527c
ILT
3563 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3564
686739e2 3565 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3566 }
8cb5eb31
SC
3567}
3568
9f32f7c2
SC
3569/*
3570 Create an absolute symbol with the given name with the value of the
3571 address of the first byte after the end of the section named.
3572
3573 If the symbol already exists, then do nothing.
3574*/
2fa0b342 3575void
c477527c
ILT
3576lang_abs_symbol_at_end_of (secname, name)
3577 const char *secname;
3578 const char *name;
075d7359 3579{
c477527c
ILT
3580 struct bfd_link_hash_entry *h;
3581
3582 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3583 if (h == (struct bfd_link_hash_entry *) NULL)
3584 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3585
3586 if (h->type == bfd_link_hash_new
3587 || h->type == bfd_link_hash_undefined)
075d7359 3588 {
c477527c 3589 asection *sec;
075d7359 3590
c477527c 3591 h->type = bfd_link_hash_defined;
075d7359 3592
c477527c
ILT
3593 sec = bfd_get_section_by_name (output_bfd, secname);
3594 if (sec == (asection *) NULL)
3595 h->u.def.value = 0;
075d7359 3596 else
c477527c
ILT
3597 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3598 + bfd_section_size (output_bfd, sec));
3599
686739e2 3600 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3601 }
2fa0b342
DHW
3602}
3603
075d7359 3604void
8ddef552
DM
3605lang_statement_append (list, element, field)
3606 lang_statement_list_type * list;
3607 lang_statement_union_type * element;
3608 lang_statement_union_type ** field;
2fa0b342
DHW
3609{
3610 *(list->tail) = element;
3611 list->tail = field;
3612}
3613
173a0c3d 3614/* Set the output format type. -oformat overrides scripts. */
6a02a973 3615
097879bc 3616void
6a02a973
ILT
3617lang_add_output_format (format, big, little, from_script)
3618 const char *format;
3619 const char *big;
3620 const char *little;
173a0c3d 3621 int from_script;
097879bc 3622{
2c6635a4 3623 if (output_target == NULL || !from_script)
6a02a973
ILT
3624 {
3625 if (command_line.endian == ENDIAN_BIG
3626 && big != NULL)
3627 format = big;
3628 else if (command_line.endian == ENDIAN_LITTLE
3629 && little != NULL)
3630 format = little;
3631
3632 output_target = format;
3633 }
097879bc 3634}
309c8153
ILT
3635
3636/* Enter a group. This creates a new lang_group_statement, and sets
3637 stat_ptr to build new statements within the group. */
3638
3639void
3640lang_enter_group ()
3641{
3642 lang_group_statement_type *g;
3643
3644 g = new_stat (lang_group_statement, stat_ptr);
3645 lang_list_init (&g->children);
3646 stat_ptr = &g->children;
3647}
3648
3649/* Leave a group. This just resets stat_ptr to start writing to the
3650 regular list of statements again. Note that this will not work if
3651 groups can occur inside anything else which can adjust stat_ptr,
3652 but currently they can't. */
3653
3654void
3655lang_leave_group ()
3656{
3657 stat_ptr = &statement_list;
3658}
061e65f8
ILT
3659
3660/* Add a new program header. This is called for each entry in a PHDRS
3661 command in a linker script. */
3662
3663void
708f7fbc 3664lang_new_phdr (name, type, filehdr, phdrs, at, flags)
061e65f8
ILT
3665 const char *name;
3666 etree_type *type;
708f7fbc
ILT
3667 boolean filehdr;
3668 boolean phdrs;
061e65f8 3669 etree_type *at;
708f7fbc 3670 etree_type *flags;
061e65f8
ILT
3671{
3672 struct lang_phdr *n, **pp;
3673
3674 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3675 n->next = NULL;
3676 n->name = name;
3677 n->type = exp_get_value_int (type, 0, "program header type",
3678 lang_final_phase_enum);
708f7fbc
ILT
3679 n->filehdr = filehdr;
3680 n->phdrs = phdrs;
061e65f8 3681 n->at = at;
708f7fbc 3682 n->flags = flags;
061e65f8
ILT
3683
3684 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3685 ;
3686 *pp = n;
3687}
3688
061e65f8
ILT
3689/* Record the program header information in the output BFD. FIXME: We
3690 should not be calling an ELF specific function here. */
3691
3692static void
3693lang_record_phdrs ()
3694{
3695 unsigned int alc;
3696 asection **secs;
3697 struct lang_output_section_phdr_list *last;
3698 struct lang_phdr *l;
3699 lang_statement_union_type *u;
3700
3701 alc = 10;
c5a0d31a 3702 secs = (asection **) xmalloc (alc * sizeof (asection *));
061e65f8
ILT
3703 last = NULL;
3704 for (l = lang_phdr_list; l != NULL; l = l->next)
3705 {
3706 unsigned int c;
708f7fbc 3707 flagword flags;
061e65f8
ILT
3708 bfd_vma at;
3709
3710 c = 0;
3711 for (u = lang_output_section_statement.head;
3712 u != NULL;
3713 u = u->output_section_statement.next)
3714 {
3715 lang_output_section_statement_type *os;
3716 struct lang_output_section_phdr_list *pl;
3717
3718 os = &u->output_section_statement;
3719
708f7fbc 3720 pl = os->phdrs;
061e65f8
ILT
3721 if (pl != NULL)
3722 last = pl;
3723 else
3724 {
86bc0974 3725 if (os->sectype == noload_section
061e65f8
ILT
3726 || os->bfd_section == NULL
3727 || (os->bfd_section->flags & SEC_ALLOC) == 0)
3728 continue;
3729 pl = last;
3730 }
3731
3732 if (os->bfd_section == NULL)
3733 continue;
3734
3735 for (; pl != NULL; pl = pl->next)
3736 {
3737 if (strcmp (pl->name, l->name) == 0)
3738 {
3739 if (c >= alc)
3740 {
3741 alc *= 2;
c5a0d31a
ILT
3742 secs = ((asection **)
3743 xrealloc (secs, alc * sizeof (asection *)));
061e65f8
ILT
3744 }
3745 secs[c] = os->bfd_section;
3746 ++c;
3747 pl->used = true;
3748 }
3749 }
3750 }
3751
708f7fbc
ILT
3752 if (l->flags == NULL)
3753 flags = 0;
3754 else
3755 flags = exp_get_vma (l->flags, 0, "phdr flags",
3756 lang_final_phase_enum);
3757
061e65f8
ILT
3758 if (l->at == NULL)
3759 at = 0;
3760 else
3761 at = exp_get_vma (l->at, 0, "phdr load address",
3762 lang_final_phase_enum);
708f7fbc
ILT
3763
3764 if (! bfd_record_phdr (output_bfd, l->type,
3765 l->flags == NULL ? false : true,
3766 flags,
061e65f8 3767 l->at == NULL ? false : true,
708f7fbc 3768 at, l->filehdr, l->phdrs, c, secs))
061e65f8
ILT
3769 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3770 }
3771
3772 free (secs);
3773
3774 /* Make sure all the phdr assignments succeeded. */
3775 for (u = lang_output_section_statement.head;
3776 u != NULL;
3777 u = u->output_section_statement.next)
3778 {
3779 struct lang_output_section_phdr_list *pl;
3780
3781 if (u->output_section_statement.bfd_section == NULL)
3782 continue;
3783
3784 for (pl = u->output_section_statement.phdrs;
3785 pl != NULL;
3786 pl = pl->next)
3787 if (! pl->used && strcmp (pl->name, "NONE") != 0)
3788 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3789 u->output_section_statement.name, pl->name);
3790 }
3791}
582dd77f
ILT
3792
3793/* Record a list of sections which may not be cross referenced. */
3794
3795void
3796lang_add_nocrossref (l)
3797 struct lang_nocrossref *l;
3798{
3799 struct lang_nocrossrefs *n;
3800
3801 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
3802 n->next = nocrossref_list;
3803 n->list = l;
3804 nocrossref_list = n;
3805
3806 /* Set notice_all so that we get informed about all symbols. */
3807 link_info.notice_all = true;
3808}
d22ec3b0
ILT
3809\f
3810/* Overlay handling. We handle overlays with some static variables. */
3811
3812/* The overlay virtual address. */
3813static etree_type *overlay_vma;
3814
3815/* The overlay load address. */
3816static etree_type *overlay_lma;
3817
c5a0d31a
ILT
3818/* Whether nocrossrefs is set for this overlay. */
3819static int overlay_nocrossrefs;
3820
d22ec3b0
ILT
3821/* An expression for the maximum section size seen so far. */
3822static etree_type *overlay_max;
3823
3824/* A list of all the sections in this overlay. */
3825
3826struct overlay_list
3827{
3828 struct overlay_list *next;
3829 lang_output_section_statement_type *os;
3830};
3831
3832static struct overlay_list *overlay_list;
3833
3834/* Start handling an overlay. */
3835
3836void
c5a0d31a 3837lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
d22ec3b0
ILT
3838 etree_type *vma_expr;
3839 etree_type *lma_expr;
c5a0d31a 3840 int nocrossrefs;
d22ec3b0
ILT
3841{
3842 /* The grammar should prevent nested overlays from occurring. */
3843 ASSERT (overlay_vma == NULL
3844 && overlay_lma == NULL
3845 && overlay_list == NULL
3846 && overlay_max == NULL);
3847
3848 overlay_vma = vma_expr;
3849 overlay_lma = lma_expr;
c5a0d31a 3850 overlay_nocrossrefs = nocrossrefs;
d22ec3b0
ILT
3851}
3852
3853/* Start a section in an overlay. We handle this by calling
3854 lang_enter_output_section_statement with the correct VMA and LMA. */
3855
3856void
3857lang_enter_overlay_section (name)
3858 const char *name;
3859{
3860 struct overlay_list *n;
3861 etree_type *size;
3862
3863 lang_enter_output_section_statement (name, overlay_vma, normal_section,
3864 0, 0, 0, overlay_lma);
3865
3866 /* If this is the first section, then base the VMA and LMA of future
3867 sections on this one. This will work correctly even if `.' is
3868 used in the addresses. */
3869 if (overlay_list == NULL)
3870 {
3871 overlay_vma = exp_nameop (ADDR, name);
3872 overlay_lma = exp_nameop (LOADADDR, name);
3873 }
3874
3875 /* Remember the section. */
3876 n = (struct overlay_list *) xmalloc (sizeof *n);
3877 n->os = current_section;
3878 n->next = overlay_list;
3879 overlay_list = n;
3880
3881 size = exp_nameop (SIZEOF, name);
3882
3883 /* Adjust the LMA for the next section. */
3884 overlay_lma = exp_binop ('+', overlay_lma, size);
3885
3886 /* Arrange to work out the maximum section end address. */
3887 if (overlay_max == NULL)
3888 overlay_max = size;
3889 else
3890 overlay_max = exp_binop (MAX, overlay_max, size);
3891}
3892
3893/* Finish a section in an overlay. There isn't any special to do
3894 here. */
3895
3896void
3897lang_leave_overlay_section (fill, phdrs)
3898 bfd_vma fill;
3899 struct lang_output_section_phdr_list *phdrs;
3900{
3901 const char *name;
3902 char *clean, *s2;
3903 const char *s1;
3904 char *buf;
3905
3906 name = current_section->name;
3907
3908 lang_leave_output_section_statement (fill, "*default*", phdrs);
3909
3910 /* Define the magic symbols. */
3911
3912 clean = xmalloc (strlen (name) + 1);
3913 s2 = clean;
3914 for (s1 = name; *s1 != '\0'; s1++)
26c61946 3915 if (isalnum ((unsigned char) *s1) || *s1 == '_')
d22ec3b0
ILT
3916 *s2++ = *s1;
3917 *s2 = '\0';
3918
3919 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
3920 sprintf (buf, "__load_start_%s", clean);
3921 lang_add_assignment (exp_assop ('=', buf,
3922 exp_nameop (LOADADDR, name)));
3923
3924 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
3925 sprintf (buf, "__load_stop_%s", clean);
3926 lang_add_assignment (exp_assop ('=', buf,
3927 exp_binop ('+',
3928 exp_nameop (LOADADDR, name),
3929 exp_nameop (SIZEOF, name))));
3930
3931 free (clean);
3932}
3933
3934/* Finish an overlay. If there are any overlay wide settings, this
3935 looks through all the sections in the overlay and sets them. */
3936
3937void
3938lang_leave_overlay (fill, memspec, phdrs)
3939 bfd_vma fill;
3940 const char *memspec;
3941 struct lang_output_section_phdr_list *phdrs;
3942{
3943 lang_memory_region_type *region;
3944 struct overlay_list *l;
3945 struct lang_nocrossref *nocrossref;
3946
3947 if (memspec == NULL)
3948 region = NULL;
3949 else
3950 region = lang_memory_region_lookup (memspec);
3951
3952 nocrossref = NULL;
3953
3954 l = overlay_list;
3955 while (l != NULL)
3956 {
d22ec3b0
ILT
3957 struct overlay_list *next;
3958
3959 if (fill != 0 && l->os->fill == 0)
3960 l->os->fill = fill;
3961 if (region != NULL && l->os->region == NULL)
3962 l->os->region = region;
3963 if (phdrs != NULL && l->os->phdrs == NULL)
3964 l->os->phdrs = phdrs;
3965
c5a0d31a
ILT
3966 if (overlay_nocrossrefs)
3967 {
3968 struct lang_nocrossref *nc;
3969
3970 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
3971 nc->name = l->os->name;
3972 nc->next = nocrossref;
3973 nocrossref = nc;
3974 }
d22ec3b0
ILT
3975
3976 next = l->next;
3977 free (l);
3978 l = next;
3979 }
3980
3981 if (nocrossref != NULL)
3982 lang_add_nocrossref (nocrossref);
3983
3984 /* Update . for the end of the overlay. */
3985 lang_add_assignment (exp_assop ('=', ".",
3986 exp_binop ('+', overlay_vma, overlay_max)));
3987
3988 overlay_vma = NULL;
3989 overlay_lma = NULL;
c5a0d31a 3990 overlay_nocrossrefs = 0;
d22ec3b0
ILT
3991 overlay_list = NULL;
3992 overlay_max = NULL;
3993}
c5a0d31a
ILT
3994\f
3995/* Version handling. This is only useful for ELF. */
3996
3997/* This global variable holds the version tree that we build. */
3998
3999struct bfd_elf_version_tree *lang_elf_version_info;
4000
4001/* This is called for each variable name or match expression. */
4002
4003struct bfd_elf_version_expr *
4004lang_new_vers_regex (orig, new)
4005 struct bfd_elf_version_expr *orig;
4006 const char *new;
4007{
4008 struct bfd_elf_version_expr *ret;
4009
4010 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4011 ret->next = orig;
4012 ret->match = new;
4013 return ret;
4014}
4015
4016/* This is called for each set of variable names and match
4017 expressions. */
4018
4019struct bfd_elf_version_tree *
4020lang_new_vers_node (globals, locals)
4021 struct bfd_elf_version_expr *globals;
4022 struct bfd_elf_version_expr *locals;
4023{
4024 struct bfd_elf_version_tree *ret;
4025
4026 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4027 ret->next = NULL;
4028 ret->name = NULL;
4029 ret->vernum = 0;
4030 ret->globals = globals;
4031 ret->locals = locals;
4032 ret->deps = NULL;
4033 ret->name_indx = (unsigned int) -1;
4034 ret->used = 0;
4035 return ret;
4036}
4037
4038/* This static variable keeps track of version indices. */
4039
4040static int version_index;
4041
4042/* This is called when we know the name and dependencies of the
4043 version. */
4044
4045void
4046lang_register_vers_node (name, version, deps)
4047 const char *name;
4048 struct bfd_elf_version_tree *version;
4049 struct bfd_elf_version_deps *deps;
4050{
4051 struct bfd_elf_version_tree *t, **pp;
4052 struct bfd_elf_version_expr *e1;
4053
4054 /* Make sure this node has a unique name. */
4055 for (t = lang_elf_version_info; t != NULL; t = t->next)
4056 if (strcmp (t->name, name) == 0)
4057 einfo ("%X%P: duplicate version tag `%s'\n", name);
4058
4059 /* Check the global and local match names, and make sure there
4060 aren't any duplicates. */
4061
4062 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4063 {
4064 for (t = lang_elf_version_info; t != NULL; t = t->next)
4065 {
4066 struct bfd_elf_version_expr *e2;
4067
c5a0d31a
ILT
4068 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4069 if (strcmp (e1->match, e2->match) == 0)
4070 einfo ("%X%P: duplicate expression `%s' in version information\n",
4071 e1->match);
4072 }
4073 }
4074
4075 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4076 {
4077 for (t = lang_elf_version_info; t != NULL; t = t->next)
4078 {
4079 struct bfd_elf_version_expr *e2;
4080
4081 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4082 if (strcmp (e1->match, e2->match) == 0)
4083 einfo ("%X%P: duplicate expression `%s' in version information\n",
4084 e1->match);
c5a0d31a
ILT
4085 }
4086 }
4087
4088 version->deps = deps;
4089 version->name = name;
4090 ++version_index;
4091 version->vernum = version_index;
4092
4093 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4094 ;
4095 *pp = version;
4096}
4097
4098/* This is called when we see a version dependency. */
4099
4100struct bfd_elf_version_deps *
4101lang_add_vers_depend (list, name)
4102 struct bfd_elf_version_deps *list;
4103 const char *name;
4104{
4105 struct bfd_elf_version_deps *ret;
4106 struct bfd_elf_version_tree *t;
4107
4108 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4109 ret->next = list;
4110
4111 for (t = lang_elf_version_info; t != NULL; t = t->next)
4112 {
4113 if (strcmp (t->name, name) == 0)
4114 {
4115 ret->version_needed = t;
4116 return ret;
4117 }
4118 }
4119
4120 einfo ("%X%P: unable to find version dependency `%s'\n", name);
4121
4122 return ret;
4123}
This page took 0.461977 seconds and 4 git commands to generate.