[PATCH v2] bfd: Fix undefined shift in common symbol alignment handling [PR binutils/34996]
Haolin Xue
xuehaolin@uniontech.com
Mon Nov 30 06:43:11 GMT 2026
PR binutils/34996
A common symbol (SHN_COMMON) with a very large or non-power-of-two
st_value can cause bfd_log2 to return 64, which then triggers
undefined behaviour when used as a shift count:
sym.st_value = 1 << h->root.u.c.p->alignment_power; /* elflink.c */
bfd_log2 (st_value) returns 64 for any st_value > 2^63, and
1 << 64 (or even 1 << 31 on 32-bit int) is undefined.
Cap the alignment result of bfd_log2 at 63 (the maximum useful
alignment for a 64-bit address space) so 64-bit shifts are safe,
and cast the left shift operands at the use sites to bfd_vma so
the cap is effective everywhere the alignment is consumed.
Guard the shifts in the alignment comparison warning code similarly.
A st_value of 0 is valid (means "no alignment requirement", per
sh_addralign semantics) and is not rejected --- bfd_log2 (0)
returns 0, which is correct. Large powers of two are also valid
and are not rejected.
PR binutils/34996
* elflink.c (elf_link_add_object_symbols): Cap alignment
from bfd_log2 to avoid undefined behaviour. Guard shifts
in the alignment comparison warning code.
(elf_link_output_extsym): Cast left shift operand to bfd_vma.
(bfd_elf_discard_info): Likewise.
* linker.c (bfd_generic_define_common_symbol): Cast left
shift operand to bfd_vma.
Signed-off-by: Haolin Xue <xuehaolin@uniontech.com>
---
bfd/elflink.c | 19 +++++++++++++++----
bfd/linker.c | 3 ++-
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 1eef868be49..e1a98c8962d 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -5469,6 +5469,13 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
We need to get the alignment from the section. */
align = new_sec->alignment_power;
}
+ /* PR binutils/34996: A common symbol with a very large or
+ non-power-of-two st_value can cause bfd_log2 to return 64,
+ which would be undefined behaviour when later used as a
+ shift count. Cap at the maximum useful alignment for a
+ 64-bit address space. */
+ if (align >= 8 * sizeof (bfd_vma))
+ align = 8 * sizeof (bfd_vma) - 1;
if (align > old_alignment)
h->root.u.c.p->alignment_power = align;
else
@@ -5598,8 +5605,12 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
if (normal_align < common_align)
{
/* PR binutils/2735 */
- uint64_t c_align = UINT64_C (1) << common_align;
- uint64_t n_align = UINT64_C (1) << normal_align;
+ /* PR binutils/34996: Guard against bfd_log2 returning 64
+ for a common symbol with a very large st_value. */
+ uint64_t c_align = common_align < 8 * sizeof (c_align)
+ ? UINT64_C (1) << common_align : 0;
+ uint64_t n_align = normal_align < 8 * sizeof (n_align)
+ ? UINT64_C (1) << normal_align : 0;
if (normal_bfd == NULL)
_bfd_error_handler
/* xgettext:c-format */
@@ -10989,7 +11000,7 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
case bfd_link_hash_common:
input_sec = h->root.u.c.p->section;
sym.st_shndx = bed->common_section_index (input_sec);
- sym.st_value = 1 << h->root.u.c.p->alignment_power;
+ sym.st_value = (bfd_vma) 1 << h->root.u.c.p->alignment_power;
break;
case bfd_link_hash_indirect:
@@ -15281,7 +15292,7 @@ bfd_elf_discard_info (struct bfd_link_info *info)
fini_reloc_cookie_for_section (&cookie, i);
}
- eh_alignment = ((1 << o->alignment_power)
+ eh_alignment = (((bfd_vma) 1 << o->alignment_power)
* bfd_octets_per_byte (info->output_bfd, o));
/* Skip over zero terminator, and prevent empty sections from
adding alignment padding at the end. */
diff --git a/bfd/linker.c b/bfd/linker.c
index c3dbe667cc9..7b4a04d693f 100644
--- a/bfd/linker.c
+++ b/bfd/linker.c
@@ -3169,7 +3169,8 @@ bfd_generic_define_common_symbol (bfd *output_bfd,
not have any alignment requirement then do not increase the
alignment unnecessarily. */
if (power_of_two)
- alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;
+ alignment = (bfd_vma) bfd_octets_per_byte (output_bfd, section)
+ << power_of_two;
else
alignment = 1;
BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
--
2.43.0
More information about the Binutils
mailing list