From 8df52eeeb06efcccd6b840689ad8e11ebb264e7f Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 4 Feb 2022 19:30:47 +1030 Subject: [PATCH] Detect .eh_frame_hdr earlier for SIZEOF_HEADERS Current code detects the need for PT_GNU_EH_FRAME using a field set by _bfd_elf_discard_section_eh_frame_hdr, which is called fairly late in the linking process. Use the elf hash table eh_info instead, which is set up earlier by size_dynamic_sections. * elf-bfd.h (struct output_elf_obj_tdata): Delete eh_frame_hdr. (elf_eh_frame_hdr): Don't define. (_bfd_elf_discard_section_eh_frame_hdr): Update prototype. * elf-eh-frame.c (_bfd_elf_discard_section_eh_frame_hdr): Delete abfd parameter. Don't set elf_eh_frame_hdr. * elf.c (elf_eh_frame_hdr): New function. (get_program_header_size): Adjust elf_eh_frame_hdr call. (_bfd_elf_map_sections_to_segments): Likewise. --- bfd/elf-bfd.h | 7 +------ bfd/elf-eh-frame.c | 3 +-- bfd/elf.c | 18 ++++++++++++++++-- bfd/elflink.c | 2 +- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 138ef46791b..1d3ae76339a 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1909,10 +1909,6 @@ struct output_elf_obj_tdata /* STT_SECTION symbols for each section */ asymbol **section_syms; - /* Used to determine if PT_GNU_EH_FRAME segment header should be - created. */ - asection *eh_frame_hdr; - /* NT_GNU_BUILD_ID note type info. */ struct { @@ -2116,7 +2112,6 @@ struct elf_obj_tdata #define elf_seg_map(bfd) (elf_tdata(bfd) -> o->seg_map) #define elf_link_info(bfd) (elf_tdata(bfd) -> o->link_info) #define elf_next_file_pos(bfd) (elf_tdata(bfd) -> o->next_file_pos) -#define elf_eh_frame_hdr(bfd) (elf_tdata(bfd) -> o->eh_frame_hdr) #define elf_stack_flags(bfd) (elf_tdata(bfd) -> o->stack_flags) #define elf_shstrtab(bfd) (elf_tdata(bfd) -> o->strtab_ptr) #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section) @@ -2412,7 +2407,7 @@ extern bool _bfd_elf_discard_section_eh_frame extern bool _bfd_elf_adjust_eh_frame_global_symbol (struct elf_link_hash_entry *, void *); extern bool _bfd_elf_discard_section_eh_frame_hdr - (bfd *, struct bfd_link_info *); + (struct bfd_link_info *); extern bfd_vma _bfd_elf_eh_frame_section_offset (bfd *, struct bfd_link_info *, asection *, bfd_vma); extern bool _bfd_elf_write_section_eh_frame diff --git a/bfd/elf-eh-frame.c b/bfd/elf-eh-frame.c index a621fedbee8..2e22d0c9215 100644 --- a/bfd/elf-eh-frame.c +++ b/bfd/elf-eh-frame.c @@ -1612,7 +1612,7 @@ _bfd_elf_discard_section_eh_frame input sections. It finalizes the size of .eh_frame_hdr section. */ bool -_bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) +_bfd_elf_discard_section_eh_frame_hdr (struct bfd_link_info *info) { struct elf_link_hash_table *htab; struct eh_frame_hdr_info *hdr_info; @@ -1644,7 +1644,6 @@ _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) sec->size += 4 + hdr_info->u.dwarf.fde_count * 8; } - elf_eh_frame_hdr (abfd) = sec; return true; } diff --git a/bfd/elf.c b/bfd/elf.c index 14c2c7ba734..79f71aa81e2 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -4343,6 +4343,20 @@ _bfd_elf_compute_section_file_positions (bfd *abfd, return true; } +/* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the + function effectively returns whether --eh-frame-hdr is given on the + command line. After size_dynamic_sections the result reflects + whether .eh_frame_hdr will actually be output (sizing isn't done + until ldemul_after_allocation). */ + +static asection * +elf_eh_frame_hdr (const struct bfd_link_info *info) +{ + if (info != NULL && is_elf_hash_table (info->hash)) + return elf_hash_table (info)->eh_info.hdr_sec; + return NULL; +} + /* Make an initial estimate of the size of the program header. If we get the number wrong here, we'll redo section placement. */ @@ -4379,7 +4393,7 @@ get_program_header_size (bfd *abfd, struct bfd_link_info *info) ++segs; } - if (elf_eh_frame_hdr (abfd)) + if (elf_eh_frame_hdr (info)) { /* We need a PT_GNU_EH_FRAME segment. */ ++segs; @@ -5134,7 +5148,7 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME segment. */ - eh_frame_hdr = elf_eh_frame_hdr (abfd); + eh_frame_hdr = elf_eh_frame_hdr (info); if (eh_frame_hdr != NULL && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) { diff --git a/bfd/elflink.c b/bfd/elflink.c index 2677561478d..6fa18d92007 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -14888,7 +14888,7 @@ bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) if (info->eh_frame_hdr_type && !bfd_link_relocatable (info) - && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) + && _bfd_elf_discard_section_eh_frame_hdr (info)) changed = 1; return changed; -- 2.30.2