dup2 on x64
duplicate a file descriptor
sys_dup2(unsigned int oldfd, unsigned int newfd)
| number | 33 (0x21) |
|---|---|
| abi | common |
| entry point | sys_dup2 |
| since | Linux 3.3 or earlier (3.3 is the first release scanned for x64) |
| man page | dup2(2) |
| source | elixir.bootlin.com |
Registers
| register | value |
|---|---|
rax | 33, the syscall number |
rdi | unsigned int oldfd |
rsi | unsigned int newfd |
rax | the return value |
Then run syscall to enter the kernel.
The syscall instruction clobbers rcx (the return address lands in rcx) and r11 (the flags land in r11).
Since Linux 5.14 the kernel reads the syscall number from eax and ignores the upper 32 bits of rax. Older kernels fail with ENOSYS when those bits are not zero, and mov eax, N clears them, so it works on both.
Same syscall on other architectures
- x86 63
- arm 63
- ia32 63
- x32 1073741857
- ppc64 63
- ppc 63
- s390x 63
- mips64 5032
- mipsn32 6032
- mipso32 4063
- alpha 90
- blackfin 63, removed in 4.17
- cris 63, removed in 4.17
- frv 63, removed in 4.17
- ia64 1070, removed in 6.7
- m32r 63, removed in 4.17
- m68k 63
- microblaze 63
- mn10300 63, removed in 4.17
- parisc 63
- score 1041, removed in 4.17
- sh 63
- sparc 90
- sparc64 90
- xtensa 11
Get this as JSON: https://api.syscall.sh/v2/syscalls/x64/dup2