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