1
1
/* Linker command language support.
2
2
Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4
4
Free Software Foundation, Inc.
6
6
This file is part of GLD, the Gnu Linker.
623
624
lookup->next = NULL;
624
625
lookup->bfd_section = NULL;
625
lookup->processed = FALSE;
626
lookup->processed = 0;
626
627
lookup->sectype = normal_section;
627
628
lookup->addr_tree = NULL;
628
629
lang_list_init (&lookup->children);
1320
1321
search != NULL;
1321
1322
search = (lang_input_statement_type *) search->next_real_file)
1323
if (search->filename == NULL && name == NULL)
1324
/* Use the local_sym_name as the name of the file that has
1325
already been loaded as filename might have been transformed
1326
via the search directory lookup mechanism. */
1327
const char * filename = search->local_sym_name;
1329
if (filename == NULL && name == NULL)
1325
if (search->filename != NULL
1331
if (filename != NULL
1326
1332
&& name != NULL
1327
&& strcmp (search->filename, name) == 0)
1333
&& strcmp (filename, name) == 0)
1331
1337
if (search == NULL)
1332
search = new_afile (name, lang_input_file_is_file_enum, default_target,
1338
search = new_afile (name, lang_input_file_is_search_file_enum, default_target,
1335
1341
/* If we have already added this file, or this file is not real
1803
/* Convert between addresses in bytes and sizes in octets.
1804
For currently supported targets, octets_per_byte is always a power
1805
of two, so we can use shifts. */
1806
#define TO_ADDR(X) ((X) >> opb_shift)
1807
#define TO_SIZE(X) ((X) << opb_shift)
1809
/* Support the above. */
1810
static unsigned int opb_shift = 0;
1815
unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1816
ldfile_output_machine);
1819
while ((x & 1) == 0)
1797
1827
/* Open all the input files. */
1813
1843
/* Maybe we should load the file's symbols. */
1814
1844
if (s->wild_statement.filename
1815
1845
&& ! wildcardp (s->wild_statement.filename))
1816
(void) lookup_name (s->wild_statement.filename);
1846
lookup_name (s->wild_statement.filename);
1817
1847
open_input_bfds (s->wild_statement.children.head, force);
1819
1849
case lang_group_statement_enum:
2066
2096
output_section_statement);
2098
case lang_data_statement_enum:
2099
/* Make sure that any sections mentioned in the expression
2101
exp_init_os (s->data_statement.exp);
2068
2103
case lang_fill_statement_enum:
2069
2104
case lang_input_section_enum:
2070
2105
case lang_object_symbols_statement_enum:
2071
case lang_data_statement_enum:
2072
2106
case lang_reloc_statement_enum:
2073
2107
case lang_padding_statement_enum:
2074
2108
case lang_input_statement_enum:
2294
2328
minfo ("0x%V %W %B\n",
2295
i->output_section->vma + i->output_offset, size / opb,
2329
i->output_section->vma + i->output_offset, TO_ADDR (size),
2298
2332
if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2715
2744
if (alignment_needed != 0)
2717
insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2746
insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2718
2747
dot += alignment_needed;
2725
2754
/* Mark how big the output section must be to contain this now. */
2726
2755
if (i->_cooked_size != 0)
2727
dot += i->_cooked_size / opb;
2756
dot += TO_ADDR (i->_cooked_size);
2729
dot += i->_raw_size / opb;
2730
o->_raw_size = (dot - o->vma) * opb;
2758
dot += TO_ADDR (i->_raw_size);
2759
o->_raw_size = TO_SIZE (dot - o->vma);
2740
2769
#define IGNORE_SECTION(bfd, s) \
2741
(((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2742
!= (SEC_ALLOC | SEC_LOAD)) \
2770
(((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2771
? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2773
: ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2743
2775
|| bfd_section_size (bfd, s) == 0)
2745
2777
/* Check to see if any allocated sections overlap with other allocated
2778
2809
/* We must check the sections' LMA addresses not their
2779
2810
VMA addresses because overlay sections can have
2780
2811
overlapping VMAs but they must have distinct LMAs. */
2781
s_start = bfd_section_lma (output_bfd, s);
2812
s_start = bfd_section_lma (output_bfd, s);
2782
2813
os_start = bfd_section_lma (output_bfd, os);
2783
s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2784
os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2814
s_end = s_start + TO_ADDR (bfd_section_size (output_bfd, s)) - 1;
2815
os_end = os_start + TO_ADDR (bfd_section_size (output_bfd, os)) - 1;
2786
2817
/* Look for an overlap. */
2787
2818
if ((s_end < os_start) || (s_start > os_end))
2846
2877
bfd_boolean *relax,
2847
2878
bfd_boolean check_regions)
2849
unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2850
ldfile_output_machine);
2852
2880
/* Size up the sections from their constituent parts. */
2853
2881
for (; s != NULL; s = s->header.next)
2910
2938
region, and some non default memory regions were
2911
2939
defined, issue an error message. */
2912
2940
if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2913
&& (bfd_get_section_flags (output_bfd, os->bfd_section)
2914
& SEC_NEVER_LOAD) == 0
2915
2941
&& ! link_info.relocatable
2916
2942
&& check_regions
2917
2943
&& strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
2959
2985
etree_value_type r;
2961
2988
r = exp_fold_tree (os->addr_tree,
2962
2989
abs_output_section,
2963
2990
lang_allocating_phase_enum,
2965
2994
if (!r.valid_p)
2966
einfo (_("%F%S: non constant address expression for section %s\n"),
2995
einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
2969
2998
dot = r.value + r.section->bfd_section->vma;
2986
3015
/* Put the section within the requested block size, or
2987
3016
align at the block boundary. */
2988
after = align_n (os->bfd_section->vma
2989
+ os->bfd_section->_raw_size / opb,
2990
(bfd_vma) os->block_value);
3017
after = ((os->bfd_section->vma
3018
+ TO_ADDR (os->bfd_section->_raw_size)
3019
+ os->block_value - 1)
3020
& - (bfd_vma) os->block_value);
2992
3022
if (bfd_is_abs_section (os->bfd_section))
2993
3023
ASSERT (after == os->bfd_section->vma);
2994
else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
2995
&& (os->bfd_section->flags & SEC_THREAD_LOCAL)
2996
&& ! link_info.relocatable)
2997
os->bfd_section->_raw_size = 0;
2999
os->bfd_section->_raw_size =
3000
(after - os->bfd_section->vma) * opb;
3002
dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3003
os->processed = TRUE;
3025
os->bfd_section->_raw_size
3026
= TO_SIZE (after - os->bfd_section->vma);
3028
dot = os->bfd_section->vma;
3029
/* .tbss sections effectively have zero size. */
3030
if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3031
|| (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3032
|| link_info.relocatable)
3033
dot += TO_ADDR (os->bfd_section->_raw_size);
3005
3037
if (os->update_dot_tree != 0)
3006
3038
exp_fold_tree (os->update_dot_tree, abs_output_section,
3037
3069
/* Set load_base, which will be handled later. */
3038
3070
os->load_base = exp_intop (os->lma_region->current);
3039
3071
os->lma_region->current +=
3040
os->bfd_section->_raw_size / opb;
3072
TO_ADDR (os->bfd_section->_raw_size);
3041
3073
if (check_regions)
3042
3074
os_region_check (os, os->lma_region, NULL,
3043
3075
os->bfd_section->lma);
3062
3094
s->data_statement.output_section =
3063
3095
output_section_statement->bfd_section;
3097
/* We might refer to provided symbols in the expression, and
3098
need to mark them as needed. */
3099
exp_fold_tree (s->data_statement.exp, abs_output_section,
3100
lang_allocating_phase_enum, dot, &dot);
3065
3102
switch (s->data_statement.type)
3080
3117
size = BYTE_SIZE;
3120
if (size < TO_SIZE ((unsigned) 1))
3121
size = TO_SIZE ((unsigned) 1);
3122
dot += TO_ADDR (size);
3086
3123
output_section_statement->bfd_section->_raw_size += size;
3087
3124
/* The output section gets contents, and then we inspect for
3088
3125
any flags set in the input script which override any ALLOC. */
3179
3216
/* Insert a pad after this statement. We can't
3180
3217
put the pad before when relaxing, in case the
3181
3218
assignment references dot. */
3182
insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3219
insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3183
3220
output_section_statement->bfd_section, dot);
3185
3222
/* Don't neuter the pad below when relaxing. */
3186
3223
s = s->header.next;
3226
/* If dot is advanced, this implies that the section should
3227
have space allocated to it, unless the user has explicitly
3228
stated that the section should never be loaded. */
3229
if (!(output_section_statement->flags & (SEC_NEVER_LOAD | SEC_ALLOC)))
3230
output_section_statement->bfd_section->flags |= SEC_ALLOC;
3261
3304
&& first + last <= exp_data_seg.pagesize)
3263
3306
exp_data_seg.phase = exp_dataseg_adjust;
3307
lang_statement_iteration++;
3264
3308
result = lang_size_sections_1 (s, output_section_statement, prev,
3265
3309
fill, dot, relax, check_regions);
3308
3349
if (os->bfd_section != NULL)
3310
3351
dot = os->bfd_section->vma;
3311
(void) lang_do_assignments_1 (os->children.head, os,
3313
dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3352
lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3353
dot = (os->bfd_section->vma
3354
+ TO_ADDR (os->bfd_section->_raw_size));
3316
3357
if (os->load_base)
3348
3389
value = exp_fold_tree (s->data_statement.exp,
3349
3390
abs_output_section,
3350
3391
lang_final_phase_enum, dot, &dot);
3351
s->data_statement.value = value.value;
3352
3392
if (!value.valid_p)
3353
3393
einfo (_("%F%P: invalid data statement\n"));
3394
s->data_statement.value
3395
= value.value + value.section->bfd_section->vma;
3356
3398
unsigned int size;
3397
3439
asection *in = s->input_section.section;
3399
3441
if (in->_cooked_size != 0)
3400
dot += in->_cooked_size / opb;
3442
dot += TO_ADDR (in->_cooked_size);
3402
dot += in->_raw_size / opb;
3444
dot += TO_ADDR (in->_raw_size);
3444
3486
lang_do_assignments (lang_statement_union_type *s,
3445
lang_output_section_statement_type
3446
*output_section_statement,
3487
lang_output_section_statement_type *output_section_statement,
3447
3488
fill_type *fill,
3489
3530
h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3490
3531
if (h != NULL && h->type == bfd_link_hash_undefined)
3494
opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3495
ldfile_output_machine);
3496
3533
h->type = bfd_link_hash_defined;
3497
3534
if (s->_cooked_size != 0)
3498
h->u.def.value = s->_cooked_size / opb;
3535
h->u.def.value = TO_ADDR (s->_cooked_size);
3500
h->u.def.value = s->_raw_size / opb;
3537
h->u.def.value = TO_ADDR (s->_raw_size);
3501
3538
h->u.def.section = bfd_abs_section_ptr;
3650
3687
if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3652
3689
if (command_line.warn_mismatch)
3653
einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3690
einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3656
3693
if (! command_line.warn_mismatch)
3707
3742
section = h->u.c.p->section;
3709
/* Increase the size of the section. */
3710
section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3711
(bfd_vma) 1 << power_of_two) * opb;
3744
/* Increase the size of the section to align the common sym. */
3745
section->_cooked_size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3746
section->_cooked_size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3713
3748
/* Adjust the alignment if necessary. */
3714
3749
if (power_of_two > section->alignment_power)
3911
lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
3913
LANG_FOR_EACH_INPUT_STATEMENT (f)
3917
for (s = f->the_bfd->sections; s != NULL; s = s->next)
3918
func (f->the_bfd, s);
3925
3942
ldlang_add_file (lang_input_statement_type *entry)
4016
4032
os->flags = SEC_NO_FLAGS;
4018
4034
os->flags = SEC_NEVER_LOAD;
4019
os->block_value = block_value ? block_value : 1;
4035
os->block_value = 1;
4020
4036
stat_ptr = &os->children;
4022
4038
os->subsection_alignment =
4248
4263
lang_do_assignments (statement_list.head, abs_output_section,
4266
/* We must do this after lang_do_assignments, because it uses
4268
lang_reset_memory_regions ();
4251
4270
/* Perform another relax pass - this time we know where the
4252
4271
globals are, so can make a better guess. */
4253
4272
lang_size_sections (statement_list.head, abs_output_section,
4256
4275
/* If the normal relax is done and the relax finalize pass
4257
4276
is not performed yet, we perform another relax pass. */
4258
if (!relax_again && !link_info.relax_finalizing)
4277
if (!relax_again && link_info.need_relax_finalize)
4260
link_info.relax_finalizing = TRUE;
4279
link_info.need_relax_finalize = FALSE;
4261
4280
relax_again = TRUE;
4264
4283
while (relax_again);
4266
4285
/* Final extra sizing to report errors. */
4286
lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4267
4287
lang_reset_memory_regions ();
4268
lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4269
4288
lang_size_sections (statement_list.head, abs_output_section,
4270
4289
&statement_list.head, 0, 0, NULL, TRUE);
4485
4504
It is an error to specify both a load region and a load address. */
4488
lang_get_regions (struct memory_region_struct **region,
4489
struct memory_region_struct **lma_region,
4507
lang_get_regions (lang_memory_region_type **region,
4508
lang_memory_region_type **lma_region,
4490
4509
const char *memspec,
4491
4510
const char *lma_memspec,
4511
bfd_boolean have_lma,
4512
bfd_boolean have_vma)
4494
4514
*lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4496
/* If no runtime region has been given, but the load region has
4497
been, use the load region. */
4498
if (lma_memspec != 0 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4516
/* If no runtime region or VMA has been specified, but the load region has
4517
been specified, then use the load region for the runtime region as well. */
4518
if (lma_memspec != NULL
4520
&& strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4499
4521
*region = *lma_region;
4501
4523
*region = lang_memory_region_lookup (memspec, FALSE);
4503
if (have_lma_p && lma_memspec != 0)
4525
if (have_lma && lma_memspec != 0)
4504
4526
einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4508
lang_leave_output_section_statement
4509
(fill_type *fill, const char *memspec,
4510
struct lang_output_section_phdr_list *phdrs, const char *lma_memspec)
4530
lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4531
lang_output_section_phdr_list *phdrs,
4532
const char *lma_memspec)
4512
4534
lang_get_regions (¤t_section->region,
4513
4535
¤t_section->lma_region,
4514
4536
memspec, lma_memspec,
4515
current_section->load_base != 0);
4537
current_section->load_base != NULL,
4538
current_section->addr_tree != NULL);
4516
4539
current_section->fill = fill;
4517
4540
current_section->phdrs = phdrs;
4518
4541
stat_ptr = &statement_list;
4575
4598
h->u.def.value = 0;
4577
4600
h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4578
+ bfd_section_size (output_bfd, sec) /
4579
bfd_octets_per_byte (output_bfd));
4601
+ TO_ADDR (bfd_section_size (output_bfd, sec)));
4581
4603
h->u.def.section = bfd_abs_section_ptr;
4691
4713
u = u->output_section_statement.next)
4693
4715
lang_output_section_statement_type *os;
4694
struct lang_output_section_phdr_list *pl;
4716
lang_output_section_phdr_list *pl;
4696
4718
os = &u->output_section_statement;
4825
4847
etree_type *size;
4827
4849
lang_enter_output_section_statement (name, overlay_vma, normal_section,
4828
0, 0, overlay_subalign, 0);
4850
0, overlay_subalign, 0);
4830
4852
/* If this is the first section, then base the VMA of future
4831
4853
sections on this one. This will work correctly even if `.' is
4900
4922
int nocrossrefs,
4901
4923
fill_type *fill,
4902
4924
const char *memspec,
4903
struct lang_output_section_phdr_list *phdrs,
4925
lang_output_section_phdr_list *phdrs,
4904
4926
const char *lma_memspec)
4906
4928
lang_memory_region_type *region;
4907
4929
lang_memory_region_type *lma_region;
4908
4930
struct overlay_list *l;
4909
struct lang_nocrossref *nocrossref;
4931
lang_nocrossref_type *nocrossref;
4911
4933
lang_get_regions (®ion, &lma_region,
4912
4934
memspec, lma_memspec,
4935
lma_expr != NULL, FALSE);
4915
4937
nocrossref = NULL;