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