From 59ea52933d5a1f5e274b8fbe72183ff1a8043724 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 24 Oct 2022 06:46:57 +0800 Subject: [PATCH] [ELF] Honor section alignment when creating copy relocations --- elf/input-files.cc | 9 ++++----- test/elf/copyrel-alignment.sh | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/elf/input-files.cc b/elf/input-files.cc index 9993bf60..b3276792 100644 --- a/elf/input-files.cc +++ b/elf/input-files.cc @@ -1488,11 +1488,10 @@ std::vector *> SharedFile::find_aliases(Symbol *sym) { template i64 SharedFile::get_alignment(Symbol *sym) { ElfShdr &shdr = this->elf_sections[sym->esym().st_shndx]; - i64 p2align = std::min(std::countr_zero(sym->value), - std::countr_zero(shdr.sh_addralign)); - - // We do not want a ridiculously large alignment. Cap it arbitrary at 64. - return std::min(64, 1 << p2align); + i64 align = std::max(1, shdr.sh_addralign); + if (sym->value) + align = std::min(align, 1LL << std::countr_zero(sym->value)); + return align; } template diff --git a/test/elf/copyrel-alignment.sh b/test/elf/copyrel-alignment.sh index d0d11f2c..7f23af1e 100755 --- a/test/elf/copyrel-alignment.sh +++ b/test/elf/copyrel-alignment.sh @@ -32,4 +32,4 @@ readelf -W --sections $t/exe2 | grep -q '\.copyrel.* 8$' $CC -B. -o $t/exe3 $t/d.o $t/c.so -no-pie $QEMU $t/exe3 > /dev/null -readelf -W --sections $t/exe3 | grep -q '\.copyrel.* 64$' +readelf -W --sections $t/exe3 | grep -q '\.copyrel.* 256$'