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