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