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