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