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