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