I am a newbie so I could be inacurate with expressions. I need to make a system call "execve" in C with assembler. I don't use any libraries. Part that doesn't work is

char *nul=(char *)0;
char *argv[] = { "/bin/date", nul };
char *envp[] = { nul };
long ret;
asm volatile ("int $0x80" : "=a" (ret) : "a" (11), "b" (argv[0]), "c" (argv), "d" (envp));
//"a" (11) ... 11 correspondes to execve

I compile the code (and get neither errors nor warnings) with

gcc -m32 -nostdlib -nostdinc -static -O2 sysc.c -o sysc

When I try to run the program I see this message:

A NULL argv[0] was passed through an exec system call.
Aborted
有帮助吗?

解决方案

If you look at the generated code, you'll see that the compiler optimized away the initialization of argv and envp, on the assumption that your asm block is not accessing them (since you only declare that you need the pointers themselves).

Solution: add a "memory" clobber to tell the compiler that you may read or write any memory.

其他提示

The arguments to the exec() family of functions are a bit strange.

In particular, arg0 and arg1 are roughly same thing:

char *args [] = {"./path/to/program", "arg1", "arg2", NULL};

int rc = execve (args[0], args, envp);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top