I am uploading a simple statically linked ELF binary to MIPS (32-bit). After matching loadable segments, this is how I set up the stack before moving on to my ELF CRT target function _start:
__asm__(" \
addi $2, %[envN], 0 ;\
.env_loop: \
addi $2, $2, -4 ;\
lw $3, 0($2) ;\
addi $sp, $sp, -4 ;\
sw $3, 0($sp) ;\
bne $2, %[env0], .env_loop ;\
addi $2, %[argN], 0 ;\
.arg_loop: \
addi $2, $2, -4 ;\
lw $3, 0($2) ;\
addi $sp, $sp, -4 ;\
sw $3, 0($sp) ;\
bne $2, %[arg0], .arg_loop ;\
addi $2, %[argc], 0 ;\
addi $sp, $sp, -4 ;\
sw $2, 0($sp) ;\
addi $2, %[func], 0 ;\
jr $2 ;"
:
: [envN] "r" (envp + envc + 1),
[env0] "r" (envp),
[argN] "r" (argv + argc + 1),
[arg0] "r" (argv),
[argc] "r" ((int32_t)argc),
[func] "r" (entry_point)
: "$2", "$3", "cc", "memory"
);
So I push the environment variables, command line arguments, argcon the stack, and finally jump to the target ELF entry point. This works correctly, and I end up inside my loaded main program function with the correct command line arguments and all but one: it mallocdoes not work! Any call returns null to it and sets errno to ENOMEM.
MIPS, (qemu-system-mips), , , , . , ; x86, x86_64 arm, , - malloc MIPS .
-, ? -, , MIPS, , ? , , - , , .
Linux musl libc. , musl expand_heap, , , (musl malloc , ).
( libc; ), (printf, fopen ..) , -, malloc (, , realloc/calloc, , ). .