/* Linker command language support.
Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
- 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+ 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Free Software Foundation, Inc.
This file is part of the GNU Binutils.
static struct bfd_hash_table lang_definedness_table;
static lang_statement_list_type *stat_save[10];
static lang_statement_list_type **stat_save_ptr = &stat_save[0];
+static struct unique_sections *unique_section_list;
+static bfd_boolean ldlang_sysrooted_script = FALSE;
/* Forward declarations. */
static void exp_init_os (etree_type *);
bfd_boolean delete_output_file_on_failure = FALSE;
struct lang_phdr *lang_phdr_list;
struct lang_nocrossrefs *nocrossref_list;
-static struct unique_sections *unique_section_list;
-static bfd_boolean ldlang_sysrooted_script = FALSE;
+bfd_boolean missing_file = FALSE;
/* Functions that traverse the linker script and might evaluate
DEFINED() need to increment this. */
}
static bfd_boolean
-unique_section_p (const asection *sec)
+unique_section_p (const asection *sec,
+ const lang_output_section_statement_type *os)
{
struct unique_sections *unam;
const char *secnam;
if (link_info.relocatable
&& sec->owner != NULL
&& bfd_is_group_section (sec->owner, sec))
- return TRUE;
+ return !(os != NULL
+ && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
secnam = sec->name;
for (unam = unique_section_list; unam; unam = unam->next)
} section_iterator_callback_data;
static bfd_boolean
-section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
+section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
{
section_iterator_callback_data *d = (section_iterator_callback_data *) data;
struct wildcard_list *sec,
asection *section,
lang_input_statement_type *file,
- void *output ATTRIBUTE_UNUSED)
+ void *output)
{
lang_section_bst_type *node;
lang_section_bst_type **tree;
+ lang_output_section_statement_type *os;
+
+ os = (lang_output_section_statement_type *) output;
- if (unique_section_p (section))
+ if (unique_section_p (section, os))
return;
node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
p->next_real_file = NULL;
p->next = NULL;
p->dynamic = config.dynamic_link;
- p->add_needed = add_needed;
- p->as_needed = as_needed;
+ p->add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
+ p->add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
p->whole_archive = whole_archive;
p->loaded = FALSE;
+ p->missing_file = FALSE;
+
lang_statement_append (&input_file_chain,
(lang_statement_union_type *) p,
&p->next_real_file);
add_child = &os->children;
lang_add_section (add_child, s, os);
- lang_leave_output_section_statement (0, "*default*", NULL, NULL);
+ if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
+ {
+ const char *region = (after->region
+ ? after->region->name_list.name
+ : DEFAULT_MEMORY_REGION);
+ const char *lma_region = (after->lma_region
+ ? after->lma_region->name_list.name
+ : NULL);
+ lang_leave_output_section_statement (NULL, region, after->phdrs,
+ lma_region);
+ }
+ else
+ lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
+ NULL);
if (ps != NULL && *ps == '\0')
{
/* Initialize an output section. */
static void
-init_os (lang_output_section_statement_type *s, asection *isec,
- flagword flags)
+init_os (lang_output_section_statement_type *s, flagword flags)
{
- if (s->bfd_section != NULL)
- return;
-
if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
/* If supplied an alignment, set it. */
if (s->section_alignment != -1)
s->bfd_section->alignment_power = s->section_alignment;
-
- if (isec)
- bfd_init_private_section_data (isec->owner, isec,
- link_info.output_bfd, s->bfd_section,
- &link_info);
}
/* Make sure that all output sections mentioned in an expression are
os = lang_output_section_find (exp->name.name);
if (os != NULL && os->bfd_section == NULL)
- init_os (os, NULL, 0);
+ init_os (os, 0);
}
}
break;
{
flagword flags = section->flags;
bfd_boolean discard;
+ lang_input_section_type *new_section;
/* Discard sections marked with SEC_EXCLUDE. */
discard = (flags & SEC_EXCLUDE) != 0;
return;
}
- if (section->output_section == NULL)
- {
- bfd_boolean first;
- lang_input_section_type *new_section;
- flagword flags;
-
- flags = section->flags;
-
- /* We don't copy the SEC_NEVER_LOAD flag from an input section
- to an output section, because we want to be able to include a
- SEC_NEVER_LOAD section in the middle of an otherwise loaded
- section (I don't know why we want to do this, but we do).
- build_link_order in ldwrite.c handles this case by turning
- the embedded SEC_NEVER_LOAD section into a fill. */
-
- flags &= ~ SEC_NEVER_LOAD;
-
- switch (output->sectype)
- {
- case normal_section:
- case overlay_section:
- break;
- case noalloc_section:
- flags &= ~SEC_ALLOC;
- break;
- case noload_section:
- flags &= ~SEC_LOAD;
- flags |= SEC_NEVER_LOAD;
- break;
- }
+ if (section->output_section != NULL)
+ return;
- if (output->bfd_section == NULL)
- init_os (output, section, flags);
+ /* We don't copy the SEC_NEVER_LOAD flag from an input section
+ to an output section, because we want to be able to include a
+ SEC_NEVER_LOAD section in the middle of an otherwise loaded
+ section (I don't know why we want to do this, but we do).
+ build_link_order in ldwrite.c handles this case by turning
+ the embedded SEC_NEVER_LOAD section into a fill. */
+ flags &= ~ SEC_NEVER_LOAD;
- first = ! output->bfd_section->linker_has_input;
- output->bfd_section->linker_has_input = 1;
+ /* If final link, don't copy the SEC_LINK_ONCE flags, they've
+ already been processed. One reason to do this is that on pe
+ format targets, .text$foo sections go into .text and it's odd
+ to see .text with SEC_LINK_ONCE set. */
- if (!link_info.relocatable
- && !stripped_excluded_sections)
- {
- asection *s = output->bfd_section->map_tail.s;
- output->bfd_section->map_tail.s = section;
- section->map_head.s = NULL;
- section->map_tail.s = s;
- if (s != NULL)
- s->map_head.s = section;
- else
- output->bfd_section->map_head.s = section;
- }
+ if (!link_info.relocatable)
+ flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
- /* Add a section reference to the list. */
- new_section = new_stat (lang_input_section, ptr);
-
- new_section->section = section;
- section->output_section = output->bfd_section;
-
- /* If final link, don't copy the SEC_LINK_ONCE flags, they've
- already been processed. One reason to do this is that on pe
- format targets, .text$foo sections go into .text and it's odd
- to see .text with SEC_LINK_ONCE set. */
+ switch (output->sectype)
+ {
+ case normal_section:
+ case overlay_section:
+ break;
+ case noalloc_section:
+ flags &= ~SEC_ALLOC;
+ break;
+ case noload_section:
+ flags &= ~SEC_LOAD;
+ flags |= SEC_NEVER_LOAD;
+ break;
+ }
- if (! link_info.relocatable)
- flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
+ if (output->bfd_section == NULL)
+ init_os (output, flags);
- /* If this is not the first input section, and the SEC_READONLY
- flag is not currently set, then don't set it just because the
- input section has it set. */
+ /* If SEC_READONLY is not set in the input section, then clear
+ it from the output section. */
+ output->bfd_section->flags &= flags | ~SEC_READONLY;
- if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
- flags &= ~ SEC_READONLY;
+ if (output->bfd_section->linker_has_input)
+ {
+ /* Only set SEC_READONLY flag on the first input section. */
+ flags &= ~ SEC_READONLY;
/* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
- if (! first
- && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
- != (flags & (SEC_MERGE | SEC_STRINGS))
- || ((flags & SEC_MERGE)
- && output->bfd_section->entsize != section->entsize)))
+ if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
+ != (flags & (SEC_MERGE | SEC_STRINGS))
+ || ((flags & SEC_MERGE) != 0
+ && output->bfd_section->entsize != section->entsize))
{
output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
flags &= ~ (SEC_MERGE | SEC_STRINGS);
}
+ }
+ output->bfd_section->flags |= flags;
- output->bfd_section->flags |= flags;
-
- if (flags & SEC_MERGE)
+ if (!output->bfd_section->linker_has_input)
+ {
+ output->bfd_section->linker_has_input = 1;
+ /* This must happen after flags have been updated. The output
+ section may have been created before we saw its first input
+ section, eg. for a data statement. */
+ bfd_init_private_section_data (section->owner, section,
+ link_info.output_bfd,
+ output->bfd_section,
+ &link_info);
+ if ((flags & SEC_MERGE) != 0)
output->bfd_section->entsize = section->entsize;
+ }
- /* If SEC_READONLY is not set in the input section, then clear
- it from the output section. */
- if ((section->flags & SEC_READONLY) == 0)
- output->bfd_section->flags &= ~SEC_READONLY;
+ if ((flags & SEC_TIC54X_BLOCK) != 0
+ && bfd_get_arch (section->owner) == bfd_arch_tic54x)
+ {
+ /* FIXME: This value should really be obtained from the bfd... */
+ output->block_value = 128;
+ }
- /* Copy over SEC_SMALL_DATA. */
- if (section->flags & SEC_SMALL_DATA)
- output->bfd_section->flags |= SEC_SMALL_DATA;
+ if (section->alignment_power > output->bfd_section->alignment_power)
+ output->bfd_section->alignment_power = section->alignment_power;
- if (section->alignment_power > output->bfd_section->alignment_power)
- output->bfd_section->alignment_power = section->alignment_power;
+ section->output_section = output->bfd_section;
- if (bfd_get_arch (section->owner) == bfd_arch_tic54x
- && (section->flags & SEC_TIC54X_BLOCK) != 0)
- {
- output->bfd_section->flags |= SEC_TIC54X_BLOCK;
- /* FIXME: This value should really be obtained from the bfd... */
- output->block_value = 128;
- }
+ if (!link_info.relocatable
+ && !stripped_excluded_sections)
+ {
+ asection *s = output->bfd_section->map_tail.s;
+ output->bfd_section->map_tail.s = section;
+ section->map_head.s = NULL;
+ section->map_tail.s = s;
+ if (s != NULL)
+ s->map_head.s = section;
+ else
+ output->bfd_section->map_head.s = section;
}
+
+ /* Add a section reference to the list. */
+ new_section = new_stat (lang_input_section, ptr);
+ new_section->section = section;
}
/* Handle wildcard sorting. This returns the lang_input_section which
lang_input_statement_type *file,
asection *section)
{
- const char *section_name;
lang_statement_union_type *l;
if (!wild->filenames_sorted
&& (sec == NULL || sec->spec.sorted == none))
return NULL;
- section_name = bfd_get_section_name (file->the_bfd, section);
for (l = wild->children.head; l != NULL; l = l->header.next)
{
lang_input_section_type *ls;
void *output)
{
lang_statement_union_type *before;
+ lang_output_section_statement_type *os;
+
+ os = (lang_output_section_statement_type *) output;
/* Exclude sections that match UNIQUE_SECTION_LIST. */
- if (unique_section_p (section))
+ if (unique_section_p (section, os))
return;
before = wild_sort (ptr, sec, file, section);
of the current list. */
if (before == NULL)
- lang_add_section (&ptr->children, section,
- (lang_output_section_statement_type *) output);
+ lang_add_section (&ptr->children, section, os);
else
{
lang_statement_list_type list;
lang_statement_union_type **pp;
lang_list_init (&list);
- lang_add_section (&list, section,
- (lang_output_section_statement_type *) output);
+ lang_add_section (&list, section, os);
/* If we are discarding the section, LIST.HEAD will
be NULL. */
struct wildcard_list *sec ATTRIBUTE_UNUSED,
asection *section,
lang_input_statement_type *file ATTRIBUTE_UNUSED,
- void *data)
+ void *output)
{
+ lang_output_section_statement_type *os;
+
+ os = (lang_output_section_statement_type *) output;
+
/* Exclude sections that match UNIQUE_SECTION_LIST. */
- if (unique_section_p (section))
+ if (unique_section_p (section, os))
return;
if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
- ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
+ os->all_input_readonly = FALSE;
}
/* This is passed a file name which must have been seen already and
ldfile_open_file (entry);
+ /* Do not process further if the file was missing. */
+ if (entry->missing_file)
+ return TRUE;
+
if (! bfd_check_format (entry->the_bfd, bfd_archive)
&& ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
{
bfd_error_type err;
bfd_boolean save_ldlang_sysrooted_script;
- bfd_boolean save_as_needed, save_add_needed;
+ bfd_boolean save_add_DT_NEEDED_for_regular;
+ bfd_boolean save_add_DT_NEEDED_for_dynamic;
+ bfd_boolean save_whole_archive;
err = bfd_get_error ();
push_stat_ptr (place);
save_ldlang_sysrooted_script = ldlang_sysrooted_script;
ldlang_sysrooted_script = entry->sysrooted;
- save_as_needed = as_needed;
- as_needed = entry->as_needed;
- save_add_needed = add_needed;
- add_needed = entry->add_needed;
+ save_add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
+ add_DT_NEEDED_for_regular = entry->add_DT_NEEDED_for_regular;
+ save_add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
+ add_DT_NEEDED_for_dynamic = entry->add_DT_NEEDED_for_dynamic;
+ save_whole_archive = whole_archive;
+ whole_archive = entry->whole_archive;
ldfile_assumed_script = TRUE;
parser_input = input_script;
ldfile_assumed_script = FALSE;
ldlang_sysrooted_script = save_ldlang_sysrooted_script;
- as_needed = save_as_needed;
- add_needed = save_add_needed;
+ add_DT_NEEDED_for_regular = save_add_DT_NEEDED_for_regular;
+ add_DT_NEEDED_for_dynamic = save_add_DT_NEEDED_for_dynamic;
+ whole_archive = save_whole_archive;
pop_stat_ptr ();
return TRUE;
break;
}
}
+
+ /* Exit if any of the files were missing. */
+ if (missing_file)
+ einfo ("%F");
}
/* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
(lang_statement_union_type *s, const char *target,
lang_output_section_statement_type *os)
{
- flagword flags;
-
for (; s != NULL; s = s->header.next)
{
+ lang_output_section_statement_type *tos;
+ flagword flags;
+
switch (s->header.type)
{
case lang_wild_statement_enum:
os);
break;
case lang_output_section_statement_enum:
- if (s->output_section_statement.constraint)
+ tos = &s->output_section_statement;
+ if (tos->constraint != 0)
{
- if (s->output_section_statement.constraint != ONLY_IF_RW
- && s->output_section_statement.constraint != ONLY_IF_RO)
+ if (tos->constraint != ONLY_IF_RW
+ && tos->constraint != ONLY_IF_RO)
break;
- s->output_section_statement.all_input_readonly = TRUE;
- check_input_sections (s->output_section_statement.children.head,
- &s->output_section_statement);
- if ((s->output_section_statement.all_input_readonly
- && s->output_section_statement.constraint == ONLY_IF_RW)
- || (!s->output_section_statement.all_input_readonly
- && s->output_section_statement.constraint == ONLY_IF_RO))
+ tos->all_input_readonly = TRUE;
+ check_input_sections (tos->children.head, tos);
+ if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
{
- s->output_section_statement.constraint = -1;
+ tos->constraint = -1;
break;
}
}
-
- map_input_to_output_sections (s->output_section_statement.children.head,
+ map_input_to_output_sections (tos->children.head,
target,
- &s->output_section_statement);
+ tos);
break;
case lang_output_statement_enum:
break;
/* Make sure that any sections mentioned in the expression
are initialized. */
exp_init_os (s->data_statement.exp);
- flags = SEC_HAS_CONTENTS;
- /* The output section gets contents, and then we inspect for
- any flags set in the input script which override any ALLOC. */
- if (!(os->flags & SEC_NEVER_LOAD))
- flags |= SEC_ALLOC | SEC_LOAD;
+ /* The output section gets CONTENTS, and usually ALLOC and
+ LOAD, but the latter two may be overridden by the script. */
+ flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
+ switch (os->sectype)
+ {
+ case normal_section:
+ case overlay_section:
+ break;
+ case noalloc_section:
+ flags = SEC_HAS_CONTENTS;
+ break;
+ case noload_section:
+ flags = SEC_HAS_CONTENTS | SEC_NEVER_LOAD;
+ break;
+ }
if (os->bfd_section == NULL)
- init_os (os, NULL, flags);
+ init_os (os, flags);
else
os->bfd_section->flags |= flags;
break;
case lang_padding_statement_enum:
case lang_input_statement_enum:
if (os != NULL && os->bfd_section == NULL)
- init_os (os, NULL, 0);
+ init_os (os, 0);
break;
case lang_assignment_statement_enum:
if (os != NULL && os->bfd_section == NULL)
- init_os (os, NULL, 0);
+ init_os (os, 0);
/* Make sure that any sections mentioned in the assignment
are initialized. */
if (!s->address_statement.segment
|| !s->address_statement.segment->used)
{
- lang_output_section_statement_type *aos
- = (lang_output_section_statement_lookup
- (s->address_statement.section_name, 0, TRUE));
-
- if (aos->bfd_section == NULL)
- init_os (aos, NULL, 0);
- aos->addr_tree = s->address_statement.address;
+ const char *name = s->address_statement.section_name;
+
+ /* Create the output section statement here so that
+ orphans with a set address will be placed after other
+ script sections. If we let the orphan placement code
+ place them in amongst other sections then the address
+ will affect following script sections, which is
+ likely to surprise naive users. */
+ tos = lang_output_section_statement_lookup (name, 0, TRUE);
+ tos->addr_tree = s->address_statement.address;
+ if (tos->bfd_section == NULL)
+ init_os (tos, 0);
}
break;
case lang_insert_statement_enum:
os_region_check (lang_output_section_statement_type *os,
lang_memory_region_type *region,
etree_type *tree,
- bfd_vma base)
+ bfd_vma rbase)
{
if ((region->current < region->origin
|| (region->current - region->origin > region->length))
&& ((region->current != region->origin + region->length)
- || base == 0))
+ || rbase == 0))
{
if (tree != NULL)
{
static bfd_vma
lang_size_sections_1
- (lang_statement_union_type *s,
+ (lang_statement_union_type **prev,
lang_output_section_statement_type *output_section_statement,
- lang_statement_union_type **prev,
fill_type *fill,
bfd_vma dot,
bfd_boolean *relax,
bfd_boolean check_regions)
{
+ lang_statement_union_type *s;
+
/* Size up the sections from their constituent parts. */
- for (; s != NULL; s = s->header.next)
+ for (s = *prev; s != NULL; s = s->header.next)
{
switch (s->header.type)
{
lang_memory_region_type *r;
os = &s->output_section_statement;
- if (link_info.relocatable)
+ /* FIXME: We shouldn't need to zero section vmas for ld -r
+ here, in lang_insert_orphan, or in the default linker scripts.
+ This is covering for coff backend linker bugs. See PR6945. */
+ if (os->addr_tree == NULL
+ && link_info.relocatable
+ && (bfd_get_flavour (link_info.output_bfd)
+ == bfd_target_coff_flavour))
os->addr_tree = exp_intop (0);
if (os->addr_tree != NULL)
{
os->bfd_section->output_offset = 0;
}
- lang_size_sections_1 (os->children.head, os, &os->children.head,
+ lang_size_sections_1 (&os->children.head, os,
os->fill, newdot, relax, check_regions);
os->processed_vma = TRUE;
break;
case lang_constructors_statement_enum:
- dot = lang_size_sections_1 (constructor_list.head,
+ dot = lang_size_sections_1 (&constructor_list.head,
output_section_statement,
- &s->wild_statement.children.head,
fill, dot, relax, check_regions);
break;
break;
case lang_wild_statement_enum:
- dot = lang_size_sections_1 (s->wild_statement.children.head,
+ dot = lang_size_sections_1 (&s->wild_statement.children.head,
output_section_statement,
- &s->wild_statement.children.head,
fill, dot, relax, check_regions);
break;
{
asection *i;
- i = (*prev)->input_section.section;
+ i = s->input_section.section;
if (relax)
{
bfd_boolean again;
break;
case lang_group_statement_enum:
- dot = lang_size_sections_1 (s->group_statement.children.head,
+ dot = lang_size_sections_1 (&s->group_statement.children.head,
output_section_statement,
- &s->group_statement.children.head,
fill, dot, relax, check_regions);
break;
one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
{
lang_statement_iteration++;
- lang_size_sections_1 (statement_list.head, abs_output_section,
- &statement_list.head, 0, 0, relax, check_regions);
+ lang_size_sections_1 (&statement_list.head, abs_output_section,
+ 0, 0, relax, check_regions);
}
void
const char *name = s->name;
int constraint = 0;
- if (config.unique_orphan_sections || unique_section_p (s))
+ if (config.unique_orphan_sections
+ || unique_section_p (s, NULL))
constraint = SPECIAL;
if (!ldemul_place_orphan (s, name, constraint))
void
lang_relax_sections (bfd_boolean need_layout)
{
- if (command_line.relax)
+ if (RELAXATION_ENABLED)
{
/* We may need more than one relaxation pass. */
int i = link_info.relax_pass;
lang_find_relro_sections ();
/* Size up the sections. */
- lang_size_sections (NULL, !command_line.relax);
+ lang_size_sections (NULL, ! RELAXATION_ENABLED);
/* See if anything special should be done now we know how big
everything is. This is where relaxation is done. */
etree_type *flags)
{
struct lang_phdr *n, **pp;
+ bfd_boolean hdrs;
n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
n->next = NULL;
n->phdrs = phdrs;
n->at = at;
n->flags = flags;
+
+ hdrs = n->type == 1 && (phdrs || filehdr);
for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
- ;
+ if (hdrs
+ && (*pp)->type == 1
+ && !((*pp)->filehdr || (*pp)->phdrs))
+ {
+ einfo (_("%X%P:%S: PHDRS and FILEHDR are not supported when prior PT_LOAD headers lack them\n"));
+ hdrs = FALSE;
+ }
+
*pp = n;
}