Bug 26543 - bfd_generic_define_common_symbol does not calculate correct alignment when bfd_octets_per_byte > 1 and power_of_two == 0
Summary: bfd_generic_define_common_symbol does not calculate correct alignment when bf...
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.34
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-27 03:13 UTC by Tucker
Modified: 2020-08-28 15:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Patch implementing described fix (313 bytes, patch)
2020-08-27 03:13 UTC, Tucker
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tucker 2020-08-27 03:13:21 UTC
Created attachment 12803 [details]
Patch implementing described fix

bfd_generic_define_common_symbol does not calculate the correct alignment for targets with more than 1 octet per byte if alignment power is less than 1.

3103: linker.c
alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;

e.g. 
A target has 16-bit bytes and an alignment of 1 byte (2 octets). 

A common variable may be defined as .comm variable,1,1

bfd_log2(1) will calculate alignment_power == 0

Mathematically 2 ** 0 == 1 which would be correct but the above statement will evaluate to 2.

Instead the statement should check for a power_of_two == 0 condition i.e.
alignment = power_of_two ? bfd_octets_per_byte (output_bfd, section) << power_of_two : 1;
Comment 1 Sourceware Commits 2020-08-28 12:28:09 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1e597a89971e781f5c33d75009b2cbf26323d2a5

commit 1e597a89971e781f5c33d75009b2cbf26323d2a5
Author: Tuckker <tuckkern+sourceware@gmail.com>
Date:   Fri Aug 28 13:27:16 2020 +0100

    Prevent the linker from overestimating the alignment requirement of common symbols on targets with octets that are larger than one byte.
    
            PR 26543
            * linker.c (bfd_generic_define_common_symbol): Force the alignment
            to 1 if the section has now alignment requirement.
Comment 2 Nick Clifton 2020-08-28 12:29:03 UTC
Hi Tucker,

  Thanks for pointing out this problem.  I have checked in a patch
  to make the fix that you have suggested.

Cheers
  Nick
Comment 3 Tucker 2020-08-28 15:21:34 UTC
Nick,

No problem. Thanks for the quick response!

-Tucker