Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bn_mul.h: fix x86 PIC inline ASM compilation with GCC < 5 #1986

Conversation

jacmet
Copy link
Contributor

@jacmet jacmet commented Aug 27, 2018

Fixes #1910

With ebx added to the MULADDC_STOP clobber list to fix #1550, the inline
assembly fails to build with GCC < 5 in PIC mode with the following error:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+. From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets. This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, detect this situation and disable the inline assembly,
similar to the MULADDC_CANNOT_USE_R7 logic.

Signed-off-by: Peter Korsgaard peter@korsgaard.com

Status

READY

Requires Backporting

Yes
Backport to 2.28 is #6096

Todos

  • Tests
  • Documentation
  • Changelog updated
  • Backported

Steps to test or reproduce

Outline the steps to test or reproduce the PR here.

@jacmet
Copy link
Contributor Author

jacmet commented Aug 27, 2018

buildroot-auto-update pushed a commit to buildroot/buildroot that referenced this pull request Aug 28, 2018
Fixes:
http://autobuild.buildroot.net/results/d6d/d6dc9a640aa1f6650a3e7b9397f2fe2ae3433f4d/
http://autobuild.buildroot.net/results/ab5/ab5a58ea7845f9f378454ee1aa7e872448618ba9/

ebx was recently added to the x86 inline asm MULADDC_STOP clobber list to
fix #1550, but this causes the build to fail with GCC < 5 when building in
PIC mode with errors like:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+.  From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets.  This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, add a patch to detect this situation and disable the inline
assembly, similar to the MULADDC_CANNOT_USE_R7 logic.

Patch submitted upstream: Mbed-TLS/mbedtls#1986

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
woodsts pushed a commit to woodsts/buildroot that referenced this pull request Aug 28, 2018
Fixes:
http://autobuild.buildroot.net/results/d6d/d6dc9a640aa1f6650a3e7b9397f2fe2ae3433f4d/
http://autobuild.buildroot.net/results/ab5/ab5a58ea7845f9f378454ee1aa7e872448618ba9/

ebx was recently added to the x86 inline asm MULADDC_STOP clobber list to
fix #1550, but this causes the build to fail with GCC < 5 when building in
PIC mode with errors like:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+.  From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets.  This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, add a patch to detect this situation and disable the inline
assembly, similar to the MULADDC_CANNOT_USE_R7 logic.

Patch submitted upstream: Mbed-TLS/mbedtls#1986

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
(cherry picked from commit 11241ac)
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
woodsts pushed a commit to woodsts/buildroot that referenced this pull request Aug 28, 2018
Fixes:
http://autobuild.buildroot.net/results/d6d/d6dc9a640aa1f6650a3e7b9397f2fe2ae3433f4d/
http://autobuild.buildroot.net/results/ab5/ab5a58ea7845f9f378454ee1aa7e872448618ba9/

ebx was recently added to the x86 inline asm MULADDC_STOP clobber list to
fix #1550, but this causes the build to fail with GCC < 5 when building in
PIC mode with errors like:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+.  From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets.  This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, add a patch to detect this situation and disable the inline
assembly, similar to the MULADDC_CANNOT_USE_R7 logic.

Patch submitted upstream: Mbed-TLS/mbedtls#1986

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
(cherry picked from commit 11241ac)
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
@RonEld
Copy link
Contributor

RonEld commented Aug 28, 2018

@jacmet Thank you for your contribution!
Out of curiosity, do you think it's reasonable to support old GCC versions in our development branch?
I believe our LTS branches should probably have such a fix, but I'm not sure our development branch should.

In addition, unfortunately our policy is to not accept contributions, without a Contributor’s Licence Agreement (CLA) signed or authorised by yourself or your employer.

If this is a personal contribution, the easiest way to do this is if you create an mbed account and accept this click through agreement. Alternatively, you can find a slightly different agreement to sign here, which can be signed and returned to us, and is applicable if you don't want to create an mbed account or alternatively if this is a corporate contribution.

Thanks for your understanding and again, thanks for the contribution!

@RonEld RonEld added bug CLA requested component-crypto Crypto primitives and low-level interfaces labels Aug 28, 2018
@jacmet
Copy link
Contributor Author

jacmet commented Aug 28, 2018

@RonEld It may not make sense to support it for new development, that is up to you. On the other hand, it doesn't add a lot of overhead. For the LTS branch it IMHO certainly makes sense (E.G. this triggered by the security bump to 2.7.5)

