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