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