Java Böy said:
could some body help me what's happening here...
In general, it's shoehorning a hand-coded x86 assembly subroutine into
a C program.
As Dan and others have pointed out, this code is not legal in ISO C.
However, depending on the compiler, you can get an executable that
does what you intend, but that's a happy accident more than anything
else.
thanks..
char sc[] =
"\x31\xc0" /* xor %eax, %eax */
"\x50" /* push %eax */
"\x68\x2f\x2f\x73\x68" /* push $0x68732f2f */
"\x68\x2f\x62\x69\x6e" /* push $0x6e69622f */
"\x89\xe3" /* mov %esp,%ebx */
"\x50" /* push %eax */
"\x53" /* push %ebx */
"\x89\xe1" /* mov %esp,%ecx */
"\x31\xd2" /* xor %edx,%edx */
"\xb0\x0b" /* mov $0xb,%al */
"\xcd\x80"; /* int $0x80 */
sc is being used to store the actual machine code for the operations
described in the comments (opcodes and data). Each "\x.." entry
represents a single byte. Each line of bytes consists correpsonds to
operation listed in the comment.
If you look at this array in a debugger you will see something like
sc+0: 31 c0 50 68 2f 2f 73 68
sc+7: 68 2f 62 69 6e 89 e3 50
....
main()
{
void (*fp) (void); // what is happening at this line
This declares fp as a pointer to a function returning void with no
parameters.
This assigns the address of sc to fp, allowing the bytes of the array
to be executed as a function by main(). Unfortunately, it's a bad
assignment for several reasons. First of all, the cast expression
isn't even correct; the type of fp isn't void *, it's void (*)(void).
Secondly, this type of conversion (from pointer to object to pointer
to function) isn't allowed under ISO C. It may have been allowed in
earlier implementations, and some compilers may still allow it if you
don't have strict conformance turned on.
And this calls the function.
[snip]