문제

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