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