I already accepted the CLA, but didn't have my email confirmed (done now).

@RonEld
Copy link
Contributor

RonEld commented Aug 28, 2018

@jacmet Thank you for your confirmation, and for accepting the CLA.
Could you confirm that this is you Mbed account?
Unfortunately, since github accounts and Mbed accounts are not linked together, we need to manually search the accounts by user names.
If this is your account, could you kindly make it public, so we could verify the CLA?

@jacmet
Copy link
Contributor Author

jacmet commented Aug 28, 2018

@RonEld correct, and I have now made the account info public

@RonEld
Copy link
Contributor

RonEld commented Aug 28, 2018

Thanks again for accepting the CLA!

@wyattoday
Copy link

Just putting in my 2-cents: I believe this patch should be applied to the development branch, because otherwise we would need to make this change manually on the older platforms we support. I know this is working around a bug in an old gcc, and that it's not necessarily in a project's best interest to support every compiler version under the sun, however I thought I would let you know in case you were wondering whether "real people" needed this change. We do. And without this fix merged to development we will need to make this change manually when upgrading to newer versions of mbedTLS (like we did with mbedTLS 2.12.0)

Again, this isn't a showstopper for us, but it does add extra work when upgrading mbedTLS.

@RonEld
Copy link
Contributor

RonEld commented Aug 28, 2018

@wyattoday Thank you for informing us your use case. We will take that into consideration.
Is updating your gcc version for your older platforms as well an option for you?

@jacmet
Copy link
Contributor Author

jacmet commented Aug 28, 2018

@RonEld I see travis fails on gcc, but don't quite understand the relation to the change (unless something is broken with the non-asm version now used for gcc < 5). Any idea?

@wyattoday
Copy link

wyattoday commented Aug 28, 2018

@RonEld

Is updating your gcc version for your older platforms as well an option for you?

Unfortunately not. Not yet, at least. And not on some platforms. For example we release binaries that work "out of the box" on CentOS 5 and CentOS 6 (Redhat 5 / 6), which means we need to use the built-in glibc on the system (and thus use the built-in gcc on the system). Both CentOS 5 and CentOS 6 (RH 5 / 6) use gcc versions older than gcc 5.

CentOS 5 is admittedly old (and RH 5 only has 2 years and 2 months of "Extended life-cycle support" left). But CentOS 6 is still widely used by our customers and Redhat still supports RH 6 in the "Maintenance Support 2" cycle for the next 2 years 2 months (and Extended life-cycle of RH 6 is for the next 6 years).

@RonEld
Copy link
Contributor

RonEld commented Aug 28, 2018

@wyattoday Thank you for your detailed use case. It sounds like a legitimate use case. WE will consider it.

@jacmet I tried reproducing this failure on my machine, using gcc 4.8 , and I didn't encounter the failure. Of course, there could be other environmental differences between my machine and Travis CI which I overlooked.
At the moment, I believe it might be some timing issue.

@tsl0922
Copy link

tsl0922 commented Oct 17, 2018

Thank you! This patch helped me to build mbedtls for openwrt x86 based on the 14.07 SDK with gcc-4.8.

Caesar-github pushed a commit to Caesar-github/buildroot that referenced this pull request Sep 26, 2021
Fixes:
http://autobuild.buildroot.net/results/d6d/d6dc9a640aa1f6650a3e7b9397f2fe2ae3433f4d/
http://autobuild.buildroot.net/results/ab5/ab5a58ea7845f9f378454ee1aa7e872448618ba9/

ebx was recently added to the x86 inline asm MULADDC_STOP clobber list to
fix #1550, but this causes the build to fail with GCC < 5 when building in
PIC mode with errors like:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+.  From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets.  This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, add a patch to detect this situation and disable the inline
assembly, similar to the MULADDC_CANNOT_USE_R7 logic.

Patch submitted upstream: Mbed-TLS/mbedtls#1986

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
Change-Id: Ia40b9b04f19540e95d9797e7bba826b740178daa
@mpg mpg added the historical-reviewing Currently reviewing (for legacy PR/issues) label Feb 24, 2022
@mpg
Copy link
Contributor

mpg commented Feb 24, 2022

Thank you for your contribution, and sorry for the very long period of inactivity on our side. We're going over our backlog of PRs, and were wondering if this is still relevant to you? If it is, can you rebase your PR on top of recent development, and we'll be sure to review quickly. Otherwise, this can be closed as the relevance of GCC <5 will only decline over time.

