diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2020-04-21 05:23:51 -0700 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2020-04-21 05:24:03 -0700 |
commit | 6f6fd151cbf226bbaa66e44977f57b7c6dc33d89 (patch) | |
tree | 0ef4a78de35fde84a58c5964f7822a19a95c620c /bfd/elflink.c | |
parent | 1f7f2abbc31ee9e6d4faca58bef14d8ee8cb1bd2 (diff) | |
download | gdb-6f6fd151cbf226bbaa66e44977f57b7c6dc33d89.zip gdb-6f6fd151cbf226bbaa66e44977f57b7c6dc33d89.tar.gz gdb-6f6fd151cbf226bbaa66e44977f57b7c6dc33d89.tar.bz2 |
elf: Strip zero-sized dynamic sections
ELF size_dynamic_sections is called by the ELF backend linker after all
the linker input files have been seen but before the section sizes have
been set. After the sections sizes have been set, target-specific,
global optimizations may make some dynamic sections zero-sized if they
are no longer needed.
Add ELF strip_zero_sized_dynamic_sections so that ELF backend linker can
strip zero-sized dynamic sections after the sections sizes have been set.
bfd/
PR ld/25849
* elf-bfd.h (elf_backend_data): Add
elf_backend_strip_zero_sized_dynamic_sections.
(_bfd_elf_strip_zero_sized_dynamic_sections): New prototype.
* elf64-alpha.c (elf_backend_strip_zero_sized_dynamic_sections):
New macro.
* elflink.c (_bfd_elf_strip_zero_sized_dynamic_sections): New
function.
* elfxx-target.h (elf_backend_strip_zero_sized_dynamic_sections):
New macro.
(elfNN_bed): Add elf_backend_strip_zero_sized_dynamic_sections.
ld/
PR ld/25849
* ldelfgen.c (ldelf_map_segments): Call
elf_backend_strip_zero_sized_dynamic_sections.
* testsuite/ld-alpha/tlsbinr.rd: Updated.
Diffstat (limited to 'bfd/elflink.c')
-rw-r--r-- | bfd/elflink.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/bfd/elflink.c b/bfd/elflink.c index eb6b3ee..6624864 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -3501,6 +3501,104 @@ _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, return TRUE; } +/* Strip zero-sized dynamic sections. */ + +bfd_boolean +_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info) +{ + struct elf_link_hash_table *hash_table; + const struct elf_backend_data *bed; + asection *s, *sdynamic, **pp; + asection *rela_dyn, *rel_dyn; + Elf_Internal_Dyn dyn; + bfd_byte *extdyn, *next; + void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); + bfd_boolean strip_zero_sized; + bfd_boolean strip_zero_sized_plt; + + if (bfd_link_relocatable (info)) + return TRUE; + + hash_table = elf_hash_table (info); + if (!is_elf_hash_table (hash_table)) + return FALSE; + + if (!hash_table->dynobj) + return TRUE; + + sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic"); + if (!sdynamic) + return TRUE; + + bed = get_elf_backend_data (hash_table->dynobj); + swap_dyn_in = bed->s->swap_dyn_in; + + strip_zero_sized = FALSE; + strip_zero_sized_plt = FALSE; + + /* Strip zero-sized dynamic sections. */ + rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn"); + rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn"); + for (pp = &info->output_bfd->sections; (s = *pp) != NULL;) + if (s->size == 0 + && (s == rela_dyn + || s == rel_dyn + || s == hash_table->srelplt->output_section + || s == hash_table->splt->output_section)) + { + *pp = s->next; + info->output_bfd->section_count--; + strip_zero_sized = TRUE; + if (s == rela_dyn) + s = rela_dyn; + if (s == rel_dyn) + s = rel_dyn; + else if (s == hash_table->splt->output_section) + { + s = hash_table->splt; + strip_zero_sized_plt = TRUE; + } + else + s = hash_table->srelplt; + s->flags |= SEC_EXCLUDE; + s->output_section = bfd_abs_section_ptr; + } + else + pp = &s->next; + + if (strip_zero_sized_plt) + for (extdyn = sdynamic->contents; + extdyn < sdynamic->contents + sdynamic->size; + extdyn = next) + { + next = extdyn + bed->s->sizeof_dyn; + swap_dyn_in (hash_table->dynobj, extdyn, &dyn); + switch (dyn.d_tag) + { + default: + break; + case DT_JMPREL: + case DT_PLTRELSZ: + case DT_PLTREL: + /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if + the procedure linkage table (the .plt section) has been + removed. */ + memmove (extdyn, next, + sdynamic->size - (next - sdynamic->contents)); + next = extdyn; + } + } + + if (strip_zero_sized) + { + /* Regenerate program headers. */ + elf_seg_map (info->output_bfd) = NULL; + return _bfd_elf_map_sections_to_segments (info->output_bfd, info); + } + + return TRUE; +} + /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error, 1 if a DT_NEEDED tag already exists, and 0 on success. */ |