From: Xi Ruoyao <xry111@xry111.site>
To: binutils@sourceware.org
Cc: liuzhensong <liuzhensong@loongson.cn>,
Lulu Cheng <chenglulu@loongson.cn>, Wang Xuerui <i@xen0n.name>,
Chenghua Xu <xuchenghua@loongson.cn>,
Xi Ruoyao <xry111@xry111.site>
Subject: [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs
Date: Tue, 13 Sep 2022 23:44:14 +0800 [thread overview]
Message-ID: <20220913154414.554861-3-xry111@xry111.site> (raw)
In-Reply-To: <20220913154414.554861-1-xry111@xry111.site>
loongarch_elf_finish_dynamic_symbol is called after elf_link_sort_relocs
if -z combreloc. elf_link_sort_relocs redistributes the contents of
.rela.* sections those would be merged into .rela.dyn, so the slot for
R_LARCH_IRELATIVE may be out of relplt->contents now.
To make things worse, the boundary check
dyn < dyn + relplt->size / sizeof (*dyn)
is obviously wrong ("x + 10 < x"? :), causing the issue undetected
during the linking process and the resulted executable suddenly crashes
at runtime.
The issue was found during an attempt to add static-pie support to the
toolchain.
Fix it by iterating through the inputs of .rela.dyn to find the slot.
---
bfd/elfnn-loongarch.c | 41 ++++++++++++++++++++++++-----------------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 4b408b1db72..1de9dbfecfa 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -3511,6 +3511,12 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd,
{
struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
+ asection *rela_dyn = bfd_get_section_by_name (output_bfd, ".rela.dyn");
+ struct bfd_link_order *lo = NULL;
+ Elf_Internal_Rela *slot = NULL, *last_slot = NULL;
+
+ if (rela_dyn)
+ lo = rela_dyn->map_head.link_order;
if (h->plt.offset != MINUS_ONE)
{
@@ -3520,6 +3526,7 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd,
uint32_t plt_entry[PLT_ENTRY_INSNS];
bfd_byte *loc;
Elf_Internal_Rela rela;
+ asection *rela_sec = NULL;
if (htab->elf.splt)
{
@@ -3572,31 +3579,31 @@ loongarch_elf_finish_dynamic_symbol (bfd *output_bfd,
&& (relplt == htab->elf.srelgot
|| relplt == htab->elf.irelplt))
{
- {
- rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE);
- rela.r_addend = (h->root.u.def.value
+ rela.r_info = ELFNN_R_INFO (0, R_LARCH_IRELATIVE);
+ rela.r_addend = (h->root.u.def.value
+ h->root.u.def.section->output_section->vma
+ h->root.u.def.section->output_offset);
- }
- /* Find the space after dyn sort. */
+ /* Find the space after dyn sort. */
+ while (slot == last_slot || slot->r_offset != 0)
{
- Elf_Internal_Rela *dyn = (Elf_Internal_Rela *)relplt->contents;
- bool fill = false;
- for (;dyn < dyn + relplt->size / sizeof (*dyn); dyn++)
+ if (slot != last_slot)
{
- if (0 == dyn->r_offset)
- {
- bed->s->swap_reloca_out (output_bfd, &rela,
- (bfd_byte *)dyn);
- relplt->reloc_count++;
- fill = true;
- break;
- }
+ slot++;
+ continue;
}
- BFD_ASSERT (fill);
+
+ BFD_ASSERT (lo != NULL);
+ rela_sec = lo->u.indirect.section;
+ lo = lo->next;
+
+ slot = (Elf_Internal_Rela *)rela_sec->contents;
+ last_slot = (Elf_Internal_Rela *)(rela_sec->contents +
+ rela_sec->size);
}
+ bed->s->swap_reloca_out (output_bfd, &rela, (bfd_byte *)slot);
+ rela_sec->reloc_count++;
}
else
{
--
2.37.0
next prev parent reply other threads:[~2022-09-13 15:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-13 15:44 [PATCH 0/2] LoongArch: Fix two bugs breaking IFUNC in static PIE Xi Ruoyao
2022-09-13 15:44 ` [PATCH 1/2] LoongArch: Avoid heap-buffer-overflow in loongarch_elf_relocate_section Xi Ruoyao
2022-09-14 8:57 ` liuzhensong
2022-09-14 10:15 ` Xi Ruoyao
2022-09-14 11:15 ` Xi Ruoyao
2022-09-15 1:47 ` liuzhensong
2022-09-15 2:56 ` Xi Ruoyao
2022-09-15 3:54 ` liuzhensong
2022-09-15 6:13 ` Xi Ruoyao
2022-09-13 15:44 ` Xi Ruoyao [this message]
2022-09-15 13:03 ` [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs liuzhensong
2022-09-16 20:11 ` Xi Ruoyao
2022-09-18 9:58 ` Xi Ruoyao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220913154414.554861-3-xry111@xry111.site \
--to=xry111@xry111.site \
--cc=binutils@sourceware.org \
--cc=chenglulu@loongson.cn \
--cc=i@xen0n.name \
--cc=liuzhensong@loongson.cn \
--cc=xuchenghua@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).