From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 85765385802F for ; Thu, 6 Jan 2022 15:05:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 85765385802F Received: by mail-pj1-x1033.google.com with SMTP id c14-20020a17090a674e00b001b31e16749cso7371401pjm.4 for ; Thu, 06 Jan 2022 07:05:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=M8KNU9FvKhOZh7GYsRSHiNS4CW1yYJzFyMi+8kKaNEk=; b=AEFDxsawguwM4LBvadjZDF5B8HrDSrPe3L8jc+AtOJJvNFh4ysqhtUIZtkpx5xlMI7 SMO/nDjTVfbeedYjTMVuUhDemkhktnnAR5nugGJP+EW7rg7jdxh6rXD0pBUYJru0xgLb MAFGwO8IDkcmn0y3kmFcjFWUIeMvkEiBF+blItP85+ZfdI4ikfAMKVQLSYH/N1hfJN3M irZUFAPcCEw4o9gN08X4VZ+PulklMg0dz4dUkmWo/UVcHlNpJf89HSIx3tFU18GTQCLD YSPh7eByOFU9Zl4MWiK2FVkzRRwQU5NGQ7lXYmrOe8IcOcvMWsBUKBH3dFUzwlunYpqk 4GDw== X-Gm-Message-State: AOAM532Qn8tV+AVUjidcE9znARF3axVZC3V6pTzchw371eNsOUPBr3xv D4UoMuPHWo1Ry+GNp3mU/O6+5lH0uwU= X-Google-Smtp-Source: ABdhPJwalt0zZf8XqIOYmszt0wKVG0PFSngZjaGLadvBeWWEKU7P6ASD3nnVvG6+cbOi4j7qOb7PxA== X-Received: by 2002:a17:902:bd0a:b0:148:b21d:bf92 with SMTP id p10-20020a170902bd0a00b00148b21dbf92mr57955699pls.16.1641481550265; Thu, 06 Jan 2022 07:05:50 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id d1sm3174018pfu.91.2022.01.06.07.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 07:05:50 -0800 (PST) Received: by gnu-tgl-3.localdomain (Postfix, from userid 1000) id C34FCC0D76; Thu, 6 Jan 2022 07:05:48 -0800 (PST) Date: Thu, 6 Jan 2022 07:05:48 -0800 From: "H.J. Lu" To: Alan Modra Cc: binutils@sourceware.org Subject: [PATCH] ld: Add scan_relocs to ELF linker Message-ID: References: <20211229221431.530456-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3028.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Jan 2022 15:05:54 -0000 On Wed, Jan 05, 2022 at 10:13:29AM +1030, Alan Modra wrote: > On Wed, Dec 29, 2021 at 02:14:31PM -0800, H.J. Lu via Binutils wrote: > > bfd/ > > > > * elf-bfd.h (elf_backend_data): Add make_reloc_section. > > No, none of the elf-bfd.h, elflink.c, elfxx-target.h, bfdlink.h, > ldlang.c or any other general linker files need changing to support an > x86 separation of check_relocs into two pieces. You do not need new > linker infrastructure. You already have that capability by defining > an x86 specific LDEMUL_AFTER_ALLOCATION in elf-x86.em. Perhaps you > might need to duplicate elf_x86.em into two files, one for 32-bit and > one for 64-bit, or otherwise arrange to call different functions for > x86_64 and ix86. > > Doing the x86 DT_RELR support that way will make it easier to port the > support to other architectures. aarch64, arm, avr, csky, hppa, > m68hc1x, metag, nios2, ppc64, ppc32, all have existing > after_allocation functions that size stubs. You need something > similar to size the DT_RELR section, after all linker inputs are > seen and symbols resolved, and if you want to minimize DT_RELR size, > a preliminary layout can be done. It would also be possible to size > DT_RELR in before_allocation, but then you'd need to assume an > address/offset word is emitted whenever changing sections. That would > be entirely reasonable too. Either way, I don't see any need to > invent a new way of switching between target functions when you > already have that via LDEMUL_*. > Here is a patch to add scan_relocs to ELF linker. It is called in ldelf_before_allocation after rel_from_abs has been set on __ehdr_start. H.J. -- DT_RELR encodes consecutive R_*_RELATIVE relocations in GOT (the global offset table) in a compact format: https://groups.google.com/g/generic-abi/c/bX460iggiKg On some targets, R_*_RELATIVE relocations are counted and the GOT offsets are allocated when setting the dynamic section sizes after seeing all relocations. R_*_RELATIVE relocations are generated while relocating sections after section layout has been finalized. To prepare for DT_RELR implementation on these targets, add scan_relocs to ELF linker to scan ELF relocations before allocation so that when relocations are checked, all input sections have been mapped to output sections, dynamic symbols are known and R_*_RELATIVE relocations can be counted. ldelf_before_allocation is updated to call scan_relocs after rel_from_abs has been set on __ehdr_start. For x86 targets, the old check_relocs is renamed to scan_relocs and a new check_relocs is added to chek input sections and create dynamic relocation sections so that they will be mapped to output sections. Since relocations are scanned after __start, __stop, .startof. and .sizeof. symbols have been finalized on x86, __[start|stop]_SECNAME for --gc-sections -z start-stop-gc are now zero when all SECNAME sections been garbage collected. This is no need for elf_x86_start_stop_gc_p. bfd/ * elf-bfd.h (elf_backend_data): Add scan_relocs. * elf32-i386.c (elf_i386_convert_load_reloc): Don't call elf_x86_start_stop_gc_p. (elf_i386_check_relocs): Renamed to ... (elf_i386_scan_relocs): This. Don't call _bfd_elf_make_dynamic_reloc_section. (elf_backend_check_relocs): Removed. (elf_backend_scan_relocs): New. * elf64-x86-64.c (elf_x86_64_convert_load_reloc): Don't call elf_x86_start_stop_gc_p. (elf_x86_64_check_relocs): Renamed to ... (elf_x86_64_scan_relocs): This. Don't call _bfd_elf_make_dynamic_reloc_section. (elf_backend_check_relocs): Removed. (elf_backend_scan_relocs): New. * elflink.c (elf_link_check_or_scan_relocs): New. Renamed and modified from _bfd_elf_link_check_relocs. (_bfd_elf_link_check_relocs): New. (_bfd_elf_link_scan_relocs): Likewise. * elfxx-target.h (elf_backend_scan_relocs): New. (elfNN_bed): Add elf_backend_scan_relocs. * elfxx-x86.c (_bfd_x86_elf_check_relocs): New. * elfxx-x86.h (X86_64_NEED_DYNAMIC_RELOC_TYPE_P): New. (I386_NEED_DYNAMIC_RELOC_TYPE_P): Likewise. (X86_NEED_DYNAMIC_RELOC_TYPE_P): Likewise. (_bfd_x86_elf_check_relocs): Likewise. (elf_backend_check_relocs): Likewise. (elf_x86_start_stop_gc_p): Removed. ld/ * ldelf.c (ldelf_before_allocation): If the backend has scan_relocs, call _bfd_elf_link_scan_relocs after rel_from_abs has been set on __ehdr_start. * testsuite/ld-i386/pr27491-1a.d: Updated. * testsuite/ld-x86-64/pr27491-1a.d: Likewise. --- bfd/elf-bfd.h | 9 +++ bfd/elf32-i386.c | 40 ++++---------- bfd/elf64-x86-64.c | 37 +++---------- bfd/elflink.c | 36 ++++++++++-- bfd/elfxx-target.h | 4 ++ bfd/elfxx-x86.c | 85 +++++++++++++++++++++++++++++ bfd/elfxx-x86.h | 58 +++++++++----------- ld/ldelf.c | 18 +++++- ld/testsuite/ld-i386/pr27491-1a.d | 4 +- ld/testsuite/ld-x86-64/pr27491-1a.d | 4 +- 10 files changed, 193 insertions(+), 102 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 896aa08fd76..d42c3cf3ec9 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1104,6 +1104,13 @@ struct elf_backend_data (bfd *abfd, struct bfd_link_info *info, asection *o, const Elf_Internal_Rela *relocs); + /* The SCAN_RELOCS function is similar to the CHECK_RELOCS function. + But it is called before allocation from linker, instead of after + input files have been opened. */ + bool (*scan_relocs) + (bfd *abfd, struct bfd_link_info *info, asection *o, + const Elf_Internal_Rela *relocs); + /* The CHECK_DIRECTIVES function is called once per input file by the add_symbols phase of the ELF backend linker. The function must inspect the bfd and create any additional symbols according @@ -2632,6 +2639,8 @@ extern int bfd_elf_add_dt_needed_tag (bfd *, struct bfd_link_info *); extern bool _bfd_elf_link_check_relocs (bfd *, struct bfd_link_info *); +extern bool _bfd_elf_link_scan_relocs + (bfd *, struct bfd_link_info *); extern bool bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *, struct elf_link_hash_entry *); diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c index 97962072ff0..4b011f8f945 100644 --- a/bfd/elf32-i386.c +++ b/bfd/elf32-i386.c @@ -522,7 +522,7 @@ elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) Functions named elf_i386_* are called by external routines, other functions are only called locally. elf_i386_* functions appear in this file more or less in the order in which they are called - from external routines. eg. elf_i386_check_relocs is called + from external routines. eg. elf_i386_scan_relocs is called early in the link process, elf_i386_finish_dynamic_sections is one of the last functions. */ @@ -1106,7 +1106,7 @@ elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd, } /* We checked the transition before when we were called from - elf_i386_check_relocs. We only want to check the new + elf_i386_scan_relocs. We only want to check the new transition which hasn't been checked before. */ check = new_to_type != to_type && from_type == to_type; to_type = new_to_type; @@ -1387,11 +1387,6 @@ elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr, || h->root.type == bfd_link_hash_defweak) && local_ref)) { - /* Skip __start_SECNAME/__stop_SECNAME when --gc-sections - -z start-stop-gc are used. */ - if (elf_x86_start_stop_gc_p (link_info, h)) - return true; - convert_load: if (opcode == 0x8b) { @@ -1452,21 +1447,20 @@ elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr, } /* Look through the relocs for a section during the first phase, and - calculate needed space in the global offset table, procedure linkage - table, and dynamic reloc sections. */ + calculate needed space in the global offset table, and procedure + linkage table. */ static bool -elf_i386_check_relocs (bfd *abfd, - struct bfd_link_info *info, - asection *sec, - const Elf_Internal_Rela *relocs) +elf_i386_scan_relocs (bfd *abfd, + struct bfd_link_info *info, + asection *sec, + const Elf_Internal_Rela *relocs) { struct elf_x86_link_hash_table *htab; Elf_Internal_Shdr *symtab_hdr; struct elf_link_hash_entry **sym_hashes; const Elf_Internal_Rela *rel; const Elf_Internal_Rela *rel_end; - asection *sreloc; bfd_byte *contents; bool converted; @@ -1496,8 +1490,6 @@ elf_i386_check_relocs (bfd *abfd, converted = false; - sreloc = NULL; - rel_end = relocs + sec->reloc_count; for (rel = relocs; rel < rel_end; rel++) { @@ -1818,18 +1810,6 @@ elf_i386_check_relocs (bfd *abfd, struct elf_dyn_relocs *p; struct elf_dyn_relocs **head; - /* We must copy these reloc types into the output file. - Create a reloc section in dynobj and make room for - this reloc. */ - if (sreloc == NULL) - { - sreloc = _bfd_elf_make_dynamic_reloc_section - (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ false); - - if (sreloc == NULL) - goto error_return; - } - /* If this is a global symbol, we count the number of relocations we need for this symbol. */ if (h != NULL) @@ -2000,7 +1980,7 @@ elf_i386_relocate_section (bfd *output_bfd, bool is_vxworks_tls; unsigned plt_entry_size; - /* Skip if check_relocs failed. */ + /* Skip if check_relocs or scan_relocs failed. */ if (input_section->check_relocs_failed) return false; @@ -4390,7 +4370,7 @@ elf_i386_link_setup_gnu_properties (struct bfd_link_info *info) #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible -#define elf_backend_check_relocs elf_i386_check_relocs +#define elf_backend_scan_relocs elf_i386_scan_relocs #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections #define elf_backend_fake_sections elf_i386_fake_sections #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 22aa3ee3b68..3799f25ccd3 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -1309,7 +1309,7 @@ elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd, } /* We checked the transition before when we were called from - elf_x86_64_check_relocs. We only want to check the new + elf_x86_64_scan_relocs. We only want to check the new transition which hasn't been checked before. */ check = new_to_type != to_type && from_type == to_type; to_type = new_to_type; @@ -1628,11 +1628,6 @@ elf_x86_64_convert_load_reloc (bfd *abfd, || h->root.type == bfd_link_hash_defweak) && h->root.u.def.section == bfd_und_section_ptr)))) { - /* Skip __start_SECNAME/__stop_SECNAME when --gc-sections - -z start-stop-gc are used. */ - if (elf_x86_start_stop_gc_p (link_info, h)) - return true; - /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */ if (no_overflow) return true; @@ -1823,20 +1818,19 @@ elf_x86_64_convert_load_reloc (bfd *abfd, } /* Look through the relocs for a section during the first phase, and - calculate needed space in the global offset table, procedure - linkage table, and dynamic reloc sections. */ + calculate needed space in the global offset table, and procedure + linkage table. */ static bool -elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, - asection *sec, - const Elf_Internal_Rela *relocs) +elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, + asection *sec, + const Elf_Internal_Rela *relocs) { struct elf_x86_link_hash_table *htab; Elf_Internal_Shdr *symtab_hdr; struct elf_link_hash_entry **sym_hashes; const Elf_Internal_Rela *rel; const Elf_Internal_Rela *rel_end; - asection *sreloc; bfd_byte *contents; bool converted; @@ -1866,8 +1860,6 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, converted = false; - sreloc = NULL; - rel_end = relocs + sec->reloc_count; for (rel = relocs; rel < rel_end; rel++) { @@ -2263,19 +2255,6 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, struct elf_dyn_relocs *p; struct elf_dyn_relocs **head; - /* We must copy these reloc types into the output file. - Create a reloc section in dynobj and make room for - this reloc. */ - if (sreloc == NULL) - { - sreloc = _bfd_elf_make_dynamic_reloc_section - (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2, - abfd, /*rela?*/ true); - - if (sreloc == NULL) - goto error_return; - } - /* If this is a global symbol, we count the number of relocations we need for this symbol. */ if (h != NULL) @@ -2413,7 +2392,7 @@ elf_x86_64_relocate_section (bfd *output_bfd, unsigned int plt_entry_size; bool status; - /* Skip if check_relocs failed. */ + /* Skip if check_relocs or scan_relocs failed. */ if (input_section->check_relocs_failed) return false; @@ -5238,7 +5217,7 @@ elf_x86_64_special_sections[]= elf_x86_64_reloc_name_lookup #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible -#define elf_backend_check_relocs elf_x86_64_check_relocs +#define elf_backend_scan_relocs elf_x86_64_scan_relocs #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol diff --git a/bfd/elflink.c b/bfd/elflink.c index 553efa26232..1f0dc9f520a 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4008,10 +4008,14 @@ _bfd_elf_notice_as_needed (bfd *ibfd, return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); } -/* Check relocations an ELF object file. */ +/* Implementation of checking or scaning relocations in an ELF object + file. */ -bool -_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) +static bool +elf_link_check_or_scan_relocs + (bfd *abfd, struct bfd_link_info *info, + bool (*action) (bfd *, struct bfd_link_info *, asection *, + const Elf_Internal_Rela *)) { const struct elf_backend_data *bed = get_elf_backend_data (abfd); struct elf_link_hash_table *htab = elf_hash_table (info); @@ -4070,7 +4074,7 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) if (internal_relocs == NULL) return false; - ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); + ok = action (abfd, info, o, internal_relocs); if (elf_section_data (o)->relocs != internal_relocs) free (internal_relocs); @@ -4083,6 +4087,30 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) return true; } +/* Check relocations in an ELF object file. This is called after + all input files have been opened. */ + +bool +_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) +{ + const struct elf_backend_data *bed = get_elf_backend_data (abfd); + return elf_link_check_or_scan_relocs (abfd, info, bed->check_relocs); +} + +/* Scan relocations in an ELF object file. This is called before + allocation. */ + +bool +_bfd_elf_link_scan_relocs (bfd *abfd, struct bfd_link_info *info) +{ + const struct elf_backend_data *bed = get_elf_backend_data (abfd); + if (bed != NULL + && bed->scan_relocs != NULL) + return elf_link_check_or_scan_relocs (abfd, info, bed->scan_relocs); + else + return true; +} + /* Add symbols from an ELF object file to the linker hash table. */ static bool diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 360b056ff58..7b8ccd6814c 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -478,6 +478,9 @@ #ifndef elf_backend_check_relocs #define elf_backend_check_relocs 0 #endif +#ifndef elf_backend_scan_relocs +#define elf_backend_scan_relocs 0 +#endif #ifndef elf_backend_check_directives #define elf_backend_check_directives 0 #endif @@ -842,6 +845,7 @@ static const struct elf_backend_data elfNN_bed = elf_backend_omit_section_dynsym, elf_backend_relocs_compatible, elf_backend_check_relocs, + elf_backend_scan_relocs, elf_backend_check_directives, elf_backend_notice_as_needed, elf_backend_adjust_dynamic_symbol, diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index ca4b90e22cc..25f7717ea88 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -892,6 +892,91 @@ _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) return _bfd_elf_link_check_relocs (abfd, info); } +/* Look through the relocs for a section before allocation to make the + dynamic reloc section. */ + +bool +_bfd_x86_elf_check_relocs (bfd *abfd, + struct bfd_link_info *info, + asection *sec, + const Elf_Internal_Rela *relocs) +{ + struct elf_x86_link_hash_table *htab; + Elf_Internal_Shdr *symtab_hdr; + struct elf_link_hash_entry **sym_hashes; + const Elf_Internal_Rela *rel; + const Elf_Internal_Rela *rel_end; + asection *sreloc; + const struct elf_backend_data *bed; + bool is_x86_64; + + if (bfd_link_relocatable (info)) + return true; + + bed = get_elf_backend_data (abfd); + htab = elf_x86_hash_table (info, bed->target_id); + if (htab == NULL) + { + sec->check_relocs_failed = 1; + return false; + } + + is_x86_64 = bed->target_id == X86_64_ELF_DATA; + + symtab_hdr = &elf_symtab_hdr (abfd); + sym_hashes = elf_sym_hashes (abfd); + + rel_end = relocs + sec->reloc_count; + for (rel = relocs; rel < rel_end; rel++) + { + unsigned int r_type; + unsigned int r_symndx; + struct elf_link_hash_entry *h; + + r_symndx = htab->r_sym (rel->r_info); + r_type = ELF32_R_TYPE (rel->r_info); + + if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) + { + /* xgettext:c-format */ + _bfd_error_handler (_("%pB: bad symbol index: %d"), + abfd, r_symndx); + goto error_return; + } + + if (r_symndx < symtab_hdr->sh_info) + h = NULL; + else + { + h = sym_hashes[r_symndx - symtab_hdr->sh_info]; + while (h->root.type == bfd_link_hash_indirect + || h->root.type == bfd_link_hash_warning) + h = (struct elf_link_hash_entry *) h->root.u.i.link; + } + + if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type) + && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec, + r_type, htab->pointer_r_type)) + { + /* We may copy these reloc types into the output file. + Create a reloc section in dynobj and make room for + this reloc. */ + sreloc = _bfd_elf_make_dynamic_reloc_section + (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2, + abfd, sec->use_rela_p); + + if (sreloc != NULL) + return true; + + error_return: + sec->check_relocs_failed = 1; + return false; + } + } + + return true; +} + bool _bfd_elf_x86_valid_reloc_p (asection *input_section, struct bfd_link_info *info, diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h index 16565a5638b..927c5a59475 100644 --- a/bfd/elfxx-x86.h +++ b/bfd/elfxx-x86.h @@ -47,6 +47,25 @@ #define X86_SIZE_TYPE_P(IS_X86_64, TYPE) \ ((IS_X86_64) ? X86_64_SIZE_TYPE_P(TYPE) : I386_SIZE_TYPE_P (TYPE)) +#define X86_64_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \ + (X86_64_SIZE_TYPE_P (TYPE) \ + || X86_64_PCREL_TYPE_P (TYPE) \ + || (TYPE) == R_X86_64_8 \ + || (TYPE) == R_X86_64_16 \ + || (TYPE) == R_X86_64_32 \ + || (TYPE) == R_X86_64_32S \ + || (TYPE) == R_X86_64_64) +#define I386_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \ + (I386_SIZE_TYPE_P (TYPE) \ + || I386_PCREL_TYPE_P (TYPE) \ + || (TYPE) == R_386_32 \ + || (TYPE) == R_386_TLS_LE \ + || (TYPE) == R_386_TLS_LE_32) +#define X86_NEED_DYNAMIC_RELOC_TYPE_P(IS_X86_64, TYPE) \ + ((IS_X86_64) \ + ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \ + : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE)) + #define PLT_CIE_LENGTH 20 #define PLT_FDE_LENGTH 36 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8 @@ -653,6 +672,10 @@ extern int _bfd_x86_elf_compare_relocs extern bool _bfd_x86_elf_link_check_relocs (bfd *, struct bfd_link_info *); +extern bool _bfd_x86_elf_check_relocs + (bfd *, struct bfd_link_info *, asection *, + const Elf_Internal_Rela *); + extern bool _bfd_elf_x86_valid_reloc_p (asection *, struct bfd_link_info *, struct elf_x86_link_hash_table *, const Elf_Internal_Rela *, struct elf_link_hash_entry *, @@ -730,6 +753,8 @@ extern void _bfd_x86_elf_link_report_relative_reloc #define bfd_elf32_bfd_link_check_relocs \ _bfd_x86_elf_link_check_relocs +#define elf_backend_check_relocs \ + _bfd_x86_elf_check_relocs #define elf_backend_size_dynamic_sections \ _bfd_x86_elf_size_dynamic_sections #define elf_backend_always_size_sections \ @@ -757,39 +782,6 @@ extern void _bfd_x86_elf_link_report_relative_reloc #define ELF_P_ALIGN ELF_MINPAGESIZE -/* Return true if H is a __start_SECNAME/__stop_SECNAME symbol for the - SECNAME section which has been garbage collected by --gc-sections - -z start-stop-gc. */ - -static inline bool -elf_x86_start_stop_gc_p (struct bfd_link_info *link_info, - struct elf_link_hash_entry *h) -{ - if (h->start_stop - && link_info->gc_sections - && link_info->start_stop_gc) - { - asection *s = h->root.u.def.section; - - do - { - /* Return false if any SECNAME section is kept. */ - if (s->gc_mark) - return false; - s = bfd_get_next_section_by_name (s->owner, s); - } - while (s != NULL); - - /* Return true only if all SECNAME sections have been garbage - collected. */ - return true; - } - - /* Return false if H isn't a __start_SECNAME/__stop_SECNAME symbol or - --gc-sections or -z start-stop-gc isn't used. */ - return false; -} - /* Allocate x86 GOT info for local symbols. */ static inline bool diff --git a/ld/ldelf.c b/ld/ldelf.c index d15f027e91a..a1b3a2086e6 100644 --- a/ld/ldelf.c +++ b/ld/ldelf.c @@ -1566,6 +1566,7 @@ ldelf_before_allocation (char *audit, char *depaudit, const char *rpath; asection *sinterp; bfd *abfd; + const struct elf_backend_data *bed = NULL; struct bfd_link_hash_entry *ehdr_start = NULL; unsigned char ehdr_start_save_type = 0; char ehdr_start_save_u[sizeof ehdr_start->u @@ -1573,6 +1574,8 @@ ldelf_before_allocation (char *audit, char *depaudit, if (is_elf_hash_table (link_info.hash)) { + bed = get_elf_backend_data (link_info.output_bfd); + _bfd_elf_tls_setup (link_info.output_bfd, &link_info); /* Make __ehdr_start hidden if it has been referenced, to @@ -1591,8 +1594,6 @@ ldelf_before_allocation (char *audit, char *depaudit, || 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; @@ -1620,6 +1621,19 @@ ldelf_before_allocation (char *audit, char *depaudit, lang_for_each_statement (ldelf_find_statement_assignment); } + /* Scan relocations after rel_from_abs has been set on __ehdr_start. */ + if (bed != NULL && bed->scan_relocs != 0) + for (abfd = link_info.input_bfds; + abfd != (bfd *) NULL; abfd = abfd->link.next) + if (!_bfd_elf_link_scan_relocs (abfd, &link_info)) + { + /* No object output, fail return. */ + config.make_executable = false; + /* Note: we do not abort the loop, but rather + continue the scan in case there are other + bad relocations to report. */ + } + /* Let the ELF backend work out the sizes of any sections required by dynamic linking. */ rpath = command_line.rpath; diff --git a/ld/testsuite/ld-i386/pr27491-1a.d b/ld/testsuite/ld-i386/pr27491-1a.d index 006c17695c1..39b25f6507f 100644 --- a/ld/testsuite/ld-i386/pr27491-1a.d +++ b/ld/testsuite/ld-i386/pr27491-1a.d @@ -9,6 +9,6 @@ Disassembly of section .text: [a-f0-9]+ : - +[a-f0-9]+: 8b 83 ([0-9a-f]{2} ){4}[ \t]+mov +-0x[a-f0-9]+\(%ebx\),%eax - +[a-f0-9]+: 8b 83 ([0-9a-f]{2} ){4}[ \t]+mov +-0x[a-f0-9]+\(%ebx\),%eax + +[a-f0-9]+: c7 c0 00 00 00 00 mov \$0x0,%eax + +[a-f0-9]+: c7 c0 00 00 00 00 mov \$0x0,%eax #pass diff --git a/ld/testsuite/ld-x86-64/pr27491-1a.d b/ld/testsuite/ld-x86-64/pr27491-1a.d index ade5c6fa4f9..215124c6401 100644 --- a/ld/testsuite/ld-x86-64/pr27491-1a.d +++ b/ld/testsuite/ld-x86-64/pr27491-1a.d @@ -9,6 +9,6 @@ Disassembly of section .text: [a-f0-9]+ : - +[a-f0-9]+: 48 8b 05 ([0-9a-f]{2} ){4}[ \t]+mov +0x[a-f0-9]+\(%rip\),%rax[ \t]+# [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+> - +[a-f0-9]+: 48 8b 05 ([0-9a-f]{2} ){4}[ \t]+mov +0x[a-f0-9]+\(%rip\),%rax[ \t]+# [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+> + +[a-f0-9]+: 48 c7 c0 00 00 00 00 mov \$0x0,%rax + +[a-f0-9]+: 48 c7 c0 00 00 00 00 mov \$0x0,%rax #pass -- 2.33.1