Bug 256899

Summary: ELF image activator does not like thumb entry points
Product: Base System Reporter: Robert Clausecker <fuz>
Component: armAssignee: freebsd-arm (Nobody) <freebsd-arm>
Status: Closed FIXED    
Severity: Affects Only Me CC: cognet, manu
Priority: ---    
Version: 13.0-RELEASE   
Hardware: arm   
OS: Any   

Description Robert Clausecker 2021-06-30 10:31:01 UTC
An ARM ELF binary with an odd entry point should start the binary in Thumb mode.  However, this does not seem to be the case, forcing the entry point to be in ARM mode.  To reproduce, copy the following code to a file named test.s:

    	.arch armv7a
	.syntax unified
	.thumb
	.thumb_func
	.globl _start
_start:	movs r0, #0
	movs r7, #1
	svc #0

Then assemble and link:

    as -o test.o test.s
    ld -o test test.o

And observe the program crashing on start with a segmentation fault or invalid instruction exception.  Expected behaviour is that the program terminates with exit status 0.
Comment 1 Emmanuel Vadot freebsd_committer 2021-06-30 11:36:27 UTC
Can't reproduce on 14-CURRENT here (on armv7).
On what version of FreeBSD are you seeing this ?
Comment 2 Robert Clausecker 2021-06-30 11:59:08 UTC
Hi, I'm trying this on 13.0-RELEASE.  If this is fixed in 14-CURRENT, that's great news!  Perhaps consider merging the fix for this into 13-STABLE for 13.1.
Comment 3 Emmanuel Vadot freebsd_committer 2021-06-30 12:06:15 UTC
I'm not sure that any patches fixed that.
On what hardware was this tested ?
Do you build yourself or are you using the release tarball/image ?
Comment 4 Robert Clausecker 2021-06-30 12:21:13 UTC
(In reply to Emmanuel Vadot from comment #3)

I installed from release tarballs.  uname says:

FreeBSD tau.fuz.su 13.0-RELEASE FreeBSD 13.0-RELEASE #0 releng/13.0-n244733-ea31abc261f: Fri Apr  9 03:54:53 UTC 2021     root@releng1.nyi.freebsd.org:/usr/obj/usr/src/arm64.aarch64/sys/GENERIC  arm64
Comment 5 Emmanuel Vadot freebsd_committer 2021-06-30 12:24:24 UTC
(In reply to Robert Clausecker from comment #4)

Ah so it's arm64 not arm.
This wasn't clear.
So yeah this isn't fixed in main.
Comment 6 commit-hook freebsd_committer 2021-06-30 12:56:33 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=712c060c94fd447c91b0e6218c12a431206b487a

commit 712c060c94fd447c91b0e6218c12a431206b487a
Author:     Olivier Houchard <cognet@FreeBSD.org>
AuthorDate: 2021-06-30 12:50:47 +0000
Commit:     Olivier Houchard <cognet@FreeBSD.org>
CommitDate: 2021-06-30 12:55:18 +0000

    arm64: Make sure COMPAT_FREEBSD32 handles thumb entry point.

    If the entry point for the binary executed is a thumb 2 entry point, make
    sure we set the PSR_T bit, or the CPU will interpret it as arm32 code and
    bad things will happen.

    PR: 256899
    MFC after: 1 week

 sys/arm64/arm64/elf32_machdep.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 7 Olivier Houchard freebsd_committer 2021-06-30 12:58:41 UTC
Hi Robert,

Any chance you can test that patch ? (712c060c94fd447c91b0e6218c12a431206b487a).
I believe it may fix the issue.

Thanks !
Comment 8 Robert Clausecker 2021-06-30 13:03:21 UTC
(In reply to Emmanuel Vadot from comment #5)

Oh sorry, I was also able to reproduce this on armv7 on a machine with this uname:

FreeBSD mochi 12.2-RELEASE-p4 FreeBSD 12.2-RELEASE-p4 142f0d36d909(releng/12.2) GENERIC  arm

It's a bit hard to update this machine (insufficient RAM to make world, no freebsd-update) so it's stuck on this slightly outdated version.

But yeah, perhaps it only affects armv7 binaries on arm64.
Comment 9 Robert Clausecker 2021-06-30 13:08:28 UTC
(In reply to Olivier Houchard from comment #7)

Hi Olivier, if the patch applies to 13.0-RELEASE, I can try to test it.  Unfortunately my only arm64 machine is in production use, so I would like to avoid installing CURRENT.  And I think bhyve for arm64 isn't a thing yet.
Comment 10 Olivier Houchard freebsd_committer 2021-06-30 13:09:45 UTC
(In reply to Robert Clausecker from comment #9)

I think it should apply on 13.0 fine.
Comment 11 Robert Clausecker 2021-06-30 14:13:12 UTC
Patch confirmed to work on FreeBSD 13.0-RELEASE.
Comment 12 commit-hook freebsd_committer 2021-06-30 21:00:58 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8c3bd133dd52824e427e350c65eae1fd9eb5a3cd

commit 8c3bd133dd52824e427e350c65eae1fd9eb5a3cd
Author:     Olivier Houchard <cognet@FreeBSD.org>
AuthorDate: 2021-06-30 20:56:50 +0000
Commit:     Olivier Houchard <cognet@FreeBSD.org>
CommitDate: 2021-06-30 20:56:50 +0000

    arm: Make sure we can handle a thumb entry point.

    Similarly to what's been done on arm64 with commit
    712c060c94fd447c91b0e6218c12a431206b487a, when executing a binary, if the
    entry point is a thumb symbol, then make sure we set the PSL_T flag, otherwise
    the CPU will interpret it in ARM mode, and that will likely leads to an
    undefined instruction.

    PR:     256899
    MFC after:      1 week

 sys/arm/arm/machdep.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 13 Robert Clausecker 2021-07-05 14:35:58 UTC
(In reply to Olivier Houchard from comment #10)

Is there any work left to do on this one?
Comment 14 Olivier Houchard freebsd_committer 2021-07-05 14:47:25 UTC
(In reply to Robert Clausecker from comment #13)
Just backporting the patches to stable branchs
Comment 15 commit-hook freebsd_committer 2021-07-07 20:24:11 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=82a1d67f018c7178485166ae3e06f29bff292263

commit 82a1d67f018c7178485166ae3e06f29bff292263
Author:     Olivier Houchard <cognet@FreeBSD.org>
AuthorDate: 2021-06-30 20:56:50 +0000
Commit:     Olivier Houchard <cognet@FreeBSD.org>
CommitDate: 2021-07-07 20:22:41 +0000

    arm: Make sure we can handle a thumb entry point.

    Similarly to what's been done on arm64 with commit
    712c060c94fd447c91b0e6218c12a431206b487a, when executing a binary, if the
    entry point is a thumb symbol, then make sure we set the PSL_T flag, otherwise
    the CPU will interpret it in ARM mode, and that will likely leads to an
    undefined instruction.

    PR:     256899
    MFC after:      1 week

    (cherry picked from commit 8c3bd133dd52824e427e350c65eae1fd9eb5a3cd)
    Signed-off-by: Olivier Houchard <cognet@FreeBSD.org>

 sys/arm/arm/machdep.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 16 commit-hook freebsd_committer 2021-07-07 20:24:12 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=d2e48ad87d3e0f63f4ac73c524fb18afce4a2d53

commit d2e48ad87d3e0f63f4ac73c524fb18afce4a2d53
Author:     Olivier Houchard <cognet@FreeBSD.org>
AuthorDate: 2021-06-30 12:50:47 +0000
Commit:     Olivier Houchard <cognet@FreeBSD.org>
CommitDate: 2021-07-07 20:22:18 +0000

    arm64: Make sure COMPAT_FREEBSD32 handles thumb entry point.

    If the entry point for the binary executed is a thumb 2 entry point, make
    sure we set the PSR_T bit, or the CPU will interpret it as arm32 code and
    bad things will happen.

    PR: 256899
    MFC after: 1 week

    (cherry picked from commit 712c060c94fd447c91b0e6218c12a431206b487a)
    Signed-off-by: Olivier Houchard <cognet@FreeBSD.org>

 sys/arm64/arm64/elf32_machdep.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 17 commit-hook freebsd_committer 2021-07-07 20:26:13 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9bc7bbcf214b9e15b67e42706cef770ede541913

commit 9bc7bbcf214b9e15b67e42706cef770ede541913
Author:     Olivier Houchard <cognet@FreeBSD.org>
AuthorDate: 2021-06-30 20:56:50 +0000
Commit:     Olivier Houchard <cognet@FreeBSD.org>
CommitDate: 2021-07-07 20:24:56 +0000

    arm: Make sure we can handle a thumb entry point.

    Similarly to what's been done on arm64 with commit
    712c060c94fd447c91b0e6218c12a431206b487a, when executing a binary, if the
    entry point is a thumb symbol, then make sure we set the PSL_T flag, otherwise
    the CPU will interpret it in ARM mode, and that will likely leads to an
    undefined instruction.

    PR:     256899
    MFC after:      1 week

    (cherry picked from commit 8c3bd133dd52824e427e350c65eae1fd9eb5a3cd)
    Signed-off-by: Olivier Houchard <cognet@FreeBSD.org>

 sys/arm/arm/machdep.c | 2 ++
 1 file changed, 2 insertions(+)