Formatting adjustments, for better smallbook formatting
[deliverable/binutils-gdb.git] / ld / ldlang.c
CommitLineData
e20873a7 1/* Linker command language support.
a4aeaacf 2 Copyright 1991, 1992, 1993 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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
2fa0b342 20#include "bfd.h"
075d7359 21#include "sysdep.h"
c477527c 22#include "bfdlink.h"
2fa0b342
DHW
23
24#include "ld.h"
fcf276c4 25#include "ldmain.h"
7bc4a0d7 26#include "ldgram.h"
2fa0b342 27#include "ldexp.h"
fcf276c4 28#include "ldlang.h"
8c514453 29#include "ldemul.h"
2fa0b342 30#include "ldlex.h"
3f8d46e7 31#include "ldmisc.h"
c611e285 32#include "ldctor.h"
fcf276c4 33#include "ldfile.h"
b98bd3db 34
2fa0b342 35/* FORWARDS */
e20873a7
JG
36static void print_statements PARAMS ((void));
37static void print_statement PARAMS ((lang_statement_union_type *,
075d7359 38 lang_output_section_statement_type *));
5af8e9d6
KR
39static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
40 size_t,
41 lang_statement_list_type*));
42
2fa0b342 43
d4c02e29 44/* LOCALS */
bfbdc80f 45static struct obstack stat_obstack;
075d7359 46
bfbdc80f
SC
47#define obstack_chunk_alloc ldmalloc
48#define obstack_chunk_free free
075d7359 49static CONST char *startup_file;
d4c02e29 50static lang_statement_list_type input_file_chain;
81016051
SC
51
52/* Points to the last statement in the .data section, so we can add
53 stuff to the data section without pain */
54static lang_statement_list_type end_of_data_section_statement_list;
55
d4c02e29
SC
56static boolean placed_commons = false;
57static lang_output_section_statement_type *default_common_section;
58static boolean map_option_f;
59static bfd_vma print_dot;
60static lang_input_statement_type *first_file;
61static lang_statement_list_type lang_output_section_statement;
62static CONST char *current_target;
63static CONST char *output_target;
fcf276c4 64static int longest_section_name = 8;
d4c02e29 65static lang_statement_list_type statement_list;
ffc50032 66
9f629407
ILT
67static void print_size PARAMS ((size_t value));
68static void print_alignment PARAMS ((unsigned int value));
69static void print_fill PARAMS ((fill_type value));
70static void print_section PARAMS ((const char *name));
71static void lang_for_each_statement_worker
72 PARAMS ((void (*func) (lang_statement_union_type *),
73 lang_statement_union_type *s));
74static lang_input_statement_type *new_afile
75 PARAMS ((const char *name, lang_input_file_enum_type file_type,
76 const char *target));
77static void print_flags PARAMS ((int *ignore_flags));
78static void init_os PARAMS ((lang_output_section_statement_type *s));
79static void wild_doit PARAMS ((lang_statement_list_type *ptr,
80 asection *section,
81 lang_output_section_statement_type *output,
82 lang_input_statement_type *file));
83static asection *our_bfd_get_section_by_name PARAMS ((bfd *abfd,
84 const char *section));
85static void wild_section PARAMS ((lang_wild_statement_type *ptr,
86 const char *section,
87 lang_input_statement_type *file,
88 lang_output_section_statement_type *output));
89static lang_input_statement_type *lookup_name PARAMS ((const char *name));
90static void wild PARAMS ((lang_wild_statement_type *s,
91 const char *section, const char *file,
92 const char *target,
93 lang_output_section_statement_type *output));
94static bfd *open_output PARAMS ((const char *name));
95static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
96static void open_input_bfds PARAMS ((lang_statement_union_type *statement));
97static void lang_reasonable_defaults PARAMS ((void));
98static void lang_place_undefineds PARAMS ((void));
99static void lang_create_output_section_statements PARAMS ((void));
9f629407
ILT
100static void map_input_to_output_sections
101 PARAMS ((lang_statement_union_type *s,
102 const char *target,
103 lang_output_section_statement_type *output_section_statement));
104static void print_output_section_statement
105 PARAMS ((lang_output_section_statement_type *output_section_statement));
106static void print_assignment
107 PARAMS ((lang_assignment_statement_type *assignment,
108 lang_output_section_statement_type *output_section));
109static void print_input_statement PARAMS ((lang_input_statement_type *statm));
110static void print_symbol PARAMS ((asymbol *q));
111static void print_input_section PARAMS ((lang_input_section_type *in));
112static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
113static void print_data_statement PARAMS ((lang_data_statement_type *data));
114static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
115static void print_wild_statement
116 PARAMS ((lang_wild_statement_type *w,
117 lang_output_section_statement_type *os));
118static void print_statement PARAMS ((lang_statement_union_type *s,
119 lang_output_section_statement_type *os));
120static void print_statements PARAMS ((void));
121static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
122 fill_type fill, unsigned int power,
123 asection *output_section_statement,
124 bfd_vma dot));
125static bfd_vma size_input_section
126 PARAMS ((lang_statement_union_type **this_ptr,
127 lang_output_section_statement_type *output_section_statement,
c477527c 128 fill_type fill, bfd_vma dot, boolean relax));
9f629407
ILT
129static bfd_vma lang_size_sections
130 PARAMS ((lang_statement_union_type *s,
131 lang_output_section_statement_type *output_section_statement,
c477527c 132 lang_statement_union_type **prev, fill_type fill,
9f629407
ILT
133 bfd_vma dot, boolean relax));
134static bfd_vma lang_do_assignments
135 PARAMS ((lang_statement_union_type * s,
136 lang_output_section_statement_type *output_section_statement,
c477527c 137 fill_type fill,
9f629407 138 bfd_vma dot));
9f629407
ILT
139static void lang_finish PARAMS ((void));
140static void lang_check PARAMS ((void));
141static void lang_common PARAMS ((void));
c477527c 142static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
9f629407
ILT
143static void lang_place_orphans PARAMS ((void));
144static int topower PARAMS ((int));
145static void reset_memory_regions PARAMS ((void));
146
2fa0b342 147/* EXPORTS */
ffc50032 148lang_output_section_statement_type *abs_output_section;
d4c02e29 149lang_statement_list_type *stat_ptr = &statement_list;
075d7359
SC
150lang_statement_list_type file_chain =
151{0};
d4c02e29 152CONST char *entry_symbol = 0;
3f8d46e7 153bfd_size_type largest_section = 0;
d4c02e29 154boolean lang_has_input_file = false;
d4c02e29
SC
155boolean had_output_filename = false;
156boolean lang_float_flag = false;
97fbbaca 157boolean delete_output_file_on_failure = false;
075d7359 158
9fce28ed
SC
159etree_type *base; /* Relocation base - or null */
160
161
2fa0b342
DHW
162#ifdef __STDC__
163#define cat(a,b) a##b
164#else
165#define cat(a,b) a/**/b
166#endif
167
168#define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
169
170#define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
171
172#define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
173
bfbdc80f 174PTR
8ddef552
DM
175stat_alloc (size)
176 size_t size;
bfbdc80f 177{
075d7359 178 return obstack_alloc (&stat_obstack, size);
bfbdc80f 179}
9f629407 180
075d7359 181static void
8ddef552
DM
182print_size (value)
183 size_t value;
3f8d46e7 184{
075d7359 185 fprintf (config.map_file, "%5x", (unsigned) value);
3f8d46e7 186}
9f629407 187
075d7359 188static void
8ddef552
DM
189print_alignment (value)
190 unsigned int value;
3f8d46e7 191{
075d7359 192 fprintf (config.map_file, "2**%1u", value);
3f8d46e7 193}
9f629407 194
075d7359 195static void
9f629407
ILT
196print_fill (value)
197 fill_type value;
3f8d46e7 198{
075d7359 199 fprintf (config.map_file, "%04x", (unsigned) value);
3f8d46e7
JG
200}
201
075d7359 202static void
8ddef552 203print_section (name)
9f629407 204 CONST char *name;
3f8d46e7 205{
075d7359 206 fprintf (config.map_file, "%*s", -longest_section_name, name);
3f8d46e7 207}
2fa0b342 208
1418c83b
SC
209/*----------------------------------------------------------------------
210 lang_for_each_statement walks the parse tree and calls the provided
211 function for each node
212*/
213
075d7359 214static void
8ddef552 215lang_for_each_statement_worker (func, s)
c477527c 216 void (*func) PARAMS ((lang_statement_union_type *));
8ddef552 217 lang_statement_union_type *s;
1418c83b 218{
075d7359
SC
219 for (; s != (lang_statement_union_type *) NULL; s = s->next)
220 {
221 func (s);
1418c83b 222
075d7359
SC
223 switch (s->header.type)
224 {
81016051 225 case lang_constructors_statement_enum:
075d7359 226 lang_for_each_statement_worker (func, constructor_list.head);
81016051 227 break;
1418c83b
SC
228 case lang_output_section_statement_enum:
229 lang_for_each_statement_worker
075d7359 230 (func,
1418c83b
SC
231 s->output_section_statement.children.head);
232 break;
233 case lang_wild_statement_enum:
234 lang_for_each_statement_worker
075d7359 235 (func,
1418c83b
SC
236 s->wild_statement.children.head);
237 break;
238 case lang_data_statement_enum:
239 case lang_object_symbols_statement_enum:
240 case lang_output_statement_enum:
241 case lang_target_statement_enum:
242 case lang_input_section_enum:
243 case lang_input_statement_enum:
1418c83b
SC
244 case lang_assignment_statement_enum:
245 case lang_padding_statement_enum:
246 case lang_address_statement_enum:
247 break;
248 default:
075d7359 249 FAIL ();
1418c83b
SC
250 break;
251 }
075d7359 252 }
1418c83b
SC
253}
254
255void
8ddef552 256lang_for_each_statement (func)
c477527c 257 void (*func) PARAMS ((lang_statement_union_type *));
1418c83b 258{
075d7359
SC
259 lang_for_each_statement_worker (func,
260 statement_list.head);
1418c83b 261}
075d7359 262
1418c83b 263/*----------------------------------------------------------------------*/
075d7359 264void
8ddef552
DM
265lang_list_init (list)
266 lang_statement_list_type *list;
2fa0b342 267{
075d7359
SC
268 list->head = (lang_statement_union_type *) NULL;
269 list->tail = &list->head;
2fa0b342
DHW
270}
271
1418c83b 272/*----------------------------------------------------------------------
075d7359 273
1418c83b
SC
274 build a new statement node for the parse tree
275
276 */
2fa0b342
DHW
277
278static
075d7359 279lang_statement_union_type *
8ddef552
DM
280new_statement (type, size, list)
281 enum statement_enum type;
5af8e9d6 282 size_t size;
8ddef552 283 lang_statement_list_type * list;
2fa0b342
DHW
284{
285 lang_statement_union_type *new = (lang_statement_union_type *)
075d7359
SC
286 stat_alloc (size);
287
2fa0b342 288 new->header.type = type;
075d7359
SC
289 new->header.next = (lang_statement_union_type *) NULL;
290 lang_statement_append (list, new, &new->header.next);
2fa0b342
DHW
291 return new;
292}
293
1418c83b
SC
294/*
295 Build a new input file node for the language. There are several ways
296 in which we treat an input file, eg, we only look at symbols, or
297 prefix it with a -l etc.
298
299 We can be supplied with requests for input files more than once;
300 they may, for example be split over serveral lines like foo.o(.text)
301 foo.o(.data) etc, so when asked for a file we check that we havn't
302 got it already so we don't duplicate the bfd.
303
304 */
2fa0b342 305static lang_input_statement_type *
8ddef552 306new_afile (name, file_type, target)
9f629407
ILT
307 CONST char *name;
308 lang_input_file_enum_type file_type;
309 CONST char *target;
2fa0b342 310{
dc4726c2 311
075d7359
SC
312 lang_input_statement_type *p = new_stat (lang_input_statement,
313 stat_ptr);
314
2fa0b342
DHW
315 lang_has_input_file = true;
316 p->target = target;
ee4af9e8 317 p->complained = false;
075d7359
SC
318 switch (file_type)
319 {
320 case lang_input_file_is_symbols_only_enum:
321 p->filename = name;
322 p->is_archive = false;
323 p->real = true;
324 p->local_sym_name = name;
325 p->just_syms_flag = true;
326 p->search_dirs_flag = false;
327 break;
328 case lang_input_file_is_fake_enum:
329 p->filename = name;
330 p->is_archive = false;
331 p->real = false;
332 p->local_sym_name = name;
333 p->just_syms_flag = false;
334 p->search_dirs_flag = false;
335 break;
336 case lang_input_file_is_l_enum:
337 p->is_archive = true;
338 p->filename = name;
339 p->real = true;
340 p->local_sym_name = concat ("-l", name, "");
341 p->just_syms_flag = false;
342 p->search_dirs_flag = true;
343 break;
344 case lang_input_file_is_search_file_enum:
345 case lang_input_file_is_marker_enum:
346 p->filename = name;
347 p->is_archive = false;
348 p->real = true;
349 p->local_sym_name = name;
350 p->just_syms_flag = false;
351 p->search_dirs_flag = true;
352 break;
353 case lang_input_file_is_file_enum:
354 p->filename = name;
355 p->is_archive = false;
356 p->real = true;
357 p->local_sym_name = name;
358 p->just_syms_flag = false;
359 p->search_dirs_flag = false;
360 break;
361 default:
362 FAIL ();
363 }
364 p->asymbols = (asymbol **) NULL;
365 p->superfile = (lang_input_statement_type *) NULL;
366 p->next_real_file = (lang_statement_union_type *) NULL;
367 p->next = (lang_statement_union_type *) NULL;
2fa0b342 368 p->symbol_count = 0;
075d7359 369 p->common_output_section = (asection *) NULL;
5e6cd559 370 p->loaded = false;
075d7359
SC
371 lang_statement_append (&input_file_chain,
372 (lang_statement_union_type *) p,
373 &p->next_real_file);
2fa0b342
DHW
374 return p;
375}
376
377lang_input_statement_type *
8ddef552
DM
378lang_add_input_file (name, file_type, target)
379 CONST char *name;
380 lang_input_file_enum_type file_type;
381 CONST char *target;
2fa0b342
DHW
382{
383 /* Look it up or build a new one */
1418c83b 384 lang_has_input_file = true;
a4aeaacf 385
1418c83b 386#if 0
2fa0b342
DHW
387 lang_input_statement_type *p;
388
075d7359
SC
389 for (p = (lang_input_statement_type *) input_file_chain.head;
390 p != (lang_input_statement_type *) NULL;
391 p = (lang_input_statement_type *) (p->next_real_file))
392 {
393 /* Sometimes we have incomplete entries in here */
394 if (p->filename != (char *) NULL)
395 {
396 if (strcmp (name, p->filename) == 0)
397 return p;
1418c83b 398 }
075d7359
SC
399
400 }
1418c83b 401#endif
075d7359 402 return new_afile (name, file_type, target);
2fa0b342
DHW
403}
404
1418c83b 405/* Build enough state so that the parser can build its tree */
2fa0b342 406void
8ddef552 407lang_init ()
2fa0b342 408{
075d7359 409 obstack_begin (&stat_obstack, 1000);
2fa0b342 410
075d7359 411 stat_ptr = &statement_list;
bfbdc80f 412
075d7359 413 lang_list_init (stat_ptr);
2fa0b342 414
075d7359
SC
415 lang_list_init (&input_file_chain);
416 lang_list_init (&lang_output_section_statement);
417 lang_list_init (&file_chain);
418 first_file = lang_add_input_file ((char *) NULL,
419 lang_input_file_is_marker_enum,
420 (char *) NULL);
421 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
9d1fe8a4 422
bfbdc80f 423 abs_output_section->bfd_section = &bfd_abs_section;
9d1fe8a4 424
2fa0b342
DHW
425}
426
1418c83b 427/*----------------------------------------------------------------------
075d7359
SC
428 A region is an area of memory declared with the
429 MEMORY { name:org=exp, len=exp ... }
430 syntax.
2fa0b342 431
1418c83b 432 We maintain a list of all the regions here
2fa0b342 433
1418c83b
SC
434 If no regions are specified in the script, then the default is used
435 which is created when looked up to be the entire data space
2fa0b342
DHW
436*/
437
438static lang_memory_region_type *lang_memory_region_list;
439static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
440
441lang_memory_region_type *
8ddef552
DM
442lang_memory_region_lookup (name)
443 CONST char *CONST name;
2fa0b342
DHW
444{
445
075d7359
SC
446 lang_memory_region_type *p = lang_memory_region_list;
447
1418c83b 448 for (p = lang_memory_region_list;
075d7359
SC
449 p != (lang_memory_region_type *) NULL;
450 p = p->next)
451 {
452 if (strcmp (p->name, name) == 0)
453 {
454 return p;
455 }
2fa0b342 456 }
075d7359 457 if (strcmp (name, "*default*") == 0)
2fa0b342 458 {
075d7359
SC
459 /* This is the default region, dig out first one on the list */
460 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
461 {
462 return lang_memory_region_list;
463 }
2fa0b342 464 }
075d7359
SC
465 {
466 lang_memory_region_type *new =
467 (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
468
469 new->name = buystring (name);
470 new->next = (lang_memory_region_type *) NULL;
471
472 *lang_memory_region_list_tail = new;
473 lang_memory_region_list_tail = &new->next;
474 new->origin = 0;
8ddef552 475 new->length = ~(bfd_size_type)0;
075d7359
SC
476 new->current = 0;
477 new->had_full_message = false;
478
479 return new;
480 }
2fa0b342
DHW
481}
482
483
2fa0b342 484lang_output_section_statement_type *
8ddef552
DM
485lang_output_section_find (name)
486 CONST char *CONST name;
2fa0b342
DHW
487{
488 lang_statement_union_type *u;
489 lang_output_section_statement_type *lookup;
490
491 for (u = lang_output_section_statement.head;
075d7359 492 u != (lang_statement_union_type *) NULL;
2fa0b342 493 u = lookup->next)
075d7359
SC
494 {
495 lookup = &u->output_section_statement;
496 if (strcmp (name, lookup->name) == 0)
497 {
1418c83b
SC
498 return lookup;
499 }
075d7359
SC
500 }
501 return (lang_output_section_statement_type *) NULL;
2fa0b342
DHW
502}
503
504lang_output_section_statement_type *
8ddef552
DM
505lang_output_section_statement_lookup (name)
506 CONST char *CONST name;
2fa0b342
DHW
507{
508 lang_output_section_statement_type *lookup;
2fa0b342 509
075d7359
SC
510 lookup = lang_output_section_find (name);
511 if (lookup == (lang_output_section_statement_type *) NULL)
512 {
2fa0b342 513
075d7359
SC
514 lookup = (lang_output_section_statement_type *)
515 new_stat (lang_output_section_statement, stat_ptr);
516 lookup->region = (lang_memory_region_type *) NULL;
517 lookup->fill = 0;
518 lookup->block_value = 1;
519 lookup->name = name;
520
521 lookup->next = (lang_statement_union_type *) NULL;
522 lookup->bfd_section = (asection *) NULL;
523 lookup->processed = false;
ae475b39 524 lookup->loadable = 1;
075d7359
SC
525 lookup->addr_tree = (etree_type *) NULL;
526 lang_list_init (&lookup->children);
527
1b8a42f3
ILT
528 lookup->memspec = (CONST char *) NULL;
529 lookup->flags = 0;
530 lookup->subsection_alignment = -1;
531 lookup->section_alignment = -1;
532 lookup->load_base = (union etree_union *) NULL;
533
075d7359
SC
534 lang_statement_append (&lang_output_section_statement,
535 (lang_statement_union_type *) lookup,
536 &lookup->next);
537 }
538 return lookup;
539}
2fa0b342 540
9d1fe8a4 541/*ARGSUSED*/
2fa0b342 542static void
8ddef552
DM
543print_flags (ignore_flags)
544 int *ignore_flags;
2fa0b342 545{
075d7359 546 fprintf (config.map_file, "(");
2fa0b342 547#if 0
075d7359
SC
548 if (flags->flag_read)
549 fprintf (outfile, "R");
550 if (flags->flag_write)
551 fprintf (outfile, "W");
552 if (flags->flag_executable)
553 fprintf (outfile, "X");
554 if (flags->flag_loadable)
555 fprintf (outfile, "L");
2fa0b342 556#endif
ae475b39 557 fprintf (config.map_file, ")");
2fa0b342
DHW
558}
559
560void
8ddef552 561lang_map ()
2fa0b342
DHW
562{
563 lang_memory_region_type *m;
075d7359
SC
564
565 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
48491e2e 566#ifdef HOST_64_BIT
075d7359 567 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
48491e2e 568#else
ae475b39
SC
569 fprintf (config.map_file,
570 "name\t\torigin length r_size c_size is attributes\n");
571
48491e2e 572#endif
2fa0b342 573 for (m = lang_memory_region_list;
075d7359
SC
574 m != (lang_memory_region_type *) NULL;
575 m = m->next)
2fa0b342 576 {
075d7359
SC
577 fprintf (config.map_file, "%-16s", m->name);
578 print_address (m->origin);
579 print_space ();
8ddef552 580 print_address ((bfd_vma)m->length);
075d7359 581 print_space ();
8ddef552 582 print_address ((bfd_vma)m->old_length);
ae475b39
SC
583 print_space();
584 print_address (m->current - m->origin);
585 print_space();
586 if (m->old_length)
fcf276c4
ILT
587 fprintf (config.map_file, " %2d%% ",
588 (int) ((m->current - m->origin) * 100 / m->old_length));
075d7359
SC
589 print_flags (&m->flags);
590 fprintf (config.map_file, "\n");
2fa0b342 591 }
075d7359
SC
592 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
593 fprintf (config.map_file, "output input virtual\n");
594 fprintf (config.map_file, "section section address tsize\n\n");
2fa0b342 595
075d7359 596 print_statements ();
2fa0b342
DHW
597
598}
599
600/*
601 *
602 */
075d7359 603static void
8ddef552
DM
604init_os (s)
605 lang_output_section_statement_type * s;
2fa0b342 606{
9d1fe8a4
SC
607/* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
608 section_userdata_type *new =
075d7359
SC
609 (section_userdata_type *)
610 stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
611
612 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
613 if (s->bfd_section == (asection *) NULL)
614 s->bfd_section = bfd_make_section (output_bfd, s->name);
615 if (s->bfd_section == (asection *) NULL)
616 {
ddddcdf0 617 einfo ("%P%F: output format %s cannot represent section called %s\n",
075d7359 618 output_bfd->xvec->name, s->name);
9d1fe8a4 619 }
2fa0b342 620 s->bfd_section->output_section = s->bfd_section;
ae475b39 621/* s->bfd_section->flags = s->flags;*/
7bc4a0d7 622
2fa0b342
DHW
623 /* We initialize an output sections output offset to minus its own */
624 /* vma to allow us to output a section through itself */
625 s->bfd_section->output_offset = 0;
075d7359 626 get_userdata (s->bfd_section) = (PTR) new;
c611e285 627
2fa0b342
DHW
628}
629
1418c83b
SC
630/***********************************************************************
631 The wild routines.
632
633 These expand statements like *(.text) and foo.o to a list of
634 explicit actions, like foo.o(.text), bar.o(.text) and
635 foo.o(.text,.data) .
636
637 The toplevel routine, wild, takes a statement, section, file and
638 target. If either the section or file is null it is taken to be the
639 wildcard. Seperate lang_input_section statements are created for
640 each part of the expanstion, and placed after the statement provided.
641
642*/
075d7359 643
2fa0b342 644static void
8ddef552
DM
645wild_doit (ptr, section, output, file)
646 lang_statement_list_type * ptr;
647 asection * section;
648 lang_output_section_statement_type * output;
649 lang_input_statement_type * file;
2fa0b342 650{
075d7359 651 if (output->bfd_section == (asection *) NULL)
ae475b39
SC
652 {
653 init_os (output);
e9b63852
ILT
654 /* Initialize the vma and size to the existing section. This will
655 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
656 set. */
657 if (section != (asection *) NULL)
658 {
659 bfd_set_section_vma (0, output->bfd_section,
660 bfd_section_vma (0, section));
661 output->bfd_section->_raw_size = section->_raw_size;
662 }
ae475b39 663 }
2fa0b342 664
075d7359
SC
665 if (section != (asection *) NULL
666 && section->output_section == (asection *) NULL)
ae475b39
SC
667 {
668 /* Add a section reference to the list */
669 lang_input_section_type *new = new_stat (lang_input_section, ptr);
670
671 new->section = section;
672 new->ifile = file;
673 section->output_section = output->bfd_section;
29f33467
SC
674
675 /* Be selective about what the output section inherits from the
676 input section */
677
8ddef552
DM
678 if ((section->flags & SEC_SHARED_LIBRARY) != 0)
679 section->output_section->flags |= section->flags;
680 else
9f629407
ILT
681 section->output_section->flags |=
682 section->flags & (flagword) (~ SEC_NEVER_LOAD);
29f33467 683
ae475b39 684 if (!output->loadable)
075d7359 685 {
29f33467 686 /* Turn off load flag */
ae475b39 687 output->bfd_section->flags &= ~SEC_LOAD;
29f33467 688 output->bfd_section->flags |= SEC_NEVER_LOAD;
ae475b39
SC
689 }
690 if (section->alignment_power > output->bfd_section->alignment_power)
691 {
692 output->bfd_section->alignment_power = section->alignment_power;
075d7359 693 }
e20873a7
JG
694 /* If supplied an aligmnet, then force it */
695 if (output->section_alignment != -1)
696 {
697 output->bfd_section->alignment_power = output->section_alignment;
698 }
ae475b39 699 }
2fa0b342
DHW
700}
701
702static asection *
8ddef552
DM
703our_bfd_get_section_by_name (abfd, section)
704 bfd * abfd;
705 CONST char *section;
2fa0b342 706{
075d7359 707 return bfd_get_section_by_name (abfd, section);
2fa0b342 708}
1418c83b 709
075d7359 710static void
8ddef552
DM
711wild_section (ptr, section, file, output)
712 lang_wild_statement_type * ptr;
713 CONST char *section;
714 lang_input_statement_type * file;
715 lang_output_section_statement_type * output;
2fa0b342
DHW
716{
717 asection *s;
075d7359
SC
718
719 if (file->just_syms_flag == false)
720 {
721 if (section == (char *) NULL)
722 {
723 /* Do the creation to all sections in the file */
724 for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next)
29f33467
SC
725 {
726 /* except for bss */
727 if ((s->flags & SEC_IS_COMMON) == 0)
075d7359
SC
728 {
729 wild_doit (&ptr->children, s, output, file);
730 }
29f33467 731 }
075d7359
SC
732 }
733 else
734 {
735 /* Do the creation to the named section only */
736 wild_doit (&ptr->children,
737 our_bfd_get_section_by_name (file->the_bfd, section),
738 output, file);
739 }
2fa0b342 740 }
2fa0b342
DHW
741}
742
1418c83b
SC
743/* passed a file name (which must have been seen already and added to
744 the statement tree. We will see if it has been opened already and
745 had its symbols read. If not then we'll read it.
2fa0b342 746
1418c83b
SC
747 Archives are pecuilar here. We may open them once, but if they do
748 not define anything we need at the time, they won't have all their
749 symbols read. If we need them later, we'll have to redo it.
750 */
2fa0b342 751static
1418c83b 752lang_input_statement_type *
8ddef552 753lookup_name (name)
9f629407 754 CONST char *name;
2fa0b342
DHW
755{
756 lang_input_statement_type *search;
075d7359
SC
757
758 for (search = (lang_input_statement_type *) input_file_chain.head;
759 search != (lang_input_statement_type *) NULL;
760 search = (lang_input_statement_type *) search->next_real_file)
761 {
762 if (search->filename == (char *) NULL && name == (char *) NULL)
c477527c
ILT
763 return search;
764 if (search->filename != (char *) NULL
765 && name != (char *) NULL
766 && strcmp (search->filename, name) == 0)
767 break;
075d7359 768 }
2fa0b342 769
c477527c
ILT
770 if (search == (lang_input_statement_type *) NULL)
771 {
772 /* There isn't an afile entry for this file yet, this must be
773 because the name has only appeared inside a load script and
774 not on the command line */
775 search = new_afile (name, lang_input_file_is_file_enum, default_target);
776 }
777
5e6cd559
ILT
778 /* If we have already added this file, or this file is not real
779 (FIXME: can that ever actually happen?) or the name is NULL
780 (FIXME: can that ever actually happen?) don't add this file. */
781 if (search->loaded
782 || ! search->real
783 || search->filename == (const char *) NULL)
784 return search;
785
c477527c
ILT
786 ldfile_open_file (search);
787
788 if (bfd_check_format (search->the_bfd, bfd_object))
5e6cd559
ILT
789 {
790 ldlang_add_file (search);
791 if (trace_files || trace_file_tries)
792 info_msg ("%I\n", search);
793 }
c477527c
ILT
794 else if (bfd_check_format (search->the_bfd, bfd_archive))
795 {
796 /* There is nothing to do here; the add_symbols routine will
797 call ldlang_add_file (via the add_archive_element callback)
798 for each element of the archive which is used. */
799 }
800 else
801 einfo ("%F%B: file not recognized: %E\n", search->the_bfd);
1418c83b 802
de08b0b1
ILT
803 bfd_set_gp_size (search->the_bfd, g_switch_value);
804
c477527c
ILT
805 if (bfd_link_add_symbols (search->the_bfd, &link_info) == false)
806 einfo ("%F%B: could not read symbols: %E\n", search->the_bfd);
1418c83b 807
5e6cd559
ILT
808 search->loaded = true;
809
c477527c 810 return search;
2fa0b342
DHW
811}
812
813static void
8ddef552
DM
814wild (s, section, file, target, output)
815 lang_wild_statement_type * s;
9f629407
ILT
816 CONST char *section;
817 CONST char *file;
818 CONST char *target;
8ddef552 819 lang_output_section_statement_type * output;
2fa0b342
DHW
820{
821 lang_input_statement_type *f;
075d7359
SC
822
823 if (file == (char *) NULL)
824 {
825 /* Perform the iteration over all files in the list */
826 for (f = (lang_input_statement_type *) file_chain.head;
827 f != (lang_input_statement_type *) NULL;
828 f = (lang_input_statement_type *) f->next)
829 {
830 wild_section (s, section, f, output);
831 }
832 }
833 else
834 {
835 /* Perform the iteration over a single file */
836 wild_section (s, section, lookup_name (file), output);
837 }
838 if (section != (char *) NULL
839 && strcmp (section, "COMMON") == 0
840 && default_common_section == (lang_output_section_statement_type *) NULL)
841 {
842 /* Remember the section that common is going to incase we later
843 get something which doesn't know where to put it */
844 default_common_section = output;
2fa0b342 845 }
2fa0b342
DHW
846}
847
848/*
075d7359 849 read in all the files
2fa0b342 850 */
97fbbaca 851
075d7359 852static bfd *
8ddef552 853open_output (name)
9f629407 854 CONST char *name;
2fa0b342 855{
097879bc 856 bfd *output;
097879bc 857
075d7359 858 if (output_target == (char *) NULL)
ae475b39
SC
859 {
860 if (current_target != (char *) NULL)
861 output_target = current_target;
862 else
863 output_target = default_target;
864 }
075d7359 865 output = bfd_openw (name, output_target);
075d7359
SC
866
867 if (output == (bfd *) NULL)
868 {
ae475b39
SC
869 if (bfd_error == invalid_target)
870 {
ddddcdf0 871 einfo ("%P%F: target %s not found\n", output_target);
ae475b39 872 }
ddddcdf0 873 einfo ("%P%F: cannot open output file %s: %E\n", name);
075d7359 874 }
30d1a390 875
97fbbaca
JL
876 delete_output_file_on_failure = 1;
877
30d1a390 878 /* output->flags |= D_PAGED;*/
075d7359 879
ddddcdf0
ILT
880 if (! bfd_set_format (output, bfd_object))
881 einfo ("%P%F:%s: can not make object file: %E\n", name);
882 if (! bfd_set_arch_mach (output,
883 ldfile_output_architecture,
884 ldfile_output_machine))
885 einfo ("%P%F:%s: can not set architecture: %E\n", name);
886
c477527c
ILT
887 link_info.hash = bfd_link_hash_table_create (output);
888 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
889 einfo ("%P%F: can not create link hash table: %E\n");
890
1b8a42f3 891 bfd_set_gp_size (output, g_switch_value);
2fa0b342
DHW
892 return output;
893}
1418c83b
SC
894
895
097879bc 896
1418c83b 897
2fa0b342 898static void
8ddef552
DM
899ldlang_open_output (statement)
900 lang_statement_union_type * statement;
2fa0b342 901{
075d7359
SC
902 switch (statement->header.type)
903 {
c477527c
ILT
904 case lang_output_statement_enum:
905 ASSERT (output_bfd == (bfd *) NULL);
075d7359
SC
906 output_bfd = open_output (statement->output_statement.name);
907 ldemul_set_output_arch ();
c477527c 908 if (config.magic_demand_paged && !link_info.relocateable)
075d7359
SC
909 output_bfd->flags |= D_PAGED;
910 else
911 output_bfd->flags &= ~D_PAGED;
912 if (config.text_read_only)
913 output_bfd->flags |= WP_TEXT;
914 else
915 output_bfd->flags &= ~WP_TEXT;
916 break;
1418c83b 917
075d7359
SC
918 case lang_target_statement_enum:
919 current_target = statement->target_statement.target;
920 break;
921 default:
922 break;
923 }
1418c83b 924}
2fa0b342 925
1418c83b 926static void
8ddef552
DM
927open_input_bfds (statement)
928 lang_statement_union_type * statement;
1418c83b 929{
075d7359
SC
930 switch (statement->header.type)
931 {
1418c83b 932 case lang_target_statement_enum:
075d7359
SC
933 current_target = statement->target_statement.target;
934 break;
935 case lang_wild_statement_enum:
936 /* Maybe we should load the file's symbols */
937 if (statement->wild_statement.filename)
938 {
939 (void) lookup_name (statement->wild_statement.filename);
940 }
941 break;
942 case lang_input_statement_enum:
943 if (statement->input_statement.real == true)
944 {
945 statement->input_statement.target = current_target;
946 lookup_name (statement->input_statement.filename);
947 }
948 break;
949 default:
950 break;
951 }
2fa0b342 952}
075d7359 953
2fa0b342
DHW
954/* If there are [COMMONS] statements, put a wild one into the bss section */
955
956static void
075d7359 957lang_reasonable_defaults ()
2fa0b342 958{
1418c83b 959#if 0
075d7359
SC
960 lang_output_section_statement_lookup (".text");
961 lang_output_section_statement_lookup (".data");
8cb5eb31 962
075d7359
SC
963 default_common_section =
964 lang_output_section_statement_lookup (".bss");
8cb5eb31 965
1418c83b 966
075d7359
SC
967 if (placed_commons == false)
968 {
969 lang_wild_statement_type *new =
970 new_stat (lang_wild_statement,
971 &default_common_section->children);
972
973 new->section_name = "COMMON";
974 new->filename = (char *) NULL;
975 lang_list_init (&new->children);
976 }
1418c83b 977#endif
8cb5eb31 978
2fa0b342
DHW
979}
980
1418c83b
SC
981/*
982 Add the supplied name to the symbol table as an undefined reference.
983 Remove items from the chain as we open input bfds
984 */
075d7359
SC
985typedef struct ldlang_undef_chain_list
986{
f177a611 987 struct ldlang_undef_chain_list *next;
1418c83b 988 char *name;
075d7359 989} ldlang_undef_chain_list_type;
1418c83b
SC
990
991static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
992
993void
8ddef552
DM
994ldlang_add_undef (name)
995 CONST char *CONST name;
2fa0b342 996{
1418c83b 997 ldlang_undef_chain_list_type *new =
075d7359
SC
998 (ldlang_undef_chain_list_type
999 *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
1418c83b
SC
1000
1001 new->next = ldlang_undef_chain_list_head;
1002 ldlang_undef_chain_list_head = new;
1003
075d7359 1004 new->name = buystring (name);
1418c83b 1005}
075d7359 1006
1418c83b
SC
1007/* Run through the list of undefineds created above and place them
1008 into the linker hash table as undefined symbols belonging to the
1009 script file.
1010*/
1011static void
8ddef552 1012lang_place_undefineds ()
1418c83b 1013{
c477527c 1014 ldlang_undef_chain_list_type *ptr;
1418c83b 1015
c477527c
ILT
1016 for (ptr = ldlang_undef_chain_list_head;
1017 ptr != (ldlang_undef_chain_list_type *) NULL;
1018 ptr = ptr->next)
075d7359 1019 {
c477527c 1020 struct bfd_link_hash_entry *h;
075d7359 1021
c477527c
ILT
1022 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1023 if (h == (struct bfd_link_hash_entry *) NULL)
1024 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1025 if (h->type == bfd_link_hash_new)
1026 {
1027 h->type = bfd_link_hash_undefined;
1028 h->u.undef.abfd = NULL;
1029 bfd_link_add_undef (link_info.hash, h);
1030 }
075d7359
SC
1031 }
1032}
1418c83b
SC
1033
1034/* Copy important data from out internal form to the bfd way. Also
1035 create a section for the dummy file
1036 */
1037
1038static void
8ddef552 1039lang_create_output_section_statements ()
1418c83b 1040{
075d7359
SC
1041 lang_statement_union_type *os;
1042
1418c83b 1043 for (os = lang_output_section_statement.head;
075d7359
SC
1044 os != (lang_statement_union_type *) NULL;
1045 os = os->output_section_statement.next)
1046 {
1047 lang_output_section_statement_type *s =
1418c83b 1048 &os->output_section_statement;
075d7359
SC
1049
1050 init_os (s);
1051 }
1418c83b
SC
1052
1053}
1054
2fa0b342
DHW
1055/* Open input files and attatch to output sections */
1056static void
8ddef552
DM
1057map_input_to_output_sections (s, target, output_section_statement)
1058 lang_statement_union_type * s;
1059 CONST char *target;
1060 lang_output_section_statement_type * output_section_statement;
2fa0b342 1061{
075d7359 1062 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 1063 {
075d7359 1064 switch (s->header.type)
2fa0b342 1065 {
075d7359
SC
1066
1067
1068 case lang_wild_statement_enum:
1069 wild (&s->wild_statement, s->wild_statement.section_name,
1070 s->wild_statement.filename, target,
1071 output_section_statement);
1072
1073 break;
1074 case lang_constructors_statement_enum:
1075 map_input_to_output_sections (constructor_list.head,
1076 target,
1077 output_section_statement);
1078 break;
1079 case lang_output_section_statement_enum:
1080 map_input_to_output_sections (s->output_section_statement.children.head,
1081 target,
1082 &s->output_section_statement);
1083 break;
1084 case lang_output_statement_enum:
1085 break;
1086 case lang_target_statement_enum:
1087 target = s->target_statement.target;
1088 break;
1089 case lang_fill_statement_enum:
1090 case lang_input_section_enum:
1091 case lang_object_symbols_statement_enum:
1092 case lang_data_statement_enum:
1093 case lang_assignment_statement_enum:
1094 case lang_padding_statement_enum:
1095 break;
1096 case lang_afile_asection_pair_statement_enum:
1097 FAIL ();
1098 break;
1099 case lang_address_statement_enum:
1100 /* Mark the specified section with the supplied address */
1101 {
1102 lang_output_section_statement_type *os =
2fa0b342 1103 lang_output_section_statement_lookup
075d7359
SC
1104 (s->address_statement.section_name);
1105
1106 os->addr_tree = s->address_statement.address;
1107 if (os->bfd_section == (asection *) NULL)
1108 {
ddddcdf0 1109 einfo ("%P%F: cannot set the address of undefined section %s\n",
075d7359
SC
1110 s->address_statement.section_name);
1111 }
48491e2e 1112 }
075d7359
SC
1113 break;
1114 case lang_input_statement_enum:
1115 /* A standard input statement, has no wildcards */
075d7359 1116 break;
2fa0b342 1117 }
2fa0b342
DHW
1118 }
1119}
1120
075d7359 1121static void
8ddef552
DM
1122print_output_section_statement (output_section_statement)
1123 lang_output_section_statement_type * output_section_statement;
2fa0b342
DHW
1124{
1125 asection *section = output_section_statement->bfd_section;
075d7359
SC
1126
1127 print_nl ();
1128 print_section (output_section_statement->name);
1129
e20873a7 1130
075d7359 1131 if (section)
e20873a7
JG
1132 {
1133 print_dot = section->vma;
1134 print_space ();
1135 print_section ("");
1136 print_space ();
1137 print_address (section->vma);
1138 print_space ();
1139 print_size (section->_raw_size);
1140 print_space();
1141 print_size(section->_cooked_size);
1142 print_space ();
1143 print_alignment (section->alignment_power);
1144 print_space ();
2fa0b342 1145#if 0
e20873a7
JG
1146 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1147 print_flags (stdout, &output_section_statement->flags);
2fa0b342 1148#endif
e20873a7
JG
1149 if (section->flags & SEC_LOAD)
1150 fprintf (config.map_file, "load ");
1151 if (section->flags & SEC_ALLOC)
1152 fprintf (config.map_file, "alloc ");
1153 if (section->flags & SEC_RELOC)
1154 fprintf (config.map_file, "reloc ");
1155 if (section->flags & SEC_HAS_CONTENTS)
1156 fprintf (config.map_file, "contents ");
2fa0b342 1157
e20873a7 1158 }
075d7359 1159 else
e20873a7
JG
1160 {
1161 fprintf (config.map_file, "No attached output section");
1162 }
1163 print_nl ();
9fce28ed
SC
1164 if (output_section_statement->load_base)
1165 {
1166 int b = exp_get_value_int(output_section_statement->load_base,
1167 0, "output base", lang_final_phase_enum);
1168 printf("Output address %08x\n", b);
1169 }
e20873a7
JG
1170 if (output_section_statement->section_alignment >= 0
1171 || output_section_statement->section_alignment >= 0)
1172 {
1173 printf("\t\t\t\t\tforced alignment ");
1174 if ( output_section_statement->section_alignment >= 0)
075d7359 1175 {
e20873a7 1176 printf("section 2**%d ",output_section_statement->section_alignment );
075d7359 1177 }
e20873a7
JG
1178 if ( output_section_statement->subsection_alignment >= 0)
1179 {
1180 printf("subsection 2**%d ",output_section_statement->subsection_alignment );
1181 }
1182
1183 print_nl ();
1184 }
075d7359
SC
1185 print_statement (output_section_statement->children.head,
1186 output_section_statement);
2fa0b342
DHW
1187
1188}
1189
075d7359 1190static void
8ddef552
DM
1191print_assignment (assignment, output_section)
1192 lang_assignment_statement_type * assignment;
1193 lang_output_section_statement_type * output_section;
2fa0b342
DHW
1194{
1195 etree_value_type result;
075d7359
SC
1196
1197 print_section ("");
1198 print_space ();
1199 print_section ("");
1200 print_space ();
1201 print_address (print_dot);
1202 print_space ();
1203 result = exp_fold_tree (assignment->exp->assign.src,
1204 output_section,
1205 lang_final_phase_enum,
1206 print_dot,
1207 &print_dot);
1208
1209 if (result.valid)
1210 {
1211 print_address (result.value);
1212 }
1213 else
1214 {
1215 fprintf (config.map_file, "*undefined*");
1216 }
1217 print_space ();
1218 exp_print_tree (assignment->exp);
1219
1220 fprintf (config.map_file, "\n");
2fa0b342
DHW
1221}
1222
1223static void
8ddef552
DM
1224print_input_statement (statm)
1225 lang_input_statement_type * statm;
2fa0b342 1226{
075d7359
SC
1227 if (statm->filename != (char *) NULL)
1228 {
1229 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1230 }
2fa0b342
DHW
1231}
1232
804c8601
SC
1233/* Print all the defined symbols for the abfd provided by in the supplied
1234 section.
1235*/
1236
1237static boolean
1238print_one_symbol (hash_entry, ptr)
1239struct bfd_link_hash_entry *hash_entry;
1240PTR ptr;
2fa0b342 1241{
804c8601
SC
1242 asection * sec = (asection *)ptr;
1243
1244 if (hash_entry->type == bfd_link_hash_defined)
1245 {
1246 if (sec == hash_entry->u.def.section) {
1247 print_section ("");
1248 fprintf (config.map_file, " ");
1249 print_section ("");
1250 fprintf (config.map_file, " ");
1251 print_address (hash_entry->u.def.value + outside_section_address (sec));
1252 fprintf (config.map_file, " %s", hash_entry->root.string);
1253 print_nl ();
1254 }
1255 }
2fa0b342 1256}
1418c83b 1257
075d7359 1258static void
8ddef552
DM
1259print_input_section (in)
1260 lang_input_section_type * in;
2fa0b342
DHW
1261{
1262 asection *i = in->section;
aa34a7c3 1263 int size = i->reloc_done ?
075d7359
SC
1264 bfd_get_section_size_after_reloc (i) :
1265 bfd_get_section_size_before_reloc (i);
1418c83b 1266
075d7359
SC
1267 if (size != 0)
1268 {
1269 print_section ("");
1270 fprintf (config.map_file, " ");
1271 print_section (i->name);
1272 fprintf (config.map_file, " ");
1273 if (i->output_section)
1274 {
1275 print_address (i->output_section->vma + i->output_offset);
1276 fprintf (config.map_file, " ");
ae475b39
SC
1277 print_size (i->_raw_size);
1278 fprintf (config.map_file, " ");
1279 print_size(i->_cooked_size);
075d7359
SC
1280 fprintf (config.map_file, " ");
1281 print_alignment (i->alignment_power);
1282 fprintf (config.map_file, " ");
1283 if (in->ifile)
1284 {
2fa0b342 1285
075d7359 1286 bfd *abfd = in->ifile->the_bfd;
2fa0b342 1287
075d7359
SC
1288 if (in->ifile->just_syms_flag == true)
1289 {
1290 fprintf (config.map_file, "symbols only ");
1291 }
2fa0b342 1292
075d7359
SC
1293 fprintf (config.map_file, " %s ", abfd->xvec->name);
1294 if (abfd->my_archive != (bfd *) NULL)
1295 {
1296 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1297 abfd->filename);
1298 }
1299 else
1300 {
1301 fprintf (config.map_file, "%s", abfd->filename);
1302 }
1303 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1304 print_nl ();
2fa0b342 1305
804c8601
SC
1306 /* Print all the symbols */
1307 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
075d7359
SC
1308 }
1309 else
1310 {
1311 print_nl ();
1312 }
1313
1314
1315 print_dot = outside_section_address (i) + size;
1316 }
1317 else
1318 {
1319 fprintf (config.map_file, "No output section allocated\n");
1320 }
1321 }
1322}
2fa0b342
DHW
1323
1324static void
8ddef552
DM
1325print_fill_statement (fill)
1326 lang_fill_statement_type * fill;
075d7359
SC
1327{
1328 fprintf (config.map_file, "FILL mask ");
1329 print_fill (fill->fill);
1330}
1331
1332static void
8ddef552
DM
1333print_data_statement (data)
1334 lang_data_statement_type * data;
2fa0b342
DHW
1335{
1336/* bfd_vma value; */
075d7359
SC
1337 print_section ("");
1338 print_space ();
1339 print_section ("");
1340 print_space ();
65c552e3 1341/* ASSERT(print_dot == data->output_vma);*/
2fa0b342 1342
075d7359
SC
1343 print_address (data->output_vma + data->output_section->vma);
1344 print_space ();
1345 print_address (data->value);
1346 print_space ();
1347 switch (data->type)
1348 {
1349 case BYTE:
1350 fprintf (config.map_file, "BYTE ");
1351 print_dot += BYTE_SIZE;
1352 break;
1353 case SHORT:
1354 fprintf (config.map_file, "SHORT ");
1355 print_dot += SHORT_SIZE;
1356 break;
1357 case LONG:
1358 fprintf (config.map_file, "LONG ");
1359 print_dot += LONG_SIZE;
1360 break;
c477527c
ILT
1361 case QUAD:
1362 fprintf (config.map_file, "QUAD ");
1363 print_dot += QUAD_SIZE;
1364 break;
075d7359
SC
1365 }
1366
1367 exp_print_tree (data->exp);
2fa0b342 1368
075d7359 1369 fprintf (config.map_file, "\n");
2fa0b342
DHW
1370}
1371
1372
1373static void
8ddef552
DM
1374print_padding_statement (s)
1375 lang_padding_statement_type * s;
075d7359
SC
1376{
1377 print_section ("");
1378 print_space ();
1379 print_section ("*fill*");
1380 print_space ();
1381 print_address (s->output_offset + s->output_section->vma);
1382 print_space ();
1383 print_size (s->size);
1384 print_space ();
1385 print_fill (s->fill);
1386 print_nl ();
ffc50032 1387
aa34a7c3 1388 print_dot = s->output_offset + s->output_section->vma + s->size;
ffc50032 1389
2fa0b342
DHW
1390}
1391
075d7359 1392static void
8ddef552
DM
1393print_wild_statement (w, os)
1394 lang_wild_statement_type * w;
1395 lang_output_section_statement_type * os;
2fa0b342 1396{
075d7359
SC
1397 fprintf (config.map_file, " from ");
1398 if (w->filename != (char *) NULL)
1399 {
1400 fprintf (config.map_file, "%s", w->filename);
1401 }
1402 else
1403 {
1404 fprintf (config.map_file, "*");
1405 }
1406 if (w->section_name != (char *) NULL)
1407 {
1408 fprintf (config.map_file, "(%s)", w->section_name);
1409 }
1410 else
1411 {
1412 fprintf (config.map_file, "(*)");
1413 }
1414 print_nl ();
1415 print_statement (w->children.head, os);
2fa0b342
DHW
1416
1417}
1418static void
8ddef552
DM
1419print_statement (s, os)
1420 lang_statement_union_type * s;
1421 lang_output_section_statement_type * os;
2fa0b342 1422{
075d7359 1423 while (s)
c611e285 1424 {
075d7359 1425 switch (s->header.type)
c611e285 1426 {
075d7359
SC
1427 case lang_constructors_statement_enum:
1428 fprintf (config.map_file, "constructors:\n");
1429 print_statement (constructor_list.head, os);
1430 break;
1431 case lang_wild_statement_enum:
1432 print_wild_statement (&s->wild_statement, os);
1433 break;
1434 default:
1435 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1436 FAIL ();
1437 break;
1438 case lang_address_statement_enum:
1439 fprintf (config.map_file, "address\n");
1440 break;
1441 case lang_object_symbols_statement_enum:
1442 fprintf (config.map_file, "object symbols\n");
1443 break;
1444 case lang_fill_statement_enum:
1445 print_fill_statement (&s->fill_statement);
1446 break;
1447 case lang_data_statement_enum:
1448 print_data_statement (&s->data_statement);
1449 break;
1450 case lang_input_section_enum:
1451 print_input_section (&s->input_section);
1452 break;
1453 case lang_padding_statement_enum:
1454 print_padding_statement (&s->padding_statement);
1455 break;
1456 case lang_output_section_statement_enum:
1457 print_output_section_statement (&s->output_section_statement);
1458 break;
1459 case lang_assignment_statement_enum:
1460 print_assignment (&s->assignment_statement,
1461 os);
1462 break;
1463 case lang_target_statement_enum:
1464 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1465 break;
1466 case lang_output_statement_enum:
1467 fprintf (config.map_file, "OUTPUT(%s %s)\n",
c611e285 1468 s->output_statement.name,
e20873a7 1469 output_target ? output_target : "");
075d7359
SC
1470 break;
1471 case lang_input_statement_enum:
1472 print_input_statement (&s->input_statement);
1473 break;
1474 case lang_afile_asection_pair_statement_enum:
1475 FAIL ();
1476 break;
c611e285 1477 }
075d7359 1478 s = s->next;
2fa0b342 1479 }
2fa0b342
DHW
1480}
1481
1482
1483static void
8ddef552 1484print_statements ()
2fa0b342 1485{
075d7359
SC
1486 print_statement (statement_list.head,
1487 abs_output_section);
1488
2fa0b342
DHW
1489}
1490
1491static bfd_vma
9f629407
ILT
1492insert_pad (this_ptr, fill, power, output_section_statement, dot)
1493 lang_statement_union_type ** this_ptr;
1494 fill_type fill;
1495 unsigned int power;
1496 asection * output_section_statement;
1497 bfd_vma dot;
075d7359
SC
1498{
1499 /* Align this section first to the
2fa0b342
DHW
1500 input sections requirement, then
1501 to the output section's requirement.
075d7359 1502 If this alignment is > than any seen before,
2fa0b342
DHW
1503 then record it too. Perform the alignment by
1504 inserting a magic 'padding' statement.
1505 */
1506
075d7359 1507 unsigned int alignment_needed = align_power (dot, power) - dot;
2fa0b342 1508
075d7359 1509 if (alignment_needed != 0)
2fa0b342 1510 {
075d7359
SC
1511 lang_statement_union_type *new =
1512 (lang_statement_union_type *)
1513 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1514
2fa0b342
DHW
1515 /* Link into existing chain */
1516 new->header.next = *this_ptr;
1517 *this_ptr = new;
1518 new->header.type = lang_padding_statement_enum;
1519 new->padding_statement.output_section = output_section_statement;
1520 new->padding_statement.output_offset =
1521 dot - output_section_statement->vma;
1522 new->padding_statement.fill = fill;
1523 new->padding_statement.size = alignment_needed;
1524 }
1525
1526
1527 /* Remember the most restrictive alignment */
075d7359
SC
1528 if (power > output_section_statement->alignment_power)
1529 {
1530 output_section_statement->alignment_power = power;
1531 }
c611e285 1532 output_section_statement->_raw_size += alignment_needed;
2fa0b342
DHW
1533 return alignment_needed + dot;
1534
1535}
1536
1418c83b 1537/* Work out how much this section will move the dot point */
075d7359 1538static bfd_vma
9f629407
ILT
1539size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1540 lang_statement_union_type ** this_ptr;
1541 lang_output_section_statement_type * output_section_statement;
c477527c 1542 fill_type fill;
9f629407
ILT
1543 bfd_vma dot;
1544 boolean relax;
2fa0b342
DHW
1545{
1546 lang_input_section_type *is = &((*this_ptr)->input_section);
1547 asection *i = is->section;
2fa0b342 1548
075d7359
SC
1549 if (is->ifile->just_syms_flag == false)
1550 {
e20873a7
JG
1551 if (output_section_statement->subsection_alignment != -1)
1552 i->alignment_power =
1553 output_section_statement->subsection_alignment;
1554
075d7359
SC
1555 dot = insert_pad (this_ptr, fill, i->alignment_power,
1556 output_section_statement->bfd_section, dot);
1557
1558 /* remember the largest size so we can malloc the largest area
1559 needed for the output stage. Only remember the size of sections
1560 which we will actually allocate */
8ddef552 1561 if ((i->flags & SEC_HAS_CONTENTS) != 0
075d7359
SC
1562 && (bfd_get_section_size_before_reloc (i) > largest_section))
1563 {
1564 largest_section = bfd_get_section_size_before_reloc (i);
1565 }
2fa0b342 1566
075d7359 1567 /* Remember where in the output section this input section goes */
ac004870 1568
075d7359
SC
1569 i->output_offset = dot - output_section_statement->bfd_section->vma;
1570
ae475b39
SC
1571 /* Mark how big the output section must be to contain this now
1572 */
1573 if (relax)
1574 {
1575 dot += i->_cooked_size;
1576 }
1577 else
1578 {
1579 dot += i->_raw_size;
1580 }
1581 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
075d7359 1582 }
ac004870 1583 else
075d7359
SC
1584 {
1585 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1586 }
2fa0b342 1587
075d7359 1588 return dot;
2fa0b342
DHW
1589}
1590
c611e285
SC
1591/* Sizing happens in two passes, first pass we allocate worst case
1592 stuff. The second pass (if relaxing), we use what we learnt to
1593 change the size of some relocs from worst case to better
1594 */
1595static boolean had_relax;
1596
1597static bfd_vma
9f629407
ILT
1598lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1599 lang_statement_union_type * s;
1600 lang_output_section_statement_type * output_section_statement;
1601 lang_statement_union_type ** prev;
c477527c 1602 fill_type fill;
9f629407
ILT
1603 bfd_vma dot;
1604 boolean relax;
c611e285
SC
1605{
1606 /* Size up the sections from their constituent parts */
075d7359 1607 for (; s != (lang_statement_union_type *) NULL; s = s->next)
ae475b39
SC
1608 {
1609 switch (s->header.type)
075d7359 1610 {
c611e285 1611
ae475b39
SC
1612 case lang_output_section_statement_enum:
1613 {
1614 bfd_vma after;
1615 lang_output_section_statement_type *os = &s->output_section_statement;
1616
e20873a7
JG
1617 /* If this is a shared library section, don't change the size
1618 and address. */
1619 if (os->bfd_section->flags & SEC_SHARED_LIBRARY)
e9b63852
ILT
1620 break;
1621
ae475b39
SC
1622 if (os->bfd_section == &bfd_abs_section)
1623 {
1624 /* No matter what happens, an abs section starts at zero */
1625 bfd_set_section_vma (0, os->bfd_section, 0);
1626 }
1627 else
1628 {
1629 if (os->addr_tree == (etree_type *) NULL)
1630 {
1631 /* No address specified for this section, get one
1632 from the region specification
1633 */
1634 if (os->region == (lang_memory_region_type *) NULL)
1635 {
1636 os->region = lang_memory_region_lookup ("*default*");
1637 }
1638 dot = os->region->current;
1639 }
1640 else
1641 {
1642 etree_value_type r;
1643
1644 r = exp_fold_tree (os->addr_tree,
1645 abs_output_section,
1646 lang_allocating_phase_enum,
1647 dot, &dot);
1648 if (r.valid == false)
1649 {
1650 einfo ("%F%S: non constant address expression for section %s\n",
1651 os->name);
1652 }
1653 dot = r.value;
1654 }
1655 /* The section starts here */
1656 /* First, align to what the section needs */
1657
1658
1659 dot = align_power (dot, os->bfd_section->alignment_power);
1660 bfd_set_section_vma (0, os->bfd_section, dot);
9fce28ed
SC
1661
1662 if (os->load_base) {
1663 os->bfd_section->lma
1664 = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum);
1665 }
ae475b39
SC
1666 }
1667
1668
1669 os->bfd_section->output_offset = 0;
1670
1671 (void) lang_size_sections (os->children.head, os, &os->children.head,
1672 os->fill, dot, relax);
1673 /* Ignore the size of the input sections, use the vma and size to */
1674 /* align against */
1675
8ddef552 1676 after = ALIGN_N (os->bfd_section->vma +
97fbbaca
JL
1677 os->bfd_section->_raw_size,
1678 /* The coercion here is important, see ld.h. */
1679 (bfd_vma) os->block_value);
ae475b39
SC
1680
1681 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1682 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1683 os->processed = true;
1684
1685 /* Replace into region ? */
1686 if (os->addr_tree == (etree_type *) NULL
1687 && os->region != (lang_memory_region_type *) NULL)
1688 {
1689 os->region->current = dot;
1690 /* Make sure this isn't silly */
9fce28ed
SC
1691 if (( os->region->current
1692 > os->region->origin + os->region->length)
1693 || ( os->region->origin > os->region->current ))
1694 {
ddddcdf0 1695 einfo ("%X%P: region %s is full (%B section %s)\n",
9fce28ed
SC
1696 os->region->name,
1697 os->bfd_section->owner,
1698 os->bfd_section->name);
1699 /* Reset the region pointer */
1700 os->region->current = 0;
ae475b39 1701
9fce28ed 1702 }
ae475b39
SC
1703
1704 }
1705 }
9d1fe8a4 1706
ae475b39
SC
1707 break;
1708 case lang_constructors_statement_enum:
1709 dot = lang_size_sections (constructor_list.head,
1710 output_section_statement,
1711 &s->wild_statement.children.head,
1712 fill,
1713 dot, relax);
1714 break;
9d1fe8a4 1715
ae475b39
SC
1716 case lang_data_statement_enum:
1717 {
1718 unsigned int size = 0;
1719
1720 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1721 s->data_statement.output_section =
1722 output_section_statement->bfd_section;
1723
1724 switch (s->data_statement.type)
1725 {
c477527c
ILT
1726 case QUAD:
1727 size = QUAD_SIZE;
1728 break;
ae475b39
SC
1729 case LONG:
1730 size = LONG_SIZE;
1731 break;
1732 case SHORT:
1733 size = SHORT_SIZE;
1734 break;
1735 case BYTE:
1736 size = BYTE_SIZE;
1737 break;
1738
1739 }
1740 dot += size;
1741 output_section_statement->bfd_section->_raw_size += size;
1742 }
1743 break;
c611e285 1744
ae475b39 1745 case lang_wild_statement_enum:
c611e285 1746
ae475b39
SC
1747 dot = lang_size_sections (s->wild_statement.children.head,
1748 output_section_statement,
1749 &s->wild_statement.children.head,
c611e285 1750
ae475b39 1751 fill, dot, relax);
c611e285 1752
ae475b39 1753 break;
c611e285 1754
ae475b39 1755 case lang_object_symbols_statement_enum:
c477527c
ILT
1756 link_info.create_object_symbols_section =
1757 output_section_statement->bfd_section;
ae475b39
SC
1758 break;
1759 case lang_output_statement_enum:
1760 case lang_target_statement_enum:
1761 break;
1762 case lang_input_section_enum:
1763 if (relax)
1764 {
c477527c
ILT
1765 lang_input_section_type *is;
1766 asection *i;
1767
c477527c
ILT
1768 is = &(*prev)->input_section;
1769 i = is->section;
1770
755f42fe
ILT
1771 /* FIXME: The interface to bfd_relax_section should be changed
1772 to not require the generic symbols to be read. Changing
1773 this would require changing both b_out_relax_section and
1774 bfd_coff_relax16_section. */
1775 if (is->ifile->asymbols == (asymbol **) NULL)
1776 {
1777 unsigned int symsize;
1778
1779 symsize = get_symtab_upper_bound (i->owner);
1780 is->ifile->asymbols = (asymbol **) ldmalloc (symsize);
1781 is->ifile->symbol_count =
1782 bfd_canonicalize_symtab (i->owner, is->ifile->asymbols);
1783
1784 /* The generic linker code in BFD requires that these
1785 symbols be stored in the outsymbols and symcount
1786 fields. When the bfd_relax_section is interface is
1787 fixed this should also be fixed. */
1788 i->owner->outsymbols = is->ifile->asymbols;
1789 i->owner->symcount = is->ifile->symbol_count;
1790 }
1791
c477527c
ILT
1792 if (bfd_relax_section (i->owner, i, &link_info, is->ifile->asymbols))
1793 had_relax = true;
ae475b39
SC
1794 }
1795 else {
1796 (*prev)->input_section.section->_cooked_size =
1797 (*prev)->input_section.section->_raw_size ;
075d7359 1798
ae475b39
SC
1799 }
1800 dot = size_input_section (prev,
1801 output_section_statement,
1802 output_section_statement->fill,
1803 dot, relax);
1804 break;
1805 case lang_input_statement_enum:
1806 break;
1807 case lang_fill_statement_enum:
1808 s->fill_statement.output_section = output_section_statement->bfd_section;
075d7359 1809
ae475b39
SC
1810 fill = s->fill_statement.fill;
1811 break;
1812 case lang_assignment_statement_enum:
1813 {
1814 bfd_vma newdot = dot;
1815
1816 exp_fold_tree (s->assignment_statement.exp,
1817 output_section_statement,
1818 lang_allocating_phase_enum,
1819 dot,
1820 &newdot);
1821
1822 if (newdot != dot && !relax)
1823 /* We've been moved ! so insert a pad */
1824 {
1825 lang_statement_union_type *new =
1826 (lang_statement_union_type *)
1827 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1828
1829 /* Link into existing chain */
1830 new->header.next = *prev;
1831 *prev = new;
1832 new->header.type = lang_padding_statement_enum;
1833 new->padding_statement.output_section =
1834 output_section_statement->bfd_section;
1835 new->padding_statement.output_offset =
1836 dot - output_section_statement->bfd_section->vma;
1837 new->padding_statement.fill = fill;
1838 new->padding_statement.size = newdot - dot;
1839 output_section_statement->bfd_section->_raw_size +=
1840 new->padding_statement.size;
1841 dot = newdot;
1842 }
1843 }
075d7359 1844
ae475b39
SC
1845 break;
1846 default:
1847 FAIL ();
1848 break;
1849 /* This can only get here when relaxing is turned on */
1850 case lang_padding_statement_enum:
075d7359 1851
ae475b39
SC
1852 case lang_address_statement_enum:
1853 break;
075d7359 1854 }
ae475b39
SC
1855 prev = &s->header.next;
1856 }
c611e285
SC
1857 return dot;
1858}
9d1fe8a4 1859
2fa0b342 1860static bfd_vma
9f629407
ILT
1861lang_do_assignments (s, output_section_statement, fill, dot)
1862 lang_statement_union_type * s;
1863 lang_output_section_statement_type * output_section_statement;
c477527c 1864 fill_type fill;
9f629407 1865 bfd_vma dot;
2fa0b342 1866{
075d7359 1867 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2fa0b342 1868 {
075d7359 1869 switch (s->header.type)
2fa0b342 1870 {
075d7359
SC
1871 case lang_constructors_statement_enum:
1872 dot = lang_do_assignments (constructor_list.head,
1873 output_section_statement,
1874 fill,
1875 dot);
1876 break;
1877
1878 case lang_output_section_statement_enum:
1879 {
1880 lang_output_section_statement_type *os =
2fa0b342 1881 &(s->output_section_statement);
2fa0b342 1882
075d7359
SC
1883 dot = os->bfd_section->vma;
1884 (void) lang_do_assignments (os->children.head, os, os->fill, dot);
1885 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1886 }
1887 break;
1888 case lang_wild_statement_enum:
2fa0b342 1889
075d7359
SC
1890 dot = lang_do_assignments (s->wild_statement.children.head,
1891 output_section_statement,
1892 fill, dot);
2fa0b342 1893
075d7359
SC
1894 break;
1895
1896 case lang_object_symbols_statement_enum:
1897 case lang_output_statement_enum:
1898 case lang_target_statement_enum:
1418c83b 1899#if 0
075d7359 1900 case lang_common_statement_enum:
1418c83b 1901#endif
2fa0b342 1902 break;
075d7359
SC
1903 case lang_data_statement_enum:
1904 {
1905 etree_value_type value;
1906
1907 value = exp_fold_tree (s->data_statement.exp,
1908 abs_output_section,
1909 lang_final_phase_enum, dot, &dot);
1910 s->data_statement.value = value.value;
1911 if (value.valid == false)
ddddcdf0 1912 einfo ("%F%P: invalid data statement\n");
075d7359
SC
1913 }
1914 switch (s->data_statement.type)
1915 {
c477527c
ILT
1916 case QUAD:
1917 dot += QUAD_SIZE;
1918 break;
075d7359
SC
1919 case LONG:
1920 dot += LONG_SIZE;
1921 break;
1922 case SHORT:
1923 dot += SHORT_SIZE;
1924 break;
1925 case BYTE:
1926 dot += BYTE_SIZE;
1927 break;
1928 }
2fa0b342 1929 break;
075d7359
SC
1930 case lang_input_section_enum:
1931 {
1932 asection *in = s->input_section.section;
1933
1934 dot += bfd_get_section_size_before_reloc (in);
1935 }
2fa0b342 1936 break;
2fa0b342 1937
075d7359
SC
1938 case lang_input_statement_enum:
1939 break;
1940 case lang_fill_statement_enum:
1941 fill = s->fill_statement.fill;
1942 break;
1943 case lang_assignment_statement_enum:
1944 {
1945 exp_fold_tree (s->assignment_statement.exp,
1946 output_section_statement,
1947 lang_final_phase_enum,
1948 dot,
1949 &dot);
1950 }
1951
1952 break;
1953 case lang_padding_statement_enum:
1954 dot += s->padding_statement.size;
1955 break;
1956 default:
1957 FAIL ();
1958 break;
1959 case lang_address_statement_enum:
1960 break;
1961 }
2fa0b342
DHW
1962
1963 }
1964 return dot;
1965}
1966
2fa0b342 1967static void
8ddef552 1968lang_finish ()
2fa0b342 1969{
c477527c
ILT
1970 struct bfd_link_hash_entry *h;
1971 boolean warn = link_info.relocateable ? false : true;
075d7359 1972
c477527c
ILT
1973 if (entry_symbol == (char *) NULL)
1974 {
1975 /* No entry has been specified. Look for start, but don't warn
1976 if we don't find it. */
1977 entry_symbol = "start";
1978 warn = false;
1979 }
2fa0b342 1980
c477527c
ILT
1981 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
1982 if (h != (struct bfd_link_hash_entry *) NULL
1983 && h->type == bfd_link_hash_defined)
075d7359 1984 {
c477527c 1985 bfd_vma val;
075d7359 1986
c477527c
ILT
1987 val = (h->u.def.value
1988 + bfd_get_section_vma (output_bfd,
1989 h->u.def.section->output_section)
1990 + h->u.def.section->output_offset);
1991 if (! bfd_set_start_address (output_bfd, val))
1992 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
075d7359 1993 }
c477527c 1994 else
22a78f0d 1995 {
c477527c
ILT
1996 asection *ts;
1997
1998 /* Can't find the entry symbol. Use the first address in the
1999 text section. */
2000 ts = bfd_get_section_by_name (output_bfd, ".text");
2001 if (ts != (asection *) NULL)
2002 {
2003 if (warn)
2004 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2005 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2006 if (! bfd_set_start_address (output_bfd,
2007 bfd_get_section_vma (output_bfd, ts)))
2008 einfo ("%P%F: can't set start address\n");
2009 }
2010 else
2011 {
2012 if (warn)
2013 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2014 entry_symbol);
2015 }
22a78f0d 2016 }
2fa0b342
DHW
2017}
2018
2019/* By now we know the target architecture, and we may have an */
2020/* ldfile_output_machine_name */
2021static void
8ddef552 2022lang_check ()
2fa0b342
DHW
2023{
2024 lang_statement_union_type *file;
075d7359
SC
2025 bfd *input_bfd;
2026 unsigned long input_machine;
2027 enum bfd_architecture input_architecture;
2028 CONST bfd_arch_info_type *compatible;
7bc4a0d7 2029
2fa0b342 2030 for (file = file_chain.head;
075d7359
SC
2031 file != (lang_statement_union_type *) NULL;
2032 file = file->input_statement.next)
2033 {
075d7359 2034 input_bfd = file->input_statement.the_bfd;
7bc4a0d7 2035
075d7359
SC
2036 input_machine = bfd_get_mach (input_bfd);
2037 input_architecture = bfd_get_arch (input_bfd);
7bc4a0d7 2038
7bc4a0d7 2039
075d7359
SC
2040 /* Inspect the architecture and ensure we're linking like with
2041 like */
7bc4a0d7 2042
075d7359
SC
2043 compatible = bfd_arch_get_compatible (input_bfd,
2044 output_bfd);
22a78f0d 2045
075d7359
SC
2046 if (compatible)
2047 {
2048 ldfile_output_machine = compatible->mach;
2049 ldfile_output_architecture = compatible->arch;
2050 }
2051 else
2052 {
7bc4a0d7 2053
c477527c 2054 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
075d7359
SC
2055 bfd_printable_name (input_bfd), input_bfd,
2056 bfd_printable_name (output_bfd));
7bc4a0d7 2057
ddddcdf0
ILT
2058 if (! bfd_set_arch_mach (output_bfd,
2059 input_architecture,
2060 input_machine))
2061 einfo ("%P%F:%s: can't set architecture: %E\n",
2062 bfd_get_filename (output_bfd));
075d7359 2063 }
2fa0b342 2064
075d7359
SC
2065 }
2066}
2fa0b342 2067
c477527c
ILT
2068/* Look through all the global common symbols and attach them to the
2069 correct section. The -sort-common command line switch may be used
2070 to roughly sort the entries by size. */
2fa0b342
DHW
2071
2072static void
8ddef552 2073lang_common ()
2fa0b342 2074{
c477527c
ILT
2075 if (link_info.relocateable
2076 && ! command_line.force_common_definition)
2077 return;
075d7359 2078
c477527c
ILT
2079 if (! config.sort_common)
2080 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2081 else
075d7359 2082 {
c477527c 2083 unsigned int power;
1418c83b 2084
c477527c
ILT
2085 for (power = 1; power <= 16; power <<= 1)
2086 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2087 (PTR) &power);
2088 }
2089}
29f33467 2090
c477527c 2091/* Place one common symbol in the correct section. */
075d7359 2092
c477527c
ILT
2093static boolean
2094lang_one_common (h, info)
2095 struct bfd_link_hash_entry *h;
2096 PTR info;
2097{
2098 unsigned int power_of_two;
2099 bfd_vma size;
2100 size_t align;
2101 asection *section;
075d7359 2102
c477527c
ILT
2103 if (h->type != bfd_link_hash_common)
2104 return true;
075d7359 2105
c477527c
ILT
2106 size = h->u.c.size;
2107 switch (size)
2108 {
2109 case 0:
2110 case 1:
2111 power_of_two = 0;
2112 align = 1;
2113 break;
2114 case 2:
2115 power_of_two = 1;
2116 align = 2;
2117 break;
2118 case 3:
2119 case 4:
2120 power_of_two = 2;
2121 align = 4;
2122 break;
2123 case 5:
2124 case 6:
2125 case 7:
2126 case 8:
2127 power_of_two = 3;
2128 align = 8;
2129 break;
2130 default:
2131 power_of_two = 4;
2132 align = 16;
2133 break;
2134 }
2135
2136 if (config.sort_common && align != *(unsigned int *) info)
2137 return true;
075d7359 2138
c477527c 2139 section = h->u.c.section;
075d7359 2140
c477527c
ILT
2141 /* Increase the size of the section. */
2142 section->_raw_size = ALIGN_N (section->_raw_size, align);
075d7359 2143
c477527c
ILT
2144 /* Adjust the alignment if necessary. */
2145 if (power_of_two > section->alignment_power)
2146 section->alignment_power = power_of_two;
075d7359 2147
c477527c
ILT
2148 /* Change the symbol from common to defined. */
2149 h->type = bfd_link_hash_defined;
2150 h->u.def.section = section;
2151 h->u.def.value = section->_raw_size;
097879bc 2152
c477527c
ILT
2153 /* Increase the size of the section. */
2154 section->_raw_size += size;
1418c83b 2155
c477527c
ILT
2156 if (write_map && config.map_file != NULL)
2157 fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2158 h->root.string, (unsigned long) size,
2159 (unsigned long) h->u.def.value, section->owner->filename);
1418c83b 2160
c477527c 2161 return true;
2fa0b342
DHW
2162}
2163
2164/*
075d7359 2165run through the input files and ensure that every input
2fa0b342
DHW
2166section has somewhere to go. If one is found without
2167a destination then create an input request and place it
2168into the statement tree.
2169*/
2170
075d7359 2171static void
8ddef552 2172lang_place_orphans ()
2fa0b342
DHW
2173{
2174 lang_input_statement_type *file;
1418c83b 2175
075d7359
SC
2176 for (file = (lang_input_statement_type *) file_chain.head;
2177 file != (lang_input_statement_type *) NULL;
2178 file = (lang_input_statement_type *) file->next)
2179 {
2180 asection *s;
2181
2182 for (s = file->the_bfd->sections;
2183 s != (asection *) NULL;
2184 s = s->next)
2185 {
2186 if (s->output_section == (asection *) NULL)
2187 {
2188 /* This section of the file is not attatched, root
2189 around for a sensible place for it to go */
2190
2191 if (file->common_section == s)
2192 {
2193 /* This is a lonely common section which must
2194 have come from an archive. We attatch to the
2195 section with the wildcard */
c477527c
ILT
2196 if (! link_info.relocateable
2197 && ! command_line.force_common_definition)
075d7359
SC
2198 {
2199 if (default_common_section ==
2200 (lang_output_section_statement_type *) NULL)
2201 {
a4aeaacf 2202 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
075d7359
SC
2203
2204 default_common_section =
2205 lang_output_section_statement_lookup (".bss");
2206
2207 }
2208 wild_doit (&default_common_section->children, s,
2209 default_common_section, file);
2210 }
2211 }
2212 else
2213 {
2214 lang_output_section_statement_type *os =
2215 lang_output_section_statement_lookup (s->name);
2fa0b342 2216
075d7359
SC
2217 wild_doit (&os->children, s, os, file);
2218 }
2219 }
2fa0b342 2220 }
2fa0b342 2221 }
2fa0b342
DHW
2222}
2223
2224
2fa0b342 2225void
8ddef552
DM
2226lang_set_flags (ptr, flags)
2227 int *ptr;
2228 CONST char *flags;
2fa0b342 2229{
075d7359
SC
2230 boolean state = false;
2231
2232 *ptr = 0;
2fa0b342 2233 while (*flags)
075d7359
SC
2234 {
2235 if (*flags == '!')
2236 {
2237 state = false;
2238 flags++;
2239 }
2240 else
2241 state = true;
2242 switch (*flags)
2243 {
2244 case 'R':
2245 /* ptr->flag_read = state; */
2246 break;
2247 case 'W':
2248 /* ptr->flag_write = state; */
2249 break;
2250 case 'X':
2251 /* ptr->flag_executable= state;*/
2252 break;
2253 case 'L':
2254 case 'I':
2255 /* ptr->flag_loadable= state;*/
2256 break;
2257 default:
ddddcdf0 2258 einfo ("%P%F: invalid syntax in flags\n");
075d7359
SC
2259 break;
2260 }
dc4726c2
SC
2261 flags++;
2262 }
2fa0b342
DHW
2263}
2264
2265
2266
2267void
8ddef552
DM
2268lang_for_each_file (func)
2269 void (*func) PARAMS ((lang_input_statement_type *));
2fa0b342
DHW
2270{
2271 lang_input_statement_type *f;
075d7359
SC
2272
2273 for (f = (lang_input_statement_type *) file_chain.head;
2274 f != (lang_input_statement_type *) NULL;
2275 f = (lang_input_statement_type *) f->next)
2276 {
2277 func (f);
2278 }
2fa0b342
DHW
2279}
2280
9f629407
ILT
2281#if 0
2282
2283/* Not used. */
2fa0b342
DHW
2284
2285void
8ddef552
DM
2286lang_for_each_input_section (func)
2287 void (*func) PARAMS ((bfd * ab, asection * as));
2fa0b342
DHW
2288{
2289 lang_input_statement_type *f;
075d7359
SC
2290
2291 for (f = (lang_input_statement_type *) file_chain.head;
2292 f != (lang_input_statement_type *) NULL;
2293 f = (lang_input_statement_type *) f->next)
2fa0b342
DHW
2294 {
2295 asection *s;
075d7359 2296
2fa0b342 2297 for (s = f->the_bfd->sections;
075d7359
SC
2298 s != (asection *) NULL;
2299 s = s->next)
2300 {
2301 func (f->the_bfd, s);
2302 }
2fa0b342
DHW
2303 }
2304}
2305
9f629407 2306#endif
2fa0b342 2307
075d7359 2308void
8ddef552
DM
2309ldlang_add_file (entry)
2310 lang_input_statement_type * entry;
2fa0b342 2311{
5e6cd559
ILT
2312 bfd **pp;
2313
075d7359
SC
2314 lang_statement_append (&file_chain,
2315 (lang_statement_union_type *) entry,
2316 &entry->next);
c477527c
ILT
2317
2318 /* The BFD linker needs to have a list of all input BFDs involved in
2319 a link. */
2320 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2321 ASSERT (entry->the_bfd != output_bfd);
5e6cd559
ILT
2322 for (pp = &link_info.input_bfds;
2323 *pp != (bfd *) NULL;
2324 pp = &(*pp)->link_next)
2325 ;
2326 *pp = entry->the_bfd;
c477527c 2327 entry->the_bfd->usrdata = (PTR) entry;
2fa0b342
DHW
2328}
2329
2fa0b342 2330void
2c6635a4 2331lang_add_output (name, from_script)
8ddef552 2332 CONST char *name;
2c6635a4 2333 int from_script;
2fa0b342 2334{
2c6635a4
ILT
2335 /* Make -o on command line override OUTPUT in script. */
2336 if (had_output_filename == false || !from_script)
2337 {
2338 output_filename = name;
2339 had_output_filename = true;
2340 }
2fa0b342
DHW
2341}
2342
2343
2344static lang_output_section_statement_type *current_section;
2345
e20873a7 2346static int topower(x)
9f629407 2347 int x;
e20873a7
JG
2348{
2349 unsigned int i = 1;
2350 int l;
2351 if (x < 0) return -1;
2352 for (l = 0; l < 32; l++)
2353 {
2354 if (i >= x) return l;
2355 i<<=1;
2356 }
29f33467 2357 return 0;
e20873a7 2358}
2fa0b342 2359void
8ddef552
DM
2360lang_enter_output_section_statement (output_section_statement_name,
2361 address_exp, flags, block_value,
9f629407 2362 align, subalign, ebase)
fcf276c4 2363 const char *output_section_statement_name;
8ddef552
DM
2364 etree_type * address_exp;
2365 int flags;
2366 bfd_vma block_value;
2367 etree_type *align;
2368 etree_type *subalign;
9f629407 2369 etree_type *ebase;
2fa0b342
DHW
2370{
2371 lang_output_section_statement_type *os;
075d7359
SC
2372
2373 current_section =
e20873a7 2374 os =
075d7359
SC
2375 lang_output_section_statement_lookup (output_section_statement_name);
2376
2377
2fa0b342 2378
2fa0b342
DHW
2379 /* Add this statement to tree */
2380 /* add_statement(lang_output_section_statement_enum,
2381 output_section_statement);*/
2382 /* Make next things chain into subchain of this */
2383
2384 if (os->addr_tree ==
075d7359 2385 (etree_type *) NULL)
e20873a7
JG
2386 {
2387 os->addr_tree =
2388 address_exp;
2389 }
7bc4a0d7 2390 os->flags = flags;
ae475b39
SC
2391 if (flags & SEC_NEVER_LOAD)
2392 os->loadable = 0;
2393 else
2394 os->loadable = 1;
29f33467 2395 os->block_value = block_value ? block_value : 1;
075d7359 2396 stat_ptr = &os->children;
2fa0b342 2397
e20873a7
JG
2398 os->subsection_alignment = topower(
2399 exp_get_value_int(subalign, -1,
2400 "subsection alignment",
2401 0));
2402 os->section_alignment = topower(
2403 exp_get_value_int(align, -1,
2404 "section alignment", 0));
9fce28ed 2405
9f629407 2406 os->load_base = ebase;
2fa0b342
DHW
2407}
2408
9fce28ed 2409
075d7359 2410void
8ddef552 2411lang_final ()
2fa0b342 2412{
2c6635a4
ILT
2413 lang_output_statement_type *new =
2414 new_stat (lang_output_statement, stat_ptr);
2fa0b342 2415
2c6635a4 2416 new->name = output_filename;
075d7359 2417}
2fa0b342 2418
c611e285
SC
2419/* Reset the current counters in the regions */
2420static void
8ddef552 2421reset_memory_regions ()
c611e285 2422{
075d7359
SC
2423 lang_memory_region_type *p = lang_memory_region_list;
2424
c611e285 2425 for (p = lang_memory_region_list;
075d7359
SC
2426 p != (lang_memory_region_type *) NULL;
2427 p = p->next)
2428 {
8ddef552 2429 p->old_length = (bfd_size_type) (p->current - p->origin);
075d7359
SC
2430 p->current = p->origin;
2431 }
c611e285 2432}
2fa0b342 2433
2fa0b342 2434void
8ddef552 2435lang_process ()
075d7359 2436{
075d7359 2437 lang_reasonable_defaults ();
1418c83b
SC
2438 current_target = default_target;
2439
075d7359 2440 lang_for_each_statement (ldlang_open_output); /* Open the output file */
1418c83b
SC
2441 /* For each output section statement, create a section in the output
2442 file */
075d7359 2443 lang_create_output_section_statements ();
1418c83b 2444
97fbbaca
JL
2445 ldemul_create_output_section_statements ();
2446
1418c83b 2447 /* Add to the hash table all undefineds on the command line */
075d7359 2448 lang_place_undefineds ();
1418c83b
SC
2449
2450 /* Create a bfd for each input file */
2451 current_target = default_target;
075d7359 2452 lang_for_each_statement (open_input_bfds);
1418c83b 2453
c477527c
ILT
2454 /* Build all sets based on the information gathered from the input
2455 files. */
2456 ldctor_build_sets ();
2457
1418c83b 2458 /* Run through the contours of the script and attatch input sections
075d7359 2459 to the correct output sections
1418c83b 2460 */
075d7359
SC
2461 map_input_to_output_sections (statement_list.head, (char *) NULL,
2462 (lang_output_section_statement_type *) NULL);
1418c83b 2463
81016051 2464
1418c83b 2465 /* Find any sections not attatched explicitly and handle them */
075d7359 2466 lang_place_orphans ();
1418c83b
SC
2467
2468 /* Size up the common data */
075d7359 2469 lang_common ();
1418c83b 2470
075d7359 2471 ldemul_before_allocation ();
1418c83b 2472
c611e285 2473
ae475b39
SC
2474#if 0
2475 had_relax = true;
2476 while (had_relax)
2477 {
2478
2479 had_relax = false;
c611e285 2480
ae475b39
SC
2481 lang_size_sections (statement_list.head,
2482 (lang_output_section_statement_type *) NULL,
2483 &(statement_list.head), 0, (bfd_vma) 0, true);
2484 /* FIXME. Until the code in relax is fixed so that it only reads in
2485 stuff once, we cant iterate since there is no way for the linker to
2486 know what has been patched and what hasn't */
2487 break;
2488
2489 }
2490#endif
c611e285 2491
c611e285 2492 /* Now run around and relax if we can */
075d7359 2493 if (command_line.relax)
c611e285 2494 {
97fbbaca
JL
2495 /* First time round is a trial run to get the 'worst case'
2496 addresses of the objects if there was no relaxing. */
ae475b39
SC
2497 lang_size_sections (statement_list.head,
2498 (lang_output_section_statement_type *) NULL,
2499 &(statement_list.head), 0, (bfd_vma) 0, false);
c611e285 2500
075d7359 2501
97fbbaca 2502 reset_memory_regions ();
075d7359 2503
97fbbaca
JL
2504 /* Do all the assignments, now that we know the final resting
2505 places of all the symbols. */
ae475b39 2506
97fbbaca
JL
2507 lang_do_assignments (statement_list.head,
2508 abs_output_section,
c477527c 2509 (fill_type) 0, (bfd_vma) 0);
ae475b39
SC
2510
2511 /* Perform another relax pass - this time we know where the
97fbbaca 2512 globals are, so can make better guess. */
ae475b39
SC
2513 lang_size_sections (statement_list.head,
2514 (lang_output_section_statement_type *) NULL,
2515 &(statement_list.head), 0, (bfd_vma) 0, true);
ae475b39 2516 }
ae475b39
SC
2517 else
2518 {
97fbbaca 2519 /* Size up the sections. */
ae475b39
SC
2520 lang_size_sections (statement_list.head,
2521 abs_output_section,
2522 &(statement_list.head), 0, (bfd_vma) 0, false);
075d7359 2523 }
c611e285 2524
1418c83b 2525 /* See if anything special should be done now we know how big
97fbbaca 2526 everything is. */
075d7359 2527 ldemul_after_allocation ();
1418c83b
SC
2528
2529 /* Do all the assignments, now that we know the final restingplaces
2530 of all the symbols */
2531
075d7359
SC
2532 lang_do_assignments (statement_list.head,
2533 abs_output_section,
c477527c 2534 (fill_type) 0, (bfd_vma) 0);
ffc50032 2535
1418c83b
SC
2536 /* Make sure that we're not mixing architectures */
2537
075d7359 2538 lang_check ();
1418c83b 2539
1418c83b 2540 /* Final stuffs */
97fbbaca
JL
2541
2542 ldemul_finish ();
2c6635a4
ILT
2543
2544#if 0
2545 /* DO NOT REENABLE THIS CALL. IF THIS CALL IS MADE, THE SUN4 LINKER
2546 CAN NOT BOOTSTRAP!! No, I don't know why, but don't change it
2547 unless you fix it. */
97fbbaca
JL
2548 /* Size up the sections. */
2549 lang_size_sections (statement_list.head,
2550 abs_output_section,
2551 &(statement_list.head), 0, (bfd_vma) 0, false);
2c6635a4
ILT
2552#endif
2553
075d7359 2554 lang_finish ();
2fa0b342
DHW
2555}
2556
2fa0b342 2557/* EXPORTED TO YACC */
1418c83b 2558
2fa0b342 2559void
8ddef552
DM
2560lang_add_wild (section_name, filename)
2561 CONST char *CONST section_name;
2562 CONST char *CONST filename;
1418c83b 2563{
075d7359
SC
2564 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2565 stat_ptr);
1418c83b 2566
075d7359
SC
2567 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2568 {
2569 placed_commons = true;
2570 }
2571 if (filename != (char *) NULL)
2572 {
2573 lang_has_input_file = true;
2574 }
1418c83b
SC
2575 new->section_name = section_name;
2576 new->filename = filename;
075d7359 2577 lang_list_init (&new->children);
1418c83b 2578}
075d7359 2579
1418c83b 2580void
8ddef552
DM
2581lang_section_start (name, address)
2582 CONST char *name;
2583 etree_type * address;
2fa0b342 2584{
075d7359
SC
2585 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2586
2fa0b342
DHW
2587 ad->section_name = name;
2588 ad->address = address;
2589}
1418c83b 2590
075d7359 2591void
8ddef552
DM
2592lang_add_entry (name)
2593 CONST char *name;
2fa0b342
DHW
2594{
2595 entry_symbol = name;
2596}
2597
2598void
8ddef552
DM
2599lang_add_target (name)
2600 CONST char *name;
2fa0b342 2601{
075d7359
SC
2602 lang_target_statement_type *new = new_stat (lang_target_statement,
2603 stat_ptr);
2604
2fa0b342
DHW
2605 new->target = name;
2606
2607}
2fa0b342 2608
2fa0b342 2609void
8ddef552
DM
2610lang_add_map (name)
2611 CONST char *name;
2fa0b342 2612{
075d7359
SC
2613 while (*name)
2614 {
2615 switch (*name)
2616 {
2617 case 'F':
2618 map_option_f = true;
2619 break;
2620 }
2621 name++;
2fa0b342 2622 }
2fa0b342
DHW
2623}
2624
075d7359 2625void
8ddef552
DM
2626lang_add_fill (exp)
2627 int exp;
2fa0b342 2628{
075d7359
SC
2629 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2630 stat_ptr);
2631
2fa0b342
DHW
2632 new->fill = exp;
2633}
2634
075d7359 2635void
8ddef552
DM
2636lang_add_data (type, exp)
2637 int type;
2638 union etree_union *exp;
2fa0b342
DHW
2639{
2640
075d7359 2641 lang_data_statement_type *new = new_stat (lang_data_statement,
2fa0b342 2642 stat_ptr);
075d7359
SC
2643
2644 new->exp = exp;
2645 new->type = type;
2fa0b342
DHW
2646
2647}
075d7359 2648
2fa0b342 2649void
8ddef552
DM
2650lang_add_assignment (exp)
2651 etree_type * exp;
2fa0b342 2652{
075d7359
SC
2653 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2654 stat_ptr);
2655
2fa0b342
DHW
2656 new->exp = exp;
2657}
2658
2659void
8ddef552
DM
2660lang_add_attribute (attribute)
2661 enum statement_enum attribute;
2fa0b342 2662{
075d7359 2663 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2fa0b342
DHW
2664}
2665
075d7359 2666void
8ddef552
DM
2667lang_startup (name)
2668 CONST char *name;
2fa0b342 2669{
075d7359
SC
2670 if (startup_file != (char *) NULL)
2671 {
ddddcdf0 2672 einfo ("%P%Fmultiple STARTUP files\n");
075d7359 2673 }
2fa0b342
DHW
2674 first_file->filename = name;
2675 first_file->local_sym_name = name;
2676
075d7359 2677 startup_file = name;
2fa0b342 2678}
075d7359
SC
2679
2680void
8ddef552
DM
2681lang_float (maybe)
2682 boolean maybe;
2fa0b342
DHW
2683{
2684 lang_float_flag = maybe;
2685}
2686
075d7359 2687void
8ddef552
DM
2688lang_leave_output_section_statement (fill, memspec)
2689 bfd_vma fill;
2690 CONST char *memspec;
2fa0b342
DHW
2691{
2692 current_section->fill = fill;
075d7359 2693 current_section->region = lang_memory_region_lookup (memspec);
2fa0b342 2694 stat_ptr = &statement_list;
81016051
SC
2695
2696 /* We remember if we are closing a .data section, since we use it to
2697 store constructors in */
075d7359
SC
2698 if (strcmp (current_section->name, ".data") == 0)
2699 {
2700 end_of_data_section_statement_list = statement_list;
81016051 2701
075d7359 2702 }
2fa0b342 2703}
075d7359 2704
9f32f7c2
SC
2705/*
2706 Create an absolute symbol with the given name with the value of the
2707 address of first byte of the section named.
2fa0b342 2708
9f32f7c2
SC
2709 If the symbol already exists, then do nothing.
2710*/
8cb5eb31 2711void
c477527c
ILT
2712lang_abs_symbol_at_beginning_of (secname, name)
2713 const char *secname;
2714 const char *name;
075d7359 2715{
c477527c
ILT
2716 struct bfd_link_hash_entry *h;
2717
2718 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2719 if (h == (struct bfd_link_hash_entry *) NULL)
2720 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2721
2722 if (h->type == bfd_link_hash_new
2723 || h->type == bfd_link_hash_undefined)
075d7359 2724 {
c477527c 2725 asection *sec;
075d7359 2726
c477527c
ILT
2727 h->type = bfd_link_hash_defined;
2728
2729 sec = bfd_get_section_by_name (output_bfd, secname);
2730 if (sec == (asection *) NULL)
2731 h->u.def.value = 0;
075d7359 2732 else
c477527c
ILT
2733 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
2734
2735 h->u.def.section = &bfd_abs_section;
9f32f7c2 2736 }
8cb5eb31
SC
2737}
2738
9f32f7c2
SC
2739/*
2740 Create an absolute symbol with the given name with the value of the
2741 address of the first byte after the end of the section named.
2742
2743 If the symbol already exists, then do nothing.
2744*/
2fa0b342 2745void
c477527c
ILT
2746lang_abs_symbol_at_end_of (secname, name)
2747 const char *secname;
2748 const char *name;
075d7359 2749{
c477527c
ILT
2750 struct bfd_link_hash_entry *h;
2751
2752 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2753 if (h == (struct bfd_link_hash_entry *) NULL)
2754 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2755
2756 if (h->type == bfd_link_hash_new
2757 || h->type == bfd_link_hash_undefined)
075d7359 2758 {
c477527c 2759 asection *sec;
075d7359 2760
c477527c 2761 h->type = bfd_link_hash_defined;
075d7359 2762
c477527c
ILT
2763 sec = bfd_get_section_by_name (output_bfd, secname);
2764 if (sec == (asection *) NULL)
2765 h->u.def.value = 0;
075d7359 2766 else
c477527c
ILT
2767 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
2768 + bfd_section_size (output_bfd, sec));
2769
2770 h->u.def.section = &bfd_abs_section;
9f32f7c2 2771 }
2fa0b342
DHW
2772}
2773
075d7359 2774void
8ddef552
DM
2775lang_statement_append (list, element, field)
2776 lang_statement_list_type * list;
2777 lang_statement_union_type * element;
2778 lang_statement_union_type ** field;
2fa0b342
DHW
2779{
2780 *(list->tail) = element;
2781 list->tail = field;
2782}
2783
173a0c3d 2784/* Set the output format type. -oformat overrides scripts. */
097879bc 2785void
173a0c3d 2786lang_add_output_format (format, from_script)
8ddef552 2787 CONST char *format;
173a0c3d 2788 int from_script;
097879bc 2789{
2c6635a4 2790 if (output_target == NULL || !from_script)
173a0c3d 2791 output_target = format;
097879bc 2792}
This page took 0.253747 seconds and 4 git commands to generate.