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