@jacmet
Copy link
Contributor Author

jacmet commented Feb 24, 2022

@mpg We're still carrying this patch in Buildroot, so it is still relevant for us: https://git.buildroot.net/buildroot/tree/package/mbedtls/0001-bn_mul.h-fix-x86-PIC-inline-ASM-compilation-with-GCC.patch

@wyattoday
Copy link

@mpg This no longer impacts us. We've dropped support for unsupported platforms and compilers. We now use CentOS 7 with the latest devtoolset (which at the time of writing is gcc 11). This lets us target ancient glibc while using modern compilers.

@jacmet
Copy link
Contributor Author

jacmet commented Feb 24, 2022

@mpg Buildroot so far still supports GCC 4.x (host/target)

@wyattoday
Copy link

@jacmet

Even the Linux kernel has moved away from gcc 4.x (recently requiring 5.1 and newer to build it).

Just FYI in case you were hanging on to ancient versions for compatibility reasons.

@mpg
Copy link
Contributor

mpg commented Mar 16, 2022

@jacmet Thanks for letting us know. This PR has a conflict, can you rebase in order to resolve it? If you do so, we'll be sure to review quickly so that this can finally be merged. (As a bugfix, it will also need a backport to the mbedtls-2.28 branch.)

@mckaygerhard
Copy link

mckaygerhard commented May 2, 2022

I forgot about this, I was too busy, this patch must be apply .. yet many devices still use gcc 4.X for example embedded... this patch should still be supported because linux kernels like 4.X are still in use... for devices that can't use 5.X as an example, i cannot rebase, cos i used older devices (have a lot of those with large life and i will not change cos represent a cost)

i will try next weekend if there's no responses here for #1910

@tom-cosgrove-arm
Copy link
Contributor

@jacmet Are you able to rebase this PR if it is still wanted? Or do you mind if someone else does it?

@tom-cosgrove-arm tom-cosgrove-arm self-assigned this Jul 8, 2022
Fixes Mbed-TLS#1910

With ebx added to the MULADDC_STOP clobber list to fix Mbed-TLS#1550, the inline
assembly fails to build with GCC < 5 in PIC mode with the following error:

include/mbedtls/bn_mul.h:46:13: error: PIC register clobbered by ‘ebx’ in ‘asm’

This is because older GCC versions treated the x86 ebx register (which is
used for the GOT) as a fixed reserved register when building as PIC.

This is fixed by an improved register allocator in GCC 5+.  From the release
notes:

Register allocation improvements: Reuse of the PIC hard register, instead of
using a fixed register, was implemented on x86/x86-64 targets.  This
improves generated PIC code performance as more hard registers can be used.

https://www.gnu.org/software/gcc/gcc-5/changes.html

As a workaround, detect this situation and disable the inline assembly,
similar to the MULADDC_CANNOT_USE_R7 logic.

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
@tom-cosgrove-arm tom-cosgrove-arm force-pushed the bn_mul-fix-x86-pic-compilation-for-gcc-4 branch from a0ae2ba to c0546e3 Compare July 18, 2022 16:33
@tom-cosgrove-arm
Copy link
Contributor

I have rebased this PR on top of latest development, and checked that (1) without it, i386 gcc 4 still gives the error and (2) with it, i386 gcc 4 no longer gives the error

mpg
mpg previously approved these changes Jul 19, 2022
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks correct to me (at least in the sense that it's reasonable and should not affect other platforms). As discussed elsewhere, probably needs a ChangeLog entry and a backport, considering it's a bugfix.

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
@tom-cosgrove-arm tom-cosgrove-arm added needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review historical-reviewed Reviewed & agreed to keep legacy PR/issue and removed historical-reviewing Currently reviewing (for legacy PR/issues) labels Jul 19, 2022
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@daverodgman daverodgman added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review labels Jul 20, 2022
@daverodgman daverodgman added the priority-low Low priority - this may not receive review soon label Jul 20, 2022
@mpg mpg added needs-ci Needs to pass CI tests and removed needs-backports Backports are missing or are pending review and approval. labels Jul 21, 2022
@daverodgman daverodgman merged commit a948f05 into Mbed-TLS:development Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug component-crypto Crypto primitives and low-level interfaces historical-reviewed Reviewed & agreed to keep legacy PR/issue needs-ci Needs to pass CI tests priority-low Low priority - this may not receive review soon
Projects
None yet
8 participants