]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldlang.c
* coffgen.c (coff_renumber_symbols): Correct handling of
[thirdparty/binutils-gdb.git] / ld / ldlang.c
CommitLineData
e20873a7 1/* Linker command language support.
307585f1 2 Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
075d7359 3
2fa0b342
DHW
4This file is part of GLD, the Gnu Linker.
5
6GLD is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GLD is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GLD; see the file COPYING. If not, write to
943fbd5b 18the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2fa0b342 19
2fa0b342 20#include "bfd.h"
075d7359 21#include "sysdep.h"
4c2123b6 22#include "libiberty.h"
c477527c 23#include "bfdlink.h"
2fa0b342
DHW
24
25#include "ld.h"
fcf276c4 26#include "ldmain.h"
7bc4a0d7 27#include "ldgram.h"
2fa0b342 28#include "ldexp.h"
fcf276c4 29#include "ldlang.h"
8c514453 30#include "ldemul.h"
2fa0b342 31#include "ldlex.h"
3f8d46e7 32#include "ldmisc.h"
c611e285 33#include "ldctor.h"
fcf276c4 34#include "ldfile.h"
b98bd3db 35
2fa0b342 36/* FORWARDS */
5af8e9d6
KR
37static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
38 size_t,
39 lang_statement_list_type*));
40
2fa0b342 41
d4c02e29 42/* LOCALS */
bfbdc80f 43static struct obstack stat_obstack;
075d7359 44
0b2f8d2e 45#define obstack_chunk_alloc xmalloc
bfbdc80f 46#define obstack_chunk_free free
075d7359 47static CONST char *startup_file;
d4c02e29
SC
48static lang_statement_list_type input_file_chain;
49static boolean placed_commons = false;
50static lang_output_section_statement_type *default_common_section;
51static boolean map_option_f;
52static bfd_vma print_dot;
53static lang_input_statement_type *first_file;
54static lang_statement_list_type lang_output_section_statement;
55static CONST char *current_target;
56static CONST char *output_target;
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;
c96386c4
ILT
1113 if (config.traditional_format)
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)
2142 i->_cooked_size = i->_raw_size;
2143 else
755f42fe 2144 {
193c5f93 2145 boolean again;
075d7359 2146
193c5f93
ILT
2147 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2148 einfo ("%P%F: can't relax section: %E\n");
2149 if (again)
2150 relax_again = true;
2151 }
2152 dot = size_input_section (prev,
2153 output_section_statement,
2154 output_section_statement->fill,
2155 dot, relax);
ae475b39 2156 }
ae475b39
SC
2157 break;
2158 case lang_input_statement_enum:
2159 break;
2160 case lang_fill_statement_enum:
2161 s->fill_statement.output_section = output_section_statement->bfd_section;
075d7359 2162
ae475b39
SC
2163 fill = s->fill_statement.fill;
2164 break;
2165 case lang_assignment_statement_enum:
2166 {
2167 bfd_vma newdot = dot;
2168
2169 exp_fold_tree (s->assignment_statement.exp,
2170 output_section_statement,
2171 lang_allocating_phase_enum,
2172 dot,
2173 &newdot);
2174
2175 if (newdot != dot && !relax)
27f7237e
ILT
2176 {
2177 /* The assignment changed dot. Insert a pad. */
2178 if (output_section_statement == abs_output_section)
2179 {
2180 /* If we don't have an output section, then just adjust
2181 the default memory address. */
2182 lang_memory_region_lookup ("*default*")->current = newdot;
2183 }
2184 else
2185 {
2186 lang_statement_union_type *new =
2187 ((lang_statement_union_type *)
2188 stat_alloc (sizeof (lang_padding_statement_type)));
2189
2190 /* Link into existing chain */
2191 new->header.next = *prev;
2192 *prev = new;
2193 new->header.type = lang_padding_statement_enum;
2194 new->padding_statement.output_section =
2195 output_section_statement->bfd_section;
2196 new->padding_statement.output_offset =
2197 dot - output_section_statement->bfd_section->vma;
2198 new->padding_statement.fill = fill;
2199 new->padding_statement.size = newdot - dot;
2200 output_section_statement->bfd_section->_raw_size +=
2201 new->padding_statement.size;
2202 }
2203
2204 dot = newdot;
2205 }
ae475b39 2206 }
27f7237e 2207 break;
9d87af56
ILT
2208
2209 case lang_padding_statement_enum:
2210 /* If we are relaxing, and this is not the first pass, some
2211 padding statements may have been inserted during previous
2212 passes. We may have to move the padding statement to a new
2213 location if dot has a different value at this point in this
2214 pass than it did at this point in the previous pass. */
2215 s->padding_statement.output_offset =
2216 dot - output_section_statement->bfd_section->vma;
2217 dot += s->padding_statement.size;
708f7fbc
ILT
2218 output_section_statement->bfd_section->_raw_size +=
2219 s->padding_statement.size;
9d87af56
ILT
2220 break;
2221
309c8153
ILT
2222 case lang_group_statement_enum:
2223 dot = lang_size_sections (s->group_statement.children.head,
2224 output_section_statement,
2225 &s->group_statement.children.head,
2226 fill, dot, relax);
2227 break;
2228
ae475b39
SC
2229 default:
2230 FAIL ();
2231 break;
9d87af56 2232
ae475b39 2233 /* This can only get here when relaxing is turned on */
075d7359 2234
ae475b39
SC
2235 case lang_address_statement_enum:
2236 break;
075d7359 2237 }
ae475b39
SC
2238 prev = &s->header.next;
2239 }
c611e285
SC
2240 return dot;
2241}
9d1fe8a4 2242
309c8153 2243bfd_vma
9f629407
ILT
2244lang_do_assignments (s, output_section_statement, fill, dot)
2245 lang_statement_union_type * s;
2246 lang_output_section_statement_type * output_section_statement;
c477527c 2247 fill_type fill;
9f629407 2248 bfd_vma dot;
2fa0b342 2249{
075d7359 2250 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 2251 {
075d7359 2252 switch (s->header.type)
2fa0b342 2253 {
075d7359
SC
2254 case lang_constructors_statement_enum:
2255 dot = lang_do_assignments (constructor_list.head,
2256 output_section_statement,
2257 fill,
2258 dot);
2259 break;
2260
2261 case lang_output_section_statement_enum:
2262 {
2263 lang_output_section_statement_type *os =
269773c1 2264 &(s->output_section_statement);
2fa0b342 2265
269773c1
ILT
2266 if (os->bfd_section != NULL)
2267 {
2268 dot = os->bfd_section->vma;
2269 (void) lang_do_assignments (os->children.head, os,
2270 os->fill, dot);
2271 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2272 }
0b3499f6
ILT
2273 if (os->load_base)
2274 {
467a0380
SC
2275 /* If nothing has been placed into the output section then
2276 it won't have a bfd_section. */
2277 if (os->bfd_section)
2278 {
2279 os->bfd_section->lma
2280 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2281 }
0b3499f6 2282 }
075d7359
SC
2283 }
2284 break;
2285 case lang_wild_statement_enum:
2fa0b342 2286
075d7359
SC
2287 dot = lang_do_assignments (s->wild_statement.children.head,
2288 output_section_statement,
2289 fill, dot);
2fa0b342 2290
075d7359
SC
2291 break;
2292
2293 case lang_object_symbols_statement_enum:
2294 case lang_output_statement_enum:
2295 case lang_target_statement_enum:
1418c83b 2296#if 0
075d7359 2297 case lang_common_statement_enum:
1418c83b 2298#endif
2fa0b342 2299 break;
075d7359
SC
2300 case lang_data_statement_enum:
2301 {
2302 etree_value_type value;
2303
2304 value = exp_fold_tree (s->data_statement.exp,
2305 abs_output_section,
2306 lang_final_phase_enum, dot, &dot);
2307 s->data_statement.value = value.value;
2308 if (value.valid == false)
ddddcdf0 2309 einfo ("%F%P: invalid data statement\n");
075d7359
SC
2310 }
2311 switch (s->data_statement.type)
2312 {
c477527c
ILT
2313 case QUAD:
2314 dot += QUAD_SIZE;
2315 break;
075d7359
SC
2316 case LONG:
2317 dot += LONG_SIZE;
2318 break;
2319 case SHORT:
2320 dot += SHORT_SIZE;
2321 break;
2322 case BYTE:
2323 dot += BYTE_SIZE;
2324 break;
2325 }
2fa0b342 2326 break;
4fdbafb2
ILT
2327
2328 case lang_reloc_statement_enum:
2329 {
2330 etree_value_type value;
2331
2332 value = exp_fold_tree (s->reloc_statement.addend_exp,
2333 abs_output_section,
2334 lang_final_phase_enum, dot, &dot);
2335 s->reloc_statement.addend_value = value.value;
2336 if (value.valid == false)
2337 einfo ("%F%P: invalid reloc statement\n");
2338 }
2339 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2340 break;
2341
075d7359
SC
2342 case lang_input_section_enum:
2343 {
2344 asection *in = s->input_section.section;
2345
f078dc7c
ILT
2346 if (in->_cooked_size != 0)
2347 dot += in->_cooked_size;
2348 else
2349 dot += in->_raw_size;
075d7359 2350 }
2fa0b342 2351 break;
2fa0b342 2352
075d7359
SC
2353 case lang_input_statement_enum:
2354 break;
2355 case lang_fill_statement_enum:
2356 fill = s->fill_statement.fill;
2357 break;
2358 case lang_assignment_statement_enum:
2359 {
2360 exp_fold_tree (s->assignment_statement.exp,
2361 output_section_statement,
2362 lang_final_phase_enum,
2363 dot,
2364 &dot);
2365 }
2366
2367 break;
2368 case lang_padding_statement_enum:
2369 dot += s->padding_statement.size;
2370 break;
309c8153
ILT
2371
2372 case lang_group_statement_enum:
2373 dot = lang_do_assignments (s->group_statement.children.head,
2374 output_section_statement,
2375 fill, dot);
2376
2377 break;
2378
075d7359
SC
2379 default:
2380 FAIL ();
2381 break;
2382 case lang_address_statement_enum:
2383 break;
2384 }
2fa0b342
DHW
2385
2386 }
2387 return dot;
2388}
2389
1d169acc
ILT
2390/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2391 operator .startof. (section_name), it produces an undefined symbol
2392 .startof.section_name. Similarly, when it sees
2393 .sizeof. (section_name), it produces an undefined symbol
2394 .sizeof.section_name. For all the output sections, we look for
2395 such symbols, and set them to the correct value. */
2396
2397static void
2398lang_set_startof ()
2399{
2400 asection *s;
2401
307585f1
ILT
2402 if (link_info.relocateable)
2403 return;
2404
1d169acc
ILT
2405 for (s = output_bfd->sections; s != NULL; s = s->next)
2406 {
2407 const char *secname;
2408 char *buf;
2409 struct bfd_link_hash_entry *h;
2410
2411 secname = bfd_get_section_name (output_bfd, s);
2412 buf = xmalloc (10 + strlen (secname));
2413
2414 sprintf (buf, ".startof.%s", secname);
2415 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2416 if (h != NULL && h->type == bfd_link_hash_undefined)
2417 {
2418 h->type = bfd_link_hash_defined;
2419 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2420 h->u.def.section = bfd_abs_section_ptr;
2421 }
2422
2423 sprintf (buf, ".sizeof.%s", secname);
2424 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2425 if (h != NULL && h->type == bfd_link_hash_undefined)
2426 {
2427 h->type = bfd_link_hash_defined;
2428 if (s->_cooked_size != 0)
2429 h->u.def.value = s->_cooked_size;
2430 else
2431 h->u.def.value = s->_raw_size;
2432 h->u.def.section = bfd_abs_section_ptr;
2433 }
2434
2435 free (buf);
2436 }
2437}
2438
2fa0b342 2439static void
8ddef552 2440lang_finish ()
2fa0b342 2441{
c477527c 2442 struct bfd_link_hash_entry *h;
cc38364d
KR
2443 boolean warn;
2444
2445 if (link_info.relocateable || link_info.shared)
2446 warn = false;
2447 else
2448 warn = true;
075d7359 2449
c477527c
ILT
2450 if (entry_symbol == (char *) NULL)
2451 {
2452 /* No entry has been specified. Look for start, but don't warn
2453 if we don't find it. */
2454 entry_symbol = "start";
2455 warn = false;
2456 }
2fa0b342 2457
c477527c
ILT
2458 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2459 if (h != (struct bfd_link_hash_entry *) NULL
8ed88239 2460 && (h->type == bfd_link_hash_defined
061e65f8
ILT
2461 || h->type == bfd_link_hash_defweak)
2462 && h->u.def.section->output_section != NULL)
075d7359 2463 {
c477527c 2464 bfd_vma val;
075d7359 2465
c477527c
ILT
2466 val = (h->u.def.value
2467 + bfd_get_section_vma (output_bfd,
2468 h->u.def.section->output_section)
2469 + h->u.def.section->output_offset);
2470 if (! bfd_set_start_address (output_bfd, val))
2471 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
075d7359 2472 }
c477527c 2473 else
22a78f0d 2474 {
c477527c
ILT
2475 asection *ts;
2476
2477 /* Can't find the entry symbol. Use the first address in the
2478 text section. */
2479 ts = bfd_get_section_by_name (output_bfd, ".text");
2480 if (ts != (asection *) NULL)
2481 {
2482 if (warn)
2483 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2484 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2485 if (! bfd_set_start_address (output_bfd,
2486 bfd_get_section_vma (output_bfd, ts)))
2487 einfo ("%P%F: can't set start address\n");
2488 }
2489 else
2490 {
2491 if (warn)
2492 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2493 entry_symbol);
2494 }
22a78f0d 2495 }
2fa0b342
DHW
2496}
2497
0b3499f6 2498/* Check that the architecture of all the input files is compatible
f400bbbb
ILT
2499 with the output file. Also call the backend to let it do any
2500 other checking that is needed. */
0b3499f6 2501
2fa0b342 2502static void
8ddef552 2503lang_check ()
2fa0b342
DHW
2504{
2505 lang_statement_union_type *file;
075d7359 2506 bfd *input_bfd;
075d7359 2507 CONST bfd_arch_info_type *compatible;
7bc4a0d7 2508
2fa0b342 2509 for (file = file_chain.head;
075d7359
SC
2510 file != (lang_statement_union_type *) NULL;
2511 file = file->input_statement.next)
2512 {
075d7359 2513 input_bfd = file->input_statement.the_bfd;
075d7359
SC
2514 compatible = bfd_arch_get_compatible (input_bfd,
2515 output_bfd);
0b3499f6
ILT
2516 if (compatible == NULL)
2517 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2518 bfd_printable_name (input_bfd), input_bfd,
2519 bfd_printable_name (output_bfd));
f400bbbb
ILT
2520
2521 else
2522 bfd_merge_private_bfd_data (input_bfd, output_bfd);
075d7359
SC
2523 }
2524}
2fa0b342 2525
c477527c
ILT
2526/* Look through all the global common symbols and attach them to the
2527 correct section. The -sort-common command line switch may be used
2528 to roughly sort the entries by size. */
2fa0b342
DHW
2529
2530static void
8ddef552 2531lang_common ()
2fa0b342 2532{
c477527c
ILT
2533 if (link_info.relocateable
2534 && ! command_line.force_common_definition)
2535 return;
075d7359 2536
c477527c
ILT
2537 if (! config.sort_common)
2538 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2539 else
075d7359 2540 {
fa9dea80 2541 int power;
1418c83b 2542
fa9dea80 2543 for (power = 4; power >= 0; power--)
c477527c
ILT
2544 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2545 (PTR) &power);
2546 }
2547}
29f33467 2548
c477527c 2549/* Place one common symbol in the correct section. */
075d7359 2550
c477527c
ILT
2551static boolean
2552lang_one_common (h, info)
2553 struct bfd_link_hash_entry *h;
2554 PTR info;
2555{
2556 unsigned int power_of_two;
2557 bfd_vma size;
c477527c 2558 asection *section;
075d7359 2559
c477527c
ILT
2560 if (h->type != bfd_link_hash_common)
2561 return true;
075d7359 2562
c477527c 2563 size = h->u.c.size;
f400bbbb 2564 power_of_two = h->u.c.p->alignment_power;
36c6e8c3
ILT
2565
2566 if (config.sort_common
f054ce20 2567 && power_of_two < (unsigned int) *(int *) info)
c477527c 2568 return true;
075d7359 2569
f400bbbb 2570 section = h->u.c.p->section;
075d7359 2571
c477527c 2572 /* Increase the size of the section. */
36c6e8c3
ILT
2573 section->_raw_size = ALIGN_N (section->_raw_size,
2574 (bfd_size_type) (1 << power_of_two));
075d7359 2575
c477527c
ILT
2576 /* Adjust the alignment if necessary. */
2577 if (power_of_two > section->alignment_power)
2578 section->alignment_power = power_of_two;
075d7359 2579
c477527c
ILT
2580 /* Change the symbol from common to defined. */
2581 h->type = bfd_link_hash_defined;
2582 h->u.def.section = section;
2583 h->u.def.value = section->_raw_size;
097879bc 2584
c477527c
ILT
2585 /* Increase the size of the section. */
2586 section->_raw_size += size;
1418c83b 2587
809391bd
ILT
2588 /* Make sure the section is allocated in memory. */
2589 section->flags |= SEC_ALLOC;
2590
b495c314 2591 if (config.map_file != NULL)
c477527c
ILT
2592 fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2593 h->root.string, (unsigned long) size,
2594 (unsigned long) h->u.def.value, section->owner->filename);
1418c83b 2595
c477527c 2596 return true;
2fa0b342
DHW
2597}
2598
2599/*
075d7359 2600run through the input files and ensure that every input
2fa0b342
DHW
2601section has somewhere to go. If one is found without
2602a destination then create an input request and place it
2603into the statement tree.
2604*/
2605
075d7359 2606static void
8ddef552 2607lang_place_orphans ()
2fa0b342
DHW
2608{
2609 lang_input_statement_type *file;
1418c83b 2610
075d7359
SC
2611 for (file = (lang_input_statement_type *) file_chain.head;
2612 file != (lang_input_statement_type *) NULL;
2613 file = (lang_input_statement_type *) file->next)
2614 {
2615 asection *s;
2616
2617 for (s = file->the_bfd->sections;
2618 s != (asection *) NULL;
2619 s = s->next)
2620 {
2621 if (s->output_section == (asection *) NULL)
2622 {
2623 /* This section of the file is not attatched, root
2624 around for a sensible place for it to go */
2625
e802f0be
DE
2626 if (file->just_syms_flag)
2627 {
2628 /* We are only retrieving symbol values from this
2629 file. We want the symbols to act as though the
2630 values in the file are absolute. */
2631 s->output_section = bfd_abs_section_ptr;
2632 s->output_offset = s->vma;
2633 }
2634 else if (file->common_section == s)
075d7359
SC
2635 {
2636 /* This is a lonely common section which must
2637 have come from an archive. We attatch to the
2638 section with the wildcard */
c477527c
ILT
2639 if (! link_info.relocateable
2640 && ! command_line.force_common_definition)
075d7359
SC
2641 {
2642 if (default_common_section ==
2643 (lang_output_section_statement_type *) NULL)
2644 {
a4aeaacf 2645 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
075d7359
SC
2646
2647 default_common_section =
2648 lang_output_section_statement_lookup (".bss");
2649
2650 }
2651 wild_doit (&default_common_section->children, s,
2652 default_common_section, file);
2653 }
2654 }
0b3499f6
ILT
2655 else if (ldemul_place_orphan (file, s))
2656 ;
075d7359
SC
2657 else
2658 {
2659 lang_output_section_statement_type *os =
2660 lang_output_section_statement_lookup (s->name);
2fa0b342 2661
075d7359
SC
2662 wild_doit (&os->children, s, os, file);
2663 }
2664 }
2fa0b342 2665 }
2fa0b342 2666 }
2fa0b342
DHW
2667}
2668
2669
2fa0b342 2670void
8ddef552
DM
2671lang_set_flags (ptr, flags)
2672 int *ptr;
2673 CONST char *flags;
2fa0b342 2674{
075d7359
SC
2675 boolean state = false;
2676
2677 *ptr = 0;
2fa0b342 2678 while (*flags)
075d7359
SC
2679 {
2680 if (*flags == '!')
2681 {
2682 state = false;
2683 flags++;
2684 }
2685 else
2686 state = true;
2687 switch (*flags)
2688 {
2689 case 'R':
2690 /* ptr->flag_read = state; */
2691 break;
2692 case 'W':
2693 /* ptr->flag_write = state; */
2694 break;
2695 case 'X':
2696 /* ptr->flag_executable= state;*/
2697 break;
2698 case 'L':
2699 case 'I':
2700 /* ptr->flag_loadable= state;*/
2701 break;
2702 default:
ddddcdf0 2703 einfo ("%P%F: invalid syntax in flags\n");
075d7359
SC
2704 break;
2705 }
dc4726c2
SC
2706 flags++;
2707 }
2fa0b342
DHW
2708}
2709
a4d2a48e
ILT
2710/* Call a function on each input file. This function will be called
2711 on an archive, but not on the elements. */
2712
2713void
2714lang_for_each_input_file (func)
2715 void (*func) PARAMS ((lang_input_statement_type *));
2716{
2717 lang_input_statement_type *f;
2718
2719 for (f = (lang_input_statement_type *) input_file_chain.head;
2720 f != NULL;
2721 f = (lang_input_statement_type *) f->next_real_file)
2722 func (f);
2723}
2fa0b342 2724
a4d2a48e
ILT
2725/* Call a function on each file. The function will be called on all
2726 the elements of an archive which are included in the link, but will
2727 not be called on the archive file itself. */
2fa0b342
DHW
2728
2729void
8ddef552
DM
2730lang_for_each_file (func)
2731 void (*func) PARAMS ((lang_input_statement_type *));
2fa0b342
DHW
2732{
2733 lang_input_statement_type *f;
075d7359
SC
2734
2735 for (f = (lang_input_statement_type *) file_chain.head;
2736 f != (lang_input_statement_type *) NULL;
2737 f = (lang_input_statement_type *) f->next)
2738 {
2739 func (f);
2740 }
2fa0b342
DHW
2741}
2742
9f629407
ILT
2743#if 0
2744
2745/* Not used. */
2fa0b342
DHW
2746
2747void
8ddef552
DM
2748lang_for_each_input_section (func)
2749 void (*func) PARAMS ((bfd * ab, asection * as));
2fa0b342
DHW
2750{
2751 lang_input_statement_type *f;
075d7359
SC
2752
2753 for (f = (lang_input_statement_type *) file_chain.head;
2754 f != (lang_input_statement_type *) NULL;
2755 f = (lang_input_statement_type *) f->next)
2fa0b342
DHW
2756 {
2757 asection *s;
075d7359 2758
2fa0b342 2759 for (s = f->the_bfd->sections;
075d7359
SC
2760 s != (asection *) NULL;
2761 s = s->next)
2762 {
2763 func (f->the_bfd, s);
2764 }
2fa0b342
DHW
2765 }
2766}
2767
9f629407 2768#endif
2fa0b342 2769
075d7359 2770void
8ddef552
DM
2771ldlang_add_file (entry)
2772 lang_input_statement_type * entry;
2fa0b342 2773{
5e6cd559
ILT
2774 bfd **pp;
2775
075d7359
SC
2776 lang_statement_append (&file_chain,
2777 (lang_statement_union_type *) entry,
2778 &entry->next);
c477527c
ILT
2779
2780 /* The BFD linker needs to have a list of all input BFDs involved in
2781 a link. */
2782 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2783 ASSERT (entry->the_bfd != output_bfd);
5e6cd559
ILT
2784 for (pp = &link_info.input_bfds;
2785 *pp != (bfd *) NULL;
2786 pp = &(*pp)->link_next)
2787 ;
2788 *pp = entry->the_bfd;
c477527c 2789 entry->the_bfd->usrdata = (PTR) entry;
fc1dfb71 2790 bfd_set_gp_size (entry->the_bfd, g_switch_value);
23595ba5
ILT
2791
2792 /* Look through the sections and check for any which should not be
2793 included in the link. We need to do this now, so that we can
2794 notice when the backend linker tries to report multiple
2795 definition errors for symbols which are in sections we aren't
2796 going to link. FIXME: It might be better to entirely ignore
2797 symbols which are defined in sections which are going to be
2798 discarded. This would require modifying the backend linker for
2799 each backend which might set the SEC_LINK_ONCE flag. If we do
2800 this, we should probably handle SEC_EXCLUDE in the same way. */
2801
2802 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) NULL);
2fa0b342
DHW
2803}
2804
2fa0b342 2805void
2c6635a4 2806lang_add_output (name, from_script)
8ddef552 2807 CONST char *name;
2c6635a4 2808 int from_script;
2fa0b342 2809{
2c6635a4
ILT
2810 /* Make -o on command line override OUTPUT in script. */
2811 if (had_output_filename == false || !from_script)
2812 {
2813 output_filename = name;
2814 had_output_filename = true;
2815 }
2fa0b342
DHW
2816}
2817
2818
2819static lang_output_section_statement_type *current_section;
2820
f054ce20
ILT
2821static int
2822topower (x)
9f629407 2823 int x;
e20873a7 2824{
f054ce20 2825 unsigned int i = 1;
e20873a7 2826 int l;
f054ce20
ILT
2827
2828 if (x < 0)
2829 return -1;
2830
e20873a7 2831 for (l = 0; l < 32; l++)
f054ce20
ILT
2832 {
2833 if (i >= (unsigned int) x)
2834 return l;
2835 i <<= 1;
2836 }
2837
29f33467 2838 return 0;
e20873a7 2839}
2fa0b342 2840void
8ddef552
DM
2841lang_enter_output_section_statement (output_section_statement_name,
2842 address_exp, flags, block_value,
9f629407 2843 align, subalign, ebase)
fcf276c4 2844 const char *output_section_statement_name;
8ddef552
DM
2845 etree_type * address_exp;
2846 int flags;
2847 bfd_vma block_value;
2848 etree_type *align;
2849 etree_type *subalign;
9f629407 2850 etree_type *ebase;
2fa0b342
DHW
2851{
2852 lang_output_section_statement_type *os;
075d7359
SC
2853
2854 current_section =
e20873a7 2855 os =
075d7359
SC
2856 lang_output_section_statement_lookup (output_section_statement_name);
2857
2858
2fa0b342 2859
2fa0b342
DHW
2860 /* Add this statement to tree */
2861 /* add_statement(lang_output_section_statement_enum,
2862 output_section_statement);*/
2863 /* Make next things chain into subchain of this */
2864
2865 if (os->addr_tree ==
075d7359 2866 (etree_type *) NULL)
e20873a7
JG
2867 {
2868 os->addr_tree =
2869 address_exp;
2870 }
7bc4a0d7 2871 os->flags = flags;
ae475b39
SC
2872 if (flags & SEC_NEVER_LOAD)
2873 os->loadable = 0;
2874 else
2875 os->loadable = 1;
29f33467 2876 os->block_value = block_value ? block_value : 1;
075d7359 2877 stat_ptr = &os->children;
2fa0b342 2878
e20873a7
JG
2879 os->subsection_alignment = topower(
2880 exp_get_value_int(subalign, -1,
2881 "subsection alignment",
2882 0));
2883 os->section_alignment = topower(
2884 exp_get_value_int(align, -1,
2885 "section alignment", 0));
9fce28ed 2886
9f629407 2887 os->load_base = ebase;
2fa0b342
DHW
2888}
2889
9fce28ed 2890
075d7359 2891void
8ddef552 2892lang_final ()
2fa0b342 2893{
2c6635a4
ILT
2894 lang_output_statement_type *new =
2895 new_stat (lang_output_statement, stat_ptr);
2fa0b342 2896
2c6635a4 2897 new->name = output_filename;
075d7359 2898}
2fa0b342 2899
c611e285
SC
2900/* Reset the current counters in the regions */
2901static void
8ddef552 2902reset_memory_regions ()
c611e285 2903{
075d7359
SC
2904 lang_memory_region_type *p = lang_memory_region_list;
2905
c611e285 2906 for (p = lang_memory_region_list;
075d7359
SC
2907 p != (lang_memory_region_type *) NULL;
2908 p = p->next)
2909 {
8ddef552 2910 p->old_length = (bfd_size_type) (p->current - p->origin);
075d7359
SC
2911 p->current = p->origin;
2912 }
c611e285 2913}
2fa0b342 2914
2fa0b342 2915void
8ddef552 2916lang_process ()
075d7359 2917{
075d7359 2918 lang_reasonable_defaults ();
1418c83b
SC
2919 current_target = default_target;
2920
075d7359 2921 lang_for_each_statement (ldlang_open_output); /* Open the output file */
1418c83b 2922
97fbbaca
JL
2923 ldemul_create_output_section_statements ();
2924
1418c83b 2925 /* Add to the hash table all undefineds on the command line */
075d7359 2926 lang_place_undefineds ();
1418c83b
SC
2927
2928 /* Create a bfd for each input file */
2929 current_target = default_target;
309c8153 2930 open_input_bfds (statement_list.head, false);
1418c83b 2931
cc38364d
KR
2932 ldemul_after_open ();
2933
c477527c
ILT
2934 /* Build all sets based on the information gathered from the input
2935 files. */
2936 ldctor_build_sets ();
2937
4fdbafb2
ILT
2938 /* Size up the common data */
2939 lang_common ();
2940
1418c83b 2941 /* Run through the contours of the script and attatch input sections
075d7359 2942 to the correct output sections
1418c83b 2943 */
075d7359
SC
2944 map_input_to_output_sections (statement_list.head, (char *) NULL,
2945 (lang_output_section_statement_type *) NULL);
1418c83b 2946
81016051 2947
1418c83b 2948 /* Find any sections not attatched explicitly and handle them */
075d7359 2949 lang_place_orphans ();
1418c83b 2950
075d7359 2951 ldemul_before_allocation ();
1418c83b 2952
061e65f8
ILT
2953 /* We must record the program headers before we try to fix the
2954 section positions, since they will affect SIZEOF_HEADERS. */
2955 lang_record_phdrs ();
2956
c611e285 2957 /* Now run around and relax if we can */
075d7359 2958 if (command_line.relax)
c611e285 2959 {
97fbbaca
JL
2960 /* First time round is a trial run to get the 'worst case'
2961 addresses of the objects if there was no relaxing. */
ae475b39 2962 lang_size_sections (statement_list.head,
193c5f93 2963 abs_output_section,
ae475b39 2964 &(statement_list.head), 0, (bfd_vma) 0, false);
c611e285 2965
193c5f93
ILT
2966 /* Keep relaxing until bfd_relax_section gives up. */
2967 do
2968 {
1d169acc
ILT
2969 reset_memory_regions ();
2970
193c5f93 2971 relax_again = false;
ae475b39 2972
193c5f93
ILT
2973 /* Do all the assignments with our current guesses as to
2974 section sizes. */
2975 lang_do_assignments (statement_list.head,
2976 abs_output_section,
2977 (fill_type) 0, (bfd_vma) 0);
ae475b39 2978
193c5f93
ILT
2979 /* Perform another relax pass - this time we know where the
2980 globals are, so can make better guess. */
2981 lang_size_sections (statement_list.head,
2982 abs_output_section,
2983 &(statement_list.head), 0, (bfd_vma) 0, true);
2984 }
2985 while (relax_again);
ae475b39 2986 }
ae475b39
SC
2987 else
2988 {
97fbbaca 2989 /* Size up the sections. */
ae475b39
SC
2990 lang_size_sections (statement_list.head,
2991 abs_output_section,
2992 &(statement_list.head), 0, (bfd_vma) 0, false);
075d7359 2993 }
c611e285 2994
1418c83b 2995 /* See if anything special should be done now we know how big
97fbbaca 2996 everything is. */
075d7359 2997 ldemul_after_allocation ();
1418c83b 2998
1d169acc
ILT
2999 /* Fix any .startof. or .sizeof. symbols. */
3000 lang_set_startof ();
3001
1418c83b
SC
3002 /* Do all the assignments, now that we know the final restingplaces
3003 of all the symbols */
3004
075d7359
SC
3005 lang_do_assignments (statement_list.head,
3006 abs_output_section,
c477527c 3007 (fill_type) 0, (bfd_vma) 0);
ffc50032 3008
1418c83b
SC
3009 /* Make sure that we're not mixing architectures */
3010
075d7359 3011 lang_check ();
1418c83b 3012
1418c83b 3013 /* Final stuffs */
97fbbaca
JL
3014
3015 ldemul_finish ();
075d7359 3016 lang_finish ();
2fa0b342
DHW
3017}
3018
2fa0b342 3019/* EXPORTED TO YACC */
1418c83b 3020
2fa0b342 3021void
8ddef552
DM
3022lang_add_wild (section_name, filename)
3023 CONST char *CONST section_name;
3024 CONST char *CONST filename;
1418c83b 3025{
075d7359
SC
3026 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3027 stat_ptr);
1418c83b 3028
075d7359
SC
3029 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3030 {
3031 placed_commons = true;
3032 }
3033 if (filename != (char *) NULL)
3034 {
3035 lang_has_input_file = true;
3036 }
1418c83b
SC
3037 new->section_name = section_name;
3038 new->filename = filename;
075d7359 3039 lang_list_init (&new->children);
1418c83b 3040}
075d7359 3041
1418c83b 3042void
8ddef552
DM
3043lang_section_start (name, address)
3044 CONST char *name;
3045 etree_type * address;
2fa0b342 3046{
075d7359
SC
3047 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3048
2fa0b342
DHW
3049 ad->section_name = name;
3050 ad->address = address;
3051}
1418c83b 3052
60e8a534
ILT
3053/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3054 because of a -e argument on the command line, or zero if this is
3055 called by ENTRY in a linker script. Command line arguments take
3056 precedence. */
3057
3f38a017
SC
3058/* WINDOWS_NT. When an entry point has been specified, we will also force
3059 this symbol to be defined by calling ldlang_add_undef (equivalent to
3060 having switch -u entry_name on the command line). The reason we do
3061 this is so that the user doesn't have to because they would have to use
3062 the -u switch if they were specifying an entry point other than
3063 _mainCRTStartup. Specifically, if creating a windows application, entry
3064 point _WinMainCRTStartup must be specified.
3065 What I have found for non console applications (entry not _mainCRTStartup)
3066 is that the .obj that contains mainCRTStartup is brought in since it is
3067 the first encountered in libc.lib and it has other symbols in it which will
3068 be pulled in by the link process. To avoid this, adding -u with the entry
3069 point name specified forces the correct .obj to be used. We can avoid
3070 making the user do this by always adding the entry point name as an
3071 undefined symbol. */
3072
075d7359 3073void
60e8a534 3074lang_add_entry (name, cmdline)
8ddef552 3075 CONST char *name;
f054ce20 3076 boolean cmdline;
2fa0b342 3077{
60e8a534
ILT
3078 if (entry_symbol == NULL
3079 || cmdline
f054ce20 3080 || ! entry_from_cmdline)
60e8a534
ILT
3081 {
3082 entry_symbol = name;
f054ce20 3083 entry_from_cmdline = cmdline;
60e8a534 3084 }
943fbd5b 3085#if 0
3f38a017
SC
3086 /* don't do this yet. It seems to work (the executables run), but the
3087 image created is very different from what I was getting before indicating
3088 that something else is being pulled in. When everything else is working,
3089 then try to put this back in to see if it will do the right thing for
3090 other more complicated applications */
3091 ldlang_add_undef (name);
3092#endif
2fa0b342
DHW
3093}
3094
3095void
8ddef552
DM
3096lang_add_target (name)
3097 CONST char *name;
2fa0b342 3098{
075d7359
SC
3099 lang_target_statement_type *new = new_stat (lang_target_statement,
3100 stat_ptr);
3101
2fa0b342
DHW
3102 new->target = name;
3103
3104}
2fa0b342 3105
2fa0b342 3106void
8ddef552
DM
3107lang_add_map (name)
3108 CONST char *name;
2fa0b342 3109{
075d7359
SC
3110 while (*name)
3111 {
3112 switch (*name)
3113 {
3114 case 'F':
3115 map_option_f = true;
3116 break;
3117 }
3118 name++;
2fa0b342 3119 }
2fa0b342
DHW
3120}
3121
075d7359 3122void
8ddef552
DM
3123lang_add_fill (exp)
3124 int exp;
2fa0b342 3125{
075d7359
SC
3126 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3127 stat_ptr);
3128
2fa0b342
DHW
3129 new->fill = exp;
3130}
3131
075d7359 3132void
8ddef552
DM
3133lang_add_data (type, exp)
3134 int type;
3135 union etree_union *exp;
2fa0b342
DHW
3136{
3137
075d7359 3138 lang_data_statement_type *new = new_stat (lang_data_statement,
2fa0b342 3139 stat_ptr);
075d7359
SC
3140
3141 new->exp = exp;
3142 new->type = type;
2fa0b342
DHW
3143
3144}
075d7359 3145
4fdbafb2
ILT
3146/* Create a new reloc statement. RELOC is the BFD relocation type to
3147 generate. HOWTO is the corresponding howto structure (we could
3148 look this up, but the caller has already done so). SECTION is the
3149 section to generate a reloc against, or NAME is the name of the
3150 symbol to generate a reloc against. Exactly one of SECTION and
3151 NAME must be NULL. ADDEND is an expression for the addend. */
3152
3153void
3154lang_add_reloc (reloc, howto, section, name, addend)
3155 bfd_reloc_code_real_type reloc;
cc38364d 3156 reloc_howto_type *howto;
4fdbafb2
ILT
3157 asection *section;
3158 const char *name;
3159 union etree_union *addend;
3160{
3161 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3162
3163 p->reloc = reloc;
3164 p->howto = howto;
3165 p->section = section;
3166 p->name = name;
3167 p->addend_exp = addend;
3168
3169 p->addend_value = 0;
3170 p->output_section = NULL;
3171 p->output_vma = 0;
3172}
3173
2fa0b342 3174void
8ddef552
DM
3175lang_add_assignment (exp)
3176 etree_type * exp;
2fa0b342 3177{
075d7359
SC
3178 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3179 stat_ptr);
3180
2fa0b342
DHW
3181 new->exp = exp;
3182}
3183
3184void
8ddef552
DM
3185lang_add_attribute (attribute)
3186 enum statement_enum attribute;
2fa0b342 3187{
075d7359 3188 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2fa0b342
DHW
3189}
3190
075d7359 3191void
8ddef552
DM
3192lang_startup (name)
3193 CONST char *name;
2fa0b342 3194{
075d7359
SC
3195 if (startup_file != (char *) NULL)
3196 {
ddddcdf0 3197 einfo ("%P%Fmultiple STARTUP files\n");
075d7359 3198 }
2fa0b342
DHW
3199 first_file->filename = name;
3200 first_file->local_sym_name = name;
193c5f93 3201 first_file->real = true;
2fa0b342 3202
075d7359 3203 startup_file = name;
2fa0b342 3204}
075d7359
SC
3205
3206void
8ddef552
DM
3207lang_float (maybe)
3208 boolean maybe;
2fa0b342
DHW
3209{
3210 lang_float_flag = maybe;
3211}
3212
075d7359 3213void
8ddef552
DM
3214lang_leave_output_section_statement (fill, memspec)
3215 bfd_vma fill;
3216 CONST char *memspec;
2fa0b342
DHW
3217{
3218 current_section->fill = fill;
075d7359 3219 current_section->region = lang_memory_region_lookup (memspec);
2fa0b342
DHW
3220 stat_ptr = &statement_list;
3221}
075d7359 3222
9f32f7c2
SC
3223/*
3224 Create an absolute symbol with the given name with the value of the
3225 address of first byte of the section named.
2fa0b342 3226
9f32f7c2
SC
3227 If the symbol already exists, then do nothing.
3228*/
8cb5eb31 3229void
c477527c
ILT
3230lang_abs_symbol_at_beginning_of (secname, name)
3231 const char *secname;
3232 const char *name;
075d7359 3233{
c477527c
ILT
3234 struct bfd_link_hash_entry *h;
3235
3236 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3237 if (h == (struct bfd_link_hash_entry *) NULL)
3238 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3239
3240 if (h->type == bfd_link_hash_new
3241 || h->type == bfd_link_hash_undefined)
075d7359 3242 {
c477527c 3243 asection *sec;
075d7359 3244
c477527c
ILT
3245 h->type = bfd_link_hash_defined;
3246
3247 sec = bfd_get_section_by_name (output_bfd, secname);
3248 if (sec == (asection *) NULL)
3249 h->u.def.value = 0;
075d7359 3250 else
c477527c
ILT
3251 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3252
686739e2 3253 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3254 }
8cb5eb31
SC
3255}
3256
9f32f7c2
SC
3257/*
3258 Create an absolute symbol with the given name with the value of the
3259 address of the first byte after the end of the section named.
3260
3261 If the symbol already exists, then do nothing.
3262*/
2fa0b342 3263void
c477527c
ILT
3264lang_abs_symbol_at_end_of (secname, name)
3265 const char *secname;
3266 const char *name;
075d7359 3267{
c477527c
ILT
3268 struct bfd_link_hash_entry *h;
3269
3270 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3271 if (h == (struct bfd_link_hash_entry *) NULL)
3272 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3273
3274 if (h->type == bfd_link_hash_new
3275 || h->type == bfd_link_hash_undefined)
075d7359 3276 {
c477527c 3277 asection *sec;
075d7359 3278
c477527c 3279 h->type = bfd_link_hash_defined;
075d7359 3280
c477527c
ILT
3281 sec = bfd_get_section_by_name (output_bfd, secname);
3282 if (sec == (asection *) NULL)
3283 h->u.def.value = 0;
075d7359 3284 else
c477527c
ILT
3285 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3286 + bfd_section_size (output_bfd, sec));
3287
686739e2 3288 h->u.def.section = bfd_abs_section_ptr;
9f32f7c2 3289 }
2fa0b342
DHW
3290}
3291
075d7359 3292void
8ddef552
DM
3293lang_statement_append (list, element, field)
3294 lang_statement_list_type * list;
3295 lang_statement_union_type * element;
3296 lang_statement_union_type ** field;
2fa0b342
DHW
3297{
3298 *(list->tail) = element;
3299 list->tail = field;
3300}
3301
173a0c3d 3302/* Set the output format type. -oformat overrides scripts. */
6a02a973 3303
097879bc 3304void
6a02a973
ILT
3305lang_add_output_format (format, big, little, from_script)
3306 const char *format;
3307 const char *big;
3308 const char *little;
173a0c3d 3309 int from_script;
097879bc 3310{
2c6635a4 3311 if (output_target == NULL || !from_script)
6a02a973
ILT
3312 {
3313 if (command_line.endian == ENDIAN_BIG
3314 && big != NULL)
3315 format = big;
3316 else if (command_line.endian == ENDIAN_LITTLE
3317 && little != NULL)
3318 format = little;
3319
3320 output_target = format;
3321 }
097879bc 3322}
309c8153
ILT
3323
3324/* Enter a group. This creates a new lang_group_statement, and sets
3325 stat_ptr to build new statements within the group. */
3326
3327void
3328lang_enter_group ()
3329{
3330 lang_group_statement_type *g;
3331
3332 g = new_stat (lang_group_statement, stat_ptr);
3333 lang_list_init (&g->children);
3334 stat_ptr = &g->children;
3335}
3336
3337/* Leave a group. This just resets stat_ptr to start writing to the
3338 regular list of statements again. Note that this will not work if
3339 groups can occur inside anything else which can adjust stat_ptr,
3340 but currently they can't. */
3341
3342void
3343lang_leave_group ()
3344{
3345 stat_ptr = &statement_list;
3346}
061e65f8
ILT
3347
3348/* Add a new program header. This is called for each entry in a PHDRS
3349 command in a linker script. */
3350
3351void
708f7fbc 3352lang_new_phdr (name, type, filehdr, phdrs, at, flags)
061e65f8
ILT
3353 const char *name;
3354 etree_type *type;
708f7fbc
ILT
3355 boolean filehdr;
3356 boolean phdrs;
061e65f8 3357 etree_type *at;
708f7fbc 3358 etree_type *flags;
061e65f8
ILT
3359{
3360 struct lang_phdr *n, **pp;
3361
3362 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3363 n->next = NULL;
3364 n->name = name;
3365 n->type = exp_get_value_int (type, 0, "program header type",
3366 lang_final_phase_enum);
708f7fbc
ILT
3367 n->filehdr = filehdr;
3368 n->phdrs = phdrs;
061e65f8 3369 n->at = at;
708f7fbc 3370 n->flags = flags;
061e65f8
ILT
3371
3372 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3373 ;
3374 *pp = n;
3375}
3376
3377/* Record that a section should be placed in a phdr. */
3378
3379void
3380lang_section_in_phdr (name)
3381 const char *name;
3382{
3383 struct lang_output_section_phdr_list *n;
3384
3385 n = ((struct lang_output_section_phdr_list *)
3386 stat_alloc (sizeof (struct lang_output_section_phdr_list)));
3387 n->name = name;
3388 n->used = false;
3389 n->next = current_section->phdrs;
3390 current_section->phdrs = n;
3391}
3392
3393/* Record the program header information in the output BFD. FIXME: We
3394 should not be calling an ELF specific function here. */
3395
3396static void
3397lang_record_phdrs ()
3398{
3399 unsigned int alc;
3400 asection **secs;
3401 struct lang_output_section_phdr_list *last;
3402 struct lang_phdr *l;
3403 lang_statement_union_type *u;
3404
3405 alc = 10;
3406 secs = xmalloc (alc * sizeof (asection *));
3407 last = NULL;
3408 for (l = lang_phdr_list; l != NULL; l = l->next)
3409 {
3410 unsigned int c;
708f7fbc 3411 flagword flags;
061e65f8
ILT
3412 bfd_vma at;
3413
3414 c = 0;
3415 for (u = lang_output_section_statement.head;
3416 u != NULL;
3417 u = u->output_section_statement.next)
3418 {
3419 lang_output_section_statement_type *os;
3420 struct lang_output_section_phdr_list *pl;
3421
3422 os = &u->output_section_statement;
3423
708f7fbc 3424 pl = os->phdrs;
061e65f8
ILT
3425 if (pl != NULL)
3426 last = pl;
3427 else
3428 {
3429 if (! os->loadable
3430 || os->bfd_section == NULL
3431 || (os->bfd_section->flags & SEC_ALLOC) == 0)
3432 continue;
3433 pl = last;
3434 }
3435
3436 if (os->bfd_section == NULL)
3437 continue;
3438
3439 for (; pl != NULL; pl = pl->next)
3440 {
3441 if (strcmp (pl->name, l->name) == 0)
3442 {
3443 if (c >= alc)
3444 {
3445 alc *= 2;
3446 secs = xrealloc (secs, alc * sizeof (asection *));
3447 }
3448 secs[c] = os->bfd_section;
3449 ++c;
3450 pl->used = true;
3451 }
3452 }
3453 }
3454
708f7fbc
ILT
3455 if (l->flags == NULL)
3456 flags = 0;
3457 else
3458 flags = exp_get_vma (l->flags, 0, "phdr flags",
3459 lang_final_phase_enum);
3460
061e65f8
ILT
3461 if (l->at == NULL)
3462 at = 0;
3463 else
3464 at = exp_get_vma (l->at, 0, "phdr load address",
3465 lang_final_phase_enum);
708f7fbc
ILT
3466
3467 if (! bfd_record_phdr (output_bfd, l->type,
3468 l->flags == NULL ? false : true,
3469 flags,
061e65f8 3470 l->at == NULL ? false : true,
708f7fbc 3471 at, l->filehdr, l->phdrs, c, secs))
061e65f8
ILT
3472 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3473 }
3474
3475 free (secs);
3476
3477 /* Make sure all the phdr assignments succeeded. */
3478 for (u = lang_output_section_statement.head;
3479 u != NULL;
3480 u = u->output_section_statement.next)
3481 {
3482 struct lang_output_section_phdr_list *pl;
3483
3484 if (u->output_section_statement.bfd_section == NULL)
3485 continue;
3486
3487 for (pl = u->output_section_statement.phdrs;
3488 pl != NULL;
3489 pl = pl->next)
3490 if (! pl->used && strcmp (pl->name, "NONE") != 0)
3491 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3492 u->output_section_statement.name, pl->name);
3493 }
3494}