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