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