系统调用宏有点难以理解。 我花了一段时间才弄清楚宏 syscall1(int,setuid,uid_t,uid) 如何扩展到显示的汇编代码。 如果能展示宏,并稍微解释一下它是如何扩展的,那就更好了。 这是 _syscall1 宏的源代码
#define _syscall1(type,name,type1,arg1) \ type name(type1 arg1) \ { \ long __res; \ __asm__ volatile ("int $0x80" \ : "=a" (__res) \ : "0" (__NR_##name),"b" ((long)(arg1))); \ if (__res >= 0) \ return (type) __res; \ errno = -__res; \ return -1; \ }
When expanded, this become the code int setuid(uid_t uid) { long __res; __asm__ volatile ("int $0x80" \ : "=a" (__res) \ : "0" (__NR_setuid), "b" ((long)(uid))); if (__res >= 0 ) return (int) __res; errno = -__res; return -1; }
It's pretty easy to see how the cleanup code converts into assembly, but the setup code eluded me until I figured out the following: "=a" (__res) means the result comes back in %eax "0" (__NR_setuid) means put the system call number into %eax on entry "b" ((long)(uid) means put the first argument into %ebx on entry 使用额外参数的 syscallX 宏使用 %ecx、%edx、%esi 和 %edi 来保存通过调用传递的额外值。 |