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