This example illustrates a simple use of execu in ARMv7.
Assumes you have a simple file contain some text to sort.
The man page indicates that placement of the pointer to the executable. In my example "/bin/sh" is the executable.
So you are looking for an array structure pointer at R0.
NAME
execve - execute program
SYNOPSIS
#include <unistd.h>
int execve(const char *filename, char *const argv[],
char *const envp[]);
DESCRIPTION
execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form:
#! interpreter [optional-arg]
For details of the latter case, see "Interpreter scripts" below.
argv is an array of argument strings passed to the new program. By convention, the first of these strings should contain the filename associated with the file being executed.
envp is an array of strings, conventionally of the form key=value, which are passed as environment to the new program. Both argv and envp must be terminated by a null pointer.
The argument vector and environment can be accessed by the called program's main function, when it is defined as:
int main(int argc, char *argv[], char *envp[])
execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded.
Sample code:
.data
_filename: .string "/bin/sh"
arg0: .string "/bin/sh"
arg1: .string "-c"
arg2: .string "sort -n myfile.txt"
args:
.word arg0
.word arg1
.word arg2
.text
.global main
main:
bl _work
_work:
push {lr}
mov r7, #11 // execve syscall
ldr r0,=_filename
ldr r1,=args
svc #0
pop {pc}
Simple text file:
$ cat myfile.txt
9
1
5
233
5
6
723
91
0
3
2
4576
557
6
353
3553
output example:
$ ./simple_exec
0
1
2
3
5
5
6
6
9
91
233
353
557
723
3553
4576
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…