Skip to contentSkip to conversation

linux-user: Fix loading static ARM cortex-m55 binaries

PATCH In review Helge Deller 3 participants 5 messages May 25 – May 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix loading static ARM cortex-m55 binaries

From: Helge Deller <[email protected]>

Static built ARM binaries for Cortex-m55 may have been linked to have their load address at address 0. When qemu-user is running as non-root user and thus will try to mmap() a host address which is smaller than mmap_min_addr (/proc/sys/vm/mmap_min_addr), it will fail with EPERM and as such loading those guest programs will fail.

Avoid this EPERM failure by automatically choosing a valid starting guest_base address which has to be higher than mmap_min_addr.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1890

---
 linux-user/elfload.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
linux-user/elfload.c +3 -2
index 0e757787d2..152c122a23 100644
@@ -1033,8 +1033,9 @@ static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
     uintptr_t brk, ret;
     PGBAddrs ga;
 
-    /* Try the identity map first. */
-    if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
+    /* Try the identity map first if guest_loadaddr is above mmap_min_addr. */
+    if (guest_loaddr >= mmap_min_addr &&
+        pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
         brk = (uintptr_t)sbrk(0);
         if (pgb_try_mmap_set(&ga, 0, brk)) {
             guest_base = 0;
Signature · 1 line
2.54.0
#1 lore raw
Quoted 9 lines · Helge Deller

From: Helge Deller <[email protected]>

Static built ARM binaries for Cortex-m55 may have been linked to have their load address at address 0. When qemu-user is running as non-root user and thus will try to mmap() a host address which is smaller than mmap_min_addr (/proc/sys/vm/mmap_min_addr), it will fail with EPERM and as such loading those guest programs will fail.

Quoted 5 lines

Avoid this EPERM failure by automatically choosing a valid starting guest_base address which has to be higher than mmap_min_addr.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1890

I just put a comment in this issue, but the reason we don't load this binary is partly because it is not a Linux binary at all, it is a bare-metal image. So the extent to which we can run it at all is a mix of luck and "we like to let the GCC developers be able to run their semihosting test binaries on qemu-user".

Quoted 9 lines
---
 linux-user/elfload.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
linux-user/elfload.c +0 -0
index 0e757787d2..152c122a23 100644

linux-user/elfload.c static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr, · line 1038

Quoted 8 lines
linux-user/elfload.c +3 -2
@@ -1033,8 +1033,9 @@ static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
     uintptr_t brk, ret;
     PGBAddrs ga;
 
-    /* Try the identity map first. */
-    if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
+    /* Try the identity map first if guest_loadaddr is above mmap_min_addr. */
+    if (guest_loaddr >= mmap_min_addr &&
+        pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {

pgb_addr_set() is supposed to return false if the identity map isn't suitable, so I feel like it would be better to have it get the answer right rather than work around it giving us the wrong answer in this particular case.

thanks
-- PMM

#2 lore raw
In reply to Helge Deller
Quoted 4 lines · Helge Deller

From: Helge Deller <[email protected]>

Static built ARM binaries for Cortex-m55 may have been linked to have their load address at address 0.

This is a problem. We also use 0 for "unassigned" a-la ET_DYN.

Perhaps ideally we'd pass around a separate flag for ET_EXEC vs ET_DYN, but ET_EXEC with 0 is not a valid binary precisely because it attempts to map at the zero page.

Honestly, I think this is user error. While we support some m-profile binaries in linux-user mode which the Linux kernel does not, for the explicit benefit of the GCC testsuite, there are limits.

r~

#3 lore raw
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Helge Deller

From: Helge Deller <[email protected]>

Static built ARM binaries for Cortex-m55 may have been linked to have their load address at address 0.

Quoted 8 lines

This is a problem. We also use 0 for "unassigned" a-la ET_DYN.

Perhaps ideally we'd pass around a separate flag for ET_EXEC vs ET_DYN, but ET_EXEC with 0 is not a valid binary precisely because it attempts to map at the zero page.

Honestly, I think this is user error. While we support some m-profile binaries in linux-user mode which the Linux kernel does not, for the explicit benefit of the GCC testsuite, there are limits.

But if the user explicitly passes a guest-base address on their QEMU command line, we run the binary happily. If we refused to run the binary at all that would be one thing (and not unreasonable, as you say). But it does seem to me like a problem in our "pick a guest-base" logic that we fail to identify one that would work fine for this particular binary.

thanks
-- PMM

#4 lore raw
Quoted 18 lines · Peter Maydell
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Helge Deller

From: Helge Deller <[email protected]>

Static built ARM binaries for Cortex-m55 may have been linked to have their load address at address 0.

Quoted 8 lines

This is a problem. We also use 0 for "unassigned" a-la ET_DYN.

Perhaps ideally we'd pass around a separate flag for ET_EXEC vs ET_DYN, but ET_EXEC with 0 is not a valid binary precisely because it attempts to map at the zero page.

Honestly, I think this is user error. While we support some m-profile binaries in linux-user mode which the Linux kernel does not, for the explicit benefit of the GCC testsuite, there are limits.

Quoted 6 lines

But if the user explicitly passes a guest-base address on their QEMU command line, we run the binary happily. If we refused to run the binary at all that would be one thing (and not unreasonable, as you say). But it does seem to me like a problem in our "pick a guest-base" logic that we fail to identify one that would work fine for this particular binary.

Then we need to do the work to track PIE separately, without overloading 0.

r~