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