/* Linker command language support.
- Copyright (C) 1991-2021 Free Software Foundation, Inc.
+ Copyright (C) 1991-2022 Free Software Foundation, Inc.
This file is part of the GNU Binutils.
address = exp_intop (0);
os_tail = (lang_output_section_statement_type **) lang_os_list.tail;
- os = lang_enter_output_section_statement (secname, address, normal_section,
- NULL, NULL, NULL, constraint, 0);
+ os = lang_enter_output_section_statement (
+ secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0);
if (add_child == NULL)
add_child = &os->children;
case normal_section:
case overlay_section:
case first_overlay_section:
+ case type_section:
break;
case noalloc_section:
flags &= ~SEC_ALLOC;
break;
+ case typed_readonly_section:
case readonly_section:
flags |= SEC_READONLY;
break;
output->block_value = 128;
}
+ /* When a .ctors section is placed in .init_array it must be copied
+ in reverse order. Similarly for .dtors. Set that up. */
+ if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
+ && ((startswith (section->name, ".ctors")
+ && strcmp (output->bfd_section->name, ".init_array") == 0)
+ || (startswith (section->name, ".dtors")
+ && strcmp (output->bfd_section->name, ".fini_array") == 0))
+ && (section->name[6] == 0 || section->name[6] == '.'))
+ section->flags |= SEC_ELF_REVERSE_COPY;
+
if (section->alignment_power > output->bfd_section->alignment_power)
output->bfd_section->alignment_power = section->alignment_power;
f = f->next_real_file)
if (f->flags.real)
{
- char *in = lrealpath (f->filename);
+ char *in = lrealpath (f->local_sym_name);
if (filename_cmp (in, out) == 0)
einfo (_("%F%P: input file '%s' is the same as output file\n"),
f->filename);
{
lang_output_section_statement_type *tos;
flagword flags;
+ unsigned int type = 0;
switch (s->header.type)
{
case readonly_section:
flags |= SEC_READONLY;
break;
+ case typed_readonly_section:
+ flags |= SEC_READONLY;
+ /* Fall through. */
+ case type_section:
+ if (os->sectype_value->type.node_class == etree_name
+ && os->sectype_value->type.node_code == NAME)
+ {
+ const char *name = os->sectype_value->name.name;
+ if (strcmp (name, "SHT_PROGBITS") == 0)
+ type = SHT_PROGBITS;
+ else if (strcmp (name, "SHT_STRTAB") == 0)
+ type = SHT_STRTAB;
+ else if (strcmp (name, "SHT_NOTE") == 0)
+ type = SHT_NOTE;
+ else if (strcmp (name, "SHT_NOBITS") == 0)
+ type = SHT_NOBITS;
+ else if (strcmp (name, "SHT_INIT_ARRAY") == 0)
+ type = SHT_INIT_ARRAY;
+ else if (strcmp (name, "SHT_FINI_ARRAY") == 0)
+ type = SHT_FINI_ARRAY;
+ else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0)
+ type = SHT_PREINIT_ARRAY;
+ else
+ einfo (_ ("%F%P: invalid type for output section `%s'\n"),
+ os->name);
+ }
+ else
+ {
+ exp_fold_tree_no_dot (os->sectype_value);
+ if (expld.result.valid_p)
+ type = expld.result.value;
+ else
+ einfo (_ ("%F%P: invalid type for output section `%s'\n"),
+ os->name);
+ }
+ break;
case noload_section:
if (bfd_get_flavour (link_info.output_bfd)
== bfd_target_elf_flavour)
init_os (os, flags | SEC_READONLY);
else
os->bfd_section->flags |= flags;
+ os->bfd_section->type = type;
break;
case lang_input_section_enum:
break;
}
static void
-ldlang_check_relro_region (lang_statement_union_type *s,
- seg_align_type *seg)
+ldlang_check_relro_region (lang_statement_union_type *s)
{
+ seg_align_type *seg = &expld.dataseg;
+
if (seg->relro == exp_seg_relro_start)
{
if (!seg->relro_start_stat)
output_section_statement->bfd_section,
&newdot);
- ldlang_check_relro_region (s, &expld.dataseg);
+ ldlang_check_relro_region (s);
expld.dataseg.relro = exp_seg_relro_none;
}
static bool
-lang_size_segment (seg_align_type *seg)
+lang_size_segment (void)
{
/* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
a page could be saved in the data segment. */
+ seg_align_type *seg = &expld.dataseg;
bfd_vma first, last;
- first = -seg->base & (seg->pagesize - 1);
- last = seg->end & (seg->pagesize - 1);
+ first = -seg->base & (seg->commonpagesize - 1);
+ last = seg->end & (seg->commonpagesize - 1);
if (first && last
- && ((seg->base & ~(seg->pagesize - 1))
- != (seg->end & ~(seg->pagesize - 1)))
- && first + last <= seg->pagesize)
+ && ((seg->base & ~(seg->commonpagesize - 1))
+ != (seg->end & ~(seg->commonpagesize - 1)))
+ && first + last <= seg->commonpagesize)
{
seg->phase = exp_seg_adjust;
return true;
}
static bfd_vma
-lang_size_relro_segment_1 (seg_align_type *seg)
+lang_size_relro_segment_1 (void)
{
+ seg_align_type *seg = &expld.dataseg;
bfd_vma relro_end, desired_end;
asection *sec;
/* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
- relro_end = ((seg->relro_end + seg->pagesize - 1)
- & ~(seg->pagesize - 1));
+ relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize;
/* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
desired_end = relro_end - seg->relro_offset;
lang_size_relro_segment (bool *relax, bool check_regions)
{
bool do_reset = false;
- bool do_data_relro;
- bfd_vma data_initial_base, data_relro_end;
if (link_info.relro && expld.dataseg.relro_end)
{
- do_data_relro = true;
- data_initial_base = expld.dataseg.base;
- data_relro_end = lang_size_relro_segment_1 (&expld.dataseg);
- }
- else
- {
- do_data_relro = false;
- data_initial_base = data_relro_end = 0;
- }
+ bfd_vma data_initial_base = expld.dataseg.base;
+ bfd_vma data_relro_end = lang_size_relro_segment_1 ();
- if (do_data_relro)
- {
lang_reset_memory_regions ();
one_lang_size_sections_pass (relax, check_regions);
/* Assignments to dot, or to output section address in a user
script have increased padding over the original. Revert. */
- if (do_data_relro && expld.dataseg.relro_end > data_relro_end)
+ if (expld.dataseg.relro_end > data_relro_end)
{
- expld.dataseg.base = data_initial_base;;
+ expld.dataseg.base = data_initial_base;
do_reset = true;
}
}
-
- if (!do_data_relro && lang_size_segment (&expld.dataseg))
+ else if (lang_size_segment ())
do_reset = true;
return do_reset;
foreach_start_stop (set_start_stop);
}
+static void
+lang_symbol_tweaks (void)
+{
+ /* Give initial values for __start and __stop symbols, so that ELF
+ gc_sections will keep sections referenced by these symbols. Must
+ be done before lang_do_assignments. */
+ if (config.build_constructors)
+ lang_init_start_stop ();
+
+ /* Make __ehdr_start hidden, and set def_regular even though it is
+ likely undefined at this stage. For lang_check_relocs. */
+ if (is_elf_hash_table (link_info.hash)
+ && !bfd_link_relocatable (&link_info))
+ {
+ struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
+ bfd_link_hash_lookup (link_info.hash, "__ehdr_start",
+ false, false, true);
+
+ /* Only adjust the export class if the symbol was referenced
+ and not defined, otherwise leave it alone. */
+ if (h != NULL
+ && (h->root.type == bfd_link_hash_new
+ || h->root.type == bfd_link_hash_undefined
+ || h->root.type == bfd_link_hash_undefweak
+ || h->root.type == bfd_link_hash_common))
+ {
+ const struct elf_backend_data *bed;
+ bed = get_elf_backend_data (link_info.output_bfd);
+ (*bed->elf_backend_hide_symbol) (&link_info, h, true);
+ if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
+ h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
+ h->def_regular = 1;
+ h->root.linker_def = 1;
+ h->root.rel_from_abs = 1;
+ }
+ }
+}
+
static void
lang_end (void)
{
lang_enter_output_section_statement (const char *output_section_statement_name,
etree_type *address_exp,
enum section_type sectype,
+ etree_type *sectype_value,
etree_type *align,
etree_type *subalign,
etree_type *ebase,
os->addr_tree = address_exp;
}
os->sectype = sectype;
- if (sectype != noload_section)
- os->flags = SEC_NO_FLAGS;
- else
+ if (sectype == type_section || sectype == typed_readonly_section)
+ os->sectype_value = sectype_value;
+ else if (sectype == noload_section)
os->flags = SEC_NEVER_LOAD;
+ else
+ os->flags = SEC_NO_FLAGS;
os->block_value = 1;
/* Make next things chain into subchain of this. */
static void
lang_find_relro_sections_1 (lang_statement_union_type *s,
- seg_align_type *seg,
bool *has_relro_section)
{
if (*has_relro_section)
for (; s != NULL; s = s->header.next)
{
- if (s == seg->relro_end_stat)
+ if (s == expld.dataseg.relro_end_stat)
break;
switch (s->header.type)
break;
case lang_constructors_statement_enum:
lang_find_relro_sections_1 (constructor_list.head,
- seg, has_relro_section);
+ has_relro_section);
break;
case lang_output_section_statement_enum:
lang_find_relro_sections_1 (s->output_section_statement.children.head,
- seg, has_relro_section);
+ has_relro_section);
break;
case lang_group_statement_enum:
lang_find_relro_sections_1 (s->group_statement.children.head,
- seg, has_relro_section);
+ has_relro_section);
break;
default:
break;
/* Check all sections in the link script. */
lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
- &expld.dataseg, &has_relro_section);
+ &has_relro_section);
if (!has_relro_section)
link_info.relro = false;
void
lang_relax_sections (bool need_layout)
{
- if (RELAXATION_ENABLED)
+ /* NB: Also enable relaxation to layout sections for DT_RELR. */
+ if (RELAXATION_ENABLED || link_info.enable_dt_relr)
{
/* We may need more than one relaxation pass. */
int i = link_info.relax_pass;
files. */
ldctor_build_sets ();
- /* Give initial values for __start and __stop symbols, so that ELF
- gc_sections will keep sections referenced by these symbols. Must
- be done before lang_do_assignments below. */
- if (config.build_constructors)
- lang_init_start_stop ();
+ lang_symbol_tweaks ();
/* PR 13683: We must rerun the assignments prior to running garbage
collection in order to make sure that all symbol aliases are resolved. */
etree_type *size;
lang_enter_output_section_statement (name, overlay_vma, overlay_section,
- 0, overlay_subalign, 0, 0, 0);
+ 0, 0, overlay_subalign, 0, 0, 0);
/* If this is the first section, then base the VMA of future
sections on this one. This will work correctly even if `.' is